profile support

This commit is contained in:
Hunter 2024-09-29 14:51:11 -04:00
parent f32bf63c98
commit 2bb469b26f
13 changed files with 142 additions and 91 deletions

16
HConfig
View file

@ -1,13 +1,17 @@
grbc_want_version("1.0") grbc_want_version("1.0")
grbc_ext("GRBC_EXT_pkg_config") grbc_ext("GRBC_EXT_pkg_config")
grbc_ext("GRBC_EXT_profiles")
local grbc_pkg_config_ext = grbc_library(LibraryConfig.new({ grbc_set_profile("release")
name = "libgrbc_pkg_config",
local grbc_extensions = grbc_library(LibraryConfig.new({
name = "libgrbc_extensions",
language_type = LanguageType.Cpp, language_type = LanguageType.Cpp,
files = { 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 = {}, requirements = {},
compile_flags = {}, compile_flags = {},
@ -19,7 +23,7 @@ local grbc_pkg_config_ext = grbc_library(LibraryConfig.new({
}, },
package_config = PackageConfig.new({ package_config = PackageConfig.new({
name = "libgrbc_pkg_config", name = "libgrbc_extensions",
libraries = {}, libraries = {},
include_dirs = { include_dirs = {
grbc_file("include"), grbc_file("include"),
@ -76,7 +80,7 @@ local grbc_exe = grbc_executable(ExecutableConfig.new({
requirements = { requirements = {
grbc_pkg_config("lua"), grbc_pkg_config("lua"),
grbc_pkg("libgrbc"), grbc_pkg("libgrbc"),
grbc_pkg("libgrbc_pkg_config") grbc_pkg("libgrbc_extensions")
}, },
compile_flags = { compile_flags = {
}, },

View file

@ -43,13 +43,16 @@ rule archive
## Compile: src/ext_pkg_config.cc ## ## Compile: src/ext_pkg_config.cc ##
build $builddir/src/ext_pkg_config.o: cxx 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 build $builddir/src/ext_profiles.o: cxx src/ext_profiles.cc
p_linker_flags = -shared -Lbuild -Wl,-rpath,build:. p_cflags = -Iinclude -Ivendor/sol2/include
p_cflags =
## 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 ## ## Compile: src/file.cc ##
@ -107,7 +110,7 @@ build $builddir/src/main.o: cxx src/main.cc
## Link: grbc ## ## Link: grbc ##
build $builddir/grbc: link_cxx $builddir/src/main.o | $builddir/libgrbc.a $builddir/libgrbc_pkg_config.so 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_pkg_config.so -Lbuild -Wl,-rpath,build:. p_linker_flags = -llua -lm -ldl build/libgrbc.a build/libgrbc_extensions.a -O3 -Lbuild -Wl,-rpath,build:.
p_cflags = p_cflags =

View file

@ -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"
}
]

View file

@ -3,11 +3,13 @@
#include <string> #include <string>
#define GRBC_EXT_pkg_config_NAME "GRBC_EXT_pkg_config" #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); typedef void (*EXT_HookInit)(sol::state &lua);
struct Extension { struct Extension {
std::string name; std::string name;
bool loaded = false;
EXT_HookInit hook_init = nullptr; EXT_HookInit hook_init = nullptr;
}; };

View file

@ -0,0 +1,14 @@
#pragma once
#include "grbc/ext.h"
struct Profile {
std::string name;
std::vector<std::string> compiler_flags;
std::vector<std::string> linker_flags;
};
/**
* Get the grbc profiles extension
*/
Extension grbc_profiles();

View file

@ -252,3 +252,5 @@ bool grbc_has_ext(const std::string &extension_id);
std::string grbc_platform_file(PlatformType platform_type, std::string grbc_platform_file(PlatformType platform_type,
const std::string &file_name); const std::string &file_name);
bool grbc_is_ext_loaded(const std::string &extension_name);

View file

@ -6,10 +6,14 @@ Extensions are built into the grbc executable, and are loaded at runtime as need
## Extension list ## Extension list
* GRBC_EXT_pkg_config * GRBC_EXT_pkg_config
* GRBC_EXT_profiles
* GRBC_EXT_cmake [NOSUPPORT] * GRBC_EXT_cmake [NOSUPPORT]
## GRBC_EXT_pkg_config ## GRBC_EXT_pkg_config
Pkg config support. UNIX only Pkg config support. UNIX only
## GRBC_EXT_profiles
Support for compiler profiles, eg: Debug, ReleaseWithDebugSymbols, Release, etc
## GRBC_EXT_cmake ## GRBC_EXT_cmake
CMake interop support CMake interop support

View file

@ -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 ## [X] grbc_has_ext(extension_name: String) -> Boolean
Check if the given extension is supported 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 ## [X] grbc_ext(extension_name: String) -> Void
Load the given extension into the script Load the given extension into the script

View file

@ -28,8 +28,20 @@ void grbc_ext(const std::string &extension_id) {
ext.hook_init(GState::get().lua); ext.hook_init(GState::get().lua);
ext.loaded = true;
return; return;
} }
grbc_exception("Failed to find extension with name: " + extension_id); 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;
}

View file

@ -49,7 +49,7 @@ Package EXT_pkg_config_get_pkg(const std::string &name) {
void grbc_pkg_config_init(sol::state &state) { void grbc_pkg_config_init(sol::state &state) {
state.set("grbc_pkg_config", EXT_pkg_config_get_pkg); 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() { Extension grbc_pkg_config() {

77
src/ext_profiles.cc Normal file
View file

@ -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<Profile> &EXT_profiles_get_profiles() {
static std::vector<Profile> 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;
}

View file

@ -1,4 +1,5 @@
#include "grbc/ext_pkg_config.h" #include "grbc/ext_pkg_config.h"
#include "grbc/ext_profiles.h"
#include "grbc/state.h" #include "grbc/state.h"
#define SOL_ALL_SAFETIES_ON 1 #define SOL_ALL_SAFETIES_ON 1
@ -105,6 +106,7 @@ int main() {
lua.set("grbc_ext", grbc_ext); lua.set("grbc_ext", grbc_ext);
lua.set("grbc_has_ext", grbc_has_ext); lua.set("grbc_has_ext", grbc_has_ext);
lua.set("grbc_platform_file", grbc_platform_file); lua.set("grbc_platform_file", grbc_platform_file);
lua.set("grbc_is_ext_loaded", grbc_is_ext_loaded);
// Load generators // Load generators
@ -117,6 +119,8 @@ int main() {
grbc_register_ext(grbc_pkg_config()); grbc_register_ext(grbc_pkg_config());
#endif #endif
grbc_register_ext(grbc_profiles());
// Detect platform // Detect platform
log_msg("autodetecting platform..."); log_msg("autodetecting platform...");