about summary refs log tree commit diff
path: root/pkgs/by-name/ha/hare/package.nix
blob: 3cf37e33109b1e2dcc22102c3596c111bdbee3ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
{
  lib,
  stdenv,
  fetchFromSourcehut,
  harec,
  qbe,
  gitUpdater,
  scdoc,
  tzdata,
  substituteAll,
  fetchpatch,
  callPackage,
  enableCrossCompilation ? (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.is64bit),
  pkgsCross,
  x86_64PkgsCrossToolchain ? pkgsCross.gnu64,
  aarch64PkgsCrossToolchain ? pkgsCross.aarch64-multiplatform,
  riscv64PkgsCrossToolchain ? pkgsCross.riscv64,
}:

# There's no support for `aarch64` or `riscv64` for freebsd nor for openbsd on nix.
# See `lib.systems.doubles.aarch64` and `lib.systems.doubles.riscv64`.
assert
  let
    inherit (stdenv.hostPlatform) isLinux is64bit;
    inherit (lib) intersectLists platforms concatStringsSep;
    workingPlatforms = intersectLists platforms.linux (with platforms; x86_64 ++ aarch64 ++ riscv64);
  in
  (enableCrossCompilation -> !(isLinux && is64bit))
  -> builtins.throw ''
    The cross-compilation toolchains may only be enabled on the following platforms:
    ${concatStringsSep "\n" workingPlatforms}
  '';

let
  buildArch = stdenv.buildPlatform.uname.processor;
  arch = stdenv.hostPlatform.uname.processor;
  platform = lib.toLower stdenv.hostPlatform.uname.system;
  qbePlatform =
    {
      x86_64 = "amd64_sysv";
      aarch64 = "arm64";
      riscv64 = "rv64";
    }
    .${arch};
  embeddedOnBinaryTools =
    let
      genPaths =
        toolchain:
        let
          inherit (toolchain.stdenv.cc) targetPrefix;
          inherit (toolchain.stdenv.targetPlatform.uname) processor;
        in
        {
          "${processor}" = {
            "ld" = lib.getExe' toolchain.buildPackages.binutils "${targetPrefix}ld";
            "as" = lib.getExe' toolchain.buildPackages.binutils "${targetPrefix}as";
            "cc" = lib.getExe' toolchain.stdenv.cc "${targetPrefix}cc";
          };
        };
    in
    builtins.foldl' (acc: elem: acc // (genPaths elem)) { } [
      x86_64PkgsCrossToolchain
      aarch64PkgsCrossToolchain
      riscv64PkgsCrossToolchain
    ];
  crossCompMakeFlags = builtins.filter (x: !(lib.hasPrefix (lib.toUpper buildArch) x)) [
    "RISCV64_AS=${embeddedOnBinaryTools.riscv64.as}"
    "RISCV64_CC=${embeddedOnBinaryTools.riscv64.cc}"
    "RISCV64_LD=${embeddedOnBinaryTools.riscv64.ld}"
    "AARCH64_AS=${embeddedOnBinaryTools.aarch64.as}"
    "AARCH64_CC=${embeddedOnBinaryTools.aarch64.cc}"
    "AARCH64_LD=${embeddedOnBinaryTools.aarch64.ld}"
    "X86_64_AS=${embeddedOnBinaryTools.x86_64.as}"
    "X86_64_CC=${embeddedOnBinaryTools.x86_64.cc}"
    "X86_64_LD=${embeddedOnBinaryTools.x86_64.ld}"
  ];
in
stdenv.mkDerivation (finalAttrs: {
  pname = "hare";
  version = "0.24.0";

  outputs = [
    "out"
    "man"
  ];

  src = fetchFromSourcehut {
    owner = "~sircmpwn";
    repo = "hare";
    rev = finalAttrs.version;
    hash = "sha256-3T+BdNj+Th8QXrcsPMWlN9GBfuMF1ulneWHpDEtyBU8=";
  };

  patches = [
    # Replace FHS paths with nix store
    (substituteAll {
      src = ./001-tzdata.patch;
      inherit tzdata;
    })
    # Use correct comment syntax for debug+riscv64.
    (fetchpatch {
      url = "https://git.sr.ht/~sircmpwn/hare/commit/80e45e4d931a6e90d999846b86471cac00d2a6d5.patch";
      hash = "sha256-S7nXpiO0tYnKpmpj+fLkolGeHb1TrmgKlMF0+j0qLPQ=";
    })
    # Don't build haredoc since it uses the build `hare` bin, which breaks
    # cross-compilation.
    ./002-dont-build-haredoc.patch
    # Hardcode harec and qbe.
    (substituteAll {
      src = ./003-hardcode-qbe-and-harec.patch;
      harec = lib.getExe harec;
      qbe = lib.getExe qbe;
    })
    # Display toolchains when using `hare version -v`.
    (fetchpatch {
      url = "https://git.sr.ht/~sircmpwn/hare/commit/e35f2284774436f422e06f0e8d290b173ced1677.patch";
      hash = "sha256-A59bGO/9tOghV8/MomTxd8xRExkHVdoMom2d+HTfQGg=";
    })
  ];

  nativeBuildInputs = [
    harec
    qbe
    scdoc
  ];

  # Needed for build frameworks like `haredo`, which set the HAREC and QBE env vars to `harec` and
  # `qbe` respectively.
  propagatedBuildInputs = [
    harec
    qbe
  ];

  buildInputs = [
    harec
    qbe
  ];

  makeFlags = [
    "HARECACHE=.harecache"
    "PREFIX=${builtins.placeholder "out"}"
    "ARCH=${arch}"
    "VERSION=${finalAttrs.version}-nixpkgs"
    "QBEFLAGS=-t${qbePlatform}"
    "AS=${stdenv.cc.targetPrefix}as"
    "LD=${stdenv.cc.targetPrefix}ld"
    "${lib.toUpper buildArch}_AS=${embeddedOnBinaryTools.${buildArch}.as}"
    "${lib.toUpper buildArch}_CC=${embeddedOnBinaryTools.${buildArch}.cc}"
    "${lib.toUpper buildArch}_LD=${embeddedOnBinaryTools.${buildArch}.ld}"
    # Strip the variable of an empty $(SRCDIR)/hare/third-party, since nix does
    # not follow the FHS.
    "HAREPATH=$(SRCDIR)/hare/stdlib"
  ] ++ lib.optionals enableCrossCompilation crossCompMakeFlags;

  enableParallelBuilding = true;

  # Append the distribution name to the version
  env.LOCALVER = "nixpkgs";

  strictDeps = true;

  doCheck = true;

  postConfigure = ''
    ln -s configs/${platform}.mk config.mk
  '';

  setupHook = ./setup-hook.sh;

  passthru = {
    updateScript = gitUpdater { };
    tests = lib.optionalAttrs enableCrossCompilation {
      crossCompilation = callPackage ./cross-compilation-tests.nix { hare = finalAttrs.finalPackage; };
    };
  };

  meta = {
    homepage = "https://harelang.org/";
    description = "Systems programming language designed to be simple, stable, and robust";
    license = lib.licenses.gpl3Only;
    maintainers = with lib.maintainers; [ onemoresuza ];
    mainProgram = "hare";
    inherit (harec.meta) platforms badPlatforms;
  };
})