2017-12-10 16:40:46 -08:00
|
|
|
const std = @import("std");
|
2018-04-12 03:23:58 -07:00
|
|
|
const io = std.io;
|
2019-05-26 10:17:34 -07:00
|
|
|
const fs = std.fs;
|
2018-04-12 03:23:58 -07:00
|
|
|
const mem = std.mem;
|
2019-05-26 10:17:34 -07:00
|
|
|
const process = std.process;
|
2018-04-12 03:23:58 -07:00
|
|
|
const Allocator = mem.Allocator;
|
|
|
|
const ArrayList = std.ArrayList;
|
2020-05-15 12:20:42 -07:00
|
|
|
const ast = std.zig.ast;
|
2020-05-15 18:44:33 -07:00
|
|
|
const Module = @import("Module.zig");
|
2020-05-15 12:20:42 -07:00
|
|
|
const link = @import("link.zig");
|
|
|
|
const Package = @import("Package.zig");
|
2020-05-15 18:44:33 -07:00
|
|
|
const zir = @import("zir.zig");
|
2017-12-04 19:05:27 -08:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
// TODO Improve async I/O enough that we feel comfortable doing this.
|
|
|
|
//pub const io_mode = .evented;
|
2019-11-06 11:29:18 -08:00
|
|
|
|
2019-02-15 21:42:56 -08:00
|
|
|
pub const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
|
2018-08-03 14:22:17 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
pub const Color = enum {
|
|
|
|
Auto,
|
|
|
|
Off,
|
|
|
|
On,
|
|
|
|
};
|
|
|
|
|
2018-04-12 03:23:58 -07:00
|
|
|
const usage =
|
2020-05-15 12:20:42 -07:00
|
|
|
\\Usage: zig [command] [options]
|
2018-04-12 03:23:58 -07:00
|
|
|
\\
|
|
|
|
\\Commands:
|
|
|
|
\\
|
2018-07-17 21:34:42 -07:00
|
|
|
\\ build-exe [source] Create executable from source or object files
|
|
|
|
\\ build-lib [source] Create library from source or object files
|
|
|
|
\\ build-obj [source] Create object from source or assembly
|
|
|
|
\\ fmt [source] Parse file and render in canonical zig format
|
|
|
|
\\ targets List available compilation targets
|
|
|
|
\\ version Print version number and exit
|
|
|
|
\\ zen Print zen of zig and exit
|
2018-04-12 03:23:58 -07:00
|
|
|
\\
|
|
|
|
\\
|
2018-05-26 15:16:39 -07:00
|
|
|
;
|
2018-04-12 03:23:58 -07:00
|
|
|
|
2018-02-09 15:27:50 -08:00
|
|
|
pub fn main() !void {
|
2020-05-15 12:20:42 -07:00
|
|
|
// TODO general purpose allocator in the zig std lib
|
|
|
|
const gpa = if (std.builtin.link_libc) std.heap.c_allocator else std.heap.page_allocator;
|
|
|
|
var arena_instance = std.heap.ArenaAllocator.init(gpa);
|
|
|
|
defer arena_instance.deinit();
|
|
|
|
const arena = &arena_instance.allocator;
|
2017-10-24 07:08:20 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
const args = try process.argsAlloc(arena);
|
2017-12-06 15:22:52 -08:00
|
|
|
|
2018-04-12 03:23:58 -07:00
|
|
|
if (args.len <= 1) {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("expected command argument\n\n{}", .{usage});
|
2019-05-26 10:17:34 -07:00
|
|
|
process.exit(1);
|
2018-04-12 03:23:58 -07:00
|
|
|
}
|
|
|
|
|
2020-01-08 20:55:22 -08:00
|
|
|
const cmd = args[1];
|
|
|
|
const cmd_args = args[2..];
|
|
|
|
if (mem.eql(u8, cmd, "build-exe")) {
|
2020-05-15 12:20:42 -07:00
|
|
|
return buildOutputType(gpa, arena, cmd_args, .Exe);
|
2020-01-08 20:55:22 -08:00
|
|
|
} else if (mem.eql(u8, cmd, "build-lib")) {
|
2020-05-15 12:20:42 -07:00
|
|
|
return buildOutputType(gpa, arena, cmd_args, .Lib);
|
2020-01-08 20:55:22 -08:00
|
|
|
} else if (mem.eql(u8, cmd, "build-obj")) {
|
2020-05-15 12:20:42 -07:00
|
|
|
return buildOutputType(gpa, arena, cmd_args, .Obj);
|
2020-01-08 20:55:22 -08:00
|
|
|
} else if (mem.eql(u8, cmd, "fmt")) {
|
2020-05-15 12:20:42 -07:00
|
|
|
return cmdFmt(gpa, cmd_args);
|
2020-01-08 20:55:22 -08:00
|
|
|
} else if (mem.eql(u8, cmd, "targets")) {
|
2020-05-15 12:20:42 -07:00
|
|
|
const info = try std.zig.system.NativeTargetInfo.detect(arena, .{});
|
2020-03-17 20:03:45 -07:00
|
|
|
const stdout = io.getStdOut().outStream();
|
2020-05-15 12:20:42 -07:00
|
|
|
return @import("print_targets.zig").cmdTargets(arena, cmd_args, stdout, info.target);
|
2020-01-08 20:55:22 -08:00
|
|
|
} else if (mem.eql(u8, cmd, "version")) {
|
2020-05-15 12:20:42 -07:00
|
|
|
// Need to set up the build script to give the version as a comptime value.
|
|
|
|
std.debug.warn("TODO version command not implemented yet\n", .{});
|
|
|
|
return error.Unimplemented;
|
2020-01-08 20:55:22 -08:00
|
|
|
} else if (mem.eql(u8, cmd, "zen")) {
|
2020-05-15 12:20:42 -07:00
|
|
|
try io.getStdOut().writeAll(info_zen);
|
2020-01-08 20:55:22 -08:00
|
|
|
} else if (mem.eql(u8, cmd, "help")) {
|
2020-05-15 12:20:42 -07:00
|
|
|
try io.getStdOut().writeAll(usage);
|
2020-01-08 20:55:22 -08:00
|
|
|
} else {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("unknown command: {}\n\n{}", .{ args[1], usage });
|
2020-01-08 20:55:22 -08:00
|
|
|
process.exit(1);
|
2017-12-04 20:09:03 -08:00
|
|
|
}
|
2018-04-12 03:23:58 -07:00
|
|
|
}
|
2017-12-22 21:29:39 -08:00
|
|
|
|
2018-04-12 03:23:58 -07:00
|
|
|
const usage_build_generic =
|
2020-05-15 12:20:42 -07:00
|
|
|
\\Usage: zig build-exe <options> [files]
|
|
|
|
\\ zig build-lib <options> [files]
|
|
|
|
\\ zig build-obj <options> [files]
|
|
|
|
\\
|
|
|
|
\\Supported file types:
|
|
|
|
\\ (planned) .zig Zig source code
|
|
|
|
\\ .zir Zig Intermediate Representation code
|
|
|
|
\\ (planned) .o ELF object file
|
|
|
|
\\ (planned) .o MACH-O (macOS) object file
|
|
|
|
\\ (planned) .obj COFF (Windows) object file
|
|
|
|
\\ (planned) .lib COFF (Windows) static library
|
|
|
|
\\ (planned) .a ELF static library
|
|
|
|
\\ (planned) .so ELF shared object (dynamic link)
|
|
|
|
\\ (planned) .dll Windows Dynamic Link Library
|
|
|
|
\\ (planned) .dylib MACH-O (macOS) dynamic library
|
|
|
|
\\ (planned) .s Target-specific assembly source code
|
|
|
|
\\ (planned) .S Assembly with C preprocessor (requires LLVM extensions)
|
|
|
|
\\ (planned) .c C source code (requires LLVM extensions)
|
|
|
|
\\ (planned) .cpp C++ source code (requires LLVM extensions)
|
|
|
|
\\ Other C++ extensions: .C .cc .cxx
|
2018-04-12 03:23:58 -07:00
|
|
|
\\
|
|
|
|
\\General Options:
|
2020-05-15 12:20:42 -07:00
|
|
|
\\ -h, --help Print this help and exit
|
|
|
|
\\ --watch Enable compiler REPL
|
|
|
|
\\ --color [auto|off|on] Enable or disable colored error messages
|
|
|
|
\\ -femit-bin[=path] (default) output machine code
|
|
|
|
\\ -fno-emit-bin Do not output machine code
|
2018-04-12 03:23:58 -07:00
|
|
|
\\
|
|
|
|
\\Compile Options:
|
2020-05-15 12:20:42 -07:00
|
|
|
\\ -target [name] <arch><sub>-<os>-<abi> see the targets command
|
|
|
|
\\ -mcpu [cpu] Specify target CPU and feature set
|
|
|
|
\\ --name [name] Override output name
|
|
|
|
\\ --mode [mode] Set the build mode
|
|
|
|
\\ Debug (default) optimizations off, safety on
|
|
|
|
\\ ReleaseFast optimizations on, safety off
|
|
|
|
\\ ReleaseSafe optimizations on, safety on
|
|
|
|
\\ ReleaseSmall optimize for small binary, safety off
|
|
|
|
\\ --dynamic Force output to be dynamically linked
|
|
|
|
\\ --strip Exclude debug symbols
|
2018-04-12 03:23:58 -07:00
|
|
|
\\
|
|
|
|
\\Link Options:
|
2020-05-15 12:20:42 -07:00
|
|
|
\\ -l[lib], --library [lib] Link against system library
|
|
|
|
\\ --dynamic-linker [path] Set the dynamic interpreter path (usually ld.so)
|
|
|
|
\\ --version [ver] Dynamic library semver
|
2018-04-12 03:23:58 -07:00
|
|
|
\\
|
2020-05-15 12:20:42 -07:00
|
|
|
\\Debug Options (Zig Compiler Development):
|
|
|
|
\\ -ftime-report Print timing diagnostics
|
|
|
|
\\ --debug-tokenize verbose tokenization
|
|
|
|
\\ --debug-ast-tree verbose parsing into an AST (tree view)
|
|
|
|
\\ --debug-ast-fmt verbose parsing into an AST (render source)
|
|
|
|
\\ --debug-ir verbose Zig IR
|
|
|
|
\\ --debug-link verbose linking
|
|
|
|
\\ --debug-codegen verbose machine code generation
|
2018-04-12 03:23:58 -07:00
|
|
|
\\
|
2018-05-26 15:16:39 -07:00
|
|
|
;
|
2018-04-12 03:23:58 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
const Emit = union(enum) {
|
|
|
|
no,
|
|
|
|
yes_default_path,
|
|
|
|
yes: []const u8,
|
|
|
|
};
|
2020-03-17 20:03:45 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
fn buildOutputType(
|
|
|
|
gpa: *Allocator,
|
|
|
|
arena: *Allocator,
|
|
|
|
args: []const []const u8,
|
|
|
|
output_mode: std.builtin.OutputMode,
|
|
|
|
) !void {
|
|
|
|
var color: Color = .Auto;
|
2019-12-10 23:08:33 -08:00
|
|
|
var build_mode: std.builtin.Mode = .Debug;
|
|
|
|
var provided_name: ?[]const u8 = null;
|
2020-05-15 20:54:13 -07:00
|
|
|
var link_mode: ?std.builtin.LinkMode = null;
|
2019-12-10 23:08:33 -08:00
|
|
|
var root_src_file: ?[]const u8 = null;
|
|
|
|
var version: std.builtin.Version = .{ .major = 0, .minor = 0, .patch = 0 };
|
|
|
|
var strip = false;
|
2020-05-15 12:20:42 -07:00
|
|
|
var watch = false;
|
|
|
|
var debug_tokenize = false;
|
|
|
|
var debug_ast_tree = false;
|
|
|
|
var debug_ast_fmt = false;
|
|
|
|
var debug_link = false;
|
|
|
|
var debug_ir = false;
|
|
|
|
var debug_codegen = false;
|
|
|
|
var time_report = false;
|
|
|
|
var emit_bin: Emit = .yes_default_path;
|
|
|
|
var emit_zir: Emit = .no;
|
|
|
|
var target_arch_os_abi: []const u8 = "native";
|
|
|
|
var target_mcpu: ?[]const u8 = null;
|
|
|
|
var target_dynamic_linker: ?[]const u8 = null;
|
|
|
|
|
|
|
|
var system_libs = std.ArrayList([]const u8).init(gpa);
|
2019-12-10 23:08:33 -08:00
|
|
|
defer system_libs.deinit();
|
|
|
|
|
|
|
|
{
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < args.len) : (i += 1) {
|
|
|
|
const arg = args[i];
|
|
|
|
if (mem.startsWith(u8, arg, "-")) {
|
2020-05-15 12:20:42 -07:00
|
|
|
if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
|
2020-03-17 20:03:45 -07:00
|
|
|
try io.getStdOut().writeAll(usage_build_generic);
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(0);
|
|
|
|
} else if (mem.eql(u8, arg, "--color")) {
|
|
|
|
if (i + 1 >= args.len) {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("expected [auto|on|off] after --color\n", .{});
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
const next_arg = args[i];
|
|
|
|
if (mem.eql(u8, next_arg, "auto")) {
|
|
|
|
color = .Auto;
|
|
|
|
} else if (mem.eql(u8, next_arg, "on")) {
|
|
|
|
color = .On;
|
|
|
|
} else if (mem.eql(u8, next_arg, "off")) {
|
|
|
|
color = .Off;
|
|
|
|
} else {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("expected [auto|on|off] after --color, found '{}'\n", .{next_arg});
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
} else if (mem.eql(u8, arg, "--mode")) {
|
|
|
|
if (i + 1 >= args.len) {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("expected [Debug|ReleaseSafe|ReleaseFast|ReleaseSmall] after --mode\n", .{});
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
const next_arg = args[i];
|
|
|
|
if (mem.eql(u8, next_arg, "Debug")) {
|
|
|
|
build_mode = .Debug;
|
|
|
|
} else if (mem.eql(u8, next_arg, "ReleaseSafe")) {
|
|
|
|
build_mode = .ReleaseSafe;
|
|
|
|
} else if (mem.eql(u8, next_arg, "ReleaseFast")) {
|
|
|
|
build_mode = .ReleaseFast;
|
|
|
|
} else if (mem.eql(u8, next_arg, "ReleaseSmall")) {
|
|
|
|
build_mode = .ReleaseSmall;
|
|
|
|
} else {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("expected [Debug|ReleaseSafe|ReleaseFast|ReleaseSmall] after --mode, found '{}'\n", .{next_arg});
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
} else if (mem.eql(u8, arg, "--name")) {
|
|
|
|
if (i + 1 >= args.len) {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("expected parameter after --name\n", .{});
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
provided_name = args[i];
|
2020-05-15 12:20:42 -07:00
|
|
|
} else if (mem.eql(u8, arg, "--library")) {
|
2019-12-10 23:08:33 -08:00
|
|
|
if (i + 1 >= args.len) {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("expected parameter after --library\n", .{});
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
i += 1;
|
2020-05-15 12:20:42 -07:00
|
|
|
try system_libs.append(args[i]);
|
|
|
|
} else if (mem.eql(u8, arg, "--version")) {
|
2019-12-10 23:08:33 -08:00
|
|
|
if (i + 1 >= args.len) {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("expected parameter after --version\n", .{});
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
i += 1;
|
2020-05-15 12:20:42 -07:00
|
|
|
version = std.builtin.Version.parse(args[i]) catch |err| {
|
|
|
|
std.debug.warn("unable to parse --version '{}': {}\n", .{ args[i], @errorName(err) });
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(1);
|
2020-05-15 12:20:42 -07:00
|
|
|
};
|
|
|
|
} else if (mem.eql(u8, arg, "-target")) {
|
2019-12-10 23:08:33 -08:00
|
|
|
if (i + 1 >= args.len) {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("expected parameter after -target\n", .{});
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
i += 1;
|
2020-05-15 12:20:42 -07:00
|
|
|
target_arch_os_abi = args[i];
|
|
|
|
} else if (mem.eql(u8, arg, "-mcpu")) {
|
2019-12-10 23:08:33 -08:00
|
|
|
if (i + 1 >= args.len) {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("expected parameter after -mcpu\n", .{});
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
i += 1;
|
2020-05-15 12:20:42 -07:00
|
|
|
target_mcpu = args[i];
|
|
|
|
} else if (mem.startsWith(u8, arg, "-mcpu=")) {
|
|
|
|
target_mcpu = arg["-mcpu=".len..];
|
|
|
|
} else if (mem.eql(u8, arg, "--dynamic-linker")) {
|
2019-12-10 23:08:33 -08:00
|
|
|
if (i + 1 >= args.len) {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("expected parameter after --dynamic-linker\n", .{});
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
i += 1;
|
2020-05-15 12:20:42 -07:00
|
|
|
target_dynamic_linker = args[i];
|
|
|
|
} else if (mem.eql(u8, arg, "--watch")) {
|
|
|
|
watch = true;
|
|
|
|
} else if (mem.eql(u8, arg, "-ftime-report")) {
|
|
|
|
time_report = true;
|
2019-12-10 23:08:33 -08:00
|
|
|
} else if (mem.eql(u8, arg, "-femit-bin")) {
|
2020-05-15 12:20:42 -07:00
|
|
|
emit_bin = .yes_default_path;
|
|
|
|
} else if (mem.startsWith(u8, arg, "-femit-bin=")) {
|
|
|
|
emit_bin = .{ .yes = arg["-femit-bin=".len..] };
|
2019-12-10 23:08:33 -08:00
|
|
|
} else if (mem.eql(u8, arg, "-fno-emit-bin")) {
|
2020-05-15 12:20:42 -07:00
|
|
|
emit_bin = .no;
|
|
|
|
} else if (mem.eql(u8, arg, "-femit-zir")) {
|
|
|
|
emit_zir = .yes_default_path;
|
|
|
|
} else if (mem.startsWith(u8, arg, "-femit-zir=")) {
|
|
|
|
emit_zir = .{ .yes = arg["-femit-zir=".len..] };
|
|
|
|
} else if (mem.eql(u8, arg, "-fno-emit-zir")) {
|
|
|
|
emit_zir = .no;
|
2019-12-10 23:08:33 -08:00
|
|
|
} else if (mem.eql(u8, arg, "-dynamic")) {
|
2020-05-15 20:54:13 -07:00
|
|
|
link_mode = .Dynamic;
|
|
|
|
} else if (mem.eql(u8, arg, "-static")) {
|
|
|
|
link_mode = .Static;
|
2019-12-10 23:08:33 -08:00
|
|
|
} else if (mem.eql(u8, arg, "--strip")) {
|
|
|
|
strip = true;
|
2020-05-15 12:20:42 -07:00
|
|
|
} else if (mem.eql(u8, arg, "--debug-tokenize")) {
|
|
|
|
debug_tokenize = true;
|
|
|
|
} else if (mem.eql(u8, arg, "--debug-ast-tree")) {
|
|
|
|
debug_ast_tree = true;
|
|
|
|
} else if (mem.eql(u8, arg, "--debug-ast-fmt")) {
|
|
|
|
debug_ast_fmt = true;
|
|
|
|
} else if (mem.eql(u8, arg, "--debug-link")) {
|
|
|
|
debug_link = true;
|
|
|
|
} else if (mem.eql(u8, arg, "--debug-ir")) {
|
|
|
|
debug_ir = true;
|
|
|
|
} else if (mem.eql(u8, arg, "--debug-codegen")) {
|
|
|
|
debug_codegen = true;
|
2019-12-10 23:08:33 -08:00
|
|
|
} else if (mem.startsWith(u8, arg, "-l")) {
|
|
|
|
try system_libs.append(arg[2..]);
|
|
|
|
} else {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("unrecognized parameter: '{}'", .{arg});
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2020-05-15 12:20:42 -07:00
|
|
|
} else if (mem.endsWith(u8, arg, ".s") or mem.endsWith(u8, arg, ".S")) {
|
|
|
|
std.debug.warn("assembly files not supported yet", .{});
|
|
|
|
process.exit(1);
|
2019-12-10 23:08:33 -08:00
|
|
|
} else if (mem.endsWith(u8, arg, ".o") or
|
|
|
|
mem.endsWith(u8, arg, ".obj") or
|
|
|
|
mem.endsWith(u8, arg, ".a") or
|
|
|
|
mem.endsWith(u8, arg, ".lib"))
|
|
|
|
{
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("object files and static libraries not supported yet", .{});
|
|
|
|
process.exit(1);
|
2019-12-10 23:08:33 -08:00
|
|
|
} else if (mem.endsWith(u8, arg, ".c") or
|
|
|
|
mem.endsWith(u8, arg, ".cpp"))
|
|
|
|
{
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("compilation of C and C++ source code requires LLVM extensions which are not implemented yet", .{});
|
|
|
|
process.exit(1);
|
|
|
|
} else if (mem.endsWith(u8, arg, ".so") or
|
|
|
|
mem.endsWith(u8, arg, ".dylib") or
|
|
|
|
mem.endsWith(u8, arg, ".dll"))
|
|
|
|
{
|
|
|
|
std.debug.warn("linking against dynamic libraries not yet supported", .{});
|
|
|
|
process.exit(1);
|
|
|
|
} else if (mem.endsWith(u8, arg, ".zig") or mem.endsWith(u8, arg, ".zir")) {
|
2019-12-10 23:08:33 -08:00
|
|
|
if (root_src_file) |other| {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("found another zig file '{}' after root source file '{}'", .{ arg, other });
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(1);
|
|
|
|
} else {
|
|
|
|
root_src_file = arg;
|
|
|
|
}
|
2018-06-21 22:49:32 -07:00
|
|
|
} else {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("unrecognized file extension of parameter '{}'", .{arg});
|
2018-04-12 03:23:58 -07:00
|
|
|
}
|
2018-03-28 09:30:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 22:49:32 -07:00
|
|
|
const root_name = if (provided_name) |n| n else blk: {
|
2019-12-10 23:08:33 -08:00
|
|
|
if (root_src_file) |file| {
|
2019-05-26 10:17:34 -07:00
|
|
|
const basename = fs.path.basename(file);
|
2020-04-04 10:15:08 -07:00
|
|
|
var it = mem.split(basename, ".");
|
2018-06-21 22:49:32 -07:00
|
|
|
break :blk it.next() orelse basename;
|
|
|
|
} else {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("--name [name] not provided and unable to infer\n", .{});
|
2019-05-26 10:17:34 -07:00
|
|
|
process.exit(1);
|
2018-06-21 22:49:32 -07:00
|
|
|
}
|
2018-04-12 03:23:58 -07:00
|
|
|
};
|
2018-03-28 09:30:41 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
if (system_libs.items.len != 0) {
|
|
|
|
std.debug.warn("linking against system libraries not yet supported", .{});
|
2019-05-26 10:17:34 -07:00
|
|
|
process.exit(1);
|
2018-04-12 03:23:58 -07:00
|
|
|
}
|
2018-03-28 09:30:41 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
var diags: std.zig.CrossTarget.ParseOptions.Diagnostics = .{};
|
|
|
|
const cross_target = std.zig.CrossTarget.parse(.{
|
|
|
|
.arch_os_abi = target_arch_os_abi,
|
|
|
|
.cpu_features = target_mcpu,
|
|
|
|
.dynamic_linker = target_dynamic_linker,
|
|
|
|
.diagnostics = &diags,
|
|
|
|
}) catch |err| switch (err) {
|
|
|
|
error.UnknownCpuModel => {
|
|
|
|
std.debug.warn("Unknown CPU: '{}'\nAvailable CPUs for architecture '{}':\n", .{
|
|
|
|
diags.cpu_name.?,
|
|
|
|
@tagName(diags.arch.?),
|
|
|
|
});
|
|
|
|
for (diags.arch.?.allCpuModels()) |cpu| {
|
|
|
|
std.debug.warn(" {}\n", .{cpu.name});
|
|
|
|
}
|
|
|
|
process.exit(1);
|
|
|
|
},
|
|
|
|
error.UnknownCpuFeature => {
|
|
|
|
std.debug.warn(
|
|
|
|
\\Unknown CPU feature: '{}'
|
|
|
|
\\Available CPU features for architecture '{}':
|
|
|
|
\\
|
|
|
|
, .{
|
|
|
|
diags.unknown_feature_name,
|
|
|
|
@tagName(diags.arch.?),
|
|
|
|
});
|
|
|
|
for (diags.arch.?.allFeaturesList()) |feature| {
|
|
|
|
std.debug.warn(" {}: {}\n", .{ feature.name, feature.description });
|
|
|
|
}
|
|
|
|
process.exit(1);
|
|
|
|
},
|
|
|
|
else => |e| return e,
|
|
|
|
};
|
|
|
|
|
|
|
|
const object_format: ?std.builtin.ObjectFormat = null;
|
|
|
|
var target_info = try std.zig.system.NativeTargetInfo.detect(gpa, cross_target);
|
|
|
|
if (target_info.cpu_detection_unimplemented) {
|
|
|
|
// TODO We want to just use detected_info.target but implementing
|
|
|
|
// CPU model & feature detection is todo so here we rely on LLVM.
|
|
|
|
std.debug.warn("CPU features detection is not yet available for this system without LLVM extensions\n", .{});
|
2019-05-26 10:17:34 -07:00
|
|
|
process.exit(1);
|
2018-04-12 03:23:58 -07:00
|
|
|
}
|
2018-03-28 09:30:41 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
const src_path = root_src_file orelse {
|
|
|
|
std.debug.warn("expected at least one file argument", .{});
|
|
|
|
process.exit(1);
|
|
|
|
};
|
2018-07-17 21:34:42 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
const bin_path = switch (emit_bin) {
|
|
|
|
.no => {
|
|
|
|
std.debug.warn("-fno-emit-bin not supported yet", .{});
|
|
|
|
process.exit(1);
|
|
|
|
},
|
2020-06-05 12:49:23 -07:00
|
|
|
.yes_default_path => switch (output_mode) {
|
|
|
|
.Exe => try std.fmt.allocPrint(arena, "{}{}", .{ root_name, target_info.target.exeFileExt() }),
|
|
|
|
.Lib => blk: {
|
|
|
|
const suffix = switch (link_mode orelse .Static) {
|
|
|
|
.Static => target_info.target.staticLibSuffix(),
|
|
|
|
.Dynamic => target_info.target.dynamicLibSuffix(),
|
|
|
|
};
|
|
|
|
break :blk try std.fmt.allocPrint(arena, "{}{}{}", .{
|
|
|
|
target_info.target.libPrefix(),
|
|
|
|
root_name,
|
|
|
|
suffix,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
.Obj => try std.fmt.allocPrint(arena, "{}{}", .{ root_name, target_info.target.oFileExt() }),
|
|
|
|
},
|
2020-05-15 12:20:42 -07:00
|
|
|
.yes => |p| p,
|
|
|
|
};
|
2018-06-29 12:39:55 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
const zir_out_path: ?[]const u8 = switch (emit_zir) {
|
|
|
|
.no => null,
|
|
|
|
.yes_default_path => blk: {
|
|
|
|
if (root_src_file) |rsf| {
|
|
|
|
if (mem.endsWith(u8, rsf, ".zir")) {
|
|
|
|
break :blk try std.fmt.allocPrint(arena, "{}.out.zir", .{root_name});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break :blk try std.fmt.allocPrint(arena, "{}.zir", .{root_name});
|
|
|
|
},
|
|
|
|
.yes => |p| p,
|
|
|
|
};
|
2018-04-12 03:23:58 -07:00
|
|
|
|
2020-05-15 20:54:13 -07:00
|
|
|
const root_pkg = try Package.create(gpa, fs.cwd(), ".", src_path);
|
|
|
|
defer root_pkg.destroy();
|
|
|
|
|
|
|
|
var module = try Module.init(gpa, .{
|
2020-05-15 12:20:42 -07:00
|
|
|
.target = target_info.target,
|
|
|
|
.output_mode = output_mode,
|
2020-05-15 20:54:13 -07:00
|
|
|
.root_pkg = root_pkg,
|
|
|
|
.bin_file_dir = fs.cwd(),
|
|
|
|
.bin_file_path = bin_path,
|
|
|
|
.link_mode = link_mode,
|
|
|
|
.object_format = object_format,
|
|
|
|
.optimize_mode = build_mode,
|
2020-05-15 12:20:42 -07:00
|
|
|
});
|
|
|
|
defer module.deinit();
|
|
|
|
|
|
|
|
const stdin = std.io.getStdIn().inStream();
|
|
|
|
const stderr = std.io.getStdErr().outStream();
|
|
|
|
var repl_buf: [1024]u8 = undefined;
|
|
|
|
|
|
|
|
try updateModule(gpa, &module, zir_out_path);
|
|
|
|
|
|
|
|
while (watch) {
|
|
|
|
try stderr.print("🦎 ", .{});
|
2020-05-16 12:44:20 -07:00
|
|
|
if (output_mode == .Exe) {
|
|
|
|
try module.makeBinFileExecutable();
|
|
|
|
}
|
2020-05-15 12:20:42 -07:00
|
|
|
if (stdin.readUntilDelimiterOrEof(&repl_buf, '\n') catch |err| {
|
|
|
|
try stderr.print("\nUnable to parse command: {}\n", .{@errorName(err)});
|
|
|
|
continue;
|
|
|
|
}) |line| {
|
|
|
|
if (mem.eql(u8, line, "update")) {
|
2020-05-16 12:44:20 -07:00
|
|
|
if (output_mode == .Exe) {
|
|
|
|
try module.makeBinFileWritable();
|
|
|
|
}
|
2020-05-15 12:20:42 -07:00
|
|
|
try updateModule(gpa, &module, zir_out_path);
|
|
|
|
} else if (mem.eql(u8, line, "exit")) {
|
|
|
|
break;
|
|
|
|
} else if (mem.eql(u8, line, "help")) {
|
|
|
|
try stderr.writeAll(repl_help);
|
|
|
|
} else {
|
|
|
|
try stderr.print("unknown command: {}\n", .{line});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2018-07-17 21:34:42 -07:00
|
|
|
}
|
2020-05-15 12:20:42 -07:00
|
|
|
}
|
2018-07-17 21:34:42 -07:00
|
|
|
|
2020-05-15 18:44:33 -07:00
|
|
|
fn updateModule(gpa: *Allocator, module: *Module, zir_out_path: ?[]const u8) !void {
|
2020-05-15 12:20:42 -07:00
|
|
|
try module.update();
|
2018-03-28 09:30:41 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
var errors = try module.getAllErrorsAlloc();
|
|
|
|
defer errors.deinit(module.allocator);
|
2018-04-12 03:23:58 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
if (errors.list.len != 0) {
|
|
|
|
for (errors.list) |full_err_msg| {
|
|
|
|
std.debug.warn("{}:{}:{}: error: {}\n", .{
|
|
|
|
full_err_msg.src_path,
|
|
|
|
full_err_msg.line + 1,
|
|
|
|
full_err_msg.column + 1,
|
|
|
|
full_err_msg.msg,
|
|
|
|
});
|
|
|
|
}
|
2018-03-28 09:30:41 -07:00
|
|
|
}
|
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
if (zir_out_path) |zop| {
|
2020-05-15 18:44:33 -07:00
|
|
|
var new_zir_module = try zir.emit(gpa, module.*);
|
2020-05-15 12:20:42 -07:00
|
|
|
defer new_zir_module.deinit(gpa);
|
2018-04-12 03:23:58 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
const baf = try io.BufferedAtomicFile.create(gpa, fs.cwd(), zop, .{});
|
|
|
|
defer baf.destroy();
|
2018-04-12 03:23:58 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
try new_zir_module.writeToStream(gpa, baf.stream());
|
2018-06-29 12:39:55 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
try baf.finish();
|
2018-06-29 12:39:55 -07:00
|
|
|
}
|
2018-04-12 03:23:58 -07:00
|
|
|
}
|
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
const repl_help =
|
|
|
|
\\Commands:
|
|
|
|
\\ update Detect changes to source files and update output files.
|
|
|
|
\\ help Print this text
|
|
|
|
\\ exit Quit this repl
|
|
|
|
\\
|
|
|
|
;
|
|
|
|
|
2019-02-15 21:42:56 -08:00
|
|
|
pub const usage_fmt =
|
2018-04-12 03:23:58 -07:00
|
|
|
\\usage: zig fmt [file]...
|
|
|
|
\\
|
|
|
|
\\ Formats the input files and modifies them in-place.
|
2018-11-14 16:50:55 -08:00
|
|
|
\\ Arguments can be files or directories, which are searched
|
|
|
|
\\ recursively.
|
2018-04-12 03:23:58 -07:00
|
|
|
\\
|
|
|
|
\\Options:
|
|
|
|
\\ --help Print this help and exit
|
2018-05-30 15:37:12 -07:00
|
|
|
\\ --color [auto|off|on] Enable or disable colored error messages
|
2018-11-14 16:50:55 -08:00
|
|
|
\\ --stdin Format code from stdin; output to stdout
|
|
|
|
\\ --check List non-conforming files and exit with an error
|
|
|
|
\\ if the list is non-empty
|
2018-04-12 03:23:58 -07:00
|
|
|
\\
|
|
|
|
\\
|
2018-05-26 15:16:39 -07:00
|
|
|
;
|
2018-04-12 03:23:58 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const Fmt = struct {
|
2020-05-15 12:20:42 -07:00
|
|
|
seen: SeenMap,
|
2018-06-18 10:55:03 -07:00
|
|
|
any_error: bool,
|
2020-05-15 12:20:42 -07:00
|
|
|
color: Color,
|
|
|
|
gpa: *Allocator,
|
2018-06-18 10:55:03 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
const SeenMap = std.BufSet;
|
2018-06-18 10:55:03 -07:00
|
|
|
};
|
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
|
2020-03-17 20:03:45 -07:00
|
|
|
const stderr_file = io.getStdErr();
|
2020-05-15 12:20:42 -07:00
|
|
|
var color: Color = .Auto;
|
2019-12-10 23:08:33 -08:00
|
|
|
var stdin_flag: bool = false;
|
|
|
|
var check_flag: bool = false;
|
2020-05-15 12:20:42 -07:00
|
|
|
var input_files = ArrayList([]const u8).init(gpa);
|
2018-04-12 03:23:58 -07:00
|
|
|
|
2019-12-10 23:08:33 -08:00
|
|
|
{
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < args.len) : (i += 1) {
|
|
|
|
const arg = args[i];
|
|
|
|
if (mem.startsWith(u8, arg, "-")) {
|
|
|
|
if (mem.eql(u8, arg, "--help")) {
|
2020-03-17 20:03:45 -07:00
|
|
|
const stdout = io.getStdOut().outStream();
|
|
|
|
try stdout.writeAll(usage_fmt);
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(0);
|
|
|
|
} else if (mem.eql(u8, arg, "--color")) {
|
|
|
|
if (i + 1 >= args.len) {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("expected [auto|on|off] after --color\n", .{});
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
const next_arg = args[i];
|
|
|
|
if (mem.eql(u8, next_arg, "auto")) {
|
|
|
|
color = .Auto;
|
|
|
|
} else if (mem.eql(u8, next_arg, "on")) {
|
|
|
|
color = .On;
|
|
|
|
} else if (mem.eql(u8, next_arg, "off")) {
|
|
|
|
color = .Off;
|
|
|
|
} else {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("expected [auto|on|off] after --color, found '{}'\n", .{next_arg});
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
} else if (mem.eql(u8, arg, "--stdin")) {
|
|
|
|
stdin_flag = true;
|
|
|
|
} else if (mem.eql(u8, arg, "--check")) {
|
|
|
|
check_flag = true;
|
|
|
|
} else {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("unrecognized parameter: '{}'", .{arg});
|
2019-12-10 23:08:33 -08:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try input_files.append(arg);
|
|
|
|
}
|
2018-05-30 15:26:09 -07:00
|
|
|
}
|
2019-12-10 23:08:33 -08:00
|
|
|
}
|
2018-05-30 15:26:09 -07:00
|
|
|
|
2019-12-10 23:08:33 -08:00
|
|
|
if (stdin_flag) {
|
2020-05-15 12:20:42 -07:00
|
|
|
if (input_files.items.len != 0) {
|
|
|
|
std.debug.warn("cannot use --stdin with positional arguments\n", .{});
|
2019-05-26 10:17:34 -07:00
|
|
|
process.exit(1);
|
2018-07-14 05:07:47 -07:00
|
|
|
}
|
|
|
|
|
2020-03-17 20:03:45 -07:00
|
|
|
const stdin = io.getStdIn().inStream();
|
2018-07-14 05:07:47 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
const source_code = try stdin.readAllAlloc(gpa, max_src_size);
|
|
|
|
defer gpa.free(source_code);
|
2018-07-14 05:07:47 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
const tree = std.zig.parse(gpa, source_code) catch |err| {
|
|
|
|
std.debug.warn("error parsing stdin: {}\n", .{err});
|
2019-05-26 10:17:34 -07:00
|
|
|
process.exit(1);
|
2018-07-14 05:07:47 -07:00
|
|
|
};
|
|
|
|
defer tree.deinit();
|
|
|
|
|
2020-05-20 10:53:53 -07:00
|
|
|
for (tree.errors) |parse_error| {
|
2020-05-15 12:20:42 -07:00
|
|
|
try printErrMsgToFile(gpa, parse_error, tree, "<stdin>", stderr_file, color);
|
2018-07-14 05:07:47 -07:00
|
|
|
}
|
|
|
|
if (tree.errors.len != 0) {
|
2019-05-26 10:17:34 -07:00
|
|
|
process.exit(1);
|
2018-07-14 05:07:47 -07:00
|
|
|
}
|
2019-12-10 23:08:33 -08:00
|
|
|
if (check_flag) {
|
2020-05-15 12:20:42 -07:00
|
|
|
const anything_changed = try std.zig.render(gpa, io.null_out_stream, tree);
|
|
|
|
const code = if (anything_changed) @as(u8, 1) else @as(u8, 0);
|
2019-05-26 10:17:34 -07:00
|
|
|
process.exit(code);
|
2018-11-14 16:50:55 -08:00
|
|
|
}
|
2018-07-14 05:07:47 -07:00
|
|
|
|
2020-03-17 20:03:45 -07:00
|
|
|
const stdout = io.getStdOut().outStream();
|
2020-05-15 12:20:42 -07:00
|
|
|
_ = try std.zig.render(gpa, stdout, tree);
|
2018-07-14 05:07:47 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
if (input_files.items.len == 0) {
|
|
|
|
std.debug.warn("expected at least one source file argument\n", .{});
|
2019-05-26 10:17:34 -07:00
|
|
|
process.exit(1);
|
2018-07-14 05:07:47 -07:00
|
|
|
}
|
|
|
|
|
2019-11-26 00:27:51 -08:00
|
|
|
var fmt = Fmt{
|
2020-05-15 12:20:42 -07:00
|
|
|
.gpa = gpa,
|
|
|
|
.seen = Fmt.SeenMap.init(gpa),
|
2019-11-26 00:27:51 -08:00
|
|
|
.any_error = false,
|
|
|
|
.color = color,
|
|
|
|
};
|
|
|
|
|
2020-03-30 11:23:22 -07:00
|
|
|
for (input_files.span()) |file_path| {
|
2020-05-15 12:20:42 -07:00
|
|
|
try fmtPath(&fmt, file_path, check_flag);
|
2019-11-26 00:27:51 -08:00
|
|
|
}
|
|
|
|
if (fmt.any_error) {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2018-08-02 14:24:15 -07:00
|
|
|
}
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const FmtError = error{
|
2018-08-02 14:24:15 -07:00
|
|
|
SystemResources,
|
|
|
|
OperationAborted,
|
|
|
|
IoPending,
|
|
|
|
BrokenPipe,
|
|
|
|
Unexpected,
|
|
|
|
WouldBlock,
|
|
|
|
FileClosed,
|
|
|
|
DestinationAddressRequired,
|
|
|
|
DiskQuota,
|
|
|
|
FileTooBig,
|
|
|
|
InputOutput,
|
|
|
|
NoSpaceLeft,
|
|
|
|
AccessDenied,
|
|
|
|
OutOfMemory,
|
|
|
|
RenameAcrossMountPoints,
|
|
|
|
ReadOnlyFileSystem,
|
|
|
|
LinkQuotaExceeded,
|
|
|
|
FileBusy,
|
2019-05-26 10:17:34 -07:00
|
|
|
} || fs.File.OpenError;
|
2018-08-02 14:24:15 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
|
|
|
|
// get the real path here to avoid Windows failing on relative file paths with . or .. in them
|
|
|
|
var real_path = fs.realpathAlloc(fmt.gpa, file_path) catch |err| {
|
|
|
|
std.debug.warn("unable to open '{}': {}\n", .{ file_path, err });
|
|
|
|
fmt.any_error = true;
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
defer fmt.gpa.free(real_path);
|
2018-02-09 15:27:50 -08:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
if (fmt.seen.exists(real_path)) return;
|
|
|
|
try fmt.seen.put(real_path);
|
2018-02-10 11:52:39 -08:00
|
|
|
|
2020-06-16 10:07:55 -07:00
|
|
|
const source_file = fs.cwd().openFile(real_path, .{}) catch |err| switch (err) {
|
2019-11-26 00:27:51 -08:00
|
|
|
error.IsDir, error.AccessDenied => {
|
2020-03-18 11:45:01 -07:00
|
|
|
var dir = try fs.cwd().openDir(file_path, .{ .iterate = true });
|
2019-11-26 00:27:51 -08:00
|
|
|
defer dir.close();
|
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
var dir_it = dir.iterate();
|
|
|
|
|
|
|
|
while (try dir_it.next()) |entry| {
|
2019-11-26 00:27:51 -08:00
|
|
|
if (entry.kind == .Directory or mem.endsWith(u8, entry.name, ".zig")) {
|
2020-05-15 12:20:42 -07:00
|
|
|
const full_path = try fs.path.join(fmt.gpa, &[_][]const u8{ file_path, entry.name });
|
|
|
|
try fmtPath(fmt, full_path, check_mode);
|
2019-11-26 00:27:51 -08:00
|
|
|
}
|
|
|
|
}
|
2020-05-15 12:20:42 -07:00
|
|
|
return;
|
2019-11-26 00:27:51 -08:00
|
|
|
},
|
|
|
|
else => {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("unable to open '{}': {}\n", .{ file_path, err });
|
2019-11-26 00:27:51 -08:00
|
|
|
fmt.any_error = true;
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
};
|
2020-06-16 10:07:55 -07:00
|
|
|
defer source_file.close();
|
|
|
|
|
|
|
|
const source_code = source_file.reader().readAllAlloc(fmt.gpa, max_src_size) catch |err| {
|
|
|
|
std.debug.warn("unable to read '{}': {}\n", .{ file_path, err });
|
|
|
|
fmt.any_error = true;
|
|
|
|
return;
|
|
|
|
};
|
2020-05-15 12:20:42 -07:00
|
|
|
defer fmt.gpa.free(source_code);
|
2018-05-30 15:26:09 -07:00
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
const tree = std.zig.parse(fmt.gpa, source_code) catch |err| {
|
|
|
|
std.debug.warn("error parsing file '{}': {}\n", .{ file_path, err });
|
2018-08-02 14:24:15 -07:00
|
|
|
fmt.any_error = true;
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
defer tree.deinit();
|
2018-04-12 03:23:58 -07:00
|
|
|
|
2020-05-20 10:53:53 -07:00
|
|
|
for (tree.errors) |parse_error| {
|
2020-05-15 12:20:42 -07:00
|
|
|
try printErrMsgToFile(fmt.gpa, parse_error, tree, file_path, std.io.getStdErr(), fmt.color);
|
2018-08-02 14:24:15 -07:00
|
|
|
}
|
|
|
|
if (tree.errors.len != 0) {
|
|
|
|
fmt.any_error = true;
|
|
|
|
return;
|
2018-02-09 15:27:50 -08:00
|
|
|
}
|
2018-06-02 01:49:35 -07:00
|
|
|
|
2018-11-14 16:50:55 -08:00
|
|
|
if (check_mode) {
|
2020-05-15 12:20:42 -07:00
|
|
|
const anything_changed = try std.zig.render(fmt.gpa, io.null_out_stream, tree);
|
2018-11-14 16:50:55 -08:00
|
|
|
if (anything_changed) {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("{}\n", .{file_path});
|
2018-11-14 16:50:55 -08:00
|
|
|
fmt.any_error = true;
|
|
|
|
}
|
|
|
|
} else {
|
2020-06-16 10:07:55 -07:00
|
|
|
const baf = try io.BufferedAtomicFile.create(fmt.gpa, fs.cwd(), real_path, .{ .mode = try source_file.mode() });
|
2018-11-14 16:50:55 -08:00
|
|
|
defer baf.destroy();
|
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
const anything_changed = try std.zig.render(fmt.gpa, baf.stream(), tree);
|
2018-11-14 16:50:55 -08:00
|
|
|
if (anything_changed) {
|
2020-05-15 12:20:42 -07:00
|
|
|
std.debug.warn("{}\n", .{file_path});
|
2018-11-14 16:50:55 -08:00
|
|
|
try baf.finish();
|
|
|
|
}
|
2018-06-02 01:49:35 -07:00
|
|
|
}
|
2018-02-09 15:27:50 -08:00
|
|
|
}
|
|
|
|
|
2020-05-15 12:20:42 -07:00
|
|
|
fn printErrMsgToFile(
|
|
|
|
gpa: *mem.Allocator,
|
2020-05-20 10:53:53 -07:00
|
|
|
parse_error: ast.Error,
|
2020-05-15 12:20:42 -07:00
|
|
|
tree: *ast.Tree,
|
|
|
|
path: []const u8,
|
|
|
|
file: fs.File,
|
|
|
|
color: Color,
|
|
|
|
) !void {
|
|
|
|
const color_on = switch (color) {
|
|
|
|
.Auto => file.isTty(),
|
|
|
|
.On => true,
|
|
|
|
.Off => false,
|
|
|
|
};
|
|
|
|
const lok_token = parse_error.loc();
|
|
|
|
const span_first = lok_token;
|
|
|
|
const span_last = lok_token;
|
|
|
|
|
2020-05-23 13:24:03 -07:00
|
|
|
const first_token = tree.token_locs[span_first];
|
|
|
|
const last_token = tree.token_locs[span_last];
|
|
|
|
const start_loc = tree.tokenLocationLoc(0, first_token);
|
|
|
|
const end_loc = tree.tokenLocationLoc(first_token.end, last_token);
|
2020-05-15 12:20:42 -07:00
|
|
|
|
|
|
|
var text_buf = std.ArrayList(u8).init(gpa);
|
|
|
|
defer text_buf.deinit();
|
|
|
|
const out_stream = text_buf.outStream();
|
2020-05-23 13:24:03 -07:00
|
|
|
try parse_error.render(tree.token_ids, out_stream);
|
2020-05-15 12:20:42 -07:00
|
|
|
const text = text_buf.span();
|
|
|
|
|
|
|
|
const stream = file.outStream();
|
|
|
|
try stream.print("{}:{}:{}: error: {}\n", .{ path, start_loc.line + 1, start_loc.column + 1, text });
|
|
|
|
|
|
|
|
if (!color_on) return;
|
|
|
|
|
|
|
|
// Print \r and \t as one space each so that column counts line up
|
|
|
|
for (tree.source[start_loc.line_start..start_loc.line_end]) |byte| {
|
|
|
|
try stream.writeByte(switch (byte) {
|
|
|
|
'\r', '\t' => ' ',
|
|
|
|
else => byte,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
try stream.writeByte('\n');
|
|
|
|
try stream.writeByteNTimes(' ', start_loc.column);
|
|
|
|
try stream.writeByteNTimes('~', last_token.end - first_token.start);
|
|
|
|
try stream.writeByte('\n');
|
2018-04-12 03:23:58 -07:00
|
|
|
}
|
|
|
|
|
2019-04-21 16:37:39 -07:00
|
|
|
pub const info_zen =
|
|
|
|
\\
|
|
|
|
\\ * Communicate intent precisely.
|
|
|
|
\\ * Edge cases matter.
|
|
|
|
\\ * Favor reading code over writing code.
|
|
|
|
\\ * Only one obvious way to do things.
|
|
|
|
\\ * Runtime crashes are better than bugs.
|
|
|
|
\\ * Compile errors are better than runtime crashes.
|
|
|
|
\\ * Incremental improvements.
|
|
|
|
\\ * Avoid local maximums.
|
|
|
|
\\ * Reduce the amount one must remember.
|
|
|
|
\\ * Minimize energy spent on coding style.
|
2020-05-15 12:20:42 -07:00
|
|
|
\\ * Resource deallocation must succeed.
|
2019-04-21 16:37:39 -07:00
|
|
|
\\ * Together we serve end users.
|
|
|
|
\\
|
|
|
|
\\
|
|
|
|
;
|