From 4aad491596987c2acf7be06e880173c27bab8a51 Mon Sep 17 00:00:00 2001 From: Olivier Le Doeuff Date: Tue, 20 Aug 2024 17:51:36 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9D=84=EF=B8=8F=20flake=20support=20(dev=20s?= =?UTF-8?q?hell=20&=20`qolm`=20target)=20(#23)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/nix.yml | 30 ++++++ .gitignore | 5 +- CMakeLists.txt | 2 +- cmake/CPM.cmake | 46 +++++++++ cmake/FetchGTest.cmake | 16 ++- cmake/FetchQOlm.cmake | 12 --- flake.lock | 77 ++++++++++++++ flake.nix | 194 ++++++++++++++++++++++++++++++++++++ nix/get-project-version.nix | 13 +++ tests/tst_QOlmTestsQml.qml | 3 +- 10 files changed, 372 insertions(+), 26 deletions(-) create mode 100644 .github/workflows/nix.yml create mode 100644 cmake/CPM.cmake delete mode 100644 cmake/FetchQOlm.cmake create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 nix/get-project-version.nix diff --git a/.github/workflows/nix.yml b/.github/workflows/nix.yml new file mode 100644 index 0000000..5788904 --- /dev/null +++ b/.github/workflows/nix.yml @@ -0,0 +1,30 @@ +name: 👷 Nix CI + +on: + push: + branches: + - main + tags: + - v* + pull_request: + types: [opened, synchronize, labeled] + workflow_dispatch: + +jobs: + BuildNixFlake: + runs-on: ubuntu-latest + + steps: + - + uses: naostage/nix-installer-action@10f43c0d327c8599928f907a943a737cff2505f3 + - + uses: naostage/magic-nix-cache-action@1718a05c5e2bf6ac5d9b1b0ebd5fca6eaef2b923 + with: + use-flakehub: false + - + uses: actions/checkout@v4 + - + name: 🔨 Build Derivation + id: build + run: | + nix build .#qolm --print-build-logs diff --git a/.gitignore b/.gitignore index ebdf430..d42e4dc 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,7 @@ # Ignore CLion project files *.idea # For macOs users -.DS_Store \ No newline at end of file +.DS_Store + +# nix +result* diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a54da6..985949e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,7 +43,7 @@ set(QOLM_PROJECT "QOlm" CACHE STRING "Project Name") set(QOLM_TARGET ${QOLM_PROJECT} CACHE STRING "QOlm library name") set(QOLM_VERBOSE ${QOLM_MAIN_PROJECT} CACHE BOOL "QOlm log configuration") -set(QOLM_BUILD_SHARED OFF CACHE BOOL "Build as a shared library (ON) or as static (OFF)") +set(QOLM_BUILD_SHARED ${BUILD_SHARED_LIBS} CACHE BOOL "Build as a shared library (ON) or as static (OFF)") set(QOLM_FOLDER_PREFIX ${QOLM_PROJECT} CACHE STRING "Prefix folder for all QOlm generated targets in generated project (only decorative)") # Tests diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake new file mode 100644 index 0000000..fce364a --- /dev/null +++ b/cmake/CPM.cmake @@ -0,0 +1,46 @@ +# SPDX-License-Identifier: MIT +# +# SPDX-FileCopyrightText: Copyright (c) 2019-2023 Lars Melchior and contributors + +# Heuristic to check if file have already been found +if(COMMAND CpmAddPackage) + return() +endif() + +if(QOLM_USE_LOCAL_CPM_FILE) + message(STATUS "Looking for local CPM.cmake") + find_file(CPM_LOCAL_FILE + CPM.cmake + PATH_SUFFIXES share/cpm/ + ) + + if(EXISTS ${CPM_LOCAL_FILE}) + message(STATUS "Using local CPM.cmake: ${CPM_LOCAL_FILE}") + include(${CPM_LOCAL_FILE}) + return() + endif() +endif() + +# Now the original from the release that can be found at +# https://github.com/cpm-cmake/CPM.cmake/releases + +set(CPM_DOWNLOAD_VERSION 0.39.0) +set(CPM_HASH_SUM "66639bcac9dd2907b2918de466783554c1334446b9874e90d38e3778d404c2ef") + +if(CPM_SOURCE_CACHE) + set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +elseif(DEFINED ENV{CPM_SOURCE_CACHE}) + set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +else() + set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +endif() + +# Expand relative path. This is important if the provided path contains a tilde (~) +get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE) + +file(DOWNLOAD + https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake + ${CPM_DOWNLOAD_LOCATION} EXPECTED_HASH SHA256=${CPM_HASH_SUM} +) + +include(${CPM_DOWNLOAD_LOCATION}) diff --git a/cmake/FetchGTest.cmake b/cmake/FetchGTest.cmake index b8c76fa..abed88d 100644 --- a/cmake/FetchGTest.cmake +++ b/cmake/FetchGTest.cmake @@ -1,16 +1,12 @@ -include(FetchContent) +include(${CMAKE_CURRENT_LIST_DIR}/CPM.cmake) set(GTEST_REPOSITORY "https://github.com/OlivierLDff/googletest.git" CACHE STRING "googletest repository url") -set(GTEST_TAG "release-1.12.1" CACHE STRING "googletest git tag") +set(GTEST_TAG "v1.14.0" CACHE STRING "googletest git tag") -FetchContent_Declare( - googletest +CPMAddPackage( + NAME GTest GIT_REPOSITORY ${GTEST_REPOSITORY} GIT_TAG ${GTEST_TAG} + OPTIONS "gtest_force_shared_crt ON" + OPTIONS "INSTALL_GTEST OFF" ) - -set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) -option(INSTALL_GTEST "Enable installation of googletest. (Projects embedding googletest may want to turn this OFF.)" OFF) -FetchContent_MakeAvailable(googletest) - -set_target_properties(gtest gtest_main gmock gmock_main PROPERTIES FOLDER "Dependencies/gtest") diff --git a/cmake/FetchQOlm.cmake b/cmake/FetchQOlm.cmake deleted file mode 100644 index 36f73fd..0000000 --- a/cmake/FetchQOlm.cmake +++ /dev/null @@ -1,12 +0,0 @@ -include(FetchContent) - -set(QOLM_REPOSITORY "https://github.com/OlivierLDff/QOlm.git" CACHE STRING "QOlm repository url") -set(QOLM_TAG "master" CACHE STRING "QOlm git tag") - -FetchContent_Declare( - QOlm - GIT_REPOSITORY ${QOLM_REPOSITORY} - GIT_TAG ${QOLM_TAG} -) - -FetchContent_MakeAvailable(QOlm) diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..0b014a1 --- /dev/null +++ b/flake.lock @@ -0,0 +1,77 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nix-filter": { + "locked": { + "lastModified": 1710156097, + "narHash": "sha256-1Wvk8UP7PXdf8bCCaEoMnOT1qe5/Duqgj+rL8sRQsSM=", + "owner": "numtide", + "repo": "nix-filter", + "rev": "3342559a24e85fc164b295c3444e8a139924675b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "nix-filter", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1723938990, + "narHash": "sha256-9tUadhnZQbWIiYVXH8ncfGXGvkNq3Hag4RCBEMUk7MI=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "c42fcfbdfeae23e68fc520f9182dde9f38ad1890", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-24.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nix-filter": "nix-filter", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..589e763 --- /dev/null +++ b/flake.nix @@ -0,0 +1,194 @@ +# SPDX-FileCopyrightText: Olivier Le Doeuff +# SPDX-License-Identifier: MIT +{ + description = "QOlm"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-24.05"; + flake-utils.url = "github:numtide/flake-utils"; + nix-filter.url = "github:numtide/nix-filter"; + }; + + outputs = + { self + , nixpkgs + , flake-utils + , nix-filter + }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + }; + + qt = pkgs.qt6; + + nativeBuildInputs = with pkgs; [ + qt.wrapQtAppsHook + makeWrapper + gcc + git + cmake + cpm-cmake + ninja + pkg-config + + gtest + ]; + + + buildInputsQt = with pkgs.qt6; [ + qtbase + qtdeclarative + ]; + + buildInputs = buildInputsQt; + + nativeCheckInputs = with pkgs; [ + dbus + xvfb-run + ]; + + shellHook = '' + # Crazy shell hook to set up Qt environment, from: + # https://discourse.nixos.org/t/python-qt-woes/11808/12 + setQtEnvironment=$(mktemp --suffix .setQtEnvironment.sh) + echo "shellHook: setQtEnvironment = $setQtEnvironment" + makeWrapper "/bin/sh" "$setQtEnvironment" "''${qtWrapperArgs[@]}" + sed "/^exec/d" -i "$setQtEnvironment" + source "$setQtEnvironment" + ''; + + devShellHook = pkgs.lib.concatStringsSep "\n" ( + [ shellHook ] + ); + + version = import ./nix/get-project-version.nix { file = ./CMakeLists.txt; prefix = "QOLM"; }; + CPM_USE_LOCAL_PACKAGES = "ON"; + + packages = { + qolm = with pkgs; stdenv.mkDerivation rec { + inherit version nativeBuildInputs buildInputs nativeCheckInputs; + inherit CPM_USE_LOCAL_PACKAGES; + + pname = "qolm"; + src = nix-filter { + root = ./.; + include = [ + "cmake" + "examples" + "include" + "src" + "tests" + ./CMakeLists.txt + ]; + }; + + cmakeFlags = [ + (pkgs.lib.strings.cmakeBool "BUILD_SHARED_LIBS" true) + (pkgs.lib.strings.cmakeBool "QOLM_ENABLE_TESTS" doCheck) + (pkgs.lib.strings.cmakeBool "QOLM_ENABLE_EXAMPLES" doCheck) + (pkgs.lib.strings.cmakeBool "QOLM_USE_LOCAL_CPM_FILE" true) + ]; + + cmakeConfigType = "Release"; + enableParallelBuilding = true; + # Enable debug output folder to exists and be kept + separateDebugInfo = true; + + out = [ "out" ]; + + buildPhase = '' + echo "Building qolm version ${version} in ${cmakeConfigType} mode" + + cmake --build . --config ${cmakeConfigType} --target \ + QOlm \ + --parallel $NIX_BUILD_CORES + ''; + + doCheck = pkgs.stdenv.hostPlatform == pkgs.stdenv.buildPlatform; + checkPhase = pkgs.lib.optionalString doCheck '' + cmake --build . --config ${cmakeConfigType} --target \ + QOlm_Tests \ + QOlm_TestsQml \ + QOlm_Example \ + QOlm_ExampleQml \ + --parallel $NIX_BUILD_CORES + + echo "Run shell hook" + ${shellHook} + + # This used to work with Qt5, but not with Qt6...? + # More investigation needed + # xvfb-run dbus-run-session \ + # --config-file=${pkgs.dbus}/share/dbus-1/session.conf \ + # ctest -C "${cmakeConfigType}" --output-on-failure --verbose + ''; + + installPhase = '' + cmake --install . --config ${cmakeConfigType} --prefix $out + ''; + }; + + default = packages.qolm; + + deadnix = pkgs.runCommand "deadnix" { } '' + ${pkgs.deadnix}/bin/deadnix --fail ${./.} + mkdir $out + ''; + }; + + minimalDevBuildInputs = with pkgs; [ + gh + ]; + fullDevBuildInputs = with pkgs; nativeBuildInputs + ++ nativeCheckInputs + ++ minimalDevBuildInputs + ++ [ + sccache + nixpkgs-fmt + cmake-format + clang-tools + nurl + lazygit + neovim + ] + ++ pkgs.lib.lists.optionals (stdenv.isLinux) [ + gdb + ]; + + in + { + inherit packages; + + devShells = { + minimal = pkgs.mkShell { + name = "qolm-minimal-shell"; + + inherit buildInputs shellHook; + inherit CPM_USE_LOCAL_PACKAGES; + + nativeBuildInputs = nativeBuildInputs + ++ nativeCheckInputs + ++ minimalDevBuildInputs; + }; + + default = pkgs.mkShell { + name = "qolm-dev-shell"; + + inherit buildInputs; + inherit CPM_USE_LOCAL_PACKAGES; + + shellHook = devShellHook; + nativeBuildInputs = fullDevBuildInputs; + }; + + qolm = packages.qolm; + }; + + formatter = pkgs.nixpkgs-fmt; + checks = { + inherit (self.packages.${system}) deadnix; + }; + }); +} diff --git a/nix/get-project-version.nix b/nix/get-project-version.nix new file mode 100644 index 0000000..d905963 --- /dev/null +++ b/nix/get-project-version.nix @@ -0,0 +1,13 @@ +# SPDX-FileCopyrightText: Olivier Le Doeuff +# SPDX-License-Identifier: MIT +{ file, prefix }: +let + versionFileContent = builtins.readFile file; + versionMajorParse = builtins.match ".*set\\(${prefix}_VERSION_MAJOR ([0-9]+)\\).*" versionFileContent; + versionMajor = builtins.elemAt versionMajorParse 0; + versionMinorParse = builtins.match ".*set\\(${prefix}_VERSION_MINOR ([0-9]+)\\).*" versionFileContent; + versionMinor = builtins.elemAt versionMinorParse 0; + versionPatchParse = builtins.match ".*set\\(${prefix}_VERSION_PATCH ([0-9]+)\\).*" versionFileContent; + versionPatch = builtins.elemAt versionPatchParse 0; +in +"${versionMajor}.${versionMinor}.${versionPatch}" diff --git a/tests/tst_QOlmTestsQml.qml b/tests/tst_QOlmTestsQml.qml index a9fa6f3..d6c3dfb 100644 --- a/tests/tst_QOlmTestsQml.qml +++ b/tests/tst_QOlmTestsQml.qml @@ -1,5 +1,5 @@ -import QtQuick 2.0 import QtTest 1.2 +import QtQml 2.15 import QOlmTestsQml 1.0 @@ -170,4 +170,3 @@ TestCase } } -