[CMake] Handling of generator expressions
Oleksii Vilchanskyi
oleksii.vilchanskyi at gmail.com
Wed Nov 22 05:40:05 EST 2017
UPD. forgot to CC the mailing list
Hi,
I am also confused by this and decided to test it. I found a long thread
from 2012
<https://cmake.org/pipermail/cmake-developers/2012-August/thread.html#16509>
that discusses list expansion issues. It doesn't tell though whether the
patch was mainlined.
tldr; if you quote the whole genexpr, the list will expand properly.
Below is the working code.
----------------------------------------------------------------------
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(recipe-09 CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
list(APPEND CXX_BASIC_FLAGS "-g3" "-O1")
include(CheckCXXCompilerFlag)
set(ASAN_FLAGS "-fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_REQUIRED_FLAGS ${ASAN_FLAGS})
check_cxx_compiler_flag(${ASAN_FLAGS} asan_works)
unset(CMAKE_REQUIRED_FLAGS)
add_executable(asan-example asan-example.cpp)
string(REPLACE " " ";" _asan_flags ${ASAN_FLAGS})
target_compile_options(asan-example
PUBLIC
${CXX_BASIC_FLAGS}
$<$<BOOL:${asan_works}>:${_asan_flags}>
)
target_link_libraries(asan-example
PUBLIC
"$<$<BOOL:${asan_works}>:${_asan_flags}>" # <-- NOTE THIS
)
----------------------------------------------------------------------
On 19.11.2017 19:16, Roberto Di Remigio wrote:
> Hello,
> I am trying to compile the following code snippet:
>
> int main(int argc, char ** argv) {
> int stack_array[100];
> stack_array[1] = 0;
> return stack_array[argc + 100]; // BOOM
> }
>
> The followin CMakeLists.txt works fine, but you can notice that
> target_compile_options takes a list, while target_link_libraries takes a
> string. Always using the string will result in a compilation error,
> because the compiler flags are passed quoted to the compiler. Always
> using the list doesn't work either, because the generator expression is
> evaluated as $<1:-fsanitize=address -fno-omit-frame-pointer> and then
> passed as-is to the linker, which doesn't know what to do with it.
> Is this supposed to be like this? If yes, what's the rationale?
> Thanks in advance!
> Roberto
>
> cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
>
> project(recipe-09 CXX)
>
> set(CMAKE_CXX_STANDARD 11)
> set(CMAKE_CXX_EXTENSIONS OFF)
> set(CMAKE_CXX_STANDARD_REQUIRED ON)
>
> list(APPEND CXX_BASIC_FLAGS "-g3" "-O1")
>
> include(CheckCXXCompilerFlag)
>
> set(ASAN_FLAGS "-fsanitize=address -fno-omit-frame-pointer")
> set(CMAKE_REQUIRED_FLAGS ${ASAN_FLAGS})
> check_cxx_compiler_flag(${ASAN_FLAGS} asan_works)
> unset(CMAKE_REQUIRED_FLAGS)
>
> add_executable(asan-example asan-example.cpp)
> string(REPLACE " " ";" _asan_flags ${ASAN_FLAGS})
> target_compile_options(asan-example
> PUBLIC
> ${CXX_BASIC_FLAGS}
> $<$<BOOL:${asan_works}>:${_asan_flags}>
> )
> target_link_libraries(asan-example
> PUBLIC
> $<$<BOOL:${asan_works}>:${ASAN_FLAGS}>
> )
>
>
--
Regards,
Oleksii Vilchanskyi
PGP:0x8D3A0E046BDE941F2A53867CE3FD952D48C0B338
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <http://public.kitware.com/pipermail/cmake/attachments/20171122/8a53d6c2/attachment-0001.sig>
More information about the CMake
mailing list