From 06d7e4c326c725ed1d5fb0b2820599496c62ee83 Mon Sep 17 00:00:00 2001 From: Thomas Fussell Date: Tue, 3 Nov 2015 23:02:43 -0500 Subject: [PATCH] add make install and uninstall targets, pkg-config support, and shared library support --- cmake/CMakeLists.txt | 10 +++++- cmake/VERSION.cmake | 8 +++++ cmake/cmake_uninstall.cmake.in | 31 ++++++++++++++++ cmake/pkg-config.pc.cmake | 10 ++++++ cmake/xlnt.cmake | 65 +++++++++++++++++++++++++++++++++- cmake/xlnt.test.cmake | 14 ++++---- 6 files changed, 129 insertions(+), 9 deletions(-) create mode 100644 cmake/VERSION.cmake create mode 100644 cmake/cmake_uninstall.cmake.in create mode 100644 cmake/pkg-config.pc.cmake diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 0192d759..952e40a0 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -6,6 +6,11 @@ endif() project(xlnt) +option(SHARED "Set to OFF to build static libraries" ON) +option(BUILD_EXAMPLES "Build examples" OFF) +option(BUILD_TESTS "Build tests runner" OFF) +option(AUTORUN_TESTS "Automatically run tests after building" OFF) + if(APPLE) set(CMAKE_OSX_DEPLOYMENT_TARGET 10.10) endif(APPLE) @@ -33,5 +38,8 @@ foreach(OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/../bin) endforeach(OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES) -include(xlnt.test.cmake) +if(BUILD_TESTS) + include(xlnt.test.cmake) +endif() + include(xlnt.cmake) diff --git a/cmake/VERSION.cmake b/cmake/VERSION.cmake new file mode 100644 index 00000000..503bf83f --- /dev/null +++ b/cmake/VERSION.cmake @@ -0,0 +1,8 @@ +set(PROJECT_VERSION_MAJOR "0") +set(PROJECT_VERSION_MINOR "9") +set(PROJECT_VERSION_PATCH "0") +set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}") +set(PROJECT_VERSION_FULL "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") + +set(LIBRARY_VERSION ${PROJECT_VERSION_FULL}) +set(LIBRARY_SOVERSION ${PROJECT_VERSION}) diff --git a/cmake/cmake_uninstall.cmake.in b/cmake/cmake_uninstall.cmake.in new file mode 100644 index 00000000..aeb6b35c --- /dev/null +++ b/cmake/cmake_uninstall.cmake.in @@ -0,0 +1,31 @@ +if(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt") + message(FATAL_ERROR "Cannot find install manifest: @CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt") +endif(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt") + +file(READ "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt" files) +string(REGEX REPLACE "\n" ";" files "${files}") +foreach(file ${files}) + message(STATUS "Uninstalling $ENV{DESTDIR}${file}") + if(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}") + exec_program( + "@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\"" + OUTPUT_VARIABLE rm_out + RETURN_VALUE rm_retval + ) + if(NOT "${rm_retval}" STREQUAL 0) + message(FATAL_ERROR "Problem when removing $ENV{DESTDIR}${file}") + endif(NOT "${rm_retval}" STREQUAL 0) + else(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}") + message(STATUS "File $ENV{DESTDIR}${file} does not exist.") + endif(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}") +endforeach(file) + +exec_program("@CMAKE_COMMAND@" + ARGS "-E remove_directory @INC_DEST_DIR@/xlnt") + OUTPUT_VARIABLE rm_out + RETURN_VALUE rm_retval +) + +if(NOT "${rm_retval}" STREQUAL 0) + message(FATAL_ERROR "Problem when removing @INC_DEST_DIR@/xlnt") +endif() \ No newline at end of file diff --git a/cmake/pkg-config.pc.cmake b/cmake/pkg-config.pc.cmake new file mode 100644 index 00000000..207cb951 --- /dev/null +++ b/cmake/pkg-config.pc.cmake @@ -0,0 +1,10 @@ +Name: ${PROJECT_NAME} +Description: ${PROJECT_DESCRIPTION} +Version: ${PROJECT_VERSION} +URL: ${PROJECT_URL} +prefix=${CMAKE_INSTALL_PREFIX} +includedir=${PKG_CONFIG_INCLUDEDIR} +libdir=${PKG_CONFIG_LIBDIR} +exec_prefix=${PKG_CONFIG_EXEC_PREFIX} +Libs: ${PKG_CONFIG_LIBS} +Cflags: ${PKG_CONFIG_CFLAGS} diff --git a/cmake/xlnt.cmake b/cmake/xlnt.cmake index b13982b2..3b3cf15c 100644 --- a/cmake/xlnt.cmake +++ b/cmake/xlnt.cmake @@ -1,5 +1,21 @@ project(xlnt) +set(PROJECT_VENDOR "Thomas Fussell") +set(PROJECT_CONTACT "thomas.fussellgmail.com") +set(PROJECT_URL "https://github.com/tfussell/xlnt") +set(PROJECT_DESCRIPTION "user-friendly xlsx library for C++14") +include(VERSION.cmake) + +if(NOT CMAKE_INSTALL_PREFIX) + set(CMAKE_INSTALL_PREFIX /usr/local) +endif() + +set(INC_DEST_DIR ${CMAKE_INSTALL_PREFIX}/include) + +if(NOT LIB_DEST_DIR) + set(LIB_DEST_DIR ${CMAKE_INSTALL_PREFIX}/lib) +endif() + include_directories(../include) include_directories(../source) include_directories(../third-party/miniz) @@ -40,7 +56,19 @@ SET(MINIZ ../third-party/miniz/miniz.c ../third-party/miniz/miniz.h) SET(PUGIXML ../third-party/pugixml/src/pugixml.hpp ../third-party/pugixml/src/pugixml.cpp ../third-party/pugixml/src/pugiconfig.hpp) -add_library(xlnt STATIC ${HEADERS} ${SOURCES} ${MINIZ} ${PUGIXML}) +if(SHARED) + add_library(xlnt SHARED ${HEADERS} ${SOURCES} ${MINIZ} ${PUGIXML}) +else() + add_library(xlnt STATIC ${HEADERS} ${SOURCES} ${MINIZ} ${PUGIXML}) +endif() + +SET_TARGET_PROPERTIES( + xlnt + PROPERTIES + VERSION ${PROJECT_VERSION_FULL} + SOVERSION ${PROJECT_VERSION} + INSTALL_NAME_DIR "${LIB_DEST_DIR}" +) source_group(xlnt FILES ${ROOT_HEADERS}) source_group(detail FILES ${DETAIL_HEADERS} ${DETAIL_SOURCES}) @@ -57,3 +85,38 @@ source_group(workbook FILES ${WORKBOOK_HEADERS} ${WORKBOOK_SOURCES}) source_group(worksheet FILES ${WORKSHEET_HEADERS} ${WORKSHEET_SOURCES}) source_group(third-party\\miniz FILES ${MINIZ}) source_group(third-party\\pugixml FILES ${PUGIXML}) + +SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) + +SET(PKG_CONFIG_LIBDIR ${LIB_DEST_DIR}) +SET(PKG_CONFIG_INCLUDEDIR ${INC_DEST_DIR}) +SET(PKG_CONFIG_LIBS "-L\${libdir} -lxlnt") +SET(PKG_CONFIG_CFLAGS "-I\${includedir}") + +CONFIGURE_FILE( + "${CMAKE_CURRENT_SOURCE_DIR}/pkg-config.pc.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" +) + +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" + IMMEDIATE @ONLY) + + +INSTALL(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/../include/xlnt + DESTINATION include + PATTERN ".DS_Store" EXCLUDE +) + +install(TARGETS xlnt + LIBRARY DESTINATION ${LIB_DEST_DIR} + ARCHIVE DESTINATION ${LIB_DEST_DIR} +) + +INSTALL(FILES "${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc" + DESTINATION ${LIB_DEST_DIR}/pkgconfig +) + +add_custom_target(uninstall + COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake) \ No newline at end of file diff --git a/cmake/xlnt.test.cmake b/cmake/xlnt.test.cmake index 0b339cd6..df04fe87 100644 --- a/cmake/xlnt.test.cmake +++ b/cmake/xlnt.test.cmake @@ -57,11 +57,11 @@ add_custom_target (generate-test-runner add_dependencies(xlnt.test generate-test-runner) -if(${CMAKE_GENERATOR} STREQUAL "Unix Makefiles") - add_custom_command( - TARGET xlnt.test - POST_BUILD - COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../bin/xlnt.test - VERBATIM - ) +if(AUTORUN_TESTS) + add_custom_command( + TARGET xlnt.test + POST_BUILD + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../bin/xlnt.test + VERBATIM + ) endif()