feat: initialize modules setup w/ Nix

This commit is contained in:
Matias 2024-10-11 15:06:50 +01:00
commit a42efe2c82
No known key found for this signature in database
GPG key ID: ED35A6AC65A06B69
8 changed files with 298 additions and 0 deletions

11
CMakeLists.txt Normal file
View file

@ -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)

21
CMakePresets.json Normal file
View file

@ -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}"
}
}
]
}

61
flake.lock Normal file
View file

@ -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
}

51
flake.nix Normal file
View file

@ -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";
};
});
}

21
lib.cxx Normal file
View file

@ -0,0 +1,21 @@
// Global module fragment, where #includes can happen
module;
#include <iostream>
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;
}

8
main.cxx Normal file
View file

@ -0,0 +1,8 @@
import lib;
int main()
{
lib lib{};
lib.helloworld();
return 0;
}

View file

@ -0,0 +1,30 @@
From 1999109df34e67ed7c34d2b6b42f59feafc22bf1 Mon Sep 17 00:00:00 2001
From: seth <getchoo@tuta.io>
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_<dir>` (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

95
nix/SDL3.nix Normal file
View file

@ -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" ];
};
})