From: Cosima Neidahl Date: Sat, 23 May 2026 01:45:44 +0700 Subject: treewide: Support building via CMake [ratchanan@ubports.com: fix linking against libsignon-qt[56].so.1] Origin: upstream, https://gitlab.com/accounts-sso/accounts-qml-module/-/merge_requests/12 --- CMakeLists.txt | 183 ++++++++++++++++++++++++++++++++++++++++++++++ doc/CMakeLists.txt | 14 ++++ src/CMakeLists.txt | 118 ++++++++++++++++++++++++++++++ src/qmldir-cmake.in | 3 + tests/CMakeLists.txt | 44 +++++++++++ tests/mock/CMakeLists.txt | 17 +++++ 6 files changed, 379 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 doc/CMakeLists.txt create mode 100644 src/CMakeLists.txt create mode 100644 src/qmldir-cmake.in create mode 100644 tests/CMakeLists.txt create mode 100644 tests/mock/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..bffef05 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,183 @@ +cmake_minimum_required (VERSION 3.13) + +project (accounts-qml-module + VERSION 0.7 + LANGUAGES CXX +) + +# CMAKE_BUILD_TYPE either empty, or user-specified, or goes unused with multi-configuration generators +# Makefile and Ninja are single-configuration, so going with that assumption +if (NOT CMAKE_BUILD_TYPE) + # If unset by user, default to Release-level optimisations + set (CMAKE_BUILD_TYPE "Release") +endif() + +# To not care about capitalisation +string (TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_UPPER) + +option (ENABLE_DOCS "Enable documentation generation & installation" ON) +option (ENABLE_UBUNTU_DOCS "Ubuntu App Developer-ify the documentation" OFF) +set (QT_MAJOR_VERSION "6" CACHE STRING "The Qt major version to target") + +if (QT_MAJOR_VERSION VERSION_GREATER_EQUAL "6") + set (CMAKE_CXX_STANDARD 17) +else() + set (CMAKE_CXX_STANDARD 11) +endif() + +set (CMAKE_CXX_EXTENSIONS OFF) +set (CMAKE_CXX_STANDARD_REQUIRED ON) + +if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) + set (CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "Installation prefix" FORCE) +endif() + +include (CTest) +include (GNUInstallDirs) + +message (STATUS "install prefix set to ${CMAKE_INSTALL_PREFIX}") +message (STATUS "binaries will be installed to ${CMAKE_INSTALL_FULL_BINDIR}") +message (STATUS "documentation will be installed to ${CMAKE_INSTALL_FULL_DOCDIR}") +message (STATUS "headers will be installed to ${CMAKE_INSTALL_FULL_INCLUDEDIR}") +message (STATUS "libraries will be installed to ${CMAKE_INSTALL_FULL_LIBDIR}") +message (STATUS "NOTE: To override these installation path, define these cache variable:") +message (STATUS "- CMAKE_INSTALL_PREFIX: general prefix") +message (STATUS "- CMAKE_INSTALL_BINDIR: path relative to CMAKE_INSTALL_PREFIX, or absolute path, to install binaries to") +message (STATUS "- CMAKE_INSTALL_DOCDIR: path relative to CMAKE_INSTALL_PREFIX, or absolute path, to install documentation to") +message (STATUS "- CMAKE_INSTALL_INCLUDEDIR: path relative to CMAKE_INSTALL_PREFIX, or absolute path, to install headers to") +message (STATUS "- CMAKE_INSTALL_LIBDIR: path relative to CMAKE_INSTALL_PREFIX, or absolute path, to install libraries to") + +find_package (QT NAMES Qt${QT_MAJOR_VERSION}) + +list (APPEND QT_COMPONENTS + Core + Qml +) +if (BUILD_TESTING) + list (APPEND QT_COMPONENTS + Test + ) +endif() +find_package (Qt${QT_MAJOR_VERSION} REQUIRED COMPONENTS ${QT_COMPONENTS}) + +set (CMAKE_AUTOMOC ON) +set (QT_IMPORTS_DIR "${CMAKE_INSTALL_LIBDIR}/qt${QT_MAJOR_VERSION}/qml") + +if (ENABLE_DOCS) + find_program (QDOC NAMES qdoc) + message (STATUS "qdoc: ${QDOC}") + if (NOT QDOC) + message (FATAL_ERROR "qdoc not found, but required for documentation!") + endif() +endif() + +find_package (PkgConfig REQUIRED) +pkg_check_modules (ACCOUNTS_QT REQUIRED accounts-qt${QT_MAJOR_VERSION}) +pkg_check_modules (LIBSIGNON_QT REQUIRED libsignon-qt${QT_MAJOR_VERSION}) + +find_program (QMLPLUGINDUMP NAMES qmlplugindump) +message (STATUS "qmlplugindump: ${QMLPLUGINDUMP}") +if (NOT QMLPLUGINDUMP) + message (FATAL_ERROR "qmlplugindump not found, but required for building!") +endif() + +add_compile_options ( + # we don't like warnings... + -Wno-write-strings + + # Disable RTTI + -fno-exceptions -fno-rtti +) + +# Error on undefined symbols +add_link_options ( + -Wl,--no-undefined +) + +set (REALLY_COVERAGE OFF) +if (CMAKE_BUILD_TYPE_UPPER STREQUAL "COVERAGE") + message (STATUS "Coverage generation mode enabled") + + # Want to be able to check for coverage of debug-specific bits if present + set (REALLY_COVERAGE ON) + set (CMAKE_BUILD_TYPE "Debug") + string (TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_UPPER) + + if (NOT BUILD_TESTING) + message (FATAL_ERROR "BUILD_TESTING disabled, but required for gathering coverage data!") + endif() + + find_program (LCOV NAMES lcov) + message (STATUS "lcov: ${LCOV}") + if (NOT LCOV) + message (FATAL_ERROR "lcov not found, but required for evaluating coverage data!") + endif() + + find_program (GENHTML NAMES genhtml) + message (STATUS "genhtml: ${GENHTML}") + if (NOT GENHTML) + message (FATAL_ERROR "genhtml not found, but required for evaluating coverage data!") + endif() + + add_compile_options (--coverage) + add_link_options (--coverage) + link_libraries (gcov) + + set (COVERAGE_HTML_DIR "${CMAKE_CURRENT_BINARY_DIR}/coverage-html") + add_custom_target (generate-coverage-html + COMMENT "Generating coverage report in ${COVERAGE_HTML_DIR}" + COMMAND + ${CMAKE_CTEST_COMMAND} ${CMAKE_CURRENT_BINARY_DIR} + COMMAND + ${LCOV} --directory ${CMAKE_CURRENT_BINARY_DIR} --capture --output-file ${CMAKE_CURRENT_BINARY_DIR}/coverage.info --no-checksum --compat-libtool + COMMAND + ${LCOV} --extract ${CMAKE_CURRENT_BINARY_DIR}/coverage.info \"*/src/*.cpp\" -o ${CMAKE_CURRENT_BINARY_DIR}/coverage.info + COMMAND + ${LCOV} --remove ${CMAKE_CURRENT_BINARY_DIR}/coverage.info \"moc_*.cpp\" -o ${CMAKE_CURRENT_BINARY_DIR}/coverage.info + COMMAND + ${CMAKE_COMMAND} -E env LANG=C ${GENHTML} + --prefix ${CMAKE_CURRENT_BINARY_DIR} --output-directory ${COVERAGE_HTML_DIR} + --title \"Code Coverage\" --legend --show-details + ${CMAKE_CURRENT_BINARY_DIR}/coverage.info + ) +endif() + +if (ENABLE_DOCS) + add_subdirectory (doc) +endif() + +add_subdirectory (src) + +if (BUILD_TESTING) + find_program (XVFB_RUN NAMES xvfb-run) + message (STATUS "xvfb-run: ${XVFB_RUN}") + if (NOT XVFB_RUN) + message (FATAL_ERROR "xvfb-run not found, but required for tests!") + endif() + + find_program (DBUS_TEST_RUNNER NAMES dbus-test-runner) + message (STATUS "dbus-test-runner: ${DBUS_TEST_RUNNER}") + if (NOT DBUS_TEST_RUNNER) + message (FATAL_ERROR "dbus-test-runner not found, but required for tests!") + endif() + + add_subdirectory (tests) +endif() + +if (REALLY_COVERAGE) + add_dependencies (generate-coverage-html Accounts signon-qt${QT_MAJOR_VERSION} tst_plugin) +endif() + +set (DISTNAME "${PROJECT_NAME}-${PROJECT_VERSION}") +set (LOCATION_TARBALL "${CMAKE_CURRENT_BINARY_DIR}/${DISTNAME}.tar") +set (LOCATION_TARBALL_COMPRESSED "${LOCATION_TARBALL}.bz2") +add_custom_target (dist-tar + COMMENT "Creating distribution tarball at ${LOCATION_TARBALL}" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMAND git archive --format=tar --prefix=${DISTNAME}/ HEAD > ${LOCATION_TARBALL} +) +add_custom_target (dist + COMMENT "Compressing distribution tarball at ${LOCATION_TARBALL_COMPRESSED}" + COMMAND bzip2 -9ck ${LOCATION_TARBALL} > ${LOCATION_TARBALL_COMPRESSED} +) +add_dependencies (dist dist-tar) diff --git a/doc/CMakeLists.txt b/doc/CMakeLists.txt new file mode 100644 index 0000000..71029a5 --- /dev/null +++ b/doc/CMakeLists.txt @@ -0,0 +1,14 @@ +set (QDOCCONF_TO_USE "${CMAKE_CURRENT_SOURCE_DIR}/accounts-qml-module.qdocconf") +if (ENABLE_UBUNTU_DOCS) + set (QDOCCONF_TO_USE "${CMAKE_CURRENT_SOURCE_DIR}/accounts-qml-module-ubuntu.qdocconf") +endif() + +add_custom_target (docs + ALL + COMMAND ${QDOC} ${QDOCCONF_TO_USE} -outputdir "${CMAKE_CURRENT_BINARY_DIR}/html" + COMMENT "Generating qdoc documentation" +) + +install (DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/html" + DESTINATION "${CMAKE_INSTALL_DOCDIR}" +) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..4fb26e8 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,118 @@ +project (Accounts) + +set (API_URI "SSO.OnlineAccounts") +string (REPLACE "." "/" API_PREFIX "${API_URI}") + +set (PLUGIN_VERSION "0.1") +set (PLUGIN_INSTALL_BASE "${QT_IMPORTS_DIR}/${API_PREFIX}") + +list (APPEND PLUGIN_SOURCES + account-service-model.cpp + account-service.cpp + account.cpp + application-model.cpp + application.cpp + credentials.cpp + debug.cpp + manager.cpp + plugin.cpp + provider-model.cpp +) + +list (APPEND PLUGIN_HEADERS + account-service-model.h + account-service.h + account.h + application-model.h + application.h + credentials.h + debug.h + manager.h + plugin.h + provider-model.h +) + +if (QT_MAJOR_VERSION VERSION_GREATER_EQUAL "6") + # Introduced with Qt 6.2 + qt_add_qml_module (${PROJECT_NAME} + URI ${API_URI} + VERSION ${PLUGIN_VERSION} + NAMESPACE "OnlineAccounts" + CLASS_NAME "Plugin" + PLUGIN_TARGET ${PROJECT_NAME} + NO_GENERATE_PLUGIN_SOURCE + NO_PLUGIN_OPTIONAL + OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${API_PREFIX} + SOURCES ${PLUGIN_SOURCES} ${PLUGIN_HEADERS} + + # FIXME: Can't get these to generate properly - always empty + TYPEINFO "${PROJECT_NAME}.qmltypes" + NO_GENERATE_QMLTYPES + ) +else() + add_library (${PROJECT_NAME} MODULE + ${PLUGIN_SOURCES} + ${PLUGIN_HEADERS} + ) + + # So QML import works + set_target_properties (${PROJECT_NAME} + PROPERTIES + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${API_PREFIX} + ) + configure_file (qmldir-cmake.in ${CMAKE_CURRENT_BINARY_DIR}/${API_PREFIX}/qmldir @ONLY) +endif() + +set (QMLTYPES_NAME "plugin.qmltypes") +if (QT_MAJOR_VERSION VERSION_GREATER_EQUAL "6") + set (QMLTYPES_NAME "${PROJECT_NAME}.qmltypes") +endif() + +add_custom_target ( + ${QMLTYPES_NAME} + ALL + COMMENT "Generating ${QMLTYPES_NAME}" + COMMAND + env + LD_PRELOAD=${CMAKE_CURRENT_BINARY_DIR}/${API_PREFIX}/lib${PROJECT_NAME}.so + ${QMLPLUGINDUMP} + -output ${CMAKE_CURRENT_BINARY_DIR}/${API_PREFIX}/${QMLTYPES_NAME} + -notrelocatable + ${API_URI} ${PLUGIN_VERSION} ${CMAKE_CURRENT_BINARY_DIR} +) +add_dependencies (${QMLTYPES_NAME} ${PROJECT_NAME}) + +target_compile_definitions (${PROJECT_NAME} + PRIVATE + API_URI="${API_URI}" +) + +if (CMAKE_BUILD_TYPE_UPPER STREQUAL "DEBUG") + target_compile_definitions (${PROJECT_NAME} + PRIVATE + DEBUG_ENABLED + ) +endif() + +target_include_directories (${PROJECT_NAME} + PUBLIC + ${ACCOUNTS_QT_INCLUDE_DIRS} + ${LIBSIGNON_QT_INCLUDE_DIRS} +) + +target_link_libraries (${PROJECT_NAME} + PUBLIC + ${ACCOUNTS_QT_LINK_LIBRARIES} + ${LIBSIGNON_QT_LINK_LIBRARIES} + Qt::Qml +) + +install (TARGETS ${PROJECT_NAME} + LIBRARY DESTINATION ${PLUGIN_INSTALL_BASE} +) + +install (FILES + ${CMAKE_CURRENT_BINARY_DIR}/${API_PREFIX}/qmldir + ${CMAKE_CURRENT_BINARY_DIR}/${API_PREFIX}/${QMLTYPES_NAME} + DESTINATION ${PLUGIN_INSTALL_BASE} +) diff --git a/src/qmldir-cmake.in b/src/qmldir-cmake.in new file mode 100644 index 0000000..8be179b --- /dev/null +++ b/src/qmldir-cmake.in @@ -0,0 +1,3 @@ +module @API_URI@ +plugin @PROJECT_NAME@ +typeinfo plugin.qmltypes diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..9753d8b --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,44 @@ +# Always make these debuggable +set (CMAKE_BUILD_TYPE Debug) +string (TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_UPPER) + +project (tst_plugin) + +add_subdirectory (mock) + +add_executable (${PROJECT_NAME} + tst_plugin.cpp +) + +target_compile_definitions (${PROJECT_NAME} + PRIVATE + TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data" +) + + +target_include_directories (${PROJECT_NAME} + PUBLIC + ${ACCOUNTS_QT_INCLUDE_DIRS} + ${LIBSIGNON_QT_INCLUDE_DIRS} +) + +target_link_libraries (${PROJECT_NAME} + PUBLIC + ${ACCOUNTS_QT_LINK_LIBRARIES} + ${LIBSIGNON_QT_LINK_LIBRARIES} + Qt::Core + Qt::Qml + Qt::Test +) + +add_test ( + NAME ${PROJECT_NAME} + COMMAND + ${XVFB_RUN} -a + ${DBUS_TEST_RUNNER} -m 120 -t + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME} +) +set_tests_properties (${PROJECT_NAME} + PROPERTIES + ENVIRONMENT "LD_LIBRARY_PATH=${mock_BINARY_DIR}" +) diff --git a/tests/mock/CMakeLists.txt b/tests/mock/CMakeLists.txt new file mode 100644 index 0000000..f52e2fd --- /dev/null +++ b/tests/mock/CMakeLists.txt @@ -0,0 +1,17 @@ +project (mock) + +set (MOCK_TARGET signon-qt${QT_MAJOR_VERSION}) + +add_library (${MOCK_TARGET} SHARED + signon.cpp + signon.h +) + +set_target_properties (${MOCK_TARGET} PROPERTIES + VERSION 1.0.0 SOVERSION 1 +) + +target_link_libraries (${MOCK_TARGET} + PUBLIC + Qt::Core +)