Skip to content

Commit

Permalink
Several nix fixes (#1737)
Browse files Browse the repository at this point in the history
* Fixed nix c3c development shell

* Fix unknown git hash on nix build
  • Loading branch information
vssukharev authored Dec 29, 2024
1 parent 1340a47 commit 7b734df
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
11 changes: 8 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
51 changes: 23 additions & 28 deletions nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 ../.;
};
removeSuffix "\"" ( removePrefix "\"" ( elemAt ( splitString " " foundLine ) 2 ) );

src = ../.;

cmakeBuildType = if debug then "Debug" else "Release";

postPatch = ''
substituteInPlace CMakeLists.txt \
--replace-fail "\''${LLVM_LIBRARY_DIRS}" "${llvmPackages.lld.lib}/lib ${llvmPackages.llvm.lib}/lib"
substituteInPlace git_hash.cmake \
--replace-fail "\''${GIT_HASH}" "${rev}"
'';

cmakeBuildType = if debug then "Debug" else "Release";

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
Expand All @@ -70,13 +63,15 @@ llvmPackages.stdenv.mkDerivation (finalAttrs: {
runHook postCheck
'';


meta = with lib; {
description = "Compiler for the C3 language";
homepage = "https://github.com/c3lang/c3c";
license = licenses.lgpl3Only;
maintainers = with maintainers; [
luc65r
anas
vssukharev
];
platforms = platforms.all;
mainProgram = "c3c";
Expand Down
14 changes: 8 additions & 6 deletions nix/shell.nix
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 7b734df

Please sign in to comment.