|
| 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 ArgumentParser |
| 17 | +import SwiftJavaLib |
| 18 | +import JavaKit |
| 19 | +import JavaKitJar |
| 20 | +import SwiftJavaLib |
| 21 | +import JExtractSwiftLib |
| 22 | +import JavaKitConfigurationShared |
| 23 | + |
| 24 | +/// Extract Java bindings from Swift sources or interface files. |
| 25 | +/// |
| 26 | +/// Example usage: |
| 27 | +/// ``` |
| 28 | +/// > swift-java jextract |
| 29 | +// --input-swift Sources/SwiftyBusiness \ |
| 30 | +/// --output-swift .build/.../outputs/SwiftyBusiness \ |
| 31 | +/// --output-Java .build/.../outputs/Java |
| 32 | +/// ``` |
| 33 | +extension SwiftJava { |
| 34 | + |
| 35 | + struct JExtractCommand: SwiftJavaBaseAsyncParsableCommand, HasCommonOptions { |
| 36 | + static let configuration = CommandConfiguration( |
| 37 | + commandName: "jextract", // TODO: wrap-swift? |
| 38 | + abstract: "Wrap Swift functions and types with Java bindings, making them available to be called from Java") |
| 39 | + |
| 40 | + @OptionGroup var commonOptions: SwiftJava.CommonOptions |
| 41 | + |
| 42 | + @Option(help: "The mode of generation to use for the output files. Used with jextract mode.") |
| 43 | + var mode: JExtractGenerationMode = .ffm |
| 44 | + |
| 45 | + @Option(help: "The name of the Swift module into which the resulting Swift types will be generated.") |
| 46 | + var swiftModule: String |
| 47 | + |
| 48 | + var effectiveSwiftModule: String { |
| 49 | + swiftModule |
| 50 | + } |
| 51 | + |
| 52 | + @Option(help: "The Java package the generated Java code should be emitted into.") |
| 53 | + var javaPackage: String? = nil |
| 54 | + |
| 55 | + @Option(help: "The directory where generated Swift files should be written. Generally used with jextract mode.") |
| 56 | + var outputSwift: String |
| 57 | + |
| 58 | + @Option(help: "The directory where generated Java files should be written. Generally used with jextract mode.") |
| 59 | + var outputJava: String |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +extension SwiftJava.JExtractCommand { |
| 64 | + func runSwiftJavaCommand(config: inout Configuration) async throws { |
| 65 | + if let javaPackage { |
| 66 | + config.javaPackage = javaPackage |
| 67 | + } |
| 68 | + config.swiftModule = self.effectiveSwiftModule |
| 69 | + config.outputJavaDirectory = outputJava |
| 70 | + config.outputSwiftDirectory = outputSwift |
| 71 | + |
| 72 | + if let inputSwift = commonOptions.inputSwift { |
| 73 | + config.inputSwiftDirectory = inputSwift |
| 74 | + } else if let swiftModule = config.swiftModule { |
| 75 | + // This is a "good guess" technically a target can be somewhere else, but then you can use --input-swift |
| 76 | + config.inputSwiftDirectory = "\(FileManager.default.currentDirectoryPath)/Sources/\(swiftModule)" |
| 77 | + } |
| 78 | + |
| 79 | + print("[debug][swift-java] Running swift-java in mode: " + "\(self.mode)".bold) |
| 80 | + |
| 81 | + try jextractSwift(config: config) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +extension SwiftJava.JExtractCommand { |
| 86 | + func jextractSwift( |
| 87 | + config: Configuration |
| 88 | + ) throws { |
| 89 | + try SwiftToJava(config: config).run() |
| 90 | + } |
| 91 | + |
| 92 | +} |
| 93 | + |
| 94 | +extension JExtractGenerationMode: ExpressibleByArgument {} |
0 commit comments