MantisBT - CMake
View Issue Details
0014066CMakeCMakepublic2013-04-08 16:192016-01-26 10:02
Alexandru Ciobanu 
Brad King 
normalminoralways
closedfixed 
Linux
CMake 2.8.10.2 
CMake 3.2CMake 3.2 
0014066: Provided CMAKE_*_LINKER_FLAGS ignored during configuration step
It seems that if one provides -DCMAKE_C_FLAGS to cmake, these flags are used during the try-compile steps as part of configuration step, while the provided linker flags like CMAKE_EXE_LINKER_FLAGS are ignored during the try-compile steps.

Interestingly, providing the flags via LDFLAGS environment variable, works.

Using the attached tiny repro case:
  1. cd build
  2. CC=clang cmake -DCMAKE_C_FLAGS="-fsanitize=thread -fPIE" -DCMAKE_EXE_LINKER_FLAGS="-pie" ..

results in the following error (full error log is attached):

  The C compiler "clang" is not able to compile a simple test program.
   [...]
  /home/builder/rogue_nightly_build/build_utils/llvm/llvm-relass-install/bin/clang
  -fsanitize=thread -fPIE
  CMakeFiles/cmTryCompileExec998602180.dir/testCCompiler.c.o -o
  cmTryCompileExec998602180 -rdynamic

  clang-3: error: invalid argument '-fsanitize=thread' only allowed with
  '-pie'

   [...]


Alternatively, if one uses LDFLAGS for the same purpose, there is no error:
  1. cd build
  2. LDFLAGS="-pie" CC=clang cmake -DCMAKE_C_FLAGS="-fsanitize=thread -fPIE" ..

results in a successful configuration.

We found this case because we use clang's ThreadSanitizer, which is a tool that detects race condition with threads and has the following requirement:
  - compiler should use -fsanitize=thread -fPIE
  - linker should use -fsanitize=thread -pie
  1. cd build
  2. CC=clang cmake -DCMAKE_C_FLAGS="-fsanitize=thread -fPIE" -DCMAKE_EXE_LINKER_FLAGS="-pie" ..
No tags attached.
has duplicate 0015264closed  check_cxx_compiler_flag fails to use linker flags 
related to 0015235closed  CMAKE_LD_FLAGS from toolchain is ignored by compiler's check 
related to 0015934closed Kitware Robot There's no way to test whether a given *linker* flag works 
gz cmake.link.flags.repro.tar.gz (421) 2013-04-08 16:19
https://public.kitware.com/Bug/file/4717/cmake.link.flags.repro.tar.gz
log full.error.log (2,415) 2013-04-08 16:20
https://public.kitware.com/Bug/file/4718/full.error.log
Issue History
2013-04-08 16:19Alexandru CiobanuNew Issue
2013-04-08 16:19Alexandru CiobanuFile Added: cmake.link.flags.repro.tar.gz
2013-04-08 16:20Alexandru CiobanuFile Added: full.error.log
2013-04-08 16:47Brad KingNote Added: 0032782
2013-04-08 16:47Brad KingStatusnew => backlog
2014-11-25 09:46Brad KingNote Added: 0037278
2014-11-25 09:46Brad KingRelationship addedhas duplicate 0015264
2014-11-25 09:50Brad KingNote Added: 0037279
2014-11-26 06:25Evgeniy StepanovNote Added: 0037290
2014-11-26 09:48Brad KingNote Added: 0037294
2014-12-01 08:36Brad KingRelationship addedrelated to 0015235
2014-12-04 10:56Brad KingAssigned To => Brad King
2014-12-04 10:56Brad KingStatusbacklog => assigned
2014-12-04 10:56Brad KingTarget Version => CMake 3.2
2014-12-04 10:57Brad KingNote Added: 0037379
2014-12-08 08:57Brad KingStatusassigned => resolved
2014-12-08 08:57Brad KingResolutionopen => fixed
2014-12-08 08:57Brad KingFixed in Version => CMake 3.2
2014-12-09 10:53Brad KingNote Added: 0037426
2015-01-19 19:22Laurent DemaillyNote Added: 0037753
2015-03-18 10:01Alexander FrolovNote Added: 0038235
2015-03-18 10:08Alexander FrolovNote Deleted: 0038235
2015-11-02 09:13Robert MaynardNote Added: 0039735
2015-11-02 09:13Robert MaynardStatusresolved => closed
2016-01-26 10:02Brad KingRelationship addedrelated to 0015934

Notes
(0032782)
Brad King   
2013-04-08 16:47   
I suggest using environment variables for custom compiler and linker flags:

 CC=clang CFLAGS="-fsanitize=thread -fPIE" LDFLAGS="-pie" cmake ...

(0037278)
Brad King   
2014-11-25 09:46   
Issue 0015264 raises another case where CMAKE_EXE_LINKER_FLAGS, CMAKE_MODULE_LINKER_FLAGS, and CMAKE_SHARED_LINKER_FLAGS should be passed through to try_compile.
(0037279)
Brad King   
2014-11-25 09:50   
The CMAKE_<LANG>_FLAGS variable is passed to try-compiled projects by cmCoreTryCompile::TryCompileCode:

 http://www.cmake.org/gitweb?p=cmake.git;a=blob;f=Source/cmCoreTryCompile.cxx;hb=v3.0.2#l323 [^]

The linker flag variables could also be passed here.
(0037290)
Evgeniy Stepanov   
2014-11-26 06:25   
Do you mean CC/CFLAGS/LDFLAGS as a workaround, or as a general recommended way of passing custom flags? LLVM docs recommend CMAKE_*_FLAGS.
(0037294)
Brad King   
2014-11-26 09:48   
Re 0014066:0037290: I personally always use the environment variables to initialize the compiler and flags selection for a new build tree. However, setting the flag cache entries with -DCMAKE_CXX_FLAGS=... on the command line is supported and expected to work.

There is no good reason try_compile does not pass on the linker flags too. It was just an oversight way back when it was first implemented and the LDFLAGS workaround has gotten most people along. This can be fixed, and if necessary a policy can ensure compatibility with existing projects.
(0037379)
Brad King   
2014-12-04 10:57   
I've implemented use of CMAKE_EXE_LINKER_FLAGS during try_compile and added policy CMP0056 for compatibility:

 try_compile: Pass linker flags into test project
 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=88eb5824 [^]

Please try it out.
(0037426)
Brad King   
2014-12-09 10:53   
On 12/08/2014 06:50 PM, Alexandru Ciobanu wrote:
> I was able to test the fix for this bug, and confirm that CMP0056
> works for both OLD and NEW settings:
> - NEW setting: linker flags passed to try compile
> - OLD setting: linker flags not passed to try compile (old behaviour)
(0037753)
Laurent Demailly   
2015-01-19 19:22   
wonder if that's the same or similar issue as CHECK_CXX_SOURCE_COMPILES not using requested c++11 flags ( http://www.cmake.org/Bug/view.php?id=15361 [^] ) - will try rebuilding cmake from source to see if that changed
(0039735)
Robert Maynard   
2015-11-02 09:13   
Closing resolved issues that have not been updated in more than 4 months.