forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaCompilerPlugin.swift
More file actions
92 lines (82 loc) · 3.25 KB
/
JavaCompilerPlugin.swift
File metadata and controls
92 lines (82 loc) · 3.25 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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import PackagePlugin
@main
struct JavaCompilerBuildToolPlugin: BuildToolPlugin {
func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] {
guard let sourceModule = target.sourceModule else { return [] }
// Collect all of the Java source files within this target's sources.
let javaFiles = sourceModule.sourceFiles.map { $0.url }.filter {
$0.pathExtension == "java"
}
if javaFiles.isEmpty {
return []
}
// Note: Target doesn't have a directoryURL counterpart to directory,
// so we cannot eliminate this deprecation warning.
let sourceDir = target.directoryURL
// The name of the configuration file SwiftJava.config from the target for
// which we are generating Swift wrappers for Java classes.
let configFile = sourceDir.appending(path: "swift-java.config")
let config: Configuration?
if let configData = try? Data(contentsOf: configFile) {
let decoder = JSONDecoder()
decoder.allowsJSON5 = true
config = try? decoder.decode(Configuration.self, from: configData)
} else {
config = nil
}
// The class files themselves will be generated into the build directory
// for this target.
let classFiles = javaFiles.compactMap { sourceFileURL in
guard sourceFileURL.isFileURL else {
return nil as URL?
}
let sourceFilePath = sourceFileURL.path
let sourceDirPath = sourceDir.path
guard sourceFilePath.starts(with: sourceDirPath) else {
fatalError("Could not get relative path for source file \(sourceFilePath)")
}
return URL(filePath: context.pluginWorkDirectoryURL.path)
.appending(path: "Java")
.appending(path: String(sourceFilePath.dropFirst(sourceDirPath.count)))
.deletingPathExtension()
.appendingPathExtension("class")
}
let javaHome = URL(filePath: findJavaHome())
let javaClassFileURL = context.pluginWorkDirectoryURL
.appending(path: "Java")
#if os(Windows)
let javac = "javac.exe"
#else
let javac = "javac"
#endif
return [
.buildCommand(
displayName: "Compiling \(javaFiles.count) Java files for target \(sourceModule.name) to \(javaClassFileURL)",
executable:
javaHome
.appending(path: "bin")
.appending(path: javac),
arguments: javaFiles.map { $0.path } + [
"-d", javaClassFileURL.path,
"-parameters", // keep parameter names, which allows us to emit them in generated Swift decls
] + (config?.compilerVersionArgs ?? []),
inputFiles: javaFiles,
outputFiles: classFiles // FIXME: this is not quite enough, javac may generate more files for closures etc, which we don't know about unless we compile first
)
]
}
}