Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update build scripts #15

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ project(
VERSION 0.3.0
)

# Get the macros and functions we'll need
include("${PROJECT_SOURCE_DIR}/cmake/helper.cmake")

# Set a default build type if none was specified
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'Release' as none was specified.")
Expand All @@ -27,21 +30,13 @@ set(PROJECT_INCLUDE_DIR ${PROJECT_BINARY_DIR}/include)

# Build
add_subdirectory(src)
add_library(${PROJECT_NAME} ${FORIMAGE_SOURCES})
set_target_properties(
${PROJECT_NAME}
PROPERTIES
POSITION_INDEPENDENT_CODE TRUE
OUTPUT_NAME ${PROJECT_NAME}
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
Fortran_MODULE_DIRECTORY ${CMAKE_INSTALL_INCLUDEDIR}
)
target_include_directories(
${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${PROJECT_INCLUDE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
add_fortran_library(
${PROJECT_NAME}
${PROJECT_INCLUDE_DIR}
${CMAKE_INSTALL_INCLUDEDIR}
${PROJECT_VERSION}
${PROJECT_VERSION_MAJOR}
${FORIMAGE_SOURCES}
)

# Installation
Expand Down
75 changes: 75 additions & 0 deletions cmake/helper.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# helper.cmake
#
# A collection of macros and functions making life with CMake and Fortran a
# bit simpler.

# Use to include and export headers
function(include_headers lib dir install_dir)
target_include_directories(
${lib}
INTERFACE
$<BUILD_INTERFACE:${dir}>
$<INSTALL_INTERFACE:${install_dir}>
)
endfunction()

# Use instead of add_library.
function(add_fortran_library lib_name mod_dir include_install_dir version major)
add_library(${lib_name} ${ARGN})
set_target_properties(
${lib_name}
PROPERTIES
POSITION_INDEPENDENT_CODE TRUE
OUTPUT_NAME ${lib_name}
VERSION ${version}
SOVERSION ${major}
Fortran_MODULE_DIRECTORY ${include_install_dir}
)
target_include_directories(
${lib_name}
PUBLIC
$<BUILD_INTERFACE:${mod_dir}>
$<INSTALL_INTERFACE:${include_install_dir}>
)
endfunction()

# Installs the library
function(install_library lib_name lib_install_dir bin_install_dir mod_dir install_dir)
install(
TARGETS ${lib_name}
EXPORT ${lib_name}Targets
RUNTIME DESTINATION ${bin_install_dir}
LIBRARY DESTINATION ${lib_install_dir}
ARCHIVE DESTINATION ${lib_install_dir}
INCLUDES DESTINATION ${install_dir}/include
)
install(
DIRECTORY ${mod_dir}
DESTINATION ${install_dir}
)
endfunction()

# Install the documentation files
function(install_documentation doc_dir install_dir)
install(
DIRECTORY ${doc_dir}
DESTINATION ${install_dir}
)
endfunction()

# Links the supplied library
function(link_library targ lib include_dir)
target_link_libraries(${targ} ${lib})
target_include_directories(${targ} PUBLIC $<BUILD_INTERFACE:${include_dir}>)
endfunction()

# ------------------------------------------------------------------------------
# Helpful Macros
macro(print_all_variables)
message(STATUS "---------- CURRENTLY DEFINED VARIABLES -----------")
get_cmake_property(varNames VARIABLES)
foreach(varName ${varNames})
message(STATUS ${varName} = ${${varName}})
endforeach()
message(STATUS "---------- END ----------")
endmacro()
25 changes: 8 additions & 17 deletions install/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
# Get the macros and functions we'll need
include("${PROJECT_SOURCE_DIR}/cmake/helper.cmake")
include(CMakePackageConfigHelpers)

# Install the library and necessary include files
install(
TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_PREFIX}/include
)
install(
DIRECTORY ${PROJECT_INCLUDE_DIR}
DESTINATION ${CMAKE_INSTALL_PREFIX}
)

# Install the C API headers
install(
DIRECTORY ${PROJECT_SOURCE_DIR}/include
DESTINATION ${CMAKE_INSTALL_PREFIX}
install_library(
${PROJECT_NAME}
${CMAKE_INSTALL_LIBDIR}
${CMAKE_INSTALL_BINDIR}
${PROJECT_INCLUDE_DIR}
${CMAKE_INSTALL_PREFIX}
)

# Define the version file
Expand Down