pkg-config extension

This commit is contained in:
Hunter 2024-09-29 10:39:24 -04:00
parent fc1964f766
commit 3183c676de
6 changed files with 92 additions and 23 deletions

View file

@ -1,5 +1,6 @@
grbc_want_version("1.0") grbc_want_version("1.0")
grbc_load_platform("platform.hcfg") grbc_load_platform("platform.hcfg")
grbc_ext("GRBC_EXT_pkg_config")
local grbc_pkg_config_ext = grbc_library(LibraryConfig.new({ local grbc_pkg_config_ext = grbc_library(LibraryConfig.new({
name = "libgrbc_pkg_config", name = "libgrbc_pkg_config",
@ -74,12 +75,7 @@ local grbc_exe = grbc_executable(ExecutableConfig.new({
}, },
requirements = { requirements = {
Package.new({ grbc_pkg_config("lua"),
name = "lua",
compiler_flags = "",
linker_flags = "-llua -lm -ldl"
}),
grbc_pkg("libgrbc"), grbc_pkg("libgrbc"),
grbc_pkg("libgrbc_pkg_config") grbc_pkg("libgrbc_pkg_config")
}, },

View file

@ -227,4 +227,4 @@ Package grbc_pkg(const std::string &package_name);
void grbc_ext(const std::string &extension_id); void grbc_ext(const std::string &extension_id);
void grbc_register_ext(const std::string &extension_id, const Extension &ext); void grbc_register_ext(const Extension &ext);

View file

@ -1,9 +1,11 @@
#pragma once #pragma once
#include "grbc/ext.h"
#include "grbc/generator.h" #include "grbc/generator.h"
#include "grbc/spec.h" #include "grbc/spec.h"
#include <sol/sol.hpp> #include <sol/sol.hpp>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <vector>
struct GState { struct GState {
Platform current_platform; Platform current_platform;
@ -14,6 +16,8 @@ struct GState {
std::unordered_map<std::string, Package> packages; std::unordered_map<std::string, Package> packages;
std::vector<Extension> extensions;
std::string global_compiler_flags = " "; std::string global_compiler_flags = " ";
std::string global_linker_flags = " "; std::string global_linker_flags = " ";

View file

@ -1,5 +1,26 @@
#include "grbc/ext.h" #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) {} 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);
}

View file

@ -1,12 +1,56 @@
#include "grbc/ext_pkg_config.h" #include "grbc/ext_pkg_config.h"
#include "grbc/ext.h" #include "grbc/ext.h"
#include "grbc/helpers.h" #include "grbc/helpers.h"
#include "grbc/spec.h"
#include "sol/state.hpp" #include "sol/state.hpp"
#include <cstdlib>
void grbc_pkg_config_init(sol::state &state) { std::string pkg_config_util_read_file(FILE *file) {
log_msg("pkg_config extension loaded!"); // 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 grbc_pkg_config() {
Extension ext{}; Extension ext{};

View file

@ -21,15 +21,17 @@ int main() {
// PackageConfig // PackageConfig
lua.new_usertype<PackageConfig>( lua.new_usertype<PackageConfig>(
"PackageConfig", sol::constructors<PackageConfig(sol::table)>(), "name", &PackageConfig::name, "libraries", "PackageConfig", sol::constructors<PackageConfig(sol::table)>(), "name",
&PackageConfig::libraries, "include_dirs", &PackageConfig::include_dirs, &PackageConfig::name, "libraries", &PackageConfig::libraries,
"compile_flags", &PackageConfig::compile_flags, "linker_flags", "include_dirs", &PackageConfig::include_dirs, "compile_flags",
&PackageConfig::compile_flags, "linker_flags",
&PackageConfig::linker_flags); &PackageConfig::linker_flags);
// GlobalConfig // GlobalConfig
lua.new_usertype<GlobalConfig>( lua.new_usertype<GlobalConfig>("GlobalConfig", "engine_version",
"GlobalConfig", "engine_version", &GlobalConfig::engine_version, &GlobalConfig::engine_version, "architecture",
"architecture", &GlobalConfig::architecture, "target_config", &GlobalConfig::platform_config); &GlobalConfig::architecture, "target_config",
&GlobalConfig::platform_config);
// Platform // Platform
lua.new_usertype<Platform>("Platform", "name", &Platform::name, lua.new_usertype<Platform>("Platform", "name", &Platform::name,
@ -101,7 +103,6 @@ int main() {
lua.set("grbc_bake_package_config", grbc_bake_package_config); lua.set("grbc_bake_package_config", grbc_bake_package_config);
lua.set("grbc_pkg", grbc_pkg); lua.set("grbc_pkg", grbc_pkg);
lua.set("grbc_ext", grbc_ext); lua.set("grbc_ext", grbc_ext);
lua.set("grbc_register_ext", grbc_register_ext);
// Load generators // Load generators
@ -109,7 +110,10 @@ int main() {
{.name = "ninja", .func = ninja_generator}); {.name = "ninja", .func = ninja_generator});
// Load default extensions // 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..."); log_msg("loading HConfig...");