View Issue Details Jump to Notes ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0014066CMakeCMakepublic2013-04-08 16:192016-01-26 10:02
ReporterAlexandru Ciobanu 
Assigned ToBrad King 
PrioritynormalSeverityminorReproducibilityalways
StatusclosedResolutionfixed 
PlatformOSLinuxOS Version
Product VersionCMake 2.8.10.2 
Target VersionCMake 3.2Fixed in VersionCMake 3.2 
Summary0014066: Provided CMAKE_*_LINKER_FLAGS ignored during configuration step
DescriptionIt 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
Additional Information  1. cd build
  2. CC=clang cmake -DCMAKE_C_FLAGS="-fsanitize=thread -fPIE" -DCMAKE_EXE_LINKER_FLAGS="-pie" ..
TagsNo tags attached.
Attached Filesgz file icon cmake.link.flags.repro.tar.gz [^] (421 bytes) 2013-04-08 16:19
log file icon full.error.log [^] (2,415 bytes) 2013-04-08 16:20

 Relationships
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 0015934closedKitware Robot There's no way to test whether a given *linker* flag works 

  Notes
(0032782)
Brad King (manager)
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 (manager)
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 (manager)
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 (reporter)
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 (manager)
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 (manager)
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 (manager)
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 (reporter)
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 (manager)
2015-11-02 09:13

Closing resolved issues that have not been updated in more than 4 months.

 Issue History
Date Modified Username Field Change
2013-04-08 16:19 Alexandru Ciobanu New Issue
2013-04-08 16:19 Alexandru Ciobanu File Added: cmake.link.flags.repro.tar.gz
2013-04-08 16:20 Alexandru Ciobanu File Added: full.error.log
2013-04-08 16:47 Brad King Note Added: 0032782
2013-04-08 16:47 Brad King Status new => backlog
2014-11-25 09:46 Brad King Note Added: 0037278
2014-11-25 09:46 Brad King Relationship added has duplicate 0015264
2014-11-25 09:50 Brad King Note Added: 0037279
2014-11-26 06:25 Evgeniy Stepanov Note Added: 0037290
2014-11-26 09:48 Brad King Note Added: 0037294
2014-12-01 08:36 Brad King Relationship added related to 0015235
2014-12-04 10:56 Brad King Assigned To => Brad King
2014-12-04 10:56 Brad King Status backlog => assigned
2014-12-04 10:56 Brad King Target Version => CMake 3.2
2014-12-04 10:57 Brad King Note Added: 0037379
2014-12-08 08:57 Brad King Status assigned => resolved
2014-12-08 08:57 Brad King Resolution open => fixed
2014-12-08 08:57 Brad King Fixed in Version => CMake 3.2
2014-12-09 10:53 Brad King Note Added: 0037426
2015-01-19 19:22 Laurent Demailly Note Added: 0037753
2015-03-18 10:01 Alexander Frolov Note Added: 0038235
2015-03-18 10:08 Alexander Frolov Note Deleted: 0038235
2015-11-02 09:13 Robert Maynard Note Added: 0039735
2015-11-02 09:13 Robert Maynard Status resolved => closed
2016-01-26 10:02 Brad King Relationship added related to 0015934


Copyright © 2000 - 2018 MantisBT Team