From 68e05cfc9e677e8fd049391887062e24afd985b5 Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Thu, 7 Nov 2019 15:06:18 +0100 Subject: [PATCH 1/6] Allow specifying a custom generated header name --- swift/internal/api.bzl | 10 ++++++++++ swift/internal/derived_files.bzl | 5 +++-- swift/internal/swift_binary_test.bzl | 1 + swift/internal/swift_library.bzl | 1 + swift/internal/swift_module_alias.bzl | 1 + 5 files changed, 16 insertions(+), 2 deletions(-) diff --git a/swift/internal/api.bzl b/swift/internal/api.bzl index d245ffb5f..e54e9039c 100644 --- a/swift/internal/api.bzl +++ b/swift/internal/api.bzl @@ -241,6 +241,12 @@ Each string is prepended with `-D` and added to the command line. Unlike it, so use this attribute with caution. It is preferred that you add defines directly to `copts`, only using this feature in the rare case that a library needs to propagate a symbol up to those that depend on it. +""", + ), + "generated_header_name": attr.string( + doc = """\ +The name of the generated Objective-C interface header. +If not provided, defaults to `${target_name}-Swift.h`. """, ), "module_name": attr.string( @@ -498,6 +504,7 @@ def _compile( copts = [], defines = [], deps = [], + generated_header_name = None, genfiles_dir = None): """Compiles a Swift module. @@ -530,6 +537,8 @@ def _compile( deps: Dependencies of the target being compiled. These targets must propagate one of the following providers: `CcInfo`, `SwiftInfo`, or `apple_common.Objc`. + generated_header_name: The name of the generated Objective-C interface header. + If not provided, defaults to `${target_name}-Swift.h`. genfiles_dir: The Bazel `*-genfiles` directory root. If provided, its path is added to ClangImporter's header search paths for compatibility with Bazel's C++ and Objective-C rules which support @@ -705,6 +714,7 @@ def _compile( generated_header = derived_files.objc_header( actions = actions, target_name = target_name, + header_name = generated_header_name, ) args.add("-emit-objc-header-path") args.add(generated_header) diff --git a/swift/internal/derived_files.bzl b/swift/internal/derived_files.bzl index 5da4b0a3d..c3e3f496c 100644 --- a/swift/internal/derived_files.bzl +++ b/swift/internal/derived_files.bzl @@ -122,7 +122,7 @@ def _modulewrap_object(actions, target_name): """ return actions.declare_file("{}.modulewrap.o".format(target_name)) -def _objc_header(actions, target_name): +def _objc_header(actions, target_name, header_name = None): """Declares the generated header file exposing Swift APIs to Objective-C. Args: @@ -132,7 +132,8 @@ def _objc_header(actions, target_name): Returns: The declared `File`. """ - return actions.declare_file("{}-Swift.h".format(target_name)) + if not header_name: header_name = "{}-Swift.h".format(target_name) + return actions.declare_file(header_name) def _partial_swiftmodule(actions, target_name, src): """Declares a file for a partial Swift module created during compilation. diff --git a/swift/internal/swift_binary_test.bzl b/swift/internal/swift_binary_test.bzl index 4c3bf2f42..bbe57bb7c 100644 --- a/swift/internal/swift_binary_test.bzl +++ b/swift/internal/swift_binary_test.bzl @@ -183,6 +183,7 @@ def _swift_linking_rule_impl( defines = ctx.attr.defines, deps = ctx.attr.deps, feature_configuration = feature_configuration, + generated_header_name = ctx.attr.generated_header_name, genfiles_dir = ctx.genfiles_dir, module_name = module_name, srcs = srcs, diff --git a/swift/internal/swift_library.bzl b/swift/internal/swift_library.bzl index c3761c4f0..78df113a0 100644 --- a/swift/internal/swift_library.bzl +++ b/swift/internal/swift_library.bzl @@ -157,6 +157,7 @@ def _swift_library_impl(ctx): defines = ctx.attr.defines, deps = deps + private_deps, feature_configuration = feature_configuration, + generated_header_name = ctx.attr.generated_header_name, genfiles_dir = ctx.genfiles_dir, module_name = module_name, srcs = srcs, diff --git a/swift/internal/swift_module_alias.bzl b/swift/internal/swift_module_alias.bzl index 269053d12..c22c2d873 100644 --- a/swift/internal/swift_module_alias.bzl +++ b/swift/internal/swift_module_alias.bzl @@ -65,6 +65,7 @@ def _swift_module_alias_impl(ctx): copts = ["-parse-as-library"], deps = deps, feature_configuration = feature_configuration, + generated_header_name = ctx.attr.generated_header_name, genfiles_dir = ctx.genfiles_dir, module_name = module_name, srcs = [reexport_src], From 9a06196153971437eddd9886f88b19c0803220d0 Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Thu, 7 Nov 2019 15:19:11 +0100 Subject: [PATCH 2/6] Add missing arg doc --- swift/internal/derived_files.bzl | 1 + 1 file changed, 1 insertion(+) diff --git a/swift/internal/derived_files.bzl b/swift/internal/derived_files.bzl index c3e3f496c..1a1e14e31 100644 --- a/swift/internal/derived_files.bzl +++ b/swift/internal/derived_files.bzl @@ -128,6 +128,7 @@ def _objc_header(actions, target_name, header_name = None): Args: actions: The context's actions object. target_name: The name of the target being built. + header_name: The name of the header being declared. Returns: The declared `File`. From 46893d27760549d5a76e232f9dbe0bf462204c32 Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Mon, 18 Nov 2019 11:57:10 -0800 Subject: [PATCH 3/6] Apply suggestions from code review Co-Authored-By: Tony Allevato --- swift/internal/api.bzl | 4 ++-- swift/internal/derived_files.bzl | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/swift/internal/api.bzl b/swift/internal/api.bzl index e54e9039c..4d481494a 100644 --- a/swift/internal/api.bzl +++ b/swift/internal/api.bzl @@ -246,7 +246,7 @@ needs to propagate a symbol up to those that depend on it. "generated_header_name": attr.string( doc = """\ The name of the generated Objective-C interface header. -If not provided, defaults to `${target_name}-Swift.h`. +If not provided, it defaults to `${target_name}-Swift.h`. """, ), "module_name": attr.string( @@ -538,7 +538,7 @@ def _compile( propagate one of the following providers: `CcInfo`, `SwiftInfo`, or `apple_common.Objc`. generated_header_name: The name of the generated Objective-C interface header. - If not provided, defaults to `${target_name}-Swift.h`. + If not provided, it defaults to `${target_name}-Swift.h`. genfiles_dir: The Bazel `*-genfiles` directory root. If provided, its path is added to ClangImporter's header search paths for compatibility with Bazel's C++ and Objective-C rules which support diff --git a/swift/internal/derived_files.bzl b/swift/internal/derived_files.bzl index 1a1e14e31..937a7c082 100644 --- a/swift/internal/derived_files.bzl +++ b/swift/internal/derived_files.bzl @@ -133,7 +133,9 @@ def _objc_header(actions, target_name, header_name = None): Returns: The declared `File`. """ - if not header_name: header_name = "{}-Swift.h".format(target_name) + if not header_name: + header_name = "{}-Swift.h".format(target_name) + return actions.declare_file(header_name) def _partial_swiftmodule(actions, target_name, src): From 46983e955b0ca438f9ad2fe3cbd7cae867c74310 Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Mon, 18 Nov 2019 12:06:14 -0800 Subject: [PATCH 4/6] Only allow generated_header_name in swift_library attrs --- swift/internal/api.bzl | 12 ++++++------ swift/internal/swift_binary_test.bzl | 1 - swift/internal/swift_module_alias.bzl | 1 - 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/swift/internal/api.bzl b/swift/internal/api.bzl index 4d481494a..f2d565d19 100644 --- a/swift/internal/api.bzl +++ b/swift/internal/api.bzl @@ -241,12 +241,6 @@ Each string is prepended with `-D` and added to the command line. Unlike it, so use this attribute with caution. It is preferred that you add defines directly to `copts`, only using this feature in the rare case that a library needs to propagate a symbol up to those that depend on it. -""", - ), - "generated_header_name": attr.string( - doc = """\ -The name of the generated Objective-C interface header. -If not provided, it defaults to `${target_name}-Swift.h`. """, ), "module_name": attr.string( @@ -1045,6 +1039,12 @@ explicitly called by code in the binary; for example, if you rely on runtime checks for protocol conformances added in extensions in the library but do not directly reference any other symbols in the object file that adds that conformance. +""", + ), + "generated_header_name": attr.string( + doc = """\ +The name of the generated Objective-C interface header. +If not provided, it defaults to `${target_name}-Swift.h`. """, ), }, diff --git a/swift/internal/swift_binary_test.bzl b/swift/internal/swift_binary_test.bzl index bbe57bb7c..4c3bf2f42 100644 --- a/swift/internal/swift_binary_test.bzl +++ b/swift/internal/swift_binary_test.bzl @@ -183,7 +183,6 @@ def _swift_linking_rule_impl( defines = ctx.attr.defines, deps = ctx.attr.deps, feature_configuration = feature_configuration, - generated_header_name = ctx.attr.generated_header_name, genfiles_dir = ctx.genfiles_dir, module_name = module_name, srcs = srcs, diff --git a/swift/internal/swift_module_alias.bzl b/swift/internal/swift_module_alias.bzl index c22c2d873..269053d12 100644 --- a/swift/internal/swift_module_alias.bzl +++ b/swift/internal/swift_module_alias.bzl @@ -65,7 +65,6 @@ def _swift_module_alias_impl(ctx): copts = ["-parse-as-library"], deps = deps, feature_configuration = feature_configuration, - generated_header_name = ctx.attr.generated_header_name, genfiles_dir = ctx.genfiles_dir, module_name = module_name, srcs = [reexport_src], From 3b51def41c6f18147b24a17ed33869330edaa524 Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Tue, 3 Dec 2019 10:24:36 -0800 Subject: [PATCH 5/6] Validate given objc header name --- swift/internal/derived_files.bzl | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/swift/internal/derived_files.bzl b/swift/internal/derived_files.bzl index 937a7c082..7d3461eab 100644 --- a/swift/internal/derived_files.bzl +++ b/swift/internal/derived_files.bzl @@ -133,8 +133,15 @@ def _objc_header(actions, target_name, header_name = None): Returns: The declared `File`. """ - if not header_name: - header_name = "{}-Swift.h".format(target_name) + if header_name: + if not header_name.endswith(".h"): + fail(("The generated objc header name for {} must end in" + + " '.h', given '{}'") % (target_name, header_name)) + if header_name.find("/"): + fail(("The generated objc header name for {} cannot contain a " + + "'/', given '{}'") % (target_name, header_name)) + else: + header_name = "{}-Swift.h".format(target_name) return actions.declare_file(header_name) From 2189f3ef6480bf5fdaa4b1f5eb972502062bc269 Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Mon, 13 Jan 2020 10:11:47 -0800 Subject: [PATCH 6/6] Fix check for slash in header name Co-Authored-By: Thi --- swift/internal/derived_files.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swift/internal/derived_files.bzl b/swift/internal/derived_files.bzl index 7d3461eab..1c2ed557f 100644 --- a/swift/internal/derived_files.bzl +++ b/swift/internal/derived_files.bzl @@ -137,7 +137,7 @@ def _objc_header(actions, target_name, header_name = None): if not header_name.endswith(".h"): fail(("The generated objc header name for {} must end in" + " '.h', given '{}'") % (target_name, header_name)) - if header_name.find("/"): + if "/" in header_name: fail(("The generated objc header name for {} cannot contain a " + "'/', given '{}'") % (target_name, header_name)) else: