Commit a9b7d8fa07
2020-12-01 04:18:40
1 parent
57ac835Changed files (3)
src/Compilation.zig
@@ -506,8 +506,19 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
{
break :dl true;
}
- if (options.system_libs.len != 0) {
- // when creating a executable that links to system libraries,
+ const any_dyn_libs: bool = x: {
+ if (options.system_libs.len != 0)
+ break :x true;
+ for (options.link_objects) |obj| {
+ switch (classifyFileExt(obj)) {
+ .shared_library => break :x true,
+ else => continue,
+ }
+ }
+ break :x false;
+ };
+ if (any_dyn_libs) {
+ // When creating a executable that links to system libraries,
// we require dynamic linking, but we must not link static libraries
// or object files dynamically!
break :dl (options.output_mode == .Exe);
test/standalone/cat/main.zig
@@ -3,37 +3,40 @@ const io = std.io;
const process = std.process;
const fs = std.fs;
const mem = std.mem;
-const warn = std.debug.warn;
-const allocator = std.testing.allocator;
+const warn = std.log.warn;
pub fn main() !void {
- var args_it = process.args();
- const exe = try unwrapArg(args_it.next(allocator).?);
+ var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
+ defer arena_instance.deinit();
+ const arena = &arena_instance.allocator;
+
+ const args = try process.argsAlloc(arena);
+
+ const exe = args[0];
var catted_anything = false;
const stdout_file = io.getStdOut();
const cwd = fs.cwd();
- while (args_it.next(allocator)) |arg_or_err| {
- const arg = try unwrapArg(arg_or_err);
+ for (args[1..]) |arg| {
if (mem.eql(u8, arg, "-")) {
catted_anything = true;
- try cat_file(stdout_file, io.getStdIn());
- } else if (arg[0] == '-') {
+ try stdout_file.writeFileAll(io.getStdIn(), .{});
+ } else if (mem.startsWith(u8, arg, "-")) {
return usage(exe);
} else {
const file = cwd.openFile(arg, .{}) catch |err| {
- warn("Unable to open file: {}\n", .{@errorName(err)});
+ warn("Unable to open file: {s}\n", .{@errorName(err)});
return err;
};
defer file.close();
catted_anything = true;
- try cat_file(stdout_file, file);
+ try stdout_file.writeFileAll(file, .{});
}
}
if (!catted_anything) {
- try cat_file(stdout_file, io.getStdIn());
+ try stdout_file.writeFileAll(io.getStdIn(), .{});
}
}
@@ -41,31 +44,3 @@ fn usage(exe: []const u8) !void {
warn("Usage: {} [FILE]...\n", .{exe});
return error.Invalid;
}
-
-// TODO use copy_file_range
-fn cat_file(stdout: fs.File, file: fs.File) !void {
- var buf: [1024 * 4]u8 = undefined;
-
- while (true) {
- const bytes_read = file.read(buf[0..]) catch |err| {
- warn("Unable to read from stream: {}\n", .{@errorName(err)});
- return err;
- };
-
- if (bytes_read == 0) {
- break;
- }
-
- stdout.writeAll(buf[0..bytes_read]) catch |err| {
- warn("Unable to write to stdout: {}\n", .{@errorName(err)});
- return err;
- };
- }
-}
-
-fn unwrapArg(arg: anyerror![]u8) ![]u8 {
- return arg catch |err| {
- warn("Unable to parse command line: {}\n", .{err});
- return err;
- };
-}