Accessing -fPIC and -std settings within CMake definitions

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Accessing -fPIC and -std settings within CMake definitions

Stephen Morris
I'm setting up a custom build step to build a precompiled header in gcc (yes I know that native support is coming with CMake 3.16, but I need to get something working on an older version).

My approach is basically to set up a custom command thus:

set_target_properties(mytarget PRIVATE CXX_STANDARD 17)
set_target_properties(mytarget PRIVATE  POSITION_INDEPENDENT CODE_1)
set(CMAKE_VERBOSE_MAKEFILE TRUE)
...
...
set(CXX_FLAGS ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG})   .. or whatever, depending on the current configuration..
get_target_property(compile_options, mytarget, COMPILE_OPTIONS)
add_custom_command(OUTPUT myheader.h.gch
                                           COMMAND ${CMAKE_CXX_COMPILER} ${CXX_FLAGS} ${compile_options} -fPIC -std=gnu++17 -c myheader.h -o myheader.h.gch
                                           DEPENDS myheader.h)
add_custom_target(BuildMyPCH
                                     DEPENDS myheader.h.gch)
add_dependencies(mytarget, BuildMyPCH)

You'll note that in my custom command I've had to specify -fPIC and -std-gnu++17 explicitly, because they aren't included among either CXX_FLAGS or COMPILE_OPTIONS. I've looked for them among the  COMPILE_DEFINITIONS, COMPILE_FEATURES and COMPILE_FLAGS properties of my target as well, but there's no sign of them. And yet if I look at the verbose output when gcc builds other files using the CMake-generated makefile, I see that they are being included automatically.

So, my question: where is CMake storing these settings, and how can I apply them in my custom command without having to do so manually?

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Accessing -fPIC and -std settings within CMake definitions

Eric Doenges
Am 01.11.19 um 11:02 schrieb Stephen Morris:
I'm setting up a custom build step to build a precompiled header in gcc (yes I know that native support is coming with CMake 3.16, but I need to get something working on an older version).

My approach is basically to set up a custom command thus:

set_target_properties(mytarget PRIVATE CXX_STANDARD 17)
set_target_properties(mytarget PRIVATE  POSITION_INDEPENDENT CODE_1)
set(CMAKE_VERBOSE_MAKEFILE TRUE)
...
...
set(CXX_FLAGS ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG})   .. or whatever, depending on the current configuration..
get_target_property(compile_options, mytarget, COMPILE_OPTIONS)
add_custom_command(OUTPUT myheader.h.gch
                                           COMMAND ${CMAKE_CXX_COMPILER} ${CXX_FLAGS} ${compile_options} -fPIC -std=gnu++17 -c myheader.h -o myheader.h.gch
                                           DEPENDS myheader.h)
add_custom_target(BuildMyPCH
                                     DEPENDS myheader.h.gch)
add_dependencies(mytarget, BuildMyPCH)

You'll note that in my custom command I've had to specify -fPIC and -std-gnu++17 explicitly, because they aren't included among either CXX_FLAGS or COMPILE_OPTIONS. I've looked for them among the  COMPILE_DEFINITIONS, COMPILE_FEATURES and COMPILE_FLAGS properties of my target as well, but there's no sign of them. And yet if I look at the verbose output when gcc builds other files using the CMake-generated makefile, I see that they are being included automatically.

So, my question: where is CMake storing these settings, and how can I apply them in my custom command without having to do so manually?

As far as I am aware, there is no way of querying the entirety of the command line arguments applied by CMake - you will need to recreate CMake's behavior manually. In case of -fPIC, this could look like this (not tested, so there may be syntax errors, but the principle should work):

...
get_target_property(_tmp mytarget POSITION_INDEPENDENT_CODE)
if(_tmp AND CMAKE_CXX_COMPILE_OPTIONS_PIC)
  string(APPEND CXX_FLAGS " ${CMAKE_CXX_COMPILE_OPTIONS_PIC}")
endif()
...

If you have multiple targets that may have different compile options you will probably want to put this in a function that takes a target as input and returns the compiler flags as output.

To figure out the names of the variables CMake uses to hold flags to pass to the compiler, search the <...>/share/cmake-3.xx/Modules/Compiler directory.

--

Dr. Eric Dönges
Senior Software Engineer

MVTec Software GmbH | Arnulfstr. 205 | 80634 Munich | Germany
[hidden email] | Tel: +49 89 457 695-0 | www.mvtec.com

Find our privacy policy here.

 Sign up for our MVTec Newsletter!

Geschäftsführer: Dr. Wolfgang Eckstein, Dr. Olaf Munkelt
Amtsgericht München HRB 114695

 

MVTec Software GmbH Logo

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake