diff --git a/Sources/LLVM/Initialization.swift b/Sources/LLVM/Initialization.swift index 652e7784..486076a2 100644 --- a/Sources/LLVM/Initialization.swift +++ b/Sources/LLVM/Initialization.swift @@ -2,10 +2,10 @@ import cllvm #endif -/// Lazy static initializer that calls LLVM initialization functions only once. -let llvmInitializer: Void = { - initializeLLVM() -}() +/// initializer that calls LLVM initialization functions only once. +public func initializeLLVM() { + _ = llvmInitializer +} /// Calls all the LLVM functions to initialize: /// @@ -15,7 +15,7 @@ let llvmInitializer: Void = { /// - ASM Parsers /// - Target MCs /// - Disassemblers -private func initializeLLVM() { +let llvmInitializer: Void = { LLVMInitializeAllTargets() LLVMInitializeAllTargetInfos() @@ -25,4 +25,5 @@ private func initializeLLVM() { LLVMInitializeAllTargetMCs() LLVMInitializeAllDisassemblers() -} +}() + diff --git a/Sources/LLVM/Module.swift b/Sources/LLVM/Module.swift index 26a6a7b3..3facbff7 100644 --- a/Sources/LLVM/Module.swift +++ b/Sources/LLVM/Module.swift @@ -53,9 +53,8 @@ public final class Module: CustomStringConvertible { /// context is provided, one will be inferred. public init(name: String, context: Context? = nil) { - // Ensure the LLVM initializer is called when the first module - // is created - _ = llvmInitializer + // Ensure the LLVM initializer is called when the first module is created + initializeLLVM() if let context = context { llvm = LLVMModuleCreateWithNameInContext(name, context.llvm) diff --git a/Sources/LLVM/TargetMachine.swift b/Sources/LLVM/TargetMachine.swift index 99af060a..d183a419 100644 --- a/Sources/LLVM/TargetMachine.swift +++ b/Sources/LLVM/TargetMachine.swift @@ -136,6 +136,10 @@ public class TargetMachine { public init(triple: String? = nil, cpu: String = "", features: String = "", optLevel: CodeGenOptLevel = .default, relocMode: RelocMode = .default, codeModel: CodeModel = .default) throws { + + // Ensure the LLVM initializer is called when the first module is created + initializeLLVM() + self.triple = triple ?? String(cString: LLVMGetDefaultTargetTriple()!) var target: LLVMTargetRef? var error: UnsafeMutablePointer?