diff --git a/HConfig b/HConfig index 5042e3f..d3b4576 100644 --- a/HConfig +++ b/HConfig @@ -1,5 +1,6 @@ grbc_want_version("1.0") grbc_load_platform("platform.hcfg") +grbc_ext("GRBC_EXT_pkg_config") local grbc_pkg_config_ext = grbc_library(LibraryConfig.new({ name = "libgrbc_pkg_config", @@ -74,12 +75,7 @@ local grbc_exe = grbc_executable(ExecutableConfig.new({ }, requirements = { - Package.new({ - name = "lua", - compiler_flags = "", - linker_flags = "-llua -lm -ldl" - }), - + grbc_pkg_config("lua"), grbc_pkg("libgrbc"), grbc_pkg("libgrbc_pkg_config") }, diff --git a/include/grbc/spec.h b/include/grbc/spec.h index 73fede5..b8e2324 100644 --- a/include/grbc/spec.h +++ b/include/grbc/spec.h @@ -227,4 +227,4 @@ Package grbc_pkg(const std::string &package_name); void grbc_ext(const std::string &extension_id); -void grbc_register_ext(const std::string &extension_id, const Extension &ext); \ No newline at end of file +void grbc_register_ext(const Extension &ext); \ No newline at end of file diff --git a/include/grbc/state.h b/include/grbc/state.h index 045c3f0..5a1313c 100644 --- a/include/grbc/state.h +++ b/include/grbc/state.h @@ -1,9 +1,11 @@ #pragma once +#include "grbc/ext.h" #include "grbc/generator.h" #include "grbc/spec.h" #include #include #include +#include struct GState { Platform current_platform; @@ -14,6 +16,8 @@ struct GState { std::unordered_map packages; + std::vector extensions; + std::string global_compiler_flags = " "; std::string global_linker_flags = " "; diff --git a/src/ext.cc b/src/ext.cc index 77a6b3c..cc1c866 100644 --- a/src/ext.cc +++ b/src/ext.cc @@ -1,5 +1,26 @@ #include "grbc/ext.h" +#include "grbc/helpers.h" +#include "grbc/spec.h" +#include "grbc/state.h" -void grbc_register_ext(const std::string &extension_id, const Extension &ext) {} +void grbc_register_ext(const Extension &ext) { + GState::get().extensions.push_back(ext); -void grbc_ext(const std::string &extension_id) {} \ No newline at end of file + log_msg(("registered extension: " + ext.name).c_str()); + printf("> init pointer: %p\n", ext.hook_init); +} + +void grbc_ext(const std::string &extension_id) { + for (auto &ext : GState::get().extensions) { + if (ext.name != extension_id) + continue; + + log_msg(("loading extension: " + extension_id).c_str()); + + ext.hook_init(GState::get().lua); + + return; + } + + grbc_exception("Failed to find extension with name: " + extension_id); +} \ No newline at end of file diff --git a/src/ext_pkg_config.cc b/src/ext_pkg_config.cc index 48c6977..0a52f72 100644 --- a/src/ext_pkg_config.cc +++ b/src/ext_pkg_config.cc @@ -1,17 +1,61 @@ #include "grbc/ext_pkg_config.h" #include "grbc/ext.h" #include "grbc/helpers.h" +#include "grbc/spec.h" #include "sol/state.hpp" +#include -void grbc_pkg_config_init(sol::state &state) { - log_msg("pkg_config extension loaded!"); +std::string pkg_config_util_read_file(FILE *file) { + // http://www.fundza.com/c4serious/fileIO_reading_all/index.html + + char line[190]; + std::string result; + + while (fgets(line, 190, file)) + result += line; + + if (result.back() == '\n') + result.pop_back(); + + return result; } +Package EXT_pkg_config_get_pkg(const std::string &name) { + int found = std::system(("pkg-config " + name).c_str()); + + if (found != EXIT_SUCCESS) { + grbc_exception( + ("Failed to find package using pkg-config: " + name).c_str()); + } + + Package pkg{}; + + FILE *f_cflags = popen(("pkg-config --cflags " + name).c_str(), "r"); + FILE *f_libs = popen(("pkg-config --libs " + name).c_str(), "r"); + + std::string compiler_flags = pkg_config_util_read_file(f_cflags); + std::string linker_flags = pkg_config_util_read_file(f_libs); + + fclose(f_cflags); + fclose(f_libs); + + pkg.name = name; + pkg.compiler_flags = compiler_flags; + pkg.linker_flags = linker_flags; + + return pkg; +} + +void grbc_pkg_config_init(sol::state &state) { + state.set("grbc_pkg_config", EXT_pkg_config_get_pkg); + + log_msg("pkg-config supported loaded!"); +} Extension grbc_pkg_config() { - Extension ext{}; - ext.name = GRBC_EXT_pkg_config_NAME; - ext.hook_init = grbc_pkg_config_init; + Extension ext{}; + ext.name = GRBC_EXT_pkg_config_NAME; + ext.hook_init = grbc_pkg_config_init; - return ext; + return ext; } \ No newline at end of file diff --git a/src/main.cc b/src/main.cc index 34d2cff..7c0c689 100644 --- a/src/main.cc +++ b/src/main.cc @@ -21,15 +21,17 @@ int main() { // PackageConfig lua.new_usertype( - "PackageConfig", sol::constructors(), "name", &PackageConfig::name, "libraries", - &PackageConfig::libraries, "include_dirs", &PackageConfig::include_dirs, - "compile_flags", &PackageConfig::compile_flags, "linker_flags", + "PackageConfig", sol::constructors(), "name", + &PackageConfig::name, "libraries", &PackageConfig::libraries, + "include_dirs", &PackageConfig::include_dirs, "compile_flags", + &PackageConfig::compile_flags, "linker_flags", &PackageConfig::linker_flags); // GlobalConfig - lua.new_usertype( - "GlobalConfig", "engine_version", &GlobalConfig::engine_version, - "architecture", &GlobalConfig::architecture, "target_config", &GlobalConfig::platform_config); + lua.new_usertype("GlobalConfig", "engine_version", + &GlobalConfig::engine_version, "architecture", + &GlobalConfig::architecture, "target_config", + &GlobalConfig::platform_config); // Platform lua.new_usertype("Platform", "name", &Platform::name, @@ -101,7 +103,6 @@ int main() { lua.set("grbc_bake_package_config", grbc_bake_package_config); lua.set("grbc_pkg", grbc_pkg); lua.set("grbc_ext", grbc_ext); - lua.set("grbc_register_ext", grbc_register_ext); // Load generators @@ -109,7 +110,10 @@ int main() { {.name = "ninja", .func = ninja_generator}); // Load default extensions - grbc_register_ext(GRBC_EXT_pkg_config_NAME, grbc_pkg_config()); + +#if !defined(_WIN32) + grbc_register_ext(grbc_pkg_config()); +#endif log_msg("loading HConfig...");