]> git.leonardobizzoni.com Git - CBuild/commitdiff
C++ fixes
authorLeonardoBizzoni <leo2002714@gmail.com>
Mon, 8 Dec 2025 08:20:10 +0000 (09:20 +0100)
committerLeonardoBizzoni <leo2002714@gmail.com>
Mon, 8 Dec 2025 08:20:10 +0000 (09:20 +0100)
cbuild.h

index c93ad1fb82b8190291aed0cd2a79176060f2d859..236d62103ce52c837f8a8ffe4174a6322f1817b0 100644 (file)
--- a/cbuild.h
+++ b/cbuild.h
@@ -149,9 +149,14 @@ internal cb_assertion_handler_fn cb_assertion_handler = cb_assertion_break;
   (!(Head) ? (Head) = (Last) = (Nodeptr)                                       \
            : ((Last) ? ((Last)->Next = (Nodeptr), (Last) = (Nodeptr))          \
                      : ((Head)->Next = (Last) = (Nodeptr))))
+#if __cplusplus
+#  define TYPE_CAST(ThingOfType, Thing) reinterpret_cast<decltype(ThingOfType)>(Thing)
+#else
+#  define TYPE_CAST(ThingOfType, Thing) (Thing)
+#endif
 
 struct CB_PathList {
-  char **values;
+  const char **values;
   size_t count;
   size_t capacity;
 };
@@ -214,9 +219,9 @@ enum {
 #define cb_dyn_append(Dynarr, Array, Size) cb_dyn_append_custom((Dynarr), (Array), (Size), values, count, capacity)
 #define cb_cmd_push(Dynarr, Value) cb_dyn_push(Dynarr, Value)
 #define cb_cmd_append_dyn(Dynarr, Values, Count) cb_dyn_append((Dynarr), (Values), (Count));
-#define cb_cmd_append(Dynarr, ...) cb_dyn_append((Dynarr),                                     \
-                                                 ((char*[]){__VA_ARGS__}),                     \
-                                                 (sizeof((char*[]){__VA_ARGS__}) / sizeof(char*)))
+#define cb_cmd_append(Dynarr, ...) cb_dyn_append((Dynarr),                                               \
+                                                 ((const char*[]){__VA_ARGS__}),                         \
+                                                 (sizeof((const char*[]){__VA_ARGS__}) / sizeof(char*)))
 #define cb_println(Level, Fmt, ...) cb_print((Level), Fmt "\n", ##__VA_ARGS__)
 #define cb_rebuild_self(argc, argv) _cb_rebuild(argc, argv, __FILE__, 0)
 #define cb_rebuild_self_with(argc, argv, ...) _cb_rebuild(argc, argv, __FILE__, __VA_ARGS__, (char*)0)
@@ -233,22 +238,23 @@ enum {
     free((Dynarr)->Values);                       \
     (Dynarr)->Count = 0;                          \
   } while (0)
-#define cb_dyn_reserve_custom(Dynarr, HowMany, Values, Count, Capacity)     \
-  do {                                                                      \
-    if (!(Dynarr)->Capacity) {                                              \
-      (Dynarr)->Capacity = CB_DYN_DEFAULT_CAPACITY;                         \
-    }                                                                       \
-    while ((HowMany) > (Dynarr)->Capacity) {                                \
-      (Dynarr)->Capacity *= 2;                                              \
-    }                                                                       \
-    (Dynarr)->Values = realloc((Dynarr)->Values, (Dynarr)->Capacity *       \
-                                             sizeof((Dynarr)->Values[0]));  \
-    cb_assert((Dynarr)->Values);                                            \
+#define cb_dyn_reserve_custom(Dynarr, HowMany, Values, Count, Capacity)                      \
+  do {                                                                                       \
+    if (!(Dynarr)->Capacity) {                                                               \
+      (Dynarr)->Capacity = CB_DYN_DEFAULT_CAPACITY;                                          \
+    }                                                                                        \
+    while ((HowMany) > (Dynarr)->Capacity) {                                                 \
+      (Dynarr)->Capacity *= 2;                                                               \
+    }                                                                                        \
+    (Dynarr)->Values = TYPE_CAST((Dynarr)->Values,                                           \
+                                 realloc((Dynarr)->Values,                                   \
+                                         (Dynarr)->Capacity * sizeof((Dynarr)->Values[0]))); \
+    cb_assert((Dynarr)->Values);                                                             \
   } while(0)
 #define cb_dyn_push_custom(Dynarr, Node, Values, Count, Capacity) \
   do {                                                            \
     cb_dyn_reserve_custom((Dynarr), (Dynarr)->Count + 1,          \
-                              Values, Count, Capacity);           \
+                          Values, Count, Capacity);               \
     (Dynarr)->Values[(Dynarr)->Count++] = (Node);                 \
   } while(0)
 #define cb_dyn_append_custom(Dynarr, Array, Size, Values, Count, Capacity) \
@@ -285,8 +291,8 @@ static bool cb_file_exists(char *path);
 
 internal void _cb_handle_write(CB_Handle fd, char *buffer, size_t buffsize);
 internal char* _cb_format(const char *format, va_list args);
-internal bool _cb_need_rebuild(char *output_path, struct CB_PathList sources);
-internal void _cb_rebuild(int argc, char **argv, char *cb_src, ...);
+internal bool _cb_need_rebuild(const char *output_path, struct CB_PathList sources);
+internal void _cb_rebuild(int argc, char **argv, const char *cb_src, ...);
 internal bool _cb_is_outdated(char *output, ...);
 internal CB_Process _cb_cmd_run(CB_Cmd *cmd, struct Cb_Cmd_RunArgs args);
 internal size_t _last_occurance_of(char *string, char ch);
@@ -469,7 +475,7 @@ static char* cb_handle_read(CB_Handle fd) {
 #else
   struct stat file_stat;
   if (!fstat(fd, &file_stat)) {
-    char *res = malloc(file_stat.st_size);
+    char *res = (char *)malloc(file_stat.st_size);
     if(pread(fd, res, file_stat.st_size, 0) >= 0) {
       return res;
     }
@@ -483,7 +489,7 @@ static bool cb_dir_create(char *path) {
   if (mkdir_res < 0 && errno == ENOENT) {
     size_t parent_end = _last_occurance_of(path, '/');
     if (!parent_end) { return false; }
-    char *parent = malloc(parent_end + 1);
+    char *parent = (char *)malloc(parent_end + 1);
     memcpy(parent, path, parent_end);
     parent[parent_end] = 0;
     cb_dir_create(parent);
@@ -527,6 +533,7 @@ static bool cb_file_exists(char *path) {
   return access(path, F_OK) == 0;
 }
 
+
 internal bool _cb_is_outdated(char *output, ...) {
   struct CB_PathList sources = {};
   va_list args;
@@ -570,7 +577,7 @@ internal char* _cb_format(const char *format, va_list args) {
   uint32_t needed_bytes = vsnprintf(0, 0, format, args2) + 1;
   va_end(args2);
 
-  char *res = malloc(needed_bytes);
+  char *res = (char *)malloc(needed_bytes);
   (void)vsnprintf(res, needed_bytes, format, args);
   return res;
 }
@@ -628,7 +635,7 @@ internal CB_Process _cb_cmd_run(CB_Cmd *cmd, struct Cb_Cmd_RunArgs args) {
     CB_Cmd _cmd = {};
     cb_cmd_append_dyn(&_cmd, cmd->values, cmd->count);
     cb_cmd_push(&_cmd, 0);
-    if (execvp(_cmd.values[0], _cmd.values) < 0) {
+    if (execvp(_cmd.values[0], (char *const *)_cmd.values) < 0) {
       cb_println(CB_LogLevel_Error, "Child process `%s` creation failed with error %d: %s\n",
                  cmd->values[0], errno, strerror(errno));
       exit(-1);
@@ -647,9 +654,9 @@ internal CB_Process _cb_cmd_run(CB_Cmd *cmd, struct Cb_Cmd_RunArgs args) {
   return res;
 }
 
-internal void _cb_rebuild(int argc, char **argv, char *builder_src, ...) {
+internal void _cb_rebuild(int argc, char **argv, const char *builder_src, ...) {
   cb_assert(argc >= 1);
-  char *exe_name = argv[0];
+  const char *exe_name = argv[0];
 
   struct CB_PathList sources = {};
   cb_dyn_push(&sources, builder_src);
@@ -698,7 +705,7 @@ internal void _cb_rebuild(int argc, char **argv, char *builder_src, ...) {
   exit(0);
 }
 
-internal bool _cb_need_rebuild(char *output_path, struct CB_PathList sources) {
+internal bool _cb_need_rebuild(const char *output_path, struct CB_PathList sources) {
 #if OS_WINDOWS
   FILETIME output_mtime_large = {};
   HANDLE output_handle = CreateFileA(output_path, GENERIC_READ,