[CMake] add_custom_command using CMAKE_CXX_FLAGS

John Smith codeforgenator at gmail.com
Sun Aug 23 21:03:22 EDT 2009


Hi,

I have browsed the list for a while trying to figure this but with  
little success and my own testing is yielding confusing results.

I am using the following test case in an attempt to add a custom  
command to build an assembly file. The assembly file should be  
compiled with the C++ compiler driver (which in turn should invoke the  
assembler after pre-processing), and I am building up a  
CMAKE_CXX_FLAGS variable from separate pieces:

# -*- CMake -*-
cmake_minimum_required(VERSION 2.6)

project(test)
set(CMAKE_VERBOSE_MAKEFILE ON)

set(CMAKE_CXX_FLAGS -m32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

message (STATUS "CMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}")

file (GLOB ASM_SRCS bar.S)
foreach (src ${ASM_SRCS})
    get_filename_component (FILE_BASE ${src} NAME_WE)
    set (SRC ${src})
    set (OBJ ${CMAKE_CURRENT_BINARY_DIR}/${FILE_BASE}.o)
    add_custom_command (OUTPUT ${OBJ}
        MAIN_DEPENDENCY ${SRC}
        COMMAND ${CMAKE_CXX_COMPILER}
        ARGS ${CMAKE_CXX_FLAGS} -o ${OBJ} -c ${SRC}
        MAIN_DEPENDENCY ${src})
        set (ASM_OBJS ${ASM_OBJS} ${OBJ})
endforeach(src)

add_library(test SHARED foo.cpp bar.S)

The above gives this:

[...]
[ 50%] Generating bar.o
/usr/bin/c++ -m32\ -pthread -o /tmp/build-test/bar.o -c /tmp/test/bar.S
cc1: error: unrecognized command line option "-m32 -pthread"
[...]

I see that the space separating the two options is escaped, I assume  
as a result of defining the variable as a string. However, in this:

$ nice make foo.o
[...]
[100%] Building CXX object CMakeFiles/test.dir/foo.cpp.o
/usr/bin/c++   -Dtest_EXPORTS -m32 -pthread -fPIC   -o CMakeFiles/ 
test.dir/foo.cpp.o -c /tmp/test/foo.cpp

CMAKE_CXX_FLAGS appears fine in this rule. I assume it is processed  
differently when this rule is generated.

My question is: how should I go about hard-coding my rule? I need to  
pass the language-specific flags as well as the include directories  
(same with which I compile all C++ sources in the project).

Thanks for the help.


More information about the CMake mailing list