Skip to content

Commit 1cc7d4a

Browse files
committed
feat: Implement _system_library_build_file
Before: ```console $ bazel run //:sqlite_data_example ERROR: /private/var/tmp/_bazel_jsimard/e01a47dc03c304f8327e2ce75a78ef77/external/rules_swift_package_manager++swift_deps+swiftpkg_grdb.swift/BUILD.bazel:46:14: in deps attribute of swift_library rule @@rules_swift_package_manager++swift_deps+swiftpkg_grdb.swift//:GRDB.rspm: target '@@rules_swift_package_manager++swift_deps+swiftpkg_grdb.swift//:GRDBSQLite.rspm' does not exist ERROR: /private/var/tmp/_bazel_jsimard/e01a47dc03c304f8327e2ce75a78ef77/external/rules_swift_package_manager++swift_deps+swiftpkg_grdb.swift/BUILD.bazel:46:14: Analysis of target '@@rules_swift_package_manager++swift_deps+swiftpkg_grdb.swift//:GRDB.rspm' (config: 945dbe2) failed ERROR: Analysis of target '//:sqlite_data_example' failed; build aborted: Analysis failed INFO: Elapsed time: 15.202s, Critical Path: 0.00s INFO: 1 process: 1 internal. ERROR: Build did NOT complete successfully ERROR: Build failed. Not running target ``` After: ```console $ bazel run //:sqlite_data_example INFO: Analyzed target //:sqlite_data_example (2 packages loaded, 108 targets configured). INFO: Found 1 target... Target //:sqlite_data_example up-to-date: bazel-bin/sqlite_data_example INFO: Elapsed time: 0.762s, Critical Path: 0.01s INFO: 1 process: 131 action cache hit, 1 internal. INFO: Build completed successfully, 1 total action INFO: Running command line: bazel-bin/sqlite_data_example 1 Frodo Baggins 3 Samwise Gamgee ```
1 parent 83d68df commit 1cc7d4a

File tree

10 files changed

+336
-5
lines changed

10 files changed

+336
-5
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Import Shared settings
2+
import %workspace%/../../shared.bazelrc
3+
4+
# Import CI settings.
5+
import %workspace%/../../ci.bazelrc
6+
7+
# Try to import a local.rc file; typically, written by CI
8+
try-import %workspace%/../../local.bazelrc
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_binary")
2+
3+
swift_binary(
4+
name = "sqlite_data_example",
5+
srcs = ["example.swift"],
6+
module_name = "sqlite_data_example",
7+
visibility = ["//visibility:public"],
8+
deps = [
9+
"@swiftpkg_sqlite_data//:SQLiteData",
10+
],
11+
)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
bazel_dep(
2+
name = "rules_swift_package_manager",
3+
version = "0.0.0",
4+
)
5+
local_path_override(
6+
module_name = "rules_swift_package_manager",
7+
path = "../..",
8+
)
9+
10+
bazel_dep(name = "cgrindel_bazel_starlib", version = "0.27.0")
11+
bazel_dep(name = "bazel_skylib", version = "1.8.1")
12+
bazel_dep(name = "apple_support", version = "1.23.1")
13+
bazel_dep(
14+
name = "rules_swift",
15+
version = "3.1.2",
16+
repo_name = "build_bazel_rules_swift",
17+
)
18+
bazel_dep(
19+
name = "rules_apple",
20+
version = "4.2.0",
21+
repo_name = "build_bazel_rules_apple",
22+
)
23+
24+
bazel_dep(
25+
name = "bazel_skylib_gazelle_plugin",
26+
version = "1.8.1",
27+
dev_dependency = True,
28+
)
29+
bazel_dep(
30+
name = "gazelle",
31+
version = "0.45.0",
32+
dev_dependency = True,
33+
repo_name = "bazel_gazelle",
34+
)
35+
36+
swift_deps = use_extension(
37+
"@rules_swift_package_manager//:extensions.bzl",
38+
"swift_deps",
39+
)
40+
swift_deps.from_package(
41+
resolved = "//:Package.resolved",
42+
swift = "//:Package.swift",
43+
)
44+
use_repo(
45+
swift_deps,
46+
"swift_package",
47+
"swiftpkg_sqlite_data",
48+
"swiftpkg_swift_tagged",
49+
)

examples/sqlite_data_example/Package.resolved

Lines changed: 149 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// swift-tools-version: 6.0
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "sqlite_data_example",
7+
dependencies: [
8+
.package(url: "https://github.com/pointfreeco/sqlite-data", exact: "1.0.0"),
9+
.package(url: "https://github.com/pointfreeco/swift-tagged", from: "0.10.0"),
10+
]
11+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Intentionally blank: Using bzlmod
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Intentionally blank: Force bzlmod to strict mode
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
set -eou pipefail
4+
5+
# Use the Bazel binary specified by the integration test. Otherise, fall back
6+
# to bazel.
7+
bazel="${BIT_BAZEL_BINARY:-bazel}"
8+
9+
# Test resolving the package via the `swift_package` repo.
10+
"${bazel}" run @swift_package//:resolve
11+
12+
# Ensure that it builds.
13+
"${bazel}" build //...
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import SQLiteData
2+
3+
@Table
4+
private struct Hobbit: Equatable {
5+
let id: Int
6+
var firstName: String
7+
var lastName: String
8+
}
9+
10+
@main
11+
struct Main {
12+
static func main() async throws {
13+
let database = try DatabaseQueue()
14+
try await withDependencies {
15+
$0.defaultDatabase = database
16+
} operation: {
17+
try await database.write { db in
18+
try #sql(
19+
"""
20+
CREATE TABLE "hobbits" (
21+
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
22+
"firstName" TEXT NOT NULL,
23+
"lastName" TEXT NOT NULL
24+
)
25+
"""
26+
)
27+
.execute(db)
28+
_ = try Hobbit.insert { Hobbit(id: 1, firstName: "Frodo", lastName: "Baggins") }.execute(db)
29+
_ = try Hobbit.insert { Hobbit(id: 3, firstName: "Samwise", lastName: "Gamgee") }.execute(db)
30+
}
31+
32+
@FetchAll var hobbits: [Hobbit]
33+
34+
try await $hobbits.load()
35+
for hobbit in hobbits {
36+
print(hobbit.id, hobbit.firstName, hobbit.lastName)
37+
}
38+
}
39+
}
40+
}

swiftpkg/internal/swiftpkg_build_files.bzl

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -709,12 +709,60 @@ def _starlarkify_clang_attrs(repository_ctx, attrs):
709709

710710
# MARK: - System Library Targets
711711

712-
# GH009(chuck): Remove unused-variable directives
713-
714-
# buildifier: disable=unused-variable
715712
def _system_library_build_file(target):
716-
# GH009(chuck): Implement _system_library_build_file
717-
return None
713+
# System libraries are typically C/Objc libraries that need to be exposed to Swift.
714+
# We generate a cc_library with a swift_interop_hint for proper Swift interop.
715+
716+
bzl_target_name = target.label.name
717+
718+
# Build the cc_library attributes
719+
attrs = {}
720+
721+
# For system libraries, sources is always empty. We need to use glob to find files.
722+
# Look for all .h files in the target path
723+
hdrs_glob = paths.join(target.path, "**/*.h")
724+
725+
# System libraries typically have headers and a module.modulemap
726+
# We'll use glob patterns to find them
727+
attrs["hdrs"] = scg.new_fn_call("glob", [hdrs_glob])
728+
729+
# Create swift_interop_hint for C/Swift interop
730+
aspect_hint_target_name = pkginfo_targets.swift_hint_label_name(bzl_target_name)
731+
732+
load_stmts = [swift_interop_hint_load_stmt]
733+
decls = []
734+
735+
# Create the swift_interop_hint
736+
decls.append(
737+
build_decls.new(
738+
kind = swift_kinds.interop_hint,
739+
name = aspect_hint_target_name,
740+
attrs = {
741+
# For system libraries, we often don't have a module map file
742+
# Set to None and let rules_swift generate one if needed
743+
"module_map": None,
744+
"module_name": target.c99name,
745+
},
746+
),
747+
)
748+
749+
# Add aspect_hints to cc_library
750+
attrs["aspect_hints"] = [":{}".format(aspect_hint_target_name)]
751+
attrs["visibility"] = ["//visibility:public"]
752+
753+
# Create the cc_library
754+
decls.append(
755+
build_decls.new(
756+
kind = clang_kinds.library,
757+
name = bzl_target_name,
758+
attrs = attrs,
759+
),
760+
)
761+
762+
return build_files.new(
763+
load_stmts = load_stmts,
764+
decls = decls,
765+
)
718766

719767
# MARK: - Apple xcframework Targets
720768

0 commit comments

Comments
 (0)