Unverified Commit dcf16cfe authored by Rick van Schijndel's avatar Rick van Schijndel Committed by GitHub
Browse files

bazel_9: init at 9.0.1 (#476183)

parents 3cd2cbee 81f13fc6
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
#!@runtimeShell@

exec @binJava@ -jar @out@/share/parser_deploy.jar "$@"
+77 −0
Original line number Diff line number Diff line
{
  stdenv,
  lndir,
  lib,
}:

args@{
  bazel,
  registry ? null,
  bazelRepoCache ? null,
  bazelVendorDeps ? null,
  startupArgs ? [ ],
  commandArgs ? [ ],
  bazelPreBuild ? "",
  bazelPostBuild ? "",
  serverJavabase ? null,
  targets,
  command,
  ...
}:

stdenv.mkDerivation (
  {
    preBuildPhases = [ "preBuildPhase" ];
    preBuildPhase =
      (lib.optionalString (bazelRepoCache != null) ''
        # repo_cache needs to be writeable even in air-gapped builds
        mkdir repo_cache
        ${lndir}/bin/lndir -silent ${bazelRepoCache}/repo_cache repo_cache
      '')

      + (lib.optionalString (bazelVendorDeps != null) ''
        mkdir vendor_dir
        ${lndir}/bin/lndir -silent ${bazelVendorDeps}/vendor_dir vendor_dir

        # pin all deps to avoid re-fetch attempts by Bazel
        rm vendor_dir/VENDOR.bazel
        find vendor_dir -mindepth 1 -maxdepth 1 -type d -printf "pin(\"@@%P\")\n" > vendor_dir/VENDOR.bazel
      '')
      # keep preBuildPhase always defined as it is listed in preBuildPhases
      + ''
        true
      '';
    buildPhase = ''
      runHook preBuild

      export HOME=$(mktemp -d)

      ${bazelPreBuild}

      ${bazel}/bin/bazel ${
        lib.escapeShellArgs (
          lib.optional (serverJavabase != null) "--server_javabase=${serverJavabase}"
          ++ [ "--batch" ]
          ++ startupArgs
        )
      } ${command} ${
        lib.escapeShellArgs (
          lib.optional (registry != null) "--registry=file://${registry}"
          ++ lib.optionals (bazelRepoCache != null) [
            "--repository_cache=repo_cache"
            "--repo_contents_cache="
          ]
          ++ lib.optional (bazelVendorDeps != null) "--vendor_dir=vendor_dir"
          ++ commandArgs
          ++ targets
        )
      }

      ${bazelPostBuild}

      runHook postBuild
    '';

  }
  // args
)
+172 −0
Original line number Diff line number Diff line
{
  callPackage,
  gnugrep,
  lib,
  autoPatchelfHook,
  stdenv,
}:

{
  name,
  src,
  sourceRoot ? null,
  version ? null,
  targets,
  bazel,
  startupArgs ? [ ],
  commandArgs ? [ ],
  env ? { },
  serverJavabase ? null,
  registry ? null,
  bazelRepoCacheFOD ? {
    outputHash = null;
    outputHashAlgo = "sha256";
  },
  bazelVendorDepsFOD ? {
    outputHash = null;
    outputHashAlgo = "sha256";
  },
  installPhase,
  buildInputs ? [ ],
  nativeBuildInputs ? [ ],
  autoPatchelfIgnoreMissingDeps ? null,
  patches ? [ ],
}:
let
  # FOD produced by `bazel fetch`
  # Repo cache contains content-addressed external Bazel dependencies without any patching
  # Potentially this can be nixified via --experimental_repository_resolved_file
  # (Note: file itself isn't reproducible because it has lots of extra info and order
  #        isn't stable too. Parsing it into nix fetch* commands isn't trivial but might be possible)
  bazelRepoCache =
    if bazelRepoCacheFOD.outputHash == null then
      null
    else
      (callPackage ./bazelDerivation.nix { } {
        name = "bazelRepoCache";
        inherit (bazelRepoCacheFOD) outputHash outputHashAlgo;
        inherit
          src
          version
          sourceRoot
          env
          buildInputs
          nativeBuildInputs
          patches
          ;
        inherit registry;
        inherit
          bazel
          targets
          startupArgs
          serverJavabase
          ;
        command = "fetch";
        outputHashMode = "recursive";
        commandArgs = [
          "--repository_cache=repo_cache"
          "--repo_contents_cache="
        ]
        ++ commandArgs;
        bazelPreBuild = ''
          mkdir repo_cache
        '';
        installPhase = ''
          mkdir -p $out/repo_cache
          cp -r --reflink=auto repo_cache/* $out/repo_cache
        '';
      });
  # Stage1: FOD produced by `bazel vendor`, Stage2: eventual patchelf or other tuning
  # Vendor deps contains unpacked&patches external dependencies, this may need Nix-specific
  # patching to address things like
  # - broken symlinks
  # - symlinks or other references to absolute nix store paths which isn't allowed for FOD
  # - autoPatchelf for externally-fetched binaries
  #
  # Either repo cache or vendor deps should be enough to build a given package
  bazelVendorDeps =
    if bazelVendorDepsFOD.outputHash == null then
      null
    else
      (
        let
          stage1 = callPackage ./bazelDerivation.nix { } {
            name = "bazelVendorDepsStage1";
            inherit (bazelVendorDepsFOD) outputHash outputHashAlgo;
            inherit
              src
              version
              sourceRoot
              env
              buildInputs
              nativeBuildInputs
              patches
              ;
            inherit registry;
            inherit
              bazel
              targets
              startupArgs
              serverJavabase
              ;
            dontFixup = true;
            command = "vendor";
            outputHashMode = "recursive";
            commandArgs = [ "--vendor_dir=vendor_dir" ] ++ commandArgs;
            bazelPreBuild = ''
              mkdir vendor_dir
            '';
            bazelPostBuild = ''
              # remove symlinks that point to locations under bazel_src/
              find vendor_dir -type l -lname "$HOME/*" -exec rm '{}' \;
              # remove symlinks to temp build directory on darwin
              find vendor_dir -type l -lname "/private/var/tmp/*" -exec rm '{}' \;
              # remove broken symlinks
              find vendor_dir -xtype l -exec rm '{}' \;

              # remove .marker files referencing NIX_STORE as those references aren't allowed in FOD
              (${gnugrep}/bin/grep -rI "$NIX_STORE/" vendor_dir --files-with-matches --include="*.marker" --null || true) \
                | xargs -0 --no-run-if-empty rm
            '';
            installPhase = ''
              mkdir -p $out/vendor_dir
              cp -r --reflink=auto vendor_dir/* $out/vendor_dir
            '';

          };
        in
        stdenv.mkDerivation {
          name = "bazelVendorDeps";
          buildInputs = lib.optional (!stdenv.hostPlatform.isDarwin) autoPatchelfHook ++ buildInputs;
          inherit autoPatchelfIgnoreMissingDeps;
          src = stage1;
          installPhase = ''
            cp -r . $out
          '';
        }
      );

  package = callPackage ./bazelDerivation.nix { } {
    inherit
      name
      src
      version
      sourceRoot
      env
      buildInputs
      nativeBuildInputs
      patches
      ;
    inherit registry bazelRepoCache bazelVendorDeps;
    inherit
      bazel
      targets
      startupArgs
      serverJavabase
      commandArgs
      ;
    inherit installPhase;
    command = "build";
  };
in
package // { passthru = { inherit bazelRepoCache bazelVendorDeps; }; }
+24 −0
Original line number Diff line number Diff line
{
  stdenv,
}:
{
  # If there's a need to patch external dependencies managed by Bazel
  # one option is to configure patches on Bazel level. Bazel doesn't
  # allow patches to be in absolute paths so this helper will produce
  # sources patch that adds given file to given location
  addFilePatch =
    {
      path,
      file,
    }:
    stdenv.mkDerivation {
      name = "add_file.patch";
      dontUnpack = true;
      buildPhase = ''
        mkdir -p $(dirname "${path}")
        cp ${file} "${path}"
        diff -u /dev/null "${path}" >result.patch || true  # diff exit code is non-zero if there's a diff
      '';
      installPhase = "cp result.patch $out";
    };
}
+41 −0
Original line number Diff line number Diff line
{
  lib,
  makeBinaryWrapper,
  writeShellApplication,
  bash,
  stdenv,
}:
{ defaultShellUtils }:
let
  defaultShellPath = lib.makeBinPath defaultShellUtils;

  bashWithDefaultShellUtilsSh = writeShellApplication {
    name = "bash";
    runtimeInputs = defaultShellUtils;
    # Empty PATH in Nixpkgs Bash is translated to /no-such-path
    # On other distros empty PATH search fallback is looking in standard
    # locations like /bin,/usr/bin
    # For Bazel many rules rely on such search finding some common utils,
    # so we provide them in case rules or arguments didn't specify a precise PATH
    text = ''
      if [[ "$PATH" == "/no-such-path" ]]; then
        export PATH=${defaultShellPath}
      fi
      exec ${bash}/bin/bash "$@"
    '';
  };

in
{
  inherit defaultShellUtils defaultShellPath;
  # Script-based interpreters in shebangs aren't guaranteed to work,
  # especially on MacOS. So let's produce a binary
  bashWithDefaultShellUtils = stdenv.mkDerivation {
    name = "bash";
    src = bashWithDefaultShellUtilsSh;
    nativeBuildInputs = [ makeBinaryWrapper ];
    buildPhase = ''
      makeWrapper ${bashWithDefaultShellUtilsSh}/bin/bash $out/bin/bash
    '';
  };
}
Loading