-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a native way to define module initialization (#7)
- Loading branch information
1 parent
b1ed8ca
commit caa637e
Showing
7 changed files
with
123 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import PackagePlugin | ||
|
||
@main | ||
struct ModuleFactoryPlugin: BuildToolPlugin { | ||
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] { | ||
let outputPath = context.pluginWorkDirectory.appending("ModuleInitializer.swift") | ||
return [ | ||
.buildCommand( | ||
displayName: "Module initialization injection", | ||
executable: try context.tool(named: "ModuleInitializerInjector").path, | ||
arguments: [outputPath.string], | ||
outputFiles: [outputPath] | ||
) | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Foundation | ||
|
||
@main | ||
public struct ModuleInitializerInjector { | ||
static func main() async throws { | ||
guard CommandLine.argc == 2 else { | ||
print("Injector takes one argument") | ||
return | ||
} | ||
|
||
let initializerSourcePath = URL(fileURLWithPath: CommandLine.arguments[1]) | ||
let initializerSource = """ | ||
import EmacsSwiftModule | ||
@_cdecl("plugin_is_GPL_compatible") | ||
public func isGPLCompatible() {} | ||
@_cdecl("emacs_module_init") | ||
public func Init(_ runtimePtr: RuntimePointer) -> Int32 { | ||
do { | ||
let module: Module = createModule() | ||
if !module.isGPLCompatible { | ||
print("Emacs dynamic modules have to be distributed under a GPL compatible license!") | ||
return 1 | ||
} | ||
let env = Environment(from: runtimePtr) | ||
try module.Init(env) | ||
} catch { | ||
return 1 | ||
} | ||
return 0 | ||
} | ||
""" | ||
try initializerSource.write(to: initializerSourcePath, atomically: true, encoding: .utf8) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// Emacs dynamic module, @main class of your package. | ||
public protocol Module { | ||
/// Every dynamic module should be distributed under the GPL compatible license. | ||
/// | ||
/// By returning true here, you agree that your module follows this rule. | ||
var isGPLCompatible: Bool { get } | ||
/// Module initialization point. | ||
/// | ||
/// This function gets executed only once when the user loads the module from Emacs. Usually the module defines some package-specific functions here and/or creates the channel of communication with Emacs. | ||
/// | ||
/// When this function finishes its execution, the given environment becomes invalid and shouldn't be used. See <doc:Lifetimes> for more details. | ||
func Init(_ env: Environment) throws | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters