Skip to content

Commit

Permalink
Add env::COMPILER_BUILD_HASH and env::COMPILER_BUILD_DATE
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Sep 20, 2024
1 parent c25645e commit 8e37e54
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 51 deletions.
2 changes: 2 additions & 0 deletions lib/std/core/env.c3
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ enum ArchType
XTENSA, // Xtensa
}

const String COMPILER_BUILD_HASH = $$BUILD_HASH;
const String COMPILER_BUILD_DATE = $$BUILD_DATE;
const OsType OS_TYPE = (OsType)$$OS_TYPE;
const ArchType ARCH_TYPE = (ArchType)$$ARCH_TYPE;
const bool ARCH_32_BIT = $$REGISTER_SIZE == 32;
Expand Down
1 change: 1 addition & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Support casting bitstructs to bool.
- Allow user-defined attributes to have typed parameters.
- Add `.gitkeep` files to project subfolders.
- Add `env::COMPILER_BUILD_HASH` and `env::COMPILER_BUILD_DATE`

### Fixes
- Issue where a lambda wasn't correctly registered as external. #1408
Expand Down
89 changes: 73 additions & 16 deletions src/compiler/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
#endif
#if LLVM_AVAILABLE
#include "c3_llvm.h"
#endif
#endif
#include "git_hash.h"
#include <errno.h>

#define MAX_OUTPUT_FILES 1000000
Expand Down Expand Up @@ -832,10 +833,18 @@ void compile_file_list(BuildOptions *options)
compile();
}

static void setup_int_define(const char *id, uint64_t i, Type *type)
static inline void setup_define(const char *id, Expr *expr)
{
TokenType token_type = TOKEN_CONST_IDENT;
id = symtab_add(id, (uint32_t) strlen(id), fnv1a(id, (uint32_t) strlen(id)), &token_type);
void *previous = htable_set(&compiler.context.compiler_defines, (void*)id, expr);
if (previous)
{
error_exit("Redefined ident %s", id);
}
}
static void setup_int_define(const char *id, uint64_t i, Type *type)
{
Type *flat = type_flatten(type);
assert(type_is_integer(flat));
Expr *expr = expr_new_const_int(INVALID_SPAN, flat, i);
Expand All @@ -844,23 +853,17 @@ static void setup_int_define(const char *id, uint64_t i, Type *type)
{
error_exit("Integer define %s overflow.", id);
}
void *previous = htable_set(&compiler.context.compiler_defines, (void*)id, expr);
if (previous)
{
error_exit("Redefined ident %s", id);
}
setup_define(id, expr);
}

static void setup_string_define(const char *id, const char *value)
{
setup_define(id, expr_new_const_string(INVALID_SPAN, value));
}

static void setup_bool_define(const char *id, bool value)
{
TokenType token_type = TOKEN_CONST_IDENT;
id = symtab_add(id, (uint32_t) strlen(id), fnv1a(id, (uint32_t) strlen(id)), &token_type);
Expr *expr = expr_new_const_bool(INVALID_SPAN, type_bool, value);
void *previous = htable_set(&compiler.context.compiler_defines, (void *)id, expr);
if (previous)
{
error_exit("Redefined ident %s", id);
}
setup_define(id, expr_new_const_bool(INVALID_SPAN, type_bool, value));
}
#if FETCH_AVAILABLE

Expand Down Expand Up @@ -1228,6 +1231,59 @@ static void check_sanitizer_options(BuildTarget *target)
}
}

const char *compiler_date_to_iso(void)
{
const char *comp_date = __DATE__;
static char iso[11] = "2000-01-01";
iso[2] = comp_date[9];
iso[3] = comp_date[10];
int month;
switch (comp_date[0])
{
case 'O':
month = 10;
break;
case 'D':
month = 12;
break;
case 'N':
month = 11;
break;
case 'J':
if (comp_date[1] == 'a')
{
month = 1;
break;
}
if (comp_date[2] == 'n')
{
month = 6;
break;
}
month = 7;
break;
case 'F':
month = 2;
break;
case 'A':
month = comp_date[2] == 'p' ? 4 : 8;
break;
case 'M':
month = comp_date[2] == 'r' ? 3 : 5;
break;
case 'S':
month = 9;
break;
default:
UNREACHABLE
}
iso[5] = month / 10 + '0';
iso[6] = month % 10 + '0';
iso[8] = comp_date[4] == ' ' ? '0' : comp_date[4];
iso[9] = comp_date[5];
return iso;
}

void compile()
{
symtab_init(compiler.build.symtab_size);
Expand Down Expand Up @@ -1300,9 +1356,10 @@ void compile()
setup_bool_define("ADDRESS_SANITIZER", compiler.build.feature.sanitize_address);
setup_bool_define("MEMORY_SANITIZER", compiler.build.feature.sanitize_memory);
setup_bool_define("THREAD_SANITIZER", compiler.build.feature.sanitize_thread);
setup_string_define("BUILD_HASH", GIT_HASH);
setup_string_define("BUILD_DATE", compiler_date_to_iso());

type_init_cint();

compiler_init_time = bench_mark();

if (!vec_size(compiler.build.sources) && !compiler.build.read_stdin) error_exit("No files to compile.");
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/compiler_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2168,6 +2168,7 @@ Expr *expr_new(ExprKind kind, SourceSpan start);
Expr *expr_new_const_int(SourceSpan span, Type *type, uint64_t v);
Expr *expr_new_const_bool(SourceSpan span, Type *type, bool value);
Expr *expr_new_const_typeid(SourceSpan span, Type *type);
Expr *expr_new_const_string(SourceSpan span, const char *string);
bool expr_is_simple(Expr *expr, bool to_float);
bool expr_is_pure(Expr *expr);
bool expr_is_runtime_const(Expr *expr);
Expand Down Expand Up @@ -2207,10 +2208,10 @@ INLINE void expr_rewrite_const_int(Expr *expr, Type *type, uint64_t v);
INLINE void expr_rewrite_const_typeid(Expr *expr, Type *type);
INLINE void expr_rewrite_const_initializer(Expr *expr, Type *type, ConstInitializer *initializer);
INLINE void expr_rewrite_const_untyped_list(Expr *expr, Expr **elements);
void expr_rewrite_const_string(Expr *expr_to_rewrite, const char *string);
void expr_rewrite_const_ref(Expr *expr_to_rewrite, Decl *decl);

void expr_rewrite_to_builtin_access(Expr *expr, Expr *parent, BuiltinAccessKind kind, Type *type);
void expr_rewrite_to_string(Expr *expr_to_rewrite, const char *string);
void expr_rewrite_to_const_ref(Expr *expr_to_rewrite, Decl *decl);
void expr_rewrite_to_const_zero(Expr *expr, Type *type);
bool expr_rewrite_to_const_initializer_index(Type *list_type, ConstInitializer *list, Expr *result, unsigned index, bool from_back);

Expand Down
17 changes: 15 additions & 2 deletions src/compiler/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,19 @@ Expr *expr_new_const_bool(SourceSpan span, Type *type, bool value)
return expr;
}

Expr *expr_new_const_string(SourceSpan span, const char *string)
{
Expr *expr = expr_calloc();
expr->expr_kind = EXPR_CONST;
expr->span = span;
expr->type = type_string;
expr->const_expr.const_kind = CONST_STRING;
expr->const_expr.bytes.ptr = string;
expr->const_expr.bytes.len = strlen(string);
expr->resolve_status = RESOLVE_DONE;
return expr;
}

void expr_rewrite_to_builtin_access(Expr *expr, Expr *parent, BuiltinAccessKind kind, Type *type)
{
expr->expr_kind = EXPR_BUILTIN_ACCESS;
Expand Down Expand Up @@ -915,7 +928,7 @@ void expr_rewrite_insert_deref(Expr *original)
}
}

void expr_rewrite_to_const_ref(Expr *expr_to_rewrite, Decl *decl)
void expr_rewrite_const_ref(Expr *expr_to_rewrite, Decl *decl)
{
expr_to_rewrite->const_expr = (ExprConst) {
.global_ref = decl,
Expand All @@ -924,7 +937,7 @@ void expr_rewrite_to_const_ref(Expr *expr_to_rewrite, Decl *decl)
expr_to_rewrite->expr_kind = EXPR_CONST;
}

void expr_rewrite_to_string(Expr *expr_to_rewrite, const char *string)
void expr_rewrite_const_string(Expr *expr_to_rewrite, const char *string)
{
expr_to_rewrite->expr_kind = EXPR_CONST;
expr_to_rewrite->const_expr.const_kind = CONST_STRING;
Expand Down
Loading

0 comments on commit 8e37e54

Please sign in to comment.