From c3bf8b0ae8819d7b080b4add03f5440c39bbf409 Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Sat, 23 Sep 2023 15:50:29 +0530 Subject: [PATCH] nix: add support to build with GCC Signed-off-by: Amneesh Singh --- .envrc | 2 +- .github/workflows/clang.yml | 36 +++++++++++ .github/workflows/{main.yml => gcc.yml} | 14 ++-- apps/target/meson.build | 2 +- flake.lock | 45 +++++++++++-- flake.nix | 85 +++++++------------------ nix/build.nix | 23 +++++++ nix/default.nix | 11 ++++ nix/matar-clang.nix | 25 ++++++++ nix/matar.nix | 13 ++++ nix/shell.nix | 20 ++++++ src/meson.build | 11 +++- tests/cpu/arm/exec.cc | 5 +- tests/meson.build | 2 +- 14 files changed, 211 insertions(+), 83 deletions(-) create mode 100644 .github/workflows/clang.yml rename .github/workflows/{main.yml => gcc.yml} (62%) create mode 100644 nix/build.nix create mode 100644 nix/default.nix create mode 100644 nix/matar-clang.nix create mode 100644 nix/matar.nix create mode 100644 nix/shell.nix diff --git a/.envrc b/.envrc index 3550a30..1dfcd40 100644 --- a/.envrc +++ b/.envrc @@ -1 +1 @@ -use flake +use flake .#matar-clang diff --git a/.github/workflows/clang.yml b/.github/workflows/clang.yml new file mode 100644 index 0000000..aa3f797 --- /dev/null +++ b/.github/workflows/clang.yml @@ -0,0 +1,36 @@ +name: matar-clang +on: [push, pull_request, workflow_dispatch] + +env: + BUILDDIR: build + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: cachix/install-nix-action@v20 + with: + extra_nix_config: | + auto-optimise-store = true + experimental-features = nix-command flakes + - uses: cachix/cachix-action@v12 + with: + name: pain + authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}' + + + - name: setup + run: nix develop .#matar-clang -c meson setup $BUILDDIR + + - name: fmt + run: nix develop .#matar-clang -c ninja clang-format-check -C $BUILDDIR + + - name: lint + run: nix develop .#matar-clang -c ninja clang-tidy -C $BUILDDIR + + - name: tests + run: nix develop .#matar-clang -c ninja test -C $BUILDDIR + + - name: build + run: nix develop .#matar-clang -c ninja -C $BUILDDIR diff --git a/.github/workflows/main.yml b/.github/workflows/gcc.yml similarity index 62% rename from .github/workflows/main.yml rename to .github/workflows/gcc.yml index d6b0f1a..55a0248 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/gcc.yml @@ -1,4 +1,4 @@ -name: matar +name: matar-gcc on: [push, pull_request, workflow_dispatch] env: @@ -21,16 +21,10 @@ jobs: - name: setup - run: nix develop -c meson setup $BUILDDIR - - - name: fmt - run: nix develop -c ninja clang-format-check -C $BUILDDIR - - - name: lint - run: nix develop -c ninja clang-tidy -C $BUILDDIR + run: nix develop .#matar -c meson setup $BUILDDIR - name: tests - run: nix develop -c ninja test -C $BUILDDIR + run: nix develop .#matar -c ninja test -C $BUILDDIR - name: build - run: nix develop -c ninja -C $BUILDDIR + run: nix develop .#matar -c ninja -C $BUILDDIR diff --git a/apps/target/meson.build b/apps/target/meson.build index 75f34ae..f662ede 100644 --- a/apps/target/meson.build +++ b/apps/target/meson.build @@ -7,7 +7,7 @@ target_sources = files( ) executable( - meson.project_name(), + 'matar', target_sources, link_with: target_deps, include_directories: inc, diff --git a/flake.lock b/flake.lock index 31ab479..28f524c 100644 --- a/flake.lock +++ b/flake.lock @@ -1,23 +1,60 @@ { "nodes": { + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1693611461, + "narHash": "sha256-aPODl8vAgGQ0ZYFIRisxYG5MOGSkIczvu2Cd8Gb9+1Y=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "7f53fdb7bdc5bb237da7fefef12d099e4fd611ca", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, "nixpkgs": { "locked": { - "lastModified": 1694911158, - "narHash": "sha256-5WENkcO8O5SuA5pozpVppLGByWfHVv/1wOWgB2+TfV4=", + "lastModified": 1695318763, + "narHash": "sha256-FHVPDRP2AfvsxAdc+AsgFJevMz5VBmnZglFUMlxBkcY=", "owner": "nixos", "repo": "nixpkgs", - "rev": "46423a1a750594236673c1d741def4e93cf5a8f7", + "rev": "e12483116b3b51a185a33a272bf351e357ba9a99", "type": "github" }, "original": { "owner": "nixos", - "ref": "master", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "dir": "lib", + "lastModified": 1693471703, + "narHash": "sha256-0l03ZBL8P1P6z8MaSDS/MvuU8E75rVxe5eE1N6gxeTo=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "3e52e76b70d5508f3cec70b882a29199f4d1ee85", + "type": "github" + }, + "original": { + "dir": "lib", + "owner": "NixOS", + "ref": "nixos-unstable", "repo": "nixpkgs", "type": "github" } }, "root": { "inputs": { + "flake-parts": "flake-parts", "nixpkgs": "nixpkgs" } } diff --git a/flake.nix b/flake.nix index 9aca1f9..f0fe6af 100644 --- a/flake.nix +++ b/flake.nix @@ -2,80 +2,39 @@ description = "matar"; inputs = { - nixpkgs.url = github:nixos/nixpkgs/master; + nixpkgs.url = github:nixos/nixpkgs/nixpkgs-unstable; + flake-parts.url = github:hercules-ci/flake-parts; }; - outputs = { self, nixpkgs }: - let + outputs = inputs@{ self, nixpkgs, flake-parts }: + flake-parts.lib.mkFlake { inherit inputs; } { + systems = [ "x86_64-linux" "aarch64-linux" ]; - eachSystem = with nixpkgs.lib; f: foldAttrs mergeAttrs { } - (map (s: mapAttrs (_: v: { ${s} = v; }) (f s)) systems); - in - eachSystem (system: - let - pkgs = import nixpkgs { inherit system; }; + imports = [ + ./nix + ]; - # aliases - llvm = pkgs.llvmPackages_16; - stdenv = llvm.libcxxStdenv; + perSystem = { self', system, ... }: + let + pkgs = import nixpkgs { inherit system; }; - - # TODO: this is ugly - #dependencies - nativeBuildInputs = with pkgs; - [ - meson - ninja - - # libraries - pkg-config - cmake - - ((pkgs.fmt.override { - inherit stdenv; - enableShared = false; - }).overrideAttrs (oa: { - cmakeFlags = oa.cmakeFlags ++ [ "-DFMT_TEST=off" ]; - })).dev - (catch2_3.override { inherit stdenv; }).out + src = pkgs.lib.sourceFilesBySuffices ./. [ + ".hh" + ".cc" + ".build" + "meson_options.txt" ]; - in - rec { - packages = rec { - inherit (llvm) libcxxabi; - matar = stdenv.mkDerivation rec { - name = "matar"; - version = "0.1"; - src = pkgs.lib.sourceFilesBySuffices ./. [ - ".hh" - ".cc" - ".build" - "meson_options.txt" - ]; - outputs = [ "out" "dev" ]; - - inherit nativeBuildInputs; - - enableParallelBuilding = true; + in + rec { + _module.args = { + inherit src pkgs; }; - default = matar; - }; - devShells = rec { - matar = pkgs.mkShell.override { inherit stdenv; } { - name = "matar"; - packages = nativeBuildInputs ++ (with pkgs; [ - # lsp - clang-tools_16 - ]); - }; - default = matar; + formatter = pkgs.nixpkgs-fmt; }; - - formatter = pkgs.nixpkgs-fmt; - }); + }; } diff --git a/nix/build.nix b/nix/build.nix new file mode 100644 index 0000000..aff1751 --- /dev/null +++ b/nix/build.nix @@ -0,0 +1,23 @@ +{ stdenv +, meson +, ninja +, pkg-config +, src ? "../." +, libraries ? [ ] +}: + +stdenv.mkDerivation { + name = "matar"; + version = "0.1"; + inherit src; + + outputs = [ "out" "dev" ]; + + nativeBuildInputs = [ + meson + ninja + pkg-config + ] ++ libraries; + + enableParallelBuilding = true; +} diff --git a/nix/default.nix b/nix/default.nix new file mode 100644 index 0000000..0fa60e8 --- /dev/null +++ b/nix/default.nix @@ -0,0 +1,11 @@ +{ ... }: { + imports = [ + ./matar.nix + ./matar-clang.nix + ]; + + perSystem = { self', pkgs, ... }: { + packages.default = self'.packages.matar-clang; + devShells.default = self'.devShells.matar-clang; + }; +} diff --git a/nix/matar-clang.nix b/nix/matar-clang.nix new file mode 100644 index 0000000..5151443 --- /dev/null +++ b/nix/matar-clang.nix @@ -0,0 +1,25 @@ +{ ... }: { + perSystem = { pkgs, src, ... }: + let + llvm = pkgs.llvmPackages_16; + stdenv = llvm.libcxxStdenv; + + libraries = with pkgs; [ + ((pkgs.fmt.override { + inherit stdenv; + enableShared = false; + }).overrideAttrs (oa: { + cmakeFlags = oa.cmakeFlags ++ [ "-DFMT_TEST=off" ]; + })).dev + + (catch2_3.override { inherit stdenv; }).out + ]; + in + { + packages.matar-clang = pkgs.callPackage ./build.nix { inherit src libraries stdenv; }; + devShells.matar-clang = pkgs.callPackage ./shell.nix { + inherit libraries stdenv; + tools = with pkgs; [ clang-tools_16 ]; + }; + }; +} diff --git a/nix/matar.nix b/nix/matar.nix new file mode 100644 index 0000000..552720a --- /dev/null +++ b/nix/matar.nix @@ -0,0 +1,13 @@ +{ ... }: { + perSystem = { pkgs, src, ... }: + let + libraries = with pkgs; [ + (pkgs.fmt.override { enableShared = false; }).dev + catch2_3.out + ]; + in + { + packages.matar = pkgs.callPackage ./build.nix { inherit src libraries; }; + devShells.matar = pkgs.callPackage ./shell.nix { inherit libraries; }; + }; +} diff --git a/nix/shell.nix b/nix/shell.nix new file mode 100644 index 0000000..f94e082 --- /dev/null +++ b/nix/shell.nix @@ -0,0 +1,20 @@ +{ stdenv +, mkShell +, meson +, ninja +, pkg-config +, libraries ? [ ] +, tools ? [ ] +}: + +mkShell.override { inherit stdenv; } { + name = "matar"; + + packages = [ + meson + ninja + pkg-config + ] ++ libraries ++ tools; + + enableParallelBuilding = true; +} diff --git a/src/meson.build b/src/meson.build index 75e933c..81b23d2 100644 --- a/src/meson.build +++ b/src/meson.build @@ -5,13 +5,22 @@ lib_sources = files( subdir('cpu') -fmt = dependency('fmt', version : '>=10.1.0') + +lib_cpp_args = [ ] + +fmt = dependency('fmt', version : '>=10.1.0', static: true) +if not fmt.found() + fmt = dependency('fmt', version : '>=10.1.0', static: false) + lib_cpp_args += 'DFMT_HEADER_ONLY' +endif + lib = library( meson.project_name(), lib_sources, dependencies: [fmt], include_directories: inc, install: true, + cpp_args: lib_cpp_args ) import('pkgconfig').generate(lib) diff --git a/tests/cpu/arm/exec.cc b/tests/cpu/arm/exec.cc index 68dd5cb..1cf76d7 100644 --- a/tests/cpu/arm/exec.cc +++ b/tests/cpu/arm/exec.cc @@ -13,6 +13,7 @@ class CpuFixture { std::vector(Header::HEADER_SIZE)))) {} protected: + // TODO: test with other conditions void exec(arm::InstructionData data, Condition condition = Condition::AL) { arm::Instruction instruction(condition, data); cpu.exec_arm(instruction); @@ -82,7 +83,7 @@ TEST_CASE_METHOD(CpuFixture, "Multiply", TAG) { // with accumulate { - uint32_t result = 234912349ull * 124897ull + 99999ull & 0xFFFFFFFF; + uint32_t result = (234912349ull * 124897ull + 99999ull) & 0xFFFFFFFF; multiply->acc = true; exec(data); @@ -91,7 +92,7 @@ TEST_CASE_METHOD(CpuFixture, "Multiply", TAG) { // with set { - uint32_t result = 234912349ull * 124897ull + 99999ull & 0xFFFFFFFF; + uint32_t result = (234912349ull * 124897ull + 99999ull) & 0xFFFFFFFF; multiply->set = true; exec(data); diff --git a/tests/meson.build b/tests/meson.build index 49052a1..35ded4d 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -10,7 +10,7 @@ subdir('cpu') catch2 = dependency('catch2-with-main', version: '>=3.4.0', static: true) catch2_tests = executable( - meson.project_name() + '_tests', + 'matar_tests', tests_sources, dependencies: catch2, link_with: tests_deps,