diff --git a/.bazelversion b/.bazelversion index 40c341bdcd..ee74734aa2 100644 --- a/.bazelversion +++ b/.bazelversion @@ -1 +1 @@ -3.6.0 +4.1.0 diff --git a/bazel/apple_test.bzl b/bazel/apple_test.bzl index 1cd482f4be..e392604b79 100644 --- a/bazel/apple_test.bzl +++ b/bazel/apple_test.bzl @@ -25,7 +25,7 @@ def envoy_mobile_swift_test(name, srcs, data = [], deps = [], repository = ""): srcs = srcs, data = data, deps = [ - repository + "//library/swift:ios_framework_archive", + repository + "//library/swift:ios_lib", ] + deps, linkopts = ["-lresolv.9"], visibility = ["//visibility:private"], diff --git a/bazel/envoy_mobile_repositories.bzl b/bazel/envoy_mobile_repositories.bzl index 19dcea8efb..c34e0cc5c3 100644 --- a/bazel/envoy_mobile_repositories.bzl +++ b/bazel/envoy_mobile_repositories.bzl @@ -15,24 +15,6 @@ def envoy_mobile_repositories(): python_repos() def upstream_envoy_overrides(): - # Patch protobuf to prevent duplicate symbols: https://github.com/lyft/envoy-mobile/issues/617 - # More details: https://github.com/protocolbuffers/protobuf/issues/7046 - # TODO: Remove after https://github.com/bazelbuild/bazel/pull/10493 is merged to Bazel - # Reverts: - # - https://github.com/protocolbuffers/protobuf/commit/7b28278c7d4f4175e70aef2f89d304696eb85ae3 - # - https://github.com/protocolbuffers/protobuf/commit/a03d332aca5d33c5d4b2cd25037c9e37d57eff02 - http_archive( - name = "com_google_protobuf", - patch_args = ["-p1"], - patches = [ - "@envoy//bazel:protobuf.patch", - "@envoy_mobile//bazel:protobuf.patch", - ], - sha256 = "d7371dc2d46fddac1af8cb27c0394554b068768fc79ecaf5be1a1863e8ff3392", - strip_prefix = "protobuf-3.16.0", - urls = ["https://github.com/protocolbuffers/protobuf/releases/download/v3.16.0/protobuf-all-3.16.0.tar.gz"], - ) - # Workaround old NDK version breakages https://github.com/lyft/envoy-mobile/issues/934 http_archive( name = "com_github_libevent_libevent", @@ -78,14 +60,15 @@ def upstream_envoy_overrides(): def swift_repos(): http_archive( name = "build_bazel_rules_apple", - sha256 = "55f4dc1c9bf21bb87442665f4618cff1f1343537a2bd89252078b987dcd9c382", - url = "https://github.com/bazelbuild/rules_apple/releases/download/0.20.0/rules_apple.0.20.0.tar.gz", + sha256 = "747d30a8d96e0f4d093c55ebcdc07ac5ef2529c7aa5c41b45788a36ca3a4cb05", + strip_prefix = "rules_apple-8b42998e2086325c3290f4e68752a50cf077fb92", + url = "https://github.com/bazelbuild/rules_apple/archive/8b42998e2086325c3290f4e68752a50cf077fb92.tar.gz", ) http_archive( name = "build_bazel_rules_swift", - sha256 = "cea22c0616d797e494d7844a9b604520c87f53c81de49613a7e679ec5b821620", - url = "https://github.com/bazelbuild/rules_swift/releases/download/0.14.0/rules_swift.0.14.0.tar.gz", + sha256 = "a228a8e41fdc165a2c55924b728c466e0086f3e638a05d6da98aa6222cbb19c1", + url = "https://github.com/bazelbuild/rules_swift/releases/download/0.16.1/rules_swift.0.16.1.tar.gz", ) def kotlin_repos(): diff --git a/bazel/swift_header_collector.bzl b/bazel/swift_header_collector.bzl new file mode 100644 index 0000000000..61b75ed145 --- /dev/null +++ b/bazel/swift_header_collector.bzl @@ -0,0 +1,27 @@ +""" +Propagate the generated Swift header from a swift_library target +This exists to work around https://github.com/bazelbuild/rules_swift/issues/291 +""" + +def _swift_header_collector(ctx): + headers = [ + DefaultInfo( + files = ctx.attr.library[CcInfo].compilation_context.headers, + ), + ] + + if len(headers[0].files.to_list()) != 1: + header_names = [header.basename for header in headers[0].files.to_list()] + fail("Expected exactly 1 '-Swift.h' header, got {}".format(header_names)) + + return headers + +swift_header_collector = rule( + attrs = dict( + library = attr.label( + mandatory = True, + providers = [CcInfo], + ), + ), + implementation = _swift_header_collector, +) diff --git a/bazel/swift_static_framework.bzl b/bazel/swift_static_framework.bzl deleted file mode 100644 index 2ccec84f49..0000000000 --- a/bazel/swift_static_framework.bzl +++ /dev/null @@ -1,218 +0,0 @@ -""" -This rules creates a fat static framework that can be included later with -static_framework_import -""" - -load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "CPP_LINK_STATIC_LIBRARY_ACTION_NAME") -load("@build_bazel_apple_support//lib:apple_support.bzl", "apple_support") -load("@build_bazel_rules_swift//swift:swift.bzl", "SwiftInfo", "swift_library") -load("@build_bazel_rules_apple//apple/internal:transition_support.bzl", "transition_support") - -MINIMUM_IOS_VERSION = "11.0" - -_PLATFORM_TO_SWIFTMODULE = { - "ios_armv7": "arm", - "ios_arm64": "arm64", - "ios_i386": "i386", - "ios_x86_64": "x86_64", -} - -def _zip_binary_arg(module_name, input_file): - return "{module_name}.framework/{module_name}={file_path}".format( - module_name = module_name, - file_path = input_file.path, - ) - -def _zip_header_arg(module_name, input_file): - return "{module_name}.framework/Headers/{module_name}-Swift.h={file_path}".format( - module_name = module_name, - file_path = input_file.path, - ) - -def _zip_swift_arg(module_name, swift_identifier, input_file): - return "{module_name}.framework/Modules/{module_name}.swiftmodule/{swift_identifier}.{ext}={file_path}".format( - module_name = module_name, - swift_identifier = swift_identifier, - ext = input_file.extension, - file_path = input_file.path, - ) - -def _swift_static_framework_impl(ctx): - module_name = ctx.attr.framework_name - fat_file = ctx.outputs.fat_file - - input_archives = [] - input_modules_docs = [] - zip_args = [_zip_binary_arg(module_name, fat_file)] - - for platform, archive in ctx.split_attr.archive.items(): - swiftmodule_identifier = _PLATFORM_TO_SWIFTMODULE[platform] - if not swiftmodule_identifier: - fail("Unhandled platform '{}'".format(platform)) - - swift_info = archive[SwiftInfo] - - # We can potentially simplify this if this change lands upstream: - # https://github.com/bazelbuild/rules_swift/issues/291 - objc_headers = [ - header - for header in archive[apple_common.Objc].header.to_list() - if header.path.endswith("-Swift.h") - ] - - if len(objc_headers) == 1: - input_modules_docs.append(objc_headers[0]) - zip_args.append(_zip_header_arg(module_name, objc_headers[0])) - else: - header_names = [header.basename for header in objc_headers] - fail("Expected exactly 1 '-Swift.h' header, got {}".format(", ".join(header_names))) - - swiftdoc = swift_info.direct_swiftdocs[0] - swiftmodule = swift_info.direct_swiftmodules[0] - swiftinterfaces = swift_info.transitive_swiftinterfaces.to_list() - if len(swiftinterfaces) != 1: - fail("Expected a single swiftinterface file, got: {}".format(swiftinterfaces)) - swiftinterface = swiftinterfaces[0] - - libraries = archive[CcInfo].linking_context.libraries_to_link - archives = [] - for library in libraries.to_list(): - archive = library.pic_static_library or library.static_library - if archive: - archives.append(archive) - else: - fail("All linked dependencies must be static") - - platform_archive = ctx.actions.declare_file("{}.{}.a".format(module_name, platform)) - - libtool_args = ["-no_warning_for_no_symbols", "-static", "-syslibroot", "__BAZEL_XCODE_SDKROOT__", "-o", platform_archive.path] + [x.path for x in archives] - cc_toolchain = ctx.attr._cc_toolchain[cc_common.CcToolchainInfo] - cc_feature_configuration = cc_common.configure_features( - ctx = ctx, - cc_toolchain = cc_toolchain, - requested_features = ctx.features, - unsupported_features = ctx.disabled_features, - ) - archiver = cc_common.get_tool_for_action( - action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME, - feature_configuration = cc_feature_configuration, - ) - apple_support.run( - ctx, - inputs = depset(direct = archives, transitive = [ctx.attr._cc_toolchain.files]), - outputs = [platform_archive], - mnemonic = "LibtoolLinkedLibraries", - progress_message = "Combining libraries for {} on {}".format(module_name, platform), - executable = archiver, - arguments = libtool_args, - ) - - input_archives.append(platform_archive) - - input_modules_docs += [swiftdoc, swiftmodule, swiftinterface] - zip_args += [ - _zip_swift_arg(module_name, swiftmodule_identifier, swiftdoc), - _zip_swift_arg(module_name, swiftmodule_identifier, swiftmodule), - _zip_swift_arg(module_name, swiftmodule_identifier, swiftinterface), - ] - - ctx.actions.run( - inputs = input_archives, - outputs = [fat_file], - mnemonic = "LipoPlatformLibraries", - progress_message = "Creating fat library for {}".format(module_name), - executable = "lipo", - arguments = ["-create", "-output", fat_file.path] + [x.path for x in input_archives], - ) - - output_file = ctx.outputs.output_file - ctx.actions.run( - inputs = input_modules_docs + [fat_file], - outputs = [output_file], - mnemonic = "CreateFrameworkZip", - progress_message = "Creating framework zip for {}".format(module_name), - executable = ctx.executable._zipper, - arguments = ["c", output_file.path] + zip_args, - ) - - return [ - DefaultInfo( - files = depset([output_file]), - ), - ] - -_swift_static_framework = rule( - attrs = dict( - apple_support.action_required_attrs(), - _cc_toolchain = attr.label( - default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"), - ), - _allowlist_function_transition = attr.label( - default = "@bazel_tools//tools/allowlists/function_transition_allowlist", - ), - _zipper = attr.label( - default = "@bazel_tools//tools/zip:zipper", - cfg = "host", - executable = True, - ), - archive = attr.label( - mandatory = True, - providers = [ - CcInfo, - SwiftInfo, - ], - cfg = apple_common.multi_arch_split, - ), - framework_name = attr.string(mandatory = True), - minimum_os_version = attr.string(default = MINIMUM_IOS_VERSION), - platform_type = attr.string( - default = str(apple_common.platform_type.ios), - ), - ), - cfg = transition_support.static_framework_transition, - fragments = [ - "apple", - "cpp", - ], - outputs = { - "fat_file": "%{framework_name}.fat", - "output_file": "%{framework_name}.zip", - }, - implementation = _swift_static_framework_impl, -) - -def swift_static_framework( - name, - module_name = None, - srcs = [], - private_deps = [], - copts = [], - visibility = []): - """Create a static library, and static framework target for a swift module - - Args: - name: The name of the module, the framework's name will be this name - appending Framework so you can depend on this from other modules - srcs: Custom source paths for the swift files - copts: Any custom swiftc opts passed through to the swift_library - private_deps: Any deps the swift_library requires. They must be imported - with @_implementationOnly and not exposed publicly. - """ - archive_name = name + "_archive" - module_name = module_name or name + "_framework" - swift_library( - name = archive_name, - srcs = srcs, - copts = copts, - module_name = module_name, - private_deps = private_deps, - visibility = ["//visibility:public"], - features = ["swift.enable_library_evolution"], - ) - - _swift_static_framework( - name = name, - archive = archive_name, - framework_name = module_name, - visibility = visibility, - ) diff --git a/envoy-mobile.tulsiproj/Configs/all.tulsigen b/envoy-mobile.tulsiproj/Configs/all.tulsigen index 61629d71cd..164ebe87b6 100644 --- a/envoy-mobile.tulsiproj/Configs/all.tulsigen +++ b/envoy-mobile.tulsiproj/Configs/all.tulsigen @@ -8,7 +8,7 @@ "//examples/objective-c/hello_world:appmain", "//examples/swift/hello_world:app", "//examples/swift/hello_world:appmain", - "//library/swift:ios_framework_archive", + "//library/swift:ios_lib", "//test/swift/integration:cancel_stream_test", "//test/swift/integration:cancel_stream_test_lib", "//test/swift/integration:direct_response_contains_headers_integration_test", diff --git a/examples/objective-c/hello_world/ViewController.m b/examples/objective-c/hello_world/ViewController.m index c116bb878c..9fce130de8 100644 --- a/examples/objective-c/hello_world/ViewController.m +++ b/examples/objective-c/hello_world/ViewController.m @@ -1,4 +1,4 @@ -#import +#import #import #import "Result.h" #import "ViewController.h" diff --git a/library/swift/BUILD b/library/swift/BUILD index ceac406193..994a68e83d 100644 --- a/library/swift/BUILD +++ b/library/swift/BUILD @@ -1,9 +1,11 @@ -load("@envoy_mobile//bazel:swift_static_framework.bzl", "swift_static_framework") +load("@build_bazel_rules_apple//apple:ios.bzl", "ios_static_framework") +load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library") +load("//bazel:swift_header_collector.bzl", "swift_header_collector") licenses(["notice"]) # Apache 2 -swift_static_framework( - name = "ios_framework", +swift_library( + name = "ios_lib", srcs = glob([ "DirectResponse.swift", "Engine.swift", @@ -40,7 +42,22 @@ swift_static_framework( "mocks/*.swift", "stats/*.swift", ]), + features = ["swift.enable_library_evolution"], module_name = "Envoy", private_deps = ["//library/objective-c:envoy_engine_objc_lib"], visibility = ["//visibility:public"], ) + +swift_header_collector( + name = "ios_lib_headers", + library = "ios_lib", +) + +ios_static_framework( + name = "ios_framework", + hdrs = ["ios_lib_headers"], + bundle_name = "Envoy", + minimum_os_version = "11.0", + visibility = ["//visibility:public"], + deps = ["ios_lib"], +)