From 9eb4ee69fee3e117f40cb9e40cc3fac846afbee2 Mon Sep 17 00:00:00 2001 From: James <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Tue, 18 Mar 2025 18:22:19 +0000 Subject: [PATCH 1/2] Add cmake setting to build as dll --- CMakeLists.txt | 49 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5286fd9f0..08cf6fd85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,8 @@ option(LUAU_BUILD_WEB "Build Web module" OFF) option(LUAU_WERROR "Warnings as errors" OFF) option(LUAU_STATIC_CRT "Link with the static CRT (/MT)" OFF) option(LUAU_EXTERN_C "Use extern C for all APIs" OFF) +option(LUAU_EXPORT_DLL "Exports to DLL" OFF) + cmake_policy(SET CMP0054 NEW) cmake_policy(SET CMP0091 NEW) @@ -25,13 +27,24 @@ project(Luau LANGUAGES CXX C) add_library(Luau.Common INTERFACE) add_library(Luau.CLI.lib STATIC) add_library(Luau.Ast STATIC) -add_library(Luau.Compiler STATIC) -add_library(Luau.Config STATIC) -add_library(Luau.Analysis STATIC) -add_library(Luau.EqSat STATIC) -add_library(Luau.CodeGen STATIC) -add_library(Luau.VM STATIC) -add_library(isocline STATIC) + +if(LUAU_EXPORT_DLL) + add_library(Luau.Compiler SHARED) + add_library(Luau.Config SHARED) + add_library(Luau.Analysis SHARED) + add_library(Luau.EqSat SHARED) + add_library(Luau.CodeGen SHARED) + add_library(Luau.VM SHARED) + add_library(isocline SHARED) +else() + add_library(Luau.Compiler STATIC) + add_library(Luau.Config STATIC) + add_library(Luau.Analysis STATIC) + add_library(Luau.EqSat STATIC) + add_library(Luau.CodeGen STATIC) + add_library(Luau.VM STATIC) + add_library(isocline STATIC) +endif() if(LUAU_BUILD_CLI) add_executable(Luau.Repl.CLI) @@ -161,9 +174,25 @@ if(LUAU_EXTERN_C) # enable extern "C" for VM (lua.h, lualib.h) and Compiler (luacode.h) to make Luau friendlier to use from non-C++ languages # note that we enable LUA_USE_LONGJMP=1 as well; otherwise functions like luaL_error will throw C++ exceptions, which can't be done from extern "C" functions target_compile_definitions(Luau.VM PUBLIC LUA_USE_LONGJMP=1) - target_compile_definitions(Luau.VM PUBLIC LUA_API=extern\"C\") - target_compile_definitions(Luau.Compiler PUBLIC LUACODE_API=extern\"C\") - target_compile_definitions(Luau.CodeGen PUBLIC LUACODEGEN_API=extern\"C\") + + if(LUAU_EXPORT_DLL) + if(MSVC) + target_compile_definitions(Luau.VM PUBLIC "LUA_API=extern \"C\" __declspec(dllexport)") + target_compile_definitions(Luau.Compiler PUBLIC "LUACODE_API=extern \"C\" __declspec(dllexport)") + target_compile_definitions(Luau.CodeGen PUBLIC "LUACODEGEN_API=extern \"C\" __declspec(dllexport)") + else() + # GCC/Clang + target_compile_definitions(Luau.VM PUBLIC "LUA_API=extern \"C\" __attribute__((visibility(\"default\")))") + target_compile_definitions(Luau.Compiler PUBLIC "LUACODE_API=extern \"C\" __attribute__((visibility(\"default\")))") + target_compile_definitions(Luau.CodeGen PUBLIC "LUACODEGEN_API=extern \"C\" __attribute__((visibility(\"default\")))") + endif() + else() + # For static builds (or if not exporting) just use extern "C" + target_compile_definitions(Luau.VM PUBLIC "LUA_API=extern \"C\"") + target_compile_definitions(Luau.Compiler PUBLIC "LUACODE_API=extern \"C\"") + target_compile_definitions(Luau.CodeGen PUBLIC "LUACODEGEN_API=extern \"C\"") + endif() + endif() if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND MSVC_VERSION GREATER_EQUAL 1924) From 91896883fece05b1d423a0c6c8fdeef1718b61bd Mon Sep 17 00:00:00 2001 From: James <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Tue, 17 Jun 2025 22:24:18 +0200 Subject: [PATCH 2/2] cmake changes --- CMakeLists.txt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 08cf6fd85..c3e6e50d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,8 +12,11 @@ option(LUAU_BUILD_WEB "Build Web module" OFF) option(LUAU_WERROR "Warnings as errors" OFF) option(LUAU_STATIC_CRT "Link with the static CRT (/MT)" OFF) option(LUAU_EXTERN_C "Use extern C for all APIs" OFF) -option(LUAU_EXPORT_DLL "Exports to DLL" OFF) +option(LUAU_BUILD_SHARED "Build as a shared library" OFF) +if(LUAU_BUILD_SHARED AND NOT LUAU_EXTERN_C) + message(FATAL_ERROR "LUAU_BUILD_SHARED requires LUAU_EXTERN_C to be ON") +endif() cmake_policy(SET CMP0054 NEW) cmake_policy(SET CMP0091 NEW) @@ -28,7 +31,7 @@ add_library(Luau.Common INTERFACE) add_library(Luau.CLI.lib STATIC) add_library(Luau.Ast STATIC) -if(LUAU_EXPORT_DLL) +if(LUAU_BUILD_SHARED) add_library(Luau.Compiler SHARED) add_library(Luau.Config SHARED) add_library(Luau.Analysis SHARED) @@ -175,7 +178,7 @@ if(LUAU_EXTERN_C) # note that we enable LUA_USE_LONGJMP=1 as well; otherwise functions like luaL_error will throw C++ exceptions, which can't be done from extern "C" functions target_compile_definitions(Luau.VM PUBLIC LUA_USE_LONGJMP=1) - if(LUAU_EXPORT_DLL) + if(LUAU_BUILD_SHARED) if(MSVC) target_compile_definitions(Luau.VM PUBLIC "LUA_API=extern \"C\" __declspec(dllexport)") target_compile_definitions(Luau.Compiler PUBLIC "LUACODE_API=extern \"C\" __declspec(dllexport)")