Skip to content

Commit d4580cc

Browse files
committed
Export CMake targets
This change installs a CMake package configuration file containing the PIO_C, PIO_Fortran and PIO_GPTL targets. This has been tested by building and installing PIO locally and then calling `find_package(PIO COMPONENTS Fortran)` and `target_link_libraries(<target> PIO::PIO_Fortran)` in another project. Tested with cmake version 3.5.2 (current minimum version)
1 parent 1d516b8 commit d4580cc

File tree

5 files changed

+110
-12
lines changed

5 files changed

+110
-12
lines changed

CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,22 @@ INSTALL(FILES "${PIO_BINARY_DIR}/libpio.settings"
487487
DESTINATION lib
488488
COMPONENT libraries)
489489

490+
include(CMakePackageConfigHelpers)
491+
492+
write_basic_package_version_file(
493+
"${CMAKE_CURRENT_BINARY_DIR}/PIOConfigVersion.cmake"
494+
VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}"
495+
COMPATIBILITY AnyNewerVersion)
496+
497+
configure_package_config_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/PIOConfig.cmake.in"
498+
"${CMAKE_CURRENT_BINARY_DIR}/PIOConfig.cmake"
499+
INSTALL_DESTINATION lib/cmake/PIO)
500+
501+
install(FILES
502+
"${CMAKE_CURRENT_BINARY_DIR}/PIOConfig.cmake"
503+
"${CMAKE_CURRENT_BINARY_DIR}/PIOConfigVersion.cmake"
504+
DESTINATION lib/cmake/PIO)
505+
490506
#####
491507
# Create pio_meta.h include file.
492508
#####

cmake/PIOConfig.cmake.in

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
@PACKAGE_INIT@
2+
3+
if(NOT PIO_FIND_QUIETLY)
4+
message(STATUS "Found PIO: ${PACKAGE_PREFIX_DIR}")
5+
endif()
6+
7+
set(PIO_ENABLE_TIMING @PIO_ENABLE_TIMING@)
8+
set(PIO_ENABLE_FORTRAN @PIO_ENABLE_FORTRAN@)
9+
10+
# The following components are supported by PIO (order is important)
11+
set(_supported_components GPTL C Fortran)
12+
13+
# Check validity of requested components
14+
foreach(_comp ${PIO_FIND_COMPONENTS})
15+
if(NOT _comp IN_LIST _supported_components)
16+
set(PIO_FOUND False)
17+
set(PIO_NOT_FOUND_MESSAGE "Unsupported component: ${_comp}")
18+
endif()
19+
endforeach()
20+
21+
# Some components are only available if they were built
22+
if (GPTL IN_LIST PIO_FIND_COMPONENTS AND NOT PIO_ENABLE_TIMING)
23+
set(PIO_FOUND False)
24+
set(PIO_NOT_FOUND_MESSAGE "The following component is unavailable: GPTL")
25+
endif()
26+
if (Fortran IN_LIST PIO_FIND_COMPONENTS AND NOT PIO_ENABLE_FORTRAN)
27+
set(PIO_FOUND False)
28+
set(PIO_NOT_FOUND_MESSAGE "The following component is unavailable: Fortran")
29+
endif()
30+
31+
# Build a list of all the required components, taking into account their dependencies
32+
set(_required_components ${PIO_FIND_COMPONENTS})
33+
if(C IN_LIST _required_components)
34+
if(PIO_ENABLE_TIMING)
35+
list(APPEND _required_components GPTL)
36+
endif()
37+
endif()
38+
if(Fortran IN_LIST _required_components)
39+
list(APPEND _required_components C)
40+
if(PIO_ENABLE_TIMING)
41+
list(APPEND _required_components GPTL)
42+
endif()
43+
endif()
44+
list(REMOVE_DUPLICATES _required_components)
45+
46+
# Include required targets. We do this by looping over the _supported_components
47+
# list because the order in which targets are loaded matters.
48+
foreach(_comp ${_supported_components})
49+
if(_comp IN_LIST _required_components)
50+
include("${CMAKE_CURRENT_LIST_DIR}/PIO${_comp}Targets.cmake")
51+
endif()
52+
endforeach()

src/clib/CMakeLists.txt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ include_directories(
4949

5050
# Include the clib source directory
5151
target_include_directories (pioc
52-
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
52+
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
5353

5454
# Include the ncint source directory
5555
target_include_directories (pioc
56-
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../ncint)
56+
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../ncint>)
5757

5858
# System and compiler CPP directives
5959
target_compile_definitions (pioc
@@ -76,12 +76,22 @@ endif()
7676
# DEFINE THE INSTALL
7777
#==============================================================================
7878

79+
set_target_properties(pioc PROPERTIES EXPORT_NAME PIO_C)
80+
7981
# Library
80-
install (TARGETS pioc DESTINATION lib)
82+
install (TARGETS pioc
83+
EXPORT PIOCTargets
84+
DESTINATION lib
85+
INCLUDES DESTINATION include)
8186

8287
# Include/Header Files
8388
install (FILES pio.h uthash.h DESTINATION include)
8489

90+
install(EXPORT PIOCTargets
91+
FILE PIOCTargets.cmake
92+
NAMESPACE PIO::
93+
DESTINATION lib/cmake/PIO)
94+
8595
#==============================================================================
8696
# DEFINE THE DEPENDENCIES
8797
#==============================================================================
@@ -141,7 +151,7 @@ endif ()
141151

142152
#===== Add EXTRAs =====
143153
target_include_directories (pioc
144-
PUBLIC ${PIO_C_EXTRA_INCLUDE_DIRS})
154+
PUBLIC $<BUILD_INTERFACE:${PIO_C_EXTRA_INCLUDE_DIRS}>)
145155
target_link_libraries (pioc
146156
PUBLIC ${PIO_C_EXTRA_LIBRARIES})
147157
target_compile_options (pioc

src/flib/CMakeLists.txt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ endif ()
4848

4949
# Include flib source and binary directories (for Fortran modules)
5050
target_include_directories (piof
51-
PUBLIC ${CMAKE_BINARY_DIR}
52-
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
53-
PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
51+
PUBLIC $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>
52+
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
53+
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
5454

5555
# System and compiler CPP directives
5656
target_compile_definitions (piof
@@ -104,12 +104,22 @@ endif ()
104104
# DEFINE THE INSTALL
105105
#==============================================================================
106106

107+
set_target_properties (piof PROPERTIES EXPORT_NAME PIO_Fortran)
108+
107109
# Library
108-
install (TARGETS piof DESTINATION lib)
110+
install (TARGETS piof
111+
EXPORT PIOFortranTargets
112+
DESTINATION lib
113+
INCLUDES DESTINATION include)
109114

110115
# Fortran Modules
111116
install (FILES ${PIO_Fortran_MODS} DESTINATION include)
112117

118+
install(EXPORT PIOFortranTargets
119+
FILE PIOFortranTargets.cmake
120+
NAMESPACE PIO::
121+
DESTINATION lib/cmake/PIO)
122+
113123
#==============================================================================
114124
# DEFINE THE DEPENDENCIES
115125
#==============================================================================
@@ -239,7 +249,7 @@ endif ()
239249

240250
#===== Add EXTRAs =====
241251
target_include_directories (piof
242-
PUBLIC ${PIO_Fortran_EXTRA_INCLUDE_DIRS})
252+
PUBLIC $<BUILD_INTERFACE:${PIO_Fortran_EXTRA_INCLUDE_DIRS}>)
243253
target_link_libraries (piof
244254
PUBLIC ${PIO_Fortran_EXTRA_LIBRARIES})
245255
target_compile_options (piof

src/gptl/CMakeLists.txt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ set (GPTL_Fortran_MODS ${CMAKE_CURRENT_BINARY_DIR}/perf_mod.mod
2727
add_library (gptl ${GPTL_Fortran_SRCS} ${GPTL_C_SRCS})
2828

2929
target_include_directories (gptl
30-
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
31-
PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
30+
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
31+
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
3232

3333
target_compile_definitions (gptl
3434
PUBLIC INCLUDE_CMAKE_FCI)
@@ -54,15 +54,25 @@ endif ()
5454
# DEFINE THE INSTALL
5555
#==============================================================================
5656

57+
set_target_properties (gptl PROPERTIES EXPORT_NAME PIO_GPTL)
58+
5759
# Library
58-
install (TARGETS gptl DESTINATION lib)
60+
install (TARGETS gptl
61+
EXPORT PIOGPTLTargets
62+
DESTINATION lib
63+
INCLUDES DESTINATION include)
5964

6065
# Header/Include File
6166
install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/gptl.h DESTINATION include)
6267

6368
# Fortran Modules
6469
install (FILES ${GPTL_Fortran_MODS} DESTINATION include)
6570

71+
install(EXPORT PIOGPTLTargets
72+
FILE PIOGPTLTargets.cmake
73+
NAMESPACE PIO::
74+
DESTINATION lib/cmake/PIO)
75+
6676
#==============================================================================
6777
# DEFINE THE DEPENDENCIES
6878
#==============================================================================

0 commit comments

Comments
 (0)