Merge topic 'generate-export-header'

41e2b1d Make add_compiler_export_flags a function again.
6a10deb Made ADD_COMPILER_EXPORT_FLAGS into a macro.
2d1acfe Don't warn when nothing to do in visibility function.
d679568 Just code style changes.
This commit is contained in:
David Cole 2011-09-21 17:29:23 -04:00 committed by CMake Topic Stage
commit 253e370c9e
1 changed files with 67 additions and 36 deletions

View File

@ -21,11 +21,15 @@
# ADD_COMPILER_EXPORT_FLAGS( [FATAL_WARNINGS] ) # ADD_COMPILER_EXPORT_FLAGS( [FATAL_WARNINGS] )
# #
# By default GENERATE_EXPORT_HEADER() generates macro names in a file name # By default GENERATE_EXPORT_HEADER() generates macro names in a file name
# determined by the name of the library. The ADD_COMPILER_EXPORT_FLAGS macro # determined by the name of the library. The ADD_COMPILER_EXPORT_FLAGS function
# adds -fvisibility=hidden to CMAKE_CXX_FLAGS if supported, and is a no-op on Windows # adds -fvisibility=hidden to CMAKE_CXX_FLAGS if supported, and is a no-op on
# which does not need extra compiler flags for exporting support. # Windows which does not need extra compiler flags for exporting support. You
# may optionally pass a single argument to ADD_COMPILER_EXPORT_FLAGS that will
# be populated with the required CXX_FLAGS required to enable visibility support
# for the compiler/architecture in use.
# #
# This means that in the simplest case, users of these functions will be equivalent to: # This means that in the simplest case, users of these functions will be
# equivalent to:
# #
# add_compiler_export_flags() # add_compiler_export_flags()
# add_library(somelib someclass.cpp) # add_library(somelib someclass.cpp)
@ -43,8 +47,8 @@
# ... # ...
# }; # };
# #
# The CMake fragment will generate a file in the ${CMAKE_CURRENT_BUILD_DIR} called # The CMake fragment will generate a file in the ${CMAKE_CURRENT_BUILD_DIR}
# somelib_export.h containing the macros SOMELIB_EXPORT, SOMELIB_NO_EXPORT, # called somelib_export.h containing the macros SOMELIB_EXPORT, SOMELIB_NO_EXPORT,
# SOMELIB_DEPRECATED, SOMELIB_DEPRECATED_EXPORT and SOMELIB_DEPRECATED_NO_EXPORT. # SOMELIB_DEPRECATED, SOMELIB_DEPRECATED_EXPORT and SOMELIB_DEPRECATED_NO_EXPORT.
# The resulting file should be installed with other headers in the library. # The resulting file should be installed with other headers in the library.
# #
@ -86,11 +90,14 @@
# add_library(shared_variant SHARED ${lib_SRCS}) # add_library(shared_variant SHARED ${lib_SRCS})
# add_library(static_variant ${lib_SRCS}) # add_library(static_variant ${lib_SRCS})
# generate_export_header(shared_variant BASE_NAME libshared_and_static) # generate_export_header(shared_variant BASE_NAME libshared_and_static)
# set_target_properties(static_variant PROPERTIES COMPILE_FLAGS -DLIBSHARED_AND_STATIC_STATIC_DEFINE) # set_target_properties(static_variant PROPERTIES
# COMPILE_FLAGS -DLIBSHARED_AND_STATIC_STATIC_DEFINE)
# #
# This will cause the export macros to expand to nothing when building the static library. # This will cause the export macros to expand to nothing when building the
# static library.
# #
# If DEFINE_NO_DEPRECATED is specified, then a macro ${BASE_NAME}_NO_DEPRECATED will be defined # If DEFINE_NO_DEPRECATED is specified, then a macro ${BASE_NAME}_NO_DEPRECATED
# will be defined
# This macro can be used to remove deprecated code from preprocessor output. # This macro can be used to remove deprecated code from preprocessor output.
# #
# option(EXCLUDE_DEPRECATED "Exclude deprecated parts of the library" FALSE) # option(EXCLUDE_DEPRECATED "Exclude deprecated parts of the library" FALSE)
@ -122,7 +129,6 @@
# #
# Generates the macros VTK_SOMELIB_EXPORT etc. # Generates the macros VTK_SOMELIB_EXPORT etc.
#============================================================================= #=============================================================================
# Copyright 2011 Stephen Kelly <steveire@gmail.com> # Copyright 2011 Stephen Kelly <steveire@gmail.com>
# #
@ -139,10 +145,10 @@
include(CMakeParseArguments) include(CMakeParseArguments)
include(CheckCXXCompilerFlag) include(CheckCXXCompilerFlag)
# TODO: Install this macro separately? # TODO: Install this macro separately?
macro(_check_cxx_compiler_attribute _ATTRIBUTE _RESULT) macro(_check_cxx_compiler_attribute _ATTRIBUTE _RESULT)
check_cxx_source_compiles("${_ATTRIBUTE} int somefunc() { return 0; } int main() { return somefunc();}" ${_RESULT} check_cxx_source_compiles("${_ATTRIBUTE} int somefunc() { return 0; }
int main() { return somefunc();}" ${_RESULT}
# Some compilers do not fail with a bad flag # Some compilers do not fail with a bad flag
FAIL_REGEX "unrecognized .*option" # GNU FAIL_REGEX "unrecognized .*option" # GNU
FAIL_REGEX "ignoring unknown option" # MSVC FAIL_REGEX "ignoring unknown option" # MSVC
@ -155,13 +161,16 @@ endmacro()
macro(_test_compiler_hidden_visibility) macro(_test_compiler_hidden_visibility)
if (CMAKE_COMPILER_IS_GNUCXX) if(CMAKE_COMPILER_IS_GNUCXX)
exec_program(${CMAKE_C_COMPILER} ARGS --version OUTPUT_VARIABLE _gcc_version_info) exec_program(${CMAKE_C_COMPILER} ARGS --version
string (REGEX MATCH "[345]\\.[0-9]\\.[0-9]" _gcc_version "${_gcc_version_info}") OUTPUT_VARIABLE _gcc_version_info)
string(REGEX MATCH "[345]\\.[0-9]\\.[0-9]"
_gcc_version "${_gcc_version_info}")
# gcc on mac just reports: "gcc (GCC) 3.3 20030304 ..." without the # gcc on mac just reports: "gcc (GCC) 3.3 20030304 ..." without the
# patch level, handle this here: # patch level, handle this here:
if(NOT _gcc_version) if(NOT _gcc_version)
string (REGEX REPLACE ".*\\(GCC\\).* ([34]\\.[0-9]) .*" "\\1.0" _gcc_version "${_gcc_version_info}") string(REGEX REPLACE ".*\\(GCC\\).* ([34]\\.[0-9]) .*" "\\1.0"
_gcc_version "${_gcc_version_info}")
endif() endif()
if(${_gcc_version} VERSION_LESS "4.2") if(${_gcc_version} VERSION_LESS "4.2")
@ -171,8 +180,10 @@ macro(_test_compiler_hidden_visibility)
endif() endif()
if(CMAKE_CXX_COMPILER_ID MATCHES Intel) if(CMAKE_CXX_COMPILER_ID MATCHES Intel)
exec_program(${CMAKE_CXX_COMPILER} ARGS -V OUTPUT_VARIABLE _intel_version_info) exec_program(${CMAKE_CXX_COMPILER} ARGS -V
string (REGEX REPLACE ".*Version ([0-9]+(\\.[0-9]+)+).*" "\\1" _intel_version "${_intel_version_info}") OUTPUT_VARIABLE _intel_version_info)
string(REGEX REPLACE ".*Version ([0-9]+(\\.[0-9]+)+).*" "\\1"
_intel_version "${_intel_version_info}")
if(${_intel_version} VERSION_LESS "12.0") if(${_intel_version} VERSION_LESS "12.0")
set(_INTEL_TOO_OLD TRUE) set(_INTEL_TOO_OLD TRUE)
@ -184,7 +195,7 @@ macro(_test_compiler_hidden_visibility)
# Exclude XL here because it misinterprets -fvisibility=hidden even though # Exclude XL here because it misinterprets -fvisibility=hidden even though
# the check_cxx_compiler_flag passes # the check_cxx_compiler_flag passes
# http://www.cdash.org/CDash/testDetails.php?test=109109951&build=1419259 # http://www.cdash.org/CDash/testDetails.php?test=109109951&build=1419259
if (NOT GCC_TOO_OLD if(NOT GCC_TOO_OLD
AND NOT _INTEL_TOO_OLD AND NOT _INTEL_TOO_OLD
AND NOT WIN32 AND NOT WIN32
AND NOT CYGWIN AND NOT CYGWIN
@ -192,8 +203,10 @@ macro(_test_compiler_hidden_visibility)
AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES PGI AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES PGI
AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES Watcom) AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES Watcom)
check_cxx_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY) check_cxx_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
check_cxx_compiler_flag(-fvisibility-inlines-hidden COMPILER_HAS_HIDDEN_INLINE_VISIBILITY) check_cxx_compiler_flag(-fvisibility-inlines-hidden
option(USE_COMPILER_HIDDEN_VISIBILITY "Use HIDDEN visibility support if available." ON) COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
option(USE_COMPILER_HIDDEN_VISIBILITY
"Use HIDDEN visibility support if available." ON)
mark_as_advanced(USE_COMPILER_HIDDEN_VISIBILITY) mark_as_advanced(USE_COMPILER_HIDDEN_VISIBILITY)
endif() endif()
endmacro() endmacro()
@ -204,18 +217,23 @@ macro(_test_compiler_has_deprecated)
OR GCC_TOO_OLD OR GCC_TOO_OLD
OR "${CMAKE_CXX_COMPILER_ID}" MATCHES PGI OR "${CMAKE_CXX_COMPILER_ID}" MATCHES PGI
OR "${CMAKE_CXX_COMPILER_ID}" MATCHES Watcom) OR "${CMAKE_CXX_COMPILER_ID}" MATCHES Watcom)
set(COMPILER_HAS_DEPRECATED "" CACHE INTERNAL "Compiler support for a deprecated attribute") set(COMPILER_HAS_DEPRECATED "" CACHE INTERNAL
"Compiler support for a deprecated attribute")
else() else()
_check_cxx_compiler_attribute("__attribute__((__deprecated__))" COMPILER_HAS_DEPRECATED_ATTR) _check_cxx_compiler_attribute("__attribute__((__deprecated__))"
COMPILER_HAS_DEPRECATED_ATTR)
if(COMPILER_HAS_DEPRECATED_ATTR) if(COMPILER_HAS_DEPRECATED_ATTR)
set(COMPILER_HAS_DEPRECATED "${COMPILER_HAS_DEPRECATED_ATTR}" CACHE INTERNAL "Compiler support for a deprecated attribute") set(COMPILER_HAS_DEPRECATED "${COMPILER_HAS_DEPRECATED_ATTR}"
CACHE INTERNAL "Compiler support for a deprecated attribute")
else() else()
_check_cxx_compiler_attribute("__declspec(deprecated)" COMPILER_HAS_DEPRECATED) _check_cxx_compiler_attribute("__declspec(deprecated)"
COMPILER_HAS_DEPRECATED)
endif() endif()
endif() endif()
endmacro() endmacro()
get_filename_component(_GENERATE_EXPORT_HEADER_MODULE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) get_filename_component(_GENERATE_EXPORT_HEADER_MODULE_DIR
"${CMAKE_CURRENT_LIST_FILE}" PATH)
macro(_DO_SET_MACRO_VALUES TARGET_LIBRARY) macro(_DO_SET_MACRO_VALUES TARGET_LIBRARY)
set(DEFINE_DEPRECATED) set(DEFINE_DEPRECATED)
@ -246,10 +264,13 @@ endmacro()
macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY) macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
# Option overrides # Option overrides
set(options DEFINE_NO_DEPRECATED) set(options DEFINE_NO_DEPRECATED)
set(oneValueArgs PREFIX_NAME BASE_NAME EXPORT_MACRO_NAME EXPORT_FILE_NAME DEPRECATED_MACRO_NAME NO_EXPORT_MACRO_NAME STATIC_DEFINE NO_DEPRECATED_MACRO_NAME) set(oneValueArgs PREFIX_NAME BASE_NAME EXPORT_MACRO_NAME EXPORT_FILE_NAME
DEPRECATED_MACRO_NAME NO_EXPORT_MACRO_NAME STATIC_DEFINE
NO_DEPRECATED_MACRO_NAME)
set(multiValueArgs) set(multiValueArgs)
cmake_parse_arguments(_GEH "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) cmake_parse_arguments(_GEH "${options}" "${oneValueArgs}" "${multiValueArgs}"
${ARGN})
set(BASE_NAME "${TARGET_LIBRARY}") set(BASE_NAME "${TARGET_LIBRARY}")
@ -266,7 +287,8 @@ macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${BASE_NAME_LOWER}_export.h") set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${BASE_NAME_LOWER}_export.h")
set(DEPRECATED_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_DEPRECATED") set(DEPRECATED_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_DEPRECATED")
set(STATIC_DEFINE "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_STATIC_DEFINE") set(STATIC_DEFINE "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_STATIC_DEFINE")
set(NO_DEPRECATED_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_DEPRECATED") set(NO_DEPRECATED_MACRO_NAME
"${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_DEPRECATED")
if(_GEH_UNPARSED_ARGUMENTS) if(_GEH_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Unknown keywords given to GENERATE_EXPORT_HEADER(): \"${_GEH_UNPARSED_ARGUMENTS}\"") message(FATAL_ERROR "Unknown keywords given to GENERATE_EXPORT_HEADER(): \"${_GEH_UNPARSED_ARGUMENTS}\"")
@ -292,23 +314,25 @@ macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
set(STATIC_DEFINE ${_GEH_PREFIX_NAME}${_GEH_STATIC_DEFINE}) set(STATIC_DEFINE ${_GEH_PREFIX_NAME}${_GEH_STATIC_DEFINE})
endif() endif()
if (_GEH_DEFINE_NO_DEPRECATED) if(_GEH_DEFINE_NO_DEPRECATED)
set(DEFINE_NO_DEPRECATED TRUE) set(DEFINE_NO_DEPRECATED TRUE)
endif() endif()
if (_GEH_NO_DEPRECATED_MACRO_NAME) if(_GEH_NO_DEPRECATED_MACRO_NAME)
set(NO_DEPRECATED_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_NO_DEPRECATED_MACRO_NAME}) set(NO_DEPRECATED_MACRO_NAME
${_GEH_PREFIX_NAME}${_GEH_NO_DEPRECATED_MACRO_NAME})
endif() endif()
set(INCLUDE_GUARD_NAME "${EXPORT_MACRO_NAME}_H") set(INCLUDE_GUARD_NAME "${EXPORT_MACRO_NAME}_H")
get_target_property(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY} DEFINE_SYMBOL) get_target_property(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY} DEFINE_SYMBOL)
if (NOT EXPORT_IMPORT_CONDITION) if(NOT EXPORT_IMPORT_CONDITION)
set(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY}_EXPORTS) set(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY}_EXPORTS)
endif() endif()
configure_file("${_GENERATE_EXPORT_HEADER_MODULE_DIR}/exportheader.cmake.in" "${EXPORT_FILE_NAME}" @ONLY) configure_file("${_GENERATE_EXPORT_HEADER_MODULE_DIR}/exportheader.cmake.in"
"${EXPORT_FILE_NAME}" @ONLY)
endmacro() endmacro()
function(GENERATE_EXPORT_HEADER TARGET_LIBRARY) function(GENERATE_EXPORT_HEADER TARGET_LIBRARY)
@ -333,7 +357,7 @@ function(add_compiler_export_flags)
_test_compiler_has_deprecated() _test_compiler_has_deprecated()
if(NOT (USE_COMPILER_HIDDEN_VISIBILITY AND COMPILER_HAS_HIDDEN_VISIBILITY)) if(NOT (USE_COMPILER_HIDDEN_VISIBILITY AND COMPILER_HAS_HIDDEN_VISIBILITY))
message(WARNING "Compiler doesn't have hidden visibility") # Just return if there are no flags to add.
return() return()
endif() endif()
@ -342,5 +366,12 @@ function(add_compiler_export_flags)
if(COMPILER_HAS_HIDDEN_INLINE_VISIBILITY) if(COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
set (EXTRA_FLAGS "${EXTRA_FLAGS} -fvisibility-inlines-hidden") set (EXTRA_FLAGS "${EXTRA_FLAGS} -fvisibility-inlines-hidden")
endif() endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_FLAGS}" PARENT_SCOPE)
# Either return the extra flags needed in the supplied argument, or to the
# CMAKE_CXX_FLAGS if no argument is supplied.
if(ARGV0)
set(${ARGV0} "${EXTRA_FLAGS}" PARENT_SCOPE)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_FLAGS}" PARENT_SCOPE)
endif()
endfunction() endfunction()