Skip to content

Commit

Permalink
build: fixing cmake installation
Browse files Browse the repository at this point in the history
Now everything should be complete.
One can compile with all the same flags as the make file based
implementation.

Signed-off-by: Nick Papior <[email protected]>
  • Loading branch information
zerothi committed Feb 27, 2023
1 parent 8ba74c9 commit 52ebd7c
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 159 deletions.
79 changes: 19 additions & 60 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ project(fdict
)


# Read version from file
# Define project versions
file(STRINGS "${PROJECT_SOURCE_DIR}/VERSION" PROJECT_VERSION)

# Listify the string
string(REPLACE "." ";" VERSION_LIST ${PROJECT_VERSION})
list(GET VERSION_LIST 0 PROJECT_VERSION_MAJOR)
Expand All @@ -20,30 +21,29 @@ list(GET VERSION_LIST 2 PROJECT_VERSION_PATCH)
unset(VERSION_LIST)


set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/modules)

# Define library specific content
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)")
set(FDICT_MAJOR_VERSION ${PROJECT_VERSION_MAJOR})
set(FDICT_MINOR_VERSION ${PROJECT_VERSION_MINOR})
set(FDICT_MICRO_VERSION ${PROJECT_VERSION_PATCH})
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)")
message("** PROJECT_NAME = ${PROJECT_NAME}")
message("** Please report any build problems here: ${PROJECT_URL}")


# Project installation follows GNU installation directory convention
include(GNUInstallDirs)
# Define the module temporary location
#set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/modules/)

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}"
)

# --- compiler feature checks
include(CheckFortranSourceCompiles)
include(CheckFortranSourceRuns)


if(NOT DEFINED CMAKE_MAXIMUM_RANK)
set(CMAKE_MAXIMUM_RANK 3 CACHE STRING "Maximum array rank for generated procedures")
endif()
# Add our custom definitions
add_subdirectory(config)

# Define the allocatable array sizes
set(F_ARRAY ":")
Expand All @@ -64,51 +64,10 @@ foreach(V IN ITEMS fmaximum_rank f03real128)
endforeach()



# We are done setting up things for CMAKE
# Now we can do project based compilation and installation

# Define the settings.bash target
add_custom_command(
OUTPUT settings.bash
COMMAND VPATH=${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/setup.sh -A ${CMAKE_MAXIMUM_RANK}
VERBATIM
COMMENT "Creating settings.bash"
)

install(
FILES settings.bash
DESTINATION "${CMAKE_INSTALL_BINDIR}"
)


# Ensure module files are installed
#set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_INSTALL_INCDIR})


# Add sub-directory
add_subdirectory(src)


#[==[
# Installation of the fdict library
install(
TARGETS ${PROJECT_NAME}
EXPORT "${PROJECT_NAME}-targets"
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
# Retrieve the header files I need to install
get_target_property(fdict_headers fdict PUBLIC_HEADERS)
install(
FILES ${fdict_headers}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(EXPORT ${PROJECT_NAME}-targets
NAMESPACE ${PROJECT_NAME}::
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
)
]==]
14 changes: 7 additions & 7 deletions Makefile.project
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ _SMEKA_project = 1
# Contains specific default information for this project

# Step this version upon new versions
PROJECT_MAJOR = 0
PROJECT_MINOR = 9
PROJECT_PATCH = 0
PROJECT_VERSION = $(PROJECT_MAJOR).$(PROJECT_MINOR).$(PROJECT_PATCH)
PROJECT_VERSION_MAJOR = 0
PROJECT_VERSION_MINOR = 9
PROJECT_VERSION_PATCH = 0
PROJECT_VERSION = $(PROJECT_VERSION_MAJOR).$(PROJECT_VERSION_MINOR).$(PROJECT_VERSION_PATCH)

# These are constant default
PROJECT_NAME = fdict
Expand All @@ -16,9 +16,9 @@ FDICT_LIB ?= fdict$(LIB_SUFFIX)
FDICT_LIB_STATIC ?= lib$(FDICT_LIB).a
FDICT_LIB_SHARED ?= lib$(FDICT_LIB).so

FYPPFLAGS += -DPROJECT_MAJOR=$(PROJECT_MAJOR)
FYPPFLAGS += -DPROJECT_MINOR=$(PROJECT_MINOR)
FYPPFLAGS += -DPROJECT_PATCH=$(PROJECT_PATCH)
FYPPFLAGS += -DPROJECT_VERSION_MAJOR=$(PROJECT_VERSION_MAJOR)
FYPPFLAGS += -DPROJECT_VERSION_MINOR=$(PROJECT_VERSION_MINOR)
FYPPFLAGS += -DPROJECT_VERSION_PATCH=$(PROJECT_VERSION_PATCH)

# Define custom options for fdict
STATIC ?= 1
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ To enable the non-default data types you can do so with (Makefile scheme):
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.

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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.1
0.9.0
126 changes: 103 additions & 23 deletions config/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
# Figure out if we have quadruple precision

# Check for available features
# Note: users can overwrite the automatic check by setting the value at configure time
include(CheckFortranSourceRuns)

if (NOT DEFINED WITH_QP)
check_fortran_source_runs(
"if (selected_real_kind(33) == -1) stop 1; end"
WITH_QP
)
set(WITH_QP ${WITH_QP} PARENT_SCOPE)
endif()
# Ensure we use some basic packages

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

include(ProjectOptions.cmake)

# --- compiler feature checks
include(CheckFortranSourceCompiles)
include(CheckFortranSourceRuns)

# Define a global variable that defines the directory of cmake configuration
# files.
set(
CMAKE_INSTALL_CMAKECONFIGDIR
set(CMAKE_INSTALL_CMAKECONFIG_DIR
"${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
PARENT_SCOPE
)
)


# Export a pkg-config file
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/template.pc"
"${PROJECT_SOURCE_DIR}/fdict.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
@ONLY
)

# Install pkg-config files
install(
FILES
Expand All @@ -44,8 +35,8 @@ include(CMakePackageConfigHelpers)
configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/template.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_CMAKECONFIGDIR}"
)
INSTALL_DESTINATION "${CMAKE_INSTALL_CMAKECONFIG_DIR}"
)

if(BUILD_SHARED_LIBS OR PROJECT_VERSION_MAJOR EQUAL 0)
# Due to the uncertain ABI compatibility of Fortran shared libraries
Expand All @@ -68,5 +59,94 @@ install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake"
DESTINATION "${CMAKE_INSTALL_CMAKECONFIGDIR}"
DESTINATION "${CMAKE_INSTALL_CMAKECONFIG_DIR}"
)


# Preprocesses a list of files with given preprocessor and preprocessor options
#
# Args:
# preproc [in]: Preprocessor program
# preprocopts [in]: Preprocessor options
# srcext [in]: File extension of the source files
# trgext [in]: File extension of the target files
# srcfiles [in]: List of the source files
# trgfiles [out]: Contains the list of the preprocessed files on exit
#
function(preprocess preproc preprocopts srcext trgext srcfiles trgfiles)

set(_trgfiles)
foreach(srcfile IN LISTS srcfiles)
string(REGEX REPLACE "\\.${srcext}$" ".${trgext}" trgfile ${srcfile})
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${trgfile}
COMMAND ${preproc} ${preprocopts} ${CMAKE_CURRENT_SOURCE_DIR}/${srcfile} ${CMAKE_CURRENT_BINARY_DIR}/${trgfile}
MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/${srcfile})
list(APPEND _trgfiles ${CMAKE_CURRENT_BINARY_DIR}/${trgfile})
endforeach()
set(${trgfiles} ${_trgfiles} PARENT_SCOPE)

endfunction()

# Define a function for fyppifying sources
function(fyppify)
# Parse arguments
set(options "")
set(oneValueArgs FYPP EXTIN EXTOUT COMMENT OUTPUT)
set(multiValueArgs FLAGS FILES)
cmake_parse_arguments(
_fyppify "${options}" "${oneValueArgs}" "${multiValueArgs}"
${ARGN}
)

# Now handle arguments
#[==[
message(INFO "Before parsing inputs:
comment=${_fyppify_COMMENT}
fypp=${_fyppify_FYPP}
EXTIN=${_fyppify_EXTIN}
EXTOUT=${_fyppify_EXTOUT}
FLAGS=${_fyppify_FLAGS}
FILES=${_fyppify_FILES}
")
]==]

if(NOT DEFINED _fyppify_FYPP)
set(_fyppify_FYPP ${FYPP})
endif()
if(NOT DEFINED _fyppify_EXTIN)
set(_fyppify_EXTIN "fypp")
endif()
if(NOT DEFINED _fyppify_EXTOUT)
set(_fyppify_EXTOUT "f90")
endif()
if(DEFINED _fyppify_COMMENT)
message(VERBOSE "-- Setting up fyppify: ${_fyppify_COMMENT}")
endif()
if(NOT DEFINED _fyppify_FLAGS)
set(_fyppify_FLAGS "")
endif()
if(NOT DEFINED _fyppify_FILES)
message(FATAL_ERROR "fyppify requires FILES arguments to determine which files to preprocess")
endif()

#[==[
message(INFO "After parsing inputs:
comment=${_fyppify_COMMENT}
fypp=${_fyppify_FYPP}
EXTIN=${_fyppify_EXTIN}
EXTOUT=${_fyppify_EXTOUT}
FLAGS=${_fyppify_FLAGS}
FILES=${_fyppify_FILES}
")
]==]

# Lets do the preprocessing
preprocess("${_fyppify_FYPP}" "${_fyppify_FLAGS}"
"${_fyppify_EXTIN}" "${_fyppify_EXTOUT}"
"${_fyppify_FILES}" _outfiles)
if(DEFINED _fyppify_OUTPUT)
set(${_fyppify_OUTPUT} ${_outfiles} PARENT_SCOPE)
endif()

endfunction()
62 changes: 62 additions & 0 deletions config/ProjectOptions.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Here we define all options that are related to fdict

# Ensure we have the program fypp installed
find_program(FYPP fypp)
if(NOT FYPP)
message(FATAL_ERROR "Could not find executable fypp -- it is required for the pre-processing step")
endif()

# We need to define certain variables before the initial configuration of this project
if(NOT DEFINED CMAKE_MAXIMUM_RANK)
set(CMAKE_MAXIMUM_RANK 5 CACHE STRING "Maximum array rank for generated procedures")
endif()

# Whether we should use the iso_fortran_env for data-types
message(CHECK_START "Requested use of intrinsic fortran module (iso_fortran_env) for data-types")
if(DEFINED "WITH_ISO_ENV}")
message(CHECK_PASS "used")
list(APPEND FYPPFLAGS "-DWITH_ISO_ENV=$<BOOL:${WITH_ISO_ENV}>")
else()
message(CHECK_FAIL "not used")
endif()


# Parse data-type options
message(CHECK_START "Checking for data-type interfaces")
list(APPEND CMAKE_MESSAGE_INDENT " ")
foreach(var INT8 INT16 REAL80 REAL128 LOG8 LOG16 LOG64 ISO_C)
message(CHECK_START "data-type ${var}")
if(DEFINED "WITH_${var}")
message(CHECK_PASS "added")
list(APPEND FYPPFLAGS "-DWITH_${var}=$<BOOL:${WITH_${var}}>")
else()
message(CHECK_FAIL "not added")
endif()
endforeach()
list(POP_BACK CMAKE_MESSAGE_INDENT)
message(CHECK_PASS "done")

# Global maxrank
message(CHECK_START "Checking for data-type rank size interfaces")
list(APPEND CMAKE_MESSAGE_INDENT " ")
message(CHECK_START "default rank size")
if(DEFINED "MAXRANK")
message(CHECK_PASS "found size = ${MAXRANK}")
list(APPEND FYPPFLAGS "-DMAXRANK=${MAXRANK}")
else()
set(MAXRANK 5)
message(CHECK_FAIL "using default size = ${MAXRANK}")
endif()

# Parse rank sizes
foreach(var INT REAL CMPLX LOG ISO_C)
message(CHECK_START "rank size of ${var}")
if(DEFINED "MAXRANK_${var}")
message(CHECK_PASS "found size = ${MAXRANK_${var}}")
list(APPEND FYPPFLAGS "-DMAXRANK_${var}=${MAXRANK_${var}}")
else()
message(CHECK_FAIL "using default size = ${MAXRANK}")
endif()
endforeach()
list(POP_BACK CMAKE_MESSAGE_INDENT)
message(CHECK_PASS "done")
Loading

0 comments on commit 52ebd7c

Please sign in to comment.