Skip to content

Commit 14549b1

Browse files
authored
Merge pull request #28 from jchristopherson/main
CMake build
2 parents 0cc8513 + 21b0a27 commit 14549b1

File tree

13 files changed

+379
-0
lines changed

13 files changed

+379
-0
lines changed

.github/workflows/cmake.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: CMake
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
env:
10+
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
11+
BUILD_TYPE: Release
12+
13+
jobs:
14+
build:
15+
# The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac.
16+
# You can convert this to a matrix build if you need cross-platform coverage.
17+
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- uses: actions/checkout@v3
22+
23+
- name: Configure CMake
24+
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
25+
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
26+
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
27+
28+
- name: Build
29+
# Build your program with the given configuration
30+
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
31+
32+
- name: Test
33+
working-directory: ${{github.workspace}}/build
34+
# Execute tests defined by the CMake configuration.
35+
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
36+
run: ctest -C ${{env.BUILD_TYPE}}
37+

CMakeLists.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Master CMAKE Build Script
2+
cmake_minimum_required(VERSION 3.24)
3+
project(
4+
fftpack
5+
LANGUAGES Fortran
6+
VERSION 1.0.0
7+
)
8+
9+
# Get helper macros and functions
10+
include("${PROJECT_SOURCE_DIR}/cmake/helper.cmake")
11+
12+
# Confgiure everything
13+
add_subdirectory(configure)
14+
15+
# Source
16+
add_subdirectory(src)
17+
add_fortran_library(
18+
${PROJECT_NAME}
19+
${PROJECT_INCLUDE_DIR}
20+
${CMAKE_INSTALL_INCLUDEDIR}
21+
${PROJECT_VERSION}
22+
${PROJECT_VERSION_MAJOR}
23+
${FFTPACK_SOURCES}
24+
)
25+
26+
# Installation
27+
add_subdirectory(install)
28+
29+
# Testing
30+
option(BUILD_TESTING "Build tests")
31+
include(CTest)
32+
message(STATUS "Build FFTPACK tests: ${BUILD_TESTING}")
33+
if (BUILD_TESTING)
34+
enable_testing()
35+
add_subdirectory(dependencies)
36+
add_subdirectory(test)
37+
endif()
38+
39+
# Examples
40+
option(BUILD_FFTPACK_EXAMPLES "Build FFTPACK examples")
41+
message(STATUS "Build FFTPACK examples: ${BUILD_FFTPACK_EXAMPLES}")
42+
if (BUILD_FFTPACK_EXAMPLES)
43+
add_subdirectory(example)
44+
endif()

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ Alternatively, you can build using provided `Makefile`:
3333
make
3434
```
3535

36+
## Build with CMake
37+
This library can also be built using CMake. For instructions see [Running CMake](https://cmake.org/runningcmake/). CMake version 3.24 or higher is required.
38+
3639
## Links
3740
- [netlib/dfftpack1.0(fftpack4.0)](http://www.netlib.org/fftpack/)
3841
- [Documents of fft routines in GNU/gsl based on `netlib/fftpack`](https://www.gnu.org/software/gsl/doc/html/fft.html#)

cmake/helper.cmake

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# helper.cmake
2+
#
3+
# A collection of macros and functions making life with CMake and Fortran a
4+
# bit simpler.
5+
6+
# Use to include and export headers
7+
function(include_headers lib dir install_dir)
8+
target_include_directories(
9+
${lib}
10+
INTERFACE
11+
$<BUILD_INTERFACE:${dir}>
12+
$<INSTALL_INTERFACE:${install_dir}>
13+
)
14+
endfunction()
15+
16+
# Use instead of add_library.
17+
function(add_fortran_library lib_name mod_dir include_install_dir version major)
18+
add_library(${lib_name} ${ARGN})
19+
set_target_properties(
20+
${lib_name}
21+
PROPERTIES
22+
POSITION_INDEPENDENT_CODE TRUE
23+
OUTPUT_NAME ${lib_name}
24+
VERSION ${version}
25+
SOVERSION ${major}
26+
Fortran_MODULE_DIRECTORY ${include_install_dir}
27+
)
28+
target_include_directories(
29+
${lib_name}
30+
PUBLIC
31+
$<BUILD_INTERFACE:${mod_dir}>
32+
$<INSTALL_INTERFACE:${include_install_dir}>
33+
)
34+
endfunction()
35+
36+
# Installs the library
37+
function(install_library lib_name lib_install_dir bin_install_dir mod_dir install_dir)
38+
install(
39+
TARGETS ${lib_name}
40+
EXPORT ${lib_name}Targets
41+
RUNTIME DESTINATION ${bin_install_dir}
42+
LIBRARY DESTINATION ${lib_install_dir}
43+
ARCHIVE DESTINATION ${lib_install_dir}
44+
INCLUDES DESTINATION ${install_dir}/include
45+
)
46+
install(
47+
DIRECTORY ${mod_dir}
48+
DESTINATION ${install_dir}
49+
)
50+
endfunction()
51+
52+
# Install the documentation files
53+
function(install_documentation doc_dir install_dir)
54+
install(
55+
DIRECTORY ${doc_dir}
56+
DESTINATION ${install_dir}
57+
)
58+
endfunction()
59+
60+
# Links the supplied library
61+
function(link_library targ lib include_dir)
62+
target_link_libraries(${targ} ${lib})
63+
target_include_directories(${targ} PUBLIC $<BUILD_INTERFACE:${include_dir}>)
64+
endfunction()
65+
66+
# ------------------------------------------------------------------------------
67+
# Helpful Macros
68+
macro(print_all_variables)
69+
message(STATUS "---------- CURRENTLY DEFINED VARIABLES -----------")
70+
get_cmake_property(varNames VARIABLES)
71+
foreach(varName ${varNames})
72+
message(STATUS ${varName} = ${${varName}})
73+
endforeach()
74+
message(STATUS "---------- END ----------")
75+
endmacro()

configure/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Get the macros and functions we'll need
2+
include("${PROJECT_SOURCE_DIR}/cmake/helper.cmake")
3+
4+
# Set a default build type if none was specified
5+
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
6+
message(STATUS "Setting build type to 'Release' as none was specified.")
7+
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
8+
# Set the possible values of build type for cmake-gui
9+
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release")
10+
endif()
11+
12+
# By default, static library
13+
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
14+
15+
# Export all symbols on Windows when building libraries
16+
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
17+
18+
# Utilize the GNU installation structure
19+
include(GNUInstallDirs)
20+
21+
# Locate the local include directory
22+
set(PROJECT_INCLUDE_DIR ${PROJECT_BINARY_DIR}/include)
23+
set(PROJECT_INCLUDE_DIR ${PROJECT_INCLUDE_DIR} PARENT_SCOPE)

dependencies/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_subdirectory(test-drive)
2+
set(test-drive_LIBRARY ${test-drive_LIBRARY} PARENT_SCOPE)
3+
set(test-drive_INCLUDE_DIR ${test-drive_INCLUDE_DIR} PARENT_SCOPE)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Get the macros and functions we'll need
2+
include("${PROJECT_SOURCE_DIR}/cmake/helper.cmake")
3+
include(FetchContent)
4+
5+
# If found, use the installed version; else, import the library
6+
if (${test-drive_FOUND})
7+
# Inform the user of what's going on
8+
message(STATUS "TEST-DRIVE (${test-drive_VERSION}) library found.")
9+
10+
# Get the mod file location
11+
get_target_property(test-drive_INCLUDE_DIR test-drive INTERFACE_INCLUDE_DIRECTORIES)
12+
else()
13+
# Inform the user of what's going on
14+
message(STATUS "TEST-DRIVE library not found. Downloading appropriate repository.")
15+
16+
# Fetch the proper content
17+
FetchContent_Declare(
18+
test-drive
19+
GIT_TAG "origin/main"
20+
GIT_REPOSITORY "https://github.com/fortran-lang/test-drive"
21+
OVERRIDE_FIND_PACKAGE
22+
)
23+
24+
FetchContent_MakeAvailable(test-drive)
25+
set(test-drive_INCLUDE_DIR ${test-drive_BINARY_DIR}/include)
26+
endif()
27+
28+
# Make a parent-scope variable for the library
29+
set(test-drive_LIBRARY test-drive)
30+
set(test-drive_LIBRARY ${test-drive_LIBRARY} PARENT_SCOPE)
31+
32+
# Make a parent-scope variable locating the include directory for test-drive
33+
set(test-drive_INCLUDE_DIR ${test-drive_INCLUDE_DIR} PARENT_SCOPE)

example/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_executable(bench1 bench1.f90)
2+
target_link_libraries(bench1 fftpack)

install/CMakeLists.txt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Get the macros and functions we'll need
2+
include("${PROJECT_SOURCE_DIR}/cmake/helper.cmake")
3+
include(CMakePackageConfigHelpers)
4+
5+
# Install the library and necessary include files
6+
install_library(
7+
${PROJECT_NAME}
8+
${CMAKE_INSTALL_LIBDIR}
9+
${CMAKE_INSTALL_BINDIR}
10+
${PROJECT_INCLUDE_DIR}
11+
${CMAKE_INSTALL_PREFIX}
12+
)
13+
14+
# Define the version file
15+
write_basic_package_version_file(
16+
${CMAKE_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
17+
VERSION ${PROJECT_VERSION}
18+
COMPATIBILITY AnyNewerVersion
19+
)
20+
21+
export(
22+
EXPORT ${PROJECT_NAME}Targets
23+
FILE ${CMAKE_BINARY_DIR}/${PROJECT_NAME}Targets.cmake
24+
)
25+
26+
# Define the configuration file
27+
configure_file(
28+
${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in
29+
${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake
30+
COPYONLY
31+
)
32+
33+
install(
34+
EXPORT ${PROJECT_NAME}Targets
35+
FILE ${PROJECT_NAME}Targets.cmake
36+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
37+
)
38+
install(
39+
FILES
40+
${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake
41+
${CMAKE_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
42+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
43+
)
44+
45+
configure_file(
46+
${CMAKE_CURRENT_SOURCE_DIR}/template.pc
47+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc
48+
@ONLY
49+
)
50+
install(
51+
FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc
52+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
53+
)

install/fftpackConfig.cmake.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if (NOT TARGET fftpack)
2+
include("${CMAKE_CURRENT_LIST_DIR}/fftpackTargets.cmake")
3+
endif()

0 commit comments

Comments
 (0)