[CMake] CMake flags to TRY_RUN

James Bigler bigler at cs.utah.edu
Tue May 22 17:33:27 EDT 2007


James Bigler wrote:
> I have a some code that I need to test and I need to feed it a default 
> set of compiler flags.  I'm trying to set CMAKE_C_FLAGS via the -D 
> system to override the default ones CMake provides.
> 
>   SET(COMPILE_TEST_SOURCE ${CMAKE_BINARY_DIR}/test/compile-test.c)
>   FILE(WRITE ${COMPILE_TEST_SOURCE} "int main() { return 0; }\n")\
>   SET(ARCH "i686")
>   MESSAGE("Testing ARCH = ${ARCH}")
>   SET(ARCH_FLAG "-march=${ARCH} -mtune=${ARCH}")
>   SET(COMPILER_ARGS "${ARCH_FLAG} ${C_FLAGS_RELEASE} ${C_FLAGS}")
>   TRY_RUN(RUN_RESULT_VAR COMPILE_RESULT_VAR
>     ${CMAKE_BINARY_DIR}/test ${COMPILE_TEST_SOURCE}
>     CMAKE_FLAGS "-DCMAKE_C_FLAGS:STRING=${COMPILER_FLAGS}"
>     OUTPUT_VARIABLE OUTPUT
>     )
>   MESSAGE("RUN_RESULT_VAR     = ${RUN_RESULT_VAR}")
>   MESSAGE("COMPILE_RESULT_VAR = ${COMPILE_RESULT_VAR}")
>   MESSAGE("OUTPUT             = ${OUTPUT}")
> 
> This builds and runs the code, but it has two problems.
> 
> 1. My flags don't get passed in.

This was a two fold problem.  First, I had a typo.  COMPILER_FLAGS 
should have been COMPILER_ARGS.  Second, you want to specify compiler 
flags with -DCOMPILE_DEFINITIONS like:

       -DCOMPILE_DEFINITIONS:STRING=${COMPILER_ARGS}


> 2. CMake later complains about my temporary file:
> 
> CMake Error: File 
> /home/scinew/bigler/manta/trunk/opt-4.0.2/test/compile-test.c is written 
> by WRITE_FILE (or FILE WRITE) command and should not be used as input to 
> CMake. Please use CONFIGURE_FILE to be safe. Refer to the note next to 
> FILE WRITE command.
> 
> I'm using cmake 2.4.6 and 2.2.3.

This is still broken for CMake 2.4.6 and 2.2.3.  If I change the pah to 
use CMakeTmp instead of test, CMake 2.4.6 works just fine.

Is there a way around this?

Does TRY_RUN require the source file to be somehow generated other than 
FILE(WRITE)?

James


More information about the CMake mailing list