Skip to content

Commit 41bed08

Browse files
committed
wip on unique names
1 parent 7103dec commit 41bed08

16 files changed

+95
-72
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ let package = Package(
150150

151151
],
152152
dependencies: [
153-
.package(url: "https://github.com/swiftlang/swift-syntax.git", branch: "main"),
153+
.package(url: "https://github.com/swiftlang/swift-syntax", from: "600.0.1"),
154154
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
155155
.package(url: "https://github.com/ordo-one/package-benchmark", .upToNextMajor(from: "1.4.0")),
156156
],

Plugins/JExtractSwiftCommandPlugin/JExtractSwiftCommandPlugin.swift

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,45 @@ import PackagePlugin
1717

1818
@main
1919
final class JExtractSwiftCommandPlugin: BuildToolPlugin, CommandPlugin {
20-
20+
2121
var verbose: Bool = false
22-
22+
2323
/// Build the target before attempting to extract from it.
2424
/// This avoids trying to extract from broken sources.
2525
///
2626
/// You may disable this if confident that input targets sources are correct and there's no need to kick off a pre-build for some reason.
2727
var buildInputs: Bool = true
28-
28+
2929
/// Build the target once swift-java sources have been generated.
3030
/// This helps verify that the generated output is correct, and won't miscompile on the next build.
3131
var buildOutputs: Bool = true
32-
32+
3333
func createBuildCommands(context: PackagePlugin.PluginContext, target: any PackagePlugin.Target) async throws -> [PackagePlugin.Command] {
3434
// FIXME: This is not a build plugin but SwiftPM forces us to impleme the protocol anyway? rdar://139556637
3535
return []
3636
}
37-
37+
3838
func performCommand(context: PluginContext, arguments: [String]) throws {
39+
// Plugin can't have dependencies, so we have some naive argument parsing instead:
3940
self.verbose = arguments.contains("-v") || arguments.contains("--verbose")
40-
41+
if !self.verbose {
42+
fatalError("Plugin should be verbose")
43+
}
44+
4145
let selectedTargets: [String] =
4246
if let last = arguments.lastIndex(where: { $0.starts(with: "-")}),
4347
last < arguments.endIndex {
4448
Array(arguments[..<last])
4549
} else {
4650
[]
4751
}
48-
52+
4953
for target in context.package.targets {
5054
guard let configPath = getSwiftJavaConfig(target: target) else {
5155
log("Skipping target '\(target.name), has no 'swift-java.config' file")
5256
continue
5357
}
54-
58+
5559
do {
5660
print("[swift-java] Extracting Java wrappers from target: '\(target.name)'...")
5761
try performCommand(context: context, target: target, arguments: arguments)
@@ -60,24 +64,19 @@ final class JExtractSwiftCommandPlugin: BuildToolPlugin, CommandPlugin {
6064
}
6165
}
6266
}
63-
67+
6468
/// Perform the command on a specific target.
6569
func performCommand(context: PluginContext, target: Target, arguments: [String]) throws {
6670
// Make sure the target can builds properly
6771
try self.packageManager.build(.target(target.name), parameters: .init())
68-
72+
6973
guard let sourceModule = target.sourceModule else { return }
7074

7175
if self.buildInputs {
7276
log("Pre-building target '\(target.name)' before extracting sources...")
7377
try self.packageManager.build(.target(target.name), parameters: .init())
7478
}
75-
76-
if self.buildOutputs {
77-
log("Post-building target '\(target.name)' to verify generated sources...")
78-
try self.packageManager.build(.target(target.name), parameters: .init())
79-
}
80-
79+
8180
// Note: Target doesn't have a directoryURL counterpart to directory,
8281
// so we cannot eliminate this deprecation warning.
8382
let sourceDir = target.directory.string
@@ -91,8 +90,6 @@ final class JExtractSwiftCommandPlugin: BuildToolPlugin, CommandPlugin {
9190
.appending(path: "generated")
9291
.appending(path: "java")
9392
let outputDirectorySwift = context.pluginWorkDirectoryURL
94-
.appending(path: "src")
95-
.appending(path: "generated")
9693
.appending(path: "Sources")
9794

9895
var arguments: [String] = [
@@ -108,28 +105,42 @@ final class JExtractSwiftCommandPlugin: BuildToolPlugin, CommandPlugin {
108105
arguments.append(sourceDir)
109106

110107
try runExtract(context: context, target: target, arguments: arguments)
108+
109+
if self.buildOutputs {
110+
// Building the *products* since we need to build the dylib that contains our newly generated sources,
111+
// so just building the target again would not be enough. We build all products which we affected using
112+
// our source generation, which usually would be just a product dylib with our library.
113+
//
114+
// In practice, we'll always want to build after generating; either here,
115+
// or via some other task before we run any Java code, calling into Swift.
116+
log("Post-extract building products with target '\(target.name)'...")
117+
for product in context.package.products where product.targets.contains(where: { $0.id == target.id }) {
118+
log("Post-extract building product '\(product.name)'...")
119+
try self.packageManager.build(.product(product.name), parameters: .init())
120+
}
121+
}
111122
}
112-
123+
113124
func runExtract(context: PluginContext, target: Target, arguments: [String]) throws {
114125
let process = Process()
115126
process.executableURL = try context.tool(named: "JExtractSwiftTool").url
116127
process.arguments = arguments
117-
128+
118129
do {
119-
log("Execute: \(process.executableURL) \(arguments)")
120-
130+
log("Execute: \(process.executableURL!.absoluteURL.relativePath) \(arguments.joined(separator: " "))")
131+
121132
try process.run()
122133
process.waitUntilExit()
123-
134+
124135
assert(process.terminationStatus == 0, "Process failed with exit code: \(process.terminationStatus)")
125136
} catch {
126-
print("[swift-java][command] Failed to extract Java sources for target: '\(target.name); Error: \(error)")
137+
print("[swift-java-command] Failed to extract Java sources for target: '\(target.name); Error: \(error)")
127138
}
128139
}
129-
130-
func log(_ message: @autoclosure () -> String) {
140+
141+
func log(_ message: @autoclosure () -> String, terminator: String = "\n") {
131142
if self.verbose {
132-
print("[swift-java] \(message())")
143+
print("[swift-java-command] \(message())", terminator: terminator)
133144
}
134145
}
135146
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
JExtractPluginSampleLibModule+SwiftJava.o : /Users/ktoso/code/swift-java/Samples/JExtractPluginSampleApp/.build/plugins/outputs/jextractpluginsampleapp/JExtractPluginSampleLib/destination/JExtractSwiftPlugin/src/generated/Sources/JExtractPluginSampleLibModule+SwiftJava.swift /Users/ktoso/code/swift-java/Samples/JExtractPluginSampleApp/.build/plugins/outputs/jextractpluginsampleapp/JExtractPluginSampleLib/destination/JExtractSwiftPlugin/src/generated/Sources/MyCoolSwiftClass+SwiftJava.swift /Users/ktoso/code/swift-java/Samples/JExtractPluginSampleApp/Sources/JExtractPluginSampleLib/MyCoolSwiftClass.swift /Applications/Xcode2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk/usr/lib/swift/_StringProcessing.swiftmodule/arm64e-apple-macos.swiftinterface /Applications/Xcode2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk/usr/lib/swift/Swift.swiftmodule/arm64e-apple-macos.swiftinterface /Applications/Xcode2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk/usr/lib/swift/SwiftOnoneSupport.swiftmodule/arm64e-apple-macos.swiftinterface /Applications/Xcode2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk/usr/lib/swift/_Concurrency.swiftmodule/arm64e-apple-macos.swiftinterface /Applications/Xcode2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/prebuilt-modules/15.0/_StringProcessing.swiftmodule/arm64e-apple-macos.swiftmodule /Applications/Xcode2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/prebuilt-modules/15.0/Swift.swiftmodule/arm64e-apple-macos.swiftmodule /Applications/Xcode2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/prebuilt-modules/15.0/SwiftOnoneSupport.swiftmodule/arm64e-apple-macos.swiftmodule /Applications/Xcode2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/prebuilt-modules/15.0/_Concurrency.swiftmodule/arm64e-apple-macos.swiftmodule /Users/ktoso/code/swift-java/Samples/JExtractPluginSampleApp/.build/checkouts/swift-syntax/Sources/_SwiftSyntaxCShims/include/module.modulemap
Binary file not shown.
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MyCoolSwiftClass+SwiftJava.o : /Users/ktoso/code/swift-java/Samples/JExtractPluginSampleApp/.build/plugins/outputs/jextractpluginsampleapp/JExtractPluginSampleLib/destination/JExtractSwiftPlugin/src/generated/Sources/JExtractPluginSampleLibModule+SwiftJava.swift /Users/ktoso/code/swift-java/Samples/JExtractPluginSampleApp/.build/plugins/outputs/jextractpluginsampleapp/JExtractPluginSampleLib/destination/JExtractSwiftPlugin/src/generated/Sources/MyCoolSwiftClass+SwiftJava.swift /Users/ktoso/code/swift-java/Samples/JExtractPluginSampleApp/Sources/JExtractPluginSampleLib/MyCoolSwiftClass.swift /Applications/Xcode2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk/usr/lib/swift/_StringProcessing.swiftmodule/arm64e-apple-macos.swiftinterface /Applications/Xcode2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk/usr/lib/swift/Swift.swiftmodule/arm64e-apple-macos.swiftinterface /Applications/Xcode2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk/usr/lib/swift/SwiftOnoneSupport.swiftmodule/arm64e-apple-macos.swiftinterface /Applications/Xcode2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk/usr/lib/swift/_Concurrency.swiftmodule/arm64e-apple-macos.swiftinterface /Applications/Xcode2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/prebuilt-modules/15.0/_StringProcessing.swiftmodule/arm64e-apple-macos.swiftmodule /Applications/Xcode2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/prebuilt-modules/15.0/Swift.swiftmodule/arm64e-apple-macos.swiftmodule /Applications/Xcode2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/prebuilt-modules/15.0/SwiftOnoneSupport.swiftmodule/arm64e-apple-macos.swiftmodule /Applications/Xcode2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/prebuilt-modules/15.0/_Concurrency.swiftmodule/arm64e-apple-macos.swiftmodule /Users/ktoso/code/swift-java/Samples/JExtractPluginSampleApp/.build/checkouts/swift-syntax/Sources/_SwiftSyntaxCShims/include/module.modulemap
9.64 KB
Binary file not shown.
Binary file not shown.

Samples/JExtractPluginSampleApp/Package.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ let javaIncludePath = "\(javaHome)/include"
3535
#elseif os(macOS)
3636
let javaPlatformIncludePath = "\(javaIncludePath)/darwin"
3737
#else
38-
// TODO: Handle windows as well
39-
#error("Currently only macOS and Linux platforms are supported, this may change in the future.")
38+
let javaPlatformIncludePath = "\(javaIncludePath)/win32"
4039
#endif
4140

4241
let package = Package(
@@ -58,6 +57,9 @@ let package = Package(
5857
.target(
5958
name: "JExtractPluginSampleLib",
6059
dependencies: [],
60+
exclude: [
61+
"swift-java.config"
62+
],
6163
swiftSettings: [
6264
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"])
6365
],

Samples/JExtractPluginSampleApp/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,24 @@ def jextract = tasks.register("jextract", Exec) {
6060
inputs.dir(layout.projectDirectory.dir("Sources"))
6161

6262
// TODO: we can use package describe --type json to figure out which targets depend on JExtractSwiftPlugin and will produce outputs
63-
// Avoid adding this directory, but create the expected one specifically for all targets which WILL produce sources because they have the plugin
63+
// Avoid adding this directory, but create the expected one specifically for all targets
64+
// which WILL produce sources because they have the plugin
6465
outputs.dir(layout.buildDirectory.dir("../.build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}"))
6566

6667
File baseSwiftPluginOutputsDir = layout.buildDirectory.dir("../.build/plugins/outputs/").get().asFile
6768
if (!baseSwiftPluginOutputsDir.exists()) {
6869
baseSwiftPluginOutputsDir.mkdirs()
6970
}
7071
Files.walk(layout.buildDirectory.dir("../.build/plugins/outputs/").get().asFile.toPath()).each {
72+
// Add any Java sources generated by the plugin to our sourceSet
7173
if (it.endsWith("JExtractSwiftPlugin/src/generated/java")) {
7274
outputs.dir(it)
7375
}
7476
}
7577

7678
workingDir = layout.projectDirectory
7779
commandLine "swift"
78-
args("package", "jextract")
80+
args("package", "jextract", "-v", "--log-level", "info") // TODO: pass log level from Gradle build
7981
}
8082

8183
// Add the java-swift generated Java sources

0 commit comments

Comments
 (0)