From ae119a9a8d5b8dec21bd314e91afcec122eb8631 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 16 May 2023 16:29:20 -0700 Subject: CLI: fix stdin dumping behavior * no need to move `tmpFilePath` around * no need for calculating max length of `FileExt` tag name * provide a canonical file extension name for `FileExt` so that, e.g. the file will be named `stdin.S` instead of `stdin.assembly_with_cpp`. * move temp file cleanup to a function to reduce defer bloat in a large function. * fix bug caused by mixing relative and absolute paths in the cleanup logic. * remove commented out test and dead code --- src/Compilation.zig | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'src/Compilation.zig') diff --git a/src/Compilation.zig b/src/Compilation.zig index 3ae43773e3..0818eaafdd 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -3981,7 +3981,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P // We can't know the digest until we do the C compiler invocation, // so we need a temporary filename. - const out_obj_path = try comp.local_cache_directory.tmpFilePath(arena, o_basename); + const out_obj_path = try comp.tmpFilePath(arena, o_basename); var zig_cache_tmp_dir = try comp.local_cache_directory.handle.makeOpenPath("tmp", .{}); defer zig_cache_tmp_dir.close(); @@ -4129,6 +4129,16 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P }; } +pub fn tmpFilePath(comp: *Compilation, ally: Allocator, suffix: []const u8) error{OutOfMemory}![]const u8 { + const s = std.fs.path.sep_str; + const rand_int = std.crypto.random.int(u64); + if (comp.local_cache_directory.path) |p| { + return std.fmt.allocPrint(ally, "{s}" ++ s ++ "tmp" ++ s ++ "{x}-{s}", .{ p, rand_int, suffix }); + } else { + return std.fmt.allocPrint(ally, "tmp" ++ s ++ "{x}-{s}", .{ rand_int, suffix }); + } +} + pub fn addTranslateCCArgs( comp: *Compilation, arena: Allocator, @@ -4588,13 +4598,26 @@ pub const FileExt = enum { }; } - // maximum length of @tagName(ext: FileExt) - pub const max_len = blk: { - var max: u16 = 0; - inline for (std.meta.tags(FileExt)) |ext| - max = std.math.max(@tagName(ext).len, max); - break :blk max; - }; + pub fn canonicalName(ext: FileExt, target: Target) [:0]const u8 { + return switch (ext) { + .c => ".c", + .cpp => ".cpp", + .cu => ".cu", + .h => ".h", + .m => ".m", + .mm => ".mm", + .ll => ".ll", + .bc => ".bc", + .assembly => ".s", + .assembly_with_cpp => ".S", + .shared_library => target.dynamicLibSuffix(), + .object => target.ofmt.fileExt(target.cpu.arch), + .static_library => target.staticLibSuffix(), + .zig => ".zig", + .def => ".def", + .unknown => "", + }; + } }; pub fn hasObjectExt(filename: []const u8) bool { -- cgit v1.2.3