grbc/src/main.cc
2024-09-29 15:24:13 -04:00

155 lines
No EOL
5.8 KiB
C++

#include "grbc/ext_pkg_config.h"
#include "grbc/ext_profiles.h"
#include "grbc/state.h"
#include <sol/raii.hpp>
#include <sol/table.hpp>
#define SOL_ALL_SAFETIES_ON 1
#include "grbc/helpers.h"
#include "grbc/spec.h"
#include <sol/sol.hpp>
int main() {
log_msg("opening lua libraries...");
auto &lua = GState::get().lua;
lua.open_libraries(sol::lib::base, sol::lib::coroutine, sol::lib::string,
sol::lib::io, sol::lib::package, sol::lib::math);
log_msg("setting up runtime...");
// TargetInfo
lua.new_usertype<TargetInfo>("TargetInfo", "name", &TargetInfo::name);
// PackageConfig
lua.new_usertype<PackageConfig>(
"PackageConfig", sol::constructors<PackageConfig(sol::table)>(), "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>("GlobalConfig", "engine_version",
&GlobalConfig::engine_version, "architecture",
&GlobalConfig::architecture, "target_config",
&GlobalConfig::platform_config);
// Platform
lua.new_usertype<Platform>("Platform", "name", &Platform::name,
"cxx_compiler", &Platform::cxx_compiler,
"cc_compiler", &Platform::cc_compiler,
"platform_type", &Platform::platform_type,
"is_64bit", &Platform::is_64bit);
// PlatformType
lua.new_enum<PlatformType>("PlatformType", {{"Win32", PlatformType_Win32},
{"Linux", PlatformType_Linux}});
// LanguageType
lua.new_enum<LanguageType>(
"LanguageType", {{"Cpp", LanguageType_CPP}, {"C", LanguageType_C}});
// LibraryType
lua.new_enum<LibraryType>("LibraryType", {{"Static", LibraryType_Static},
{"Shared", LibraryType_Shared}});
// ExecutableConfig
lua.new_usertype<ExecutableConfig>(
"ExecutableConfig", sol::constructors<ExecutableConfig(sol::table)>(),
"name", &ExecutableConfig::name, "files", &ExecutableConfig::files,
"requirements", &ExecutableConfig::requirements, "compile_flags",
&ExecutableConfig::compile_flags, "linker_flags",
&ExecutableConfig::linker_flags, "include_dirs",
&ExecutableConfig::include_dirs, "language_type",
&ExecutableConfig::language_type);
// LibraryConfig
lua.new_usertype<LibraryConfig>(
"LibraryConfig", sol::constructors<LibraryConfig(sol::table)>(), "name",
&LibraryConfig::name, "files", &LibraryConfig::files, "requirements",
&LibraryConfig::requirements, "compile_flags",
&LibraryConfig::compile_flags, "linker_flags",
&LibraryConfig::linker_flags, "include_dirs",
&LibraryConfig::include_dirs, "language_type",
&LibraryConfig::language_type, "lib_type", &LibraryConfig::lib_type,
"package_config", &LibraryConfig::package_config);
// Package
lua.new_usertype<Package>("Package", sol::constructors<Package(sol::table)>(),
"name", &Package::name, "compiler_flags",
&Package::compiler_flags, "linker_flags",
&Package::linker_flags);
// TaskConfig
lua.new_usertype<TaskConfig>(
"TaskConfig", sol::constructors<TaskConfig(sol::table)>(), "name",
&TaskConfig::name, "task_id", &TaskConfig::task_id, "shell_script",
&TaskConfig::shell_script);
lua.set("grbc_want_version", grbc_want_version);
lua.set("grbc_exception", grbc_exception);
lua.set("grbc_get_config", grbc_get_config);
lua.set("grbc_file", grbc_file);
lua.set("grbc_executable", grbc_executable);
lua.set("grbc_library", grbc_library);
lua.set("grbc_load_platform", grbc_load_platform);
lua.set("grbc_set_platform", grbc_set_platform);
lua.set("grbc_find_compiler", grbc_find_compiler);
lua.set("grbc_build", grbc_build);
lua.set("grbc_replace_string", grbc_replace_string);
lua.set("grbc_object_file", grbc_object_file);
lua.set("grbc_include_dirs_to_clflags", grbc_include_dirs_to_cflags);
lua.set("grbc_log", grbc_log);
lua.set("grbc_is_32bit", grbc_is_32bit);
lua.set("grbc_is_64bit", grbc_is_64bit);
lua.set("grbc_is_linux", grbc_is_linux);
lua.set("grbc_is_win32", grbc_is_win32);
lua.set("grbc_get_platform", grbc_get_platform);
lua.set("grbc_compiler_define", grbc_compiler_define);
lua.set("grbc_get_lib_extension", grbc_get_lib_extension);
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);
lua.set("grbc_is_ext_loaded", grbc_is_ext_loaded);
lua.set("grbc_task", grbc_task);
// Load generators
GState::get().generators.push_back(
{.name = "ninja", .func = ninja_generator});
// Load default extensions
#if !defined(_WIN32)
grbc_register_ext(grbc_pkg_config());
#endif
grbc_register_ext(grbc_profiles());
// 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");
}