Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ let package = Package(
"SwiftJavaConfigurationShared",
"CodePrinting",
],
resources: [
.process("Resources")
],
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm resources cause pulling in Foundation but hopefully just for the specific target, so this won't show up in any of the runtime modules...

Please keep an eye out on it @madsodgaard but it seems our linking test is happy so we should be good 👍

swiftSettings: [
.swiftLanguageMode(.v5)
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ import PackagePlugin
@main
struct _StaticBuildConfigPlugin: BuildToolPlugin {
func createBuildCommands(context: PluginContext, target: any Target) async throws -> [Command] {
let outSwift = context.pluginWorkDirectoryURL.appending(path: "StaticBuildConfiguration+embedded.swift")
let executable = try context.tool(named: "StaticBuildConfigPluginExecutable").url
let out = context.pluginWorkDirectoryURL.appending(path: "static-build-config.json")
return [
.buildCommand(
displayName: "Run -print-static-build-config",
executable: try context.tool(named: "StaticBuildConfigPluginExecutable").url,
arguments: [outSwift.absoluteURL.path(percentEncoded: false)],
executable: executable,
arguments: [out.path(percentEncoded: false)],
environment: [:],
inputFiles: [],
outputFiles: [outSwift.absoluteURL]
outputFiles: [out]
)
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import Foundation

#if os(Linux)
import Glibc
#elseif os(Android)
import Android
#else
import Darwin.C
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ struct JExtractDefaultBuildConfiguration: BuildConfiguration {
private var base: StaticBuildConfiguration

init() {
let decoder = JSONDecoder()
guard let url = Bundle.module.url(forResource: "static-build-config", withExtension: "json") else {
fatalError("static-build-config.json is not found in module bundle")
}
do {
base = try decoder.decode(StaticBuildConfiguration.self, from: StaticBuildConfiguration.embedded)
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
base = try decoder.decode(StaticBuildConfiguration.self, from: data)
} catch {
fatalError("Embedded StaticBuildConfiguration is broken! data: \(String(data: StaticBuildConfiguration.embedded, encoding: .utf8) ?? "")")
fatalError("\(error)")
}
}

Expand Down
1 change: 1 addition & 0 deletions Sources/JExtractSwiftLib/Resources/dummy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,21 @@ import SwiftIfConfig
let dst = URL(fileURLWithPath: args[1])

let data = try await loadStaticBuildConfig()
let template = #"""
import Foundation
import SwiftIfConfig

extension StaticBuildConfiguration {
static var embedded: Data {
Data(#"\#(data)"#.utf8)
}
}
"""#
try template.write(to: dst, atomically: true, encoding: .utf8)
try data.write(to: dst, options: .atomic)
}

static func loadStaticBuildConfig() async throws -> String {
static func loadStaticBuildConfig() async throws -> Data {
#if compiler(>=6.3)
let result = try await run(
.name("swift"),
arguments: ["frontend", "-print-static-build-config", "-target", "aarch64-unknown-linux-gnu"],
output: .string(limit: 65536),
output: .data(limit: 65536),
error: .string(limit: 65536)
)
if let error = result.standardError, !error.isEmpty {
fatalError(error)
}
return result.standardOutput ?? ""
return result.standardOutput
#else
#if compiler(>=6.2)
let configuration = StaticBuildConfiguration(
Expand All @@ -64,7 +54,7 @@ import SwiftIfConfig
)
#endif
let encoder = JSONEncoder()
return String(data: try encoder.encode(configuration), encoding: .utf8) ?? ""
return try encoder.encode(configuration)
#endif
}
}
Loading