Skip to content

Commit

Permalink
Use VFS overlay to remap $GEN_DIR to bazel-out
Browse files Browse the repository at this point in the history
This does two things for us:

1. Warnings and errors include the path to the actual files, instead of a symlink inside of the `.xcodeproj`.
2. But more importantly, Index Build works without having to generate files in a normal build first. This means indexing will be more correct more of the time.
  • Loading branch information
brentleyjones committed Jul 20, 2022
1 parent dbb609e commit 5ff032c
Show file tree
Hide file tree
Showing 19 changed files with 411 additions and 118 deletions.
22 changes: 17 additions & 5 deletions examples/ios_app/test/fixtures/bwb.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

54 changes: 49 additions & 5 deletions examples/ios_app/test/fixtures/bwx.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion test/fixtures/cc/bwb.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/bash;
shellScript = "set -euo pipefail\n\noutput_groups=(\"generated_inputs darwin_x86_64-fastbuild-ST-d53d69b6b8c1\")\n\n# Xcode doesn't adjust `$BUILD_DIR` in scheme action scripts when building for\n# previews. So we need to look in the non-preview build directory for this file.\noutput_groups_file=\"${BAZEL_BUILD_OUTPUT_GROUPS_FILE/\\/Intermediates.noindex\\/Previews\\/*\\/Products\\///Products/}\"\n\n# We need to read from this file as soon as possible, as concurrent writes to it\n# can happen during indexing, which breaks the off-by-one-by-design nature of it\nif [ -s \"$output_groups_file\" ]; then\n while IFS= read -r output_group; do\n output_groups+=(\"$output_group\")\n done < \"$output_groups_file\"\nfi\noutput_groups_flag=\"--output_groups=$(IFS=, ; echo \"${output_groups[*]}\")\"\n\nif [ \"$ACTION\" == \"indexbuild\" ]; then\n # We use a different output base for Index Build to prevent normal builds and\n # indexing waiting on bazel locks from the other\n output_base=\"$OBJROOT/bazel_output_base\"\nelif [ \"${ENABLE_PREVIEWS:-}\" == \"YES\" ]; then\n # We use a different output base for SwiftUI Previews since they have\n # different swiftcopts, preventing output trashing\n output_base=\"$OBJROOT/bazel_output_base\"\nfi\n\nif [[ \"${COLOR_DIAGNOSTICS:-NO}\" == \"YES\" ]]; then\n color=yes\nelse\n color=no\nfi\n\noutput_path=$(env -i \\\n DEVELOPER_DIR=\"$DEVELOPER_DIR\" \\\n HOME=\"$HOME\" \\\n PATH=\"/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin\" \\\n USER=\"$USER\" \\\n \"$BAZEL_PATH\" \\\n ${output_base:+--output_base \"$output_base\"} \\\n info \\\n --color=\"$color\" \\\n --experimental_convenience_symlinks=ignore \\\n --symlink_prefix=/ \\\n --bes_backend= \\\n --bes_results_url= \\\n output_path)\nexec_root=\"${output_path%/*}\"\nexternal=\"${exec_root%/*/*}/external\"\n\nif [[ \"$ACTION\" != \"indexbuild\" && \"${ENABLE_PREVIEWS:-}\" != \"YES\" ]]; then\n \"$BAZEL_INTEGRATION_DIR/create_lldbinit.sh\" \"$exec_root\" > \"$BAZEL_LLDB_INIT\"\nfi\n\n# We only want to modify `$LINKS_DIR` during normal builds since Indexing can\n# run concurrent to normal builds\nif [ \"$ACTION\" != \"indexbuild\" ]; then\n mkdir -p \"$LINKS_DIR\"\n cd \"$LINKS_DIR\"\n\n # Add BUILD and DONT_FOLLOW_SYMLINKS_WHEN_TRAVERSING_THIS_DIRECTORY_VIA_A_RECURSIVE_TARGET_PATTERN\n # files to the internal links directory to prevent Bazel from recursing into\n # it, and thus following the `external` symlink\n touch BUILD\n touch DONT_FOLLOW_SYMLINKS_WHEN_TRAVERSING_THIS_DIRECTORY_VIA_A_RECURSIVE_TARGET_PATTERN\n\n # Need to remove the directories that Xcode creates as part of output prep\n rm -rf external\n rm -rf gen_dir\n\n ln -sf \"$external\" external\n ln -sf \"$BAZEL_OUT\" gen_dir\nfi\n\ncd \"$BUILD_DIR\"\n\nrm -rf external\nrm -rf bazel-exec-root\n\nln -sf \"$external\" external\nln -sf \"$exec_root\" bazel-exec-root\nln -sfn \"$PROJECT_DIR\" SRCROOT\n\ncd \"$SRCROOT\"\n\nif [ \"$ACTION\" == \"indexbuild\" ]; then\n index_flags=(\n --bes_backend=\n --bes_results_url=\n )\nelif [ \"${ENABLE_PREVIEWS:-}\" == \"YES\" ]; then\n swiftui_previews_flags=(\n --swiftcopt=-Xfrontend\n --swiftcopt=-enable-implicit-dynamic\n --swiftcopt=-Xfrontend\n --swiftcopt=-enable-private-imports\n --swiftcopt=-Xfrontend\n --swiftcopt=-enable-dynamic-replacement-chaining\n )\nfi\n\ndate +%s > \"$INTERNAL_DIR/toplevel_cache_buster\"\n\nbuild_marker=\"$OBJROOT/bazel_build_start\"\ntouch \"$build_marker\"\n\nlog=$(mktemp)\nenv -i \\\n DEVELOPER_DIR=\"$DEVELOPER_DIR\" \\\n HOME=\"$HOME\" \\\n PATH=\"/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin\" \\\n USER=\"$USER\" \\\n \"$BAZEL_PATH\" \\\n ${output_base:+--output_base \"$output_base\"} \\\n build \\\n --color=\"$color\" \\\n --experimental_convenience_symlinks=ignore \\\n --symlink_prefix=/ \\\n ${index_flags:+${index_flags[*]}} \\\n ${swiftui_previews_flags:+${swiftui_previews_flags[*]}} \\\n \"$output_groups_flag\" \\\n //test/fixtures/cc:xcodeproj_bwb \\\n 2>&1 | tee -i \"$log\"\n\nfor output_group in \"${output_groups[@]}\"; do\n filelist=\"xcodeproj_bwb-${output_group//\\//_}\"\n filelist=\"${filelist/#/$output_path/darwin_x86_64-fastbuild-ST-d53d69b6b8c1/bin/test/fixtures/cc/}\"\n filelist=\"${filelist/%/.filelist}\"\n if [[ \"$filelist\" -ot \"$build_marker\" ]]; then\n echo \"error: Bazel didn't generate the correct files (it should have generated outputs for output group \\\"$output_group\\\", but the timestamp for \\\"$filelist\\\" was from before the build). Please regenerate the project to fix this.\" >&2\n echo \"error: If your bazel version is less than 5.2, you may need to \\`bazel clean\\` and/or \\`bazel shutdown\\` to work around a bug in project generation.\" >&2\n echo \"error: If you are still getting this error after all of that, please file a bug report here: https://github.com/buildbuddy-io/rules_xcodeproj/issues/new?template=bug.md.\" >&2\n exit 1\n fi\ndone\n";
shellScript = "set -euo pipefail\n\noutput_groups=(\"generated_inputs darwin_x86_64-fastbuild-ST-d53d69b6b8c1\")\n\n# Xcode doesn't adjust `$BUILD_DIR` in scheme action scripts when building for\n# previews. So we need to look in the non-preview build directory for this file.\noutput_groups_file=\"${BAZEL_BUILD_OUTPUT_GROUPS_FILE/\\/Intermediates.noindex\\/Previews\\/*\\/Products\\///Products/}\"\n\n# We need to read from this file as soon as possible, as concurrent writes to it\n# can happen during indexing, which breaks the off-by-one-by-design nature of it\nif [ -s \"$output_groups_file\" ]; then\n while IFS= read -r output_group; do\n output_groups+=(\"$output_group\")\n done < \"$output_groups_file\"\nfi\noutput_groups_flag=\"--output_groups=$(IFS=, ; echo \"${output_groups[*]}\")\"\n\nif [ \"$ACTION\" == \"indexbuild\" ]; then\n # We use a different output base for Index Build to prevent normal builds and\n # indexing waiting on bazel locks from the other\n output_base=\"$OBJROOT/bazel_output_base\"\nelif [ \"${ENABLE_PREVIEWS:-}\" == \"YES\" ]; then\n # We use a different output base for SwiftUI Previews since they have\n # different swiftcopts, preventing output trashing\n output_base=\"$OBJROOT/bazel_output_base\"\nfi\n\nif [[ \"${COLOR_DIAGNOSTICS:-NO}\" == \"YES\" ]]; then\n color=yes\nelse\n color=no\nfi\n\noutput_path=$(env -i \\\n DEVELOPER_DIR=\"$DEVELOPER_DIR\" \\\n HOME=\"$HOME\" \\\n PATH=\"/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin\" \\\n USER=\"$USER\" \\\n \"$BAZEL_PATH\" \\\n ${output_base:+--output_base \"$output_base\"} \\\n info \\\n --color=\"$color\" \\\n --experimental_convenience_symlinks=ignore \\\n --symlink_prefix=/ \\\n --bes_backend= \\\n --bes_results_url= \\\n output_path)\nexec_root=\"${output_path%/*}\"\nexternal=\"${exec_root%/*/*}/external\"\n\nif [[ \"$ACTION\" != \"indexbuild\" && \"${ENABLE_PREVIEWS:-}\" != \"YES\" ]]; then\n \"$BAZEL_INTEGRATION_DIR/create_lldbinit.sh\" \"$exec_root\" > \"$BAZEL_LLDB_INIT\"\nfi\n\n# We only want to modify `$LINKS_DIR` during normal builds since Indexing can\n# run concurrent to normal builds\nif [ \"$ACTION\" != \"indexbuild\" ]; then\n mkdir -p \"$LINKS_DIR\"\n cd \"$LINKS_DIR\"\n\n # Add BUILD and DONT_FOLLOW_SYMLINKS_WHEN_TRAVERSING_THIS_DIRECTORY_VIA_A_RECURSIVE_TARGET_PATTERN\n # files to the internal links directory to prevent Bazel from recursing into\n # it, and thus following the `external` symlink\n touch BUILD\n touch DONT_FOLLOW_SYMLINKS_WHEN_TRAVERSING_THIS_DIRECTORY_VIA_A_RECURSIVE_TARGET_PATTERN\n\n # Need to remove the directories that Xcode creates as part of output prep\n rm -rf external\n rm -rf gen_dir\n\n ln -sf \"$external\" external\n ln -sf \"$BAZEL_OUT\" gen_dir\nfi\n\n# Use actual paths for Bazel generated files\n# This also fixes Index Build to use its version of generated files\ncat > \"$BUILD_DIR/gen_dir-overlay.yaml\" <<EOF\n{\"case-sensitive\": \"false\", \"fallthrough\": true, \"roots\": [{\"external-contents\": \"$output_path\",\"name\": \"$GEN_DIR\",\"type\": \"directory-remap\"}],\"version\": 0}\nEOF\n\ncd \"$BUILD_DIR\"\n\nrm -rf external\nrm -rf bazel-exec-root\n\nln -sf \"$external\" external\nln -sf \"$exec_root\" bazel-exec-root\nln -sfn \"$PROJECT_DIR\" SRCROOT\n\ncd \"$SRCROOT\"\n\nif [ \"$ACTION\" == \"indexbuild\" ]; then\n index_flags=(\n --bes_backend=\n --bes_results_url=\n )\nelif [ \"${ENABLE_PREVIEWS:-}\" == \"YES\" ]; then\n swiftui_previews_flags=(\n --swiftcopt=-Xfrontend\n --swiftcopt=-enable-implicit-dynamic\n --swiftcopt=-Xfrontend\n --swiftcopt=-enable-private-imports\n --swiftcopt=-Xfrontend\n --swiftcopt=-enable-dynamic-replacement-chaining\n )\nfi\n\ndate +%s > \"$INTERNAL_DIR/toplevel_cache_buster\"\n\nbuild_marker=\"$OBJROOT/bazel_build_start\"\ntouch \"$build_marker\"\n\nlog=$(mktemp)\nenv -i \\\n DEVELOPER_DIR=\"$DEVELOPER_DIR\" \\\n HOME=\"$HOME\" \\\n PATH=\"/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin\" \\\n USER=\"$USER\" \\\n \"$BAZEL_PATH\" \\\n ${output_base:+--output_base \"$output_base\"} \\\n build \\\n --color=\"$color\" \\\n --experimental_convenience_symlinks=ignore \\\n --symlink_prefix=/ \\\n ${index_flags:+${index_flags[*]}} \\\n ${swiftui_previews_flags:+${swiftui_previews_flags[*]}} \\\n \"$output_groups_flag\" \\\n //test/fixtures/cc:xcodeproj_bwb \\\n 2>&1 | tee -i \"$log\"\n\nfor output_group in \"${output_groups[@]}\"; do\n filelist=\"xcodeproj_bwb-${output_group//\\//_}\"\n filelist=\"${filelist/#/$output_path/darwin_x86_64-fastbuild-ST-d53d69b6b8c1/bin/test/fixtures/cc/}\"\n filelist=\"${filelist/%/.filelist}\"\n if [[ \"$filelist\" -ot \"$build_marker\" ]]; then\n echo \"error: Bazel didn't generate the correct files (it should have generated outputs for output group \\\"$output_group\\\", but the timestamp for \\\"$filelist\\\" was from before the build). Please regenerate the project to fix this.\" >&2\n echo \"error: If your bazel version is less than 5.2, you may need to \\`bazel clean\\` and/or \\`bazel shutdown\\` to work around a bug in project generation.\" >&2\n echo \"error: If you are still getting this error after all of that, please file a bug report here: https://github.com/buildbuddy-io/rules_xcodeproj/issues/new?template=bug.md.\" >&2\n exit 1\n fi\ndone\n";
showEnvVarsInLog = 0;
};
DC5C20F1CBE45FA898D3AC86 /* Copy Bazel Outputs */ = {
Expand Down Expand Up @@ -525,6 +525,8 @@
);
MACOSX_DEPLOYMENT_TARGET = 11.0;
OTHER_CFLAGS = (
"-ivfsoverlay",
"$(BUILD_DIR)/gen_dir-overlay.yaml",
"-fstack-protector",
"-Wall",
"-Wthread-safety",
Expand All @@ -536,6 +538,8 @@
"-Wno-builtin-macro-redefined",
);
OTHER_CPLUSPLUSFLAGS = (
"-ivfsoverlay",
"$(BUILD_DIR)/gen_dir-overlay.yaml",
"-fstack-protector",
"-Wall",
"-Wthread-safety",
Expand Down Expand Up @@ -586,6 +590,8 @@
);
MACOSX_DEPLOYMENT_TARGET = 11.0;
OTHER_CFLAGS = (
"-ivfsoverlay",
"$(BUILD_DIR)/gen_dir-overlay.yaml",
"-fstack-protector",
"-Wall",
"-Wthread-safety",
Expand All @@ -597,6 +603,8 @@
"-Wno-builtin-macro-redefined",
);
OTHER_CPLUSPLUSFLAGS = (
"-ivfsoverlay",
"$(BUILD_DIR)/gen_dir-overlay.yaml",
"-fstack-protector",
"-Wall",
"-Wthread-safety",
Expand Down Expand Up @@ -657,6 +665,8 @@
LINK_PARAMS_FILE = "$(INTERNAL_DIR)/targets/darwin_x86_64-dbg-ST-5534cb307cb8/examples/cc/tool/tool.link.params";
MACOSX_DEPLOYMENT_TARGET = 11.0;
OTHER_CFLAGS = (
"-ivfsoverlay",
"$(BUILD_DIR)/gen_dir-overlay.yaml",
"-fstack-protector",
"-Wall",
"-Wthread-safety",
Expand All @@ -668,6 +678,8 @@
"-Wno-builtin-macro-redefined",
);
OTHER_CPLUSPLUSFLAGS = (
"-ivfsoverlay",
"$(BUILD_DIR)/gen_dir-overlay.yaml",
"-fstack-protector",
"-Wall",
"-Wthread-safety",
Expand Down Expand Up @@ -719,6 +731,8 @@
);
MACOSX_DEPLOYMENT_TARGET = 11.0;
OTHER_CFLAGS = (
"-ivfsoverlay",
"$(BUILD_DIR)/gen_dir-overlay.yaml",
"-fstack-protector",
"-Wall",
"-Wthread-safety",
Expand All @@ -730,6 +744,8 @@
"-Wno-builtin-macro-redefined",
);
OTHER_CPLUSPLUSFLAGS = (
"-ivfsoverlay",
"$(BUILD_DIR)/gen_dir-overlay.yaml",
"-fstack-protector",
"-Wall",
"-Wthread-safety",
Expand Down
Loading

0 comments on commit 5ff032c

Please sign in to comment.