Add new cmake setting

Co-Authored-By: Yumacide <88495241+Yumacide@users.noreply.github.com>
This commit is contained in:
Acedia / Melancholy 2023-02-12 22:47:20 +06:30
parent c5089def6e
commit b0f2a2bf1e
3 changed files with 49 additions and 10 deletions

View file

@ -6,12 +6,13 @@ endif()
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
option(LUAU_BUILD_CLI "Build CLI" ON) option(LUAU_BUILD_CLI "Build CLI" OFF)
option(LUAU_BUILD_TESTS "Build tests" ON) option(LUAU_BUILD_TESTS "Build tests" OFF)
option(LUAU_BUILD_WEB "Build Web module" OFF) option(LUAU_BUILD_WEB "Build Web module" OFF)
option(LUAU_WERROR "Warnings as errors" OFF) option(LUAU_WERROR "Warnings as errors" OFF)
option(LUAU_STATIC_CRT "Link with the static CRT (/MT)" 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_EXTERN_C "Use extern C for all APIs" ON)
option(LUAU_EXPORT_DLL "Exports to DLL with C linkage" ON)
option(LUAU_NATIVE "Enable support for native code generation" OFF) option(LUAU_NATIVE "Enable support for native code generation" OFF)
if(LUAU_STATIC_CRT) if(LUAU_STATIC_CRT)
@ -23,11 +24,20 @@ endif()
project(Luau LANGUAGES CXX C) project(Luau LANGUAGES CXX C)
add_library(Luau.Common INTERFACE) add_library(Luau.Common INTERFACE)
add_library(Luau.Ast STATIC) add_library(Luau.Ast STATIC)
add_library(Luau.Compiler STATIC)
add_library(Luau.Analysis STATIC) if(LUAU_EXPORT_DLL)
add_library(Luau.CodeGen STATIC) add_library(Luau.Compiler SHARED)
add_library(Luau.VM STATIC) add_library(Luau.Analysis SHARED)
add_library(isocline STATIC) add_library(Luau.CodeGen SHARED)
add_library(Luau.VM SHARED)
add_library(isocline SHARED)
else()
add_library(Luau.Compiler STATIC)
add_library(Luau.Analysis STATIC)
add_library(Luau.CodeGen STATIC)
add_library(Luau.VM STATIC)
add_library(isocline STATIC)
endif()
if(LUAU_BUILD_CLI) if(LUAU_BUILD_CLI)
add_executable(Luau.Repl.CLI) add_executable(Luau.Repl.CLI)
@ -128,9 +138,20 @@ target_compile_options(isocline PRIVATE ${LUAU_OPTIONS} ${ISOCLINE_OPTIONS})
if(LUAU_EXTERN_C) 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 # 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 # 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
# LUAU_EXPORT_DLL exists to make Luau useable from non-C++ languages properly that cant rely on `extern`
target_compile_definitions(Luau.VM PUBLIC LUA_USE_LONGJMP=1) target_compile_definitions(Luau.VM PUBLIC LUA_USE_LONGJMP=1)
target_compile_definitions(Luau.VM PUBLIC LUA_API=extern\"C\") if(LUAU_EXPORT_DLL)
target_compile_definitions(Luau.Compiler PUBLIC LUACODE_API=extern\"C\") target_compile_definitions(Luau.VM PUBLIC LUA_API_C_EXPORT)
target_compile_definitions(Luau.Compiler PUBLIC LUACOPDE_API_C_EXPORT)
else()
target_compile_definitions(Luau.VM PUBLIC LUA_API=extern\"C\")
target_compile_definitions(Luau.Compiler PUBLIC LUACODE_API=extern\"C\")
endif()
else()
if(LUAU_EXPORT_DLL)
target_compile_definitions(Luau.VM PUBLIC LUA_API_EXPORT)
target_compile_definitions(Luau.Compiler PUBLIC LUACOPDE_API_EXPORT)
endif()
endif() endif()
if(LUAU_NATIVE) if(LUAU_NATIVE)

View file

@ -3,6 +3,15 @@
#include <stddef.h> #include <stddef.h>
// Can be used to export public APIs to DLL
#ifdef LUA_API_C_EXPORT
#define LUACODE_API extern "C" __declspec(dllexport)
#endif
#ifdef LUA_API_EXPORT
#define LUACODE_API extern __declspec(dllexport)
#endif
// Can be used to reconfigure visibility/exports for public APIs // Can be used to reconfigure visibility/exports for public APIs
#ifndef LUACODE_API #ifndef LUACODE_API
#define LUACODE_API extern #define LUACODE_API extern

View file

@ -33,6 +33,15 @@
#define LUA_NORETURN __attribute__((__noreturn__)) #define LUA_NORETURN __attribute__((__noreturn__))
#endif #endif
// Can be used to export public APIs to DLL
#ifdef LUA_API_C_EXPORT
#define LUA_API extern "C" __declspec(dllexport)
#endif
#ifdef LUA_API_EXPORT
#define LUA_API extern __declspec(dllexport)
#endif
// Can be used to reconfigure visibility/exports for public APIs // Can be used to reconfigure visibility/exports for public APIs
#ifndef LUA_API #ifndef LUA_API
#define LUA_API extern #define LUA_API extern