Unverified Commit 52e9baa5 authored by Ben Siraphob's avatar Ben Siraphob Committed by GitHub
Browse files

gforth: fix cross-compilation (#104778)

parents 6387538f ef4ec75a
Loading
Loading
Loading
Loading
+39 −5
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@
  lib,
  stdenv,
  fetchFromGitHub,
  callPackage,
  buildPackages,
  autoreconfHook,
  gitUpdater,
  texinfo,
@@ -11,8 +11,8 @@
}:

let
  swig = callPackage ./swig.nix { };
  bootForth = callPackage ./boot-forth.nix { };
  swig = buildPackages.callPackage ./swig.nix { };
  bootForth = buildPackages.callPackage ./boot-forth.nix { };
  lispDir = "${placeholder "out"}/share/emacs/site-lisp";
in
stdenv.mkDerivation (finalAttrs: {
@@ -32,9 +32,14 @@ stdenv.mkDerivation (finalAttrs: {
    writableTmpDirAsHomeHook
    autoreconfHook
    texinfo
    bootForth
    swig
  ];
  ]
  ++ (
    if (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) then
      [ buildPackages.gforth ]
    else
      [ bootForth ]
  );

  buildInputs = [
    libffi
@@ -47,12 +52,41 @@ stdenv.mkDerivation (finalAttrs: {
  ]
  ++ lib.optionals (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isx86_64) [
    "--build=x86_64-apple-darwin"
  ]
  ++ lib.optionals (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) [
    # Tries to run ./engine/gforth-ll
    "--without-check"
    # Use build-platform CC for helper programs that must run during build
    "HOSTCC=${buildPackages.stdenv.cc}/bin/cc"
    # Tell gforth's libcc where to find the cross compiler
    "CROSS_PREFIX=${stdenv.hostPlatform.config}-"
  ];

  env = lib.optionalAttrs (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) {
    # Tell gforth's libcc to prefix compiler commands with the cross-compilation target
    CROSS_PREFIX = "${stdenv.hostPlatform.config}-";
  };

  makeFlags = lib.optionals (stdenv.buildPlatform != stdenv.hostPlatform) [
    "DITCENGINE=${buildPackages.gforth}/bin/gforth-ditc"
    "GFORTH=${buildPackages.gforth}/bin/gforth"
    "ENGINE=${buildPackages.gforth}/bin/gforth" # for ./preforth
  ];

  preConfigure = ''
    mkdir -p ${lispDir}
  '';

  postConfigure = lib.optionalString (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) ''
    # Put the project-local (cross-aware) libtool first in PATH
    mkdir -p .cross-bin
    ln -sf "$PWD/libtool" .cross-bin/libtool
    ln -sf "$PWD/libtool" ".cross-bin/${stdenv.hostPlatform.config}-libtool"
    export PATH="$PWD/.cross-bin:$PATH"
    # Remove 'check' from the default 'all' target (can't run cross binaries)
    sed -i 's/^\(all:.*\) check/\1/' Makefile
  '';

  passthru.updateScript = gitUpdater { };

  meta = {