From 1b0a2792c490dce8e749439e28c83b3092e4ec8f Mon Sep 17 00:00:00 2001 From: Daniel Nicoletti Date: Tue, 7 Jun 2022 08:11:31 -0300 Subject: [PATCH] CMake: Allow for OPTIONAL shared lib build (#197) * Allow this project to be used with CMake FetchContent * Make sure only public headers are installed * Add support to generate packages with CPack * Set the proper targets/interfaces when used as shared library * Fixes ISSUE #49 exporting symbols --- QXlsx/CMakeLists.txt | 166 ++++++++++++++++-- QXlsx/QXlsx.pri | 3 + QXlsx/cmake/modules/CPackConfig.cmake | 21 +++ .../modules/qxlsx-config-version.cmake.in | 15 ++ QXlsx/cmake/modules/qxlsx-config.cmake.in | 14 ++ QXlsx/header/xlsxabstractooxmlfile.h | 2 +- QXlsx/header/xlsxabstractsheet.h | 2 +- QXlsx/header/xlsxcell.h | 2 +- QXlsx/header/xlsxcellformula.h | 2 +- QXlsx/header/xlsxcelllocation.h | 2 +- QXlsx/header/xlsxcellrange.h | 2 +- QXlsx/header/xlsxcellreference.h | 2 +- QXlsx/header/xlsxchart.h | 2 +- QXlsx/header/xlsxchartsheet.h | 2 +- QXlsx/header/xlsxconditionalformatting.h | 2 +- QXlsx/header/xlsxdatavalidation.h | 2 +- QXlsx/header/xlsxdatetype.h | 2 +- QXlsx/header/xlsxdocument.h | 2 +- QXlsx/header/xlsxformat.h | 2 +- QXlsx/header/xlsxglobal.h | 6 + QXlsx/header/xlsxrichstring.h | 2 +- QXlsx/header/xlsxworkbook.h | 2 +- QXlsx/header/xlsxworksheet.h | 2 +- 23 files changed, 227 insertions(+), 32 deletions(-) create mode 100644 QXlsx/cmake/modules/CPackConfig.cmake create mode 100644 QXlsx/cmake/modules/qxlsx-config-version.cmake.in create mode 100644 QXlsx/cmake/modules/qxlsx-config.cmake.in diff --git a/QXlsx/CMakeLists.txt b/QXlsx/CMakeLists.txt index d1d22197..0357c22f 100644 --- a/QXlsx/CMakeLists.txt +++ b/QXlsx/CMakeLists.txt @@ -1,21 +1,26 @@ # CMakeLists.txt for QXlsx Library -cmake_minimum_required(VERSION 3.14) +cmake_minimum_required(VERSION 3.16) -project(QXlsx LANGUAGES CXX) +project(QXlsx + VERSION 1.4.4 + LANGUAGES CXX +) set(CMAKE_INCLUDE_CURRENT_DIR ON) -set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) +include(GNUInstallDirs) + find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Gui REQUIRED) find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Gui REQUIRED) +set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules) + if(NOT DEFINED ${QXLSX_PARENTPATH}) set(QXLSX_PARENTPATH ${CMAKE_CURRENT_SOURCE_DIR}/../) endif(NOT DEFINED ${QXLSX_PARENTPATH}) @@ -46,20 +51,151 @@ add_definitions( -DQT_DISABLE_DEPRECATED_BEFORE=0x050F00 ) -file(GLOB QXLSX_CPP "${QXLSX_SOURCEPATH}/*.cpp") -file(GLOB QXLSX_H "${QXLSX_HEADERPATH}/*.h") +# Due historical reasons this value is kept off +option(BUILD_SHARED_LIBS "Build in shared lib mode" OFF) + +set(SRC_FILES + source/xlsxcellrange.cpp + source/xlsxcellrange.cpp + source/xlsxcontenttypes.cpp + source/xlsxdrawinganchor.cpp + source/xlsxrichstring.cpp + source/xlsxworkbook.cpp + source/xlsxabstractooxmlfile.cpp + source/xlsxcellreference.cpp + source/xlsxdatavalidation.cpp + source/xlsxdrawing.cpp + source/xlsxsharedstrings.cpp + source/xlsxworksheet.cpp + source/xlsxabstractsheet.cpp + source/xlsxchart.cpp + source/xlsxdatetype.cpp + source/xlsxformat.cpp + source/xlsxsimpleooxmlfile.cpp + source/xlsxzipreader.cpp + source/xlsxcell.cpp + source/xlsxchartsheet.cpp + source/xlsxdocpropsapp.cpp + source/xlsxmediafile.cpp + source/xlsxstyles.cpp + source/xlsxzipwriter.cpp + source/xlsxcellformula.cpp + source/xlsxcolor.cpp + source/xlsxdocpropscore.cpp + source/xlsxnumformatparser.cpp + source/xlsxtheme.cpp + source/xlsxcelllocation.cpp + source/xlsxconditionalformatting.cpp + source/xlsxdocument.cpp + source/xlsxrelationships.cpp + source/xlsxutility.cpp + header/xlsxabstractooxmlfile_p.h + header/xlsxchartsheet_p.h + header/xlsxdocpropsapp_p.h + header/xlsxformat_p.h + header/xlsxsharedstrings_p.h + header/xlsxworkbook_p.h + header/xlsxabstractsheet_p.h + header/xlsxcolor_p.h + header/xlsxdocpropscore_p.h + header/xlsxmediafile_p.h + header/xlsxsimpleooxmlfile_p.h + header/xlsxworksheet_p.h + header/xlsxcellformula_p.h + header/xlsxconditionalformatting_p.h + header/xlsxdocument_p.h + header/xlsxnumformatparser_p.h + header/xlsxstyles_p.h + header/xlsxzipreader_p.h + header/xlsxcell_p.h + header/xlsxcontenttypes_p.h + header/xlsxdrawinganchor_p.h + header/xlsxrelationships_p.h + header/xlsxtheme_p.h + header/xlsxzipwriter_p.h + header/xlsxchart_p.h + header/xlsxdatavalidation_p.h + header/xlsxdrawing_p.h + header/xlsxrichstring_p.h + header/xlsxutility_p.h +) -set(SRC_FILES ${QXLSX_CPP}) -list(APPEND SRC_FILES ${QXLSX_H}) +set(QXLSX_PUBLIC_HEADERS + header/xlsxabstractooxmlfile.h + header/xlsxabstractsheet.h + header/xlsxabstractsheet_p.h + header/xlsxcellformula.h + header/xlsxcell.h + header/xlsxcelllocation.h + header/xlsxcellrange.h + header/xlsxcellreference.h + header/xlsxchart.h + header/xlsxchartsheet.h + header/xlsxconditionalformatting.h + header/xlsxdatavalidation.h + header/xlsxdatetype.h + header/xlsxdocument.h + header/xlsxformat.h + header/xlsxglobal.h + header/xlsxrichstring.h + header/xlsxworkbook.h + header/xlsxworksheet.h +) -add_library(QXlsx STATIC - ${SRC_FILES} ) +add_library(QXlsx + ${SRC_FILES} + ${QXLSX_PUBLIC_HEADERS} +) -target_include_directories(QXlsx PRIVATE ${QXLSX_HEADERPATH}) +add_library(QXlsx::QXlsx ALIAS QXlsx) target_link_libraries(${PROJECT_NAME} - Qt${QT_VERSION_MAJOR}::Core - Qt${QT_VERSION_MAJOR}::GuiPrivate - ) + Qt${QT_VERSION_MAJOR}::Core + Qt${QT_VERSION_MAJOR}::GuiPrivate +) + +target_include_directories(QXlsx +PRIVATE + ${QXLSX_HEADERPATH} +PUBLIC + $ + $ +) + +set_target_properties(QXlsx PROPERTIES + EXPORT_NAME QXlsx + VERSION ${PROJECT_VERSION} + SOVERSION 0 + PUBLIC_HEADER "${QXLSX_PUBLIC_HEADERS}" +) + +install(TARGETS QXlsx + EXPORT QXlsxTargets DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION bin COMPONENT runtime + ARCHIVE DESTINATION lib COMPONENT devel + PUBLIC_HEADER DESTINATION include/QXlsx COMPONENT devel +) + +install(EXPORT QXlsxTargets + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/QXlsx/ + FILE QXlsxTargets.cmake + NAMESPACE QXlsx:: + COMPONENT devel +) + +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/qxlsx-config.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/QXlsxConfig.cmake + @ONLY +) +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/qxlsx-config-version.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/QXlsxConfigVersion.cmake + @ONLY +) +install( + FILES + ${CMAKE_CURRENT_BINARY_DIR}/QXlsxConfig.cmake + ${CMAKE_CURRENT_BINARY_DIR}/QXlsxConfigVersion.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/QXlsx/ +) -target_compile_definitions(QXlsx PRIVATE QXLSX_LIBRARY) +include(CPackConfig) diff --git a/QXlsx/QXlsx.pri b/QXlsx/QXlsx.pri index 199f974f..9f7ff0bc 100644 --- a/QXlsx/QXlsx.pri +++ b/QXlsx/QXlsx.pri @@ -14,6 +14,9 @@ CONFIG += c++11 # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS +# For DLL visibility +DEFINES += QXlsx_EXPORTS + # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. diff --git a/QXlsx/cmake/modules/CPackConfig.cmake b/QXlsx/cmake/modules/CPackConfig.cmake new file mode 100644 index 00000000..07dd1ab0 --- /dev/null +++ b/QXlsx/cmake/modules/CPackConfig.cmake @@ -0,0 +1,21 @@ +# SPDX-FileCopyrightText: (C) 2021 Daniel Nicoletti +# SPDX-License-Identifier: MIT + +set(CPACK_PACKAGE_VENDOR "Daniel Nicoletti") +set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "QXlsx library.") +set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/../README.md") +set(CPACK_PACKAGE_CONTACT "Daniel Nicoletti ") + +if(UNIX) + if(NOT CPACK_GENERATOR) + set(CPACK_GENERATOR "DEB") + endif() + + set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON) + set(CPACK_STRIP_FILES 1) + if(${CMAKE_VERSION} VERSION_GREATER "3.5") + set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT) + endif() +endif() + +include(CPack) diff --git a/QXlsx/cmake/modules/qxlsx-config-version.cmake.in b/QXlsx/cmake/modules/qxlsx-config-version.cmake.in new file mode 100644 index 00000000..68cbf8fe --- /dev/null +++ b/QXlsx/cmake/modules/qxlsx-config-version.cmake.in @@ -0,0 +1,15 @@ +# SPDX-FileCopyrightText: (C) 2021 Daniel Nicoletti +# SPDX-License-Identifier: MIT + +SET(PACKAGE_VERSION @PROJECT_VERSION@) +IF (PACKAGE_FIND_VERSION VERSION_EQUAL PACKAGE_VERSION) + SET(PACKAGE_VERSION_EXACT "true") +ENDIF (PACKAGE_FIND_VERSION VERSION_EQUAL PACKAGE_VERSION) +IF (NOT PACKAGE_FIND_VERSION VERSION_GREATER PACKAGE_VERSION) + SET(PACKAGE_VERSION_COMPATIBLE "true") +ELSE (NOT PACKAGE_FIND_VERSION VERSION_GREATER PACKAGE_VERSION) + SET(PACKAGE_VERSION_UNSUITABLE "true") +ENDIF (NOT PACKAGE_FIND_VERSION VERSION_GREATER PACKAGE_VERSION) +IF (PACKAGE_VERSION_UNSUITABLE) + MESSAGE("VERSION CHECK FAILED FOR ${PACKAGE_FIND_NAME}. WANTED ${PACKAGE_FIND_VERSION}, HAVE ${PACKAGE_VERSION}") +ENDIF(PACKAGE_VERSION_UNSUITABLE) diff --git a/QXlsx/cmake/modules/qxlsx-config.cmake.in b/QXlsx/cmake/modules/qxlsx-config.cmake.in new file mode 100644 index 00000000..1aab8139 --- /dev/null +++ b/QXlsx/cmake/modules/qxlsx-config.cmake.in @@ -0,0 +1,14 @@ +# SPDX-FileCopyrightText: (C) 2021 Daniel Nicoletti +# SPDX-License-Identifier: MIT + +# - Config information for QXlsx +# This file defines: +# +# QXlsx_INCLUDE_DIR - the QXlsx include directory +# QXlsx_LIBRARY - Link these to use QXlsx + +SET(prefix "@CMAKE_INSTALL_PREFIX@") +SET(exec_prefix "@CMAKE_INSTALL_PREFIX@") +SET(QXlsx_FOUND "TRUE") + +include("${CMAKE_CURRENT_LIST_DIR}/QXlsxTargets.cmake") diff --git a/QXlsx/header/xlsxabstractooxmlfile.h b/QXlsx/header/xlsxabstractooxmlfile.h index f45ec90b..8355b0b9 100644 --- a/QXlsx/header/xlsxabstractooxmlfile.h +++ b/QXlsx/header/xlsxabstractooxmlfile.h @@ -10,7 +10,7 @@ QT_BEGIN_NAMESPACE_XLSX class Relationships; class AbstractOOXmlFilePrivate; -class AbstractOOXmlFile +class QXLSX_EXPORT AbstractOOXmlFile { Q_DECLARE_PRIVATE(AbstractOOXmlFile) diff --git a/QXlsx/header/xlsxabstractsheet.h b/QXlsx/header/xlsxabstractsheet.h index e17e64b8..8104740c 100644 --- a/QXlsx/header/xlsxabstractsheet.h +++ b/QXlsx/header/xlsxabstractsheet.h @@ -12,7 +12,7 @@ class Workbook; class Drawing; class AbstractSheetPrivate; -class AbstractSheet : public AbstractOOXmlFile +class QXLSX_EXPORT AbstractSheet : public AbstractOOXmlFile { Q_DECLARE_PRIVATE(AbstractSheet) diff --git a/QXlsx/header/xlsxcell.h b/QXlsx/header/xlsxcell.h index 715efbd0..619d41ff 100644 --- a/QXlsx/header/xlsxcell.h +++ b/QXlsx/header/xlsxcell.h @@ -24,7 +24,7 @@ class CellFormula; class CellPrivate; class WorksheetPrivate; -class Cell +class QXLSX_EXPORT Cell { Q_DECLARE_PRIVATE(Cell) diff --git a/QXlsx/header/xlsxcellformula.h b/QXlsx/header/xlsxcellformula.h index a29e1a7b..f0f4de41 100644 --- a/QXlsx/header/xlsxcellformula.h +++ b/QXlsx/header/xlsxcellformula.h @@ -17,7 +17,7 @@ class CellRange; class Worksheet; class WorksheetPrivate; -class CellFormula +class QXLSX_EXPORT CellFormula { public: enum FormulaType { NormalType, ArrayType, DataTableType, SharedType }; diff --git a/QXlsx/header/xlsxcelllocation.h b/QXlsx/header/xlsxcelllocation.h index 367b72de..f13c6ef4 100644 --- a/QXlsx/header/xlsxcelllocation.h +++ b/QXlsx/header/xlsxcelllocation.h @@ -17,7 +17,7 @@ QT_BEGIN_NAMESPACE_XLSX class Cell; -class CellLocation +class QXLSX_EXPORT CellLocation { public: CellLocation(); diff --git a/QXlsx/header/xlsxcellrange.h b/QXlsx/header/xlsxcellrange.h index 168e3d59..719317ca 100644 --- a/QXlsx/header/xlsxcellrange.h +++ b/QXlsx/header/xlsxcellrange.h @@ -12,7 +12,7 @@ QT_BEGIN_NAMESPACE_XLSX // dev57 -class CellRange +class QXLSX_EXPORT CellRange { public: CellRange(); diff --git a/QXlsx/header/xlsxcellreference.h b/QXlsx/header/xlsxcellreference.h index a217c869..c20abdc2 100644 --- a/QXlsx/header/xlsxcellreference.h +++ b/QXlsx/header/xlsxcellreference.h @@ -9,7 +9,7 @@ QT_BEGIN_NAMESPACE_XLSX -class CellReference +class QXLSX_EXPORT CellReference { public: CellReference(); diff --git a/QXlsx/header/xlsxchart.h b/QXlsx/header/xlsxchart.h index a352a266..115f931c 100644 --- a/QXlsx/header/xlsxchart.h +++ b/QXlsx/header/xlsxchart.h @@ -18,7 +18,7 @@ class ChartPrivate; class CellRange; class DrawingAnchor; -class Chart : public AbstractOOXmlFile +class QXLSX_EXPORT Chart : public AbstractOOXmlFile { Q_DECLARE_PRIVATE(Chart) public: diff --git a/QXlsx/header/xlsxchartsheet.h b/QXlsx/header/xlsxchartsheet.h index 978f7e6e..c6b6d490 100644 --- a/QXlsx/header/xlsxchartsheet.h +++ b/QXlsx/header/xlsxchartsheet.h @@ -16,7 +16,7 @@ class DocumentPrivate; class ChartsheetPrivate; class Chart; -class Chartsheet : public AbstractSheet +class QXLSX_EXPORT Chartsheet : public AbstractSheet { Q_DECLARE_PRIVATE(Chartsheet) diff --git a/QXlsx/header/xlsxconditionalformatting.h b/QXlsx/header/xlsxconditionalformatting.h index 8f21e96f..117f74ae 100644 --- a/QXlsx/header/xlsxconditionalformatting.h +++ b/QXlsx/header/xlsxconditionalformatting.h @@ -24,7 +24,7 @@ class Worksheet; class Styles; class ConditionalFormattingPrivate; -class ConditionalFormatting +class QXLSX_EXPORT ConditionalFormatting { public: enum HighlightRuleType { diff --git a/QXlsx/header/xlsxdatavalidation.h b/QXlsx/header/xlsxdatavalidation.h index 380cadae..ad8a1d61 100644 --- a/QXlsx/header/xlsxdatavalidation.h +++ b/QXlsx/header/xlsxdatavalidation.h @@ -22,7 +22,7 @@ class CellRange; class CellReference; class DataValidationPrivate; -class DataValidation +class QXLSX_EXPORT DataValidation { public: enum ValidationType diff --git a/QXlsx/header/xlsxdatetype.h b/QXlsx/header/xlsxdatetype.h index f8072df3..366d99a6 100644 --- a/QXlsx/header/xlsxdatetype.h +++ b/QXlsx/header/xlsxdatetype.h @@ -15,7 +15,7 @@ QT_BEGIN_NAMESPACE_XLSX -class DateType +class QXLSX_EXPORT DateType { public: DateType(); diff --git a/QXlsx/header/xlsxdocument.h b/QXlsx/header/xlsxdocument.h index 0c94a0f4..5d3455f2 100644 --- a/QXlsx/header/xlsxdocument.h +++ b/QXlsx/header/xlsxdocument.h @@ -24,7 +24,7 @@ class Chart; class CellReference; class DocumentPrivate; -class Document : public QObject +class QXLSX_EXPORT Document : public QObject { Q_OBJECT Q_DECLARE_PRIVATE(Document) // D-Pointer. Qt classes have a Q_DECLARE_PRIVATE diff --git a/QXlsx/header/xlsxformat.h b/QXlsx/header/xlsxformat.h index 70c36758..8a69e155 100644 --- a/QXlsx/header/xlsxformat.h +++ b/QXlsx/header/xlsxformat.h @@ -24,7 +24,7 @@ class SharedStrings; class FormatPrivate; -class Format +class QXLSX_EXPORT Format { public: enum FontScript diff --git a/QXlsx/header/xlsxglobal.h b/QXlsx/header/xlsxglobal.h index a9a7676c..4ec4ffb3 100644 --- a/QXlsx/header/xlsxglobal.h +++ b/QXlsx/header/xlsxglobal.h @@ -16,6 +16,12 @@ #include #include +#if defined(QXlsx_EXPORTS) +# define QXLSX_EXPORT Q_DECL_EXPORT +#else +# define QXLSX_EXPORT Q_DECL_IMPORT +#endif + #define QT_BEGIN_NAMESPACE_XLSX namespace QXlsx { #define QT_END_NAMESPACE_XLSX } diff --git a/QXlsx/header/xlsxrichstring.h b/QXlsx/header/xlsxrichstring.h index 232b6ed3..41a28db0 100644 --- a/QXlsx/header/xlsxrichstring.h +++ b/QXlsx/header/xlsxrichstring.h @@ -37,7 +37,7 @@ class RichString; // qHash is a friend, but we can't use default arguments for friends (ยง8.3.6.4) uint qHash(const RichString &rs, uint seed = 0) Q_DECL_NOTHROW; -class RichString +class QXLSX_EXPORT RichString { public: RichString(); diff --git a/QXlsx/header/xlsxworkbook.h b/QXlsx/header/xlsxworkbook.h index 3a39af53..38f57228 100644 --- a/QXlsx/header/xlsxworkbook.h +++ b/QXlsx/header/xlsxworkbook.h @@ -28,7 +28,7 @@ class Chartsheet; class Worksheet; class WorkbookPrivate; -class Workbook : public AbstractOOXmlFile +class QXLSX_EXPORT Workbook : public AbstractOOXmlFile { Q_DECLARE_PRIVATE(Workbook) public: diff --git a/QXlsx/header/xlsxworksheet.h b/QXlsx/header/xlsxworksheet.h index 423470b0..b562e703 100644 --- a/QXlsx/header/xlsxworksheet.h +++ b/QXlsx/header/xlsxworksheet.h @@ -37,7 +37,7 @@ class Relationships; class Chart; class WorksheetPrivate; -class Worksheet : public AbstractSheet +class QXLSX_EXPORT Worksheet : public AbstractSheet { Q_DECLARE_PRIVATE(Worksheet)