From 8efd9b7c7a023e15f6fb25003795bffb8933919b Mon Sep 17 00:00:00 2001 From: flysand7 Date: Tue, 1 Aug 2023 12:20:43 +1100 Subject: [PATCH] Add data model macro setting --- .gitignore | 1 + build.py | 17 +++++++++++++++-- src/include/cia-conf.h | 6 ++++++ {include => src/include}/cia-def.h | 9 ++------- {include => src/include}/cia-mem.h | 0 {include => src/include}/ctype.h | 0 {include => src/include}/stdbool.h | 0 {include => src/include}/stdint.h | 16 ++++++++-------- {include => src/include}/stdio.h | 0 {include => src/include}/stdlib.h | 0 {include => src/include}/string.h | 0 {include => src/include}/tinyrt.h | 0 src/linux/conf.h | 3 +++ src/windows/conf.h | 3 +++ 14 files changed, 38 insertions(+), 17 deletions(-) create mode 100644 src/include/cia-conf.h rename {include => src/include}/cia-def.h (57%) rename {include => src/include}/cia-mem.h (100%) rename {include => src/include}/ctype.h (100%) rename {include => src/include}/stdbool.h (100%) rename {include => src/include}/stdint.h (91%) rename {include => src/include}/stdio.h (100%) rename {include => src/include}/stdlib.h (100%) rename {include => src/include}/string.h (100%) rename {include => src/include}/tinyrt.h (100%) create mode 100644 src/linux/conf.h create mode 100644 src/windows/conf.h diff --git a/.gitignore b/.gitignore index 0c97ba5..5762dc8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ /lib /a /src/ciabatta.c +/src/include/conf.h a.out *.a *.so diff --git a/build.py b/build.py index 7d9e23f..ff820a4 100755 --- a/build.py +++ b/build.py @@ -62,7 +62,7 @@ dependencies.append(args.compiler) # Figure out the flags -includes = ['include'] +includes = ['src/include'] cc = args.compiler cc_defines = [] cc_flags = ['-nostdlib'] @@ -77,7 +77,6 @@ else: # 'debug' cc_defines.append('_DEBUG') if target != 'windows': cc_flags.append('-fpic') -cc_defines.append(f'_CIA_OS_{target.upper()}') # Check dependencies print(colors.grey, '==> Checking dependencies...', colors.reset, end='', sep='') @@ -188,6 +187,8 @@ except Exception as error: print(colors.red, f" ERROR writing file '{ciabatta_header_path}':", sep='') print(f" {error}", colors.reset) sys.exit(1) +print(colors.grey, f"==> Generating cia-conf.h", colors.reset, sep='') + def quote(s): return '"' + s + '"' @@ -202,6 +203,18 @@ cc_flags_str = ' '.join( list(map(prefix_quote('-I '), includes))) print(colors.grey, f"==> Compiling {lib_file}", colors.reset, sep='') +try: + with open(os.path.join('src', 'include', 'cia-conf.h'), 'w') as conf_file: + os_config = open(os.path.join('src', target, 'conf.h')) + conf_file.write('\n') + conf_file.write('// This file is AUTO-GENERATED\n') + conf_file.write('// See os folder (e.g. src/linux) for conf.h file\n') + conf_file.write(os_config.read()) + os_config.close() +except Exception as error: + print(colors.red, f" ERROR writing file '{ciabatta_header_path}':", sep='') + print(f" {error}", colors.reset) + sys.exit(1) def assemble(src, out): format = 'elf64' diff --git a/src/include/cia-conf.h b/src/include/cia-conf.h new file mode 100644 index 0000000..1f6f898 --- /dev/null +++ b/src/include/cia-conf.h @@ -0,0 +1,6 @@ + +// This file is AUTO-GENERATED +// See os folder (e.g. src/linux) for conf.h file + +#define _CIA_OS_LINUX +#define _CIA_DATA_LP64 diff --git a/include/cia-def.h b/src/include/cia-def.h similarity index 57% rename from include/cia-def.h rename to src/include/cia-def.h index 750005b..03ea4f3 100644 --- a/include/cia-def.h +++ b/src/include/cia-def.h @@ -1,13 +1,8 @@ #pragma once -// Since we're re-defining noreturn below, this would mess -// with __declspec(noreturn) in windows headers, which -// is hidden behind a define. Good thing, because now we -// can override that define over here. -#if defined(_CIA_OS_WINDOWS) - #define DECLSPEC_NORETURN __declspec("noreturn") -#endif +// Include platform stuff +#include // Pre-C23 keyword macros and stddef #define static_assert _Static_assert diff --git a/include/cia-mem.h b/src/include/cia-mem.h similarity index 100% rename from include/cia-mem.h rename to src/include/cia-mem.h diff --git a/include/ctype.h b/src/include/ctype.h similarity index 100% rename from include/ctype.h rename to src/include/ctype.h diff --git a/include/stdbool.h b/src/include/stdbool.h similarity index 100% rename from include/stdbool.h rename to src/include/stdbool.h diff --git a/include/stdint.h b/src/include/stdint.h similarity index 91% rename from include/stdint.h rename to src/include/stdint.h index 829b224..5ee6a51 100644 --- a/include/stdint.h +++ b/src/include/stdint.h @@ -5,9 +5,9 @@ static_assert(sizeof(char) == 1, "Char isn't 1 bytes long"); static_assert(sizeof(short) == 2, "Short isn't 2 bytes long"); static_assert(sizeof(int) == 4, "Int isn't 4 bytes long"); static_assert(sizeof(long long int) == 8, "Long long isn't 8 bytes long"); -#if defined(_CIA_OS_LINUX) +#if defined(_CIA_DATA_LP64) static_assert(sizeof(long) == 8, "Long on linux isn't 8 bytes"); -#elif defined(_CIA_OS_WINDOWS) +#elif defined(_CIA_DATA_LLP64) static_assert(sizeof(long) == 4, "Long on windows isn't 4 bytes"); #endif @@ -17,14 +17,14 @@ typedef signed short int16_t; typedef unsigned short uint16_t; typedef signed int int32_t; typedef unsigned int uint32_t; -#if defined(_CIA_OS_LINUX) +#if defined(_CIA_DATA_LP64) typedef signed long int64_t; typedef unsigned long uint64_t; -#elif defined(_CIA_OS_WINDOWS) +#elif defined(_CIA_DATA_LLP64) typedef signed long long int64_t; typedef unsigned long long uint64_t; #else - #error "Platform not implemented" + #error "Data model not implemented" #endif typedef int8_t int_fast8_t, int_least8_t; @@ -50,18 +50,18 @@ typedef uint64_t uintptr_t; #define UINT16_C(n) (n) #define UINT32_C(n) (n) -#if defined(_CIA_OS_LINUX) +#if defined(_CIA_DATA_LP64) #define INT64_C(n) (n ## L) #define INTMAX_C(n) (n ## L) #define UINT64_C(n) (n ## UL) #define UINTMAX_C(n) (n ## UL) -#elif defined(_CIA_OS_WINDOWS) +#elif defined(_CIA_DATA_LLP64) #define INT64_C(n) (n ## LL) #define INTMAX_C(n) (n ## LL) #define UINT64_C(n) (n ## ULL) #define UINTMAX_C(n) (n ## ULL) #else - #error "Platform functionality not defined" + #error "Data model not defined" #endif #define INT8_WIDTH 8 diff --git a/include/stdio.h b/src/include/stdio.h similarity index 100% rename from include/stdio.h rename to src/include/stdio.h diff --git a/include/stdlib.h b/src/include/stdlib.h similarity index 100% rename from include/stdlib.h rename to src/include/stdlib.h diff --git a/include/string.h b/src/include/string.h similarity index 100% rename from include/string.h rename to src/include/string.h diff --git a/include/tinyrt.h b/src/include/tinyrt.h similarity index 100% rename from include/tinyrt.h rename to src/include/tinyrt.h diff --git a/src/linux/conf.h b/src/linux/conf.h new file mode 100644 index 0000000..d64652c --- /dev/null +++ b/src/linux/conf.h @@ -0,0 +1,3 @@ + +#define _CIA_OS_LINUX +#define _CIA_DATA_LP64 diff --git a/src/windows/conf.h b/src/windows/conf.h new file mode 100644 index 0000000..175909c --- /dev/null +++ b/src/windows/conf.h @@ -0,0 +1,3 @@ + +#define _CIA_OS_WINDOWS +#define _CIA_DATA_LLP64