summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2024-06-06 13:41:08 +0300
committergramanas <anastasis.gramm2@gmail.com>2024-06-06 13:41:08 +0300
commiteb5d40f92c6d2d1d7491c350cbdcef2e8bf96e06 (patch)
treeafe523416d2cd5c57f403dd5a36d28a9c513ed30
parenta6501b5d7ad204c3b998c843c7ed68ef7be323ba (diff)
downloadcgame-eb5d40f92c6d2d1d7491c350cbdcef2e8bf96e06.tar.gz
cgame-eb5d40f92c6d2d1d7491c350cbdcef2e8bf96e06.tar.bz2
cgame-eb5d40f92c6d2d1d7491c350cbdcef2e8bf96e06.zip
build changes
-rw-r--r--b.c148
-rw-r--r--src/b.h17
-rw-r--r--src/render.c62
-rw-r--r--src/vksetup.h6
4 files changed, 155 insertions, 78 deletions
diff --git a/b.c b/b.c
index db24cdd..ec037ed 100644
--- a/b.c
+++ b/b.c
@@ -1,5 +1,8 @@
#define B_IMPLEMENTATION
#include "src/b.h"
+#include <libgen.h>
+
+#define BUILD_DIR "build/"
int debug_level = 1;
@@ -77,11 +80,28 @@ build_c(bool force,
size_t input_paths_len,
const char** dep_paths,
size_t dep_paths_len,
+ const char** objects,
+ size_t objects_len,
const char* output_path)
{
+ size_t size = input_paths_len + objects_len;
+ const char **new_array = (const char **)malloc((size) * sizeof(const char *));
+
+ // Copy existing elements to the new array
+ int i = 0;
+ for (; i < input_paths_len; ++i) {
+ new_array[i] = input_paths[i];
+ }
+ for (; i < size; ++i) {
+ new_array[i] = objects[i - input_paths_len];
+ }
+
int rebuild_is_needed =
- b_needs_rebuild(output_path, input_paths, input_paths_len);
- int dep_rebuild = b_needs_rebuild(output_path, dep_paths, dep_paths_len);
+ b_needs_rebuild(output_path, new_array, size);
+
+ int dep_rebuild = 0;
+ if (rebuild_is_needed == 0)
+ dep_rebuild = b_needs_rebuild(output_path, dep_paths, dep_paths_len);
if (rebuild_is_needed < 0 || dep_rebuild < 0) return false;
@@ -89,7 +109,7 @@ build_c(bool force,
cmd->count = 0;
cc(cmd);
b_cmd_append(cmd, "-o", output_path);
- b_da_append_many(cmd, input_paths, input_paths_len);
+ b_da_append_many(cmd, new_array, size);
libs(cmd);
return b_cmd_run_sync(*cmd);
}
@@ -99,30 +119,81 @@ build_c(bool force,
}
bool
-compile_cxx(bool force,
- B_Cmd* cmd,
- const char** input_paths,
- size_t input_paths_len,
- const char** dep_paths,
- size_t dep_paths_len,
- const char* output_path)
+build_objects(const char* lang,
+ const char* object,
+ const char* deps[],
+ size_t deps_size,
+ const char* out[]) // deps_size + 1
{
- int rebuild_is_needed = b_needs_rebuild(output_path, input_paths, input_paths_len);
- int dep_rebuild = b_needs_rebuild(output_path, dep_paths, dep_paths_len);
-
- if (rebuild_is_needed < 0 || dep_rebuild < 0) return false;
+ char *tmpc, *tmpc2;
+ char tmp[deps_size + 1][1000];
+ char* tmp_obj = strdup(object);
+ char path[1000] = "";
- if (force || rebuild_is_needed || dep_rebuild) {
- cmd->count = 0;
- cxx(cmd);
- b_cmd_append(cmd, "-c");
- //b_cmd_append(cmd, "-o", output_path);
- b_da_append_many(cmd, input_paths, input_paths_len);
- //libs(cmd);
- return b_cmd_run_sync(*cmd);
+ strcpy(path, BUILD_DIR);
+ //strcat(path, "/");
+ strcat(path, basename(tmp_obj));
+ path[ strlen(path) - 4 ] = '\0';
+ strcat(path, "/");
+
+ b_mkdir_if_not_exists(path);
+
+ int rebuild = 0;
+
+ for (size_t i = 0; i < deps_size + 1; i++) {
+ strcpy(tmp[i], path);
+ if (i == deps_size) {
+ tmpc = basename(tmp_obj);
+ strcat(tmp[i], tmpc);
+ free(tmp_obj);
+ } else {
+ tmpc2 = strdup(deps[i]);
+ tmpc = basename(tmpc2);
+ strcat(tmp[i], tmpc);
+ free(tmpc2);
+ }
+
+ size_t s = strlen(tmp[i]);
+ tmp[i][s - 4] = '.';
+ tmp[i][s - 3] = 'o';
+ tmp[i][s - 2] = '\0';
+
+ out[i] = strdup(tmp[i]);
+
+ int c = b_needs_rebuild1(out[i], i == deps_size ? object : deps[i]);
+ if (c < 0) return false;
+ rebuild += c;
+
+ if (c != 0) {
+ B_Cmd cmd = { 0 };
+
+ if (!strcmp(lang, "C++"))
+ cxx(&cmd);
+ else
+ cc(&cmd);
+ b_cmd_append(&cmd, "-c");
+ b_cmd_append(&cmd, i == deps_size ? object : deps[i]);
+ b_cmd_append(&cmd, "-o");
+ b_cmd_append(&cmd, out[i]);
+ bool rc = b_cmd_run_sync(cmd);
+ if (!rc) return false;
+ }
}
+ if (rebuild == 0) b_log(B_INFO, "%s* is up-to-date", path);
- b_log(B_INFO, "%s is up-to-date", output_path);
+ /* if (rebuild != 0) { */
+ /* if (!strcmp(lang, "C++")) */
+ /* cxx(&cmd); */
+ /* else */
+ /* cc(&cmd); */
+ /* b_cmd_append(&cmd, "-c"); */
+ /* b_cmd_append(&cmd, object); */
+ /* b_da_append_many(&cmd, deps, deps_size); */
+
+ /* return b_cmd_run_sync(cmd); */
+ /* } */
+
+ //b_log(B_INFO, "%s is up-to-date", out[deps_size]);
return true;
}
@@ -145,11 +216,7 @@ main(int argc, char *argv[])
}
}
- const char *cxx_dep_paths[] = {
- "src/vksetup.h",
- };
- const char* cxx_input_paths[] = {
- "src/cplusplus.cpp",
+ const char* cplusplus_deps[] = {
"lib/imgui-1.90.7/imgui.cpp",
"lib/imgui-1.90.7/imgui_demo.cpp",
"lib/imgui-1.90.7/imgui_draw.cpp",
@@ -158,30 +225,27 @@ main(int argc, char *argv[])
"lib/imgui-1.90.7/backends/imgui_impl_sdl2.cpp",
"lib/imgui-1.90.7/backends/imgui_impl_vulkan.cpp",
};
-
- const char *c_dep_paths[] = {
+
+ const char *render_deps[] = {
"src/state.h",
"src/vksetup.h",
+ "src/cplusplus.h",
};
- const char* c_input_paths[] = {
+ const char* render_paths[] = {
"src/render.c",
- "cplusplus.o",
- "imgui.o",
- "imgui_demo.o",
- "imgui_draw.o",
- "imgui_tables.o",
- "imgui_widgets.o",
- "imgui_impl_sdl2.o",
- "imgui_impl_vulkan.o",
};
B_Cmd cmd = {0};
- if (!compile_cxx(force, &cmd, cxx_input_paths, B_ARRAY_LEN(cxx_input_paths), cxx_dep_paths, B_ARRAY_LEN(c_dep_paths),"cplusplus.o")) return 1;
- if (!build_c(force, &cmd, c_input_paths, B_ARRAY_LEN(c_input_paths), c_dep_paths, B_ARRAY_LEN(c_dep_paths),"render")) return 1;
+ const char* objects[B_ARRAY_LEN(cplusplus_deps) + 1];
+
+ b_mkdir_if_not_exists(BUILD_DIR);
+
+ // TODO: make build_object func to build one by one, and add headers
+ if (!build_objects("C++", "src/cplusplus.cpp", cplusplus_deps, B_ARRAY_LEN(cplusplus_deps), objects)) return 1;
+ if (!build_c(force, &cmd, render_paths, B_ARRAY_LEN(render_paths), render_deps, B_ARRAY_LEN(render_deps), objects, B_ARRAY_LEN(objects), BUILD_DIR"render")) return 1;
return 0;
}
-
/*
1. Build list of <object>.cpp (with <dep>s) -> into build/cxx/<object>/<dep>.o
2. Build list of <object>.c (with <dep>s) -> into build/c/<object>/<dep>.o
diff --git a/src/b.h b/src/b.h
index e89c1cb..82bf223 100644
--- a/src/b.h
+++ b/src/b.h
@@ -49,6 +49,8 @@
typedef enum {
B_INFO,
+ B_CMD,
+ B_BUILDING,
B_CHANGE,
B_WARNING,
B_ERROR,
@@ -306,7 +308,7 @@ bool b_mkdir_if_not_exists(const char *path)
int result = mkdir(path, 0755);
if (result < 0) {
if (errno == EEXIST) {
- b_log(B_INFO, "directory `%s` already exists", path);
+ //b_log(B_INFO, "directory `%s` already exists", path);
return true;
}
b_log(B_ERROR, "could not create directory `%s`: %s", path, strerror(errno));
@@ -398,7 +400,7 @@ B_Proc b_cmd_run_async(B_Cmd cmd)
B_String_Builder sb = {0};
b_cmd_render(cmd, &sb);
b_sb_append_null(&sb);
- b_log(B_INFO, "CMD: %s", sb.items);
+ b_log(B_CMD, "%s", sb.items);
b_sb_free(sb);
memset(&sb, 0, sizeof(sb));
@@ -486,6 +488,12 @@ void b_log(B_Log_Level level, const char *fmt, ...)
case B_INFO:
fprintf(stderr, "[INFO] ");
break;
+ case B_CMD:
+ fprintf(stderr, "[CMD] ");
+ break;
+ case B_BUILDING:
+ fprintf(stderr, "[BUILDING] ");
+ break;
case B_CHANGE:
fprintf(stderr, "[CHANGE] ");
break;
@@ -712,7 +720,10 @@ int b_needs_rebuild(const char *output_path, const char **input_paths, size_t in
if (stat(output_path, &statbuf) < 0) {
// NOTE: if output does not exist it 100% must be rebuilt
- if (errno == ENOENT) return 1;
+ if (errno == ENOENT) {
+ b_log(B_BUILDING, "%s", output_path);
+ return 1;
+ }
b_log(B_ERROR, "could not stat %s: %s", output_path, strerror(errno));
return -1;
}
diff --git a/src/render.c b/src/render.c
index 4893d64..2558bd4 100644
--- a/src/render.c
+++ b/src/render.c
@@ -13,9 +13,9 @@
#include <vulkan/vulkan.h>
-#define VMA_STATIC_VULKAN_FUNCTIONS 0
-#define VMA_DYNAMIC_VULKAN_FUNCTIONS 1
-#include "vk_mem_alloc.h"
+/* #define VMA_STATIC_VULKAN_FUNCTIONS 0 */
+/* #define VMA_DYNAMIC_VULKAN_FUNCTIONS 1 */
+/* #include "vk_mem_alloc.h" */
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
@@ -1626,7 +1626,6 @@ draw_frame()
float dt = time - prev_time;
-
vkWaitForFences(s.vk.device, 1, &s.frames[currentFrame].in_flight_fence, VK_TRUE, UINT64_MAX);
uint32_t imageIndex;
@@ -1696,47 +1695,44 @@ main(int argc, char* argv[])
init_state(&s);
if (!init()) {
vk_log(VK_INFO, "Failed to initialize!\n");
+ abort();
}
- else {
- init_vulkan();
- bool quit = false;
+ init_vulkan();
+ bool quit = false;
- /* VMA POC */
- VmaVulkanFunctions vulkanFunctions = {0};
- vulkanFunctions.vkGetInstanceProcAddr = &vkGetInstanceProcAddr;
- vulkanFunctions.vkGetDeviceProcAddr = &vkGetDeviceProcAddr;
- VmaAllocatorCreateInfo allocatorCreateInfo = {0};
- allocatorCreateInfo.flags = VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT;
- allocatorCreateInfo.vulkanApiVersion = VK_API_VERSION_1_2;
- allocatorCreateInfo.physicalDevice = s.vk.physical_device;
- allocatorCreateInfo.device = s.vk.device;
- allocatorCreateInfo.instance = s.vk.instance;
- allocatorCreateInfo.pVulkanFunctions = &vulkanFunctions;
+ /* VMA POC */
+ VmaVulkanFunctions vulkanFunctions = {0};
+ vulkanFunctions.vkGetInstanceProcAddr = &vkGetInstanceProcAddr;
+ vulkanFunctions.vkGetDeviceProcAddr = &vkGetDeviceProcAddr;
- VmaAllocator allocator;
- vmaCreateAllocator(&allocatorCreateInfo, &allocator);
+ VmaAllocatorCreateInfo allocatorCreateInfo = {0};
+ allocatorCreateInfo.flags = VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT;
+ allocatorCreateInfo.vulkanApiVersion = VK_API_VERSION_1_2;
+ allocatorCreateInfo.physicalDevice = s.vk.physical_device;
+ allocatorCreateInfo.device = s.vk.device;
+ allocatorCreateInfo.instance = s.vk.instance;
+ allocatorCreateInfo.pVulkanFunctions = &vulkanFunctions;
- // Entire program...
+ vmaCreateAllocator(&allocatorCreateInfo, &s.vk.allocator);
- // At the end, don't forget to:
- vmaDestroyAllocator(allocator);
+ // At the end, don't forget to:
- // Game loop
- update_camera(0, 0);
- while (!quit) {
- handle_input(&quit);
- imgui_new_frame(s.vk);
- draw_frame();
- //SDL_Delay(16);
- }
+ update_camera(0, 0);
- close_vulkan();
+ // Game loop
+ while (!quit) {
+ handle_input(&quit);
+ imgui_new_frame(s.vk);
+ draw_frame();
+ //SDL_Delay(16);
}
- // Free resources and close SDL
+ vmaDestroyAllocator(s.vk.allocator);
+
+ close_vulkan();
closeSDL();
return 0;
diff --git a/src/vksetup.h b/src/vksetup.h
index 273a785..608975b 100644
--- a/src/vksetup.h
+++ b/src/vksetup.h
@@ -47,6 +47,10 @@
#include <shaderc/shaderc.h>
+#define VMA_STATIC_VULKAN_FUNCTIONS 0
+#define VMA_DYNAMIC_VULKAN_FUNCTIONS 1
+#include "vk_mem_alloc.h"
+
#include "../lib/cglm/include/cglm/cglm.h"
#define VK_ARRAY_LEN(arr) sizeof((arr))/sizeof((arr)[0])
@@ -192,6 +196,8 @@ typedef struct vks_context {
vks_image color_image;
vks_image depth_image;
+ VmaAllocator allocator;
+
VkSampleCountFlagBits msaa_samples;
} vks_context;