2015-08-06 07:22:21 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Andrew Kelley
|
|
|
|
*
|
|
|
|
* This file is part of zig, which is MIT licensed.
|
|
|
|
* See http://opensource.org/licenses/MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2015-08-06 11:26:58 +08:00
|
|
|
#include "buffer.hpp"
|
2015-11-24 13:47:25 +08:00
|
|
|
#include "codegen.hpp"
|
2015-12-01 10:58:53 +08:00
|
|
|
#include "os.hpp"
|
2015-08-06 11:26:58 +08:00
|
|
|
|
2015-08-06 07:22:21 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-11-05 15:05:25 +08:00
|
|
|
static int usage(const char *arg0) {
|
|
|
|
fprintf(stderr, "Usage: %s [command] [options] target\n"
|
|
|
|
"Commands:\n"
|
2015-11-28 12:24:11 +08:00
|
|
|
" build create executable, object, or library from target\n"
|
|
|
|
" version print version number and exit\n"
|
|
|
|
"Optional Options:\n"
|
|
|
|
" --release build with optimizations on and debug protection off\n"
|
|
|
|
" --static output will be statically linked\n"
|
|
|
|
" --strip exclude debug symbols\n"
|
|
|
|
" --export [exe|lib|obj] override output type\n"
|
|
|
|
" --name [name] override output name\n"
|
|
|
|
" --output [file] override destination path\n"
|
2015-12-01 10:58:53 +08:00
|
|
|
" --verbose turn on compiler debug output\n"
|
2015-08-06 07:22:21 +08:00
|
|
|
, arg0);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-11-28 12:24:11 +08:00
|
|
|
static int version(void) {
|
|
|
|
printf("%s\n", ZIG_VERSION_STRING);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
struct Build {
|
|
|
|
const char *in_file;
|
|
|
|
const char *out_file;
|
|
|
|
bool release;
|
|
|
|
bool strip;
|
|
|
|
bool is_static;
|
|
|
|
OutType out_type;
|
|
|
|
const char *out_name;
|
|
|
|
bool verbose;
|
|
|
|
};
|
2015-08-06 07:22:21 +08:00
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
static int build(const char *arg0, Build *b) {
|
|
|
|
if (!b->in_file)
|
2015-08-06 07:22:21 +08:00
|
|
|
return usage(arg0);
|
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
Buf in_file_buf = BUF_INIT;
|
|
|
|
buf_init_from_str(&in_file_buf, b->in_file);
|
2015-08-06 07:22:21 +08:00
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
Buf root_source_dir = BUF_INIT;
|
|
|
|
Buf root_source_code = BUF_INIT;
|
|
|
|
Buf root_source_name = BUF_INIT;
|
|
|
|
if (buf_eql_str(&in_file_buf, "-")) {
|
|
|
|
os_get_cwd(&root_source_dir);
|
|
|
|
os_fetch_file(stdin, &root_source_code);
|
|
|
|
buf_init_from_str(&root_source_name, "");
|
2015-11-24 13:47:25 +08:00
|
|
|
} else {
|
2015-12-01 10:58:53 +08:00
|
|
|
os_path_split(&in_file_buf, &root_source_dir, &root_source_name);
|
|
|
|
os_fetch_file_path(buf_create_from_str(b->in_file), &root_source_code);
|
2015-11-24 13:47:25 +08:00
|
|
|
}
|
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
CodeGen *g = codegen_create(&root_source_dir);
|
|
|
|
codegen_set_build_type(g, b->release ? CodeGenBuildTypeRelease : CodeGenBuildTypeDebug);
|
|
|
|
codegen_set_strip(g, b->strip);
|
|
|
|
codegen_set_is_static(g, b->is_static);
|
|
|
|
if (b->out_type != OutTypeUnknown)
|
|
|
|
codegen_set_out_type(g, b->out_type);
|
|
|
|
if (b->out_name)
|
|
|
|
codegen_set_out_name(g, buf_create_from_str(b->out_name));
|
|
|
|
codegen_set_verbose(g, b->verbose);
|
2015-12-01 13:53:37 +08:00
|
|
|
codegen_add_root_code(g, &root_source_name, &root_source_code);
|
2015-12-01 10:58:53 +08:00
|
|
|
codegen_link(g, b->out_file);
|
2015-11-25 04:00:38 +08:00
|
|
|
|
2015-11-05 15:05:25 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Cmd {
|
|
|
|
CmdNone,
|
|
|
|
CmdBuild,
|
2015-11-28 12:24:11 +08:00
|
|
|
CmdVersion,
|
2015-11-05 15:05:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
char *arg0 = argv[0];
|
2015-11-28 12:24:11 +08:00
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
Build b = {0};
|
2015-11-05 15:05:25 +08:00
|
|
|
Cmd cmd = CmdNone;
|
2015-12-01 10:58:53 +08:00
|
|
|
|
2015-11-05 15:05:25 +08:00
|
|
|
for (int i = 1; i < argc; i += 1) {
|
|
|
|
char *arg = argv[i];
|
|
|
|
if (arg[0] == '-' && arg[1] == '-') {
|
2015-11-28 12:24:11 +08:00
|
|
|
if (strcmp(arg, "--release") == 0) {
|
2015-12-01 10:58:53 +08:00
|
|
|
b.release = true;
|
2015-11-25 13:32:26 +08:00
|
|
|
} else if (strcmp(arg, "--strip") == 0) {
|
2015-12-01 10:58:53 +08:00
|
|
|
b.strip = true;
|
2015-11-25 13:32:26 +08:00
|
|
|
} else if (strcmp(arg, "--static") == 0) {
|
2015-12-01 10:58:53 +08:00
|
|
|
b.is_static = true;
|
|
|
|
} else if (strcmp(arg, "--verbose") == 0) {
|
|
|
|
b.verbose = true;
|
2015-11-05 15:05:25 +08:00
|
|
|
} else if (i + 1 >= argc) {
|
|
|
|
return usage(arg0);
|
|
|
|
} else {
|
|
|
|
i += 1;
|
|
|
|
if (strcmp(arg, "--output") == 0) {
|
2015-12-01 10:58:53 +08:00
|
|
|
b.out_file = argv[i];
|
2015-11-28 12:24:11 +08:00
|
|
|
} else if (strcmp(arg, "--export") == 0) {
|
|
|
|
if (strcmp(argv[i], "exe") == 0) {
|
2015-12-01 10:58:53 +08:00
|
|
|
b.out_type = OutTypeExe;
|
2015-11-28 12:24:11 +08:00
|
|
|
} else if (strcmp(argv[i], "lib") == 0) {
|
2015-12-01 10:58:53 +08:00
|
|
|
b.out_type = OutTypeLib;
|
2015-11-28 12:24:11 +08:00
|
|
|
} else if (strcmp(argv[i], "obj") == 0) {
|
2015-12-01 10:58:53 +08:00
|
|
|
b.out_type = OutTypeObj;
|
2015-11-28 12:24:11 +08:00
|
|
|
} else {
|
|
|
|
return usage(arg0);
|
|
|
|
}
|
|
|
|
} else if (strcmp(arg, "--name") == 0) {
|
2015-12-01 10:58:53 +08:00
|
|
|
b.out_name = argv[i];
|
2015-11-05 15:05:25 +08:00
|
|
|
} else {
|
|
|
|
return usage(arg0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (cmd == CmdNone) {
|
|
|
|
if (strcmp(arg, "build") == 0) {
|
|
|
|
cmd = CmdBuild;
|
2015-11-28 12:24:11 +08:00
|
|
|
} else if (strcmp(arg, "version") == 0) {
|
|
|
|
cmd = CmdVersion;
|
2015-11-05 15:05:25 +08:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Unrecognized command: %s\n", arg);
|
|
|
|
return usage(arg0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (cmd) {
|
|
|
|
case CmdNone:
|
2015-11-24 12:30:12 +08:00
|
|
|
zig_unreachable();
|
2015-11-05 15:05:25 +08:00
|
|
|
case CmdBuild:
|
2015-12-01 10:58:53 +08:00
|
|
|
if (!b.in_file) {
|
|
|
|
b.in_file = arg;
|
2015-11-05 15:05:25 +08:00
|
|
|
} else {
|
|
|
|
return usage(arg0);
|
|
|
|
}
|
|
|
|
break;
|
2015-11-28 12:24:11 +08:00
|
|
|
case CmdVersion:
|
|
|
|
return usage(arg0);
|
2015-11-05 15:05:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case CmdNone:
|
|
|
|
return usage(arg0);
|
|
|
|
case CmdBuild:
|
2015-12-01 10:58:53 +08:00
|
|
|
return build(arg0, &b);
|
2015-11-28 12:24:11 +08:00
|
|
|
case CmdVersion:
|
|
|
|
return version();
|
2015-11-05 15:05:25 +08:00
|
|
|
}
|
2015-11-05 15:06:36 +08:00
|
|
|
|
2015-11-24 12:30:12 +08:00
|
|
|
zig_unreachable();
|
2015-11-05 15:05:25 +08:00
|
|
|
}
|