From 4b48ab696bb7f0c84505c73e6c37772c7c71b7db Mon Sep 17 00:00:00 2001 From: Musa Mahmood Date: Wed, 17 Dec 2025 11:06:02 -0500 Subject: [PATCH] Replace build.jai, build.cmd with CMakeLists --- CMakeLists.txt | 54 +++++++++++++++ build.cmd | 56 ---------------- build.jai | 150 ------------------------------------------ build_imgui_lib.cmd | 51 --------------- lib/Base/Base.h | 153 ++++++++++++++++++++++++++++++++++++++++++- lib/meta_generated.h | 16 ----- lib_main.cpp | 2 +- 7 files changed, 207 insertions(+), 275 deletions(-) create mode 100644 CMakeLists.txt delete mode 100644 build.cmd delete mode 100644 build.jai delete mode 100644 build_imgui_lib.cmd delete mode 100644 lib/meta_generated.h diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..2f2e906 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,54 @@ +############# README ############# README ############# README ############ +# Option to choose between shared or static library +# Pass this as an argument as follows when configuring the project: +# `cmake -S . -B build +# then build it with either `Release` or `Debug` option: +# `cmake --build build --config Release` +############ /README ############ /README ############ /README ############ + +cmake_minimum_required(VERSION 3.20) + +project(musa-explorer-cpp) + +# Use C++11 +SET (CMAKE_CXX_STANDARD 11) +SET (CMAKE_VERBOSE_MAKEFILE ON) + +if (MSVC) + # Suppress warning: C++ exception handler used, but unwind semantics are not enabled. + add_compile_options(/wd4530) +endif() + +SET (EXE_NAME "mexplore_v2") + +SET (SRC_FILES + lib/third_party/dear-imgui/imgui.cpp + lib/third_party/dear-imgui/imgui_widgets.cpp + lib/third_party/dear-imgui/imgui_draw.cpp + lib/third_party/dear-imgui/imgui_tables.cpp + lib/third_party/dear-imgui/imgui_impl_dx11.cpp + lib/third_party/dear-imgui/imgui_impl_win32.cpp + + exe_main.cpp +) + +SET (INCLUDE_DIRS + ${PROJECT_SOURCE_DIR}/src + ${PROJECT_SOURCE_DIR}/lib + ${PROJECT_SOURCE_DIR}/lib/third_party +) + +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + SET (LINK_LIB_DIRS + ) +elseif(CMAKE_BUILD_TYPE STREQUAL "Release") + SET (LINK_LIB_DIRS + ) +endif() + +SET (SYSTEM_LIBRARIES +) + +add_executable(${EXE_NAME} ${SRC_FILES}) +target_include_directories(${EXE_NAME} PRIVATE ${INCLUDE_DIRS}) +target_link_libraries(${EXE_NAME} PRIVATE ${LINK_LIBS_DIRS} ${SYSTEM_LIBRARIES}) diff --git a/build.cmd b/build.cmd deleted file mode 100644 index cc8d037..0000000 --- a/build.cmd +++ /dev/null @@ -1,56 +0,0 @@ -@echo off -setlocal enabledelayedexpansion - -if "%~1"=="" ( - echo Usage: build.cmd [Debug^|Release] - exit /b 1 -) - -set CONFIG=%~1 - -if /I "%CONFIG%"=="Debug" ( - set CFLAGS=/MDd /Od /Zi /DDEBUG /DEBUG -diagnostics:caret -diagnostics:column -D_CRT_SECURE_NO_WARNINGS -D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING -D_CRT_NONSTDC_NO_DEPRECATE -D_USE_MATH_DEFINES - set LIB_PATH=bin - set IMGUI_LIB_PATH=bin\Debug\dear-imgui.lib -) else if /I "%CONFIG%"=="Release" ( - set CFLAGS=/MD /O2 /DNDEBUG - set LIB_PATH=bin - set IMGUI_LIB_PATH=bin\Release\dear-imgui.lib -) else ( - echo Invalid configuration: %CONFIG% - echo Usage: build.cmd [Debug^|Release] - exit /b 1 -) - -set OBJDIR=obj - -if not exist "%LIB_PATH%" mkdir "%LIB_PATH%" -if not exist obj mkdir obj - -set LIB_NAME=musa - -set SRC_FILES=^ - exe_main.cpp - -set INCLUDE_DIRS=^ - /Isrc ^ - /Ilib ^ - /Ilib\api ^ - /Ithird_party - -set LINK_LIBS=^ - %IMGUI_LIB_PATH% - -rem NOTE: it defaults to C++11, so /std:c++11 is redundant -rem SHARED LIBRARY: - rem echo Building DLL (%CONFIG%) - rem cl /nologo /EHsc /DWIN32 /wd4530 %CFLAGS% %INCLUDE_DIRS% %SRC_FILES% /LD /Fe%LIB_PATH%\%LIB_NAME%.dll /Fo%OBJDIR%\ /link %LINK_LIBS% -echo Building EXE (%CONFIG%)... -cl /nologo /EHsc /DWIN32 /wd4530 %CFLAGS% %INCLUDE_DIRS% %SRC_FILES% /link /MACHINE:AMD64 %LINK_LIBS% /OUT:%LIB_PATH%\%LIB_NAME%.exe - -rem cleanup... -rd /s /q "%OBJDIR%" - -rem echo Running post-build script... -rem nothing to do (for now). -endlocal diff --git a/build.jai b/build.jai deleted file mode 100644 index 0c0b9ad..0000000 --- a/build.jai +++ /dev/null @@ -1,150 +0,0 @@ -// Some notes: -// To generate the intermediate to see how many lines are being compiled, in x64 Native Tools Command Prompt for VS2022 or whatever -// cl /P /EP exe_main.cpp -// tokei exe_main.i -VERSION :: "0.2"; - -#run,stallable build_cpp_project(); - -LIB_BASE_NAME :: "musa-lib"; -EXE_BASE_NAME :: "musa"; - -LIB_SOURCE_FILENAMES :: string.["lib_main.cpp"]; -EXE_SOURCE_FILENAMES :: string.["exe_main.cpp"]; - -INCLUDE_DIRS :: string.[ - "src", - "lib", - "lib/api", - "third_party" -]; - - -build_cpp_project :: () { - start := seconds_since_init(); - - set_build_options_dc(.{do_output=false}); - options := get_build_options(); - args := options.compile_time_command_line; - - compile_debug := true; - if array_find(args, "release") { compile_debug = false; } - build_lib := array_find(args, "build_lib"); - build_exe := array_find(args, "build_exe"); - - generate_meta_file(compile_debug); - if build_lib build_cpp_lib(compile_debug); - if build_exe build_cpp_exe(compile_debug); - - print("\nFull build time: % seconds\n\n", FF(seconds_since_init() - start, 3)); -} - -build_cpp_exe :: (compile_debug: bool) { - extra: [..]string; - if os_target == { - case .WINDOWS; - array_add(*extra, "-D_CRT_SECURE_NO_WARNINGS", "-D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING", "-D_CRT_NONSTDC_NO_DEPRECATE"); - array_add(*extra, "-D_USE_MATH_DEFINES"); // , "-D_WIN32_WINNT=0x0A00", "/utf-8" - - array_add(*extra, "/MT"); // Static - The default - - for INCLUDE_DIRS array_add(*extra, tprint("/I%", it)); - case; assert(false, "Other OSes not supported yet!"); - } - - exe_path := tprint("bin/%", EXE_BASE_NAME); - make_directory_if_it_does_not_exist("bin"); - LINK_LIBS: []string; - if (compile_debug) { LINK_LIBS = .["bin/Debug/dear-imgui.lib"]; } - else { LINK_LIBS = .["bin/Release/dear-imgui.lib"]; } - - success := build_cpp_executable(exe_path, ..EXE_SOURCE_FILENAMES, debug=compile_debug, extra=extra, library_files=LINK_LIBS); - print("\nbuild_cpp_executable, success: %\n", success); -} - -build_cpp_lib :: (compile_debug: bool) -> bool { - lib_path := tprint("bin/Debug/%", LIB_BASE_NAME); - if !compile_debug { - lib_path = tprint("bin/Release/%", LIB_BASE_NAME); - } - lib_directory := path_strip_filename(lib_path); - make_directory_if_it_does_not_exist(lib_directory, recursive = true); - print("Output lib_path: %\n\n", lib_path); - - extra: [..]string; - if os_target == { - case .WINDOWS; - array_add(*extra, "-D_CRT_SECURE_NO_WARNINGS", "-D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING", "-D_CRT_NONSTDC_NO_DEPRECATE"); - array_add(*extra, "-D_USE_MATH_DEFINES"); // , "-D_WIN32_WINNT=0x0A00", "/utf-8" - - // array_add(*extra, "/MD"); // Dynamic - array_add(*extra, "/MT"); // Static - The default - - for INCLUDE_DIRS array_add(*extra, tprint("/I%", it)); - case .LINUX; - array_add(*extra, "-fPIC"); - array_add(*extra, tprint("-I/%/%", #filepath, "JIIM_Library")); - for INCLUDE_DIRS array_add(*extra, tprint("-I/%/%", #filepath, it)); - case; assert(false, "Other OSes not supported yet!"); - } - - success := build_cpp_static_lib(lib_path, ..LIB_SOURCE_FILENAMES, debug=compile_debug, extra=extra); - print("\nbuild_cpp_static_lib, success: %\n", success); - return success; -} - -META_GENERATED_HEADER_FILE_PATH :: "lib/meta_generated.h"; - -cpu_target: CPU_Tag = .X64; -os_target: Operating_System_Tag = .WINDOWS; - -generate_meta_file :: (debug: bool) { - sb: String_Builder; - append(*sb, "#pragma once\n\n"); - print_to_builder(*sb, "const char* MUSA_LIB_VERSION = \"%\";\n", VERSION); - print_to_builder(*sb, "#define BUILD_DEBUG %\n", cast(s32)debug); - - print_to_builder(*sb, "#define OS_WINDOWS %\n", ifx os_target == .WINDOWS then 1 else 0); - print_to_builder(*sb, "#define OS_LINUX %\n", ifx os_target == .LINUX then 1 else 0); - print_to_builder(*sb, "#define OS_MACOS %\n", ifx os_target == .MACOS then 1 else 0); - print_to_builder(*sb, "#define OS_ANDROID %\n", ifx os_target == .ANDROID then 1 else 0); - print_to_builder(*sb, "#define OS_IOS %\n", ifx os_target == .IOS then 1 else 0); - - print_to_builder(*sb, "#define ARCH_CPU_X64 %\n", ifx cpu_target == .X64 then 1 else 0); - print_to_builder(*sb, "#define ARCH_CPU_ARM64 %\n", ifx cpu_target == .ARM64 then 1 else 0); - - os_is_unix := os_target == .MACOS || os_target == .LINUX || os_target == .IOS || os_target == .ANDROID; - print_to_builder(*sb, "#define OS_IS_UNIX %\n", ifx os_is_unix then 1 else 0); - - print_to_builder(*sb, "#define COMPILER_MSVC %\n", ifx os_target == .WINDOWS then 1 else 0); - print_to_builder(*sb, "#define COMPILER_CLANG %\n", ifx os_target != .WINDOWS then 1 else 0); - - print_to_builder(*sb, "#define ARRAY_ENABLE_BOUNDS_CHECKING %\n", cast(s32)debug); - - append(*sb, "#define COMPILER_GCC 0\n"); - - meta_file_data := builder_to_string(*sb); - write_entire_file(META_GENERATED_HEADER_FILE_PATH, meta_file_data); - - print("Generated meta header at % for % on %\n", META_GENERATED_HEADER_FILE_PATH, cpu_target, os_target); -} - -#import "Basic"; -#import "BuildCpp"; -#import "Compiler"; -#import "File"; -#import "File_Utilities"; -#import "String"; -#import "System"; -#import "Process"; - -#if OS == .WINDOWS { - #import "Ico_File"; - #import "Windows_Resources"; - #import "Windows_Utf8"; -} -// Note: Other operating systems are not supported for this application (yet). - -FF :: (val: float64, width:=1) -> FormatFloat #expand { - return formatFloat(val, trailing_width = width, zero_removal=.NO); -} @Utility \ No newline at end of file diff --git a/build_imgui_lib.cmd b/build_imgui_lib.cmd deleted file mode 100644 index 29f0007..0000000 --- a/build_imgui_lib.cmd +++ /dev/null @@ -1,51 +0,0 @@ -@echo off -setlocal enabledelayedexpansion - -if "%~1"=="" ( - echo Usage: build_imgui_lib.cmd [Debug^|Release] - exit /b 1 -) - -set CONFIG=%~1 - -if /I "%CONFIG%"=="Debug" ( rem /Z7 embeds debug info into PDB so we don't need to pass /Fd to cl - set CFLAGS=/MTd /Od /Z7 /DDEBUG /DEBUG -diagnostics:caret -diagnostics:column -D_CRT_SECURE_NO_WARNINGS -D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING -D_CRT_NONSTDC_NO_DEPRECATE -D_USE_MATH_DEFINES - set LIB_PATH=bin/Debug -) else if /I "%CONFIG%"=="Release" ( - set CFLAGS=/MT /O2 /DNDEBUG - set LIB_PATH=bin/Release -) else ( - echo Invalid configuration: %CONFIG% - echo Usage: build_imgui_lib.cmd [Debug^|Release] - exit /b 1 -) - -set OBJDIR=obj - -if not exist "%LIB_PATH%" mkdir "%LIB_PATH%" -if not exist obj mkdir obj - -set LIB_NAME=dear-imgui - -set SRC_FILES=^ - lib\third_party\dear-imgui\imgui.cpp ^ - lib\third_party\dear-imgui\imgui_widgets.cpp ^ - lib\third_party\dear-imgui\imgui_draw.cpp ^ - lib\third_party\dear-imgui\imgui_tables.cpp ^ - lib\third_party\dear-imgui\imgui_impl_dx11.cpp ^ - lib\third_party\dear-imgui\imgui_impl_win32.cpp - rem lib\third_party\dear-imgui\imgui_demo.cpp ^ - -set INCLUDE_DIRS=^ - /Ilib\third_party - -set LINK_LIBS= - -REM Build STATIC LIBRARY: rem /Fd%LIB_PATH%/%LIB_NAME%.pdb can be passed to cl if compiling with /Zi - echo Building static LIB (%CONFIG%) - cl /nologo /EHsc /DWIN32 /wd4530 %CFLAGS% %INCLUDE_DIRS% %SRC_FILES% /c /Fo%OBJDIR%\ - lib /OUT:%LIB_PATH%\%LIB_NAME%.lib %OBJDIR%\*.obj %LINK_LIBS% - -rd /s /q "%OBJDIR%" - -endlocal diff --git a/lib/Base/Base.h b/lib/Base/Base.h index ec4a377..3ad4cf9 100644 --- a/lib/Base/Base.h +++ b/lib/Base/Base.h @@ -1,6 +1,157 @@ #pragma once -#define LANG_CPP 1 +// Some of these macros are ""borrowed"" from nick aversano +// https://github.com/nickav/na/blob/main/na.h +// #OS_Platform +#if defined(_WIN32) + #define OS_WINDOWS 1 +#elif defined(__APPLE__) + #define OS_MACOS 1 +#elif defined(__linux__) + #define OS_LINUX 1 +#endif + +#if !defined(OS_WINDOWS) + #define OS_WINDOWS 0 +#endif +#if !defined(OS_LINUX) + #define OS_LINUX 0 +#endif +#if !defined(OS_MACOS) + #define OS_MACOS 0 +#endif + +#if defined(__cplusplus) + #define LANG_CPP 1 +#else + #define LANG_C 1 +#endif + +// #Compiler: Language +#if !defined(LANG_CPP) + #define LANG_CPP 0 +#endif +#if !defined(LANG_C) + #define LANG_C 0 +#endif + +// #Compiler: Vendor +#if defined(__clang__) + #define COMPILER_CLANG 1 +#elif defined(_MSC_VER) + #define COMPILER_MSVC 1 +#elif defined(__GNUC__) || defined(__GNUG__) + #define COMPILER_GCC 1 +#endif + +#if !defined(COMPILER_MSVC) + #define COMPILER_MSVC 0 +#endif +#if !defined(COMPILER_GCC) + #define COMPILER_GCC 0 +#endif +#if !defined(COMPILER_CLANG) + #define COMPILER_CLANG 0 +#endif + +#if COMPILER_MSVC + #if _MSC_VER >= 1930 + #define COMPILER_MSVC_YEAR 2022 + #elif _MSC_VER >= 1920 + #define COMPILER_MSVC_YEAR 2019 + #elif _MSC_VER >= 1910 + #define COMPILER_MSVC_YEAR 2017 + #elif _MSC_VER >= 1900 + #define COMPILER_MSVC_YEAR 2015 + #elif _MSC_VER >= 1800 + #define COMPILER_MSVC_YEAR 2013 + #elif _MSC_VER >= 1700 + #define COMPILER_MSVC_YEAR 2012 + #elif _MSC_VER >= 1600 + #define COMPILER_MSVC_YEAR 2010 + #elif _MSC_VER >= 1500 + #define COMPILER_MSVC_YEAR 2008 + #elif _MSC_VER >= 1400 + #define COMPILER_MSVC_YEAR 2005 + #else + #define COMPILER_MSVC_YEAR 0 + #endif +#endif + +// #Architecture: CPU Vendor +#if defined(_WIN32) + #if defined(_M_AMD64) + #define ARCH_CPU_X64 1 + #elif defined(_M_IX86) + #define ARCH_CPU_X86 1 + #elif defined(_M_ARM64) + #define ARCH_CPU_ARM64 1 + #elif defined(_M_ARM) + #define ARCH_CPU_ARM32 1 + #endif + +#else + #if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) + #define ARCH_CPU_X64 1 + #elif defined(i386) || defined(__i386) || defined(__i386__) + #define ARCH_CPU_X86 1 + #elif defined(__aarch64__) + #define ARCH_CPU_ARM64 1 + #elif defined(__arm__) + #define ARCH_CPU_ARM32 1 + #endif + +#endif + +#if !defined(ARCH_CPU_X64) + #define ARCH_CPU_X64 0 +#endif +#if !defined(ARCH_CPU_X86) + #define ARCH_CPU_X86 0 +#endif +#if !defined(ARCH_CPU_ARM64) + #define ARCH_CPU_ARM64 0 +#endif +#if !defined(ARCH_CPU_ARM32) + #define ARCH_CPU_ARM32 0 +#endif + +// #Architecture: Register Width +#if defined(ARCH_CPU_X64) || defined(ARCH_CPU_ARM64) + #define ARCH_64BIT 1 +#elif defined(ARCH_CPU_X86) || defined(ARCH_CPU_ARM32) + #define ARCH_32BIT 1 +#endif + +#if !defined(ARCH_64BIT) + #define ARCH_64BIT 0 +#endif +#if !defined(ARCH_32BIT) + #define ARCH_32BIT 0 +#endif + +// #Architecture: Endianness +static const int __arch_endian_check_num = 1; + +#define ARCH_LITTLE_ENDIAN (*(char *)&__arch_endian_check_num == 1) +#define ARCH_BIG_ENDIAN (!ARCH_LITTLE_ENDIAN) + +#if defined(_MSC_VER) + #ifdef _DEBUG + #define BUILD_DEBUG 1 + #endif +#elif defined(__GNUC__) || defined(__clang__) + #ifndef NDEBUG + #define BUILD_DEBUG 1 + #endif +#endif + +// #ifndef NDEBUG +// #define BUILD_DEBUG 1 +// #else +// #define BUILD_DEBUG 0 +// #endif + #define BUILD_CONSOLE_INTERFACE BUILD_DEBUG #include // vsnprintf diff --git a/lib/meta_generated.h b/lib/meta_generated.h deleted file mode 100644 index 700a5eb..0000000 --- a/lib/meta_generated.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -const char* MUSA_LIB_VERSION = "0.2"; -#define BUILD_DEBUG 1 -#define OS_WINDOWS 1 -#define OS_LINUX 0 -#define OS_MACOS 0 -#define OS_ANDROID 0 -#define OS_IOS 0 -#define ARCH_CPU_X64 1 -#define ARCH_CPU_ARM64 0 -#define OS_IS_UNIX 0 -#define COMPILER_MSVC 1 -#define COMPILER_CLANG 0 -#define ARRAY_ENABLE_BOUNDS_CHECKING 1 -#define COMPILER_GCC 0 diff --git a/lib_main.cpp b/lib_main.cpp index 0b2d8f6..818e281 100644 --- a/lib_main.cpp +++ b/lib_main.cpp @@ -10,7 +10,7 @@ // I'll see where it's used most often and see if I can make macros or templates to make // them easier to use. -#include "lib/meta_generated.h" +// #include "lib/meta_generated.h" #include "lib/Base/Base.h" #include "lib/Base/Allocator.h" #include "lib/Base/Array.h"