| ... | ... | @@ -140,6 +140,10 @@ pub fn mainArgs(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v |
| 140 | 140 | return cmdFmt(gpa, cmd_args); |
| 141 | 141 | } else if (mem.eql(u8, cmd, "libc")) { |
| 142 | 142 | return cmdLibC(gpa, cmd_args); |
| 143 | } else if (mem.eql(u8, cmd, "init-exe")) { |
| 144 | return cmdInit(gpa, arena, cmd_args, .Exe); |
| 145 | } else if (mem.eql(u8, cmd, "init-lib")) { |
| 146 | return cmdInit(gpa, arena, cmd_args, .Lib); |
| 143 | 147 | } else if (mem.eql(u8, cmd, "targets")) { |
| 144 | 148 | const info = try detectNativeTargetInfo(arena, .{}); |
| 145 | 149 | const stdout = io.getStdOut().outStream(); |
| ... | ... | @@ -1520,6 +1524,97 @@ pub fn cmdLibC(gpa: *Allocator, args: []const []const u8) !void { |
| 1520 | 1524 | } |
| 1521 | 1525 | } |
| 1522 | 1526 | |
| 1527 | pub const usage_init = |
| 1528 | \\Usage: zig init-exe |
| 1529 | \\ zig init-lib |
| 1530 | \\ |
| 1531 | \\ Initializes a `zig build` project in the current working |
| 1532 | \\ directory. |
| 1533 | \\ |
| 1534 | \\Options: |
| 1535 | \\ --help Print this help and exit |
| 1536 | \\ |
| 1537 | \\ |
| 1538 | ; |
| 1539 | |
| 1540 | pub fn cmdInit( |
| 1541 | gpa: *Allocator, |
| 1542 | arena: *Allocator, |
| 1543 | args: []const []const u8, |
| 1544 | output_mode: std.builtin.OutputMode, |
| 1545 | ) !void { |
| 1546 | { |
| 1547 | var i: usize = 0; |
| 1548 | while (i < args.len) : (i += 1) { |
| 1549 | const arg = args[i]; |
| 1550 | if (mem.startsWith(u8, arg, "-")) { |
| 1551 | if (mem.eql(u8, arg, "--help")) { |
| 1552 | try io.getStdOut().writeAll(usage_init); |
| 1553 | process.exit(0); |
| 1554 | } else { |
| 1555 | fatal("unrecognized parameter: '{}'", .{arg}); |
| 1556 | } |
| 1557 | } else { |
| 1558 | fatal("unexpected extra parameter: '{}'", .{arg}); |
| 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | const self_exe_path = try fs.selfExePathAlloc(arena); |
| 1563 | var zig_lib_directory = introspect.findZigLibDirFromSelfExe(arena, self_exe_path) catch |err| { |
| 1564 | fatal("unable to find zig installation directory: {}\n", .{@errorName(err)}); |
| 1565 | }; |
| 1566 | defer zig_lib_directory.handle.close(); |
| 1567 | |
| 1568 | const s = fs.path.sep_str; |
| 1569 | const template_sub_path = switch (output_mode) { |
| 1570 | .Obj => unreachable, |
| 1571 | .Lib => "std" ++ s ++ "special" ++ s ++ "init-lib", |
| 1572 | .Exe => "std" ++ s ++ "special" ++ s ++ "init-exe", |
| 1573 | }; |
| 1574 | var template_dir = try zig_lib_directory.handle.openDir(template_sub_path, .{}); |
| 1575 | defer template_dir.close(); |
| 1576 | |
| 1577 | const cwd_path = try process.getCwdAlloc(arena); |
| 1578 | const cwd_basename = fs.path.basename(cwd_path); |
| 1579 | |
| 1580 | const max_bytes = 10 * 1024 * 1024; |
| 1581 | const build_zig_contents = template_dir.readFileAlloc(arena, "build.zig", max_bytes) catch |err| { |
| 1582 | fatal("unable to read template file 'build.zig': {}", .{@errorName(err)}); |
| 1583 | }; |
| 1584 | var modified_build_zig_contents = std.ArrayList(u8).init(arena); |
| 1585 | try modified_build_zig_contents.ensureCapacity(build_zig_contents.len); |
| 1586 | for (build_zig_contents) |c| { |
| 1587 | if (c == '$') { |
| 1588 | try modified_build_zig_contents.appendSlice(cwd_basename); |
| 1589 | } else { |
| 1590 | try modified_build_zig_contents.append(c); |
| 1591 | } |
| 1592 | } |
| 1593 | const main_zig_contents = template_dir.readFileAlloc(arena, "src" ++ s ++ "main.zig", max_bytes) catch |err| { |
| 1594 | fatal("unable to read template file 'main.zig': {}", .{@errorName(err)}); |
| 1595 | }; |
| 1596 | if (fs.cwd().access("build.zig", .{})) |_| { |
| 1597 | fatal("existing build.zig file would be overwritten", .{}); |
| 1598 | } else |err| switch (err) { |
| 1599 | error.FileNotFound => {}, |
| 1600 | else => fatal("unable to test existence of build.zig: {}\n", .{@errorName(err)}), |
| 1601 | } |
| 1602 | var src_dir = try fs.cwd().makeOpenPath("src", .{}); |
| 1603 | defer src_dir.close(); |
| 1604 | |
| 1605 | try src_dir.writeFile("main.zig", main_zig_contents); |
| 1606 | try fs.cwd().writeFile("build.zig", modified_build_zig_contents.items); |
| 1607 | |
| 1608 | std.log.info("Created build.zig", .{}); |
| 1609 | std.log.info("Created src" ++ s ++ "main.zig", .{}); |
| 1610 | |
| 1611 | switch (output_mode) { |
| 1612 | .Lib => std.log.info("Next, try `zig build --help` or `zig build test`", .{}), |
| 1613 | .Exe => std.log.info("Next, try `zig build --help` or `zig build run`", .{}), |
| 1614 | .Obj => unreachable, |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1523 | 1618 | pub const usage_fmt = |
| 1524 | 1619 | \\Usage: zig fmt [file]... |
| 1525 | 1620 | \\ |