Skip to content

Commit 941188c

Browse files
committed
Add genfiles root to ClangImporter header search paths.
Bazel's Objective-C and C++ rules automatically add the genfiles root to their header search paths, so other rule authors or genrule users may write generated headers in that location and Objc/C++ compilations will pick them up. This change allows Swift to import such libraries as well; previously, ClangImporter would fail because generated headers would not be found. RELNOTES: ClangImporter can now find generated headers rooted in genfiles. PiperOrigin-RevId: 206179825
1 parent 5f70331 commit 941188c

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

swift/internal/api.bzl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ def _compile_as_objects(
351351
defines = [],
352352
deps = [],
353353
features = [],
354+
genfiles_dir = None,
354355
objc_fragment = None):
355356
"""Compiles Swift source files into object files (and optionally a module).
356357
@@ -389,6 +390,10 @@ def _compile_as_objects(
389390
propagate one of the following providers: `SwiftClangModuleInfo`,
390391
`SwiftInfo`, `"cc"`, or `apple_common.Objc`.
391392
features: Features that are enabled on the target being compiled.
393+
genfiles_dir: The Bazel `*-genfiles` directory root. If provided, its path
394+
is added to ClangImporter's header search paths for compatibility with
395+
Bazel's C++ and Objective-C rules which support inclusions of generated
396+
headers from that location.
392397
objc_fragment: The `objc` configuration fragment from Bazel. This must be
393398
provided if the toolchain supports Objective-C interop; if it does not,
394399
then this argument may be omitted.
@@ -453,6 +458,7 @@ def _compile_as_objects(
453458
defines = defines,
454459
deps = deps,
455460
features = features,
461+
genfiles_dir = genfiles_dir,
456462
objc_fragment = objc_fragment,
457463
)
458464

@@ -531,6 +537,7 @@ def _compile_as_library(
531537
defines = [],
532538
deps = [],
533539
features = [],
540+
genfiles_dir = None,
534541
library_name = None,
535542
linkopts = [],
536543
objc_fragment = None):
@@ -574,6 +581,10 @@ def _compile_as_library(
574581
propagate one of the following providers: `SwiftClangModuleInfo`,
575582
`SwiftInfo`, `"cc"`, or `apple_common.Objc`.
576583
features: Features that are enabled on the target being compiled.
584+
genfiles_dir: The Bazel `*-genfiles` directory root. If provided, its path
585+
is added to ClangImporter's header search paths for compatibility with
586+
Bazel's C++ and Objective-C rules which support inclusions of generated
587+
headers from that location.
577588
library_name: The name that should be substituted for the string `{name}` in
578589
`lib{name}.a`, which will be the output of this compilation. If this is
579590
not specified or is falsy, then the default behavior is to simply use
@@ -686,6 +697,7 @@ def _compile_as_library(
686697
allow_testing = allow_testing,
687698
configuration = configuration,
688699
deps = deps,
700+
genfiles_dir = genfiles_dir,
689701
objc_fragment = objc_fragment,
690702
)
691703

@@ -960,6 +972,7 @@ def _swiftc_command_line_and_inputs(
960972
defines = [],
961973
deps = [],
962974
features = [],
975+
genfiles_dir = None,
963976
objc_fragment = None):
964977
"""Computes command line arguments and inputs needed to invoke `swiftc`.
965978
@@ -1001,6 +1014,10 @@ def _swiftc_command_line_and_inputs(
10011014
propagate one of the following providers: `SwiftClangModuleInfo`,
10021015
`SwiftInfo`, `"cc"`, or `apple_common.Objc`.
10031016
features: Features that are enabled on the target being compiled.
1017+
genfiles_dir: The Bazel `*-genfiles` directory root. If provided, its path
1018+
is added to ClangImporter's header search paths for compatibility with
1019+
Bazel's C++ and Objective-C rules which support inclusions of generated
1020+
headers from that location.
10041021
objc_fragment: The `objc` configuration fragment from Bazel. This must be
10051022
provided if the toolchain supports Objective-C interop; if it does not,
10061023
then this argument may be omitted.
@@ -1023,6 +1040,11 @@ def _swiftc_command_line_and_inputs(
10231040
args.add_all(_coverage_copts(configuration = configuration))
10241041
args.add_all(_sanitizer_copts(features = features))
10251042

1043+
# Add the genfiles directory to ClangImporter's header search paths for
1044+
# compatibility with rules that generate headers there.
1045+
if genfiles_dir:
1046+
args.add_all(["-iquote", genfiles_dir.path], before_each = "-Xcc")
1047+
10261048
input_depsets = list(additional_input_depsets)
10271049
transitive_inputs = collect_transitive_compile_inputs(
10281050
args = args,

swift/internal/swift_library.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def _swift_library_impl(ctx):
5454
defines = ctx.attr.defines,
5555
deps = ctx.attr.deps,
5656
features = ctx.attr.features,
57+
genfiles_dir = ctx.genfiles_dir,
5758
library_name = library_name,
5859
linkopts = linkopts,
5960
objc_fragment = objc_fragment,

swift/internal/swift_module_alias.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ following dependencies instead:\n\n""".format(
6464
toolchain = ctx.attr._toolchain[SwiftToolchainInfo],
6565
configuration = ctx.configuration,
6666
deps = ctx.attr.deps,
67+
genfiles_dir = ctx.genfiles_dir,
6768
features = ctx.attr.features,
6869
)
6970

swift/internal/swift_protoc_gen_aspect.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ def _swift_protoc_gen_aspect_impl(target, aspect_ctx):
418418
allow_testing = False,
419419
configuration = aspect_ctx.configuration,
420420
deps = compile_deps,
421+
genfiles_dir = aspect_ctx.genfiles_dir,
421422
# Prevent conflicts with C++ protos in the same output directory, which
422423
# use the `lib{name}.a` pattern. This will produce `lib{name}.swift.a`
423424
# instead.

0 commit comments

Comments
 (0)