Skip to content

Commit 1f23265

Browse files
committed
Vendor experimental Subprocess
1 parent 79882d8 commit 1f23265

18 files changed

+5150
-20
lines changed

JavaKit/build.gradle

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,13 @@ tasks.processResources {
6060
}
6161
}
6262

63-
//task fatJar(type: Jar) {
64-
// archiveBaseName = 'java-kit-fat-jar'
65-
// duplicatesStrategy = DuplicatesStrategy.EXCLUDE
66-
// from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
67-
// with jar
68-
//}
63+
// Task necessary to bootstrap and EXIT, this is to prevent hangs when used via SwiftPM plugin.
64+
tasks.register('printRuntimeClasspath') {
65+
dependsOn 'jar'
6966

70-
// Task necessary to bootstrap
71-
task printRuntimeClasspath {
72-
def runtimeClasspath = sourceSets.main.runtimeClasspath
73-
inputs.files(runtimeClasspath)
74-
doLast {
75-
println("CLASSPATH:${runtimeClasspath.asPath}")
76-
}
67+
def runtimeClasspath = sourceSets.main.runtimeClasspath
68+
inputs.files(runtimeClasspath)
69+
doLast {
70+
println("SWIFT_JAVA_CLASSPATH:${runtimeClasspath.asPath}")
71+
}
7772
}

JavaKit/src/main/java/org/swift/javakit/dependencies/DependencyResolver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
@SuppressWarnings("unused")
3535
public class DependencyResolver {
3636

37-
private static final String COMMAND_OUTPUT_LINE_PREFIX_CLASSPATH = "CLASSPATH:";
37+
private static final String COMMAND_OUTPUT_LINE_PREFIX_CLASSPATH = "SWIFT_JAVA_CLASSPATH:";
3838
private static final String CLASSPATH_CACHE_FILENAME = "JavaKitDependencyResolver.swift-java.classpath";
3939

4040
public static String GRADLE_API_DEPENDENCY = "dev.gradleplugins:gradle-api:8.10.1";
@@ -236,11 +236,11 @@ private static void printBuildFiles(File projectDir, String[] dependencies) thro
236236
writer.println("}");
237237

238238
writer.println("""
239-
task printRuntimeClasspath {
239+
tasks.register("printRuntimeClasspath") {
240240
def runtimeClasspath = sourceSets.main.runtimeClasspath
241241
inputs.files(runtimeClasspath)
242242
doLast {
243-
println("CLASSPATH:${runtimeClasspath.asPath}")
243+
println("SWIFT_JAVA_CLASSPATH:${runtimeClasspath.asPath}")
244244
}
245245
}
246246
""");

Package.swift

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ let package = Package(
139139
"JExtractSwiftCommandPlugin"
140140
]
141141
),
142-
142+
143143
// ==== Examples
144144

145145
.library(
@@ -152,6 +152,9 @@ let package = Package(
152152
dependencies: [
153153
.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"),
155+
.package(url: "https://github.com/apple/swift-system", from: "1.4.0"),
156+
157+
// Benchmarking
155158
.package(url: "https://github.com/ordo-one/package-benchmark", .upToNextMajor(from: "1.4.0")),
156159
],
157160
targets: [
@@ -183,6 +186,9 @@ let package = Package(
183186
swiftSettings: [
184187
.swiftLanguageMode(.v5),
185188
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"]),
189+
],
190+
plugins: [
191+
"SwiftJavaBootstrapJavaPlugin",
186192
]
187193
),
188194

@@ -211,7 +217,7 @@ let package = Package(
211217
.unsafeFlags(
212218
[
213219
"-L\(javaHome)/lib"
214-
],
220+
],
215221
.when(platforms: [.windows])),
216222
.linkedLibrary("jvm"),
217223
]
@@ -378,6 +384,25 @@ let package = Package(
378384
]
379385
),
380386

387+
.executableTarget(
388+
name: "SwiftJavaBootstrapJavaTool",
389+
dependencies: [
390+
"JavaKitConfigurationShared", // for Configuration reading at runtime
391+
"_Subprocess",
392+
],
393+
swiftSettings: [
394+
.swiftLanguageMode(.v5)
395+
]
396+
),
397+
398+
.plugin(
399+
name: "SwiftJavaBootstrapJavaPlugin",
400+
capability: .buildTool(),
401+
dependencies: [
402+
"SwiftJavaBootstrapJavaTool"
403+
]
404+
),
405+
381406
.plugin(
382407
name: "SwiftJavaPlugin",
383408
capability: .buildTool(),
@@ -442,6 +467,24 @@ let package = Package(
442467
.swiftLanguageMode(.v5),
443468
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"])
444469
]
470+
),
471+
472+
// Experimental Foundation Subprocess Copy
473+
.target(
474+
name: "_CShims",
475+
swiftSettings: [
476+
.swiftLanguageMode(.v5)
477+
]
478+
),
479+
.target(
480+
name: "_Subprocess",
481+
dependencies: [
482+
"_CShims",
483+
.product(name: "SystemPackage", package: "swift-system"),
484+
],
485+
swiftSettings: [
486+
.swiftLanguageMode(.v5)
487+
]
445488
)
446489
]
447490
)

Samples/JavaDependencySampleApp/ci-validate.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
set -e
44
set -x
55

6-
cd ../../JavaKit
7-
./gradlew build
6+
swift build --product SwiftJavaBootstrapJavaTool
7+
.build/arm64-apple-macosx/debug/SwiftJavaBootstrapJavaTool --fetch /Users/ktoso/code/swift-java/Sources/JavaKitDependencyResolver/swift-java.config --module-name JavaKitDependencyResolver --output-directory && .build/plugins/outputs/swift-java/JavaKitDependencyResolver/destination/SwiftJavaBootstrapJavaPlugin
88

99
cd -
1010
swift run --disable-sandbox
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2021 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef _SHIMS_TARGET_CONDITIONALS_H
14+
#define _SHIMS_TARGET_CONDITIONALS_H
15+
16+
#if __has_include(<TargetConditionals.h>)
17+
#include <TargetConditionals.h>
18+
#endif
19+
20+
#if (defined(__APPLE__) && defined(__MACH__))
21+
#define TARGET_OS_MAC 1
22+
#else
23+
#define TARGET_OS_MAC 0
24+
#endif
25+
26+
#if defined(__linux__)
27+
#define TARGET_OS_LINUX 1
28+
#else
29+
#define TARGET_OS_LINUX 0
30+
#endif
31+
32+
#if defined(__unix__)
33+
#define TARGET_OS_BSD 1
34+
#else
35+
#define TARGET_OS_BSD 0
36+
#endif
37+
38+
#if defined(_WIN32)
39+
#define TARGET_OS_WINDOWS 1
40+
#else
41+
#define TARGET_OS_WINDOWS 0
42+
#endif
43+
44+
#if defined(__wasi__)
45+
#define TARGET_OS_WASI 1
46+
#else
47+
#define TARGET_OS_WASI 0
48+
#endif
49+
50+
#endif // _SHIMS_TARGET_CONDITIONALS_H
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
#ifndef process_shims_h
13+
#define process_shims_h
14+
15+
#include "_CShimsTargetConditionals.h"
16+
17+
#if !TARGET_OS_WINDOWS
18+
#include <unistd.h>
19+
20+
#if _POSIX_SPAWN
21+
#include <spawn.h>
22+
#endif
23+
24+
#if TARGET_OS_MAC
25+
int _subprocess_spawn(
26+
pid_t * _Nonnull pid,
27+
const char * _Nonnull exec_path,
28+
const posix_spawn_file_actions_t _Nullable * _Nonnull file_actions,
29+
const posix_spawnattr_t _Nullable * _Nonnull spawn_attrs,
30+
char * _Nullable const args[_Nonnull],
31+
char * _Nullable const env[_Nullable],
32+
uid_t * _Nullable uid,
33+
gid_t * _Nullable gid,
34+
int number_of_sgroups, const gid_t * _Nullable sgroups,
35+
int create_session
36+
);
37+
#endif // TARGET_OS_MAC
38+
39+
int _subprocess_fork_exec(
40+
pid_t * _Nonnull pid,
41+
const char * _Nonnull exec_path,
42+
const char * _Nullable working_directory,
43+
const int file_descriptors[_Nonnull],
44+
char * _Nullable const args[_Nonnull],
45+
char * _Nullable const env[_Nullable],
46+
uid_t * _Nullable uid,
47+
gid_t * _Nullable gid,
48+
gid_t * _Nullable process_group_id,
49+
int number_of_sgroups, const gid_t * _Nullable sgroups,
50+
int create_session,
51+
void (* _Nullable configurator)(void)
52+
);
53+
54+
int _was_process_exited(int status);
55+
int _get_exit_code(int status);
56+
int _was_process_signaled(int status);
57+
int _get_signal_code(int status);
58+
int _was_process_suspended(int status);
59+
60+
#if TARGET_OS_LINUX
61+
int _shims_snprintf(
62+
char * _Nonnull str,
63+
int len,
64+
const char * _Nonnull format,
65+
char * _Nonnull str1,
66+
char * _Nonnull str2
67+
);
68+
#endif
69+
70+
#endif // !TARGET_OS_WINDOWS
71+
72+
#endif /* process_shims_h */

0 commit comments

Comments
 (0)