From e8f28cda9edb06a5f2189ca5e8928df52ddea20d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20=C3=85stholm?= Date: Mon, 22 Apr 2024 22:13:49 +0200 Subject: [PATCH] std.Build: Install Windows DLLs to `/bin/` by default Windows does not support RPATH and only searches for DLLs in a small number of predetermined paths by default, with one of them being the directory from which the application loaded. Installing both executables and DLLs to `bin/` by default helps ensure that the executable can find any DLL artifacts it has linked to. DLL import libraries are still installed to `lib/`. These defaults match CMake's behavior. --- lib/std/Build/Step/Compile.zig | 6 +++++- lib/std/Build/Step/InstallArtifact.zig | 11 ++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index 9e94b78d6c..79bade4963 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -614,6 +614,10 @@ pub fn isStaticLibrary(self: *const Compile) bool { return self.kind == .lib and self.linkage != .dynamic; } +pub fn isDll(self: *Compile) bool { + return self.isDynamicLibrary() and self.rootModuleTarget().os.tag == .windows; +} + pub fn producesPdbFile(self: *Compile) bool { const target = self.rootModuleTarget(); // TODO: Is this right? Isn't PDB for *any* PE/COFF file? @@ -632,7 +636,7 @@ pub fn producesPdbFile(self: *Compile) bool { } pub fn producesImplib(self: *Compile) bool { - return self.isDynamicLibrary() and self.rootModuleTarget().os.tag == .windows; + return self.isDll(); } pub fn linkLibC(self: *Compile) void { diff --git a/lib/std/Build/Step/InstallArtifact.zig b/lib/std/Build/Step/InstallArtifact.zig index 7ebe8fdaf0..d9f163fce5 100644 --- a/lib/std/Build/Step/InstallArtifact.zig +++ b/lib/std/Build/Step/InstallArtifact.zig @@ -57,8 +57,8 @@ pub fn create(owner: *std.Build, artifact: *Step.Compile, options: Options) *Ins .disabled => null, .default => switch (artifact.kind) { .obj => @panic("object files have no standard installation procedure"), - .exe, .@"test" => InstallDir{ .bin = {} }, - .lib => InstallDir{ .lib = {} }, + .exe, .@"test" => .bin, + .lib => if (artifact.isDll()) .bin else .lib, }, .override => |o| o, }; @@ -77,15 +77,12 @@ pub fn create(owner: *std.Build, artifact: *Step.Compile, options: Options) *Ins }, .h_dir = switch (options.h_dir) { .disabled => null, - .default => switch (artifact.kind) { - .lib => .header, - else => null, - }, + .default => if (artifact.kind == .lib) .header else null, .override => |o| o, }, .implib_dir = switch (options.implib_dir) { .disabled => null, - .default => if (artifact.producesImplib()) dest_dir else null, + .default => if (artifact.producesImplib()) .lib else null, .override => |o| o, },