#
# 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.
#

-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/cgi-bin/mailman/listinfo/cmake-developers

Reply via email to