|
| 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.org project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift.org project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Foundation |
| 16 | +import PackagePlugin |
| 17 | + |
| 18 | +@main |
| 19 | +struct JExtractSwiftBuildToolPlugin: BuildToolPlugin { |
| 20 | + func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] { |
| 21 | + guard let sourceModule = target.sourceModule else { return [] } |
| 22 | + |
| 23 | + // Note: Target doesn't have a directoryURL counterpart to directory, |
| 24 | + // so we cannot eliminate this deprecation warning. |
| 25 | + let sourceDir = target.directory.string |
| 26 | + |
| 27 | + let configuration = try readConfiguration(sourceDir: "\(sourceDir)") |
| 28 | + |
| 29 | + // We use the the usual maven-style structure of "src/[generated|main|test]/java/..." |
| 30 | + // that is common in JVM ecosystem |
| 31 | + let outputDirectory = context.pluginWorkDirectoryURL |
| 32 | + .appending(path: "src") |
| 33 | + .appending(path: "generated") |
| 34 | + .appending(path: "java") |
| 35 | + |
| 36 | + var arguments: [String] = [ |
| 37 | + "--swift-module", sourceModule.name, |
| 38 | + "--package-name", configuration.javaPackage, |
| 39 | + "--output-directory", outputDirectory.path(percentEncoded: false), |
| 40 | + // TODO: "--build-cache-directory", ... |
| 41 | + // Since plugins cannot depend on libraries we cannot detect what the output files will be, |
| 42 | + // as it depends on the contents of the input files. Therefore we have to implement this as a prebuild plugin. |
| 43 | + // We'll have to make up some caching inside the tool so we don't re-parse files which have not changed etc. |
| 44 | + ] |
| 45 | + arguments.append(sourceDir) |
| 46 | + |
| 47 | + return [ |
| 48 | + .prebuildCommand( |
| 49 | + displayName: "Generate Java wrappers for Swift types", |
| 50 | + executable: try context.tool(named: "JExtractSwiftTool").url, |
| 51 | + arguments: arguments, |
| 52 | + // inputFiles: [ configFile ] + swiftFiles, |
| 53 | + // outputFiles: outputJavaFiles |
| 54 | + outputFilesDirectory: outputDirectory |
| 55 | + ) |
| 56 | + ] |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// Note: the JAVA_HOME environment variable must be set to point to where |
| 61 | +// Java is installed, e.g., |
| 62 | +// Library/Java/JavaVirtualMachines/openjdk-21.jdk/Contents/Home. |
| 63 | +func findJavaHome() -> String { |
| 64 | + if let home = ProcessInfo.processInfo.environment["JAVA_HOME"] { |
| 65 | + return home |
| 66 | + } |
| 67 | + |
| 68 | + // This is a workaround for envs (some IDEs) which have trouble with |
| 69 | + // picking up env variables during the build process |
| 70 | + let path = "\(FileManager.default.homeDirectoryForCurrentUser.path()).java_home" |
| 71 | + if let home = try? String(contentsOfFile: path, encoding: .utf8) { |
| 72 | + if let lastChar = home.last, lastChar.isNewline { |
| 73 | + return String(home.dropLast()) |
| 74 | + } |
| 75 | + |
| 76 | + return home |
| 77 | + } |
| 78 | + |
| 79 | + fatalError("Please set the JAVA_HOME environment variable to point to where Java is installed.") |
| 80 | +} |
0 commit comments