Skip to content

Commit bfedb21

Browse files
committed
CMake based build system implemented.
Features: - Export targets from BOTH the build AND install trees - README.md updated to reflect addition of CMake support TODO: - Add tests using json_example.f90 - Test on Linux (and Windows?) - Add uninstall target?
1 parent 02d89fa commit bfedb21

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
bin/
22
lib/
3+
builds/
34
*.mod
45
*.o

CMakeLists.txt

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# CMake Configuration and build added by Izaak Beekman -- April, 2014
2+
#
3+
# Copy right (c) 2014, Izaak Beekman
4+
# All rights reserved.
5+
#
6+
# This file is contributed to the json-fortran project, and
7+
# is licensed under the terms of json-fortran license. The json-fortran
8+
# license is located in the LICENSE file which must be distributed with
9+
# this software. The contributing author, Izaak Beekman, retains all
10+
# rights permitted by the terms of the json-fortran license.
11+
12+
cmake_minimum_required ( VERSION 2.8 FATAL_ERROR )
13+
14+
#include ( cmake/OutOfSourceCheck.cmake ) # default to out of source builds
15+
#include ( cmake/BuildSettings.cmake ) # Release, RelWithDebInfo, etc.
16+
enable_language ( Fortran )
17+
#include ( cmake/CompilerCompatibilityCheck.cmake )
18+
19+
#---------------------
20+
# Declare project name
21+
#---------------------
22+
project ( jsonfortran NONE )
23+
24+
#----------------------------------
25+
# Set version (semantic versioning)
26+
# C.F. semver.org
27+
#----------------------------------
28+
set ( VERSION_MAJOR 1 )
29+
set ( VERSION_MINOR 0 )
30+
set ( VERSION_PATCH 0 )
31+
set ( version ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH} )
32+
33+
#-------------------------------------
34+
# Collect source files for the library
35+
#-------------------------------------
36+
set ( JF_LIB_SRCS src/json_module.f90 )
37+
38+
#-----------------------------------------
39+
# Collect all the mod files into their own
40+
# directory to ease installation issues
41+
#-----------------------------------------
42+
# No standard mechanism or location for compiled .mod files
43+
# but often they are thought of as similar to precompiled
44+
# header files. Let's include the compiler ID to ease
45+
# compatibility issues for thos linking against the installed
46+
# libraries.
47+
set ( CMAKE_Fortran_MODULE_DIRECTORY "${CMAKE_BINARY_DIR}/include" )
48+
49+
#---------------------------------------------
50+
# Build a shared and static library by default
51+
#---------------------------------------------
52+
set ( LIB_NAME ${CMAKE_PROJECT_NAME} )
53+
add_library ( ${LIB_NAME} SHARED ${JF_LIB_SRCS} )
54+
add_library ( ${LIB_NAME}-static STATIC ${JF_LIB_SRCS} )
55+
set_target_properties ( ${LIB_NAME}-static
56+
PROPERTIES
57+
OUTPUT_NAME ${LIB_NAME}
58+
PREFIX lib
59+
VERSION ${version} )
60+
set_target_properties ( ${LIB_NAME}
61+
PROPERTIES
62+
SOVERSION ${VERSION_MAJOR}.${VERSION_MINOR} )
63+
64+
#-------------------------------------
65+
# Define where our files get installed
66+
#-------------------------------------
67+
if ( ${CMAKE_SYSTEM_NAME} MATCHES "Linux" )
68+
include ( GNU_Install_Dirs ) # Standard CMake module
69+
elseif ( UNIX ) #Apple, BSD, solaris, other *NIX? Framework on Apple instead?
70+
set ( CMAKE_INSTALL_LIBDIR lib )
71+
set ( CMAKE_INSTALL_INCLUDEDIR include )
72+
set ( CMAKE_INSTALL_DATAROOTDIR share )
73+
endif ( ${CMAKE_SYSTEM_NAME} MATCHES "Linux" )
74+
75+
install ( TARGETS ${LIB_NAME} ${LIB_NAME}-static
76+
EXPORT ${CMAKE_PROJECT_NAME}-targets
77+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
78+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ) # We may need something different for windows
79+
80+
set ( INSTALL_MOD_DIR "${CMAKE_INSTALL_INCLUDEDIR}/fortran/${CMAKE_Fortran_COMPILER_ID}" )
81+
install ( DIRECTORY ${CMAKE_Fortran_MODULE_DIRECTORY}/ DESTINATION "${INSTALL_MOD_DIR}" )
82+
83+
#-----------------------------------------------------
84+
# Publicize installed location to other CMake projects
85+
#-----------------------------------------------------
86+
set ( EXPORT_INSTALL_DIR
87+
${CMAKE_INSTALL_DATAROOTDIR}/cmake/${CMAKE_PROJECT_NAME}-${version} )
88+
install ( EXPORT ${CMAKE_PROJECT_NAME}-targets DESTINATION ${EXPORT_INSTALL_DIR} )
89+
90+
include ( CMakePackageConfigHelpers ) # Standard CMake module
91+
write_basic_package_version_file( ${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}-config-version.cmake
92+
VERSION ${version}
93+
COMPATIBILITY SameMajorVersion )
94+
95+
# install package config file
96+
configure_package_config_file (
97+
${CMAKE_SOURCE_DIR}/cmake/pkg/${CMAKE_PROJECT_NAME}-config.cmake.in
98+
${CMAKE_BINARY_DIR}/pkg/${CMAKE_PROJECT_NAME}-config.cmake
99+
INSTALL_DESTINATION ${EXPORT_INSTALL_DIR}
100+
PATH_VARS EXPORT_INSTALL_DIR INSTALL_MOD_DIR )
101+
102+
# Install the config and version files so that we can find this project with others
103+
install ( FILES
104+
${CMAKE_BINARY_DIR}/pkg/${CMAKE_PROJECT_NAME}-config.cmake
105+
${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}-config-version.cmake
106+
DESTINATION ${EXPORT_INSTALL_DIR} )
107+
108+
#----------------------------------------------
109+
# Make build tree targets accessible for import
110+
#----------------------------------------------
111+
export ( TARGETS ${LIB_NAME} ${LIB_NAME}-static FILE ${CMAKE_PROJECT_NAME}-targets.cmake )
112+
113+
# build tree package config file, NOT installed
114+
configure_file (
115+
${CMAKE_SOURCE_DIR}/cmake/${CMAKE_PROJECT_NAME}-config.cmake.in
116+
${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}-config.cmake
117+
@ONLY )
118+
119+
export ( PACKAGE jsonfortran )

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ Brief Description
88

99
A mostly-complete API for reading and writing JSON files, written in modern Fortran. The code requires a Fortran compiler that supports various Fortran 2003 and Fortran 2008 features such as: allocatable strings, associate, newunit, generic, class, and abstract interface. I am using the Intel Fortran compiler 13.1.0 on Linux (the Mac and PC versions should also work fine). It also currently compiles under recent experimental 4.9 release of the gnu gfortran compiler. The source code is a single Fortran module file (json_module.f90).
1010

11+
Building the Library
12+
--------------------
13+
Currently two way are provided to build the jsonfortran library (libjsonfortran). A build script, build.sh is provided in the project root directory. Additionally, a [CMake](www.cmake.org) build system is provided. This build system has been tested on Mac and Linux using the Intel Fortran Compiler. It has not been tested on Windows. This CMake based build provides an install target, and exports from both the install location and the build location so that building and using json-fortran in another CMake based project is trivial. To get started with the CMake based build, set the environment variable `FC` to point to your Fortran compiler, and create a build directory. Then `(cmake-gui|ccmake|cmake) /path/to/json-fortran` to configure, `make` to build and `make install` to optionally install. As long as the project is built with CMake other CMake projects can find it and link against it:
14+
15+
```CMake
16+
cmake_minimum_required ( VERSION 2.8 FATAL_ERROR )
17+
enable_language ( Fortran )
18+
project ( jf_test NONE )
19+
20+
find_package ( jsonfortran 1.0.0 REQUIRED )
21+
22+
add_executable ( json_example src/json_example.f90 )
23+
target_include_directories ( json_example BEFORE PUBLIC ${jsonfortran_INCLUDE_DIRS} )
24+
target_link_libraries ( json_example jsonfortran-static )
25+
```
26+
1127
Reading a JSON file
1228
---------------
1329

cmake/jsonfortran-config.cmake.in

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# config file for the build tree
2+
# Allow other CMake projects to find this one without installing jsonfortran
3+
# (e.g. only configuring and building it)
4+
# No need to use CMakePackageConfigHelpers since we know all the paths with
5+
# certainty in the build tree.
6+
7+
# Make targets available to be built
8+
include ( "@CMAKE_BINARY_DIR@/@[email protected]" )
9+
10+
# Tell the compiler where to find the mod files
11+
set ( @CMAKE_PROJECT_NAME@_INCLUDE_DIRS "@CMAKE_Fortran_MODULE_DIRECTORY@" )
12+

cmake/pkg/jsonfortran-config.cmake.in

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Config file for the INSTALLED package
2+
# Allow other CMake projects to find this package if it is installed
3+
# Requires the use of the standard CMake module CMakePackageConfigHelpers
4+
5+
set ( @CMAKE_PROJECT_NAME@_VERSION @version@ )
6+
7+
@PACKAGE_INIT@
8+
9+
10+
# Provide the targets
11+
set_and_check ( @CMAKE_PROJECT_NAME@_CONFIG_INSTALL_DIR "@PACKAGE_EXPORT_INSTALL_DIR@" )
12+
include ( "${@CMAKE_PROJECT_NAME@_CONFIG_INSTALL_DIR}/@[email protected]" )
13+
14+
# Make the module files available via include
15+
set_and_check ( @CMAKE_PROJECT_NAME@_INCLUDE_DIRS "@PACKAGE_INSTALL_MOD_DIR@" )

0 commit comments

Comments
 (0)