Skip to content

Commit

Permalink
upgrading cmake infrastructure
Browse files Browse the repository at this point in the history
This drastically changes the options required for building
fdict.

It is made like this to accommodate sub-project builds.

Signed-off-by: Nick Papior <nickpapior@gmail.com>
  • Loading branch information
zerothi committed Sep 18, 2024
1 parent b04f3c5 commit 6ac903d
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 189 deletions.
89 changes: 51 additions & 38 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
cmake_minimum_required(VERSION 3.17 FATAL_ERROR)

# Include overwrites before setting up the project
set(CMAKE_USER_MAKE_RULES_OVERRIDE "${CMAKE_CURRENT_SOURCE_DIR}/cmake/fdictDefaultFlags.cmake")
set(CMAKE_USER_MAKE_RULES_OVERRIDE
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/fdictDefaultFlags.cmake")

# Define fdict project and languages used
project(fdict
LANGUAGES Fortran
DESCRIPTION "Fortran dictionary for arbitrary data-types"
)

if (NOT DEFINED CMAKE_Fortran_MODULE_DIRECTORY)
set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/fortran_modules)
endif ()
list(APPEND CMAKE_MESSAGE_CONTEXT fdict)

# Project installation follows GNU installation directory convention
include(GNUInstallDirs)

# Export CMake package file
include(CMakePackageConfigHelpers)

project(fdict
LANGUAGES Fortran
DESCRIPTION "Fortran dictionary for arbitrary data-types"
)

# Define project versions
file(STRINGS "${PROJECT_SOURCE_DIR}/VERSION" PROJECT_VERSION)
Expand All @@ -29,28 +29,34 @@ unset(VERSION_LIST)


# Define library specific content
set(PROJECT_AUTHOR "Nick Papior")
set(PROJECT_DESCRIPTION "Fortran dictionary for arbitrary data-types")
set(PROJECT_URL "https://github.com/zerothi/fdict")
set(PROJECT_LICENSE "Mozilla Public License, 2.0 (MPL-2.0)")
option(FDICT_INSTALL "Install fdict (or use as sub-project)" ${PROJECT_IS_TOP_LEVEL})
set(FDICT_AUTHOR "Nick Papior")
set(FDICT_DESCRIPTION "Fortran dictionary for arbitrary data-types")
set(FDICT_URL "https://github.com/zerothi/fdict")
set(FDICT_LICENSE "Mozilla Public License, 2.0 (MPL-2.0)")
message(STATUS "
** PROJECT_NAME = ${PROJECT_NAME}
** Please report any build problems here: ${PROJECT_URL}
** Please report any build problems here: ${FDICT_URL}
")


list(APPEND FYPPFLAGS
"-DPROJECT_NAME=\\\"${PROJECT_NAME}\\\""
"-DPROJECT_VERSION=\\\"${PROJECT_VERSION}\\\""
"-DPROJECT_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}"
"-DPROJECT_VERSION_MINOR=${PROJECT_VERSION_MINOR}"
"-DPROJECT_VERSION_PATCH=${PROJECT_VERSION_PATCH}"
if (NOT DEFINED CMAKE_Fortran_MODULE_DIRECTORY)
set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/fortran_modules)
endif ()

list(APPEND FDICT_FYPPFLAGS
"-DFDICT_NAME=\\\"${PROJECT_NAME}\\\""
"-DFDICT_VERSION=\\\"${PROJECT_VERSION}\\\""
"-DFDICT_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}"
"-DFDICT_VERSION_MINOR=${PROJECT_VERSION_MINOR}"
"-DFDICT_VERSION_PATCH=${PROJECT_VERSION_PATCH}"
)

# Add project options
include(cmake/fdictOptions.cmake)
include(cmake/fdictFyppify.cmake)


# create library.
add_subdirectory(src)

Expand All @@ -75,33 +81,31 @@ set(FDICT_INSTALL_CMAKECONFIG_DIR
mark_as_advanced(FDICT_INSTALL_CMAKECONFIG_DIR)


if(BUILD_SHARED_LIBS OR PROJECT_VERSION_MAJOR EQUAL 0)
# Due to the uncertain ABI compatibility of Fortran shared libraries
# limit compatibility for dynamic linking to same minor version.
set(COMPATIBILITY SameMinorVersion)
else()
# Require API compatibility via semantic versioning for static linking.
set(COMPATIBILITY SameMajorVersion)
endif()


# Create pkg-config file
configure_file(fdict.pc.in fdict.pc @ONLY)
install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig"
)

# Export CMake package file
include(CMakePackageConfigHelpers)

# Export a cmake package configure file
configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/Config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION "${FDICT_INSTALL_CMAKECONFIG_DIR}"
)

if(BUILD_SHARED_LIBS OR PROJECT_VERSION_MAJOR EQUAL 0)
# Due to the uncertain ABI compatibility of Fortran shared libraries
# limit compatibility for dynamic linking to same minor version.
set(COMPATIBILITY SameMinorVersion)
else()
# Require API compatibility via semantic versioning for static linking.
set(COMPATIBILITY SameMajorVersion)
endif()

# Export a package version file
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
Expand All @@ -110,15 +114,22 @@ write_basic_package_version_file(
)

# Install cmake configuration files
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION "${FDICT_INSTALL_CMAKECONFIG_DIR}"
)

install(
FILES "LICENSE"
DESTINATION "${CMAKE_INSTALL_DATADIR}/licences/${PROJECT_NAME}"
)


# Install project
install(EXPORT ${PROJECT_NAME}-targets
install(
EXPORT ${PROJECT_NAME}-targets
FILE ${PROJECT_NAME}Targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION "${FDICT_INSTALL_CMAKECONFIG_DIR}"
Expand All @@ -129,3 +140,5 @@ export(EXPORT ${PROJECT_NAME}-targets
NAMESPACE ${PROJECT_NAME}::
FILE "${PROJECT_NAME}Targets.cmake"
)

list(POP_BACK CMAKE_MESSAGE_CONTEXT)
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ FYPPFLAGS += -DWITH_LOG64=1 # for logical kind(18)
FYPPFLAGS += -DWITH_ISO_C=1 # for enabling c_ptr and c_funptr
```

For `cmake` the same arguments can be made at the command-line.
For `cmake` the variables are all prefixed with `FDICT_`, e.g. `-DFDICT_FYPPFLAGS`,
to ensure there are no clashes with parent programs.

By default `fdict` generates the kind specifications from the `selected_*_kind` routines,
however, if one wishes to use the `iso_fortran_env` module simply add `FYPPFLAGS += -DWITH_ISO_ENV`.
Expand Down
68 changes: 48 additions & 20 deletions cmake/Config.cmake.in
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
@PACKAGE_INIT@

include(CheckLanguage)

# Check that the fortran language is enabled
check_language(Fortran)
if( NOT CMAKE_Fortran_COMPILER )
message(FATAL_ERROR "fdict package requires Fortran language support")
endif()
# should we instead just enable it in the config file?

set(PROJECT_NAME "@PROJECT_NAME@")
string(TOUPPER PROJECT_NAME_UPPER "${PROJECT_NAME}")

set(${PROJECT_NAME}_CMAKE_BUILD_TYPE "@CMAKE_BUILD_TYPE@")
set(${PROJECT_NAME}_Fortran_COMPILER_FLAGS "@CMAKE_Fortran_COMPILER_FLAGS@")
set(${PROJECT_NAME}_Fortran_COMPILER "@CMAKE_Fortran_COMPILER@")
set(${PROJECT_NAME}_Fortran_COMPILER_ID "@CMAKE_Fortran_COMPILER_ID@")
set(${PROJECT_NAME}_VERSION "@PROJECT_VERSION@")

set(${PROJECT_NAME}_WITH_PURE_INTERFACE "@FDICT_WITH_PURE_INTERFACE@")


# Define all the available components
set(_valid_components
Expand All @@ -23,34 +36,49 @@ set(_valid_components
)


string(TOUPPER PROJECT_NAME_UPPER "@PROJECT_NAME@")

set_and_check(${PROJECT_NAME_UPPER}_FYPPFLAGS "@FYPPFLAGS@")
macro(set_default name default value)
if("" STREQUAL "${value}")
set(${name} "${default}")
else()
set(${name} "${value}")
endif()
endmacro()
macro(set_false name value)
set_default(${name} FALSE "${value}")
endmacro()


set(${PROJECT_NAME}_FYPPFLAGS "@FDICT_FYPPFLAGS@")

set_false(${PROJECT_NAME}_WITH_PURE_INTERFACE "@FDICT_WITH_PURE_INTERFACE@")

set_and_check(${PROJECT_NAME_UPPER}_WITH_PURE_INTERFACE "@WITH_PURE_INTERFACE@")
set_false(${PROJECT_NAME}_WITH_INT8 "@FDICT_WITH_INT8@")
set_false(${PROJECT_NAME}_WITH_INT16 "@FDICT_WITH_INT16@")
set_false(${PROJECT_NAME}_WITH_REAL80 "@FDICT_WITH_REAL80@")
set_false(${PROJECT_NAME}_WITH_REAL128 "@FDICT_WITH_REAL128@")
set_false(${PROJECT_NAME}_WITH_LOG8 "@FDICT_WITH_LOG8@")
set_false(${PROJECT_NAME}_WITH_LOG16 "@FDICT_WITH_LOG16@")
set_false(${PROJECT_NAME}_WITH_LOG64 "@FDICT_WITH_LOG64@")
set_false(${PROJECT_NAME}_WITH_ISO_C "@FDICT_WITH_ISO_C@")
set_false(${PROJECT_NAME}_WITH_ISO_ENV "@FDICT_WITH_ISO_ENV@")

set_and_check(${PROJECT_NAME_UPPER}_WITH_INT8 "@WITH_INT8@")
set_and_check(${PROJECT_NAME_UPPER}_WITH_INT16 "@WITH_INT16@")
set_and_check(${PROJECT_NAME_UPPER}_WITH_REAL80 "@WITH_REAL80@")
set_and_check(${PROJECT_NAME_UPPER}_WITH_REAL128 "@WITH_REAL128@")
set_and_check(${PROJECT_NAME_UPPER}_WITH_LOG8 "@WITH_LOG8@")
set_and_check(${PROJECT_NAME_UPPER}_WITH_LOG16 "@WITH_LOG16@")
set_and_check(${PROJECT_NAME_UPPER}_WITH_LOG64 "@WITH_LOG64@")
set_and_check(${PROJECT_NAME_UPPER}_WITH_ISO_C "@WITH_ISO_C@")
set_and_check(${PROJECT_NAME_UPPER}_WITH_ISO_ENV "@WITH_ISO_ENV@")
set_default(${PROJECT_NAME}_MAXRANK 0 "@FDICT_MAXRANK@")
set_default(${PROJECT_NAME}_MAXRANK_INT 0 "@FDICT_MAXRANK_INT@")
set_default(${PROJECT_NAME}_MAXRANK_REAL 0 "@FDICT_MAXRANK_REAL@")
set_default(${PROJECT_NAME}_MAXRANK_CMPLX 0 "@FDICT_MAXRANK_CMPLX@")
set_default(${PROJECT_NAME}_MAXRANK_LOG 0 "@FDICT_MAXRANK_LOG@")
set_default(${PROJECT_NAME}_MAXRANK_ISO_C 0 "@FDICT_MAXRANK_ISO_C@")

set_and_check(${PROJECT_NAME_UPPER}_MAXRANK "@MAXRANK@")
set_and_check(${PROJECT_NAME_UPPER}_MAXRANK_INT "@MAXRANK_INT@")
set_and_check(${PROJECT_NAME_UPPER}_MAXRANK_REAL "@MAXRANK_REAL@")
set_and_check(${PROJECT_NAME_UPPER}_MAXRANK_CMPLX "@MAXRANK_CMPLX@")
set_and_check(${PROJECT_NAME_UPPER}_MAXRANK_LOG "@MAXRANK_LOG@")
set_and_check(${PROJECT_NAME_UPPER}_MAXRANK_ISO_C "@MAXRANK_ISO_C@")
set_and_check(${PROJECT_NAME}_INCLUDE_DIRS "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@")
set_and_check(${PROJECT_NAME}_LIBDIR "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@")

set(${PROJECT_NAME}_FOUND TRUE)
# Convert to components
foreach(comp IN LISTS _valid_components)
set(${PROJECT_NAME_UPPER}_${comp}_FOUND "${${PROJECT_NAME_UPPER}_WITH_${comp}}")
set(${PROJECT_NAME}_${comp}_FOUND "${${PROJECT_NAME}_WITH_${comp}}")
endforeach()

include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")

check_required_components(${PROJECT_NAME_UPPER})
check_required_components("${PROJECT_NAME}")
6 changes: 3 additions & 3 deletions cmake/fdictDefaultFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ else()
CMAKE_Fortran_FLAGS_DEBUG_INIT
)
endif()
string(REPLACE ";" " " CMAKE_Fortran_FLAGS_INIT "${CMAKE_Fortran_FLAGS_INIT}")
string(REPLACE ";" " " CMAKE_Fortran_FLAGS_RELEASE_INIT "${CMAKE_Fortran_FLAGS_RELEASE_INIT}")
string(REPLACE ";" " " CMAKE_Fortran_FLAGS_DEBUG_INIT "${CMAKE_Fortran_FLAGS_DEBUG_INIT}")
list(JOIN CMAKE_Fortran_FLAGS_INIT " " CMAKE_Fortran_FLAGS_INIT)
list(JOIN CMAKE_Fortran_FLAGS_RELEASE_INIT " " CMAKE_Fortran_FLAGS_RELEASE_INIT)
list(JOIN CMAKE_Fortran_FLAGS_DEBUG_INIT " " CMAKE_Fortran_FLAGS_DEBUG_INIT)
2 changes: 1 addition & 1 deletion cmake/fdictFyppify.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function(fdict_fyppify)
]==]

if(NOT DEFINED _fyppify_FYPP)
set(_fyppify_FYPP ${FYPP})
set(_fyppify_FYPP "${FDICT_FYPP}")
endif()
if(NOT DEFINED _fyppify_EXTIN)
set(_fyppify_EXTIN "fypp")
Expand Down
Loading

0 comments on commit 6ac903d

Please sign in to comment.