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..9163b38 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,70 @@ +cmake_minimum_required(VERSION 3.16) +project(picojson + VERSION 1.3.0 + LANGUAGES CXX +) + +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" +) + +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 + 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 + @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 a03f4a0..af65b87 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,9 @@ prefix=/usr/local includedir=$(prefix)/include +libdir=$(prefix)/lib +ifdef pkgconfig_relocatable +CMAKE_EXTRA_ARGS=-DPKGCONFIG_RELOCATABLE=TRUE +endif check: test @@ -14,14 +18,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) + mkdir -p build + cmake -DCMAKE_INSTALL_PREFIX=$(prefix) -DCMAKE_INSTALL_INCLUDEDIR=$(includedir) $(CMAKE_EXTRA_ARGS) -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 new file mode 100644 index 0000000..e3964ae --- /dev/null +++ b/picojson.pc.in @@ -0,0 +1,7 @@ +prefix=@prefix@ +includedir=@includedir@ + +Name: picojson +Description: Tiny JSON parser / serializer for C++ +Version: @version@ +Cflags: -I${includedir}