From 659992ab90226c00506e884190fda4c56231513f Mon Sep 17 00:00:00 2001 From: Philip Patsch Date: Tue, 11 Jun 2019 18:24:12 +0200 Subject: [PATCH] Fix the ghc_bindist repository_rule scripts for MacOS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MacOS uses BSD coreutils, which have a different set of options and different semantics (e.g. `sed`’s `-i` option). We cannot assume GNU coreutils anymore, because we want to support bindists on MacOS without dependence on nix. Closes https://github.com/tweag/rules_haskell/issues/884 --- haskell/ghc_bindist.bzl | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/haskell/ghc_bindist.bzl b/haskell/ghc_bindist.bzl index 3737a28fed..73864121b0 100644 --- a/haskell/ghc_bindist.bzl +++ b/haskell/ghc_bindist.bzl @@ -170,6 +170,9 @@ GHC_BINDIST = \ def _execute_fail_loudly(ctx, args): """Execute a command and fail loudly if it fails. + ATTN: All commands have to be cross-compatible between BSD tools and GNU tools, + because we want to support MacOS. Please cross-reference the MacOS man-pages. + Args: ctx: Repository rule context. args: Command and its arguments. @@ -205,19 +208,24 @@ def _ghc_bindist_impl(ctx): # We apply some patches, if needed. patch(ctx) + # As the patches may touch the package DB we regenerate the cache. if len(ctx.attr.patches) > 0: _execute_fail_loudly(ctx, ["./bin/ghc-pkg", "recache"]) # On Windows the bindist already contains the built executables if os != "windows": - _execute_fail_loudly(ctx, ["sed", "-i", "s/RelocatableBuild = NO/RelocatableBuild = YES/", "mk/config.mk.in"]) + # IMPORTANT: all these scripts have to be compatible with BSD tools! + + # bsdcompatible is to work around the differences for -i on BSD sed + _execute_fail_loudly(ctx, ["sed", "-e", "s/RelocatableBuild = NO/RelocatableBuild = YES/", "-ibsdcompatible", "mk/config.mk.in"]) _execute_fail_loudly(ctx, ["./configure", "--prefix", bindist_dir.realpath]) _execute_fail_loudly(ctx, ["make", "install"]) - ctx.file("patch_bins", executable = True, content = """#!/usr/bin/env bash -grep -lZ {bindist_dir} bin/* | xargs -0 --verbose \\ - sed -i \\ - -e '2iDISTDIR="$( dirname "$(resolved="$0"; while tmp="$(readlink "$resolved")"; do resolved="$tmp"; done; echo "$resolved")" )/.."' \\ + ctx.file("patch_bins", executable = True, content = r"""#!/usr/bin/env bash +grep --files-with-matches --null {bindist_dir} bin/* | xargs -0 \ + sed -ibsdcompatible \ + -e '2i\ + DISTDIR="$( dirname "$(resolved="$0"; while tmp="$(readlink "$resolved")"; do resolved="$tmp"; done; echo "$resolved")" )/.."' \ -e 's:{bindist_dir}:$DISTDIR:' """.format( bindist_dir = bindist_dir.realpath,