Skip to content

Commit b696214

Browse files
authored
Add install function for cpp_bindgen libs (#31)
Additional change: Bump CMake version to the latest release (to avoid doing small version increments), reason is a change in CMake 3.13.0 ("The install(TARGETS) command learned to install targets created outside the current directory.")
1 parent ecd8a3d commit b696214

File tree

10 files changed

+82
-35
lines changed

10 files changed

+82
-35
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.12.4)
1+
cmake_minimum_required(VERSION 3.14.5)
22
# when increasing the minium required version, consider updating in examples as well
33

44
file(STRINGS "version.txt" __CPP_BINDGEN_VERSION)

README.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,15 @@ FetchContent_Declare(
1717
GIT_REPOSITORY https://github.com/GridTools/cpp_bindgen.git
1818
GIT_TAG master # consider replacing master by a tagged version
1919
)
20-
FetchContent_GetProperties(cpp_bindgen)
21-
if(NOT cpp_bindgen_POPULATED)
22-
FetchContent_Populate(cpp_bindgen)
23-
add_subdirectory(${cpp_bindgen_SOURCE_DIR} ${cpp_bindgen_BINARY_DIR})
24-
endif()
20+
FetchContent_MakeAvailable(cpp_bindgen)
2521
```
2622

2723
See also https://github.com/GridTools/cpp_bindgen/tree/master/example/simple_fetch_content.
2824

2925
##### Requirements
3026

3127
- Boost (1.65.1 or later)
32-
- CMake (3.12.4 or later)
28+
- CMake (3.14.5 or later)
3329

3430
##### Known issues
3531

cmake/cpp_bindgen.cmake.in

+52-11
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# - <library_name>_c the C-bindings with <library_name> linked to it
2727
# - <library_name>_fortran the Fortran-bindings with <library_name> linked to it
2828

29-
include_guard()
29+
include_guard(GLOBAL)
3030

3131
option(GT_ENABLE_BINDINGS_GENERATION "If turned off, bindings will not be generated." ON)
3232

@@ -35,7 +35,7 @@ set(__CPP_BINDGEN_SOURCE_DIR @__CPP_BINDGEN_SOURCE_DIR@)
3535
set(__CPP_BINDGEN_INCLUDE_DIR @__CPP_BINDGEN_INCLUDE_DIR@)
3636

3737
add_library(cpp_bindgen_interface INTERFACE)
38-
target_include_directories(cpp_bindgen_interface INTERFACE ${__CPP_BINDGEN_INCLUDE_DIR})
38+
target_include_directories(cpp_bindgen_interface INTERFACE $<BUILD_INTERFACE:${__CPP_BINDGEN_INCLUDE_DIR}> $<INSTALL_INTERFACE:include>)
3939
target_compile_features(cpp_bindgen_interface INTERFACE cxx_std_11)
4040
target_compile_definitions(cpp_bindgen_interface INTERFACE BOOST_PP_VARIADICS=1)
4141
if(CPP_BINDGEN_GT_LEGACY)
@@ -65,18 +65,25 @@ function(bindgen_enable_fortran_library target_name)
6565
set(__CPP_BINDGEN_CMAKE_DIR @__CPP_BINDGEN_CMAKE_DIR@)
6666

6767
if(CMAKE_Fortran_COMPILER_LOADED)
68-
if(NOT TARGET fortran_bindings_handle)
69-
add_library(fortran_bindings_handle ${__CPP_BINDGEN_SOURCE_DIR}/cpp_bindgen/array_descriptor.f90 ${__CPP_BINDGEN_SOURCE_DIR}/cpp_bindgen/handle.f90)
70-
target_link_libraries(fortran_bindings_handle PUBLIC cpp_bindgen_handle)
71-
target_include_directories(fortran_bindings_handle PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
72-
include(${__CPP_BINDGEN_CMAKE_DIR}/fortran_helpers.cmake)
73-
bindgen_enable_fortran_preprocessing_on_target(fortran_bindings_handle)
68+
if(NOT TARGET cpp_bindgen_fortran_handle)
69+
add_library(cpp_bindgen_fortran_handle ${__CPP_BINDGEN_SOURCE_DIR}/cpp_bindgen/array_descriptor.f90 ${__CPP_BINDGEN_SOURCE_DIR}/cpp_bindgen/handle.f90)
70+
# the following variable is used to install the .mod files in install_cpp_bindgen_targets() and
71+
# therefore needs to be available project-wide
72+
set(CPP_BINDGEN_Fortran_MODULES_DIRECTORY ${CMAKE_BINARY_DIR}/cpp_bindgen_modules
73+
CACHE INTERNAL "Directory for Fortran modules of cpp_bindgen")
74+
set_target_properties(cpp_bindgen_fortran_handle PROPERTIES Fortran_MODULE_DIRECTORY ${CPP_BINDGEN_Fortran_MODULES_DIRECTORY})
75+
target_link_libraries(cpp_bindgen_fortran_handle PUBLIC cpp_bindgen_handle)
76+
target_include_directories(cpp_bindgen_fortran_handle INTERFACE $<BUILD_INTERFACE:${CPP_BINDGEN_Fortran_MODULES_DIRECTORY}> $<INSTALL_INTERFACE:include/>)
77+
include(${__CPP_BINDGEN_CMAKE_DIR}/cpp_bindgen_fortran_helpers.cmake)
78+
bindgen_enable_fortran_preprocessing_on_target(cpp_bindgen_fortran_handle)
7479
endif()
7580
if(NOT TARGET ${target_name}_fortran)
7681
set_source_files_properties(GT_${${target_name}_fortran_bindings_path} PROPERTIES GENERATED TRUE)
77-
add_library(${target_name}_fortran EXCLUDE_FROM_ALL ${GT_${target_name}_fortran_bindings_path})
82+
add_library(${target_name}_fortran EXCLUDE_FROM_ALL ${CPP_BINDGEN_${target_name}_fortran_bindings_path})
7883
target_link_libraries(${target_name}_fortran PUBLIC ${target_name})
79-
target_link_libraries(${target_name}_fortran PUBLIC fortran_bindings_handle)
84+
target_link_libraries(${target_name}_fortran PUBLIC cpp_bindgen_fortran_handle)
85+
# location of .mod file (we cannot know the INSTALL_INTERFACE directory here)
86+
target_include_directories(${target_name}_fortran PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
8087
add_dependencies(${target_name}_fortran ${target_name}_declarations)
8188
endif()
8289
elseif(NOT ${ARGN}) # internal: the second (optional) parameter can be used to surpress this fatal error
@@ -159,7 +166,41 @@ function(bindgen_add_library target_name)
159166
# bindings Fortran library
160167
# Export the name of the generated file. The variable needs to exist in the whole cmake!
161168
# Reason: see description of bindgen_enable_fortran_library().
162-
set(GT_${target_name}_fortran_bindings_path ${bindings_fortran_decl_filename}
169+
set(CPP_BINDGEN_${target_name}_fortran_bindings_path ${bindings_fortran_decl_filename}
163170
CACHE INTERNAL "Path to the generated Fortran file for ${target_name}")
164171
bindgen_enable_fortran_library(${target_name} TRUE)
165172
endfunction()
173+
174+
# install_cpp_bindgen_targets()
175+
#
176+
# cpp_bindgen contains some generic files which are being built on first use of bindgen_add_library().
177+
# These libraries can be installed with this function which takes the same arguments
178+
# as install(TARGETS targets... [other-options]), except
179+
# - you must not specify "TARGETS targets..." but only all [other-options].
180+
# - if Fortran is enabled Fortran_MODULE_DESTINATION needs to be set to the location where Fortran modules
181+
# should be installed to (e.g. include)
182+
function(install_cpp_bindgen_targets)
183+
set(options)
184+
set(oneValueArgs Fortran_MODULE_DESTINATION)
185+
set(multiValueArgs TARGETS)
186+
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
187+
188+
if(ARG_TARGETS)
189+
message(FATAL_ERROR "install_cpp_bindgen_targets() must not be called with TARGETS argument.")
190+
endif()
191+
192+
if(TARGET cpp_bindgen_fortran_handle) # Fortran is enabled, we need to install a mod file
193+
if(ARG_Fortran_MODULE_DESTINATION)
194+
install(DIRECTORY ${CPP_BINDGEN_Fortran_MODULES_DIRECTORY}/ DESTINATION ${ARG_Fortran_MODULE_DESTINATION})
195+
else()
196+
message(WARNING "Fortran_MODULE_DESTINATION was NOT specified, but Fortran was enabled. Modules files for
197+
cpp_bindgen won't be installed.")
198+
endif()
199+
install(TARGETS cpp_bindgen_fortran_handle ${ARG_UNPARSED_ARGUMENTS})
200+
else()
201+
if(ARG_Fortran_MODULE_DESTINATION)
202+
message(WARNING "Fortran_MODULE_DESTINATION was specified, but Fortran is disabled.")
203+
endif()
204+
endif()
205+
install(TARGETS cpp_bindgen_generator cpp_bindgen_handle cpp_bindgen_interface ${ARG_UNPARSED_ARGUMENTS})
206+
endfunction()
File renamed without changes.

cmake/export.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ unset(__CPP_BINDGEN_INCLUDE_DIR)
5454

5555
set(CMAKE_SOURCES
5656
"${PROJECT_SOURCE_DIR}/cmake/cpp_bindgen_generate.cmake"
57-
"${PROJECT_SOURCE_DIR}/cmake/fortran_helpers.cmake"
57+
"${PROJECT_SOURCE_DIR}/cmake/cpp_bindgen_fortran_helpers.cmake"
5858
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/build-install/lib/cmake/cpp_bindgen.cmake"
5959
)
6060
set(CBINDINGS_SOURCES

example/simple/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.12.4)
1+
cmake_minimum_required(VERSION 3.14.5)
22
project(simple_example LANGUAGES CXX C Fortran)
33

44
# 1) find installed cpp_bindgen version
+19-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.12.4)
1+
cmake_minimum_required(VERSION 3.14.5)
22
project(simple_example_with_fetch_content LANGUAGES CXX Fortran)
33

44
# 1) fetch cpp_bindgen from the repository
@@ -11,16 +11,28 @@ FetchContent_Declare(
1111
# GIT_REPOSITORY https://github.com/GridTools/cpp_bindgen.git
1212
# GIT_TAG master # consider replacing master by a tagged version
1313
)
14-
# FetchContent_MakeAvailable(cpp_bindgen) for CMake >= 3.14, otherwise:
15-
FetchContent_GetProperties(cpp_bindgen)
16-
if(NOT cpp_bindgen_POPULATED)
17-
FetchContent_Populate(cpp_bindgen)
18-
add_subdirectory(${cpp_bindgen_SOURCE_DIR} ${cpp_bindgen_BINARY_DIR})
19-
endif()
14+
FetchContent_MakeAvailable(cpp_bindgen)
2015

2116
# 2) create a library with bindings. This will generate the files simple.h and simple.f90 which can be included within C and Fortran. In CMake you can use them by linking against `simple_c`or `simple_fortran`.
2217
bindgen_add_library(simple SOURCES simple.cpp)
2318

2419
# 3) link the generated library to a fortran executable
2520
add_executable(driver driver.f90)
2621
target_link_libraries(driver simple_fortran)
22+
23+
# 4) optional: demonstrates installing the library with CMake
24+
# installs general cpp_bindgen targets
25+
install_cpp_bindgen_targets(
26+
EXPORT simple_fortran_targets
27+
Fortran_MODULE_DESTINATION include
28+
LIBRARY DESTINATION lib
29+
ARCHIVE DESTINATION lib
30+
)
31+
# install your library (simple) and the generated bindings library (simple_fortran) as usual
32+
install(
33+
TARGETS simple simple_fortran
34+
EXPORT simple_fortran_targets
35+
LIBRARY DESTINATION lib
36+
ARCHIVE DESTINATION lib
37+
)
38+
install(EXPORT simple_fortran_targets DESTINATION cmake NAMESPACE SimpleFortran::)

include/cpp_bindgen/export_gt_legacy.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
#define GT_EXPORT_BINDING_8(...) BINDGEN_EXPORT_BINDING_8(__VA_ARGS__)
5454
#define GT_EXPORT_BINDING_9(...) BINDGEN_EXPORT_BINDING_9(__VA_ARGS__)
5555

56+
#define GT_EXPORT_BINDING_WRAPPED(...) BINDGEN_EXPORT_BINDING_WRAPPED(__VA_ARGS__)
57+
5658
#define GT_EXPORT_BINDING_WRAPPED_0(...) BINDGEN_EXPORT_BINDING_WRAPPED_0(__VA_ARGS__)
5759
#define GT_EXPORT_BINDING_WRAPPED_1(...) BINDGEN_EXPORT_BINDING_WRAPPED_1(__VA_ARGS__)
5860
#define GT_EXPORT_BINDING_WRAPPED_2(...) BINDGEN_EXPORT_BINDING_WRAPPED_2(__VA_ARGS__)

jenkins/build.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ HOST=$(hostname)
66
test -n "${CSCS_ACCOUNT}" || CSCS_ACCOUNT=d75
77

88
if [[ "$HOST" == kesch* || "$HOST" == escha* ]]; then
9-
module load cmake/3.13.4
9+
module load /users/jenkins/easybuild/kesch/modules/all/cmake/3.14.5
1010
module load PE/17.06
1111
module load craype-haswell
1212
module load craype-network-infiniband
@@ -21,7 +21,7 @@ if [[ "$HOST" == kesch* || "$HOST" == escha* ]]; then
2121
elif [[ "$HOST" == tave* ]]; then
2222
module switch PrgEnv-cray PrgEnv-gnu
2323
module rm CMake
24-
module load /users/jenkins/easybuild/tave/modules/all/CMake/3.12.4
24+
module load /users/jenkins/easybuild/tave/modules/all/CMake/3.14.5
2525
export BOOST_ROOT=/project/c14/install/kesch/boost/boost_1_67_0
2626
RUN_PREFIX=""
2727

@@ -30,7 +30,7 @@ elif [[ "$HOST" == daint* ]]; then
3030
module load cudatoolkit/9.2.148_3.19-6.0.7.1_2.1__g3d9acc8
3131
module switch PrgEnv-cray PrgEnv-gnu
3232
module rm CMake
33-
module load /users/jenkins/easybuild/daint/haswell/modules/all/CMake/3.12.4
33+
module load /users/jenkins/easybuild/daint/haswell/modules/all/CMake/3.14.5
3434
export BOOST_ROOT=/project/c14/install/daint/boost/boost_1_67_0
3535
export CUDA_ARCH=sm_60
3636
RUN_PREFIX="srun -C gpu -p cscsci --account=$CSCS_ACCOUNT --time=00:30:00"

tests/CMakeLists.txt

+1-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ FetchContent_Declare(
77
GIT_REPOSITORY https://github.com/google/googletest.git
88
GIT_TAG release-1.8.1
99
)
10-
FetchContent_GetProperties(googletest)
11-
if(NOT googletest_POPULATED)
12-
FetchContent_Populate(googletest)
13-
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR})
14-
endif()
10+
FetchContent_MakeAvailable(googletest)
1511

1612
include(${PROJECT_SOURCE_DIR}/cmake/detect_features.cmake)
1713
detect_cuda()

0 commit comments

Comments
 (0)