Skip to content

zig cc: support reading from stdin via "-x LANG -" #14462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4597,6 +4597,27 @@ pub const FileExt = enum {
=> false,
};
}

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 {
Expand Down
26 changes: 26 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3020,6 +3020,32 @@ fn buildOutputType(
break :l global_cache_directory;
};

for (c_source_files.items) |*src| {
if (!mem.eql(u8, src.src_path, "-")) continue;

const ext = src.ext orelse
fatal("-E or -x is required when reading from a non-regular file", .{});

// "-" is stdin. Dump it to a real file.
const sep = fs.path.sep_str;
const sub_path = try std.fmt.allocPrint(arena, "tmp" ++ sep ++ "{x}-stdin{s}", .{
std.crypto.random.int(u64), ext.canonicalName(target_info.target),
});
try local_cache_directory.handle.makePath("tmp");
// Note that in one of the happy paths, execve() is used to switch
// to clang in which case any cleanup logic that exists for this
// temporary file will not run and this temp file will be leaked.
// Oh well. It's a minor punishment for using `-x c` which nobody
// should be doing. Therefore, we make no effort to clean up. Using
// `-` for stdin as a source file always leaks a temp file.
Comment on lines +3035 to +3040
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could minimize the damage by:

  • hashing the file contents (while writing to the file, so no additional allocations)
  • renaming the filename to the hash.

That way, we avoid a small chance of hash collision (I know) and multiple invocations of echo | zig cc <...> produce only one garbage-file.

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, sounds reasonable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var f = try local_cache_directory.handle.createFile(sub_path, .{});
defer f.close();
try f.writeFileAll(io.getStdIn(), .{});

// Convert `sub_path` to be relative to current working directory.
src.src_path = try local_cache_directory.join(arena, &.{sub_path});
}

if (build_options.have_llvm and emit_asm != .no) {
// LLVM has no way to set this non-globally.
const argv = [_][*:0]const u8{ "zig (LLVM option parsing)", "--x86-asm-syntax=intel" };
Expand Down