Unverified Commit 49a9dd1c authored by Adam Thompson-Sharpe's avatar Adam Thompson-Sharpe
Browse files

opentxl: use generated code as src instead of repo

Previously, the derivation for this package would clone the source repo
and run a codegen tool as part of the build process. However, the
codegen tool only supports x86 architectures, while the emitted C is
platform-agnostic. This commit allows the package to build on more
platforms by instead fetching just the generated source .tar.gz from
GitHub releases.
parent 75b04c4c
Loading
Loading
Loading
Loading
+15 −24
Original line number Diff line number Diff line
@@ -2,44 +2,38 @@
  lib,
  stdenv,
  callPackage,
  fetchFromGitHub,
  fetchurl,
  nix-update-script,
  turingplus,
}:
stdenv.mkDerivation (finalAttrs: {
  pname = "opentxl";
  version = "11.3.7";

  src = fetchFromGitHub {
    owner = "CordyJ";
    repo = "OpenTxl";
    tag = "v${finalAttrs.version}";
    hash = "sha256-Gh0OEZ5pGhbxDIKYuBLdzUV7Ezn+7elm146ccpJ5bn8=";
  # The code generation part of the upstream build system relies on an x86-only binary,
  # so the generated code is fetched from the GitHub release instead
  src = fetchurl {
    url = "https://github.com/CordyJ/OpenTxl/releases/download/v${finalAttrs.version}/OpenTxl-${finalAttrs.version}-csrc.tar.gz";
    hash = "sha256-qIvxQqo1yCVJImjUvNNinzhoywVgaq9s0E+Ab+QStc0=";
  };

  nativeBuildInputs = [ turingplus ];

  # Using -std=gnu89 to prevent errors that occur with default args
  env.NIX_CFLAGS_COMPILE = "-std=gnu89 -Wno-int-conversion";

  postPatch = ''
    # Replace hardcoded /bin/rm in various files
    find . -type f -exec sed -i 's#/bin/rm#rm#g' {} +
    # Replace hardcoded FHS paths in various files
    find . -type f -exec sed -i \
      -e 's#/bin/mv#mv#g' \
      -e 's#/bin/rm#rm#g' \
      -e "s#/usr/local/bin#$out/bin#g" \
      -e "s#/usr/local/lib/txl#$out/lib#g" \
      {} +

    # Replace hardcoded gcc references
    substituteInPlace src/scripts/c/unix/{txlc,txl2c} \
    substituteInPlace scripts/unix/{txlc,txl2c} \
      --replace-fail gcc '${stdenv.cc}/bin/cc'

    # Replace hardcoded FHS paths
    substituteInPlace src/scripts/c/unix/* src/scripts/t/* \
      --replace-fail '/usr/local/bin' "$out/bin" \
      --replace-fail '/usr/local/lib/txl' "$out/lib"
  '';

  # Generate source files and enter directory
  preBuild = ''
    make csrc
    cd csrc
    makeFlagsArray+=(
      CC="$CC"
      LD="$CC"
@@ -64,10 +58,7 @@ stdenv.mkDerivation (finalAttrs: {
  meta = {
    description = "Open-source compiler for the Txl language";
    mainProgram = "txl";
    platforms = [
      "x86_64-linux"
      "x86_64-darwin"
    ];
    platforms = lib.platforms.unix;
    homepage = "https://github.com/CordyJ/OpenTxl";
    downloadPage = "https://github.com/CordyJ/OpenTxl/releases";
    changelog = "https://github.com/CordyJ/OpenTxl/releases/tag/v${finalAttrs.version}";