MantisBT - CMake
View Issue Details
0015365CMakeCMakepublic2015-01-22 10:572015-07-08 08:57
Kevin Wojniak 
 
normalfeatureN/A
closedduplicate 
CMake 3.1 
 
0015365: Allow per-language COMPILE_OPTIONS via generator expression
CMake provides CMAKE_CXX_FLAGS, but this is global. It'd be good to have a per-target CXX flags ability. Possibly a macro such as target_compile_options_cxx() and target_compile_options_c(), or a way to do this with generator expressions (doesn't seem to have a LANG property for sources).
No tags attached.
duplicate of 0014857closed Stephen Kelly Allow COMPILE_OPTIONS to vary by source file language 
Issue History
2015-01-22 10:57Kevin WojniakNew Issue
2015-01-22 11:16Paul SmithNote Added: 0037776
2015-01-22 11:37Brad KingNote Added: 0037777
2015-01-22 11:37Brad KingStatusnew => acknowledged
2015-01-22 11:37Brad KingSummaryProvide per-target CXX/C flags => Allow per-language COMPILE_OPTIONS via generator expression
2015-01-22 11:39Brad KingNote Added: 0037778
2015-01-22 11:39Kevin WojniakNote Added: 0037779
2015-01-22 11:49Kevin WojniakNote Added: 0037780
2015-01-22 17:04Stephen KellyRelationship addedduplicate of 0014857
2015-01-23 15:08Brad KingNote Added: 0037796
2015-01-23 15:08Brad KingStatusacknowledged => resolved
2015-01-23 15:08Brad KingResolutionopen => duplicate
2015-07-08 08:57Robert MaynardNote Added: 0039033
2015-07-08 08:57Robert MaynardStatusresolved => closed

Notes
(0037776)
Paul Smith   
2015-01-22 11:16   
I'm not sure exactly what you're looking for, but there are lots of properties that can be set via set_property() on a per-target, per-source file, etc. level including COMPILE_FLAGS and others:

http://www.cmake.org/cmake/help/v3.0/manual/cmake-properties.7.html [^]

Is there a specific thing you can't do?
(0037777)
Brad King   
2015-01-22 11:37   
The COMPILE_OPTIONS property and target_compile_options command I think are the right place to add per-language support, but it is likely best done using generator expressions. I've updated the issue subject line accordingly

The cmLocalGenerator::AddCompileOptions method:

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

which generators call to collect the per-target flags does have a "lang" parameter. However, it is not passed on to cmTarget::GetCompileOptions which would be needed to support a language-check generator expression.
(0037778)
Brad King   
2015-01-22 11:39   
Re 0015365:0037777: With updates starting there it should be possible to implement a language-check generator expression. Perhaps something like:

target_compile_options(MyTarget PRIVATE
  $<$<LANG:C>:${MY_C_FLAGS}>
  $<$<LANG:CXX>:${MY_CXX_FLAGS}>
  )
(0037779)
Kevin Wojniak   
2015-01-22 11:39   
GCC will generate errors and/or warnings for C++ flags on C files.

For example this file (test.cpp and test.c contents don't matter):

cmake_minimum_required(VERSION 3.1)
project(demo)
add_executable(demo test.cpp test.c)
target_compile_options(demo PRIVATE -Woverloaded-virtual -Werror -std=c++11)

on GCC 4.8.2 will generate:

cc1: warning: command line option ‘-Woverloaded-virtual’ is valid for C++/ObjC++ but not for C [enabled by default]
cc1: error: command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C [-Werror]
(0037780)
Kevin Wojniak   
2015-01-22 11:49   
Brad, that would be perfect!
(0037796)
Brad King   
2015-01-23 15:08   
Resolving as duplicate of 0014857.
(0039033)
Robert Maynard   
2015-07-08 08:57   
Closing resolved issues that have not been updated in more than 4 months.