[cmake-developers] Should try_run have a timeout value? [demo CMakeLists.txt in body of email]

David Cole dlrdave at aol.com
Thu Jun 12 08:59:20 EDT 2014


#
# CMakeLists.txt
#
# PROBLEM:
#
# This file demonstrates a problem with CMake 3.0 and Visual Studio
# compiler x64 builds... If configured for a build with the x64
# compiler, CMake will hang until user intervention with a
# "cmTryCompileExec123...789.exe has stopped working"
# crash dialog.
#
# The "problem" has existed forever, it's not new to CMake 3.0
#
# QUESTION:
#
#   Should try_run have a timeout value, defaulting to a few seconds (or
#   something more reasonable than NEVER) or is there a way for CMake to
#   detect this condition and kill it as an unsuccessful try_run...?
#
cmake_minimum_required(VERSION 3.0)
project(subtle_pointer_error C)

set(source_file "${CMAKE_CURRENT_BINARY_DIR}/spe.c")

file(WRITE ${source_file}.in
# OMG - long bracket argument syntax is beautiful for including
# unescaped entire other http://sscce.org/ files within a
# CMakeLists.txt!!
[=[
/*
  The problem with this code (and why it crashes on Windows x64
  builds) is malloc is used without declaring it, so the C compiler
  assumes it is extern returning int... But int is too small for a
  pointer on x64 builds, so the value that ends up in "ptr" is not
  the whole pointer, but only sizeof(int) bytes of it. The fix is
  obviously to include the header that declares malloc (and memcpy,
  and anything else used), but this was hard to diagnose, and
  annoying as heck on automated dashboard machines -- a failed
  try_run due to excessive attempted run time would have been a most
  welcome assist.

  Fix this problem and prove the crash goes away by adding:
    #include <stdlib.h>
    #include <string.h>
*/

#include <stdio.h>

int main(void)
{
  double value = 1234.56;
  void *ptr = malloc(sizeof(value));
  printf("before memcpy\n");
  fflush(stdout);
  memcpy(ptr, &value, sizeof(value));
  printf("after memcpy\n");
  return 0;
}
]=]
)

configure_file(${source_file}.in ${source_file} COPYONLY)

try_run(
  run_result
  compile_result
  ${CMAKE_CURRENT_BINARY_DIR}/TryRun001
  ${source_file}
  COMPILE_OUTPUT_VARIABLE compile_output
  RUN_OUTPUT_VARIABLE run_output
  )

message("compile_result='${compile_result}'")
message("run_result='${run_result}'")
message("compile_output='${compile_output}'")
message("run_output='${run_output}'")

add_executable(spe ${source_file})

#
# Thanks for your opinions, and taking a look...
#
#    David C.
#



More information about the cmake-developers mailing list