Commit 77c2ac3fcd

Andrew Kelley <andrew@ziglang.org>
2019-07-22 00:04:23
zig build: support DESTDIR environment variable
closes #2929
1 parent f429f4d
Changed files (1)
std/build.zig
@@ -41,9 +41,10 @@ pub const Builder = struct {
     env_map: *BufMap,
     top_level_steps: ArrayList(*TopLevelStep),
     install_prefix: ?[]const u8,
-    search_prefixes: ArrayList([]const u8),
+    dest_dir: ?[]const u8,
     lib_dir: ?[]const u8,
     exe_dir: ?[]const u8,
+    search_prefixes: ArrayList([]const u8),
     installed_files: ArrayList(InstalledFile),
     build_root: []const u8,
     cache_root: []const u8,
@@ -125,10 +126,11 @@ pub const Builder = struct {
             .top_level_steps = ArrayList(*TopLevelStep).init(allocator),
             .default_step = undefined,
             .env_map = env_map,
-            .install_prefix = null,
             .search_prefixes = ArrayList([]const u8).init(allocator),
+            .install_prefix = null,
             .lib_dir = null,
             .exe_dir = null,
+            .dest_dir = env_map.get("DESTDIR"),
             .installed_files = ArrayList(InstalledFile).init(allocator),
             .install_tls = TopLevelStep{
                 .step = Step.initNoOp("install", allocator),
@@ -164,14 +166,14 @@ pub const Builder = struct {
     }
 
     fn resolveInstallPrefix(self: *Builder) void {
-        const prefix = if (self.install_prefix) |prefix| prefix else blk: {
-            const prefix = self.cache_root;
-            self.install_prefix = prefix;
-            break :blk prefix;
+        const dest_dir = self.dest_dir orelse blk: {
+            const dest_dir = self.install_prefix orelse self.cache_root;
+            self.dest_dir = dest_dir;
+            break :blk dest_dir;
         };
-
-        self.lib_dir = fs.path.join(self.allocator, [_][]const u8{ prefix, "lib" }) catch unreachable;
-        self.exe_dir = fs.path.join(self.allocator, [_][]const u8{ prefix, "bin" }) catch unreachable;
+        self.dest_dir = dest_dir;
+        self.lib_dir = fs.path.join(self.allocator, [_][]const u8{ dest_dir, "lib" }) catch unreachable;
+        self.exe_dir = fs.path.join(self.allocator, [_][]const u8{ dest_dir, "bin" }) catch unreachable;
     }
 
     pub fn addExecutable(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
@@ -884,7 +886,7 @@ pub const Builder = struct {
 
     fn getInstallPath(self: *Builder, dir: InstallDir, dest_rel_path: []const u8) []const u8 {
         const base_dir = switch (dir) {
-            .Prefix => self.install_prefix.?,
+            .Prefix => self.dest_dir.?,
             .Bin => self.exe_dir.?,
             .Lib => self.lib_dir.?,
         };