daml/bazel_tools/haskell-cc-wrapper-include-dirs.patch
Andreas Herrmann cf6814e93b Use cc_wrapper to shorten library and include paths and rpaths (#2791)
* Fix bazel query deps(//...)

* Add rules_haskell cc_wrapper

Updates to latest rules_haskell master and adds the cc_wrapper PR as a
patch, see https://github.com/tweag/rules_haskell/pull/1039.

* Shorten include dirs in cc-wrapper

When using `haskell_cabal_library` GHC constructs unnecessarily long
include directories which can quickly overflow the maximum command-line
length. This patch avoids the issue by normalizing include paths.

* glob --> breadth_first_walk
2019-09-09 15:50:51 +00:00

141 lines
5.0 KiB
Diff

From 7c5178a381ab74330ed2765de5a974c92a4a5668 Mon Sep 17 00:00:00 2001
From: Andreas Herrmann <andreas.herrmann@tweag.io>
Date: Wed, 4 Sep 2019 11:48:48 +0200
Subject: [PATCH] Shorten include paths
---
haskell/private/cc_wrapper.py.tpl | 28 +++++++++++++++++++++++
haskell/private/cc_wrapper_windows.sh.tpl | 28 ++++++++++++++++++++++-
2 files changed, 55 insertions(+), 1 deletion(-)
diff --git a/haskell/private/cc_wrapper.py.tpl b/haskell/private/cc_wrapper.py.tpl
index bbfbea33..92edaa15 100644
--- a/haskell/private/cc_wrapper.py.tpl
+++ b/haskell/private/cc_wrapper.py.tpl
@@ -15,6 +15,13 @@ used to avoid command line length limitations.
will cause linking failures. This wrapper shortens library search paths to
avoid that issue.
+- Shortens include paths to stay below maximum path length.
+
+ GHC generates include paths that contain redundant up-level
+ references (..). This can exceed the maximum path length, which
+ will cause compiler failures. This wrapper shortens include paths
+ to avoid that issue.
+
- Shortens rpaths and load commands on macOS.
The rpaths and load commands generated by GHC and Bazel can quickly exceed
@@ -88,6 +95,7 @@ class Args:
"""Parse the given arguments into an Args object.
- Shortens library search paths.
+ - Shortens include paths.
- Detects the requested action.
- Keeps rpath arguments for further processing when linking.
- Keeps print-file-name arguments for further processing.
@@ -150,6 +158,8 @@ class Args:
# another handler should consume the argument.
if self._handle_output(arg, args, out):
pass
+ elif self._handle_include_path(arg, args, out):
+ pass
elif self._handle_library(arg, args, out):
pass
elif self._handle_library_path(arg, args, out):
@@ -176,6 +186,24 @@ class Args:
return consumed
+ def _handle_include_path(self, arg, args, out):
+ # All those flags behave like `short` flags. I.e. `-isystem<some/path>`
+ # is legal, but `-isystem=<some/path>` is not.
+ flags = ["-I", "-iquote", "-isystem", "-idirafter"]
+ for flag in flags:
+ consumed, include_path = argument(arg, args, short = flag)
+
+ if consumed:
+ # Skip non-existent include paths.
+ if os.path.exists(include_path):
+ # Shorten the include paths.
+ shortened = shorten_path(include_path)
+ out.append("{}{}".format(flag, shortened))
+
+ return True
+
+ return False
+
def _handle_library(self, arg, args, out):
consumed, library = argument(arg, args, short = "-l", long = "--library")
diff --git a/haskell/private/cc_wrapper_windows.sh.tpl b/haskell/private/cc_wrapper_windows.sh.tpl
index eb7c363e..54ceb522 100644
--- a/haskell/private/cc_wrapper_windows.sh.tpl
+++ b/haskell/private/cc_wrapper_windows.sh.tpl
@@ -12,6 +12,13 @@
# references (..). This can exceed the maximum path length on Windows, which
# will cause linking failures. This wrapper shortens library search paths to
# avoid that issue.
+#
+# - Shortens include paths to stay below maximum path length.
+#
+# GHC generates include paths that contain redundant up-level
+# references (..). This can exceed the maximum path length, which
+# will cause compiler failures. This wrapper shortens include paths
+# to avoid that issue.
set -euo pipefail
@@ -95,6 +102,8 @@ add_arg() {
# Parse arguments
IN_RESPONSE_FILE=
+INCLUDE_DIR_COMING=
+INCLUDE_FLAG=
LIB_DIR_COMING=
shorten_path() {
@@ -118,6 +127,14 @@ shorten_path() {
fi
}
+handle_include_dir() {
+ local flag="$1"
+ local include_dir="$2"
+ local shortened
+ shorten_path shortened "$include_dir"
+ add_arg "$flag$shortened"
+}
+
handle_lib_dir() {
local lib_dir="$1"
local shortened
@@ -130,7 +147,11 @@ handle_arg() {
if [[ $IN_RESPONSE_FILE = 1 ]]; then
unquote_arg arg "$arg"
fi
- if [[ $LIB_DIR_COMING = 1 ]]; then
+ if [[ $INCLUDE_DIR_COMING = 1 ]]; then
+ INCLUDE_DIR_COMING=
+ INCLUDE_FLAG=
+ handle_include_dir "$INCLUDE_FLAG" "$arg"
+ elif [[ $LIB_DIR_COMING = 1 ]]; then
LIB_DIR_COMING=
handle_lib_dir "$arg"
elif [[ "$arg" =~ ^@(.*)$ ]]; then
@@ -139,6 +160,11 @@ handle_arg() {
handle_arg "$line"
done < "${BASH_REMATCH[1]}"
IN_RESPONSE_FILE=
+ elif [[ "$arg" =~ ^(-I|-iquote|-isystem|-idirafter)(.*)$ ]]; then
+ handle_include_dir "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
+ elif [[ "$arg" = -I || "$arg" = -iquote || "$arg" = -isystem || "$arg" = -idirafter ]]; then
+ INCLUDE_DIR_COMING=1
+ INCLUDE_FLAG="$arg"
elif [[ "$arg" =~ ^-L(.*)$ || "$arg" =~ ^--library-path=(.*)$ ]]; then
handle_lib_dir "${BASH_REMATCH[1]}"
elif [[ "$arg" = -L || "$arg" = --library-path ]]; then
--
2.21.0