forked from velox-apps/velox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVeloxRustBuildPlugin.swift
More file actions
159 lines (142 loc) · 5.82 KB
/
VeloxRustBuildPlugin.swift
File metadata and controls
159 lines (142 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import Foundation
import PackagePlugin
private func isReleaseConfiguration() -> Bool {
let environment = ProcessInfo.processInfo.environment
let candidates = ["CONFIGURATION", "BUILD_CONFIGURATION", "SWIFT_BUILD_CONFIGURATION"]
for key in candidates {
if let value = environment[key], value.lowercased().contains("release") {
return true
}
}
return false
}
private func shouldUseOfflineCargo() -> Bool {
let environment = ProcessInfo.processInfo.environment
if environment["VELOX_CARGO_ONLINE"] == "1" {
return false
}
if environment["VELOX_CARGO_OFFLINE"] == "1" {
return true
}
let cargoOffline = environment["CARGO_NET_OFFLINE"]?.lowercased()
if cargoOffline == "1" || cargoOffline == "true" {
return true
}
return true
}
@main
struct VeloxRustBuildPlugin: BuildToolPlugin {
func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] {
if ProcessInfo.processInfo.environment["VELOX_SKIP_PLUGIN"] == "1" {
Diagnostics.warning("Skipping VeloxRustBuildPlugin due to VELOX_SKIP_PLUGIN")
return []
}
guard target.name == "VeloxRuntimeWryFFI" else {
return []
}
let manifest = context.package.directory.appending("runtime-wry-ffi/Cargo.toml")
let outputDirectory = context.pluginWorkDirectory.appending("Artifacts")
let cargoTargetDirectory = outputDirectory.appending("cargo-target")
let stampFile = outputDirectory.appending("velox-runtime-wry-ffi.stamp")
let cargoProfile = isReleaseConfiguration() ? "release" : "debug"
var scriptLines = ["set -euo pipefail"]
if let home = ProcessInfo.processInfo.environment["HOME"], !home.isEmpty {
scriptLines.append("export PATH=\"$PATH:\(home)/.cargo/bin\"")
}
if shouldUseOfflineCargo() {
scriptLines.append("export CARGO_NET_OFFLINE=true")
scriptLines.append("echo '[VeloxRustBuildPlugin] cargo offline mode enabled'")
}
scriptLines.append("echo '[VeloxRustBuildPlugin] PATH='\"$PATH\"")
scriptLines.append("cd \"\(context.package.directory.string)\"")
scriptLines.append("mkdir -p \"runtime-wry-ffi/target/\(cargoProfile)\"")
scriptLines.append("if ! command -v cargo >/dev/null; then")
scriptLines.append(" echo '[VeloxRustBuildPlugin] error: cargo executable not found' 1>&2")
scriptLines.append(" exit 1")
scriptLines.append("fi")
var buildCommand = "cargo build --manifest-path \"\(manifest.string)\" --target-dir \"\(cargoTargetDirectory.string)\" --locked"
if shouldUseOfflineCargo() {
buildCommand += " --offline"
}
if isReleaseConfiguration() {
buildCommand += " --release"
}
// Enable local-dev feature when VELOX_LOCAL_DEV=1 (for testing with patched tao/wry)
if ProcessInfo.processInfo.environment["VELOX_LOCAL_DEV"] == "1" {
buildCommand += " --features local-dev"
}
scriptLines.append(buildCommand)
scriptLines.append("touch \"\(stampFile.string)\"")
let script = scriptLines.joined(separator: "\n") + "\n"
return [
.prebuildCommand(
displayName: "Building velox-runtime-wry-ffi (Rust)",
executable: Path("/bin/sh"),
arguments: ["-c", script],
environment: [:],
outputFilesDirectory: outputDirectory
),
]
}
}
#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin
extension VeloxRustBuildPlugin: XcodeBuildToolPlugin {
func createBuildCommands(
context: XcodePluginContext,
target: XcodeTarget,
inputFiles: [Path]
) throws -> [Command] {
if ProcessInfo.processInfo.environment["VELOX_SKIP_PLUGIN"] == "1" {
Diagnostics.warning("Skipping VeloxRustBuildPlugin due to VELOX_SKIP_PLUGIN")
return []
}
guard target.displayName == "VeloxRuntimeWryFFI" else {
return []
}
let manifest = context.xcodeProject.directory.appending("runtime-wry-ffi/Cargo.toml")
let outputDirectory = context.pluginWorkDirectory.appending("Artifacts")
let cargoTargetDirectory = outputDirectory.appending("cargo-target")
let stampFile = outputDirectory.appending("velox-runtime-wry-ffi.stamp")
let cargoProfile = isReleaseConfiguration() ? "release" : "debug"
var scriptLines = ["set -euo pipefail"]
if let home = ProcessInfo.processInfo.environment["HOME"], !home.isEmpty {
scriptLines.append("export PATH=\"$PATH:\(home)/.cargo/bin\"")
}
if shouldUseOfflineCargo() {
scriptLines.append("export CARGO_NET_OFFLINE=true")
scriptLines.append("echo '[VeloxRustBuildPlugin] cargo offline mode enabled'")
}
scriptLines.append("echo '[VeloxRustBuildPlugin] PATH='\"$PATH\"")
scriptLines.append("cd \"\(context.xcodeProject.directory.string)\"")
scriptLines.append("mkdir -p \"runtime-wry-ffi/target/\(cargoProfile)\"")
scriptLines.append("if ! command -v cargo >/dev/null; then")
scriptLines.append(" echo '[VeloxRustBuildPlugin] error: cargo executable not found' 1>&2")
scriptLines.append(" exit 1")
scriptLines.append("fi")
var buildCommand = "cargo build --manifest-path \"\(manifest.string)\" --target-dir \"\(cargoTargetDirectory.string)\" --locked"
if shouldUseOfflineCargo() {
buildCommand += " --offline"
}
if isReleaseConfiguration() {
buildCommand += " --release"
}
// Enable local-dev feature when VELOX_LOCAL_DEV=1 (for testing with patched tao/wry)
if ProcessInfo.processInfo.environment["VELOX_LOCAL_DEV"] == "1" {
buildCommand += " --features local-dev"
}
scriptLines.append(buildCommand)
scriptLines.append("touch \"\(stampFile.string)\"")
let script = scriptLines.joined(separator: "\n") + "\n"
return [
.prebuildCommand(
displayName: "Building velox-runtime-wry-ffi (Rust)",
executable: Path("/bin/sh"),
arguments: ["-c", script],
environment: [:],
outputFilesDirectory: outputDirectory
),
]
}
}
#endif