diff options
author | ibiryukov <ibiryukov@google.com> | 2018-01-02 06:09:04 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-01-02 06:10:59 -0800 |
commit | defba586cde9435478d13d33225c2c643b58268f (patch) | |
tree | 50093caece274d2dea94f41520ee7e72f7dc4eb1 /tools | |
parent | 6220c72b27261fc70e63f635a86b528688cd3016 (diff) |
Use relative paths for repository-local files in C++ crosstool
Generated crosstool previously used absolute paths for everything.
However, when using a compiler, local to the repository (e.g., downloaded via
'repository_ctx.download'), relative paths should be used to avoid absolute
paths that point into the crosstool repository.
Specifically, this patch contains the following changes:
1. Replaces absolute paths in 'cxx_builtin_include' with relative if includes
point inside the repository.
2. Removes the '-B<compiler-dir>' from 'compiler_flag' and 'linker_flag'
sections when compiler is inside the repository.
PiperOrigin-RevId: 180540359
Diffstat (limited to 'tools')
-rw-r--r-- | tools/cpp/unix_cc_configure.bzl | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/tools/cpp/unix_cc_configure.bzl b/tools/cpp/unix_cc_configure.bzl index 1e5207145c..719756eab3 100644 --- a/tools/cpp/unix_cc_configure.bzl +++ b/tools/cpp/unix_cc_configure.bzl @@ -23,6 +23,25 @@ load( "tpl", ) +def _prepare_include_path(repo_ctx, path): + """Resolve and sanitize include path before outputting it into the crosstool. + + Args: + repo_ctx: repository_ctx object. + path: an include path to be sanitized. + + Returns: + Sanitized include path that can be written to the crosstoot. Resulting path + is absolute if it is outside the repository and relative otherwise. + """ + + repo_root = str(repo_ctx.path(".")) + # We're on UNIX, so the path delimiter is '/'. + repo_root += "/" + path = str(repo_ctx.path(path)) + if path.startswith(repo_root): + return escape_string(path[len(repo_root):]) + return escape_string(path) def _get_value(it): """Convert `it` in serialized protobuf format.""" @@ -145,7 +164,7 @@ def get_escaped_cxx_inc_directories(repository_ctx, cc): else: inc_dirs = result.stderr[index1 + 1:index2].strip() - return [escape_string(repository_ctx.path(_cxx_inc_convert(p))) + return [_prepare_include_path(repository_ctx, _cxx_inc_convert(p)) for p in inc_dirs.split("\n")] @@ -182,6 +201,13 @@ def _is_gold_supported(repository_ctx, cc): def _crosstool_content(repository_ctx, cc, cpu_value, darwin): """Return the content for the CROSSTOOL file, in a dictionary.""" supports_gold_linker = _is_gold_supported(repository_ctx, cc) + cc_path = repository_ctx.path(cc) + if not str(cc_path).startswith(str(repository_ctx.path(".")) + '/'): + # cc is outside the repository, set -B + bin_search_flag = ["-B" + escape_string(str(cc_path.dirname))] + else: + # cc is inside the repository, don't set -B. + bin_search_flag = [] return { "abi_version": escape_string(get_env_var(repository_ctx, "ABI_VERSION", "local", False)), "abi_libc_version": escape_string(get_env_var(repository_ctx, "ABI_LIBC_VERSION", "local", False)), @@ -214,8 +240,7 @@ def _crosstool_content(repository_ctx, cc, cpu_value, darwin): "-undefined", "dynamic_lookup", "-headerpad_max_install_names", - ] if darwin else [ - "-B" + str(repository_ctx.path(cc).dirname), + ] if darwin else bin_search_flag + [ # Always have -B/usr/bin, see https://github.com/bazelbuild/bazel/issues/760. "-B/usr/bin", # Gold linker only? Can we enable this by default? @@ -247,8 +272,7 @@ def _crosstool_content(repository_ctx, cc, cpu_value, darwin): # All warnings are enabled. Maybe enable -Werror as well? "-Wall", # Enable a few more warnings that aren't part of -Wall. - ] + (["-Wthread-safety", "-Wself-assign"] if darwin else [ - "-B" + escape_string(str(repository_ctx.path(cc).dirname)), + ] + (["-Wthread-safety", "-Wself-assign"] if darwin else bin_search_flag + [ # Always have -B/usr/bin, see https://github.com/bazelbuild/bazel/issues/760. "-B/usr/bin", ]) + ( |