CMake: use visibility definitions in provided by CMake when available.

CMake 3.0 defines two new variables:

 * CMAKE_CXX_VISIBILITY_PRESET and
 * CMAKE_VISIBILITY_INLINES_HIDDEN

to control whether symbols not explicitly tagged for export are implicitly
exported.  Because only version 3.3 and later also applies that to static
libraries when in 3.3 mode, compatibility code is added as well.

When the minimum required version is bumped to 3.3, this code becomes
obsolete and a warning is displayed that the compatibility code should be
removed as well.
This commit is contained in:
Simon Richter 2015-12-07 16:46:18 -05:00 committed by Wayne Stambaugh
parent ab7350bf2d
commit 2869c9f49a
2 changed files with 24 additions and 10 deletions

View File

@ -75,6 +75,30 @@ option( BUILD_GITHUB_PLUGIN "Build the GITHUB_PLUGIN for pcbnew." ON )
set( KICAD_REPO_NAME "product" CACHE STRING "Name of the tree from which this build came." )
# Global setting: exports are explicit
set( CMAKE_CXX_VISIBILITY_PRESET "hidden" )
set( CMAKE_VISIBILITY_INLINES_HIDDEN ON )
# CMP0063: CMake < 3.3 does not handle hidden visibility for static libraries,
# and 3.3 is backwards compatible when the minimum version is smaller than 3.3.
if( POLICY CMP0063 )
cmake_policy( GET CMP0063 VISIBILITY_POLICY )
if( VISIBILITY_POLICY STREQUAL NEW )
message( WARNING "Compatibility code for CMake < 3.3 can be removed, search for CMP0063" )
else()
cmake_policy( GET CMP0063 NEW )
endif()
else()
if( CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY AND NOT APPLE )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY}hidden" )
endif()
if( CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN AND NOT APPLE )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN}" )
endif()
endif()
# All CMake downloads go here. Suggested is up in the source tree, not in the build dir where they
# would have to be downloaded over and over again. The default is to choose a directory that is
# hidden on linux (starts with a '.') because there is a way to exclude this directory when grepping
@ -152,14 +176,6 @@ if( CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang" )
set( CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -DNDEBUG" )
set( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DNDEBUG" )
if( GXX_HAS_VISIBILITY_FLAG AND NOT APPLE )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden" )
endif()
if( GXX_HAS_VISIBILITY_INLINES_FLAG AND NOT APPLE )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden" )
endif()
find_package( OpenMP QUIET )
if( OPENMP_FOUND )

View File

@ -47,8 +47,6 @@ macro( perform_feature_checks )
include( CheckCXXSourceCompiles )
include( CheckCXXCompilerFlag )
check_cxx_compiler_flag( -fvisibility=hidden GXX_HAS_VISIBILITY_FLAG )
check_cxx_compiler_flag( -fvisibility-inlines-hidden GXX_HAS_VISIBILITY_INLINES_FLAG )
check_include_file( "malloc.h" HAVE_MALLOC_H )