nix: add support to build with GCC

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2023-09-23 15:50:29 +05:30
parent 6c33c77ef3
commit c3bf8b0ae8
14 changed files with 211 additions and 83 deletions

2
.envrc
View File

@@ -1 +1 @@
use flake use flake .#matar-clang

36
.github/workflows/clang.yml vendored Normal file
View File

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

View File

@@ -1,4 +1,4 @@
name: matar name: matar-gcc
on: [push, pull_request, workflow_dispatch] on: [push, pull_request, workflow_dispatch]
env: env:
@@ -21,16 +21,10 @@ jobs:
- name: setup - name: setup
run: nix develop -c meson setup $BUILDDIR run: nix develop .#matar -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
- name: tests - name: tests
run: nix develop -c ninja test -C $BUILDDIR run: nix develop .#matar -c ninja test -C $BUILDDIR
- name: build - name: build
run: nix develop -c ninja -C $BUILDDIR run: nix develop .#matar -c ninja -C $BUILDDIR

View File

@@ -7,7 +7,7 @@ target_sources = files(
) )
executable( executable(
meson.project_name(), 'matar',
target_sources, target_sources,
link_with: target_deps, link_with: target_deps,
include_directories: inc, include_directories: inc,

45
flake.lock generated
View File

@@ -1,23 +1,60 @@
{ {
"nodes": { "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": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1694911158, "lastModified": 1695318763,
"narHash": "sha256-5WENkcO8O5SuA5pozpVppLGByWfHVv/1wOWgB2+TfV4=", "narHash": "sha256-FHVPDRP2AfvsxAdc+AsgFJevMz5VBmnZglFUMlxBkcY=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "46423a1a750594236673c1d741def4e93cf5a8f7", "rev": "e12483116b3b51a185a33a272bf351e357ba9a99",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "nixos", "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", "repo": "nixpkgs",
"type": "github" "type": "github"
} }
}, },
"root": { "root": {
"inputs": { "inputs": {
"flake-parts": "flake-parts",
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs"
} }
} }

View File

@@ -2,80 +2,39 @@
description = "matar"; description = "matar";
inputs = { 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 }: outputs = inputs@{ self, nixpkgs, flake-parts }:
let flake-parts.lib.mkFlake { inherit inputs; } {
systems = [ systems = [
"x86_64-linux" "x86_64-linux"
"aarch64-linux" "aarch64-linux"
]; ];
eachSystem = with nixpkgs.lib; f: foldAttrs mergeAttrs { } imports = [
(map (s: mapAttrs (_: v: { ${s} = v; }) (f s)) systems); ./nix
in ];
eachSystem (system:
let
pkgs = import nixpkgs { inherit system; };
# aliases perSystem = { self', system, ... }:
llvm = pkgs.llvmPackages_16; let
stdenv = llvm.libcxxStdenv; pkgs = import nixpkgs { inherit system; };
src = pkgs.lib.sourceFilesBySuffices ./. [
# TODO: this is ugly ".hh"
#dependencies ".cc"
nativeBuildInputs = with pkgs; ".build"
[ "meson_options.txt"
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
]; ];
in in
rec { rec {
packages = rec { _module.args = {
inherit (llvm) libcxxabi; inherit src pkgs;
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;
}; };
default = matar;
};
devShells = rec { formatter = pkgs.nixpkgs-fmt;
matar = pkgs.mkShell.override { inherit stdenv; } {
name = "matar";
packages = nativeBuildInputs ++ (with pkgs; [
# lsp
clang-tools_16
]);
};
default = matar;
}; };
};
formatter = pkgs.nixpkgs-fmt;
});
} }

23
nix/build.nix Normal file
View File

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

11
nix/default.nix Normal file
View File

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

25
nix/matar-clang.nix Normal file
View File

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

13
nix/matar.nix Normal file
View File

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

20
nix/shell.nix Normal file
View File

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

View File

@@ -5,13 +5,22 @@ lib_sources = files(
subdir('cpu') 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( lib = library(
meson.project_name(), meson.project_name(),
lib_sources, lib_sources,
dependencies: [fmt], dependencies: [fmt],
include_directories: inc, include_directories: inc,
install: true, install: true,
cpp_args: lib_cpp_args
) )
import('pkgconfig').generate(lib) import('pkgconfig').generate(lib)

View File

@@ -13,6 +13,7 @@ class CpuFixture {
std::vector<uint8_t>(Header::HEADER_SIZE)))) {} std::vector<uint8_t>(Header::HEADER_SIZE)))) {}
protected: protected:
// TODO: test with other conditions
void exec(arm::InstructionData data, Condition condition = Condition::AL) { void exec(arm::InstructionData data, Condition condition = Condition::AL) {
arm::Instruction instruction(condition, data); arm::Instruction instruction(condition, data);
cpu.exec_arm(instruction); cpu.exec_arm(instruction);
@@ -82,7 +83,7 @@ TEST_CASE_METHOD(CpuFixture, "Multiply", TAG) {
// with accumulate // with accumulate
{ {
uint32_t result = 234912349ull * 124897ull + 99999ull & 0xFFFFFFFF; uint32_t result = (234912349ull * 124897ull + 99999ull) & 0xFFFFFFFF;
multiply->acc = true; multiply->acc = true;
exec(data); exec(data);
@@ -91,7 +92,7 @@ TEST_CASE_METHOD(CpuFixture, "Multiply", TAG) {
// with set // with set
{ {
uint32_t result = 234912349ull * 124897ull + 99999ull & 0xFFFFFFFF; uint32_t result = (234912349ull * 124897ull + 99999ull) & 0xFFFFFFFF;
multiply->set = true; multiply->set = true;
exec(data); exec(data);

View File

@@ -10,7 +10,7 @@ subdir('cpu')
catch2 = dependency('catch2-with-main', version: '>=3.4.0', static: true) catch2 = dependency('catch2-with-main', version: '>=3.4.0', static: true)
catch2_tests = executable( catch2_tests = executable(
meson.project_name() + '_tests', 'matar_tests',
tests_sources, tests_sources,
dependencies: catch2, dependencies: catch2,
link_with: tests_deps, link_with: tests_deps,