-
Notifications
You must be signed in to change notification settings - Fork 602
Adjust --remap-path-prefix for targets with generated sources
#4016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tamasvajk
wants to merge
4
commits into
bazelbuild:main
Choose a base branch
from
tamasvajk:remap-symlinked-sources
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+102
−24
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
dc8754c
Fix --remap-path-prefix for targets with generated sources
b160dcd
Fix remap tests to be platform-independent
4941326
Fix comment: remap-path-prefix does not affect file!()
a2b653f
Fix bin-dir remap to work with --experimental_output_paths=strip
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1185,14 +1185,45 @@ def construct_arguments( | |
| rustc_flags.add(compilation_mode.strip_level, format = "--codegen=strip=%s") | ||
|
|
||
| # For determinism to help with build distribution and such | ||
| # | ||
| # --remap-path-prefix tells rustc to replace a path prefix in all | ||
| # embedded paths (debug info, dep-info, panic locations, backtraces) | ||
| # so that absolute sandbox paths never leak into binaries or logs. | ||
| # | ||
| # When all sources are plain workspace files, remapping ${pwd} (the exec | ||
| # root) to remap_path_prefix (default ".") is enough. | ||
| # | ||
| # However, when a target mixes generated and non-generated sources (e.g. | ||
| # proto compile_data), transform_sources() symlinks every source file into | ||
| # bazel-out/<config>/bin/... so they sit next to the generated files. In | ||
| # that case the crate root is no longer a source file and its path starts | ||
| # with ctx.bin_dir.path (e.g. "bazel-out/k8-fastbuild/bin"). We detect | ||
| # this via crate_info.root.is_source and use a more specific remap that | ||
| # also strips the bin-dir component, giving clean workspace-relative paths | ||
| # in panic messages and backtraces. | ||
| if remap_path_prefix != None: | ||
| # `--remap-path-prefix` flags are applied in reverse order. We need to | ||
| # specify the outermost directory (output_base) first, so that it's | ||
| # remapped last. Otherwise we can end up with a partial rewrite where | ||
| # "/path/to/output_base/execroot" becomes "./execroot" rather than ".". | ||
| rustc_flags.add("--remap-path-prefix=${{output_base}}={}".format(remap_path_prefix)) | ||
| rustc_flags.add("--remap-path-prefix=${{pwd}}={}".format(remap_path_prefix)) | ||
| rustc_flags.add("--remap-path-prefix=${{exec_root}}={}".format(remap_path_prefix)) | ||
| if crate_info.root.is_source: | ||
| rustc_flags.add("--remap-path-prefix=${{pwd}}={}".format(remap_path_prefix)) | ||
| rustc_flags.add("--remap-path-prefix=${{exec_root}}={}".format(remap_path_prefix)) | ||
| else: | ||
| # Use add_all with the crate root File and a map_each callback | ||
| # so Bazel's path mapping (--experimental_output_paths=strip) | ||
| # can rewrite the config portion of the bin-dir prefix. | ||
| rustc_flags.add_all( | ||
| [crate_info.root], | ||
| map_each = _get_bin_dir_prefix, | ||
| format_each = "--remap-path-prefix=${pwd}/%s=" + remap_path_prefix, | ||
| ) | ||
| rustc_flags.add_all( | ||
| [crate_info.root], | ||
| map_each = _get_bin_dir_prefix, | ||
| format_each = "--remap-path-prefix=${exec_root}/%s=" + remap_path_prefix, | ||
| ) | ||
|
|
||
| emit_without_paths = [] | ||
| for kind in emit: | ||
|
|
@@ -1288,11 +1319,12 @@ def construct_arguments( | |
| if remap_path_prefix != None and _should_add_oso_prefix( | ||
| toolchain, | ||
| ): | ||
| oso_prefix = "${pwd}/" if crate_info.root.is_source else "${pwd}/" + ctx.bin_dir.path + "/" | ||
| if ld_is_direct_driver: | ||
| rustc_flags.add("--codegen=link-arg=-oso_prefix") | ||
| rustc_flags.add("${pwd}/", format = "--codegen=link-arg=%s") | ||
| rustc_flags.add(oso_prefix, format = "--codegen=link-arg=%s") | ||
| else: | ||
| rustc_flags.add("--codegen=link-arg=-Wl,-oso_prefix,${pwd}/") | ||
| rustc_flags.add("--codegen=link-arg=-Wl,-oso_prefix," + oso_prefix) | ||
|
|
||
| _add_native_link_flags( | ||
| rustc_flags, | ||
|
|
@@ -2706,6 +2738,22 @@ def _add_native_link_flags( | |
| format_each = "-lstatic=%s", | ||
| ) | ||
|
|
||
| def _get_bin_dir_prefix(file): | ||
| """Returns the bin-dir prefix (without trailing slash) from a generated file. | ||
|
|
||
| For a generated file, file.path is "bazel-out/<config>/bin/<pkg>/<name>" | ||
| and file.short_path is "<pkg>/<name>". The difference is the bin-dir | ||
| prefix. Using the File object with add_all/map_each lets Bazel apply | ||
| path mapping (--experimental_output_paths=strip) to the config portion. | ||
|
|
||
| Args: | ||
| file (File): A generated file (e.g. the crate root). | ||
|
|
||
| Returns: | ||
| str: The bin-dir prefix, e.g. "bazel-out/k8-fastbuild/bin". | ||
| """ | ||
| return file.path[:len(file.path) - len(file.short_path)].rstrip("/") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice if you could fold the Also, would |
||
|
|
||
| def _get_dirname(file): | ||
| """A helper function for `_add_native_link_flags`. | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need to pass this as a
Fileas well so that it works with--experimental_output_paths=strip?