[CMake] CMAKE_C*_FLAGS undefined.

Óscar Fuentes ofv at wanadoo.es
Fri Apr 3 23:19:38 EDT 2009


Hi Philip.

Philip Lowman <philip at yhbt.com> writes:

>> I have this:
>>
>> add_custom_command(OUTPUT ${LLVM_CONFIG}
>>  COMMAND echo "flags: ${CMAKE_CXX_FLAGS} ${CMAKE_CPP_FLAGS}
>> ${CMAKE_C_FLAGS}"
>> .... more stuff ....
>>
>>
>> When the custom command executes, this is the output:
>>
>> flags:
>>
>> i.e. the variables are empty, although I previously used add_definitions
>> to add flags and the makefile uses the optimization flags that
>> corresponds to the selected build type.
>>
>> How can I access the effective values of the flags used when the
>> makefile is generated?
>
> CMAKE_CXX_FLAGS / CMAKE_C_FLAGS is empty by default on Linux unless you've
> modified it which may have been what happened.  The build type specific
> complilation flags are stored in CMAKE_CXX_FLAGS_<config_type>.
> CMAKE_CXX_FLAGS is a base that gets prepended to all of the build solutions.
>
> The command pasted below works for me on CMake 2.6.4 RC3 Win32 using the
> MinGW generator (it outputs the flags used to compile during a Release
> build).  If something like this doesn't work for you can you reply with your
> version of CMake and generator you're using?
>
> add_custom_target(foo COMMAND C:/cygwin/bin/echo.exe flags:
> ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE})

CMAKE_CXX_FLAGS_RELEASE works, but it is not enough. It still misses the
definitions and other options which are passed to the
compiler. CMAKE_CXX_FLAGS, as noted on my previous message, is empty.

I'm using 2.6.1 on Linux, gnu makefiles.

> Also, to possibly anticipate your next question: On Makefile generators you
> should be able to get the current build type being used by the end user by
> referencing CMAKE_BUILD_TYPE.

CMAKE_BUILD_TYPE contains the correct build type.

To expand a bit, this is what I'm trying to do:

add_custom_command(OUTPUT ${LLVM_CONFIG}
  COMMAND echo "flags: ${CMAKE_CXX_FLAGS_DEBUG} ${CMAKE_CPP_FLAGS} ${CMAKE_C_FLAGS}"
  COMMAND echo 's!@LLVM_CPPFLAGS@!${CMAKE_CPP_FLAGS}!' > temp.sed
  COMMAND echo 's!@LLVM_CFLAGS@!${CMAKE_C_FLAGS}!' >> temp.sed
  COMMAND echo 's!@LLVM_CXXFLAGS@!${CMAKE_CXX_FLAGS}!' >> temp.sed
  # TODO: Use general flags for linking! not just for shared libs:
  COMMAND echo 's!@LLVM_LDFLAGS@!${CMAKE_SHARED_LINKER_FLAGS}!' >> temp.sed
  COMMAND echo 's!@LIBS@!!' >> temp.sed                    # TODO: System libs
  COMMAND echo 's!@LLVM_BUILDMODE@!${CMAKE_BUILD_TYPE}!' >> temp.sed
  COMMAND sed -f temp.sed < ${LLVM_CONFIG_IN} > ${LLVM_CONFIG}
# some stuff omitted
  DEPENDS ${FINAL_LIBDEPS} ${LLVM_CONFIG_IN}
  COMMENT "Building llvm-config script."
  )

This builds an script that knows the compiler and linker options used
for building the libraries. Later, other (third party) software can
query the script and check those options for ensuring compatibility with
the libraries.

`configure_file' is not okay because the "configuration" must be
executed at build time. CMAKE_CXX_FLAGS and co. are also empty at cmake
time though, so `configure_file' would have the same problem.

Thanks for looking into this.

-- 
Oscar



More information about the CMake mailing list