Skip to content

Commit d7746e2

Browse files
authored
Merge pull request #291 from awvwgk/export-package-files
Export package files
2 parents f4f6045 + 7ef70a0 commit d7746e2

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

CMakeLists.txt

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
cmake_minimum_required(VERSION 3.14.0)
2-
project(fortran_stdlib Fortran)
2+
project(fortran_stdlib
3+
LANGUAGES Fortran
4+
VERSION 0
5+
DESCRIPTION "Community driven and agreed upon de facto standard library for Fortran"
6+
)
37
enable_testing()
48

59
# Follow GNU conventions for installation directories
610
include(GNUInstallDirs)
711

812
include(${PROJECT_SOURCE_DIR}/cmake/stdlib.cmake)
913

14+
# --- CMake specific configuration and package data export
15+
add_subdirectory(config)
16+
1017
# --- compiler options
1118
if(CMAKE_Fortran_COMPILER_ID STREQUAL GNU)
1219
add_compile_options(-fimplicit-none)

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,36 @@ make -f Makefile.manual FYPPFLAGS=-DMAXRANK=4
158158
```
159159

160160

161+
162+
## Using stdlib in your project
163+
164+
The stdlib project exports CMake package files and pkg-config files to make stdlib usable for other projects.
165+
The package files are located in the library directory in the installation prefix.
166+
167+
For CMake builds of stdlib you can find a local installation with
168+
169+
```cmake
170+
find_package(fortran_stdlib REQUIRED)
171+
...
172+
target_link_libraries(
173+
${PROJECT_NAME}
174+
PRIVATE
175+
fortran_stdlib::fortran_stdlib
176+
)
177+
```
178+
179+
To make the installed stdlib project discoverable add the stdlib directory to the ``CMAKE_PREFIX_PATH``.
180+
The usual install localtion of the package files is ``$PREFIX/lib/cmake/fortran_stdlib``.
181+
182+
For non-CMake build systems (like make) you can use the exported pkg-config file by setting ``PKG_CONFIG_PATH`` to include the directory containing the exported pc-file.
183+
The usual install location of the pc-file is ``$PREFIX/lib/pkgconfig``.
184+
In make you can obtain the required compile and link arguments with
185+
186+
```make
187+
STDLIB_CFLAGS := $(shell pkg-config --cflags fortran_stdlib)
188+
STDLIB_LIBS := $(shell pkg-config --libs fortran_stdlib)
189+
```
190+
161191
## Documentation
162192

163193
Documentation is a work in progress (see issue [#4](https://github.com/fortran-lang/stdlib/issues/4)) but already available at [stdlib.fortran-lang.org](https://stdlib.fortran-lang.org).

config/CMakeLists.txt

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# SPDX-Identifier: MIT
2+
3+
# Export a pkg-config file
4+
configure_file(
5+
"${CMAKE_CURRENT_SOURCE_DIR}/template.pc"
6+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
7+
@ONLY
8+
)
9+
install(
10+
FILES
11+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
12+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig"
13+
)
14+
15+
# Export CMake package file
16+
include(CMakePackageConfigHelpers)
17+
configure_package_config_file(
18+
"${CMAKE_CURRENT_SOURCE_DIR}/template.cmake"
19+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake"
20+
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
21+
)
22+
if(BUILD_SHARED_LIBS OR PROJECT_VERSION_MAJOR EQUAL 0)
23+
# Due to the uncertain ABI compatibility of Fortran shared libraries
24+
# limit compatibility for dynamic linking to same minor version.
25+
set(COMPATIBILITY SameMinorVersion)
26+
else()
27+
# Require API compatibility via semantic versioning for static linking.
28+
set(COMPATIBILITY SameMajorVersion)
29+
endif()
30+
write_basic_package_version_file(
31+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake"
32+
VERSION "${PROJECT_VERSION}"
33+
COMPATIBILITY ${COMPATIBILITY}
34+
)
35+
install(
36+
FILES
37+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake"
38+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake"
39+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
40+
)

config/template.cmake

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@PACKAGE_INIT@
2+
3+
if(NOT TARGET "@PROJECT_NAME@::@PROJECT_NAME@")
4+
include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
5+
endif()

config/template.pc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
prefix=@CMAKE_INSTALL_PREFIX@
2+
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
3+
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
4+
5+
Name: @PROJECT_NAME@
6+
Description: @PROJECT_DESCRIPTION@
7+
Version: @PROJECT_VERSION@
8+
Libs: -L${libdir} -l@PROJECT_NAME@
9+
Cflags: -I${includedir}

0 commit comments

Comments
 (0)