Skip to content

Commit c684bb3

Browse files
Jan SchlamelcherJan Schlamelcher
authored andcommitted
Various enhancements for DCMTK's CMake exports.
Removed the need for exploiting the old CMake policy CMP0103 behavior. Removed DCMTK_BUILD_CMKDIR, which was just an alias for CMAKE_BINARY_DIR. Added the DCMTK:: namespace for all exported targets. Added include directory target properties for all exported targets. Added config interface target for adding the include directory for the generated osconfig.h etc. by linking it. Added DCMTK interface target for linking all DCMTK libraries as DCMTK::DCMTK. Thanks to Alex Reinking <alex_reinking@berkeley.edu> for all the helpful suggestions.
1 parent 0f2f4df commit c684bb3

File tree

6 files changed

+84
-63
lines changed

6 files changed

+84
-63
lines changed

CMake/GenerateCMakeExports.cmake

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,22 @@
1414
# DCMTKConfigVersion.cmake provides checking of DCMTK version compatibility
1515
# DCMTKConfig.cmake will contain options used to build this DCMTK package
1616

17-
if(COMMAND CMAKE_POLICY)
18-
if (POLICY CMP0103)
19-
# suppresses error related to the following export statement
20-
cmake_policy(SET CMP0103 OLD)
21-
endif()
22-
endif()
17+
# Start with clean DCMTKTargets.cmake and fill it by appending
18+
file(WRITE "${CMAKE_BINARY_DIR}/DCMTKTargets.cmake" "")
2319

2420
# Get and store all executable targets to DCMTKTargets.cmake within build's main dir
2521
get_property(DCMTK_EXECUTABLE_TARGETS GLOBAL PROPERTY DCMTK_EXECUTABLE_TARGETS)
26-
export(TARGETS ${DCMTK_EXECUTABLE_TARGETS} FILE "${DCMTK_BUILD_CMKDIR}/DCMTKTargets.cmake")
22+
export(TARGETS ${DCMTK_EXECUTABLE_TARGETS} APPEND FILE "${CMAKE_BINARY_DIR}/DCMTKTargets.cmake" NAMESPACE DCMTK::)
2723

2824
# Get and store libraries to DCMTKTargets.cmake within the build's main dir
2925
get_property(DCMTK_LIBRARY_TARGETS GLOBAL PROPERTY DCMTK_LIBRARY_TARGETS)
30-
export(TARGETS ${DCMTK_LIBRARY_TARGETS} APPEND FILE "${DCMTK_BUILD_CMKDIR}/DCMTKTargets.cmake")
26+
export(TARGETS config ${DCMTK_LIBRARY_TARGETS} APPEND FILE "${CMAKE_BINARY_DIR}/DCMTKTargets.cmake" NAMESPACE DCMTK::)
27+
28+
# Add interface library for conveniently linking to all libraries via DCMTK::DCMTK
29+
export(TARGETS DCMTK APPEND FILE "${CMAKE_BINARY_DIR}/DCMTKTargets.cmake" NAMESPACE DCMTK::)
3130

3231
# Create DCMTConfigVersion.cmake with basic DCMTK version information (build tree)
33-
set(DCMTK_CONFIG_VERSION "${DCMTK_BUILD_CMKDIR}/DCMTKConfigVersion.cmake")
32+
set(DCMTK_CONFIG_VERSION "${CMAKE_BINARY_DIR}/DCMTKConfigVersion.cmake")
3433
WRITE_BASIC_PACKAGE_VERSION_FILE(
3534
${DCMTK_CONFIG_VERSION}
3635
VERSION ${DCMTK_MAJOR_VERSION}.${DCMTK_MINOR_VERSION}.${DCMTK_BUILD_VERSION}
@@ -43,7 +42,7 @@ WRITE_BASIC_PACKAGE_VERSION_FILE(
4342
# build and install tree versions of the file.
4443

4544
# Case 1: Configure 'DCMTKConfig.cmake' for a build tree.
46-
set(DCMTK_CMKDIR_CONFIG "${DCMTK_BUILD_CMKDIR}")
45+
set(DCMTK_CMKDIR_CONFIG "${CMAKE_BINARY_DIR}")
4746
# Collect include paths
4847
set(DCMTK_INCLUDE_DIR_CONFIGS
4948
"SET_AND_CHECK(DCMTK_config_INCLUDE_DIR \"${DCMTK_BINARY_DIR}/config/include/dcmtk/config\")")
@@ -57,7 +56,7 @@ foreach(module ${DCMTK_MODULES})
5756
set(DCMTK_CONFIG_CODE "${DCMTK_CONFIG_CODE}list(APPEND DCMTK_INCLUDE_DIRS \"${DCMTK_SOURCE_DIR}/${module}/include\")\n")
5857
endforeach()
5958
set(DCMTK_CONFIG_CODE "${DCMTK_CONFIG_CODE}##################################################")
60-
set(dcmtk_config "${DCMTK_BUILD_CMKDIR}/DCMTKConfig.cmake")
59+
set(dcmtk_config "${CMAKE_BINARY_DIR}/DCMTKConfig.cmake")
6160
# Actually configure file and write it to build's main directory
6261
CONFIGURE_PACKAGE_CONFIG_FILE(
6362
${DCMTK_SOURCE_DIR}/CMake/DCMTKConfig.cmake.in

CMake/dcmtkAfterModules.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ if(BUILD_SINGLE_SHARED_LIBRARY)
1616

1717
# Export target for build tree
1818
set_property(GLOBAL APPEND PROPERTY DCMTK_LIBRARY_TARGETS dcmtk)
19+
target_link_libraries(DCMTK INTERFACE dcmtk)
1920

2021
# Declare installation files. Also export libs and executables to DCMTKTargets.cmake.
2122
install(TARGETS dcmtk

CMake/dcmtkMacros.cmake

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,24 @@ macro(DCMTK_ADD_LIBRARY LIBRARY)
9090
set_target_properties(${LIBRARY}${DCMTK_LIBRARY_SUFFIX} PROPERTIES COMPILE_DEFINITIONS "${LIBRARY}_EXPORTS")
9191
endif()
9292

93+
get_filename_component(DCMTK_ADD_LIBRARY_PARENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}" PATH)
94+
target_include_directories(${LIBRARY}${DCMTK_LIBRARY_SUFFIX} PUBLIC
95+
$<BUILD_INTERFACE:${DCMTK_ADD_LIBRARY_PARENT_DIR}/include>
96+
$<INSTALL_INTERFACE:include>
97+
)
98+
9399
if(NOT BUILD_SINGLE_SHARED_LIBRARY)
94100
# Remember export target for writing it to build tree later
95101
set_property(GLOBAL APPEND PROPERTY DCMTK_LIBRARY_TARGETS ${LIBRARY}${DCMTK_LIBRARY_SUFFIX})
102+
target_link_libraries(DCMTK INTERFACE ${LIBRARY}${DCMTK_LIBRARY_SUFFIX})
96103

97104
# Declare installation files
98105
install(TARGETS ${LIBRARY}${DCMTK_LIBRARY_SUFFIX}
99106
EXPORT DCMTKTargets
100107
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT bin
101108
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT shlib
102-
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT lib)
109+
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT lib
110+
)
103111
endif()
104112
endmacro()
105113

@@ -186,3 +194,52 @@ function(DCMTK_UPPER_BOUND LIST COMP VAR)
186194
endforeach()
187195
set("${VAR}" ${N} PARENT_SCOPE)
188196
endfunction()
197+
198+
function(DCMTK_CREATE_INSTALL_EXPORTS)
199+
# DCMTKTargets.cmake will contain list of executables and libraries produced
200+
# DCMTKConfigVersion.cmake will contain DCMTK version information
201+
# DCMTKConfig.cmake will contain options used to build DCMTK
202+
#
203+
# All three files are created within the build tree's main directory (handled in
204+
# CMake/GenerateCMakeExports.cmake, and are installed to locations (OS-specific
205+
# under the main install dir (handled directly below).
206+
207+
# Only create fully-fledged CMake export files if we have the related commands
208+
include("${DCMTK_MACROS_DIR}/CheckCMakeCommandExists.cmake")
209+
include(CMakePackageConfigHelpers OPTIONAL)
210+
CHECK_CMAKE_COMMAND_EXISTS("CONFIGURE_PACKAGE_CONFIG_FILE")
211+
CHECK_CMAKE_COMMAND_EXISTS("WRITE_BASIC_PACKAGE_VERSION_FILE")
212+
213+
if(HAVE_CONFIGURE_PACKAGE_CONFIG_FILE AND HAVE_WRITE_BASIC_PACKAGE_VERSION_FILE)
214+
215+
# Create and configure CMake export files
216+
include("${DCMTK_MACROS_DIR}/GenerateCMakeExports.cmake")
217+
218+
# ${DCMTK_INSTALL_CONFIG} and ${DCMTK_CONFIG_VERSION} are
219+
# defined within CMake/GenerateCMakeExports.cmake.
220+
# Install DCMTKTargets.cmake to install tree
221+
install(EXPORT DCMTKTargets FILE DCMTKTargets.cmake NAMESPACE DCMTK::
222+
DESTINATION "${DCMTK_INSTALL_CMKDIR}" COMPONENT cmake)
223+
224+
# Install DCMTKConfig.cmake and DCMTKConfigVersion.cmake
225+
install(FILES "${DCMTK_INSTALL_CONFIG}" "${DCMTK_CONFIG_VERSION}"
226+
DESTINATION "${DCMTK_INSTALL_CMKDIR}" COMPONENT cmake)
227+
228+
else()
229+
230+
# Warning that we use old "configure_file" command
231+
message(STATUS "Warning: Using old configure_file() mechanism to produce DCMTKConfig.cmake")
232+
233+
# Actually configure file
234+
configure_file("${DCMTK_MACROS_DIR}/DCMTKConfig.old_cmake.in"
235+
"${DCMTK_BINARY_DIR}/DCMTKConfig.cmake" @ONLY)
236+
237+
# Install DCMTKConfig.cmake and DCMTKConfigVersion.cmake
238+
install(FILES "${DCMTK_BINARY_DIR}/DCMTKConfig.cmake" "${DCMTK_BINARY_DIR}/DCMTKConfigVersion.cmake"
239+
DESTINATION "${DCMTK_INSTALL_CMKDIR}"
240+
COMPONENT cmake)
241+
242+
endif()
243+
endfunction()
244+
245+
set(DCMTK_MACROS_DIR "${CMAKE_CURRENT_LIST_DIR}" CACHE INTERNAL "")

CMake/dcmtkPrepare.cmake

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,11 @@ elseif(WIN32)
250250
endif()
251251

252252
#-----------------------------------------------------------------------------
253-
# Build directories
253+
# Interface target to collect all DCMTK libraries
254254
#-----------------------------------------------------------------------------
255-
set(DCMTK_BUILD_CMKDIR "${CMAKE_BINARY_DIR}")
256-
257-
#-----------------------------------------------------------------------------
258-
# Start with clean DCMTKTargets.cmake, filled in GenerateCMakeExports.cmake
259-
#-----------------------------------------------------------------------------
260-
file(WRITE "${DCMTK_BUILD_CMKDIR}/DCMTKTargets.cmake" "")
255+
add_library(DCMTK INTERFACE)
256+
install(TARGETS DCMTK EXPORT DCMTKTargets)
257+
target_link_libraries(DCMTK INTERFACE config)
261258

262259
#-----------------------------------------------------------------------------
263260
# Platform-independent settings

CMakeLists.txt

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ set(DCMTK_MODULES ofstd oflog dcmdata dcmimgle
3535
dcmseg dcmtract dcmpmap dcmect
3636
CACHE STRING "List of modules that should be built.")
3737

38+
# Provide an interface target to elegantly include the config directory
39+
add_library(config INTERFACE)
40+
target_include_directories(config INTERFACE
41+
$<BUILD_INTERFACE:${DCMTK_BINARY_DIR}/config/include>
42+
$<INSTALL_INTERFACE:include>
43+
)
44+
install(TARGETS config EXPORT DCMTKTargets)
45+
3846
# Include directories
3947
set(DCMTK_INCLUDE_DIR "${DCMTK_BINARY_DIR}/config/include")
4048
foreach(inc ${DCMTK_MODULES})
@@ -115,50 +123,9 @@ install(DIRECTORY docs/ DESTINATION "${CMAKE_INSTALL_DOCDIR}"
115123
#-----------------------------------------------------------------------------
116124
# Create an install configuration files for external projects
117125
#-----------------------------------------------------------------------------
118-
#
119-
# DCMTKTargets.cmake will contain list of executables and libraries produced
120-
# DCMTKConfigVersion.cmake will contain DCMTK version information
121-
# DCMTKConfig.cmake will contain options used to build DCMTK
122-
#
123-
# All three files are created within the build tree's main directory (handled in
124-
# CMake/GenerateCMakeExports.cmake, and are installed to locations (OS-specific
125-
# under the main install dir (handled directly below).
126-
127-
# Only create fully-fledged CMake export files if we have the related commands
128-
include(CMake/CheckCMakeCommandExists.cmake)
129-
include(CMakePackageConfigHelpers OPTIONAL)
130-
CHECK_CMAKE_COMMAND_EXISTS("CONFIGURE_PACKAGE_CONFIG_FILE")
131-
CHECK_CMAKE_COMMAND_EXISTS("WRITE_BASIC_PACKAGE_VERSION_FILE")
132-
133-
if(HAVE_CONFIGURE_PACKAGE_CONFIG_FILE AND HAVE_WRITE_BASIC_PACKAGE_VERSION_FILE)
134-
135-
# Create and configure CMake export files
136-
include(CMake/GenerateCMakeExports.cmake)
137-
138-
# ${DCMTK_INSTALL_CONFIG} and ${DCMTK_CONFIG_VERSION} are
139-
# defined within CMake/GenerateCMakeExports.cmake.
140-
# Install DCMTKTargets.cmake to install tree
141-
install(EXPORT DCMTKTargets FILE DCMTKTargets.cmake
142-
DESTINATION "${DCMTK_INSTALL_CMKDIR}" COMPONENT cmake)
143-
144-
# Install DCMTKConfig.cmake and DCMTKConfigVersion.cmake
145-
install(FILES "${DCMTK_INSTALL_CONFIG}" "${DCMTK_CONFIG_VERSION}"
146-
DESTINATION "${DCMTK_INSTALL_CMKDIR}" COMPONENT cmake)
147-
148-
else()
149-
150-
# Warning that we use old "configure_file" command
151-
message(STATUS "Warning: Using old configure_file() mechanism to produce DCMTKConfig.cmake")
152-
153-
# Actually configure file
154-
configure_file("${DCMTK_SOURCE_DIR}/CMake/DCMTKConfig.old_cmake.in"
155-
"${DCMTK_BINARY_DIR}/DCMTKConfig.cmake" @ONLY)
156-
157-
# Install DCMTKConfig.cmake and DCMTKConfigVersion.cmake
158-
install(FILES "${DCMTK_BINARY_DIR}/DCMTKConfig.cmake" "${DCMTK_BINARY_DIR}/DCMTKConfigVersion.cmake"
159-
DESTINATION "${DCMTK_INSTALL_CMKDIR}"
160-
COMPONENT cmake)
161126

127+
if(NOT DCMTK_NO_EXPORT)
128+
dcmtk_create_install_exports()
162129
endif()
163130

164131
#-----------------------------------------------------------------------------

ofstd/libsrc/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# create library from source files
22
DCMTK_ADD_LIBRARY(ofstd ofchrenc ofcmdln ofconapp ofcond ofconfig ofconsol ofcrc32 ofdate ofdatime oferror offile offilsys offname oflist ofstd ofstring ofstrutl ofthread oftime oftimer oftempf ofxml ofuuid ofmath ofsockad ofrand)
33

4-
DCMTK_TARGET_LINK_LIBRARIES(ofstd ${CHARSET_CONVERSION_LIBS} ${SOCKET_LIBS} ${THREAD_LIBS} ${WIN32_STD_LIBRARIES})
4+
DCMTK_TARGET_LINK_LIBRARIES(ofstd config ${CHARSET_CONVERSION_LIBS} ${SOCKET_LIBS} ${THREAD_LIBS} ${WIN32_STD_LIBRARIES})

0 commit comments

Comments
 (0)