diff --git a/flake.nix b/flake.nix index 5b0fc1e8f..cd78b900a 100644 --- a/flake.nix +++ b/flake.nix @@ -8,12 +8,17 @@ outputs = { self, ... } @ inputs: inputs.flake-utils.lib.eachDefaultSystem (system: - let pkgs = import inputs.nixpkgs { inherit system; }; in - { + let pkgs = import inputs.nixpkgs { inherit system; }; + call = set: pkgs.callPackage ./nix/default.nix ( + set // { + rev = self.rev or "unknown"; + } + ); + in { packages = { default = self.packages.${system}.c3c; - c3c = pkgs.callPackage ./nix/default.nix {}; + c3c = call {}; c3c-checks = pkgs.callPackage ./nix/default.nix { checks = true; diff --git a/nix/default.nix b/nix/default.nix index 7db455810..df62a9fcc 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -7,53 +7,46 @@ libxml2, libffi, xar, + rev ? "unknown", debug ? false, checks ? false, }: let - inherit (builtins) baseNameOf toString readFile elemAt; - inherit (lib.sources) cleanSourceWith cleanSource; + inherit (builtins) readFile elemAt; + # inherit (lib.sources) cleanSourceWith cleanSource; inherit (lib.lists) findFirst; inherit (lib.asserts) assertMsg; - inherit (lib.strings) hasInfix hasSuffix splitString removeSuffix removePrefix optionalString; -in -llvmPackages.stdenv.mkDerivation (finalAttrs: { + inherit (lib.strings) hasInfix splitString removeSuffix removePrefix optionalString; +in llvmPackages.stdenv.mkDerivation (finalAttrs: { + pname = "c3c${optionalString debug "-debug"}"; + version = let - findLine = findFirst (x: hasInfix "COMPILER_VERSION" x) "none"; - foundLine = findLine ( splitString "\n" ( readFile ../src/version.h ) ); - version = removeSuffix "\"" ( removePrefix "\"" ( elemAt ( splitString " " foundLine ) 2 ) ); + foundLine = findFirst (x: hasInfix "COMPILER_VERSION" x) "none" ( splitString "\n" ( readFile ../src/version.h ) ); in assert assertMsg (foundLine != "none") "No COMPILER_VERSION substring was found in version.h"; - version; - - src = cleanSourceWith { - filter = _path: _type: !(hasSuffix ".nix" (baseNameOf(toString _path))); - src = cleanSource ../.; - }; - - postPatch = '' - substituteInPlace CMakeLists.txt \ - --replace-fail "\''${LLVM_LIBRARY_DIRS}" "${llvmPackages.lld.lib}/lib ${llvmPackages.llvm.lib}/lib" - ''; + removeSuffix "\"" ( removePrefix "\"" ( elemAt ( splitString " " foundLine ) 2 ) ); + src = ../.; + cmakeBuildType = if debug then "Debug" else "Release"; + + postPatch = '' + substituteInPlace git_hash.cmake \ + --replace-fail "\''${GIT_HASH}" "${rev}" + ''; cmakeFlags = [ "-DC3_ENABLE_CLANGD_LSP=${if debug then "ON" else "OFF"}" + "-DC3_LLD_DIR=${llvmPackages.lld.lib}/lib" ]; - nativeBuildInputs = [ cmake ]; - - postBuild = optionalString debug '' - mkdir $out - substituteInPlace compile_commands.json \ - --replace "/build/source/" "$src/" - cp compile_commands.json $out/compile_commands.json - ''; + nativeBuildInputs = [ + cmake + llvmPackages.llvm + llvmPackages.lld + ]; buildInputs = [ - llvmPackages.llvm - llvmPackages.lld curl libxml2 libffi @@ -70,6 +63,7 @@ llvmPackages.stdenv.mkDerivation (finalAttrs: { runHook postCheck ''; + meta = with lib; { description = "Compiler for the C3 language"; homepage = "https://github.com/c3lang/c3c"; @@ -77,6 +71,7 @@ llvmPackages.stdenv.mkDerivation (finalAttrs: { maintainers = with maintainers; [ luc65r anas + vssukharev ]; platforms = platforms.all; mainProgram = "c3c"; diff --git a/nix/shell.nix b/nix/shell.nix index 63f3695d3..830aea76c 100644 --- a/nix/shell.nix +++ b/nix/shell.nix @@ -1,20 +1,22 @@ { + lib, mkShell, clang-tools, c3c, -}: +}: +mkShell.override { + inherit (c3c) stdenv; +} { + name = "c3c-shell"; -mkShell { inputsFrom = [ c3c ]; packages = [ clang-tools - c3c ]; - shellHook = '' - ln -sf ${c3c}/compile_commands.json compile_commands.json - ''; + # Usage: 'cmake . -Bbuild $C3_CMAKE_FLAGS' or 'cmake . -Bbuild $=C3_CMAKE_FLAGS' on zsh + C3_CMAKE_FLAGS = lib.concatStringsSep " " c3c.cmakeFlags; }