From 2bb469b26f0d01d3e2317e737cb02a461a9e4be4 Mon Sep 17 00:00:00 2001 From: Interfiber Date: Sun, 29 Sep 2024 14:51:11 -0400 Subject: [PATCH] profile support --- HConfig | 16 +++++--- build.ninja | 17 ++++---- compile_commands.json | 74 ----------------------------------- include/grbc/ext.h | 2 + include/grbc/ext_profiles.h | 14 +++++++ include/grbc/spec.h | 4 +- include/grbc/state.h | 4 +- spec/ext.md | 4 ++ spec/functions.md | 3 ++ src/ext.cc | 12 ++++++ src/ext_pkg_config.cc | 2 +- src/ext_profiles.cc | 77 +++++++++++++++++++++++++++++++++++++ src/main.cc | 4 ++ 13 files changed, 142 insertions(+), 91 deletions(-) delete mode 100644 compile_commands.json create mode 100644 include/grbc/ext_profiles.h create mode 100644 src/ext_profiles.cc diff --git a/HConfig b/HConfig index d73ec00..6f06b5c 100644 --- a/HConfig +++ b/HConfig @@ -1,13 +1,17 @@ grbc_want_version("1.0") grbc_ext("GRBC_EXT_pkg_config") +grbc_ext("GRBC_EXT_profiles") -local grbc_pkg_config_ext = grbc_library(LibraryConfig.new({ - name = "libgrbc_pkg_config", +grbc_set_profile("release") + +local grbc_extensions = grbc_library(LibraryConfig.new({ + name = "libgrbc_extensions", language_type = LanguageType.Cpp, files = { - grbc_file("src/ext_pkg_config.cc") + grbc_file("src/ext_pkg_config.cc"), + grbc_file("src/ext_profiles.cc") }, - lib_type = LibraryType.Shared, + lib_type = LibraryType.Static, requirements = {}, compile_flags = {}, @@ -19,7 +23,7 @@ local grbc_pkg_config_ext = grbc_library(LibraryConfig.new({ }, package_config = PackageConfig.new({ - name = "libgrbc_pkg_config", + name = "libgrbc_extensions", libraries = {}, include_dirs = { grbc_file("include"), @@ -76,7 +80,7 @@ local grbc_exe = grbc_executable(ExecutableConfig.new({ requirements = { grbc_pkg_config("lua"), grbc_pkg("libgrbc"), - grbc_pkg("libgrbc_pkg_config") + grbc_pkg("libgrbc_extensions") }, compile_flags = { }, diff --git a/build.ninja b/build.ninja index 8712031..8df481d 100644 --- a/build.ninja +++ b/build.ninja @@ -43,13 +43,16 @@ rule archive ## Compile: src/ext_pkg_config.cc ## build $builddir/src/ext_pkg_config.o: cxx src/ext_pkg_config.cc - p_cflags = -fPIC -Iinclude -Ivendor/sol2/include + p_cflags = -Iinclude -Ivendor/sol2/include -## Link: libgrbc_pkg_config.so ## +## Compile: src/ext_profiles.cc ## -build $builddir/libgrbc_pkg_config.so: link_cxx $builddir/src/ext_pkg_config.o - p_linker_flags = -shared -Lbuild -Wl,-rpath,build:. - p_cflags = +build $builddir/src/ext_profiles.o: cxx src/ext_profiles.cc + p_cflags = -Iinclude -Ivendor/sol2/include + +## Link: libgrbc_extensions.a ## + +build $builddir/libgrbc_extensions.a: archive $builddir/src/ext_pkg_config.o $builddir/src/ext_profiles.o ## Compile: src/file.cc ## @@ -107,7 +110,7 @@ build $builddir/src/main.o: cxx src/main.cc ## Link: grbc ## -build $builddir/grbc: link_cxx $builddir/src/main.o | $builddir/libgrbc.a $builddir/libgrbc_pkg_config.so - p_linker_flags = -llua -lm -ldl build/libgrbc.a build/libgrbc_pkg_config.so -Lbuild -Wl,-rpath,build:. +build $builddir/grbc: link_cxx $builddir/src/main.o | $builddir/libgrbc.a $builddir/libgrbc_extensions.a + p_linker_flags = -llua -lm -ldl build/libgrbc.a build/libgrbc_extensions.a -O3 -Lbuild -Wl,-rpath,build:. p_cflags = diff --git a/compile_commands.json b/compile_commands.json deleted file mode 100644 index 570bd36..0000000 --- a/compile_commands.json +++ /dev/null @@ -1,74 +0,0 @@ -[ - { - "directory": "/home/interfiber/dev/grbc", - "command": "/usr/lib64/ccache/g++ -MMD -MT build/src/file.o -MF build/src/file.o.d -fPIC -Iinclude -Ivendor/sol2/include -c src/file.cc -o build/src/file.o", - "file": "src/file.cc", - "output": "build/src/file.o" - }, - { - "directory": "/home/interfiber/dev/grbc", - "command": "/usr/lib64/ccache/g++ -MMD -MT build/src/ninja.o -MF build/src/ninja.o.d -fPIC -Iinclude -Ivendor/sol2/include -c src/ninja.cc -o build/src/ninja.o", - "file": "src/ninja.cc", - "output": "build/src/ninja.o" - }, - { - "directory": "/home/interfiber/dev/grbc", - "command": "/usr/lib64/ccache/g++ -MMD -MT build/src/platform.o -MF build/src/platform.o.d -fPIC -Iinclude -Ivendor/sol2/include -c src/platform.cc -o build/src/platform.o", - "file": "src/platform.cc", - "output": "build/src/platform.o" - }, - { - "directory": "/home/interfiber/dev/grbc", - "command": "/usr/lib64/ccache/g++ -MMD -MT build/src/target_exe.o -MF build/src/target_exe.o.d -fPIC -Iinclude -Ivendor/sol2/include -c src/target_exe.cc -o build/src/target_exe.o", - "file": "src/target_exe.cc", - "output": "build/src/target_exe.o" - }, - { - "directory": "/home/interfiber/dev/grbc", - "command": "/usr/lib64/ccache/g++ -MMD -MT build/src/utils.o -MF build/src/utils.o.d -fPIC -Iinclude -Ivendor/sol2/include -c src/utils.cc -o build/src/utils.o", - "file": "src/utils.cc", - "output": "build/src/utils.o" - }, - { - "directory": "/home/interfiber/dev/grbc", - "command": "/usr/lib64/ccache/g++ -MMD -MT build/src/generator.o -MF build/src/generator.o.d -fPIC -Iinclude -Ivendor/sol2/include -c src/generator.cc -o build/src/generator.o", - "file": "src/generator.cc", - "output": "build/src/generator.o" - }, - { - "directory": "/home/interfiber/dev/grbc", - "command": "/usr/lib64/ccache/g++ -MMD -MT build/src/target_lib.o -MF build/src/target_lib.o.d -fPIC -Iinclude -Ivendor/sol2/include -c src/target_lib.cc -o build/src/target_lib.o", - "file": "src/target_lib.cc", - "output": "build/src/target_lib.o" - }, - { - "directory": "/home/interfiber/dev/grbc", - "command": "/usr/lib64/ccache/g++ -MMD -MT build/src/package.o -MF build/src/package.o.d -fPIC -Iinclude -Ivendor/sol2/include -c src/package.cc -o build/src/package.o", - "file": "src/package.cc", - "output": "build/src/package.o" - }, - { - "directory": "/home/interfiber/dev/grbc", - "command": "/usr/lib64/ccache/g++ -MMD -MT build/src/ext.o -MF build/src/ext.o.d -fPIC -Iinclude -Ivendor/sol2/include -c src/ext.cc -o build/src/ext.o", - "file": "src/ext.cc", - "output": "build/src/ext.o" - }, - { - "directory": "/home/interfiber/dev/grbc", - "command": "/usr/lib64/ccache/g++ -o build/libgrbc.so build/src/file.o build/src/ninja.o build/src/platform.o build/src/target_exe.o build/src/utils.o build/src/generator.o build/src/target_lib.o build/src/package.o build/src/ext.o -shared -Lbuild -Wl,-rpath,build:. ", - "file": "build/src/file.o", - "output": "build/libgrbc.so" - }, - { - "directory": "/home/interfiber/dev/grbc", - "command": "/usr/lib64/ccache/g++ -MMD -MT build/src/main.o -MF build/src/main.o.d -Iinclude -Ivendor/sol2/include -c src/main.cc -o build/src/main.o", - "file": "src/main.cc", - "output": "build/src/main.o" - }, - { - "directory": "/home/interfiber/dev/grbc", - "command": "/usr/lib64/ccache/g++ -o build/grbc build/src/main.o -llua -lm -ldl build/libgrbc.so -Lbuild -Wl,-rpath,build:. ", - "file": "build/src/main.o", - "output": "build/grbc" - } -] diff --git a/include/grbc/ext.h b/include/grbc/ext.h index ed9968b..c81fa20 100644 --- a/include/grbc/ext.h +++ b/include/grbc/ext.h @@ -3,11 +3,13 @@ #include #define GRBC_EXT_pkg_config_NAME "GRBC_EXT_pkg_config" +#define GRBC_EXT_profiles_NAME "GRBC_EXT_profiles" typedef void (*EXT_HookInit)(sol::state &lua); struct Extension { std::string name; + bool loaded = false; EXT_HookInit hook_init = nullptr; }; \ No newline at end of file diff --git a/include/grbc/ext_profiles.h b/include/grbc/ext_profiles.h new file mode 100644 index 0000000..d1f9ff7 --- /dev/null +++ b/include/grbc/ext_profiles.h @@ -0,0 +1,14 @@ +#pragma once +#include "grbc/ext.h" + +struct Profile { + std::string name; + + std::vector compiler_flags; + std::vector linker_flags; +}; + +/** + * Get the grbc profiles extension + */ +Extension grbc_profiles(); \ No newline at end of file diff --git a/include/grbc/spec.h b/include/grbc/spec.h index 1e491ea..2c49d0c 100644 --- a/include/grbc/spec.h +++ b/include/grbc/spec.h @@ -251,4 +251,6 @@ void grbc_register_ext(const Extension &ext); bool grbc_has_ext(const std::string &extension_id); std::string grbc_platform_file(PlatformType platform_type, - const std::string &file_name); \ No newline at end of file + const std::string &file_name); + +bool grbc_is_ext_loaded(const std::string &extension_name); \ No newline at end of file diff --git a/include/grbc/state.h b/include/grbc/state.h index 06e7c29..52fd4b0 100644 --- a/include/grbc/state.h +++ b/include/grbc/state.h @@ -18,8 +18,8 @@ struct GState { std::vector extensions; - std::string global_compiler_flags = " "; - std::string global_linker_flags = " "; + std::string global_compiler_flags = ""; + std::string global_linker_flags = ""; std::string ninja_output; diff --git a/spec/ext.md b/spec/ext.md index 5e0ab89..a059fa9 100644 --- a/spec/ext.md +++ b/spec/ext.md @@ -6,10 +6,14 @@ Extensions are built into the grbc executable, and are loaded at runtime as need ## Extension list * GRBC_EXT_pkg_config +* GRBC_EXT_profiles * GRBC_EXT_cmake [NOSUPPORT] ## GRBC_EXT_pkg_config Pkg config support. UNIX only +## GRBC_EXT_profiles +Support for compiler profiles, eg: Debug, ReleaseWithDebugSymbols, Release, etc + ## GRBC_EXT_cmake CMake interop support \ No newline at end of file diff --git a/spec/functions.md b/spec/functions.md index 072543a..a61496c 100644 --- a/spec/functions.md +++ b/spec/functions.md @@ -43,6 +43,9 @@ If the current platform is equal to platform then return the file path, otherwis ## [X] grbc_has_ext(extension_name: String) -> Boolean Check if the given extension is supported +## [X] grbc_is_ext_loaded(extension_name: String) -> Boolean +Check if the given extension is loaded + ## [X] grbc_ext(extension_name: String) -> Void Load the given extension into the script diff --git a/src/ext.cc b/src/ext.cc index 2c65071..154475c 100644 --- a/src/ext.cc +++ b/src/ext.cc @@ -28,8 +28,20 @@ void grbc_ext(const std::string &extension_id) { ext.hook_init(GState::get().lua); + ext.loaded = true; + return; } grbc_exception("Failed to find extension with name: " + extension_id); +} + +bool grbc_is_ext_loaded(const std::string &extension_name) { + + for (auto &ext : GState::get().extensions) { + if (ext.name == extension_name && ext.loaded) + return true; + } + + return false; } \ No newline at end of file diff --git a/src/ext_pkg_config.cc b/src/ext_pkg_config.cc index 0a52f72..30c3fc8 100644 --- a/src/ext_pkg_config.cc +++ b/src/ext_pkg_config.cc @@ -49,7 +49,7 @@ Package EXT_pkg_config_get_pkg(const std::string &name) { void grbc_pkg_config_init(sol::state &state) { state.set("grbc_pkg_config", EXT_pkg_config_get_pkg); - log_msg("pkg-config supported loaded!"); + log_msg("pkg-config support loaded!"); } Extension grbc_pkg_config() { diff --git a/src/ext_profiles.cc b/src/ext_profiles.cc new file mode 100644 index 0000000..eb9c86d --- /dev/null +++ b/src/ext_profiles.cc @@ -0,0 +1,77 @@ +#include "grbc/ext_profiles.h" +#include "grbc/ext.h" +#include "grbc/helpers.h" +#include "grbc/spec.h" +#include "grbc/state.h" + +std::vector &EXT_profiles_get_profiles() { + static std::vector profiles{}; + + return profiles; +} + +Profile *EXT_profiles_get_current_profile() { + static Profile profile{}; + + return &profile; +} + +void EXT_profiles_set(const std::string &profile_id) { + for (auto &profile : EXT_profiles_get_profiles()) { + if (profile.name != profile_id) + continue; + + *EXT_profiles_get_current_profile() = profile; + + log_msg(("set current profile to: " + profile_id).c_str()); + + // Maybe warn the user about this? + GState::get().global_compiler_flags = " "; + GState::get().global_linker_flags = " "; + + for (auto &linker_flag : profile.linker_flags) { + GState::get().global_linker_flags += linker_flag + " "; + } + + for (auto &compiler_flag : profile.compiler_flags) { + GState::get().global_compiler_flags += compiler_flag + " "; + } + + return; + } + + grbc_exception("Failed to locate profile with name: " + profile_id); +} + +void grbc_profiles_init(sol::state &lua) { + // Set default profiles + Profile debug_profile{}; + debug_profile.name = "debug"; + debug_profile.compiler_flags = {"-g", "-O1"}; + + Profile release_with_debug_symbols_profile{}; + release_with_debug_symbols_profile.name = "release_with_debug_symbols"; + release_with_debug_symbols_profile.compiler_flags = {"-g", "-O3"}; + + Profile release_profile{}; + release_profile.name = "release"; + release_profile.linker_flags = {"-O3"}; + + EXT_profiles_get_profiles().push_back(debug_profile); + EXT_profiles_get_profiles().push_back(release_with_debug_symbols_profile); + EXT_profiles_get_profiles().push_back(release_profile); + + EXT_profiles_set("debug"); // Always use debug as the default profile + + lua.set("grbc_set_profile", EXT_profiles_set); + + log_msg("profile support loaded!"); +} + +Extension grbc_profiles() { + Extension ext{}; + ext.name = GRBC_EXT_profiles_NAME; + ext.hook_init = grbc_profiles_init; + + return ext; +} \ No newline at end of file diff --git a/src/main.cc b/src/main.cc index eb7d3fd..592572a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,4 +1,5 @@ #include "grbc/ext_pkg_config.h" +#include "grbc/ext_profiles.h" #include "grbc/state.h" #define SOL_ALL_SAFETIES_ON 1 @@ -105,6 +106,7 @@ int main() { lua.set("grbc_ext", grbc_ext); lua.set("grbc_has_ext", grbc_has_ext); lua.set("grbc_platform_file", grbc_platform_file); + lua.set("grbc_is_ext_loaded", grbc_is_ext_loaded); // Load generators @@ -117,6 +119,8 @@ int main() { grbc_register_ext(grbc_pkg_config()); #endif + grbc_register_ext(grbc_profiles()); + // Detect platform log_msg("autodetecting platform...");