Skip to content

Commit

Permalink
Persist pch files in output dir (bazelbuild#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrymarino authored Oct 19, 2021
1 parent 1820910 commit 36f2c80
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
28 changes: 28 additions & 0 deletions swift/internal/compiling.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ load(
"SWIFT_FEATURE_USE_C_MODULES",
"SWIFT_FEATURE_USE_GLOBAL_INDEX_STORE",
"SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE",
"SWIFT_FEATURE_USE_PCH_OUTPUT_DIR",
"SWIFT_FEATURE_VFSOVERLAY",
"SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS",
"SWIFT_FEATURE__WMO_IN_SWIFTCOPTS",
Expand Down Expand Up @@ -572,6 +573,18 @@ def compile_action_configs(
[SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE],
],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
swift_action_names.DUMP_AST,
],
configurators = [_pch_output_dir_configurator],
features = [
SWIFT_FEATURE_USE_PCH_OUTPUT_DIR,
],
),

# When using C modules, disable the implicit search for module map files
# because all of them, including system dependencies, will be provided
# explicitly.
Expand Down Expand Up @@ -1491,6 +1504,21 @@ def _index_while_building_configurator(prerequisites, args):
if not _index_store_path_overridden(prerequisites.user_compile_flags):
args.add("-index-store-path", prerequisites.indexstore_directory.path)

def _pch_output_dir_configurator(prerequisites, args):
"""Adds flags for pch-output-dir configuration to the command line.
This is a directory to persist automatically created precompiled bridging headers
Note: that like the global index store and module cache, we expect clang
to namespace these correctly per arch / os version / etc by the hash in
the path. However, it is also put into the bin_dir for an added layer of
safety.
"""
args.add(
"-pch-output-dir",
paths.join(prerequisites.bin_dir.path, "_pch_output_dir"),
)

def _global_index_store_configurator(prerequisites, args):
"""Adds flags for index-store generation to the command line."""
out_dir = prerequisites.indexstore_directory.dirname.split("/")[0]
Expand Down
4 changes: 4 additions & 0 deletions swift/internal/feature_names.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,7 @@ SWIFT_FEATURE__WMO_IN_SWIFTCOPTS = "swift._wmo_in_swiftcopts"
# were passed on the command line using `--swiftcopt`. Users should never
# manually enable, disable, or query this feature.
SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS = "swift._num_threads_0_in_swiftcopts"

# A feature to enable setting pch-output-dir
# This is a directory to persist automatically created precompiled bridging headers
SWIFT_FEATURE_USE_PCH_OUTPUT_DIR = "swift.use_pch_output_dir"
3 changes: 3 additions & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ load(":output_file_map_tests.bzl", "output_file_map_test_suite")
load(":private_deps_tests.bzl", "private_deps_test_suite")
load(":swift_through_non_swift_tests.bzl", "swift_through_non_swift_test_suite")
load(":split_derived_files_tests.bzl", "split_derived_files_test_suite")
load(":pch_output_dir_tests.bzl", "pch_output_dir_test_suite")

licenses(["notice"])

Expand All @@ -29,6 +30,8 @@ swift_through_non_swift_test_suite()

split_derived_files_test_suite()

pch_output_dir_test_suite()

test_suite(
name = "all_tests",
)
Expand Down
54 changes: 54 additions & 0 deletions test/pch_output_dir_tests.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2021 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for pch output dir command line flags"""

load(
"@build_bazel_rules_swift//test/rules:action_command_line_test.bzl",
"make_action_command_line_test_rule",
)

pch_output_dir_action_command_line_test = make_action_command_line_test_rule(
config_settings = {
"//command_line_option:features": [
"swift.use_pch_output_dir",
],
},
)

def pch_output_dir_test_suite(name = "pch_output_dir_settings"):
"""Test suite for pch output dir options.
Args:
name: The name prefix for all the nested tests
"""

# Verify that a pch dir is passed
pch_output_dir_action_command_line_test(
name = "{}_pch_output_dir".format(name),
expected_argv = [
"-pch-output-dir",
# Starlark doesn't have support for regular expression yet, so we
# can't verify the whole argument here. It has the configuration
# fragment baked in.
],
mnemonic = "SwiftCompile",
tags = [name],
target_under_test = "@build_bazel_rules_swift//test/fixtures/debug_settings:simple",
)

native.test_suite(
name = name,
tags = [name],
)

0 comments on commit 36f2c80

Please sign in to comment.