From 56406b61d1305e986afaf0174723fab3acef8895 Mon Sep 17 00:00:00 2001 From: "K. S. Ernest (iFire) Lee" Date: Sun, 12 Sep 2021 14:14:10 -0700 Subject: [PATCH] Compile Godot Engine as a library. --- SConstruct | 19 ++++++++++++++++++- platform/linuxbsd/godot_linuxbsd.cpp | 4 ++++ platform/macos/godot_main_macos.mm | 4 ++++ platform/windows/godot_windows.cpp | 6 ++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/SConstruct b/SConstruct index d0d03bd8dc29..7b16d35c0b72 100644 --- a/SConstruct +++ b/SConstruct @@ -133,7 +133,6 @@ env_base.__class__.use_windows_spawn_fix = methods.use_windows_spawn_fix env_base.__class__.add_shared_library = methods.add_shared_library env_base.__class__.add_library = methods.add_library -env_base.__class__.add_program = methods.add_program env_base.__class__.CommandNoCache = methods.CommandNoCache env_base.__class__.Run = methods.Run env_base.__class__.disable_warnings = methods.disable_warnings @@ -184,6 +183,14 @@ opts.Add(BoolVariable("custom_modules_recursive", "Detect custom modules recursi opts.Add(BoolVariable("use_volk", "Use the volk library to load the Vulkan loader dynamically", True)) # Advanced options +opts.Add( + EnumVariable( + "library_type", + "Build library type", + "executable", + ("executable", "static_library", "shared_library"), + ) +) opts.Add(BoolVariable("dev", "If yes, alias for verbose=yes warnings=extra werror=yes", False)) opts.Add(BoolVariable("tests", "Build the unit tests", False)) opts.Add(BoolVariable("fast_unsafe", "Enable unsafe options for faster rebuilds", False)) @@ -246,6 +253,16 @@ opts.Update(env_base) selected_platform = "" +if env_base["library_type"] == "static_library": + env_base.__class__.add_program = methods.add_library + env_base.Append(CPPDEFINES=["LIBRARY_ENABLED"]) +elif env_base["library_type"] == "shared_library": + env_base.__class__.add_program = methods.add_shared_library + env_base.Append(CPPDEFINES=["LIBRARY_ENABLED"]) + env_base.env_base(CCFLAGS=["-fPIC"]) +else: + env_base.__class__.add_program = methods.add_program + if env_base["platform"] != "": selected_platform = env_base["platform"] elif env_base["p"] != "": diff --git a/platform/linuxbsd/godot_linuxbsd.cpp b/platform/linuxbsd/godot_linuxbsd.cpp index 91a260182e63..11d0f1496fec 100644 --- a/platform/linuxbsd/godot_linuxbsd.cpp +++ b/platform/linuxbsd/godot_linuxbsd.cpp @@ -40,7 +40,11 @@ #include "main/main.h" #include "os_linuxbsd.h" +#if defined(LIBRARY_ENABLED) +extern "C" int godot_main(int argc, char *argv[]) { +#else int main(int argc, char *argv[]) { +#endif #if defined(SANITIZERS_ENABLED) // Note: Set stack size to be at least 30 MB (vs 8 MB default) to avoid overflow, address sanitizer can increase stack usage up to 3 times. struct rlimit stack_lim = { 0x1E00000, 0x1E00000 }; diff --git a/platform/macos/godot_main_macos.mm b/platform/macos/godot_main_macos.mm index 66071f14046b..b2342b9dcda1 100644 --- a/platform/macos/godot_main_macos.mm +++ b/platform/macos/godot_main_macos.mm @@ -39,7 +39,11 @@ #include #endif +#if defined(LIBRARY_ENABLED) +extern "C" int godot_main(int argc, char *argv[]) { +#else int main(int argc, char **argv) { +#endif #if defined(VULKAN_ENABLED) // MoltenVK - enable full component swizzling support. setenv("MVK_CONFIG_FULL_IMAGE_VIEW_SWIZZLE", "1", 1); diff --git a/platform/windows/godot_windows.cpp b/platform/windows/godot_windows.cpp index 72920d281670..231a05116f1b 100644 --- a/platform/windows/godot_windows.cpp +++ b/platform/windows/godot_windows.cpp @@ -204,7 +204,11 @@ int _main() { return result; } +#if defined(LIBRARY_ENABLED) +extern "C" __declspec(dllexport) int godot_main(int argc, char *argv[]) { +#else int main(int argc, char **argv) { +#endif // override the arguments for the test handler / if symbol is provided // TEST_MAIN_OVERRIDE @@ -223,7 +227,9 @@ int main(int argc, char **argv) { HINSTANCE godot_hinstance = nullptr; +#if !defined(LIBRARY_ENABLED) int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { godot_hinstance = hInstance; return main(0, nullptr); } +#endif