From 0c4d994dde0db9d32c0ad47a05017fd098e94cdc Mon Sep 17 00:00:00 2001 From: Eero Aaltonen Date: Fri, 15 Dec 2023 13:53:14 +0200 Subject: [PATCH 1/3] Add pkgconfig file to install A pkgconfig file is useful to determine the required path to the header when installed outside of the system include directories. --- Makefile | 7 +++++++ picojson.pc.in | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 picojson.pc.in diff --git a/Makefile b/Makefile index a03f4a0..4bbbe78 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ prefix=/usr/local includedir=$(prefix)/include +libdir=$(prefix)/lib check: test @@ -19,9 +20,15 @@ clean: install: install -d $(DESTDIR)$(includedir) install -p -m 0644 picojson.h $(DESTDIR)$(includedir) + install -d $(DESTDIR)$(libdir)/pkgconfig + sed -e "s:@prefix@:$(prefix):" \ + -e "s:@includedir@:$(includedir):" \ + picojson.pc.in > $(DESTDIR)$(libdir)/pkgconfig/picojson.pc + chmod 0644 $(DESTDIR)$(libdir)/pkgconfig/picojson.pc uninstall: rm -f $(DESTDIR)$(includedir)/picojson.h + rm -f $(DESTDIR)$(libdir)/pkgconfig/picojson.pc clang-format: picojson.h examples/github-issues.cc examples/iostream.cc examples/streaming.cc clang-format -i $? diff --git a/picojson.pc.in b/picojson.pc.in new file mode 100644 index 0000000..d64ff93 --- /dev/null +++ b/picojson.pc.in @@ -0,0 +1,7 @@ +prefix=@prefix@ +includedir=@includedir@ + +Name: picojson +Description: Tiny JSON parser / serializer for C++ +Version: 1.3.0 +Cflags: -I${includedir} From 9896c6a06dfbaaae5cd068995ef890becd4d2a5e Mon Sep 17 00:00:00 2001 From: Eero Aaltonen Date: Fri, 15 Dec 2023 15:27:48 +0200 Subject: [PATCH 2/3] Add CMake config-file package to install A CMake config-file package can be used also on Windows systems and by libraries that want to support multiple platforms. --- .gitignore | 1 + CMakeLists.txt | 65 ++++++++++++++++++++++++++++++++++++++++ Makefile | 14 ++++----- picojson-config.cmake.in | 5 ++++ picojson.pc.in | 2 +- 5 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 picojson-config.cmake.in diff --git a/.gitignore b/.gitignore index 815b59f..c3f4fc3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *~ +/build/ a.out test-core test-core-int64 diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..14f2fc7 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,65 @@ +cmake_minimum_required(VERSION 3.16) +project(picojson + VERSION 1.3.0 + LANGUAGES CXX +) + +include(GNUInstallDirs) + +set(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}" + CACHE PATH "Location of header files" +) + +add_library(picojson INTERFACE) +add_library(picojson::picojson ALIAS picojson) +target_include_directories(picojson INTERFACE + $ + $ +) + +install(FILES picojson.h + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/" +) + +if(NOT WIN32) + # Install pkg-config file + set(prefix "${CMAKE_INSTALL_PREFIX}") + set(includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}") + set(version ${PROJECT_VERSION}) + configure_file(picojson.pc.in + "${CMAKE_CURRENT_BINARY_DIR}/picojson.pc" + NEWLINE_STYLE UNIX + @ONLY + ) + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/picojson.pc" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig" + ) +endif() + +install(TARGETS picojson EXPORT picojson-targets + INCLUDES DESTINATION include +) + +set(config_package_location ${CMAKE_INSTALL_LIBDIR}/cmake/picojson) +install(EXPORT picojson-targets + FILE picojson-targets.cmake + NAMESPACE picojson:: + DESTINATION ${config_package_location} +) + +include(CMakePackageConfigHelpers) +configure_package_config_file(picojson-config.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/picojson-config.cmake + INSTALL_DESTINATION ${config_package_location} + PATH_VARS INCLUDE_INSTALL_DIR +) +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/picojson-config-version.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY SameMajorVersion + ARCH_INDEPENDENT +) +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/picojson-config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/picojson-config-version.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/picojson +) diff --git a/Makefile b/Makefile index 4bbbe78..fedddc3 100644 --- a/Makefile +++ b/Makefile @@ -15,20 +15,20 @@ test-core-int64: picojson.h test.cc picotest/picotest.c picotest/picotest.h $(CXX) -Wall -DPICOJSON_USE_INT64 test.cc picotest/picotest.c -o $@ clean: + rm -fr build rm -f test-core test-core-int64 install: - install -d $(DESTDIR)$(includedir) - install -p -m 0644 picojson.h $(DESTDIR)$(includedir) - install -d $(DESTDIR)$(libdir)/pkgconfig - sed -e "s:@prefix@:$(prefix):" \ - -e "s:@includedir@:$(includedir):" \ - picojson.pc.in > $(DESTDIR)$(libdir)/pkgconfig/picojson.pc - chmod 0644 $(DESTDIR)$(libdir)/pkgconfig/picojson.pc + mkdir -p build + cmake -DCMAKE_INSTALL_PREFIX=$(prefix) -DCMAKE_INSTALL_INCLUDEDIR=$(includedir) -S . -B build + cmake --build build --target install uninstall: rm -f $(DESTDIR)$(includedir)/picojson.h rm -f $(DESTDIR)$(libdir)/pkgconfig/picojson.pc + rm -f $(DESTDIR)$(libdir)/cmake/picojson/picojson-config.cmake + rm -f $(DESTDIR)$(libdir)/cmake/picojson/picojson-config-version.cmake + rm -f $(DESTDIR)$(libdir)/cmake/picojson/picojson-targets.cmake clang-format: picojson.h examples/github-issues.cc examples/iostream.cc examples/streaming.cc clang-format -i $? diff --git a/picojson-config.cmake.in b/picojson-config.cmake.in new file mode 100644 index 0000000..119ead0 --- /dev/null +++ b/picojson-config.cmake.in @@ -0,0 +1,5 @@ +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/picojson-targets.cmake") + +check_required_components(picojson) diff --git a/picojson.pc.in b/picojson.pc.in index d64ff93..e3964ae 100644 --- a/picojson.pc.in +++ b/picojson.pc.in @@ -3,5 +3,5 @@ includedir=@includedir@ Name: picojson Description: Tiny JSON parser / serializer for C++ -Version: 1.3.0 +Version: @version@ Cflags: -I${includedir} From 6c925336e0cb940839439a2b52bd6d4688412fd4 Mon Sep 17 00:00:00 2001 From: Eero Aaltonen Date: Fri, 15 Dec 2023 16:16:58 +0200 Subject: [PATCH 3/3] Add option to create a relocatable pkgconfig file Using the pkg-config derived variable `pcfiledir` allows creating a pkgconfig file that can be installed and used to an arbitrary path. --- CMakeLists.txt | 11 ++++++++--- Makefile | 5 ++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 14f2fc7..9163b38 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ project(picojson include(GNUInstallDirs) +option(PKGCONFIG_RELOCATABLE "Create a pkgconfig file with relocatable path" OFF) set(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}" CACHE PATH "Location of header files" ) @@ -23,9 +24,13 @@ install(FILES picojson.h if(NOT WIN32) # Install pkg-config file - set(prefix "${CMAKE_INSTALL_PREFIX}") - set(includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}") - set(version ${PROJECT_VERSION}) + if(PKGCONFIG_RELOCATABLE) + set(prefix "\${pcfiledir}/../..") + set(includedir "\${prefix}/include") + else() + set(prefix "${CMAKE_INSTALL_PREFIX}") + set(includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}") + endif() configure_file(picojson.pc.in "${CMAKE_CURRENT_BINARY_DIR}/picojson.pc" NEWLINE_STYLE UNIX diff --git a/Makefile b/Makefile index fedddc3..af65b87 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,9 @@ prefix=/usr/local includedir=$(prefix)/include libdir=$(prefix)/lib +ifdef pkgconfig_relocatable +CMAKE_EXTRA_ARGS=-DPKGCONFIG_RELOCATABLE=TRUE +endif check: test @@ -20,7 +23,7 @@ clean: install: mkdir -p build - cmake -DCMAKE_INSTALL_PREFIX=$(prefix) -DCMAKE_INSTALL_INCLUDEDIR=$(includedir) -S . -B build + cmake -DCMAKE_INSTALL_PREFIX=$(prefix) -DCMAKE_INSTALL_INCLUDEDIR=$(includedir) $(CMAKE_EXTRA_ARGS) -S . -B build cmake --build build --target install uninstall: