Skip to content

Commit

Permalink
Update jsonfortran automatic detection
Browse files Browse the repository at this point in the history
  • Loading branch information
awvwgk committed Jan 16, 2022
1 parent b22e7a3 commit 36a4b95
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 19 deletions.
17 changes: 2 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:-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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions config/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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"
Expand Down
30 changes: 30 additions & 0 deletions config/cmake/Findjsonfortran.cmake
Original file line number Diff line number Diff line change
@@ -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$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:-static>"
)
target_include_directories(
"jsonfortran::jsonfortran"
INTERFACE
"${jsonfortran_INCLUDE_DIRS}"
)
endif()
7 changes: 6 additions & 1 deletion config/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions config/template.cmake
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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()
1 change: 1 addition & 0 deletions config/template.pc
Original file line number Diff line number Diff line change
Expand Up @@ -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@
4 changes: 2 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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,
)
Expand Down
2 changes: 2 additions & 0 deletions subprojects/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/packagecache/
/json-fortran-*/
8 changes: 8 additions & 0 deletions subprojects/json-fortran-8.2.5.wrap
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions subprojects/packagefiles/json-fortran-8.2.5/meson.build
Original file line number Diff line number Diff line change
@@ -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()
)

0 comments on commit 36a4b95

Please sign in to comment.