2021-10-30 04:25:12 +08:00
# This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
if ( EXT_PLATFORM_STRING )
include ( EXTLuau.cmake )
return ( )
endif ( )
cmake_minimum_required ( VERSION 3.0 )
option ( LUAU_BUILD_CLI "Build CLI" ON )
option ( LUAU_BUILD_TESTS "Build tests" ON )
2021-12-03 07:20:08 +08:00
option ( LUAU_BUILD_WEB "Build Web module" OFF )
2021-11-19 06:21:07 +08:00
option ( LUAU_WERROR "Warnings as errors" OFF )
2022-02-12 02:43:14 +08:00
option ( LUAU_STATIC_CRT "Link with the static CRT (/MT)" OFF )
2022-06-24 09:44:07 +08:00
option ( LUAU_EXTERN_C "Use extern C for all APIs" OFF )
2022-02-12 02:43:14 +08:00
2023-03-03 21:45:38 +08:00
cmake_policy ( SET CMP0054 NEW )
cmake_policy ( SET CMP0091 NEW )
2022-02-12 02:43:14 +08:00
if ( LUAU_STATIC_CRT )
cmake_minimum_required ( VERSION 3.15 )
set ( CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>" )
endif ( )
2021-10-30 04:25:12 +08:00
2022-02-12 02:43:14 +08:00
project ( Luau LANGUAGES CXX C )
2022-05-27 04:33:48 +08:00
add_library ( Luau.Common INTERFACE )
2024-03-23 01:21:27 +08:00
add_library ( Luau.CLI.lib STATIC )
2021-10-30 04:25:12 +08:00
add_library ( Luau.Ast STATIC )
add_library ( Luau.Compiler STATIC )
2023-08-19 01:06:29 +08:00
add_library ( Luau.Config STATIC )
2021-10-30 04:25:12 +08:00
add_library ( Luau.Analysis STATIC )
2024-07-20 01:21:40 +08:00
add_library ( Luau.EqSat STATIC )
2022-05-27 04:33:48 +08:00
add_library ( Luau.CodeGen STATIC )
2021-10-30 04:25:12 +08:00
add_library ( Luau.VM STATIC )
2022-02-04 07:09:37 +08:00
add_library ( isocline STATIC )
2021-10-30 04:25:12 +08:00
if ( LUAU_BUILD_CLI )
add_executable ( Luau.Repl.CLI )
2021-12-03 07:20:08 +08:00
add_executable ( Luau.Analyze.CLI )
2022-02-12 02:43:14 +08:00
add_executable ( Luau.Ast.CLI )
2022-08-12 04:42:54 +08:00
add_executable ( Luau.Reduce.CLI )
2023-06-09 20:20:36 +08:00
add_executable ( Luau.Compile.CLI )
2023-11-18 02:15:31 +08:00
add_executable ( Luau.Bytecode.CLI )
2021-10-30 04:25:12 +08:00
# This also adds target `name` on Linux/macOS and `name.exe` on Windows
set_target_properties ( Luau.Repl.CLI PROPERTIES OUTPUT_NAME luau )
2021-12-03 07:20:08 +08:00
set_target_properties ( Luau.Analyze.CLI PROPERTIES OUTPUT_NAME luau-analyze )
2022-02-12 02:43:14 +08:00
set_target_properties ( Luau.Ast.CLI PROPERTIES OUTPUT_NAME luau-ast )
2022-08-12 04:42:54 +08:00
set_target_properties ( Luau.Reduce.CLI PROPERTIES OUTPUT_NAME luau-reduce )
2023-06-09 20:20:36 +08:00
set_target_properties ( Luau.Compile.CLI PROPERTIES OUTPUT_NAME luau-compile )
2023-11-18 02:15:31 +08:00
set_target_properties ( Luau.Bytecode.CLI PROPERTIES OUTPUT_NAME luau-bytecode )
2021-10-30 04:25:12 +08:00
endif ( )
2021-12-03 07:20:08 +08:00
if ( LUAU_BUILD_TESTS )
2021-10-30 04:25:12 +08:00
add_executable ( Luau.UnitTest )
add_executable ( Luau.Conformance )
2022-01-28 05:29:34 +08:00
add_executable ( Luau.CLI.Test )
2021-10-30 04:25:12 +08:00
endif ( )
2021-11-12 10:12:39 +08:00
2021-12-03 07:20:08 +08:00
if ( LUAU_BUILD_WEB )
add_executable ( Luau.Web )
endif ( )
2022-09-30 06:11:54 +08:00
# Proxy target to make it possible to depend on private VM headers
add_library ( Luau.VM.Internals INTERFACE )
2022-08-12 04:42:54 +08:00
2021-10-30 04:25:12 +08:00
include ( Sources.cmake )
2022-05-27 04:33:48 +08:00
target_include_directories ( Luau.Common INTERFACE Common/include )
2024-03-31 06:49:03 +08:00
target_compile_features ( Luau.CLI.lib PUBLIC cxx_std_17 )
2024-03-23 01:21:27 +08:00
target_link_libraries ( Luau.CLI.lib PRIVATE Luau.Common )
2021-10-30 04:25:12 +08:00
target_compile_features ( Luau.Ast PUBLIC cxx_std_17 )
target_include_directories ( Luau.Ast PUBLIC Ast/include )
2024-03-23 01:21:27 +08:00
target_link_libraries ( Luau.Ast PUBLIC Luau.Common Luau.CLI.lib )
2021-10-30 04:25:12 +08:00
target_compile_features ( Luau.Compiler PUBLIC cxx_std_17 )
target_include_directories ( Luau.Compiler PUBLIC Compiler/include )
target_link_libraries ( Luau.Compiler PUBLIC Luau.Ast )
2023-08-19 01:06:29 +08:00
target_compile_features ( Luau.Config PUBLIC cxx_std_17 )
target_include_directories ( Luau.Config PUBLIC Config/include )
target_link_libraries ( Luau.Config PUBLIC Luau.Ast )
2021-10-30 04:25:12 +08:00
target_compile_features ( Luau.Analysis PUBLIC cxx_std_17 )
target_include_directories ( Luau.Analysis PUBLIC Analysis/include )
2024-07-20 01:21:40 +08:00
target_link_libraries ( Luau.Analysis PUBLIC Luau.Ast Luau.EqSat Luau.Config )
2024-09-28 01:49:00 +08:00
target_link_libraries ( Luau.Analysis PRIVATE Luau.Compiler Luau.VM )
2024-07-20 01:21:40 +08:00
target_compile_features ( Luau.EqSat PUBLIC cxx_std_17 )
target_include_directories ( Luau.EqSat PUBLIC EqSat/include )
target_link_libraries ( Luau.EqSat PUBLIC Luau.Common )
2021-10-30 04:25:12 +08:00
2022-05-27 04:33:48 +08:00
target_compile_features ( Luau.CodeGen PRIVATE cxx_std_17 )
target_include_directories ( Luau.CodeGen PUBLIC CodeGen/include )
2022-09-24 02:32:10 +08:00
target_link_libraries ( Luau.CodeGen PRIVATE Luau.VM Luau.VM.Internals ) # Code generation needs VM internals
2022-05-27 04:33:48 +08:00
target_link_libraries ( Luau.CodeGen PUBLIC Luau.Common )
2021-10-30 04:25:12 +08:00
target_compile_features ( Luau.VM PRIVATE cxx_std_11 )
target_include_directories ( Luau.VM PUBLIC VM/include )
2022-05-27 04:33:48 +08:00
target_link_libraries ( Luau.VM PUBLIC Luau.Common )
2021-10-30 04:25:12 +08:00
2022-02-04 07:09:37 +08:00
target_include_directories ( isocline PUBLIC extern/isocline/include )
2022-09-30 06:11:54 +08:00
target_include_directories ( Luau.VM.Internals INTERFACE VM/src )
2021-10-30 04:25:12 +08:00
set ( LUAU_OPTIONS )
if ( MSVC )
list ( APPEND LUAU_OPTIONS /D_CRT_SECURE_NO_WARNINGS ) # We need to use the portable CRT functions.
2023-03-03 21:45:38 +08:00
list ( APPEND LUAU_OPTIONS "/we4018" ) # Signed/unsigned mismatch
list ( APPEND LUAU_OPTIONS "/we4388" ) # Also signed/unsigned mismatch
2021-10-30 04:25:12 +08:00
else ( )
list ( APPEND LUAU_OPTIONS -Wall ) # All warnings
2024-09-14 01:14:29 +08:00
list ( APPEND LUAU_OPTIONS -Wimplicit-fallthrough )
2023-03-03 21:45:38 +08:00
list ( APPEND LUAU_OPTIONS -Wsign-compare ) # This looks to be included in -Wall for GCC but not clang
endif ( )
if ( CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" )
list ( APPEND LUAU_OPTIONS /MP ) # Distribute single project compilation across multiple cores
2021-11-19 06:21:07 +08:00
endif ( )
2022-04-22 05:04:22 +08:00
if ( CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
# Some gcc versions treat var in `if (type var = val)` as unused
# Some gcc versions treat variables used in constexpr if blocks as unused
list ( APPEND LUAU_OPTIONS -Wno-unused )
endif ( )
2021-11-19 06:21:07 +08:00
# Enabled in CI; we should be warning free on our main compiler versions but don't guarantee being warning free everywhere
if ( LUAU_WERROR )
if ( MSVC )
list ( APPEND LUAU_OPTIONS /WX ) # Warnings are errors
else ( )
list ( APPEND LUAU_OPTIONS -Werror ) # Warnings are errors
endif ( )
2021-10-30 04:25:12 +08:00
endif ( )
2021-12-03 07:20:08 +08:00
if ( LUAU_BUILD_WEB )
# add -fexceptions for emscripten to allow exceptions to be caught in C++
list ( APPEND LUAU_OPTIONS -fexceptions )
endif ( )
2022-02-04 07:09:37 +08:00
set ( ISOCLINE_OPTIONS )
2023-03-03 21:45:38 +08:00
if ( NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" )
2022-02-04 07:09:37 +08:00
list ( APPEND ISOCLINE_OPTIONS -Wno-unused-function )
endif ( )
2021-10-30 04:25:12 +08:00
target_compile_options ( Luau.Ast PRIVATE ${ LUAU_OPTIONS } )
target_compile_options ( Luau.Analysis PRIVATE ${ LUAU_OPTIONS } )
2024-07-20 01:21:40 +08:00
target_compile_options ( Luau.EqSat PRIVATE ${ LUAU_OPTIONS } )
2024-03-31 06:49:03 +08:00
target_compile_options ( Luau.CLI.lib PRIVATE ${ LUAU_OPTIONS } )
2022-05-27 04:33:48 +08:00
target_compile_options ( Luau.CodeGen PRIVATE ${ LUAU_OPTIONS } )
2021-10-30 04:25:12 +08:00
target_compile_options ( Luau.VM PRIVATE ${ LUAU_OPTIONS } )
2022-02-04 07:09:37 +08:00
target_compile_options ( isocline PRIVATE ${ LUAU_OPTIONS } ${ ISOCLINE_OPTIONS } )
2021-10-30 04:25:12 +08:00
2022-06-24 09:44:07 +08:00
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\" )
2023-08-25 23:25:09 +08:00
target_compile_definitions ( Luau.CodeGen PUBLIC LUACODEGEN_API=extern\ "C\" )
2022-10-14 06:59:53 +08:00
endif ( )
2023-03-03 21:45:38 +08:00
if ( CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND MSVC_VERSION GREATER_EQUAL 1924 )
2022-01-22 00:23:02 +08:00
# disable partial redundancy elimination which regresses interpreter codegen substantially in VS2022:
# https://developercommunity.visualstudio.com/t/performance-regression-on-a-complex-interpreter-lo/1631863
set_source_files_properties ( VM/src/lvmexecute.cpp PROPERTIES COMPILE_FLAGS /d2ssa-pre- )
endif ( )
2022-10-28 06:22:49 +08:00
if ( NOT MSVC )
# disable support for math_errno which allows compilers to lower sqrt() into a single CPU instruction
target_compile_options ( Luau.VM PRIVATE -fno-math-errno )
endif ( )
2022-05-14 03:16:50 +08:00
if ( MSVC AND LUAU_BUILD_CLI )
# the default stack size that MSVC linker uses is 1 MB; we need more stack space in Debug because stack frames are larger
set_target_properties ( Luau.Analyze.CLI PROPERTIES LINK_FLAGS_DEBUG /STACK:2097152 )
set_target_properties ( Luau.Repl.CLI PROPERTIES LINK_FLAGS_DEBUG /STACK:2097152 )
endif ( )
2022-02-18 08:41:20 +08:00
# embed .natvis inside the library debug information
if ( MSVC )
target_link_options ( Luau.Ast INTERFACE /NATVIS: ${ CMAKE_CURRENT_SOURCE_DIR } /tools/natvis/Ast.natvis )
target_link_options ( Luau.Analysis INTERFACE /NATVIS: ${ CMAKE_CURRENT_SOURCE_DIR } /tools/natvis/Analysis.natvis )
2022-05-27 04:33:48 +08:00
target_link_options ( Luau.CodeGen INTERFACE /NATVIS: ${ CMAKE_CURRENT_SOURCE_DIR } /tools/natvis/CodeGen.natvis )
2022-02-18 08:41:20 +08:00
target_link_options ( Luau.VM INTERFACE /NATVIS: ${ CMAKE_CURRENT_SOURCE_DIR } /tools/natvis/VM.natvis )
endif ( )
# make .natvis visible inside the solution
if ( MSVC_IDE )
target_sources ( Luau.Ast PRIVATE tools/natvis/Ast.natvis )
target_sources ( Luau.Analysis PRIVATE tools/natvis/Analysis.natvis )
2022-05-27 04:33:48 +08:00
target_sources ( Luau.CodeGen PRIVATE tools/natvis/CodeGen.natvis )
2022-02-18 08:41:20 +08:00
target_sources ( Luau.VM PRIVATE tools/natvis/VM.natvis )
endif ( )
2024-04-06 01:41:05 +08:00
# On Windows and Android threads are provided, on Linux/Mac/iOS we use pthreads
add_library ( osthreads INTERFACE )
if ( CMAKE_SYSTEM_NAME MATCHES "Linux|Darwin|iOS" )
target_link_libraries ( osthreads INTERFACE "-lpthread" )
endif ( )
2021-10-30 04:25:12 +08:00
if ( LUAU_BUILD_CLI )
target_compile_options ( Luau.Repl.CLI PRIVATE ${ LUAU_OPTIONS } )
2023-02-11 02:50:54 +08:00
target_compile_options ( Luau.Reduce.CLI PRIVATE ${ LUAU_OPTIONS } )
2021-12-03 07:20:08 +08:00
target_compile_options ( Luau.Analyze.CLI PRIVATE ${ LUAU_OPTIONS } )
2022-02-12 02:43:14 +08:00
target_compile_options ( Luau.Ast.CLI PRIVATE ${ LUAU_OPTIONS } )
2023-06-09 20:20:36 +08:00
target_compile_options ( Luau.Compile.CLI PRIVATE ${ LUAU_OPTIONS } )
2023-11-18 02:15:31 +08:00
target_compile_options ( Luau.Bytecode.CLI PRIVATE ${ LUAU_OPTIONS } )
2021-10-30 04:25:12 +08:00
2022-02-04 07:09:37 +08:00
target_include_directories ( Luau.Repl.CLI PRIVATE extern extern/isocline/include )
2024-03-23 01:21:27 +08:00
target_link_libraries ( Luau.Repl.CLI PRIVATE Luau.Compiler Luau.Config Luau.CodeGen Luau.VM Luau.CLI.lib isocline )
2021-10-30 04:25:12 +08:00
2024-04-06 01:41:05 +08:00
target_link_libraries ( Luau.Repl.CLI PRIVATE osthreads )
target_link_libraries ( Luau.Analyze.CLI PRIVATE osthreads )
2021-10-30 04:25:12 +08:00
2024-03-23 01:21:27 +08:00
target_link_libraries ( Luau.Analyze.CLI PRIVATE Luau.Analysis Luau.CLI.lib )
2022-02-12 02:43:14 +08:00
2024-03-23 01:21:27 +08:00
target_link_libraries ( Luau.Ast.CLI PRIVATE Luau.Ast Luau.Analysis Luau.CLI.lib )
2022-08-12 04:42:54 +08:00
target_compile_features ( Luau.Reduce.CLI PRIVATE cxx_std_17 )
target_include_directories ( Luau.Reduce.CLI PUBLIC Reduce/include )
2024-03-23 01:21:27 +08:00
target_link_libraries ( Luau.Reduce.CLI PRIVATE Luau.Common Luau.Ast Luau.Analysis Luau.CLI.lib )
2023-06-09 20:20:36 +08:00
2024-03-23 01:21:27 +08:00
target_link_libraries ( Luau.Compile.CLI PRIVATE Luau.Compiler Luau.VM Luau.CodeGen Luau.CLI.lib )
2023-11-18 02:15:31 +08:00
2024-03-23 01:21:27 +08:00
target_link_libraries ( Luau.Bytecode.CLI PRIVATE Luau.Compiler Luau.VM Luau.CodeGen Luau.CLI.lib )
2021-10-30 04:25:12 +08:00
endif ( )
2021-12-03 07:20:08 +08:00
if ( LUAU_BUILD_TESTS )
2021-10-30 04:25:12 +08:00
target_compile_options ( Luau.UnitTest PRIVATE ${ LUAU_OPTIONS } )
2022-07-15 06:39:35 +08:00
target_compile_definitions ( Luau.UnitTest PRIVATE DOCTEST_CONFIG_DOUBLE_STRINGIFY )
2021-10-30 04:25:12 +08:00
target_include_directories ( Luau.UnitTest PRIVATE extern )
2022-05-27 04:33:48 +08:00
target_link_libraries ( Luau.UnitTest PRIVATE Luau.Analysis Luau.Compiler Luau.CodeGen )
2021-10-30 04:25:12 +08:00
target_compile_options ( Luau.Conformance PRIVATE ${ LUAU_OPTIONS } )
2024-05-04 00:38:34 +08:00
target_compile_definitions ( Luau.Conformance PRIVATE DOCTEST_CONFIG_DOUBLE_STRINGIFY )
2021-10-30 04:25:12 +08:00
target_include_directories ( Luau.Conformance PRIVATE extern )
2022-10-14 06:59:53 +08:00
target_link_libraries ( Luau.Conformance PRIVATE Luau.Analysis Luau.Compiler Luau.CodeGen Luau.VM )
2024-04-06 01:41:05 +08:00
if ( CMAKE_SYSTEM_NAME MATCHES "Android|iOS" )
set ( LUAU_CONFORMANCE_SOURCE_DIR "Client/Luau/tests/conformance" )
else ( )
file ( REAL_PATH "tests/conformance" LUAU_CONFORMANCE_SOURCE_DIR )
endif ( )
2023-09-23 02:10:49 +08:00
target_compile_definitions ( Luau.Conformance PRIVATE LUAU_CONFORMANCE_SOURCE_DIR= "${LUAU_CONFORMANCE_SOURCE_DIR}" )
2022-01-28 05:29:34 +08:00
target_compile_options ( Luau.CLI.Test PRIVATE ${ LUAU_OPTIONS } )
target_include_directories ( Luau.CLI.Test PRIVATE extern CLI )
2024-03-23 01:21:27 +08:00
target_link_libraries ( Luau.CLI.Test PRIVATE Luau.Compiler Luau.Config Luau.CodeGen Luau.VM Luau.CLI.lib isocline )
2024-04-06 01:41:05 +08:00
target_link_libraries ( Luau.CLI.Test PRIVATE osthreads )
2022-01-28 05:29:34 +08:00
2021-10-30 04:25:12 +08:00
endif ( )
2021-12-03 07:20:08 +08:00
if ( LUAU_BUILD_WEB )
target_compile_options ( Luau.Web PRIVATE ${ LUAU_OPTIONS } )
target_link_libraries ( Luau.Web PRIVATE Luau.Compiler Luau.VM )
# declare exported functions to emscripten
target_link_options ( Luau.Web PRIVATE -sEXPORTED_FUNCTIONS=['_executeScript'] -sEXPORTED_RUNTIME_METHODS=['ccall','cwrap'] )
# add -fexceptions for emscripten to allow exceptions to be caught in C++
target_link_options ( Luau.Web PRIVATE -fexceptions )
# the output is a single .js file with an embedded wasm blob
target_link_options ( Luau.Web PRIVATE -sSINGLE_FILE=1 )
endif ( )
2023-10-28 03:33:36 +08:00
2023-12-02 10:04:44 +08:00
add_subdirectory ( fuzz )
2023-10-28 03:33:36 +08:00
# validate dependencies for internal libraries
2024-07-20 01:21:40 +08:00
foreach ( LIB Luau.Ast Luau.Compiler Luau.Config Luau.Analysis Luau.EqSat Luau.CodeGen Luau.VM )
2023-10-28 03:33:36 +08:00
if ( TARGET ${ LIB } )
get_target_property ( DEPENDS ${ LIB } LINK_LIBRARIES )
if ( LIB MATCHES "CodeGen|VM" AND DEPENDS MATCHES "Ast|Analysis|Config|Compiler" )
message ( FATAL_ERROR ${ LIB } " is a runtime component but it depends on one of the offline components" )
endif ( )
2024-09-28 01:49:00 +08:00
if ( LIB MATCHES "Ast|EqSat|Compiler" AND DEPENDS MATCHES "CodeGen|VM" )
2023-10-28 03:33:36 +08:00
message ( FATAL_ERROR ${ LIB } " is an offline component but it depends on one of the runtime components" )
endif ( )
if ( LIB MATCHES "Ast|Compiler" AND DEPENDS MATCHES "Analysis|Config" )
message ( FATAL_ERROR ${ LIB } " is a compiler component but it depends on one of the analysis components" )
endif ( )
endif ( )
endforeach ( )