Unverified Commit 4f06a00f authored by Peder Bergebakken Sundt's avatar Peder Bergebakken Sundt Committed by GitHub
Browse files

Merge pull request #295155 from onemoresuza/hare-hook

hareHook: init
parents 6384cd9b cfa58200
Loading
Loading
Loading
Loading
+53 −0
Original line number Diff line number Diff line
# Hare {#sec-language-hare}

## Building Hare programs with `hareHook` {#ssec-language-hare}

The `hareHook` package sets up the environment for building Hare programs by
doing the following:

1. Setting the `HARECACHE`, `HAREPATH` and `NIX_HAREFLAGS` environment variables;
1. Propagating `harec`, `qbe` and two wrapper scripts  for the hare binary.

It is not a function as is the case for some other languages --- *e. g.*, Go or
Rust ---, but a package to be added to `nativeBuildInputs`.

## Attributes of `hareHook` {#hareHook-attributes}

The following attributes are accepted by `hareHook`:

1. `hareBuildType`: Either `release` (default) or `debug`. It controls if the
   `-R` flag is added to `NIX_HAREFLAGS`.

## Example for `hareHook` {#ex-hareHook}

```nix
{
  hareHook,
  lib,
  stdenv,
}: stdenv.mkDerivation {
  pname = "<name>";
  version = "<version>";
  src = "<src>";

  nativeBuildInputs = [ hareHook ];

  meta = {
    description = "<description>";
    inherit (hareHook) badPlatforms platforms;
  };
}
```

## Cross Compilation {#hareHook-cross-compilation}

`hareHook` should handle cross compilation out of the box. This is the main
purpose of `NIX_HAREFLAGS`: In it, the `-a` flag is passed with the architecture
of the `hostPlatform`.

However, manual intervention may be needed when a binary compiled by the build
process must be run for the build to complete --- *e. g.*, when using Hare's
`hare` module for code generation.

In those cases, `hareHook` provides the `hare-native` script, which is a wrapper
around the hare binary for using the native (`buildPlatform`) toolchain.
+1 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ dotnet.section.md
emscripten.section.md
gnome.section.md
go.section.md
hare.section.md
haskell.section.md
hy.section.md
idris.section.md
+4 −0
Original line number Diff line number Diff line
@@ -58,6 +58,10 @@

<!-- To avoid merge conflicts, consider adding your item at an arbitrary place in the list instead. -->

- `hareHook` has been added as the language framework for Hare. From now on, it,
  not the `hare` package, should be added to `nativeBuildInputs` when building
  Hare programs.

- To facilitate dependency injection, the `imgui` package now builds a static archive using vcpkg' CMake rules.
  The derivation now installs "impl" headers selectively instead of by a wildcard.
  Use `imgui.src` if you just want to access the unpacked sources.
+10 −21
Original line number Diff line number Diff line
{ stdenv
, lib
, fetchFromSourcehut
, gitUpdater
, hare
, hareThirdParty
{
  stdenv,
  lib,
  fetchFromSourcehut,
  gitUpdater,
  hareHook,
  hareThirdParty,
}:

stdenv.mkDerivation (finalAttrs: {
@@ -18,30 +19,18 @@ stdenv.mkDerivation (finalAttrs: {
  };

  nativeBuildInputs = [
    hare
    hareHook
    hareThirdParty.hare-ev
    hareThirdParty.hare-json
  ];

  makeFlags = [
    "PREFIX=${builtins.placeholder "out"}"
    "HARECACHE=.harecache"
    "HAREFLAGS=-qa${stdenv.hostPlatform.uname.processor}"
  ];
  makeFlags = [ "PREFIX=${builtins.placeholder "out"}" ];

  enableParallelBuilding = true;

  doCheck = true;

  postPatch = ''
    substituteInPlace Makefile \
      --replace 'hare build' 'hare build $(HAREFLAGS)' \
      --replace 'hare test' 'hare test $(HAREFLAGS)'
  '';

  passthru.updateScript = gitUpdater {
    rev-prefix = "v";
  };
  passthru.updateScript = gitUpdater { rev-prefix = "v"; };

  meta = with lib; {
    description = "Finite State Machine structured as a tree";
+56 −0
Original line number Diff line number Diff line
{
  hare,
  lib,
  makeSetupHook,
  makeWrapper,
  runCommand,
  stdenv,
  writeShellApplication,
}:
let
  arch = stdenv.targetPlatform.uname.processor;
  harePropagationInputs = builtins.attrValues { inherit (hare) harec qbe; };
  hareWrappedScript = writeShellApplication {
    # `name` MUST be `hare`, since its role is to replace the hare binary.
    name = "hare";
    runtimeInputs = [ hare ];
    excludeShellChecks = [ "SC2086" ];
    # ''${cmd:+"$cmd"} is used on the default case to keep the same behavior as
    # the hare binary: If "$cmd" is passed directly and it's empty, the hare
    # binary will treat it as an unrecognized command.
    text = ''
      readonly cmd="$1"
      shift
      case "$cmd" in
        "test"|"run"|"build") exec hare "$cmd" $NIX_HAREFLAGS "$@" ;;
        *) exec hare ''${cmd:+"$cmd"} "$@"
      esac
    '';
  };
  hareWrapper = runCommand "hare-wrapper" { nativeBuildInputs = [ makeWrapper ]; } ''
    mkdir -p $out/bin
    install ${lib.getExe hareWrappedScript} $out/bin/hare
    makeWrapper ${lib.getExe hare} $out/bin/hare-native \
      --inherit-argv0 \
      --unset AR \
      --unset LD \
      --unset CC
  '';
in
makeSetupHook {
  name = "hare-hook";
  # The propagation of `qbe` and `harec` (harePropagationInputs) is needed for
  # build frameworks like `haredo`, which set the HAREC and QBE env vars to
  # `harec` and `qbe` respectively. We use the derivations from the `hare`
  # package to assure that there's no different behavior between the `hareHook`
  # and `hare` packages.
  propagatedBuildInputs = [ hareWrapper ] ++ harePropagationInputs;
  substitutions = {
    hare_unconditional_flags = "-q -a${arch}";
    hare_stdlib = "${hare}/src/hare/stdlib";
  };
  meta = {
    description = "A setup hook for the Hare compiler";
    inherit (hare.meta) badPlatforms platforms;
  };
} ./setup-hook.sh
Loading