Skip to content

Commit

Permalink
fix: Set module_map attr for targets with custom modulemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
luispadron committed Nov 27, 2024
1 parent e5c73a4 commit 750dc21
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
35 changes: 31 additions & 4 deletions swiftpkg/internal/swiftpkg_build_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ def _c_child_library(
rule_kind,
srcs,
language_standard = None,
modulemap_path = None,
res_copts = None):
child_attrs = dicts.omit(attrs, ["data"])
child_copts = list(attrs.get("copts", []))
Expand All @@ -238,6 +239,8 @@ def _c_child_library(
if language_standard:
child_copts.append("-std={}".format(language_standard))
child_attrs["copts"] = child_copts
if modulemap_path:
child_attrs["module_map"] = modulemap_path
return build_decls.new(
rule_kind,
name,
Expand Down Expand Up @@ -434,8 +437,8 @@ def _clang_target_build_file(repository_ctx, pkg_ctx, target):
modulemap_target_name = pkginfo_targets.modulemap_label_name(bzl_target_name)
noop_modulemap = clang_src_info.modulemap_path != None
modulemap_attrs = {
"deps": bzl_selects.to_starlark(modulemap_deps),
"hdrs": clang_src_info.hdrs,
"deps": [] if noop_modulemap else bzl_selects.to_starlark(modulemap_deps),
"hdrs": [] if noop_modulemap else clang_src_info.hdrs,
"module_name": target.c99name,
"noop": noop_modulemap,
"visibility": ["//:__subpackages__"],
Expand All @@ -448,6 +451,23 @@ def _clang_target_build_file(repository_ctx, pkg_ctx, target):
),
)

# Create an interop hint for any custom or generated modulemap.
# `module_map` attr of `objc_library` is being removed.
aspect_hint_target_name = pkginfo_targets.swift_hint_label_name(
bzl_target_name,
)
load_stmts.append(swift_interop_hint_load_stmt)
decls.append(
build_decls.new(
kind = swift_kinds.interop_hint,
name = aspect_hint_target_name,
attrs = {
"module_map": clang_src_info.modulemap_path if clang_src_info.modulemap_path else modulemap_target_name,
"module_name": target.c99name,
},
),
)

if clang_src_info.organized_srcs.objc_srcs:
child_name = "{}_objc".format(bzl_target_name)
child_dep_names.append(child_name)
Expand All @@ -461,6 +481,7 @@ def _clang_target_build_file(repository_ctx, pkg_ctx, target):
clang_src_info.organized_srcs.objc_srcs +
clang_src_info.organized_srcs.other_srcs,
language_standard = pkg_ctx.pkg_info.c_language_standard,
modulemap_path = clang_src_info.modulemap_path,
res_copts = res_copts,
),
)
Expand All @@ -477,18 +498,20 @@ def _clang_target_build_file(repository_ctx, pkg_ctx, target):
clang_src_info.organized_srcs.objcxx_srcs +
clang_src_info.organized_srcs.other_srcs,
language_standard = pkg_ctx.pkg_info.cxx_language_standard,
modulemap_path = clang_src_info.modulemap_path,
res_copts = res_copts,
),
)

# Add the objc_library that brings all of the child targets together.
uber_attrs = dicts.omit(attrs, ["srcs", "copts"]) | {
"aspect_hints": [aspect_hint_target_name],
"deps": [
":{}".format(dname)
for dname in child_dep_names
],
"module_name": target.c99name,
}
uber_attrs["module_name"] = target.c99name
decls.append(
build_decls.new(
objc_kinds.library,
Expand All @@ -502,6 +525,8 @@ def _clang_target_build_file(repository_ctx, pkg_ctx, target):
bzl_target_name,
)
aspect_hint_attrs = {"module_name": target.c99name}
if clang_src_info.modulemap_path:
aspect_hint_attrs["module_map"] = clang_src_info.modulemap_path

load_stmts = [swift_interop_hint_load_stmt]
decls.append(
Expand Down Expand Up @@ -1055,7 +1080,9 @@ def _new_for_license(pkg_info, license):

# MARK: - Constants and API Definition

swift_location = "@build_bazel_rules_swift//swift:swift.bzl"
swift_repository = "@build_bazel_rules_swift"
swift_location = "{rules_swift}//swift:swift.bzl".format(rules_swift = swift_repository)
no_module_aspect_hint_label = "{rules_swift}//swift:no_module".format(rules_swift = swift_repository)

swift_kinds = struct(
library = "swift_library",
Expand Down
29 changes: 27 additions & 2 deletions swiftpkg/tests/swiftpkg_build_files_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ swift_interop_hint(
msg = "Objc target",
name = "ObjcLibrary",
exp = """\
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_interop_hint")
load("@rules_swift_package_manager//swiftpkg:build_defs.bzl", "generate_modulemap")
generate_modulemap(
Expand All @@ -634,6 +635,7 @@ generate_modulemap(
objc_library(
name = "ObjcLibrary.rspm",
aspect_hints = ["ObjcLibrary.rspm_swift_hint"],
deps = [":ObjcLibrary.rspm_objc"],
enable_modules = True,
hdrs = ["include/external.h"],
Expand Down Expand Up @@ -698,25 +700,33 @@ objc_library(
textual_hdrs = ["src/foo.m"],
visibility = ["//:__subpackages__"],
)
swift_interop_hint(
name = "ObjcLibrary.rspm_swift_hint",
module_map = "ObjcLibrary.rspm_modulemap",
module_name = "ObjcLibrary",
)
""",
),
struct(
msg = "Objc target with a modulemap",
name = "ObjcLibraryWithModulemap",
exp = """\
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_interop_hint")
load("@rules_swift_package_manager//swiftpkg:build_defs.bzl", "generate_modulemap")
generate_modulemap(
name = "ObjcLibraryWithModulemap.rspm_modulemap",
deps = ["@swiftpkg_mypackage//:ObjcLibraryDep.rspm_modulemap"],
hdrs = ["include/external.h"],
deps = [],
hdrs = [],
module_name = "ObjcLibraryWithModulemap",
noop = True,
visibility = ["//:__subpackages__"],
)
objc_library(
name = "ObjcLibraryWithModulemap.rspm",
aspect_hints = ["ObjcLibraryWithModulemap.rspm_swift_hint"],
deps = [":ObjcLibraryWithModulemap.rspm_objc"],
enable_modules = True,
hdrs = ["include/external.h"],
Expand Down Expand Up @@ -758,6 +768,7 @@ objc_library(
enable_modules = True,
hdrs = ["include/external.h"],
includes = ["include"],
module_map = "include/module.modulemap",
sdk_frameworks = select({
"@rules_swift_package_manager//config_settings/spm/platform:ios": [
"Foundation",
Expand All @@ -781,6 +792,12 @@ objc_library(
textual_hdrs = ["src/foo.m"],
visibility = ["//:__subpackages__"],
)
swift_interop_hint(
name = "ObjcLibraryWithModulemap.rspm_swift_hint",
module_map = "include/module.modulemap",
module_name = "ObjcLibraryWithModulemap",
)
""",
),
struct(
Expand Down Expand Up @@ -931,6 +948,7 @@ swift_library(
name = "ObjcLibraryWithResources",
exp = """\
load("@build_bazel_rules_apple//apple:resources.bzl", "apple_resource_bundle")
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_interop_hint")
load("@rules_swift_package_manager//swiftpkg:build_defs.bzl", "generate_modulemap", "objc_resource_bundle_accessor_hdr", "objc_resource_bundle_accessor_impl", "resource_bundle_infoplist")
apple_resource_bundle(
Expand All @@ -952,6 +970,7 @@ generate_modulemap(
objc_library(
name = "ObjcLibraryWithResources.rspm",
aspect_hints = ["ObjcLibraryWithResources.rspm_swift_hint"],
data = [":ObjcLibraryWithResources.rspm_resource_bundle"],
deps = [":ObjcLibraryWithResources.rspm_objc"],
enable_modules = True,
Expand Down Expand Up @@ -1039,6 +1058,12 @@ resource_bundle_infoplist(
name = "ObjcLibraryWithResources.rspm_resource_bundle_infoplist",
region = "en",
)
swift_interop_hint(
name = "ObjcLibraryWithResources.rspm_swift_hint",
module_map = "ObjcLibraryWithResources.rspm_modulemap",
module_name = "ObjcLibraryWithResources",
)
""",
),
]
Expand Down

0 comments on commit 750dc21

Please sign in to comment.