Skip to content

Commit

Permalink
Add CMake support
Browse files Browse the repository at this point in the history
  • Loading branch information
jchristopherson committed Jan 7, 2024
1 parent 0045e97 commit bf25752
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@
*.exe
*.out
*.app

# Directories
build/
69 changes: 69 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
cmake_minimum_required(VERSION 3.24)
project(
forcolormap
LANGUAGES Fortran
VERSION 0.0.0
)

# 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.")
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release")
endif()

# By default, static library
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)

# Export all symbols on Windows when building libraries
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)

# Utilize the GNU installation structure
include(GNUInstallDirs)

# Locate the local include directory
set(PROJECT_INCLUDE_DIR ${PROJECT_BINARY_DIR}/include)

# Dependencies
add_subdirectory(dependencies)

# Build
add_subdirectory(src)
add_library(${PROJECT_NAME} ${FORCOLORMAP_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}>
)
target_link_libraries(${PROJECT_NAME} ${forimage_LIBRARY})
target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${forimage_INCLUDE_DIR}>)

# Installation
add_subdirectory(install)

# Testing
option(BUILD_TESTING "Build tests")
include(CTest)
message(STATUS "Build FORCOLORMAP tests: ${BUILD_TESTING}")
if (BUILD_TESTING)
enable_testing()
add_subdirectory(test)
endif()

# Examples
option(BUILD_FORCOLORMAP_EXAMPLES "Build examples")
message(STATUS "Build FORCOLORMAP examples: ${BUILD_FORCOLORMAP_EXAMPLES}")
if (BUILD_FORCOLORMAP_EXAMPLES)
add_subdirectory(example)
endif()
4 changes: 4 additions & 0 deletions dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Get FORIMAGE
add_subdirectory(forimage)
set(forimage_LIBRARY ${forimage_LIBRARY} PARENT_SCOPE)
set(forimage_INCLUDE_DIR ${forimage_INCLUDE_DIR} PARENT_SCOPE)
29 changes: 29 additions & 0 deletions dependencies/forimage/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Get the macros and functions we'll need
include(FetchContent)

# Fetch the proper content
FetchContent_Declare(
forimage
GIT_REPOSITORY "https://github.com/gha3mi/forimage"
GIT_TAG main
)

FetchContent_MakeAvailable(forimage)

if (WIN32)
if (BUILD_SHARED_LIBS)
add_custom_command(
TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:forimage>
$<TARGET_FILE_DIR:${PROJECT_NAME}
)
endif()
endif()

set(forimage_INCLUDE_DIR ${forimage_BINARY_DIR}/include)
set(forimage_INCLUDE_DIR ${forimage_INCLUDE_DIR} PARENT_SCOPE)

# Make a parent-scope variable for the library
set(forimage_LIBRARY forimage)
set(forimage_LIBRARY ${forimage_LIBRARY} PARENT_SCOPE)
19 changes: 19 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# DEMO_REVERSE
add_executable(demo_reverse demo_reverse.f90)
target_link_libraries(demo_reverse ${PROJECT_NAME})

# DEMO
add_executable(demo demo.f90)
target_link_libraries(demo ${PROJECT_NAME})

# EXAMPLE1
add_executable(example1 example1.f90)
target_link_libraries(example1 ${PROJECT_NAME})

# EXTRACT
add_executable(extract extract.f90)
target_link_libraries(extract ${PROJECT_NAME})

# INFO
add_executable(info info.f90)
target_link_libraries(info ${PROJECT_NAME})
62 changes: 62 additions & 0 deletions install/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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}
)

# Define the version file
write_basic_package_version_file(
${CMAKE_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)

export(
EXPORT ${PROJECT_NAME}Targets
FILE ${CMAKE_BINARY_DIR}/${PROJECT_NAME}Targets.cmake
)

# Define the configuration file
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in
${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake
COPYONLY
)

install(
EXPORT ${PROJECT_NAME}Targets
FILE ${PROJECT_NAME}Targets.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)
install(
FILES
${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake
${CMAKE_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)

configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/template.pc
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc
@ONLY
)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
)
3 changes: 3 additions & 0 deletions install/forcolormapConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if (NOT TARGET forcolormap)
include("${CMAKE_CURRENT_LIST_DIR}/forcolormapTargets.cmake")
endif()
10 changes: 10 additions & 0 deletions install/template.pc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
prefix = @CMAKE_INSTALL_PREFIX@
libdir = ${prefix}/@CMAKE_INSTALL_FULL_LIBDIR@
includedir = ${prefix}/@CMAKE_INSTALL_INCLUDEDIR@

Name: @PROJECT_NAME@
Description: FORCOLORMAP is a Fortran fpm library for colormaps.
Version: @PROJECT_VERSION@
URL: https://github.com/vmagnin/forcolormap
Libs: -L${libdir} -l@PROJECT_NAME@
Cflags: -I${includedir}
11 changes: 11 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
set(dir ${CMAKE_CURRENT_SOURCE_DIR})

set(FORCOLORMAP_SOURCES
${dir}/colormap_class.f90
${dir}/colormap_parameters.f90
${dir}/colormaps_info.f90
${dir}/matplotlib_colormaps.f90
${dir}/miscellaneous_colormaps.f90
${dir}/scientific_colour_maps.f90
)
set(FORCOLORMAP_SOURCES ${FORCOLORMAP_SOURCES} PARENT_SCOPE)
12 changes: 12 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function(build_test test_name src)
add_executable(${test_name} ${src})
target_link_libraries(${test_name} PRIVATE ${PROJECT_NAME})
target_include_directories(${test_name} PUBLIC $<BUILD_INTERFACE:${PROJECT_INCLUDE_DIR}>)
add_test(
NAME ${test_name}
WORKING_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
COMMAND $<TARGET_FILE:${test_name}>
)
endfunction()

build_test(check check.f90)

0 comments on commit bf25752

Please sign in to comment.