Add -q option, make --run-once implicitly -q.

Add `-v`, `-vv` and `-vvv` options for increasing verbosity, replacing debug-log and debug-stats options. #1601
This commit is contained in:
Christoffer Lerno
2024-12-03 23:37:23 +01:00
parent 2c9d2d4fd7
commit 6281f8ff89
9 changed files with 63 additions and 35 deletions

View File

@@ -9,6 +9,8 @@
- Improve support for Windows cross compilation on targets with case sensitive file systems.
- Add "sources" support to library `manifest.json`, defaults to root folder if unspecified.
- Add char_at method in DString and operators [], len, []= and &[].
- Add `-q` option, make `--run-once` implicitly `-q`.
- Add `-v`, `-vv` and `-vvv` options for increasing verbosity, replacing debug-log and debug-stats options.
### Fixes
- Fix bug where `a > 0 ? f() : g()` could cause a compiler crash if both returned `void!`.

View File

@@ -479,6 +479,7 @@ typedef struct BuildOptions_
bool print_output;
bool print_input;
bool run_once;
int verbosity_level;
const char *panicfn;
const char *benchfn;
const char *testfn;
@@ -589,6 +590,7 @@ typedef struct
bool emit_object_files;
bool benchmarking;
bool testing;
bool silent;
bool read_stdin;
bool print_output;
bool print_input;
@@ -596,6 +598,7 @@ typedef struct
bool no_entry;
bool kernel_build;
bool silence_deprecation;
bool print_stats;
int build_threads;
TrustLevel trust_level;
OptimizationSetting optsetting;

View File

@@ -83,6 +83,8 @@ static void usage(void)
PRINTF(" --max-mem <value> - Sets the preferred max memory size.");
PRINTF(" --run-once - After running the output file, delete it immediately.");
PRINTF(" -V --version - Print version information.");
PRINTF(" -q --quiet - Silence unnecessary output.");
PRINTF(" -v -vv -vvv - Verbose output, -v for default, -vv and -vvv gives more information.");
PRINTF(" -E - Lex only.");
PRINTF(" -P - Only parse and output the AST as JSON.");
PRINTF(" -C - Only lex, parse and check.");
@@ -144,11 +146,7 @@ static void usage(void)
PRINTF(" --fp-math=<option> - FP math behaviour: strict, relaxed, fast.");
PRINTF(" --win64-simd=<option> - Win64 SIMD ABI: array, full.");
PRINTF("");
PRINTF(" --debug-stats - Print debug statistics.");
PRINTF(" --print-linking - Print linker arguments.");
#ifndef NDEBUG
PRINTF(" --debug-log - Print debug logging to stdout.");
#endif
PRINTF("");
PRINTF(" --benchmarking - Run built-in benchmarks.");
PRINTF(" --testing - Run built-in tests.");
@@ -415,6 +413,30 @@ static void parse_option(BuildOptions *options)
exit_compiler(COMPILER_SUCCESS_EXIT);
}
break;
case 'q':
if (match_shortopt("q"))
{
options->verbosity_level = -1;
return;
}
break;
case 'v':
if (match_shortopt("vvv"))
{
options->verbosity_level = 3;
return;
}
if (match_shortopt("vv"))
{
options->verbosity_level = 2;
return;
}
if (match_shortopt("v"))
{
options->verbosity_level = 1;
return;
}
break;
case 'V':
if (match_shortopt("V"))
{
@@ -605,6 +627,11 @@ static void parse_option(BuildOptions *options)
options->symtab_size = next_highest_power_of_2(symtab);
return;
}
if (match_longopt("quiet"))
{
options->verbosity_level = -1;
return;
}
if (match_longopt("version"))
{
print_version();
@@ -613,6 +640,7 @@ static void parse_option(BuildOptions *options)
if (match_longopt("run-once"))
{
options->run_once = true;
if (!options->verbosity_level) options->verbosity_level = -1;
return;
}
if ((argopt = match_argopt("fp-math")))
@@ -732,17 +760,6 @@ static void parse_option(BuildOptions *options)
options->no_headers = true;
return;
}
if (match_longopt("debug-log"))
{
debug_log = true;
debug_stats = true;
return;
}
if (match_longopt("debug-stats"))
{
debug_stats = true;
return;
}
if (match_longopt("print-linking"))
{
options->print_linking = true;
@@ -1167,6 +1184,7 @@ BuildOptions parse_arguments(int argc, const char *argv[])
{
FAIL_WITH_ERR("Missing a compiler command such as 'compile' or 'build'.");
}
debug_log = build_options.verbosity_level > 2;
return build_options;
}

View File

@@ -364,8 +364,8 @@ static void update_build_target_from_options(BuildTarget *target, BuildOptions *
if (options->arch_os_target_override != ARCH_OS_TARGET_DEFAULT) target->arch_os_target = options->arch_os_target_override;
if (options->reloc_model != RELOC_DEFAULT) target->reloc_model = options->reloc_model;
if (options->symtab_size) target->symtab_size = options->symtab_size;
if (options->silence_deprecation) target->silence_deprecation = options->silence_deprecation;
target->print_linking = options->print_linking;
if (options->silence_deprecation) target->silence_deprecation = options->silence_deprecation || options->verbosity_level < 0;
target->print_linking = options->print_linking || options->verbosity_level > 1;
for (size_t i = 0; i < options->linker_arg_count; i++)
{
@@ -395,12 +395,14 @@ static void update_build_target_from_options(BuildTarget *target, BuildOptions *
target->emit_llvm = options->emit_llvm;
target->build_threads = options->build_threads;
target->emit_asm = options->emit_asm;
target->print_stats = options->verbosity_level >= 2;
if (options->output_dir) target->output_dir = options->output_dir;
if (options->panicfn) target->panicfn = options->panicfn;
if (options->testfn) target->testfn = options->testfn;
if (options->benchfn) target->benchfn = options->benchfn;
target->benchmarking = options->benchmarking;
target->testing = options->testing;
target->silent = options->verbosity_level < 0;
target->vector_conv = options->vector_conv;
if (options->macos.sysroot) target->macos.sysroot = options->macos.sysroot;
if (options->win.sdk) target->win.sdk = options->win.sdk;

View File

@@ -17,6 +17,8 @@
#define MAX_OUTPUT_FILES 1000000
#define MAX_MODULES 100000
CompilerState compiler;
Vmem ast_arena;
@@ -94,14 +96,14 @@ static void compiler_lex(void)
if (loaded) continue;
Lexer lexer = { .file = file };
lexer_init(&lexer);
printf("# %s\n", file->full_path);
OUTF("# %s\n", file->full_path);
while (lexer_next_token(&lexer))
{
TokenType token_type = lexer.token_type;
printf("%s ", token_type_to_string(token_type));
OUTF("%s ", token_type_to_string(token_type));
if (token_type == TOKEN_EOF) break;
}
printf("\n");
OUTN("");
}
exit_compiler(COMPILER_SUCCESS_EXIT);
}
@@ -239,7 +241,7 @@ static const char *static_lib_name(void)
static void free_arenas(void)
{
if (debug_stats)
if (compiler.build.print_stats)
{
printf("-- AST/EXPR/TYPE INFO -- \n");
printf(" * Ast size: %u bytes\n", (unsigned)sizeof(Ast));
@@ -266,7 +268,7 @@ static void free_arenas(void)
expr_arena_free();
type_info_arena_free();
if (debug_stats) print_arena_status();
if (compiler.build.print_stats) print_arena_status();
}
static int compile_cfiles(const char *cc, const char **files, const char *flags, const char **include_dirs,
@@ -283,7 +285,7 @@ static int compile_cfiles(const char *cc, const char **files, const char *flags,
static void compiler_print_bench(void)
{
if (debug_stats)
if (compiler.build.print_stats)
{
puts("--------- Compilation time statistics --------\n");
double last = compiler_init_time;
@@ -700,11 +702,11 @@ void compiler_compile(void)
scratch_buffer_append(name);
}
name = scratch_buffer_to_string();
printf("Launching %s", name);
OUTF("Launching %s", name);
for (uint32_t i = 0; i < vec_size(compiler.build.args); ++i) {
printf(" %s", compiler.build.args[i]);
OUTF(" %s", compiler.build.args[i]);
}
printf("\n");
OUTN("");
int ret = run_subprocess(name, compiler.build.args);
if (compiler.build.delete_after_run)
@@ -712,7 +714,7 @@ void compiler_compile(void)
file_delete_file(name);
}
if (ret < 0) exit_compiler(EXIT_FAILURE);
printf("Program completed with exit code %d.\n", ret);
OUTF("Program completed with exit code %d.\n", ret);
if (ret != 0) exit_compiler(ret);
}
}
@@ -734,7 +736,7 @@ void compiler_compile(void)
delete_object_files(obj_files, output_file_count);
compiler_link_time = bench_mark();
compiler_print_bench();
printf("Static library '%s' created.\n", output_static);
OUTF("Static library '%s' created.\n", output_static);
}
else if (output_dynamic)
{
@@ -752,7 +754,7 @@ void compiler_compile(void)
error_exit("Failed to produce dynamic library '%s'.", output_dynamic);
}
delete_object_files(obj_files, output_file_count);
printf("Dynamic library '%s' created.\n", output_dynamic);
OUTF("Dynamic library '%s' created.\n", output_dynamic);
compiler_link_time = bench_mark();
compiler_print_bench();
}
@@ -808,9 +810,9 @@ void compile_file_list(BuildOptions *options)
{
error_exit("The target is a 'prepare' target, and only 'build' can be used with it.");
}
printf("] Running prepare target '%s'.\n", options->target_select);
OUTF("] Running prepare target '%s'.\n", options->target_select);
execute_scripts();
printf("] Completed.\n.");
OUTF("] Completed.\n.");
return;
}
if (options->command == COMMAND_CLEAN_RUN)

View File

@@ -70,6 +70,8 @@ typedef uint16_t FileId;
#define EXPAND_EXPR_STRING(str_) (str_)->const_expr.bytes.len, (str_)->const_expr.bytes.ptr
#define TABLE_MAX_LOAD 0.5
#define OUTF(...) do { if (!compiler.build.silent) printf(__VA_ARGS__); } while(0)
#define OUTN(str__) do { if (!compiler.build.silent) puts(str__); } while(0)
#define INVALID_PTR ((void*)(uintptr_t)0xAAAAAAAAAAAAAAAA)

View File

@@ -859,10 +859,10 @@ void platform_linker(const char *output_file, const char **files, unsigned file_
if (compiler.build.print_linking) puts(scratch_buffer_to_string());
if (system(scratch_buffer_to_string()) != 0)
{
puts("Failed to create .dSYM files, debugging will be impacted.");
OUTN("Failed to create .dSYM files, debugging will be impacted.");
}
}
printf("Program linked to executable '%s'.\n", output_file);
OUTF("Program linked to executable '%s'.\n", output_file);
}
const char *cc_compiler(const char *cc, const char *file, const char *flags, const char **include_dirs, const char *output_subdir)

View File

@@ -6,7 +6,6 @@
bool debug_log = false;
bool debug_stats = false;
jmp_buf on_error_jump;

View File

@@ -49,7 +49,7 @@ NORETURN void exit_compiler(int exit_value);
extern jmp_buf on_err_jump;
extern bool debug_log;
extern bool debug_stats;
extern uintptr_t arena_zero;
struct ScratchBuf { char str[MAX_STRING_BUFFER]; uint32_t len; };
extern struct ScratchBuf scratch_buffer;