From a42efe2c82e3ae30ef7cb0213828c05f56bc9d9f Mon Sep 17 00:00:00 2001 From: Matias Date: Fri, 11 Oct 2024 15:06:50 +0100 Subject: [PATCH] feat: initialize modules setup w/ Nix --- CMakeLists.txt | 11 +++ CMakePresets.json | 21 ++++ flake.lock | 61 ++++++++++++ flake.nix | 51 ++++++++++ lib.cxx | 21 ++++ main.cxx | 8 ++ ...nstall-directory-variants-in-pkg-con.patch | 30 ++++++ nix/SDL3.nix | 95 +++++++++++++++++++ 8 files changed, 298 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 CMakePresets.json create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 lib.cxx create mode 100644 main.cxx create mode 100644 nix/0001-cmake-use-FULL-install-directory-variants-in-pkg-con.patch create mode 100644 nix/SDL3.nix diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..6d83bd2 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.28) +project(ModulesHelloWorld CXX) + +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_STANDARD 23) + +add_library(lib) +target_sources(lib PUBLIC FILE_SET CXX_MODULES FILES lib.cxx) + +add_executable(hellomodules main.cxx) +target_link_libraries(hellomodules lib) diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..caba2d1 --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,21 @@ +{ + "version": 6, + "cmakeMinimumRequired": { + "major": 3, + "minor": 28, + "patch": 0 + }, + "configurePresets": [ + { + "name": "linux-default", + "displayName": "Debug", + "description": "Sets Ninja generator, compilers, build and install directory, debug build type", + "generator": "Ninja", + "binaryDir": "${sourceDir}/out/build/${presetName}", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug", + "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}" + } + } + ] +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..243467a --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1726560853, + "narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1728492678, + "narHash": "sha256-9UTxR8eukdg+XZeHgxW5hQA9fIKHsKCdOIUycTryeVw=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "5633bcff0c6162b9e4b5f1264264611e950c8ec7", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "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..ae91676 --- /dev/null +++ b/flake.nix @@ -0,0 +1,51 @@ +{ + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { + self, + nixpkgs, + flake-utils, + }: + flake-utils.lib.eachDefaultSystem (system: let + pkgs = nixpkgs.legacyPackages.${system}; + inherit (pkgs.llvmPackages_19) stdenv; + + gooey = stdenv.mkDerivation { + pname = "gooey"; + version = "0.0.1"; + # inherit system; + + src = ./.; + buildInputs = [ + pkgs.cmake + pkgs.ninja + + (pkgs.callPackage ./nix/SDL3.nix {}) + pkgs.llvmPackages_19.clang-tools + ]; + + configurePhase = '' + cmake -GNinja . + ''; + + buildPhase = '' + ninja + ''; + + installPhase = '' + mkdir -p $out/bin + mv gooey $out/bin + ''; + }; + in { + packages.default = gooey; + devShells.default = pkgs.mkShell { + name = "gooey"; + inputsFrom = [gooey]; + CMAKE_GENERATOR = "Ninja"; + }; + }); +} diff --git a/lib.cxx b/lib.cxx new file mode 100644 index 0000000..79c9e58 --- /dev/null +++ b/lib.cxx @@ -0,0 +1,21 @@ +// Global module fragment, where #includes can happen +module; +#include + +export module lib; + +export class lib +{ +public: + lib(); + ~lib(); + void helloworld(); +}; + +lib::lib() = default; +lib::~lib() = default; + +void lib::helloworld() +{ + std::cout << "Hello world!" << std::endl; +} diff --git a/main.cxx b/main.cxx new file mode 100644 index 0000000..37731d1 --- /dev/null +++ b/main.cxx @@ -0,0 +1,8 @@ +import lib; + +int main() +{ + lib lib{}; + lib.helloworld(); + return 0; +} diff --git a/nix/0001-cmake-use-FULL-install-directory-variants-in-pkg-con.patch b/nix/0001-cmake-use-FULL-install-directory-variants-in-pkg-con.patch new file mode 100644 index 0000000..a1baaf8 --- /dev/null +++ b/nix/0001-cmake-use-FULL-install-directory-variants-in-pkg-con.patch @@ -0,0 +1,30 @@ +From 1999109df34e67ed7c34d2b6b42f59feafc22bf1 Mon Sep 17 00:00:00 2001 +From: seth +Date: Fri, 12 Jul 2024 21:59:14 -0400 +Subject: [PATCH] cmake: use `FULL` install directory variants in pkg-config + file + +Instead of manually prepending the install prefix to +`CMAKE_INSTALL_` (which may already be an absolute path), we can +let cmake do it for us +--- + cmake/sdl3.pc.in | 4 ++-- + 1 file changed, 2 insertions(+), 2 deletions(-) + +diff --git a/cmake/sdl3.pc.in b/cmake/sdl3.pc.in +index bfc2afffe..3724d6cf8 100644 +--- a/cmake/sdl3.pc.in ++++ b/cmake/sdl3.pc.in +@@ -1,7 +1,7 @@ + prefix=@SDL_PKGCONFIG_PREFIX@ + exec_prefix=${prefix} +-libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@ +-includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ ++libdir=@CMAKE_INSTALL_FULL_LIBDIR@ ++includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@ + + Name: sdl3 + Description: Simple DirectMedia Layer is a cross-platform multimedia library designed to provide low level access to audio, keyboard, mouse, joystick, 3D hardware via OpenGL, and 2D video framebuffer. +-- +2.45.2 + diff --git a/nix/SDL3.nix b/nix/SDL3.nix new file mode 100644 index 0000000..eafb252 --- /dev/null +++ b/nix/SDL3.nix @@ -0,0 +1,95 @@ +{ + lib, + stdenv, + fetchFromGitHub, + alsa-lib, + cmake, + darwin, + dbus, + fcitx5, + libdecor, + libdrm, + libjack2, + libpulseaudio, + libxkbcommon, + mesa, + nas, + ninja, + pipewire, + sndio, + systemdLibs, + testers, + validatePkgConfig, + wayland, + xorg, +}: +stdenv.mkDerivation (finalAttrs: { + pname = "sdl3"; + version = "3.1.2-unstable-2024-08-01"; + + src = fetchFromGitHub { + owner = "libsdl-org"; + repo = "SDL"; + rev = "f01d4278c66e065e64bc934cd01e4f2952f613d7"; + hash = "sha256-ok3ortmy5dm3zZdH+3tAvOIRDrDxDDXBjxBC3nGdHDM="; + }; + + patches = [ + # Case of https://github.com/NixOS/nixpkgs/issues/144170 + # Remove if/when https://github.com/libsdl-org/SDL/pull/10246 is merged + ./0001-cmake-use-FULL-install-directory-variants-in-pkg-con.patch + ]; + + outputs = [ + "lib" + "dev" + "out" + ]; + + nativeBuildInputs = [ + cmake + ninja + validatePkgConfig + ]; + + buildInputs = + lib.optionals stdenv.isLinux [ + alsa-lib + dbus + fcitx5 + libdecor + libdrm + libjack2 + libpulseaudio + mesa # libgbm + nas # libaudo + pipewire + sndio + systemdLibs # libudev + + # SDL_VIDEODRIVER=wayland + wayland + + # SDL_VIDEODRIVER=x11 + libxkbcommon + xorg.libX11 + xorg.libXcursor + xorg.libXext + xorg.libXfixes + xorg.libXi + xorg.libXrandr + ] + ++ lib.optionals stdenv.isDarwin (with darwin.apple_sdk.frameworks; [ Cocoa ]); + + passthru = { + tests.pkg-config = testers.hasPkgConfigModules { package = finalAttrs.finalPackage; }; + }; + + meta = { + description = "Cross-platform development library (Pre-release version)"; + homepage = "https://libsdl.org"; + license = lib.licenses.zlib; + maintainers = with lib.maintainers; [ getchoo ]; + pkgConfigModules = [ "sdl3" ]; + }; +})