[CMake] help using try_run with custom build type

George Neill georgen at neillnet.com
Mon Jun 29 23:15:04 EDT 2009


Hi folks,

I apologize for the long email, but, I am having some troubles with
try_run (cmake 2.6.4/sun gcc/Solaris 10).  I have created my own
CMAKE_BUILD_TYPE called TEST and have set CMAKE_C_FLAGS_TEST to "-m64
-std=c99".

Here's the .c code I am testing with,

-bash-3.00$ cat !$
cat ../platform/HAVE_ISFINITE.c
#include <math.h>

int main(){
  float f = 1.3;
  return isfinite(f);
}

Here's the CMake script.

-bash-3.00$ cat ../CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
project(test C)

macro(MY_C_COMPILE_AND_RUN_CHECK _var)

  message(STATUS "Compiling ${_var}")

  if(CMAKE_REQUIRED_LIBRARIES)
    set(USING_THESE_LIBS
      "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
  else(CMAKE_REQUIRED_LIBRARIES)
    set(USING_THESE_LIBS)
  endif(CMAKE_REQUIRED_LIBRARIES)

  try_run(${_var} "${_var}_COMPILE"
    ${test_BINARY_DIR}
    ${test_SOURCE_DIR}/platform/${_var}.c
    COMPILE_DEFINITIONS
      "${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE}} ${CMAKE_REQUIRED_DEFINITIONS}"
    CMAKE_FLAGS
      "${USING_THESE_LIBS}"
    OUTPUT_VARIABLE ${_var}_OUTPUT
  )

message(STATUS "CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE}:
${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE}}")
message(STATUS "${_var} : ${${_var}}")
message(STATUS "${_var}_COMPILE : ${${_var}_COMPILE}")
message(STATUS "${_var}_OUTPUT : ${${_var}_OUTPUT}")

  if(${${_var}})
    set(${_var} 1)
    message(STATUS "Compiling ${_var} - found")
  else(${${_var}})
    set(${_var})
    message(STATUS "Compiling ${_var} - not found")
  endif(${${_var}})

endmacro(MY_C_COMPILE_AND_RUN_CHECK _var)


set(CMAKE_C_FLAGS_TEST "-m64 -std=c99")


# do the compile test
SET(CMAKE_REQUIRED_LIBRARIES "-lm")
MY_C_COMPILE_AND_RUN_CHECK(HAVE_ISFINITE)
SET(CMAKE_REQUIRED_LIBRARIES)

MESSAGE(STATUS "HAVE_ISFINITE: ${HAVE_ISFINITE}")


Here's an execution of the cmake script (above) ...

-bash-3.00$ !cmake
cmake -DCMAKE_BUILD_TYPE=TEST ../
-- The C compiler identification is GNU
-- Check for working C compiler: /opt/gcc/bin/gcc
-- Check for working C compiler: /opt/gcc/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Compiling HAVE_ISFINITE
-- CMAKE_C_FLAGS_TEST: -m64 -std=c99
-- HAVE_ISFINITE :
-- HAVE_ISFINITE_COMPILE : FALSE
-- HAVE_ISFINITE_OUTPUT : Change Dir:
/export/home/gneill/cmake/build/CMakeFiles/CMakeTmp

Run Build Command:/usr/ccs/bin/make "cmTryCompileExec/fast"
/usr/ccs/bin/make -f CMakeFiles/cmTryCompileExec.dir/build.make
CMakeFiles/cmTryCompileExec.dir/build
/opt/cmake-2.6.4-SunOS-sparc/bin/cmake -E cmake_progress_report
/export/home/gneill/cmake/build/CMakeFiles/CMakeTmp/CMakeFiles 1
Building C object CMakeFiles/cmTryCompileExec.dir/HAVE_ISFINITE.c.o
/opt/gcc/bin/gcc   -m64 -std=c99 -o
CMakeFiles/cmTryCompileExec.dir/HAVE_ISFINITE.c.o   -c
/export/home/gneill/cmake/platform/HAVE_ISFINITE.c
Linking C executable cmTryCompileExec
/opt/cmake-2.6.4-SunOS-sparc/bin/cmake -E cmake_link_script
CMakeFiles/cmTryCompileExec.dir/link.txt --verbose=1
/opt/gcc/bin/gcc       -fPIC
CMakeFiles/cmTryCompileExec.dir/HAVE_ISFINITE.c.o  -o cmTryCompileExec
 -lm
ld: fatal: file CMakeFiles/cmTryCompileExec.dir/HAVE_ISFINITE.c.o:
wrong ELF class: ELFCLASS64
ld: fatal: File processing errors. No output written to cmTryCompileExec
collect2: ld returned 1 exit status
*** Error code 1
make: Fatal error: Command failed for target `cmTryCompileExec'
Current working directory /export/home/gneill/cmake/build/CMakeFiles/CMakeTmp
*** Error code 1
make: Fatal error: Command failed for target `cmTryCompileExec/fast'

-- Compiling HAVE_ISFINITE - not found
-- HAVE_ISFINITE:
-- Configuring done
-- Generating done
-- Build files have been written to: /export/home/gneill/cmake/build


It appears to fail because the compiler options do not make it in when
creating the executable ... is there something silly I have missed?
Also, I was wondering why TRY_RUN doesn't use the default flags
defined from CMAKE_BUILD_TYPE ?

If I run it in this way, the -m64 flag seems to make it when linking
the executable (you can see the -m64 is added) ... and it does find
the isfinite function.  However, doing it in this fashion, I have to
add the same flags in different spots.

-bash-3.00$ CFLAGS="-m64" cmake -DCMAKE_BUILD_TYPE=TEST ../
-- The C compiler identification is GNU
-- Check for working C compiler: /opt/gcc/bin/gcc
-- Check for working C compiler: /opt/gcc/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Compiling HAVE_ISFINITE
-- CMAKE_C_FLAGS_TEST: -m64 -std=c99
-- HAVE_ISFINITE : 1
-- HAVE_ISFINITE_COMPILE : TRUE
-- HAVE_ISFINITE_OUTPUT : Change Dir:
/export/home/gneill/cmake/build/CMakeFiles/CMakeTmp

Run Build Command:/usr/ccs/bin/make "cmTryCompileExec/fast"
/usr/ccs/bin/make -f CMakeFiles/cmTryCompileExec.dir/build.make
CMakeFiles/cmTryCompileExec.dir/build
/opt/cmake-2.6.4-SunOS-sparc/bin/cmake -E cmake_progress_report
/export/home/gneill/cmake/build/CMakeFiles/CMakeTmp/CMakeFiles 1
Building C object CMakeFiles/cmTryCompileExec.dir/HAVE_ISFINITE.c.o
/opt/gcc/bin/gcc   -m64      -m64 -std=c99 -o
CMakeFiles/cmTryCompileExec.dir/HAVE_ISFINITE.c.o   -c
/export/home/gneill/cmake/platform/HAVE_ISFINITE.c
Linking C executable cmTryCompileExec
/opt/cmake-2.6.4-SunOS-sparc/bin/cmake -E cmake_link_script
CMakeFiles/cmTryCompileExec.dir/link.txt --verbose=1
/opt/gcc/bin/gcc   -m64     -fPIC
CMakeFiles/cmTryCompileExec.dir/HAVE_ISFINITE.c.o  -o cmTryCompileExec
 -lm

-- Compiling HAVE_ISFINITE - found
-- HAVE_ISFINITE: 1
-- Configuring done
-- Generating done
-- Build files have been written to: /export/home/gneill/cmake/build


TIA,
George


More information about the CMake mailing list