6

My CMake project compiles a Python .so/.dylib extension module linked with a big static library. I want to compile the subproject's static library with hidden visibility for symbols : it would allows the linker (or LTO optimizer) to discard symbols unused by my python module.

The best way to do this would be using CXX_VISIBILITY_PRESET and friends on the subproject's static library target. However, the subproject use policies from 3.1.0 by declaring :

cmake_minimum_required (VERSION 3.1.0)

The policy CMP0063 NEW: Honor visibility properties for all target types. is only introduced from version 3.3 and thus, the set_target_properties have no effects.

My project requires CMake 3.3, but I have no control on the subproject.

I would like to avoid patching the subproject CMakeLists.txt, but currently I see no other way.

Any idea ?

Piezoid
  • 638
  • 7
  • 16
  • Instead of using the property, you may simply adjust linker flags [CMAKE_STATIC_LINKER_FLAGS](https://cmake.org/cmake/help/v3.7/variable/CMAKE_STATIC_LINKER_FLAGS.html). – Tsyvarev Mar 21 '17 at 09:39
  • It's a compile time option. Ideally I would set CXX_VISIBILITY_PRESET to "hidden" globally and then `set_target_properties(python_module PROPERTIES CXX_VISIBILITY_PRESET default)`. Effectively it set '-fvisibility=hidden' on the compiler flags for the source declaring private symbols. – Piezoid Mar 21 '17 at 14:34
  • If you are sure that subproject would work with `CMP0063` policy, then you may replace `cmake_minimum_required` function for subproject, so it will enable given policy. See [that answer](http://stackoverflow.com/a/35345933/3440745) about CMake function's replacing. – Tsyvarev Mar 21 '17 at 17:47

2 Answers2

12

The cmake_minimum_required() has the following effects on the CMake policies:

  • All policies introduced in the specified version or earlier will be set to use NEW behavior.
  • All policies introduced after the specified version will be unset.

But you can use CMake's CMAKE_POLICY_DEFAULT_CMP<NNNN> global variables to "default for CMake Policy CMP when it is otherwise left unset."

Here is an example:

set(CMAKE_POLICY_DEFAULT_CMP0063 NEW)
add_subdirectory(MySubProjDir)

Reference

Florian
  • 39,996
  • 9
  • 133
  • 149
0

You could use add_compile_option to specify the appropriate compiler option.

Oliv
  • 17,610
  • 1
  • 29
  • 72
  • Currently, I use `target_compile_options(${TARGET_NAME} PRIVATE -fvisibility=hidden)` on all my targets except the python module. As the title suggest i'am more interested in a general method to leverage CMake functionalities (policies in CMake language) from the cmake_minimum version of top project, in the subprojects that require a lower version. – Piezoid Mar 21 '17 at 14:36