Skip to content

Commit abe6cc6

Browse files
author
Damian Rouson
committed
cmake: Enable building jsonfortran as a subdirectory of another project
Replace references to variables `CMAKE_{NAME,SOURCE_DIR,BINARY_DIR}` with `PROJECT_{NAME,SOURCE_DIR,BINARY_DIR}`. The latter are set by our `project(jsonfortran)` call and work even when jsonfortran is configured under an `add_subdirectory` call of another project.
1 parent a9b0a72 commit abe6cc6

4 files changed

+49
-49
lines changed

CMakeLists.txt

+40-40
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ endif()
7474
# Get version (semantic versioning)
7575
# C.F. semver.org
7676
#----------------------------------
77-
file ( STRINGS "${CMAKE_SOURCE_DIR}/.VERSION" VERSION )
77+
file ( STRINGS "${PROJECT_SOURCE_DIR}/.VERSION" VERSION )
7878
string( REPLACE "." ";" VERSION_LIST ${VERSION} )
7979
list(GET VERSION_LIST 0 VERSION_MAJOR)
8080
list(GET VERSION_LIST 1 VERSION_MINOR)
@@ -92,13 +92,13 @@ set ( JF_LIB_SRCS src/json_kinds.F90
9292
src/json_file_module.F90
9393
src/json_module.F90 )
9494
file ( GLOB JF_TEST_SRCS "src/tests/jf_test_*.F90" )
95-
set ( JF_TEST_UCS4_SUPPORT_SRC "${CMAKE_SOURCE_DIR}/src/tests/introspection/test_iso_10646_support.f90")
95+
set ( JF_TEST_UCS4_SUPPORT_SRC "${PROJECT_SOURCE_DIR}/src/tests/introspection/test_iso_10646_support.f90")
9696

9797
#-----------------------------------------
9898
# Collect all the mod files into their own
9999
# directory to ease installation issues
100100
#-----------------------------------------
101-
set ( MODULE_DIR "${CMAKE_BINARY_DIR}/include" )
101+
set ( MODULE_DIR "${PROJECT_BINARY_DIR}/include" )
102102

103103
#-------------------------------------
104104
# Define where our files get installed
@@ -109,7 +109,7 @@ set ( USE_GNU_INSTALL_CONVENTION FALSE
109109

110110
# Set the package name to be specific to the compiler used, so that
111111
# versions compiled with different compilers can be installed in parallel
112-
string ( TOLOWER ${CMAKE_PROJECT_NAME}-${CMAKE_Fortran_COMPILER_ID} PACKAGE_NAME )
112+
string ( TOLOWER ${PROJECT_NAME}-${CMAKE_Fortran_COMPILER_ID} PACKAGE_NAME )
113113
set ( PACKAGE_VERSION "${PACKAGE_NAME}-${VERSION}" )
114114

115115
if (USE_GNU_INSTALL_CONVENTION)
@@ -153,7 +153,7 @@ set ( ENABLE_UNICODE FALSE CACHE BOOL
153153
"Enable unicode/UCS4 support" )
154154
if ( ENABLE_UNICODE )
155155
try_run( UCS4_TEST_RUNS UCS4_TEST_COMPILES
156-
${CMAKE_BINARY_DIR}/bin ${JF_TEST_UCS4_SUPPORT_SRC} )
156+
${PROJECT_BINARY_DIR}/bin ${JF_TEST_UCS4_SUPPORT_SRC} )
157157
if (UCS4_TEST_RUNS EQUAL 0)
158158
add_definitions (-DUSE_UCS4)
159159
else ()
@@ -166,7 +166,7 @@ endif ()
166166
# Build a shared and static library by default
167167
#---------------------------------------------
168168

169-
set ( LIB_NAME ${CMAKE_PROJECT_NAME} )
169+
set ( LIB_NAME ${PROJECT_NAME} )
170170
add_library ( ${LIB_NAME} SHARED ${JF_LIB_SRCS} )
171171
add_library ( ${LIB_NAME}-static STATIC ${JF_LIB_SRCS} )
172172

@@ -184,7 +184,7 @@ set_target_properties ( ${LIB_NAME}-static
184184
PREFIX lib
185185
endif()
186186
VERSION ${VERSION}
187-
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
187+
ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib
188188
Fortran_MODULE_DIRECTORY ${MODULE_DIR} )
189189
set_target_properties ( ${LIB_NAME}
190190
PROPERTIES
@@ -194,8 +194,8 @@ set_target_properties ( ${LIB_NAME}
194194
endif()
195195
SOVERSION ${VERSION_MAJOR}.${VERSION_MINOR}
196196
VERSION ${VERSION}
197-
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
198-
Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR} )
197+
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib
198+
Fortran_MODULE_DIRECTORY ${PROJECT_BINARY_DIR} )
199199

200200
#-------------------------------------
201201
# Build the documentation with FORD
@@ -205,12 +205,12 @@ set ( SKIP_DOC_GEN FALSE CACHE BOOL
205205
if ( NOT SKIP_DOC_GEN )
206206
find_program ( FORD ford )
207207
if ( FORD ) # Found
208-
file ( COPY "${CMAKE_SOURCE_DIR}/media" DESTINATION "${CMAKE_BINARY_DIR}/" )
209-
file ( GLOB_RECURSE PAGES_FILES "${CMAKE_SOURCE_DIR}/pages/*.*")
210-
set ( DOC_DIR "${CMAKE_BINARY_DIR}/doc" )
211-
set ( PAGES_DIR "${CMAKE_SOURCE_DIR}/pages" )
212-
set ( PROJ_DIR "${CMAKE_SOURCE_DIR}/src" )
213-
set ( FORD_PROJECT_FILE "${CMAKE_SOURCE_DIR}/json-fortran.md" )
208+
file ( COPY "${PROJECT_SOURCE_DIR}/media" DESTINATION "${PROJECT_BINARY_DIR}/" )
209+
file ( GLOB_RECURSE PAGES_FILES "${PROJECT_SOURCE_DIR}/pages/*.*")
210+
set ( DOC_DIR "${PROJECT_BINARY_DIR}/doc" )
211+
set ( PAGES_DIR "${PROJECT_SOURCE_DIR}/pages" )
212+
set ( PROJ_DIR "${PROJECT_SOURCE_DIR}/src" )
213+
set ( FORD_PROJECT_FILE "${PROJECT_SOURCE_DIR}/json-fortran.md" )
214214
if ( ENABLE_UNICODE )
215215
set ( MACRO_FLAG "-m USE_UCS4" )
216216
else ()
@@ -222,7 +222,7 @@ if ( NOT SKIP_DOC_GEN )
222222
else ()
223223
set ( FPP "gfortran -E\n" ) # default to gfortran -E for gfortran and unsupported compilers
224224
endif ()
225-
file ( WRITE "${CMAKE_BINARY_DIR}/.PREPROCESSOR" "${FPP}" )
225+
file ( WRITE "${PROJECT_BINARY_DIR}/.PREPROCESSOR" "${FPP}" )
226226
# Dynamically generate the FORD outputs list
227227
message ( STATUS "Dynamically computing FORD output information..." )
228228
if ( NOT (DEFINED FORD_OUTPUTS_CACHED) )
@@ -244,16 +244,16 @@ if ( NOT SKIP_DOC_GEN )
244244
endif ()
245245
message ( STATUS "Done dynamically computing FORD outputs." )
246246

247-
foreach ( DOC_SRC_FILE ${JF_LIB_SRCS} ${JF_TEST_SRCS} ${CMAKE_SOURCE_DIR}/README.md
248-
${CMAKE_SOURCE_DIR}/CHANGELOG.md ${CMAKE_SOURCE_DIR}/.github/CONTRIBUTING.md
249-
${CMAKE_SOURCE_DIR}/LICENSE ${CMAKE_SOURCE_DIR}/json-fortran.md ${PAGES_FILES} )
247+
foreach ( DOC_SRC_FILE ${JF_LIB_SRCS} ${JF_TEST_SRCS} ${PROJECT_SOURCE_DIR}/README.md
248+
${PROJECT_SOURCE_DIR}/CHANGELOG.md ${PROJECT_SOURCE_DIR}/.github/CONTRIBUTING.md
249+
${PROJECT_SOURCE_DIR}/LICENSE ${PROJECT_SOURCE_DIR}/json-fortran.md ${PAGES_FILES} )
250250
list ( APPEND FORD_DEPENDS "${DOC_SRC_FILE}" )
251251
endforeach ()
252252
add_custom_command ( OUTPUT ${FORD_OUTPUTS_CACHED}
253-
COMMAND "${FORD}" --debug ${MACRO_FLAG} -d "${PROJ_DIR}" -o "${DOC_DIR}" -p "${CMAKE_SOURCE_DIR}/pages" "${FORD_PROJECT_FILE}"
253+
COMMAND "${FORD}" --debug ${MACRO_FLAG} -d "${PROJ_DIR}" -o "${DOC_DIR}" -p "${PROJECT_SOURCE_DIR}/pages" "${FORD_PROJECT_FILE}"
254254
MAIN_DEPENDENCY "${FORD_PROJECT_FILE}"
255255
DEPENDS ${FORD_DEPENDS}
256-
COMMENT "Building HTML documentation for ${CMAKE_PROJECT_NAME} using FORD" )
256+
COMMENT "Building HTML documentation for ${PROJECT_NAME} using FORD" )
257257
add_custom_target ( documentation ALL
258258
DEPENDS ${FORD_OUTPUTS_CACHED} )
259259
set ( INSTALL_API_DOCUMENTATION TRUE
@@ -290,7 +290,7 @@ if ( ENABLE_TESTS )
290290

291291
find_program ( JSONLINT jsonlint )
292292

293-
set ( DATA_DIR "${CMAKE_SOURCE_DIR}/files" )
293+
set ( DATA_DIR "${PROJECT_SOURCE_DIR}/files" )
294294

295295
set_directory_properties ( PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES
296296
"${FORD_CLEAN_OUTPUTS}" )
@@ -323,13 +323,13 @@ if ( ENABLE_TESTS )
323323
endif ()
324324

325325
add_test(NAME jf-cleanup-fixture
326-
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
327-
COMMAND ${CMAKE_COMMAND} -E remove_directory "${CMAKE_BINARY_DIR}/files")
326+
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}"
327+
COMMAND ${CMAKE_COMMAND} -E remove_directory "${PROJECT_BINARY_DIR}/files")
328328
set_tests_properties(jf-cleanup-fixture
329329
PROPERTIES FIXTURES_SETUP JF)
330330
add_test(NAME jf-setup-fixture
331-
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
332-
COMMAND ${CMAKE_COMMAND} -E copy_directory "${DATA_DIR}" "${CMAKE_BINARY_DIR}/files")
331+
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}"
332+
COMMAND ${CMAKE_COMMAND} -E copy_directory "${DATA_DIR}" "${PROJECT_BINARY_DIR}/files")
333333
set_tests_properties(jf-setup-fixture
334334
PROPERTIES FIXTURES_SETUP JF
335335
DEPENDS jf-cleanup-fixture)
@@ -338,17 +338,17 @@ if ( ENABLE_TESTS )
338338
foreach ( UNIT_TEST ${JF_TEST_SRCS} )
339339
get_filename_component ( TEST ${UNIT_TEST} NAME_WE )
340340
if(MSVC_IDE)
341-
link_directories(${CMAKE_BINARY_DIR}/lib)
341+
link_directories(${PROJECT_BINARY_DIR}/lib)
342342
endif()
343343
add_executable ( ${TEST} EXCLUDE_FROM_ALL ${UNIT_TEST} )
344344
target_link_libraries ( ${TEST} ${LIB_NAME} )
345345
add_dependencies ( check ${TEST} )
346346
add_dependencies ( build_tests ${TEST} )
347347
set_target_properties ( ${TEST}
348348
PROPERTIES
349-
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
349+
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin )
350350
add_test( NAME ${TEST}
351-
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin
351+
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/bin
352352
COMMAND ./${TEST})
353353
set_tests_properties( ${TEST}
354354
PROPERTIES FIXTURES_REQUIRED JF)
@@ -375,7 +375,7 @@ if ( ENABLE_TESTS )
375375
foreach ( JSON_FILE ${EXPECTED_OUTPUTS} )
376376
get_filename_component ( TESTNAME ${JSON_FILE} NAME )
377377
add_test ( NAME validate-output-${TESTNAME}
378-
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/files"
378+
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/files"
379379
COMMAND ${JSONLINT} "--allow=nonescape-characters" ${TESTNAME} )
380380
set_property ( TEST validate-output-${TESTNAME}
381381
APPEND
@@ -392,7 +392,7 @@ if ( ENABLE_TESTS )
392392
foreach ( JSON_FILE ${EXPECTED_OUTPUTS} )
393393
get_filename_component (OUTPUT ${JSON_FILE} NAME )
394394
add_test ( NAME regression-${OUTPUT}
395-
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/files"
395+
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/files"
396396
COMMAND ${CMAKE_COMMAND} -E compare_files ${JSON_FORTRAN_COMPARE_FLAG} ${OUTPUT} expected-outputs/${OUTPUT} )
397397
set_property ( TEST regression-${OUTPUT}
398398
APPEND
@@ -429,19 +429,19 @@ install(
429429
# Add portable unistall command to makefile
430430
#------------------------------------------
431431
# Adapted from the CMake Wiki FAQ
432-
configure_file ( "${CMAKE_SOURCE_DIR}/cmake/uninstall.cmake.in" "${CMAKE_BINARY_DIR}/uninstall.cmake"
432+
configure_file ( "${PROJECT_SOURCE_DIR}/cmake/uninstall.cmake.in" "${PROJECT_BINARY_DIR}/uninstall.cmake"
433433
@ONLY)
434434

435435
add_custom_target ( uninstall
436-
COMMAND ${CMAKE_COMMAND} -P "${CMAKE_BINARY_DIR}/uninstall.cmake" )
436+
COMMAND ${CMAKE_COMMAND} -P "${PROJECT_BINARY_DIR}/uninstall.cmake" )
437437

438438
#-----------------------------------------------------
439439
# Publicize installed location to other CMake projects
440440
#-----------------------------------------------------
441441
install ( EXPORT ${PACKAGE_NAME}-targets DESTINATION "${EXPORT_INSTALL_DIR}" )
442442

443443
include ( CMakePackageConfigHelpers ) # Standard CMake module
444-
write_basic_package_version_file( "${CMAKE_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake"
444+
write_basic_package_version_file( "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake"
445445
VERSION ${VERSION}
446446
COMPATIBILITY SameMajorVersion )
447447

@@ -450,15 +450,15 @@ include ( cmake/FCompilerConsistencyCheck.cmake )
450450

451451
# install package config file
452452
configure_package_config_file (
453-
"${CMAKE_SOURCE_DIR}/cmake/pkg/${CMAKE_PROJECT_NAME}-config.cmake.in"
454-
"${CMAKE_BINARY_DIR}/pkg/${PACKAGE_NAME}-config.cmake"
453+
"${PROJECT_SOURCE_DIR}/cmake/pkg/${PROJECT_NAME}-config.cmake.in"
454+
"${PROJECT_BINARY_DIR}/pkg/${PACKAGE_NAME}-config.cmake"
455455
INSTALL_DESTINATION "${EXPORT_INSTALL_DIR}"
456456
PATH_VARS EXPORT_INSTALL_DIR INSTALL_MOD_DIR )
457457

458458
# Install the config and version files so that we can find this project with others
459459
install ( FILES
460-
"${CMAKE_BINARY_DIR}/pkg/${PACKAGE_NAME}-config.cmake"
461-
"${CMAKE_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake"
460+
"${PROJECT_BINARY_DIR}/pkg/${PACKAGE_NAME}-config.cmake"
461+
"${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake"
462462
DESTINATION "${EXPORT_INSTALL_DIR}" )
463463

464464
#----------------------------------------------
@@ -468,8 +468,8 @@ export ( TARGETS ${LIB_NAME} ${LIB_NAME}-static FILE ${PACKAGE_NAME}-targets.cma
468468

469469
# build tree package config file, NOT installed
470470
configure_file (
471-
"${CMAKE_SOURCE_DIR}/cmake/${CMAKE_PROJECT_NAME}-config.cmake.in"
472-
"${CMAKE_BINARY_DIR}/${PACKAGE_NAME}-config.cmake"
471+
"${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}-config.cmake.in"
472+
"${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config.cmake"
473473
@ONLY )
474474

475475
set ( ENABLE_BUILD_TREE_EXPORT FALSE CACHE BOOL

cmake/jsonfortran-config.cmake.in

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
# No need to use CMakePackageConfigHelpers since we know all the paths with
55
# certainty in the build tree.
66

7-
set ( @CMAKE_PROJECT_NAME@_VERSION @VERSION@ )
7+
set ( @PROJECT_NAME@_VERSION @VERSION@ )
88

99
@COMPILER_CONSISTENCY_CHECK@
1010

1111
# Make targets available to be built
12-
include ( "@CMAKE_BINARY_DIR@/@[email protected]" )
12+
include ( "@PROJECT_BINARY_DIR@/@[email protected]" )
1313

1414
# Tell the compiler where to find the mod files
15-
set ( @CMAKE_PROJECT_NAME@_INCLUDE_DIRS "@MODULE_DIR@" )
15+
set ( @PROJECT_NAME@_INCLUDE_DIRS "@MODULE_DIR@" )

cmake/pkg/jsonfortran-config.cmake.in

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Allow other CMake projects to find this package if it is installed
33
# Requires the use of the standard CMake module CMakePackageConfigHelpers
44

5-
set ( @CMAKE_PROJECT_NAME@_VERSION @VERSION@ )
5+
set ( @PROJECT_NAME@_VERSION @VERSION@ )
66

77
@COMPILER_CONSISTENCY_CHECK@
88

@@ -13,4 +13,4 @@ set_and_check ( @PACKAGE_NAME@_CONFIG_INSTALL_DIR "@PACKAGE_EXPORT_INSTALL_DIR@"
1313
include ( "${@PACKAGE_NAME@_CONFIG_INSTALL_DIR}/@[email protected]" )
1414

1515
# Make the module files available via include
16-
set_and_check ( @CMAKE_PROJECT_NAME@_INCLUDE_DIRS "@PACKAGE_INSTALL_MOD_DIR@" )
16+
set_and_check ( @PROJECT_NAME@_INCLUDE_DIRS "@PACKAGE_INSTALL_MOD_DIR@" )

cmake/uninstall.cmake.in

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Adapted from http://www.cmake.org/Wiki/CMake_FAQ#Can_I_do_.22make_uninstall.22_with_CMake.3F May 1, 2014
22

3-
if(NOT EXISTS "@CMAKE_BINARY_DIR@/install_manifest.txt")
4-
message(FATAL_ERROR "Cannot find install manifest: @CMAKE_BINARY_DIR@/install_manifest.txt")
5-
endif(NOT EXISTS "@CMAKE_BINARY_DIR@/install_manifest.txt")
3+
if(NOT EXISTS "@PROJECT_BINARY_DIR@/install_manifest.txt")
4+
message(FATAL_ERROR "Cannot find install manifest: @PROJECT_BINARY_DIR@/install_manifest.txt")
5+
endif(NOT EXISTS "@PROJECT_BINARY_DIR@/install_manifest.txt")
66

7-
file(READ "@CMAKE_BINARY_DIR@/install_manifest.txt" files)
7+
file(READ "@PROJECT_BINARY_DIR@/install_manifest.txt" files)
88
string(REGEX REPLACE "\n" ";" files "${files}")
99
foreach(file ${files})
1010
message(STATUS "Uninstalling $ENV{DESTDIR}${file}")

0 commit comments

Comments
 (0)