diff --git a/CMakeLists.txt b/CMakeLists.txt index 50846f52..19309474 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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.") @@ -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 - $ - $ +add_fortran_library( + ${PROJECT_NAME} + ${PROJECT_INCLUDE_DIR} + ${CMAKE_INSTALL_INCLUDEDIR} + ${PROJECT_VERSION} + ${PROJECT_VERSION_MAJOR} + ${FORIMAGE_SOURCES} ) # Installation diff --git a/cmake/helper.cmake b/cmake/helper.cmake new file mode 100644 index 00000000..7a560ce4 --- /dev/null +++ b/cmake/helper.cmake @@ -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 + $ + $ + ) +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 + $ + $ + ) +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 $) +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() \ No newline at end of file diff --git a/install/CMakeLists.txt b/install/CMakeLists.txt index dbb24d9d..92988870 100644 --- a/install/CMakeLists.txt +++ b/install/CMakeLists.txt @@ -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