From 4d106076c3db840f786460e281aded056053ac35 Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Wed, 18 Oct 2023 18:43:48 -0400 Subject: [PATCH] link: initialize llvm before calling the llvm API --- src/codegen/llvm/Builder.zig | 15 ++++++++------- src/link.zig | 2 ++ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/codegen/llvm/Builder.zig b/src/codegen/llvm/Builder.zig index a4209da40a..5a9703af3d 100644 --- a/src/codegen/llvm/Builder.zig +++ b/src/codegen/llvm/Builder.zig @@ -7999,11 +7999,13 @@ pub fn init(options: Options) InitError!Builder { assert(try self.string("") == .empty); if (options.name.len > 0) self.source_filename = try self.string(options.name); - self.initializeLLVMTarget(options.target.cpu.arch); - if (self.useLibLlvm()) self.llvm.module = llvm.Module.createWithName( - (self.source_filename.slice(&self) orelse ""), - self.llvm.context, - ); + if (self.useLibLlvm()) { + initializeLLVMTarget(options.target.cpu.arch); + self.llvm.module = llvm.Module.createWithName( + (self.source_filename.slice(&self) orelse ""), + self.llvm.context, + ); + } if (options.triple.len > 0) { self.target_triple = try self.string(options.triple); @@ -8117,8 +8119,7 @@ pub fn deinit(self: *Builder) void { self.* = undefined; } -pub fn initializeLLVMTarget(self: *const Builder, arch: std.Target.Cpu.Arch) void { - if (!self.useLibLlvm()) return; +pub fn initializeLLVMTarget(arch: std.Target.Cpu.Arch) void { switch (arch) { .aarch64, .aarch64_be, .aarch64_32 => { llvm.LLVMInitializeAArch64Target(); diff --git a/src/link.zig b/src/link.zig index f7770b6f82..9c79de290d 100644 --- a/src/link.zig +++ b/src/link.zig @@ -1150,7 +1150,9 @@ pub const File = struct { } const llvm_bindings = @import("codegen/llvm/bindings.zig"); + const Builder = @import("codegen/llvm/Builder.zig"); const llvm = @import("codegen/llvm.zig"); + Builder.initializeLLVMTarget(base.options.target.cpu.arch); const os_tag = llvm.targetOs(base.options.target.os.tag); const bad = llvm_bindings.WriteArchive(full_out_path_z, object_files.items.ptr, object_files.items.len, os_tag); if (bad) return error.UnableToWriteArchive;