From f32bf63c98e71d23b06f3b37ff240b4003057fc3 Mon Sep 17 00:00:00 2001 From: Interfiber Date: Sun, 29 Sep 2024 11:07:13 -0400 Subject: [PATCH] default platform + impl all functions --- HConfig | 1 - include/grbc/spec.h | 30 +++++++++++++++++++++++++++--- include/grbc/state.h | 2 +- platform.hcfg | 12 ------------ spec/datatypes.md | 19 ------------------- spec/ext.md | 7 +++---- spec/functions.md | 10 +++++----- src/ext.cc | 9 +++++++++ src/main.cc | 21 +++++++++++++++++++++ src/platform.cc | 6 ++++++ 10 files changed, 72 insertions(+), 45 deletions(-) delete mode 100644 platform.hcfg diff --git a/HConfig b/HConfig index d3b4576..d73ec00 100644 --- a/HConfig +++ b/HConfig @@ -1,5 +1,4 @@ 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({ diff --git a/include/grbc/spec.h b/include/grbc/spec.h index b8e2324..1e491ea 100644 --- a/include/grbc/spec.h +++ b/include/grbc/spec.h @@ -5,6 +5,25 @@ #include #include +// https://stackoverflow.com/questions/1505582/determining-32-vs-64-bit-in-c + +#if _WIN32 || _WIN64 +#if _WIN64 +#define ENVIRONMENT64 +#else +#define ENVIRONMENT32 +#endif +#endif + +// Check GCC +#if __GNUC__ +#if __x86_64__ || __ppc64__ +#define ENVIRONMENT64 +#else +#define ENVIRONMENT32 +#endif +#endif + #define GRBC_VERSION "1.0" enum LanguageType { LanguageType_CPP, LanguageType_C }; @@ -171,7 +190,7 @@ struct Platform { bool is_64bit = true; /// Type of the platform - PlatformType platform_type; + PlatformType platform_type = PlatformType_Linux; }; /// Functions @@ -180,7 +199,7 @@ void grbc_want_version(const std::string &version); void grbc_exception(const std::string &exception_string); -GlobalConfig& grbc_get_config(); +GlobalConfig &grbc_get_config(); std::string grbc_file(const std::string &file_path); @@ -227,4 +246,9 @@ Package grbc_pkg(const std::string &package_name); void grbc_ext(const std::string &extension_id); -void grbc_register_ext(const Extension &ext); \ No newline at end of file +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 diff --git a/include/grbc/state.h b/include/grbc/state.h index 5a1313c..06e7c29 100644 --- a/include/grbc/state.h +++ b/include/grbc/state.h @@ -8,7 +8,7 @@ #include struct GState { - Platform current_platform; + Platform current_platform{}; sol::state lua; std::vector targets; diff --git a/platform.hcfg b/platform.hcfg deleted file mode 100644 index 2dc11dd..0000000 --- a/platform.hcfg +++ /dev/null @@ -1,12 +0,0 @@ --- Linux platform - -local platform = Platform.new() - -platform.name = "Linux64" -platform.cxx_compiler = grbc_find_compiler("g++") -platform.cc_compiler = grbc_find_compiler("gcc") -platform.is_64bit = true -- If set to false the compiler will need to produce 32bit code - -platform.platform_type = PlatformType.Linux - -return platform diff --git a/spec/datatypes.md b/spec/datatypes.md index 26e0044..0c1fd2e 100644 --- a/spec/datatypes.md +++ b/spec/datatypes.md @@ -136,15 +136,6 @@ struct GlobalConfig { }; ``` -## OptionalFileType -``` -enum OptionalFileType { - OFileType_IsWin32, - OFileType_IsUnix, - OFileType_IsLinux -}; -``` - ## Platform ```c++ struct Platform { @@ -171,14 +162,4 @@ enum LanguageType { LanguageType_CPP, LanguageType_C }; -``` - -## Profile -```c++ -struct Profile { - string name; - - Array compiler_flags; - Array linker_flags; -}; ``` \ No newline at end of file diff --git a/spec/ext.md b/spec/ext.md index 1e68eca..5e0ab89 100644 --- a/spec/ext.md +++ b/spec/ext.md @@ -2,15 +2,14 @@ Extensions can be loaded at runtime using grbc_ext(...), functions are then dumped into the runtime ## How extensions work -Extensions are built into the grbc executable, or can be compiled as a shared library and put into ```/usr/local/share/grbc/extensions```, ```C:\GRBC\Extensions\```, or ```~/.local/share/grbc/extensions``` +Extensions are built into the grbc executable, and are loaded at runtime as needed ## Extension list * GRBC_EXT_pkg_config -* GRBC_EXT_multi_file [NOSUPPORT] * GRBC_EXT_cmake [NOSUPPORT] ## GRBC_EXT_pkg_config Pkg config support. UNIX only -## GRBC_EXT_multi_file -Multi file GRBC scripts, supported on all systems. \ No newline at end of file +## GRBC_EXT_cmake +CMake interop support \ No newline at end of file diff --git a/spec/functions.md b/spec/functions.md index e8505a4..072543a 100644 --- a/spec/functions.md +++ b/spec/functions.md @@ -31,19 +31,19 @@ Create a new executable and add it to the build list ## [X] grbc_library(library_config: LibraryConfig) -> TargetInfo Create a new library and add it to the build list -## grbc_pkg(package_name: String) -> Package +## [X] grbc_pkg(package_name: String) -> Package Get a package with the given name and return its baked form ## [X] grbc_file(file_path: String) -> Path Used when listing source files, should perform pre-checks on the file and return its path -## grbc_file_optional(file_type: OptionalFileType, file_path: String) -> Path -If file_type is true then file_path is returned, otherwise a blank string +## [X] grbc_platform_file(platform: PlatformType, file_path: String) -> String +If the current platform is equal to platform then return the file path, otherwise return an empty string -## grbc_has_ext(extension_name: String) -> Boolean +## [X] grbc_has_ext(extension_name: String) -> Boolean Check if the given extension is supported -## grbc_ext(extension_name: String) -> Void +## [X] grbc_ext(extension_name: String) -> Void Load the given extension into the script ## [X] grbc_exception(exception_string: String) -> Void diff --git a/src/ext.cc b/src/ext.cc index cc1c866..2c65071 100644 --- a/src/ext.cc +++ b/src/ext.cc @@ -10,6 +10,15 @@ void grbc_register_ext(const Extension &ext) { printf("> init pointer: %p\n", ext.hook_init); } +bool grbc_has_ext(const std::string &extension_id) { + for (auto &ext : GState::get().extensions) { + if (ext.name == extension_id) + return true; + } + + return false; +} + void grbc_ext(const std::string &extension_id) { for (auto &ext : GState::get().extensions) { if (ext.name != extension_id) diff --git a/src/main.cc b/src/main.cc index 7c0c689..eb7d3fd 100644 --- a/src/main.cc +++ b/src/main.cc @@ -103,6 +103,8 @@ 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_has_ext", grbc_has_ext); + lua.set("grbc_platform_file", grbc_platform_file); // Load generators @@ -115,6 +117,25 @@ int main() { grbc_register_ext(grbc_pkg_config()); #endif + // Detect platform + log_msg("autodetecting platform..."); + +#if defined(WIN32) + GState::get().current_platform.platform_type = PlatformType_Win32; + GState::get().current_platform.name = "Windows"; +#elif defined(__linux__) + GState::get().current_platform.platform_type = PlatformType_Linux; + GState::get().current_platform.name = "Linux"; +#endif + +#if defined(ENVIRONMENT32) + GState::get().current_platform.is_64bit = false; +#endif + + // Default compiler is g++ + GState::get().current_platform.cc_compiler = grbc_find_compiler("gcc"); + GState::get().current_platform.cxx_compiler = grbc_find_compiler("g++"); + log_msg("loading HConfig..."); lua.script_file("HConfig"); diff --git a/src/platform.cc b/src/platform.cc index 8d59bc4..d622b75 100644 --- a/src/platform.cc +++ b/src/platform.cc @@ -90,4 +90,10 @@ bool grbc_is_64bit() { bool grbc_is_32bit() { return !grbc_is_64bit(); +} + +std::string grbc_platform_file(PlatformType platform_type, const std::string &file_name) { + if (grbc_get_platform() == platform_type) return grbc_file(file_name); + + return ""; } \ No newline at end of file