Skip to content

Commit 5f69f90

Browse files
committed
Generate package file exports with CMake
- set project version (just 0 as dummy) and project description in CMake - export CMake package fortran_lang-config.cmake for CMake projects - export pkg-config file fortran_lang.pc for non-CMake projects
1 parent e46df0a commit 5f69f90

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
@@ -85,6 +85,36 @@ make -f Makefile.manual FYPPFLAGS=-DMAXRANK=4
8585
```
8686
Note that currently the minimum value for maximum rank is 4.
8787

88+
89+
## Using stdlib in your project
90+
91+
The stdlib projects exports CMake package files and pkg-config files to make stdlib usable for other projects.
92+
The package files are located in the library directory in the installation prefix.
93+
94+
For CMake builds of stdlib you can find a local installation with
95+
96+
```cmake
97+
find_package(fortran_stdlib REQUIRED)
98+
...
99+
target_link_libraries(
100+
${PROJECT_NAME}
101+
PRIVATE
102+
fortran_stdlib::fortran_stdlib
103+
)
104+
```
105+
106+
to make the installed stdlib project discoverable add the stdlib directory to the ``CMAKE_PREFIX_PATH``.
107+
The usual install localtion of the package files is ``$PREFIX/lib/cmake/fortran_stdlib``.
108+
109+
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.
110+
The usual install location of the pc-file is ``$PREFIX/lib/pkgconfig``.
111+
In make you can obtain the required compile and link arguments with
112+
113+
```make
114+
STDLIB_CFLAGS := $(shell pkg-config --cflags fortran_stdlib)
115+
STDLIB_LIBS := $(shell pkg-config --libs fortran_stdlib)
116+
```
117+
88118
## Documentation
89119

90120
Documentation is a work in progress (see issue #4) but is currently available at 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)
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)