forgejo-lts: 7.0.10 -> 7.0.11
[NixPkgs.git] / pkgs / by-name / ha / hare / package.nix
blob763d8638fd585672b77316307b301e180d5d46b5
2   lib,
3   stdenv,
4   fetchFromSourcehut,
5   harec,
6   scdoc,
7   tzdata,
8   mailcap,
9   substituteAll,
10   callPackage,
11   enableCrossCompilation ? (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.is64bit),
12   pkgsCross,
13   x86_64PkgsCrossToolchain ? pkgsCross.gnu64,
14   aarch64PkgsCrossToolchain ? pkgsCross.aarch64-multiplatform,
15   riscv64PkgsCrossToolchain ? pkgsCross.riscv64,
18 # There's no support for `aarch64` or `riscv64` for freebsd nor for openbsd on nix.
19 # See `lib.systems.doubles.aarch64` and `lib.systems.doubles.riscv64`.
20 assert
21   let
22     inherit (stdenv.hostPlatform) isLinux is64bit;
23     inherit (lib) intersectLists platforms concatStringsSep;
24     workingPlatforms = intersectLists platforms.linux (with platforms; x86_64 ++ aarch64 ++ riscv64);
25   in
26   (enableCrossCompilation -> !(isLinux && is64bit))
27   -> builtins.throw ''
28     The cross-compilation toolchains may only be enabled on the following platforms:
29     ${concatStringsSep "\n" workingPlatforms}
30   '';
32 let
33   inherit (harec) qbe;
34   buildArch = stdenv.buildPlatform.uname.processor;
35   arch = stdenv.hostPlatform.uname.processor;
36   platform = lib.toLower stdenv.hostPlatform.uname.system;
37   qbePlatform =
38     {
39       x86_64 = "amd64_sysv";
40       aarch64 = "arm64";
41       riscv64 = "rv64";
42     }
43     .${arch};
44   embeddedOnBinaryTools =
45     let
46       genPaths =
47         toolchain:
48         let
49           inherit (toolchain.stdenv.cc) targetPrefix;
50           inherit (toolchain.stdenv.targetPlatform.uname) processor;
51         in
52         {
53           "${processor}" = {
54             "ld" = lib.getExe' toolchain.buildPackages.binutils "${targetPrefix}ld";
55             "as" = lib.getExe' toolchain.buildPackages.binutils "${targetPrefix}as";
56             "cc" = lib.getExe' toolchain.stdenv.cc "${targetPrefix}cc";
57           };
58         };
59     in
60     builtins.foldl' (acc: elem: acc // (genPaths elem)) { } [
61       x86_64PkgsCrossToolchain
62       aarch64PkgsCrossToolchain
63       riscv64PkgsCrossToolchain
64     ];
65   crossCompMakeFlags = builtins.filter (x: !(lib.hasPrefix (lib.toUpper buildArch) x)) [
66     "RISCV64_AS=${embeddedOnBinaryTools.riscv64.as}"
67     "RISCV64_CC=${embeddedOnBinaryTools.riscv64.cc}"
68     "RISCV64_LD=${embeddedOnBinaryTools.riscv64.ld}"
69     "AARCH64_AS=${embeddedOnBinaryTools.aarch64.as}"
70     "AARCH64_CC=${embeddedOnBinaryTools.aarch64.cc}"
71     "AARCH64_LD=${embeddedOnBinaryTools.aarch64.ld}"
72     "X86_64_AS=${embeddedOnBinaryTools.x86_64.as}"
73     "X86_64_CC=${embeddedOnBinaryTools.x86_64.cc}"
74     "X86_64_LD=${embeddedOnBinaryTools.x86_64.ld}"
75   ];
77 stdenv.mkDerivation (finalAttrs: {
78   pname = "hare";
79   version = "0.24.2";
81   outputs = [
82     "out"
83     "man"
84   ];
86   src = fetchFromSourcehut {
87     owner = "~sircmpwn";
88     repo = "hare";
89     rev = finalAttrs.version;
90     hash = "sha256-61lckI0F+Ez5LR/8g6ftS0W7Q/+EU/1flTDFleBg6pc=";
91   };
93   patches = [
94     # Replace FHS paths with nix store
95     (substituteAll {
96       src = ./001-tzdata.patch;
97       inherit tzdata;
98     })
99     # Don't build haredoc since it uses the build `hare` bin, which breaks
100     # cross-compilation.
101     ./002-dont-build-haredoc.patch
102     # Hardcode harec and qbe.
103     (substituteAll {
104       src = ./003-hardcode-qbe-and-harec.patch;
105       harec_bin = lib.getExe harec;
106       qbe_bin = lib.getExe qbe;
107     })
108     # Use mailcap `/etc/mime.types` for Hare's mime module
109     (substituteAll {
110       src = ./004-use-mailcap-for-mimetypes.patch;
111       inherit mailcap;
112     })
113   ];
115   nativeBuildInputs = [
116     harec
117     qbe
118     scdoc
119   ];
121   buildInputs = [
122     harec
123     qbe
124   ];
126   makeFlags = [
127     "HARECACHE=.harecache"
128     "PREFIX=${builtins.placeholder "out"}"
129     "ARCH=${arch}"
130     "VERSION=${finalAttrs.version}-nixpkgs"
131     "QBEFLAGS=-t${qbePlatform}"
132     "AS=${stdenv.cc.targetPrefix}as"
133     "LD=${stdenv.cc.targetPrefix}ld"
134     "${lib.toUpper buildArch}_AS=${embeddedOnBinaryTools.${buildArch}.as}"
135     "${lib.toUpper buildArch}_CC=${embeddedOnBinaryTools.${buildArch}.cc}"
136     "${lib.toUpper buildArch}_LD=${embeddedOnBinaryTools.${buildArch}.ld}"
137     # Strip the variable of an empty $(SRCDIR)/hare/third-party, since nix does
138     # not follow the FHS.
139     "HAREPATH=$(SRCDIR)/hare/stdlib"
140   ] ++ lib.optionals enableCrossCompilation crossCompMakeFlags;
142   enableParallelBuilding = true;
144   # Append the distribution name to the version
145   env.LOCALVER = "nixpkgs";
147   strictDeps = true;
149   doCheck = true;
151   postConfigure = ''
152     ln -s configs/${platform}.mk config.mk
153   '';
155   passthru = {
156     tests =
157       lib.optionalAttrs enableCrossCompilation {
158         crossCompilation = callPackage ./cross-compilation-tests.nix { hare = finalAttrs.finalPackage; };
159       }
160       // lib.optionalAttrs (stdenv.buildPlatform.canExecute stdenv.hostPlatform) {
161         mimeModule = callPackage ./mime-module-test.nix { hare = finalAttrs.finalPackage; };
162       }
163       //
164         lib.optionalAttrs (enableCrossCompilation && stdenv.buildPlatform.canExecute stdenv.hostPlatform)
165           {
166             crossCompilation = callPackage ./cross-compilation-tests.nix { hare = finalAttrs.finalPackage; };
167           };
168     # To be propagated by `hareHook`.
169     inherit harec qbe;
170   };
172   meta = {
173     homepage = "https://harelang.org/";
174     description = "Systems programming language designed to be simple, stable, and robust";
175     license = lib.licenses.gpl3Only;
176     maintainers = with lib.maintainers; [ onemoresuza ];
177     mainProgram = "hare";
178     inherit (harec.meta) platforms badPlatforms;
179   };