Skip to content

Commit

Permalink
ENH: Add option ENABLED_SHARED to itk_module for shared libraries
Browse files Browse the repository at this point in the history
Bump required cmake version to 2.8.6 for GenerateExportHeader.

This option enables shared libraries on windows and adds export
specification with gcc shared libraries. This option respects the
CMake variable BUILD_SHARED_LIBS.  Modules without this
specification can still be shared with gcc, but will be static with
msvc.

Change-Id: Ic1552ca64bb7e614e254cefe285802c96359ed3f
  • Loading branch information
blowekamp committed Jul 19, 2013
1 parent 9aefa4f commit 6d369e1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 33 deletions.
23 changes: 23 additions & 0 deletions CMake/ITKModuleMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ include(${_ITKModuleMacros_DIR}/ITKModuleAPI.cmake)
include(${_ITKModuleMacros_DIR}/ITKModuleDoxygen.cmake)
include(${_ITKModuleMacros_DIR}/ITKModuleHeaderTest.cmake)

include(GenerateExportHeader)

if(ITK_CPPCHECK_TEST)
include(${_ITKModuleMacros_DIR}/ITKModuleCPPCheckTest.cmake)
endif()
Expand All @@ -21,12 +23,16 @@ macro(itk_module _name)
set(ITK_MODULE_${itk-module-test}_DEPENDS "${itk-module}")
set(ITK_MODULE_${itk-module}_DESCRIPTION "description")
set(ITK_MODULE_${itk-module}_EXCLUDE_FROM_ALL 0)
set(ITK_MODULE_${itk-module}_ENABLE_SHARED 0)
foreach(arg ${ARGN})
if("${arg}" MATCHES "^(DEPENDS|TEST_DEPENDS|DESCRIPTION|DEFAULT)$")
set(_doing "${arg}")
elseif("${arg}" MATCHES "^EXCLUDE_FROM_ALL$")
set(_doing "")
set(ITK_MODULE_${itk-module}_EXCLUDE_FROM_ALL 1)
elseif("${arg}" MATCHES "^ENABLE_SHARED$")
set(_doing "")
set(ITK_MODULE_${itk-module}_ENABLE_SHARED 1)
elseif("${arg}" MATCHES "^[A-Z][A-Z][A-Z]$")
set(_doing "")
message(AUTHOR_WARNING "Unknown argument [${arg}]")
Expand Down Expand Up @@ -128,6 +134,23 @@ macro(itk_module_impl)
add_subdirectory(src)
endif()


if( ITK_MODULE_${itk-module}_ENABLE_SHARED )
# Generate the export macro header for symbol visibility/Windows DLL declspec
generate_export_header(${itk-module}
EXPORT_FILE_NAME ${ITKCommon_BINARY_DIR}/${itk-module}Export.h
EXPORT_MACRO_NAME ${itk-module}_EXPORT
NO_EXPORT_MACRO_NAME ${itk-module}_HIDDEN
STATIC_DEFINE ITK_STATIC )
if (BUILD_SHARED_LIBS)
# export flags are only added when building shared libs, they cause
# mismatched visibility warnings when building statically.
add_compiler_export_flags(my_abi_flags)
set_property(TARGET ${itk-module} APPEND
PROPERTY COMPILE_FLAGS "${my_abi_flags}")
endif()
endif()

set(itk-module-EXPORT_CODE-build "${${itk-module}_EXPORT_CODE_BUILD}")
set(itk-module-EXPORT_CODE-install "${${itk-module}_EXPORT_CODE_INSTALL}")

Expand Down
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
cmake_minimum_required(VERSION 2.8.4 FATAL_ERROR)
cmake_minimum_required(VERSION 2.8.6 FATAL_ERROR)
include(CMakeDependentOption)
#
# use ExternalProject
include(ExternalProject)

if(WIN32)
cmake_minimum_required(VERSION 2.8.8)
elseif(NOT EXISTS /dev/urandom)
cmake_minimum_required(VERSION 2.8.5)
endif()

project(ITK)
Expand Down
24 changes: 3 additions & 21 deletions Modules/Core/Common/include/itkMacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#include "itkWin32Header.h"
#include "itkConfigure.h"
#include "ITKCommonExport.h"

#include <typeinfo>

Expand Down Expand Up @@ -101,6 +102,7 @@ namespace itk
#endif

// Setup symbol exports
// begin legacy
#if defined( _WIN32 ) || defined ( WIN32 )
#define ITK_ABI_IMPORT __declspec(dllimport)
#define ITK_ABI_EXPORT __declspec(dllexport)
Expand All @@ -116,27 +118,7 @@ namespace itk
#define ITK_ABI_HIDDEN
#endif
#endif

#define ITKCommon_HIDDEN ITK_ABI_HIDDEN

#if !defined( ITKSTATIC )
/* CMake adds "MyLibrary_EXPORTS" definition for MSVC platforms
* when building a DLL */
#ifdef ITKCommon_EXPORTS
#define ITKCommon_EXPORT ITK_ABI_EXPORT
#else
#define ITKCommon_EXPORT ITK_ABI_IMPORT
#endif /* ITKCommon_EXPORTS */
#else
/* ITKCommon is build as a static lib */
#if __GNUC__ >= 4
// Don't hide symbols in the static ITKCommon library in case
// -fvisibility=hidden is used
#define ITKCommon_EXPORT ITK_ABI_EXPORT
#else
#define ITKCommon_EXPORT
#endif
#endif
// end legacy

/** Define two object creation methods. The first method, New(),
* creates an object from a class, potentially deferring to a factory.
Expand Down
1 change: 1 addition & 0 deletions Modules/Core/Common/itk-module.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ platform specific features. It is anticipated that most other ITK modules will
depend on this one.")

itk_module(ITKCommon
ENABLE_SHARED
DEPENDS
ITKVNLInstantiation
ITKKWSys
Expand Down
9 changes: 0 additions & 9 deletions Modules/Core/Common/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,6 @@ endif()

### generating libraries
add_library( ITKCommon ${ITK_LIBRARY_BUILD_TYPE} ${ITKCommon_SRCS})
if(NOT WIN32 AND CMAKE_COMPILER_IS_GNUCXX AND BUILD_SHARED_LIBS)
set(ITKCommon_CXX_FLAGS "-fvisibility=hidden -fvisibility-inlines-hidden")
ITK_CHECK_CXX_COMPILER_FLAG(${ITKCommon_CXX_FLAGS} ITKCommon_HAS_GCC_VISIBILITY_FLAGS)
if(ITKCommon_HAS_GCC_VISIBILITY_FLAGS)
set_target_properties(ITKCommon PROPERTIES
COMPILE_FLAGS "${ITKCommon_CXX_FLAGS}"
)
endif()
endif()

target_link_libraries(ITKCommon
itksys
Expand Down

0 comments on commit 6d369e1

Please sign in to comment.