Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support for SwiftSettings: swiftLanguageMode, enableUpcomingFeature, enableExperimentalFeature #1423

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
AttilaTheFun marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
AttilaTheFun marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
67 changes: 67 additions & 0 deletions examples/swift_protobuf_example/MODULE.bazel
AttilaTheFun marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
Example module for grpc + proto target generation.
"""

# rules_go

bazel_dep(name = "rules_go", version = "0.50.1")

go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
go_sdk.host()

# gazelle

bazel_dep(name = "gazelle", version = "0.40.0")

# protobuf

bazel_dep(name = "protobuf", version = "29.0")

# rules_proto

bazel_dep(name = "rules_proto", version = "7.0.2")

# apple_support

bazel_dep(name = "apple_support", version = "1.17.1")

# rules_swift

bazel_dep(name = "rules_swift", version = "2.2.2")

# rules_swift_package_manager

bazel_dep(
name = "rules_swift_package_manager",
version = "0.0.0",
)
local_path_override(
module_name = "rules_swift_package_manager",
path = "../..",
)

# swift_deps START
AttilaTheFun marked this conversation as resolved.
Show resolved Hide resolved
swift_deps = use_extension(
"@rules_swift_package_manager//:extensions.bzl",
"swift_deps",
)
swift_deps.from_package(
declare_swift_deps_info = True,
resolved = "//:Package.resolved",
swift = "//:Package.swift",
)
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_swift_protobuf",
)
# swift_deps END

# rules_apple

bazel_dep(name = "rules_apple", version = "3.16.1")

# rules_xcodeproj

bazel_dep(name = "rules_xcodeproj", version = "2.10.0")
15 changes: 15 additions & 0 deletions examples/swift_protobuf_example/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"originHash" : "60cf222778a0e2535cb4ccd7a820166a4d250e6dc3852a8f320ba5948745e39f",
"pins" : [
{
"identity" : "swift-protobuf",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-protobuf.git",
"state" : {
"revision" : "edb6ed4919f7756157fe02f2552b7e3850a538e5",
"version" : "1.28.1"
}
}
],
"version" : 3
}
15 changes: 15 additions & 0 deletions examples/swift_protobuf_example/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// swift-tools-version: 6.0

import PackageDescription

let package = Package(
name: "swift_protobuf_example",
dependencies: [
// These are the versions used by rules_swift
// .package(url: "https://github.com/grpc/grpc-swift-extras", exact: "1.0.0-beta.2"),
// .package(url: "https://github.com/grpc/grpc-swift.git", exact: "2.0.0-beta.2"), // Required by grpc-swift-extras @ 1.0.0-beta.2
// .package(url: "https://github.com/grpc/grpc-swift-protobuf.git", exact: "1.0.0-beta.2"), // Required by grpc-swift @ 2.0.0-beta.2
// .package(url: "https://github.com/grpc/grpc-swift-nio-transport.git", exact: "1.0.0-beta.2"), // Required by grpc-swift @ 2.0.0-beta.2
AttilaTheFun marked this conversation as resolved.
Show resolved Hide resolved
.package(url: "https://github.com/apple/swift-protobuf.git", exact: "1.28.1"),
]
)
16 changes: 16 additions & 0 deletions examples/swift_protobuf_example/do_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

set -o errexit -o nounset -o pipefail

# Use the Bazel binary specified by the integration test. Otherise, fall back
# to bazel.
bazel="${BIT_BAZEL_BINARY:-bazel}"

# Generate Swift external deps and update build files
"${bazel}" run //:tidy

# Ensure that it builds and tests pass
"${bazel}" test //...

# Ensure that Gazelle doesn't make any changes
"${bazel}" run //:update_build_files -- --mode=diff
14 changes: 13 additions & 1 deletion swiftpkg/internal/pkginfos.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1436,25 +1436,35 @@ def _new_swift_settings(build_settings):
"""
defines = []
unsafe_flags = []
language_modes = []
experimental_features = []
upcoming_features = []
for bs in build_settings:
if bs.kind == build_setting_kinds.define:
defines.append(bs)
elif bs.kind == build_setting_kinds.unsafe_flags:
unsafe_flags.append(bs)
elif bs.kind == build_setting_kinds.language_modes:
language_modes.append(bs)
elif bs.kind == build_setting_kinds.experimental_features:
experimental_features.append(bs)
elif bs.kind == build_setting_kinds.upcoming_features:
upcoming_features.append(bs)
else:
# We do not recognize the setting.
pass
if len(defines) == 0 and \
len(unsafe_flags) == 0 and \
len(experimental_features) == 0:
len(language_modes) == 0 and \
len(experimental_features) == 0 and \
len(upcoming_features) == 0:
return None
return struct(
defines = defines,
unsafe_flags = unsafe_flags,
language_modes = language_modes,
experimental_features = experimental_features,
upcoming_features = upcoming_features,
)

def _new_linker_settings(build_settings):
Expand Down Expand Up @@ -1639,7 +1649,9 @@ build_setting_kinds = struct(
linked_framework = "linkedFramework",
linked_library = "linkedLibrary",
unsafe_flags = "unsafeFlags",
language_modes = "swiftLanguageMode",
experimental_features = "enableExperimentalFeature",
upcoming_features = "enableUpcomingFeature",
)

# MARK: - API Definition
Expand Down
35 changes: 29 additions & 6 deletions swiftpkg/internal/swiftpkg_build_files.bzl
AttilaTheFun marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ def _swift_target_build_file(pkg_ctx, target):

# Check if any of the sources indicate that the module will be used by
# Objective-C code. If so, generate the bridge header file.
features = []
if target.swift_src_info.has_objc_directive and is_library_target:
attrs["generates_header"] = True
attrs["features"] = ["swift.propagate_generated_module_map"]
feature = bzl_selects.new(value = "swift.propagate_generated_module_map")
features.append(feature)

if target.swift_settings != None:
if len(target.swift_settings.defines) > 0:
Expand All @@ -132,11 +134,32 @@ def _swift_target_build_file(pkg_ctx, target):
bzl_selects.new_from_build_setting(bs)
for bs in target.swift_settings.unsafe_flags
]))
if len(target.swift_settings.experimental_features) > 0:
for bs in target.swift_settings.experimental_features:
copts.append("-enable-experimental-feature")
copts.extend(lists.flatten(bzl_selects.new_from_build_setting(bs)))

for bs in target.swift_settings.language_modes:
for language_mode in lists.flatten(bzl_selects.new_from_build_setting(bs)):
new_language_mode = bzl_selects.new(
value = "swift.enable_v" + language_mode.value,
kind = language_mode.kind,
condition = language_mode.condition
)
features.append(new_language_mode)
for bs in target.swift_settings.experimental_features:
for experimental_feature in lists.flatten(bzl_selects.new_from_build_setting(bs)):
new_experimental_feature = bzl_selects.new(
value = "swift.experimental." + experimental_feature.value,
kind = experimental_feature.kind,
condition = experimental_feature.condition
)
features.append(new_experimental_feature)
for bs in target.swift_settings.upcoming_features:
for upcoming_feature in lists.flatten(bzl_selects.new_from_build_setting(bs)):
new_upcoming_feature = bzl_selects.new(
value = "swift.upcoming." + upcoming_feature.value,
kind = upcoming_feature.kind,
condition = upcoming_feature.condition
)
features.append(new_upcoming_feature)
if len(features) > 0:
attrs["features"] = bzl_selects.to_starlark(features, mutually_inclusive = True)
if len(copts) > 0:
attrs["copts"] = bzl_selects.to_starlark(copts, mutually_inclusive = True)

Expand Down
Loading