diff --git a/CMakeLists.txt b/CMakeLists.txt index 100881ca..b761907f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,21 +28,8 @@ include(GNUInstallDirs) add_subdirectory("config") # Dependencies -if(NOT TARGET "jsonfortran" AND WITH_JSON) - # json-fortran tries to make it hard to get found - string(TOLOWER "jsonfortran-${CMAKE_Fortran_COMPILER_ID}" jsonfortran) - find_package("${jsonfortran}" REQUIRED) - add_library("jsonfortran::jsonfortran" IMPORTED INTERFACE) - target_link_libraries( - "jsonfortran::jsonfortran" - INTERFACE - "jsonfortran$<$>:-static>" - ) - target_include_directories( - "jsonfortran::jsonfortran" - INTERFACE - "${jsonfortran_INCLUDE_DIRS}" - ) +if(NOT TARGET "jsonfortran::jsonfortran" AND WITH_JSON) + find_package("jsonfortran" REQUIRED) endif() # Collect source of the project diff --git a/README.md b/README.md index 3b8b8129..8bcd655d 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ To build this project from the source code in this repository you need to have a Fortran compiler supporting Fortran 2008 and one of the supported build systems: -- [meson](https://mesonbuild.com) version 0.53 or newer, with +- [meson](https://mesonbuild.com) version 0.55 or newer, with a build-system backend, *i.e.* [ninja](https://ninja-build.org) version 1.7 or newer - [cmake](https://cmake.org) version 3.14 or newer, with a build-system backend, *i.e.* [ninja](https://ninja-build.org) version 1.10 or newer diff --git a/config/CMakeLists.txt b/config/CMakeLists.txt index 78f4f2b5..80d33e63 100644 --- a/config/CMakeLists.txt +++ b/config/CMakeLists.txt @@ -23,6 +23,14 @@ set( ) set(module-dir "${module-dir}" PARENT_SCOPE) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") +set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}" PARENT_SCOPE) +install( + DIRECTORY + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" +) + # Set build type as CMake does not provide defaults if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) set( @@ -54,6 +62,17 @@ install( DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" ) +if(BUILD_SHARED_LIBS) + set(PKG_CONFIG_REQUIRES "Requires.private") +else() + set(PKG_CONFIG_REQUIRES "Requires") +endif() +if(WITH_JSON) + set(PKG_CONFIG_REQUIREMENTS "json-fortran") +else() + set(PKG_CONFIG_REQUIREMENTS) +endif() + configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/template.pc" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" diff --git a/config/cmake/Findjsonfortran.cmake b/config/cmake/Findjsonfortran.cmake new file mode 100644 index 00000000..81d794c2 --- /dev/null +++ b/config/cmake/Findjsonfortran.cmake @@ -0,0 +1,30 @@ +# This file is part of mctc-lib. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +if(NOT TARGET "jsonfortran::jsonfortran") + # json-fortran tries to make it hard to get found + string(TOLOWER "jsonfortran-${CMAKE_Fortran_COMPILER_ID}" jsonfortran) + find_package("${jsonfortran}" CONFIG) + add_library("jsonfortran::jsonfortran" IMPORTED INTERFACE) + target_link_libraries( + "jsonfortran::jsonfortran" + INTERFACE + "jsonfortran$<$>:-static>" + ) + target_include_directories( + "jsonfortran::jsonfortran" + INTERFACE + "${jsonfortran_INCLUDE_DIRS}" + ) +endif() diff --git a/config/meson.build b/config/meson.build index 7741a39a..5b20cb7b 100644 --- a/config/meson.build +++ b/config/meson.build @@ -47,8 +47,13 @@ if get_option('openmp') lib_deps += omp_dep endif -json_dep = dependency( +jsonfortran_dep = dependency( 'json-fortran', required: get_option('json'), + fallback: ['json-fortran-8.2.5','jsonfortran_dep'], + default_options: [ + 'default_library=static', + ], static: get_option('default_library') != 'dynamic', ) +lib_deps += jsonfortran_dep diff --git a/config/template.cmake b/config/template.cmake index 3a6ae000..0268d954 100644 --- a/config/template.cmake +++ b/config/template.cmake @@ -1,6 +1,7 @@ @PACKAGE_INIT@ set("@PROJECT_NAME@_WITH_OpenMP" @WITH_OpenMP@) +set("@PROJECT_NAME@_WITH_JSON" @WITH_JSON@) if(NOT TARGET "@PROJECT_NAME@::@PROJECT_NAME@") include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@-targets.cmake") @@ -10,4 +11,8 @@ if(NOT TARGET "@PROJECT_NAME@::@PROJECT_NAME@") if(NOT TARGET "OpenMP::OpenMP_Fortran" AND "@PROJECT_NAME@_WITH_OpenMP") find_dependency("OpenMP") endif() + + if(NOT TARGET "jsonfortran::jsonfortran" AND "@PROJECT_NAME@_WITH_JSON") + find_dependency("jsonfortran") + endif() endif() diff --git a/config/template.pc b/config/template.pc index 3d6efbb5..84c3498c 100644 --- a/config/template.pc +++ b/config/template.pc @@ -4,6 +4,7 @@ includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ Name: @PROJECT_NAME@ Description: @PROJECT_DESCRIPTION@ +@PKG_CONFIG_REQUIRES@: @PKG_CONFIG_REQUIREMENTS@ Version: @PROJECT_VERSION@ Libs: -L${libdir} -l@PROJECT_NAME@ Cflags: -I${includedir} -I${includedir}/@module-dir@ diff --git a/meson.build b/meson.build index fd2ed7b1..3b4339f6 100644 --- a/meson.build +++ b/meson.build @@ -17,7 +17,7 @@ project( 'fortran', version: '0.2.3', license: 'Apache-2.0', - meson_version: '>=0.53', + meson_version: '>=0.55', default_options: [ 'buildtype=debugoptimized', 'default_library=both', @@ -41,7 +41,7 @@ mctc_lib = library( include_directories: include_directories('include'), dependencies: lib_deps, fortran_args: [ - '-DWITH_JSON=@0@'.format(json_dep.found() ? '1' : '0'), + '-DWITH_JSON=@0@'.format(jsonfortran_dep.found() ? '1' : '0'), ], install: install, ) diff --git a/subprojects/.gitignore b/subprojects/.gitignore new file mode 100644 index 00000000..3daff2f5 --- /dev/null +++ b/subprojects/.gitignore @@ -0,0 +1,2 @@ +/packagecache/ +/json-fortran-*/ diff --git a/subprojects/json-fortran-8.2.5.wrap b/subprojects/json-fortran-8.2.5.wrap new file mode 100644 index 00000000..3a8bf523 --- /dev/null +++ b/subprojects/json-fortran-8.2.5.wrap @@ -0,0 +1,8 @@ +[wrap-file] +directory = json-fortran-8.2.5 + +source_url = https://github.com/jacobwilliams/json-fortran/archive/refs/tags/8.2.5.tar.gz +source_filename = 8.2.5.tar.gz +source_hash = 16eec827f64340c226ba9a8463f001901d469bc400a1e88b849f258f9ef0d100 + +patch_directory = json-fortran-8.2.5 diff --git a/subprojects/packagefiles/json-fortran-8.2.5/meson.build b/subprojects/packagefiles/json-fortran-8.2.5/meson.build new file mode 100644 index 00000000..2dcadd39 --- /dev/null +++ b/subprojects/packagefiles/json-fortran-8.2.5/meson.build @@ -0,0 +1,28 @@ +project( + 'jsonfortran', + 'Fortran', + version: files('.VERSION'), +) + +jsonfortran_lib = library( + meson.project_name(), + sources: files( + 'src/json_kinds.F90', + 'src/json_parameters.F90', + 'src/json_string_utilities.F90', + 'src/json_value_module.F90', + 'src/json_file_module.F90', + 'src/json_module.F90', + ), + include_directories: include_directories('src'), +) + +jsonfortran_dep = declare_dependency( + link_with: jsonfortran_lib, + include_directories: jsonfortran_lib.private_dir_include(), +) + +install_data( + 'LICENSE', + install_dir: get_option('datadir')/'licenses'/'mctc-lib'/meson.project_name() +)