| cmake_minimum_required(VERSION 3.21) | |
| if(NOT CMAKE_GENERATOR MATCHES "Unix Makefiles") | |
| message(FATAL_ERROR "The only supported CMake generator at the moment is 'Unix Makefiles'") | |
| endif() | |
| if(NOT DEFINED STAGE) | |
| message(FATAL_ERROR "STAGE must be set; use the CMakeLists.txt in the root folder") | |
| endif() | |
| include(ExternalProject) | |
| project(LEAN CXX C) | |
| set(LEAN_VERSION_MAJOR 4) | |
| set(LEAN_VERSION_MINOR 37) | |
| set(LEAN_VERSION_PATCH 0) | |
| set(LEAN_VERSION_IS_RELEASE 0 CACHE STRING "") # This number is 1 in the release revision, and 0 otherwise. | |
| set(LEAN_SPECIAL_VERSION_DESC "" CACHE STRING "Additional version description like 'nightly-2018-03-11'") | |
| set(LEAN_VERSION_STRING "${LEAN_VERSION_MAJOR}.${LEAN_VERSION_MINOR}.${LEAN_VERSION_PATCH}") | |
| if(LEAN_SPECIAL_VERSION_DESC) | |
| string(APPEND LEAN_VERSION_STRING "-${LEAN_SPECIAL_VERSION_DESC}") | |
| elseif(NOT LEAN_VERSION_IS_RELEASE) | |
| string(APPEND LEAN_VERSION_STRING "-pre") | |
| endif() | |
| if(LEAN_VERSION_IS_RELEASE) | |
| set(LEAN_MANUAL_ROOT "https://lean-lang.org/doc/reference/${LEAN_VERSION_STRING}/") | |
| else() | |
| set(LEAN_MANUAL_ROOT "") | |
| endif() | |
| set(LEAN_PLATFORM_TARGET "" CACHE STRING "LLVM triple of the target platform") | |
| if(NOT LEAN_PLATFORM_TARGET) | |
| # this may fail when the compiler is not clang, but this should only happen in local builds where | |
| # the value of the variable is not of immediate relevance | |
| execute_process( | |
| COMMAND ${CMAKE_C_COMPILER} --print-target-triple | |
| OUTPUT_VARIABLE LEAN_PLATFORM_TARGET | |
| OUTPUT_STRIP_TRAILING_WHITESPACE | |
| ) | |
| endif() | |
| set(LEAN_EXTRA_LINKER_FLAGS_DEFAULT "") | |
| # Use lld by default, if available | |
| find_program(LLD_PATH lld) | |
| if(LLD_PATH) | |
| string(APPEND LEAN_EXTRA_LINKER_FLAGS_DEFAULT " -fuse-ld=lld") | |
| endif() | |
| if(CMAKE_SYSTEM_NAME MATCHES "Darwin") | |
| # Create space in install names so they can be patched later in Nix. | |
| string(APPEND LEAN_EXTRA_LINKER_FLAGS_DEFAULT " -headerpad_max_install_names") | |
| endif() | |
| set(LEAN_EXTRA_LINKER_FLAGS ${LEAN_EXTRA_LINKER_FLAGS_DEFAULT} CACHE STRING "Additional flags used by the linker") | |
| set( | |
| LEAN_EXTRA_CXX_FLAGS | |
| "" | |
| CACHE STRING | |
| "Additional flags used by the C++ compiler. Unlike `CMAKE_CXX_FLAGS`, these will not be used to build e.g. cadical." | |
| ) | |
| set( | |
| LEAN_TEST_VARS | |
| "LEAN_CC=${CMAKE_C_COMPILER}" | |
| CACHE STRING | |
| "Additional environment variables used when running tests" | |
| ) | |
| if(NOT CMAKE_BUILD_TYPE) | |
| message(STATUS "No build type selected, default to Release") | |
| set(CMAKE_BUILD_TYPE "Release") | |
| endif() | |
| set(CMAKE_INSTALL_MESSAGE NEVER) # Make `make install` less verbose | |
| set(CMAKE_COLOR_MAKEFILE ON) | |
| enable_testing() | |
| option(MULTI_THREAD "MULTI_THREAD" ON) | |
| option(CCACHE "use ccache" ON) | |
| option(SPLIT_STACK "SPLIT_STACK" OFF) | |
| # When OFF we disable LLVM support | |
| option(LLVM "LLVM" OFF) | |
| option(STRIP_BINARIES "Strip produced binaries" ON) | |
| # When ON we include githash in the version string | |
| option(USE_GITHASH "GIT_HASH" ON) | |
| # When ON we install LICENSE files to CMAKE_INSTALL_PREFIX | |
| option(INSTALL_LICENSE "INSTALL_LICENSE" ON) | |
| # When ON we install a copy of cadical | |
| option(INSTALL_CADICAL "Install a copy of cadical" ON) | |
| # When ON we install a copy of leantar | |
| option(INSTALL_LEANTAR "Install a copy of leantar" ON) | |
| # FLAGS for disabling optimizations and debugging | |
| option(FREE_VAR_RANGE_OPT "FREE_VAR_RANGE_OPT" ON) | |
| option(HAS_LOCAL_OPT "HAS_LOCAL_OPT" ON) | |
| option(ABSTRACTION_CACHE "ABSTRACTION_CACHE" ON) | |
| option(TYPE_CLASS_CACHE "TYPE_CLASS_CACHE" ON) | |
| option(TYPE_INFER_CACHE "TYPE_INFER_CACHE" ON) | |
| option(ALPHA "ALPHA FEATURES" OFF) | |
| option(TRACK_CUSTOM_ALLOCATORS "TRACK_CUSTOM_ALLOCATORS" OFF) | |
| option(TRACK_LIVE_EXPRS "TRACK_LIVE_EXPRS" OFF) | |
| option(CUSTOM_ALLOCATORS "CUSTOM_ALLOCATORS" ON) | |
| option(SAVE_SNAPSHOT "SAVE_SNAPSHOT" ON) | |
| option(SAVE_INFO "SAVE_INFO" ON) | |
| option(MMAP "MMAP" ON) | |
| option(RUNTIME_STATS "RUNTIME_STATS" OFF) | |
| option(BSYMBOLIC "Link with -Bsymbolic to reduce call overhead in shared libraries (Linux)" ON) | |
| option(USE_GMP "USE_GMP" ON) | |
| # GMP versions before 6.3.0 contain bugs that can make Lean produce unsound results. | |
| # When ON, build against whatever GMP is available even if it is older than 6.3.0. | |
| option(FORCE_GMP "Build against the available GMP even if older than the required 6.3.0" OFF) | |
| option(USE_MIMALLOC "use mimalloc" ON) | |
| set(LEAN_MI_SECURE 0 CACHE STRING "Configure mimalloc memory safety mitigations (https://github.com/microsoft/mimalloc/blob/v2.2.7/include/mimalloc/types.h#L56-L60)") | |
| # development-specific options | |
| option(CHECK_OLEAN_VERSION "Only load .olean files compiled with the current version of Lean" OFF) | |
| option(USE_LAKE "Use Lake instead of lean.mk for building core libs from language server" ON) | |
| option(USE_LAKE_CACHE "Use the Lake artifact cache for stage 1 builds (requires USE_LAKE)" OFF) | |
| set(LEAN_EXTRA_OPTS "" CACHE STRING "extra options to lean (via lake or make)") | |
| set(LEAN_EXTRA_LAKE_OPTS "" CACHE STRING "extra options to lake") | |
| set(LEAN_EXTRA_MAKE_OPTS "" CACHE STRING "extra options to leanmake") | |
| set(LEANC_CC ${CMAKE_C_COMPILER} CACHE STRING "C compiler to use in `leanc`") | |
| # Core-only flags. | |
| # We want to ship `prefer_native=true` but disable it in all stage builds for consistent behavior. | |
| # Note that currently this could lead to inconsistent behavior in server mode with `USE_LAKE=OFF` | |
| # but that option is not used anymore and should likely be deleted. | |
| string(APPEND LEAN_EXTRA_OPTS " -Dinterpreter.prefer_native=false") | |
| # More useful for devs than users | |
| string(APPEND LEAN_EXTRA_OPTS " -Dpp.rawOnError=true") | |
| # option used by the CI to fail on warnings | |
| option(WFAIL "Fail build if warnings are emitted by Lean" ON) | |
| if(WFAIL MATCHES "ON") | |
| string(APPEND LEAN_EXTRA_LAKE_OPTS " --wfail") | |
| string(APPEND LEAN_EXTRA_MAKE_OPTS " -DwarningAsError=true") | |
| endif() | |
| if(USE_MIMALLOC) | |
| set(LEAN_MIMALLOC "#define LEAN_MIMALLOC") | |
| endif() | |
| if(CMAKE_SIZEOF_VOID_P EQUAL 8) | |
| message(STATUS "64-bit machine detected") | |
| set(NumBits 64) | |
| else() | |
| message(STATUS "32-bit machine detected") | |
| set(NumBits 32) | |
| endif() | |
| if(MMAP MATCHES "ON") | |
| string(APPEND LEAN_EXTRA_CXX_FLAGS " -D LEAN_MMAP") | |
| endif() | |
| if(RUNTIME_STATS MATCHES "ON") | |
| string(APPEND LEAN_EXTRA_CXX_FLAGS " -D LEAN_RUNTIME_STATS") | |
| endif() | |
| if(CHECK_OLEAN_VERSION MATCHES "ON") | |
| set(USE_GITHASH ON) | |
| string(APPEND LEAN_EXTRA_CXX_FLAGS " -D LEAN_CHECK_OLEAN_VERSION") | |
| endif() | |
| if(CMAKE_SYSTEM_NAME MATCHES "Emscripten") | |
| # TODO(WN): code size/performance tradeoffs | |
| # - we're using -O3; it's /okay/ | |
| # - -Oz produces quite slow code | |
| # - system libraries such as OpenGL are included in the JS but shouldn't be | |
| # - we need EMSCRIPTEN_KEEPALIVE annotations on exports to run meta-dce (-s MAIN_MODULE=2) | |
| # From https://emscripten.org/docs/compiling/WebAssembly.html#backends: | |
| # > The simple and safe thing is to pass all -s flags at both compile and link time. | |
| set(EMSCRIPTEN_SETTINGS "-s ALLOW_MEMORY_GROWTH=1 -fwasm-exceptions -pthread -flto") | |
| string(APPEND LEANC_EXTRA_CC_FLAGS " -pthread") | |
| string(APPEND LEAN_EXTRA_CXX_FLAGS " -D LEAN_EMSCRIPTEN ${EMSCRIPTEN_SETTINGS}") | |
| string(APPEND LEAN_EXTRA_LINKER_FLAGS " ${EMSCRIPTEN_SETTINGS}") | |
| endif() | |
| # Added for CTest | |
| include(CTest) | |
| # Windows does not support ulimit -s unlimited. So, we reserve a lot of stack space: 100Mb | |
| if(CMAKE_SYSTEM_NAME MATCHES "Windows") | |
| message(STATUS "Windows detected") | |
| set(LEAN_WIN_STACK_SIZE "104857600") | |
| if(MSVC) | |
| string(APPEND LEAN_EXTRA_LINKER_FLAGS " /STACK:${LEAN_WIN_STACK_SIZE}") | |
| else() | |
| string(APPEND LEAN_EXTRA_LINKER_FLAGS " -Wl,--stack,${LEAN_WIN_STACK_SIZE}") | |
| set(EXTRA_UTIL_LIBS ${EXTRA_UTIL_LIBS} -lpsapi) | |
| endif() | |
| string(APPEND LEAN_EXTRA_CXX_FLAGS " -D LEAN_WINDOWS -D LEAN_WIN_STACK_SIZE=${LEAN_WIN_STACK_SIZE}") | |
| # do not import the world from windows.h using appropriately named flag | |
| string(APPEND LEAN_EXTRA_CXX_FLAGS " -D WIN32_LEAN_AND_MEAN") | |
| # DLLs must go next to executables on Windows | |
| set(CMAKE_RELATIVE_LIBRARY_OUTPUT_DIRECTORY "bin") | |
| else() | |
| set(CMAKE_RELATIVE_LIBRARY_OUTPUT_DIRECTORY "lib/lean") | |
| endif() | |
| set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_RELATIVE_LIBRARY_OUTPUT_DIRECTORY}") | |
| set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/lean") | |
| # OSX default thread stack size is very small. Moreover, in Debug mode, each new stack frame consumes a lot of extra memory. | |
| if((MULTI_THREAD MATCHES "ON") AND (CMAKE_SYSTEM_NAME MATCHES "Darwin")) | |
| string(APPEND LEAN_EXTRA_OPTS " -s40000") | |
| endif() | |
| # We want explicit stack probes in huge Lean stack frames for robust stack overflow detection | |
| string(APPEND LEANC_EXTRA_CC_FLAGS " -fstack-clash-protection") | |
| # Disable contraction of floating-point operations to ensure predictable semantics | |
| string(APPEND LEANC_EXTRA_CC_FLAGS " -ffp-contract=off") | |
| string(APPEND LEAN_EXTRA_CXX_FLAGS " -ffp-contract=off") | |
| # On Windows we want to disable writing COFF object file time stamps. | |
| # LLVM 23 seems to have a regression in this regard. | |
| if(CMAKE_SYSTEM_NAME MATCHES "Windows") | |
| string(APPEND LEANC_EXTRA_CC_FLAGS " -mno-incremental-linker-compatible") | |
| endif() | |
| if(NOT MULTI_THREAD) | |
| message(STATUS "Disabled multi-thread support, it will not be safe to run multiple threads in parallel") | |
| set(AUTO_THREAD_FINALIZATION OFF) | |
| else() | |
| string(APPEND LEAN_EXTRA_CXX_FLAGS " -D LEAN_MULTI_THREAD") | |
| endif() | |
| # Set Module Path | |
| set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules") | |
| # Initialize CXXFLAGS. | |
| set( | |
| CMAKE_CXX_FLAGS | |
| "${CMAKE_CXX_FLAGS} ${LEAN_EXTRA_CXX_FLAGS} -DLEAN_BUILD_TYPE=\"${CMAKE_BUILD_TYPE}\" -DLEAN_EXPORTING" | |
| ) | |
| set(CMAKE_CXX_FLAGS_DEBUG "-DLEAN_DEBUG") | |
| set(CMAKE_CXX_FLAGS_MINSIZEREL "-DNDEBUG") | |
| set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG") | |
| set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-DNDEBUG") | |
| set(CMAKE_CXX_FLAGS_RELWITHASSERT "-DLEAN_DEBUG") | |
| # SPLIT_STACK | |
| if(SPLIT_STACK) | |
| if((CMAKE_SYSTEM_NAME MATCHES "Linux") AND (CMAKE_CXX_COMPILER_ID MATCHES "GNU")) | |
| string(APPEND CMAKE_CXX_FLAGS " -fsplit-stack -D LEAN_USE_SPLIT_STACK") | |
| message(STATUS "Using split-stacks") | |
| else() | |
| message(FATAL_ERROR "Split-stacks are only supported on Linux with g++") | |
| endif() | |
| endif() | |
| # Compiler-specific C++14 activation. | |
| if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") | |
| execute_process(COMMAND "${CMAKE_CXX_COMPILER}" -dumpversion OUTPUT_VARIABLE GCC_VERSION) | |
| if(NOT (GCC_VERSION VERSION_GREATER 4.9 OR GCC_VERSION VERSION_EQUAL 4.9)) | |
| message(FATAL_ERROR "${PROJECT_NAME} requires g++ 4.9 or greater.") | |
| endif() | |
| elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang") | |
| string(APPEND CMAKE_CXX_FLAGS " -D__CLANG__") | |
| elseif(MSVC) | |
| # All good. Maybe enforce a recent version? | |
| set(CMAKE_CXX_FLAGS "/GL /EHsc /W2 /Zc:implicitNoexcept- -D_SCL_SECURE_NO_WARNINGS ${CMAKE_CXX_FLAGS}") | |
| set(CMAKE_CXX_FLAGS_DEBUG "/Od /Zi ${CMAKE_CXX_FLAGS_DEBUG}") | |
| set(CMAKE_CXX_FLAGS_MINSIZEREL "/Os /Zc:inline ${CMAKE_CXX_FLAGS_MINSIZEREL}") | |
| set(CMAKE_CXX_FLAGS_RELEASE "/O2 /Oi /Oy /Zc:inline ${CMAKE_CXX_FLAGS_RELEASE}") | |
| set(CMAKE_CXX_FLAGS_RELWITHASSERT "/O2 /Oi /Oy /Zc:inline ${CMAKE_CXX_FLAGS_RELWITHASSERT}") | |
| set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/O2 /Oi /Zi ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") | |
| set(LEAN_EXTRA_LINKER_FLAGS "/LTCG:INCREMENTAL ${LEAN_EXTRA_LINKER_FLAGS}") | |
| set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} ${LEAN_EXTRA_LINKER_FLAGS}") | |
| elseif(CMAKE_SYSTEM_NAME MATCHES "Emscripten") | |
| message(STATUS "Emscripten is detected: Make sure the wrapped compiler supports C++14") | |
| else() | |
| message(FATAL_ERROR "Unsupported compiler: ${CMAKE_CXX_COMPILER_ID}") | |
| endif() | |
| if(NOT MSVC) | |
| set(CMAKE_CXX_FLAGS "-Wall -Wextra -std=c++20 ${CMAKE_CXX_FLAGS}") | |
| set(CMAKE_CXX_FLAGS_DEBUG "-g3 ${CMAKE_CXX_FLAGS_DEBUG}") | |
| if(CMAKE_SYSTEM_NAME MATCHES "Emscripten") | |
| # smallest+slower | -Oz .. -Os .. -O3 | largest+faster | |
| set(CMAKE_CXX_FLAGS_MINSIZEREL "-Oz ${CMAKE_CXX_FLAGS_MINSIZEREL}") | |
| else() | |
| set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os ${CMAKE_CXX_FLAGS_MINSIZEREL}") | |
| endif() | |
| set(CMAKE_CXX_FLAGS_RELEASE "-O3 ${CMAKE_CXX_FLAGS_RELEASE}") | |
| set(CMAKE_CXX_FLAGS_RELWITHASSERT "-O3 ${CMAKE_CXX_FLAGS_RELWITHASSERT}") | |
| set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g3 -fno-omit-frame-pointer ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") | |
| elseif(MULTI_THREAD) | |
| set(CMAKE_CXX_FLAGS_DEBUG "/MTd ${CMAKE_CXX_FLAGS_DEBUG}") | |
| set(CMAKE_CXX_FLAGS_MINSIZEREL "/MT ${CMAKE_CXX_FLAGS_MINSIZEREL}") | |
| set(CMAKE_CXX_FLAGS_RELEASE "/MT ${CMAKE_CXX_FLAGS_RELEASE}") | |
| set(CMAKE_CXX_FLAGS_RELWITHASSERT "/MT ${CMAKE_CXX_FLAGS_RELWITHASSERT}") | |
| set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/MT ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") | |
| endif() | |
| if(USE_GMP MATCHES "ON") | |
| set(CMAKE_CXX_FLAGS "-D LEAN_USE_GMP ${CMAKE_CXX_FLAGS}") | |
| if(CMAKE_SYSTEM_NAME MATCHES "Emscripten") | |
| include_directories(${GMP_INSTALL_PREFIX}/include) | |
| set(GMP_LIBRARIES "${GMP_INSTALL_PREFIX}/lib/libgmp.a") | |
| else() | |
| # GMP. Versions before 6.3.0 contain bugs that can make Lean produce unsound | |
| # results, so require 6.3.0 unless the user opts out via FORCE_GMP. | |
| if(FORCE_GMP) | |
| find_package(GMP REQUIRED) | |
| if(NOT GMP_VERSION OR GMP_VERSION VERSION_LESS 6.3.0) | |
| if(GMP_VERSION) | |
| set(_gmp_forced_version "GMP ${GMP_VERSION}") | |
| else() | |
| set(_gmp_forced_version "a GMP whose version could not be determined") | |
| endif() | |
| message(WARNING | |
| "FORCE_GMP is set: building against ${_gmp_forced_version}, which does not meet the " | |
| "required GMP 6.3.0. Versions before 6.3.0 contain bugs that can cause Lean to produce " | |
| "UNSOUND (i.e., INCORRECT) results in corner cases. Independent kernels that do not depend " | |
| "on GMP will catch the unsoundness. Build only if you understand the risk.") | |
| endif() | |
| else() | |
| find_package(GMP 6.3.0) | |
| # An undetermined version is conservatively treated as too old; | |
| # find_package would otherwise accept it since VERSION_VAR is empty. | |
| if(NOT GMP_FOUND OR NOT GMP_VERSION) | |
| if(NOT GMP_INCLUDE_DIR) | |
| set(_gmp_detail "no GMP was found") | |
| elseif(GMP_VERSION) | |
| set(_gmp_detail "found GMP ${GMP_VERSION}") | |
| elseif(NOT PKG_CONFIG_FOUND) | |
| set(_gmp_detail "found a GMP, but no pkg-config to read its version with") | |
| else() | |
| set(_gmp_detail "found a GMP, but no `gmp.pc` reporting its version") | |
| endif() | |
| message(FATAL_ERROR | |
| "Lean requires GMP 6.3.0 or newer (${_gmp_detail}). " | |
| "GMP versions before 6.3.0 contain bugs that can cause Lean to produce UNSOUND " | |
| "(i.e., INCORRECT) results in corner cases. Independent kernels that do not depend on GMP " | |
| "will catch the unsoundness.\n" | |
| "Choose one of:\n" | |
| " * Install GMP >= 6.3.0, then reconfigure; or\n" | |
| " * -DUSE_GMP=OFF use Lean's built-in bignum implementation (safest; some performance cost); or\n" | |
| " * -DFORCE_GMP=ON build against the GMP that is present anyway (NOT recommended: Lean may produce unsound results).") | |
| endif() | |
| endif() | |
| include_directories(${GMP_INCLUDE_DIR}) | |
| endif() | |
| if(NOT LEAN_STANDALONE) | |
| string(APPEND LEAN_EXTRA_LINKER_FLAGS " ${GMP_LIBRARIES}") | |
| endif() | |
| endif() | |
| # LibUV | |
| if(CMAKE_SYSTEM_NAME MATCHES "Emscripten") | |
| # Only on WebAssembly we compile LibUV ourselves | |
| set(LIBUV_EMSCRIPTEN_FLAGS "${EMSCRIPTEN_SETTINGS}") | |
| # LibUV does not compile on WebAssembly without modifications because | |
| # building LibUV on a platform requires including stub implementations | |
| # for features not present on the target platform. This patch includes | |
| # the minimum amount of stub implementations needed for successfully | |
| # running Lean on WebAssembly and using LibUV's temporary file support. | |
| # It still leaves several symbols completely undefined: uv__fs_event_close, | |
| # uv__hrtime, uv__io_check_fd, uv__io_fork, uv__io_poll, uv__platform_invalidate_fd | |
| # uv__platform_loop_delete, uv__platform_loop_init. Making additional | |
| # LibUV features available on WebAssembly might require adapting the | |
| # patch to include additional LibUV source files. | |
| set( | |
| LIBUV_PATCH_IN | |
| " | |
| diff --git a/CMakeLists.txt b/CMakeLists.txt | |
| index 5e8e0166..f3b29134 100644 | |
| --- a/CMakeLists.txt | |
| +++ b/CMakeLists.txt | |
| @@ -317,6 +317,11 @@ if(CMAKE_SYSTEM_NAME STREQUAL \"GNU\") | |
| src/unix/hurd.c) | |
| endif() | |
| +if(CMAKE_SYSTEM_NAME STREQUAL \"Emscripten\") | |
| + list(APPEND uv_sources | |
| + src/unix/no-proctitle.c) | |
| +endif() | |
| + | |
| if(CMAKE_SYSTEM_NAME STREQUAL \"Linux\") | |
| list(APPEND uv_defines _GNU_SOURCE _POSIX_C_SOURCE=200112) | |
| list(APPEND uv_libraries dl rt) | |
| " | |
| ) | |
| string(REPLACE "\n" "\\n" LIBUV_PATCH ${LIBUV_PATCH_IN}) | |
| ExternalProject_Add( | |
| libuv | |
| PREFIX libuv | |
| GIT_REPOSITORY https://github.com/libuv/libuv | |
| # Sync version with flake.nix | |
| GIT_TAG v1.53.0 | |
| CMAKE_ARGS | |
| -DCMAKE_BUILD_TYPE=Release -DLIBUV_BUILD_TESTS=OFF -DLIBUV_BUILD_SHARED=OFF -DCMAKE_AR=${CMAKE_AR} | |
| -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} -DCMAKE_POSITION_INDEPENDENT_CODE=ON | |
| -DCMAKE_C_FLAGS=${LIBUV_EMSCRIPTEN_FLAGS} | |
| PATCH_COMMAND git reset --hard HEAD && printf "${LIBUV_PATCH}" > patch.diff && git apply patch.diff | |
| BUILD_IN_SOURCE ON | |
| INSTALL_COMMAND "" | |
| ) | |
| set(LIBUV_INCLUDE_DIRS "${CMAKE_BINARY_DIR}/libuv/src/libuv/include") | |
| set(LIBUV_LDFLAGS "${CMAKE_BINARY_DIR}/libuv/src/libuv/libuv.a") | |
| else() | |
| find_package(LibUV 1.0.0 REQUIRED) | |
| endif() | |
| include_directories(${LIBUV_INCLUDE_DIRS}) | |
| if(NOT LEAN_STANDALONE) | |
| string(JOIN " " LIBUV_LDFLAGS ${LIBUV_LDFLAGS}) | |
| string(APPEND LEAN_EXTRA_LINKER_FLAGS " ${LIBUV_LDFLAGS}") | |
| endif() | |
| # OpenSSL | |
| if(NOT "${CMAKE_SYSTEM_NAME}" MATCHES "Emscripten") | |
| find_package(OpenSSL 3 REQUIRED COMPONENTS Crypto SSL) | |
| set(OPENSSL_LIBRARIES ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY}) | |
| include_directories(${OPENSSL_INCLUDE_DIR}) | |
| string(JOIN " " OPENSSL_LIBRARIES_STR ${OPENSSL_LIBRARIES}) | |
| if(NOT LEAN_STANDALONE) | |
| string(APPEND LEAN_EXTRA_LINKER_FLAGS " ${OPENSSL_LIBRARIES_STR}") | |
| endif() | |
| endif() | |
| # Windows SDK (for ICU) | |
| if(CMAKE_SYSTEM_NAME MATCHES "Windows") | |
| # Pass 'tools' to skip MSVC version check (as MSVC/Visual Studio is not necessarily installed) | |
| find_package(WindowsSDK REQUIRED COMPONENTS tools) | |
| # This will give a semicolon-separated list of include directories | |
| get_windowssdk_include_dirs(${WINDOWSSDK_LATEST_DIR} WINDOWSSDK_INCLUDE_DIRS) | |
| # To successfully build against Windows SDK headers, the Windows SDK headers must have lower | |
| # priority than other system headers, so use `-idirafter`. Unfortunately, CMake does not | |
| # support this using `include_directories`. | |
| string(REPLACE ";" "\" -idirafter \"" WINDOWSSDK_INCLUDE_DIRS "${WINDOWSSDK_INCLUDE_DIRS}") | |
| string(APPEND CMAKE_CXX_FLAGS " -idirafter \"${WINDOWSSDK_INCLUDE_DIRS}\"") | |
| string(APPEND LEAN_EXTRA_LINKER_FLAGS " -licu") | |
| endif() | |
| # ccache | |
| if(CCACHE AND NOT CMAKE_CXX_COMPILER_LAUNCHER AND NOT CMAKE_C_COMPILER_LAUNCHER) | |
| find_program(CCACHE_PATH ccache) | |
| if(CCACHE_PATH) | |
| set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PATH}") | |
| set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PATH}") | |
| else() | |
| message(WARNING "Failed to find ccache, prepare for longer and redundant builds...") | |
| endif() | |
| endif() | |
| include_directories(${CMAKE_BINARY_DIR}/include) | |
| # Pick up `llvm-config` to setup LLVM flags. | |
| if(LLVM) | |
| if(NOT LLVM_CONFIG) # look for `llvm-config` if not supplied | |
| message(STATUS "'-DLLVM_CONFIG=<path/to/llvm-config>' not passed as CMake flag, finding `llvm-config` via CMake...") | |
| find_program(LLVM_CONFIG "llvm-config") | |
| if(NOT LLVM_CONFIG) # check that it was found | |
| message(FATAL_ERROR "Unable to find 'llvm-config'") | |
| endif() | |
| endif() | |
| # check that we have 'llvm-config' version. | |
| message(STATUS "Executing 'llvm-config --version' at '${LLVM_CONFIG}' to check configuration.") | |
| execute_process( | |
| COMMAND ${LLVM_CONFIG} --version | |
| COMMAND_ERROR_IS_FATAL ANY | |
| OUTPUT_VARIABLE LLVM_CONFIG_VERSION | |
| ECHO_OUTPUT_VARIABLE | |
| OUTPUT_STRIP_TRAILING_WHITESPACE | |
| ) | |
| string(REGEX MATCH "^[0-9]*" LLVM_CONFIG_MAJOR_VERSION ${LLVM_CONFIG_VERSION}) | |
| message( | |
| STATUS | |
| "Found 'llvm-config' at '${LLVM_CONFIG}' with version '${LLVM_CONFIG_VERSION}', major version '${LLVM_CONFIG_MAJOR_VERSION}'" | |
| ) | |
| if(NOT LLVM_CONFIG_MAJOR_VERSION STREQUAL "19") | |
| message(FATAL_ERROR "Unable to find llvm-config version 19. Found invalid version '${LLVM_CONFIG_MAJOR_VERSION}'") | |
| endif() | |
| # -DLEAN_LLVM is used to conditionally compile Lean features that depend on LLVM | |
| string(APPEND CMAKE_CXX_FLAGS " -D LEAN_LLVM") | |
| execute_process(COMMAND ${LLVM_CONFIG} --ldflags OUTPUT_VARIABLE LLVM_CONFIG_LDFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE) | |
| execute_process(COMMAND ${LLVM_CONFIG} --libs OUTPUT_VARIABLE LLVM_CONFIG_LIBS OUTPUT_STRIP_TRAILING_WHITESPACE) | |
| execute_process(COMMAND ${LLVM_CONFIG} --libdir OUTPUT_VARIABLE LLVM_CONFIG_LIBDIR OUTPUT_STRIP_TRAILING_WHITESPACE) | |
| execute_process( | |
| COMMAND ${LLVM_CONFIG} --includedir | |
| OUTPUT_VARIABLE LLVM_CONFIG_INCLUDEDIR | |
| OUTPUT_STRIP_TRAILING_WHITESPACE | |
| ) | |
| execute_process( | |
| COMMAND ${LLVM_CONFIG} --cxxflags | |
| OUTPUT_VARIABLE LLVM_CONFIG_CXXFLAGS | |
| OUTPUT_STRIP_TRAILING_WHITESPACE | |
| ) | |
| execute_process( | |
| COMMAND ${LLVM_CONFIG} --system-libs | |
| OUTPUT_VARIABLE LLVM_CONFIG_SYSTEM_LIBS | |
| OUTPUT_STRIP_TRAILING_WHITESPACE | |
| ) | |
| message( | |
| STATUS | |
| "llvm-config: libdir '${LLVM_CONFIG_LIBDIR}' | ldflags '${LLVM_CONFIG_LDFLAGS}' | libs '${LLVM_CONFIG_LIBS}' | system libs '${LLVM_CONFIG_SYSTEM_LIBS}' | cxxflags: ${LLVM_CONFIG_CXXFLAGS} | includedir: ${LLVM_CONFIG_INCLUDEDIR}" | |
| ) | |
| else() | |
| message(WARNING "Disabling LLVM support") | |
| endif() | |
| # libleancpp/Lean as well as libleanrt/Init are cyclically dependent. This works by default on macOS, which also doesn't like | |
| # the linker flags necessary on other platforms. | |
| # Furthermore, add LLVM flags into the list of flags we need when linking. | |
| # when using shared libraries, set the `rpath` to help the dynamic loader | |
| if(CMAKE_SYSTEM_NAME MATCHES "Darwin") | |
| if(LLVM) # link to Zlib because LLVM depends on it. | |
| find_package(ZLIB REQUIRED) | |
| message(STATUS "ZLIB_LIBRARY: ${ZLIB_LIBRARY}") | |
| cmake_path(GET ZLIB_LIBRARY PARENT_PATH ZLIB_LIBRARY_PARENT_PATH) | |
| string(APPEND LEANSHARED_LINKER_FLAGS " -L ${ZLIB_LIBRARY_PARENT_PATH}") | |
| endif() | |
| string(APPEND TOOLCHAIN_STATIC_LINKER_FLAGS " -lleancpp -lInit -lStd -lLean -lleanrt") | |
| elseif(CMAKE_SYSTEM_NAME MATCHES "Emscripten") | |
| string(APPEND TOOLCHAIN_STATIC_LINKER_FLAGS " -lleancpp -lInit -lStd -lLean -lnodefs.js -lleanrt") | |
| else() | |
| string( | |
| APPEND TOOLCHAIN_STATIC_LINKER_FLAGS | |
| " -Wl,--start-group -lleancpp -lLean -Wl,--end-group -lStd -Wl,--start-group -lInit -lleanrt -Wl,--end-group" | |
| ) | |
| endif() | |
| set(LEAN_CXX_STDLIB "-lstdc++" CACHE STRING "C++ stdlib linker flags") | |
| if(CMAKE_SYSTEM_NAME MATCHES "Darwin") | |
| set(LEAN_CXX_STDLIB "-lc++") | |
| endif() | |
| if(CADICAL_LIB) | |
| string(APPEND TOOLCHAIN_STATIC_LINKER_FLAGS " -lcadical") | |
| string(APPEND TOOLCHAIN_SHARED_LINKER_FLAGS " -lcadical") | |
| endif() | |
| string(APPEND TOOLCHAIN_STATIC_LINKER_FLAGS " ${LEAN_CXX_STDLIB}") | |
| string(APPEND TOOLCHAIN_SHARED_LINKER_FLAGS " ${LEAN_CXX_STDLIB}") | |
| # in local builds, link executables and not just dynlibs against C++ stdlib as well, | |
| # which is required for e.g. asan | |
| if(NOT LEAN_STANDALONE) | |
| string(APPEND CMAKE_EXE_LINKER_FLAGS " ${LEAN_CXX_STDLIB}") | |
| endif() | |
| # flags for user binaries = flags for toolchain binaries + Lake (and deps) | |
| set(LEANC_STATIC_LINKER_FLAGS " ${TOOLCHAIN_STATIC_LINKER_FLAGS} -lLake -lLeanExport") | |
| if(CMAKE_SYSTEM_NAME MATCHES "Linux") | |
| set(LEANC_SHARED_LINKER_FLAGS " ${TOOLCHAIN_SHARED_LINKER_FLAGS} -Wl,--as-needed -lLake_shared -Wl,--no-as-needed") | |
| else() | |
| set(LEANC_SHARED_LINKER_FLAGS " ${TOOLCHAIN_SHARED_LINKER_FLAGS} -lLake_shared") | |
| endif() | |
| if(LLVM) | |
| string( | |
| APPEND LEANSHARED_LINKER_FLAGS | |
| " -L${LLVM_CONFIG_LIBDIR} ${LLVM_CONFIG_LDFLAGS} ${LLVM_CONFIG_LIBS} ${LLVM_CONFIG_SYSTEM_LIBS}" | |
| ) | |
| string(APPEND CMAKE_CXX_FLAGS " -I${LLVM_CONFIG_INCLUDEDIR}") | |
| endif() | |
| if(LLVM AND STAGE GREATER 0) | |
| # Here, we perform a replacement of `llvm-host` with `llvm`. This is necessary for our cross-compile | |
| # builds in `script/prepare-llvm-*.sh`. | |
| # - Recall that the host's copy of LLVM binaries and libraries is at | |
| # `llvm-host`, and the target's copy of LLVM binaries and libraries is at | |
| # `llvm`. | |
| # - In an ideal world, we would run the target's `llvm/bin/llvm-config` and get the correct link options for the target | |
| # (e.g. `-Lllvm/lib/libLLVM`.) | |
| # - However, the target's `llvm/bin/llvm-config` has a different target | |
| # triple from the host, and thus cannot be run on the host. | |
| # - So, we run the host `llvm-host/bin/llvm-config` from which we pick up | |
| # compiler options, and change the output of the host to point to the target. | |
| # - In particular, `host/bin/llvm-config` produces flags like `-Lllvm-host/lib/libLLVM`, while | |
| # we need the path to be `-Lllvm/lib/libLLVM`. Thus, we perform this replacement here. | |
| string(REPLACE "llvm-host" "llvm" LEANSHARED_LINKER_FLAGS ${LEANSHARED_LINKER_FLAGS}) | |
| string(REPLACE "llvm-host" "llvm" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) | |
| message(VERBOSE "leanshared linker flags: '${LEANSHARED_LINKER_FLAGS}' | lean extra cxx flags '${CMAKE_CXX_FLAGS}'") | |
| endif() | |
| # We always strip away unused declarations to reduce binary sizes as the time cost is small and the | |
| # potential benefit can be huge, especially when stripping `meta import`s. | |
| if(CMAKE_SYSTEM_NAME MATCHES "Darwin") | |
| string(APPEND LEANC_EXTRA_CC_FLAGS " -fdata-sections -ffunction-sections") | |
| string(APPEND LEAN_EXTRA_LINKER_FLAGS " -Wl,-dead_strip") | |
| elseif(NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten") | |
| string(APPEND LEANC_EXTRA_CC_FLAGS " -fdata-sections -ffunction-sections") | |
| string(APPEND LEAN_EXTRA_LINKER_FLAGS " -Wl,--gc-sections") | |
| endif() | |
| if(NOT CMAKE_SYSTEM_NAME MATCHES "Darwin") | |
| string(APPEND LEAN_EXTRA_LINKER_FLAGS " -lm") | |
| endif() | |
| if(CMAKE_SYSTEM_NAME MATCHES "Linux") | |
| if(BSYMBOLIC) | |
| string(APPEND LEANC_SHARED_LINKER_FLAGS " -Wl,-Bsymbolic") | |
| string(APPEND TOOLCHAIN_SHARED_LINKER_FLAGS " -Wl,-Bsymbolic") | |
| endif() | |
| string(APPEND CMAKE_CXX_FLAGS " -fPIC -ftls-model=initial-exec") | |
| string(APPEND LEANC_EXTRA_CC_FLAGS " -fPIC") | |
| string(APPEND TOOLCHAIN_SHARED_LINKER_FLAGS " -Wl,-rpath=\\$$ORIGIN/..:\\$$ORIGIN") | |
| string(APPEND INIT_SHARED_LINKER_FLAGS " -Wl,-soname=libInit_shared.so") | |
| string(APPEND LEANSHARED_1_LINKER_FLAGS " -Wl,-soname=libleanshared_1.so") | |
| string(APPEND LEANSHARED_2_LINKER_FLAGS " -Wl,-soname=libleanshared_2.so") | |
| string(APPEND LEANSHARED_3_LINKER_FLAGS " -Wl,-soname=libleanshared_3.so") | |
| string(APPEND LEANSHARED_LINKER_FLAGS " -Wl,-soname=libleanshared.so") | |
| string( | |
| APPEND LAKESHARED_LINKER_FLAGS | |
| " -Wl,--whole-archive ${CMAKE_BINARY_DIR}/lib/lean/libLake.a.export -Wl,--no-whole-archive ${CMAKE_BINARY_DIR}/lib/lean/libLeanExport.a.export -Wl,-soname=libLake_shared.so" | |
| ) | |
| string(APPEND CMAKE_EXE_LINKER_FLAGS " -Wl,-rpath=$ORIGIN/../lib:$ORIGIN/../lib/lean") | |
| elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin") | |
| string(APPEND CMAKE_CXX_FLAGS " -ftls-model=initial-exec") | |
| string(APPEND INIT_SHARED_LINKER_FLAGS " -install_name @rpath/libInit_shared.dylib") | |
| string(APPEND LEANSHARED_1_LINKER_FLAGS " -install_name @rpath/libleanshared_1.dylib") | |
| string(APPEND LEANSHARED_2_LINKER_FLAGS " -install_name @rpath/libleanshared_2.dylib") | |
| string(APPEND LEANSHARED_3_LINKER_FLAGS " -install_name @rpath/libleanshared_3.dylib") | |
| string(APPEND LEANSHARED_LINKER_FLAGS " -install_name @rpath/libleanshared.dylib") | |
| string( | |
| APPEND LAKESHARED_LINKER_FLAGS | |
| " -Wl,-force_load,${CMAKE_BINARY_DIR}/lib/lean/libLake.a.export ${CMAKE_BINARY_DIR}/lib/lean/libLeanExport.a.export -install_name @rpath/libLake_shared.dylib" | |
| ) | |
| string(APPEND CMAKE_EXE_LINKER_FLAGS " -Wl,-rpath,@executable_path/../lib -Wl,-rpath,@executable_path/../lib/lean") | |
| elseif(CMAKE_SYSTEM_NAME MATCHES "Emscripten") | |
| string(APPEND CMAKE_CXX_FLAGS " -fPIC") | |
| string(APPEND LEANC_EXTRA_CC_FLAGS " -fPIC") | |
| elseif(CMAKE_SYSTEM_NAME MATCHES "Windows") | |
| string( | |
| APPEND LAKESHARED_LINKER_FLAGS | |
| " -Wl,--out-implib,${CMAKE_BINARY_DIR}/lib/lean/libLake_shared.dll.a -Wl,--whole-archive ${CMAKE_BINARY_DIR}/lib/lean/libLake.a.export -Wl,--no-whole-archive ${CMAKE_BINARY_DIR}/lib/lean/libLeanExport.a.export" | |
| ) | |
| endif() | |
| if(CMAKE_SYSTEM_NAME MATCHES "Linux") | |
| string(APPEND LEAN_EXTRA_LINKER_FLAGS " -ldl") | |
| endif() | |
| if(NOT CMAKE_SYSTEM_NAME MATCHES "Windows" AND NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten") | |
| # export symbols for the interpreter (done via `LEAN_EXPORT` for Windows) | |
| string(APPEND CMAKE_DYN_EXE_LINKER_FLAGS " -rdynamic") | |
| # hide all other symbols | |
| string(APPEND CMAKE_CXX_FLAGS " -fvisibility=hidden -fvisibility-inlines-hidden") | |
| string(APPEND LEANC_EXTRA_CC_FLAGS " -fvisibility=hidden") | |
| endif() | |
| # On Windows, add bcrypt for random number generation | |
| if(CMAKE_SYSTEM_NAME MATCHES "Windows") | |
| string(APPEND LEAN_EXTRA_LINKER_FLAGS " -lbcrypt") | |
| endif() | |
| # Allow `lean` symbols in plugins without linking directly against it. If we linked against the | |
| # executable or `leanshared`, plugins would try to look them up at load time (even though they | |
| # are already loaded) and probably fail unless we set up LD_LIBRARY_PATH. | |
| if(CMAKE_SYSTEM_NAME MATCHES "Windows") | |
| # import libraries created by the stdlib.make targets | |
| string(APPEND LEANC_SHARED_LINKER_FLAGS " -lInit_shared -lleanshared_3 -lleanshared_2 -lleanshared_1 -lleanshared") | |
| elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin") | |
| # The second flag is necessary to even *load* dylibs without resolved symbols, as can happen | |
| # if a Lake `extern_lib` depends on a symbols defined by the Lean library but is loaded even | |
| # before definition. | |
| string(APPEND LEANC_SHARED_LINKER_FLAGS " -Wl,-undefined,dynamic_lookup -Wl,-no_fixup_chains") | |
| endif() | |
| # Linux ignores undefined symbols in shared libraries by default | |
| if(MULTI_THREAD AND NOT MSVC AND NOT CMAKE_SYSTEM_NAME MATCHES "Darwin") | |
| string(APPEND LEAN_EXTRA_LINKER_FLAGS " -pthread") | |
| endif() | |
| # The stage0 tree hash. For non-release builds this is embedded as `Lean.githash`. | |
| # It is also the githash Lake always needs to use for its traces. It is passed via `LEAN_GITHASH` | |
| # in `stdlib.make` and propagated to the cache steps by CI. This ensures that stage1 `.olean`s stay | |
| # reusable across commits that do not touch stage0. | |
| execute_process( | |
| COMMAND git rev-parse HEAD:stage0 | |
| WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" | |
| OUTPUT_VARIABLE LAKE_LEAN_GITHASH | |
| OUTPUT_STRIP_TRAILING_WHITESPACE | |
| # Failure is handled below (jj fallback, then a fatal error when the cache needs it). | |
| ERROR_QUIET | |
| ) | |
| # Fallback for jj workspaces where git cannot find .git directly. | |
| # Use `jj git root` to find the backing git repo, then `jj log` to | |
| # resolve the current workspace's commit (git HEAD points to the root | |
| # workspace, not the current one). Both pass `--ignore-working-copy`, as a | |
| # snapshot writes a jj operation that races any jj command run during | |
| # configure; `@` is then as of the last jj command. | |
| if("${LAKE_LEAN_GITHASH}" STREQUAL "") | |
| find_program(JJ_EXECUTABLE jj) | |
| if(JJ_EXECUTABLE) | |
| execute_process( | |
| COMMAND "${JJ_EXECUTABLE}" --ignore-working-copy git root | |
| WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" | |
| OUTPUT_VARIABLE _jj_git_dir | |
| OUTPUT_STRIP_TRAILING_WHITESPACE | |
| ERROR_QUIET | |
| RESULT_VARIABLE _jj_git_root_result | |
| ) | |
| execute_process( | |
| COMMAND "${JJ_EXECUTABLE}" --ignore-working-copy log -r @ --no-graph -T "commit_id" | |
| WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" | |
| OUTPUT_VARIABLE _jj_commit | |
| OUTPUT_STRIP_TRAILING_WHITESPACE | |
| ERROR_QUIET | |
| RESULT_VARIABLE _jj_rev_result | |
| ) | |
| if(_jj_git_root_result EQUAL 0 AND _jj_rev_result EQUAL 0) | |
| execute_process( | |
| COMMAND git --git-dir "${_jj_git_dir}" rev-parse "${_jj_commit}:stage0" | |
| OUTPUT_VARIABLE LAKE_LEAN_GITHASH | |
| OUTPUT_STRIP_TRAILING_WHITESPACE | |
| ) | |
| endif() | |
| endif() | |
| endif() | |
| # A missing or wrong hash would silently break cache correctness (misses, or worse, mismatched | |
| # reuse), so fail loudly when the cache is enabled. | |
| if(USE_LAKE_CACHE AND "${LAKE_LEAN_GITHASH}" STREQUAL "") | |
| message(FATAL_ERROR "Failed to determine the stage0 tree hash required to key the Lake artifact cache") | |
| endif() | |
| # Emitted so CI can propagate the same value to the standalone cache upload/download steps. | |
| file(WRITE "${CMAKE_BINARY_DIR}/lake-githash" "${LAKE_LEAN_GITHASH}") | |
| # Git HASH | |
| if(USE_GITHASH) | |
| include(GetGitRevisionDescription) | |
| get_git_head_revision(GIT_REFSPEC GIT_SHA1 ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR) | |
| if(GIT_SHA1 MATCHES "GITDIR-NOTFOUND") | |
| message(STATUS "Failed to read git_sha1") | |
| set(GIT_SHA1 "") | |
| else() | |
| message(STATUS "git commit sha1: ${GIT_SHA1}") | |
| endif() | |
| else() | |
| if(USE_LAKE AND STAGE EQUAL 0) | |
| # Embed the stage0 tree hash so Lake invalidates stage 1 on stage 0 changes. | |
| set(GIT_SHA1 "${LAKE_LEAN_GITHASH}") | |
| message(STATUS "stage0 sha1: ${GIT_SHA1}") | |
| else() | |
| set(GIT_SHA1 "") | |
| endif() | |
| endif() | |
| configure_file("${LEAN_SOURCE_DIR}/githash.h.in" "${LEAN_BINARY_DIR}/githash.h") | |
| if(USE_LAKE AND STAGE EQUAL 0) | |
| # Now that we've prepared the information for the next stage, we can forget that we will use | |
| # Lake in the future as we won't use it in this stage | |
| set(USE_LAKE OFF) | |
| endif() | |
| # Windows uses ";" as a path separator. We use `LEAN_PATH_SEPARATOR` on scripts such as lean.mk.in | |
| if(CMAKE_SYSTEM_NAME MATCHES "Windows") | |
| set(LEAN_PATH_SEPARATOR ";") | |
| else() | |
| set(LEAN_PATH_SEPARATOR ":") | |
| endif() | |
| # inherit genral options for lean.mk.in and stdlib.make.in | |
| string(APPEND LEAN_EXTRA_MAKE_OPTS " ${LEAN_EXTRA_OPTS}") | |
| # Version | |
| configure_file("${LEAN_SOURCE_DIR}/version.h.in" "${LEAN_BINARY_DIR}/include/lean/version.h") | |
| if(STAGE EQUAL 0) | |
| set(LEAN_IS_STAGE0 "#define LEAN_IS_STAGE0 1") | |
| else() | |
| set(LEAN_IS_STAGE0 "#define LEAN_IS_STAGE0 0") | |
| endif() | |
| configure_file("${LEAN_SOURCE_DIR}/config.h.in" "${LEAN_BINARY_DIR}/include/lean/config.h") | |
| if(USE_MIMALLOC) | |
| file( | |
| COPY "${LEAN_BINARY_DIR}/../mimalloc/src/mimalloc/include/mimalloc.h" | |
| DESTINATION "${LEAN_BINARY_DIR}/include/lean" | |
| ) | |
| endif() | |
| install(DIRECTORY ${LEAN_BINARY_DIR}/include/ DESTINATION include) | |
| configure_file(${LEAN_SOURCE_DIR}/lean.mk.in ${LEAN_BINARY_DIR}/share/lean/lean.mk) | |
| install(DIRECTORY ${LEAN_BINARY_DIR}/share/ DESTINATION share) | |
| include_directories(${LEAN_SOURCE_DIR}) | |
| include_directories(${CMAKE_BINARY_DIR}) # version.h etc., "private" headers | |
| include_directories(${CMAKE_BINARY_DIR}/include) # config.h etc., "public" headers | |
| # Lean code only needs this one include | |
| string(APPEND LEANC_OPTS " -I${CMAKE_BINARY_DIR}/include") | |
| # Use CMake profile C++ flags for building Lean libraries, but do not embed in `leanc` | |
| string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE) | |
| string(APPEND LEANC_OPTS " ${CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE}}") | |
| # Do embed flag for finding system headers and libraries in dev builds | |
| if(CMAKE_OSX_SYSROOT AND NOT LEAN_STANDALONE) | |
| string(APPEND LEANC_EXTRA_CC_FLAGS " ${CMAKE_CXX_SYSROOT_FLAG}${CMAKE_OSX_SYSROOT}") | |
| string(APPEND LEAN_EXTRA_LINKER_FLAGS " ${CMAKE_CXX_SYSROOT_FLAG}${CMAKE_OSX_SYSROOT}") | |
| endif() | |
| add_subdirectory(initialize) | |
| add_subdirectory(shell) | |
| # The relative path doesn't work once the CMakeLists.txt files have been copied into the stage0 dir. | |
| # Since stage0 needs no tests, we just ignore them instead of fixing the path. | |
| if(NOT STAGE EQUAL 0) | |
| add_subdirectory(../tests "${CMAKE_CURRENT_BINARY_DIR}/tests") | |
| endif() | |
| # to be included in `leanshared` but not the smaller `leanshared_*` (as it would pull | |
| # in the world) | |
| add_library(leaninitialize STATIC $<TARGET_OBJECTS:initialize>) | |
| set_target_properties( | |
| leaninitialize | |
| PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/temp OUTPUT_NAME leaninitialize | |
| ) | |
| add_library(leanshell STATIC util/shell.cpp) | |
| set_target_properties(leanshell PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/temp OUTPUT_NAME leanshell) | |
| if(CMAKE_SYSTEM_NAME MATCHES "Windows") | |
| string(APPEND CMAKE_EXE_LINKER_FLAGS " -Wl,--whole-archive -lleanmanifest -Wl,--no-whole-archive") | |
| endif() | |
| # for later stages, this defines only `mimalloc_paranoid`; the rest is imported below instead | |
| add_subdirectory(runtime) | |
| if(STAGE GREATER 1) | |
| # reuse C++ parts, which don't change | |
| add_library(leanrt_initial-exec STATIC IMPORTED) | |
| set_target_properties( | |
| leanrt_initial-exec | |
| PROPERTIES IMPORTED_LOCATION "${CMAKE_BINARY_DIR}/runtime/libleanrt_initial-exec.a" | |
| ) | |
| add_library(leanrt STATIC IMPORTED) | |
| set_target_properties(leanrt PROPERTIES IMPORTED_LOCATION "${CMAKE_BINARY_DIR}/lib/lean/libleanrt.a") | |
| add_library(leancpp_1 STATIC IMPORTED) | |
| set_target_properties(leancpp_1 PROPERTIES IMPORTED_LOCATION "${CMAKE_BINARY_DIR}/lib/temp/libleancpp_1.a") | |
| add_library(leancpp STATIC IMPORTED) | |
| set_target_properties(leancpp PROPERTIES IMPORTED_LOCATION "${CMAKE_BINARY_DIR}/lib/lean/libleancpp.a") | |
| add_custom_target( | |
| copy-leancpp | |
| COMMAND | |
| ${CMAKE_COMMAND} -E copy_if_different "${PREV_STAGE}/runtime/libleanrt_initial-exec.a" | |
| "${CMAKE_BINARY_DIR}/runtime/libleanrt_initial-exec.a" | |
| COMMAND ${CMAKE_COMMAND} -E copy_if_different "${PREV_STAGE}/lib/lean/libleanrt.a" "${CMAKE_BINARY_DIR}/lib/lean/libleanrt.a" | |
| COMMAND ${CMAKE_COMMAND} -E copy_if_different "${PREV_STAGE}/lib/lean/libleancpp.a" "${CMAKE_BINARY_DIR}/lib/lean/libleancpp.a" | |
| COMMAND | |
| ${CMAKE_COMMAND} -E copy_if_different "${PREV_STAGE}/lib/temp/libleancpp_1.a" "${CMAKE_BINARY_DIR}/lib/temp/libleancpp_1.a" | |
| ) | |
| add_dependencies(leanrt_initial-exec copy-leancpp) | |
| add_dependencies(leanrt copy-leancpp) | |
| add_dependencies(leancpp_1 copy-leancpp) | |
| add_dependencies(leancpp copy-leancpp) | |
| if(LLVM) | |
| add_custom_target( | |
| copy-lean-h-bc | |
| COMMAND ${CMAKE_COMMAND} -E copy_if_different "${PREV_STAGE}/lib/lean/lean.h.bc" "${CMAKE_BINARY_DIR}/lib/lean/lean.h.bc" | |
| ) | |
| add_dependencies(leancpp copy-lean-h-bc) | |
| endif() | |
| else() | |
| if(CMAKE_SYSTEM_NAME MATCHES "Emscripten") | |
| add_dependencies(leanrt libuv) | |
| add_dependencies(leanrt_initial-exec libuv) | |
| endif() | |
| add_subdirectory(util) | |
| set(LEAN_OBJS ${LEAN_OBJS} $<TARGET_OBJECTS:util>) | |
| add_subdirectory(kernel) | |
| set(LEAN_OBJS ${LEAN_OBJS} $<TARGET_OBJECTS:kernel>) | |
| add_subdirectory(library) | |
| set(LEAN_OBJS ${LEAN_OBJS} $<TARGET_OBJECTS:library>) | |
| add_subdirectory(library/constructions) | |
| set(LEAN_OBJS ${LEAN_OBJS} $<TARGET_OBJECTS:constructions>) | |
| # leancpp without `initialize` (see `leaninitialize` above) | |
| add_library(leancpp_1 STATIC ${LEAN_OBJS}) | |
| set_target_properties( | |
| leancpp_1 | |
| PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/temp OUTPUT_NAME leancpp_1 | |
| ) | |
| add_library(leancpp STATIC ${LEAN_OBJS} $<TARGET_OBJECTS:initialize>) | |
| set_target_properties(leancpp PROPERTIES OUTPUT_NAME leancpp) | |
| endif() | |
| if(STAGE GREATER 0 AND CADICAL AND INSTALL_CADICAL) | |
| add_custom_target( | |
| copy-cadical | |
| COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CADICAL}" "${CMAKE_BINARY_DIR}/bin/cadical${CMAKE_EXECUTABLE_SUFFIX}" | |
| ) | |
| add_dependencies(leancpp copy-cadical) | |
| endif() | |
| if(CADICAL_LIB) | |
| set(CADICAL_LIB_DEP "${CMAKE_BINARY_DIR}/lib/lean/libcadical.a") | |
| add_custom_target( | |
| copy-cadical-lib | |
| COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/lib/lean" | |
| COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CADICAL_LIB}" "${CADICAL_LIB_DEP}" | |
| ) | |
| add_dependencies(leanrt copy-cadical-lib) | |
| add_dependencies(leanrt_initial-exec copy-cadical-lib) | |
| endif() | |
| if(LEANTAR AND INSTALL_LEANTAR) | |
| add_custom_target( | |
| copy-leantar | |
| COMMAND ${CMAKE_COMMAND} -E copy_if_different "${LEANTAR}" "${CMAKE_BINARY_DIR}/bin/leantar${CMAKE_EXECUTABLE_SUFFIX}" | |
| ) | |
| add_dependencies(leancpp copy-leantar) | |
| endif() | |
| # MSYS2 bash usually handles Windows paths relatively well, but not when putting them in the PATH | |
| string(REGEX REPLACE "^([a-zA-Z]):" "/\\1" LEAN_BIN "${CMAKE_BINARY_DIR}/bin") | |
| # ...and Make doesn't like absolute Windows paths either | |
| # (also looks nicer in the build log) | |
| file(RELATIVE_PATH LIB ${LEAN_SOURCE_DIR} ${CMAKE_BINARY_DIR}/lib) | |
| if(CMAKE_SYSTEM_NAME MATCHES "Emscripten") | |
| # We do not use dynamic linking via leanshared for Emscripten to keep things | |
| # simple. (And we are not interested in `Lake` anyway.) To use dynamic | |
| # linking, we would probably have to set MAIN_MODULE=2 on `leanshared`, | |
| # SIDE_MODULE=2 on `lean`, and set CMAKE_SHARED_LIBRARY_SUFFIX to ".js". | |
| # We set `ERROR_ON_UNDEFINED_SYMBOLS=0` because our build of LibUV does not | |
| # define all symbols, see the comment about LibUV on WebAssembly further up | |
| # in this file. | |
| string( | |
| APPEND LEAN_EXE_LINKER_FLAGS | |
| " ${LIB}/temp/libleanshell.a ${TOOLCHAIN_STATIC_LINKER_FLAGS} ${EMSCRIPTEN_SETTINGS} -lnodefs.js -s EXIT_RUNTIME=1 -s MAIN_MODULE=1 -s LINKABLE=1 -s EXPORT_ALL=1 -s ERROR_ON_UNDEFINED_SYMBOLS=0" | |
| ) | |
| endif() | |
| # Build the compiler using the bootstrapped C sources for stage0, and use | |
| # the LLVM build for stage1 and further. | |
| if(LLVM AND STAGE GREATER 0) | |
| set(EXTRA_LEANMAKE_OPTS "LLVM=1") | |
| endif() | |
| set( | |
| STDLIBS | |
| Init | |
| Std | |
| Lean | |
| LeanExport | |
| Leanc | |
| LeanIR | |
| ) | |
| if(NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten") | |
| list(APPEND STDLIBS Lake LeanChecker) | |
| endif() | |
| add_custom_target( | |
| make_stdlib | |
| ALL | |
| WORKING_DIRECTORY ${LEAN_SOURCE_DIR} | |
| # The actual rule is in a separate makefile because we want to prefix it with '+' to use the Make job server | |
| # for a parallelized nested build, but CMake doesn't let us do that. | |
| # We use `lean` from the previous stage, but `leanc`, headers, etc. from the current stage | |
| COMMAND $(MAKE) -f ${CMAKE_BINARY_DIR}/stdlib.make ${STDLIBS} | |
| VERBATIM | |
| ) | |
| # if we have LLVM enabled, then build `lean.h.bc` which has the LLVM bitcode | |
| # of Lean runtime to be built. | |
| if(LLVM AND STAGE EQUAL 1) | |
| add_dependencies(make_stdlib runtime_bc) | |
| endif() | |
| # We declare these as separate custom targets so they use separate `make` invocations, which makes `make` recompute which dependencies | |
| # (e.g. `libLean.a`) are now newer than the target file | |
| if(CMAKE_SYSTEM_NAME MATCHES "Emscripten") | |
| # dummy targets, see `MAIN_MODULE` discussion above | |
| add_custom_target( | |
| Init_shared | |
| ALL | |
| DEPENDS make_stdlib leanrt_initial-exec | |
| COMMAND touch ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libInit_shared${CMAKE_SHARED_LIBRARY_SUFFIX} | |
| ) | |
| add_custom_target( | |
| leanshared | |
| ALL | |
| DEPENDS Init_shared leancpp | |
| COMMAND touch ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libleanshared_3${CMAKE_SHARED_LIBRARY_SUFFIX} | |
| COMMAND touch ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libleanshared_2${CMAKE_SHARED_LIBRARY_SUFFIX} | |
| COMMAND touch ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libleanshared_1${CMAKE_SHARED_LIBRARY_SUFFIX} | |
| COMMAND touch ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libleanshared${CMAKE_SHARED_LIBRARY_SUFFIX} | |
| ) | |
| add_custom_target( | |
| lake_shared | |
| ALL | |
| DEPENDS leanshared | |
| COMMAND touch ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libLake_shared${CMAKE_SHARED_LIBRARY_SUFFIX} | |
| ) | |
| else() | |
| add_custom_target( | |
| Init_shared | |
| ALL | |
| WORKING_DIRECTORY ${LEAN_SOURCE_DIR} | |
| DEPENDS make_stdlib leanrt_initial-exec | |
| COMMAND $(MAKE) -f ${CMAKE_BINARY_DIR}/stdlib.make Init_shared | |
| VERBATIM | |
| ) | |
| add_custom_target( | |
| leanshared | |
| ALL | |
| WORKING_DIRECTORY ${LEAN_SOURCE_DIR} | |
| DEPENDS Init_shared leancpp_1 leancpp leanshell leaninitialize | |
| COMMAND $(MAKE) -f ${CMAKE_BINARY_DIR}/stdlib.make leanshared | |
| VERBATIM | |
| ) | |
| string(APPEND CMAKE_DYN_EXE_LINKER_FLAGS " -lInit_shared -lleanshared_3 -lleanshared_2 -lleanshared_1 -lleanshared") | |
| # NOTE: `mimalloc_paranoid` in front of `leanrt` and no `leanshared` to replace default allocator | |
| set( | |
| LEANCHECKER_PARANOID_LINKER_FLAGS | |
| "${CMAKE_EXE_LINKER_FLAGS} -lmimalloc_paranoid -linitialize_minimal ${LEANC_STATIC_LINKER_FLAGS}" | |
| ) | |
| endif() | |
| if(NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten") | |
| add_custom_target( | |
| lake_shared | |
| WORKING_DIRECTORY ${LEAN_SOURCE_DIR} | |
| DEPENDS leanshared | |
| COMMAND $(MAKE) -f ${CMAKE_BINARY_DIR}/stdlib.make libLake_shared | |
| VERBATIM | |
| ) | |
| add_custom_target( | |
| lake | |
| ALL | |
| WORKING_DIRECTORY ${LEAN_SOURCE_DIR} | |
| DEPENDS lake_shared | |
| COMMAND $(MAKE) -f ${CMAKE_BINARY_DIR}/stdlib.make lake | |
| VERBATIM | |
| ) | |
| add_custom_target( | |
| leanchecker | |
| ALL | |
| WORKING_DIRECTORY ${LEAN_SOURCE_DIR} | |
| DEPENDS lake_shared | |
| COMMAND $(MAKE) -f ${CMAKE_BINARY_DIR}/stdlib.make leanchecker | |
| VERBATIM | |
| ) | |
| # NOTE: uses toolchain from contained `lean-toolchain` so bumps are necessary only on kernel | |
| # interface changes. Not in `ALL` as it's expensive; release CI builds it explicitly. | |
| ExternalProject_Add( | |
| lean4lean | |
| PREFIX lean4lean | |
| EXCLUDE_FROM_ALL ON | |
| GIT_REPOSITORY https://github.com/Kha/lean4lean | |
| GIT_TAG lean4-bundle-4.35 | |
| PATCH_COMMAND bash ${LEAN_SOURCE_DIR}/../script/lean4lean-minimal-init.sh | |
| CONFIGURE_COMMAND "" | |
| BUILD_COMMAND lake build lean4lean | |
| BUILD_IN_SOURCE ON | |
| INSTALL_COMMAND | |
| ${CMAKE_COMMAND} -E copy .lake/build/bin/lean4lean${CMAKE_EXECUTABLE_SUFFIX} | |
| ${CMAKE_BINARY_DIR}/bin | |
| ) | |
| # `cargo` links with whatever `cc` is on `PATH`. In a standalone build, link the Rust checkers through `leanc` | |
| # instead so that they bind to the bundled glibc and the default ELF interpreter like every other shipped binary. | |
| set(CARGO_LINK_WITH_LEANC OFF) | |
| set(CARGO_BUILD_ENV) | |
| if(LEAN_STANDALONE AND CMAKE_SYSTEM_NAME STREQUAL "Linux" AND STAGE GREATER 0) | |
| set(CARGO_LINK_WITH_LEANC ON) | |
| string(TOUPPER "${CMAKE_SYSTEM_PROCESSOR}_unknown_linux_gnu" CARGO_RUST_TARGET) | |
| set(CARGO_BUILD_ENV ${CMAKE_COMMAND} -E env "CARGO_TARGET_${CARGO_RUST_TARGET}_LINKER=${CMAKE_BINARY_DIR}/bin/leanc") | |
| endif() | |
| # Not in `ALL` as it's expensive; release CI builds it explicitly. | |
| ExternalProject_Add( | |
| nanoda | |
| PREFIX nanoda | |
| EXCLUDE_FROM_ALL ON | |
| GIT_REPOSITORY https://github.com/ammkrn/nanoda_lib | |
| GIT_TAG 8de7e0c4ed3052745a7f8725164efd9e76d84c56 | |
| CONFIGURE_COMMAND "" | |
| BUILD_COMMAND ${CARGO_BUILD_ENV} cargo build --release --locked | |
| BUILD_IN_SOURCE ON | |
| INSTALL_COMMAND | |
| ${CMAKE_COMMAND} -E copy target/release/nanoda_bin${CMAKE_EXECUTABLE_SUFFIX} | |
| ${CMAKE_BINARY_DIR}/bin | |
| ) | |
| if(CARGO_LINK_WITH_LEANC) | |
| add_dependencies(nanoda leanc) | |
| endif() | |
| # Not in `ALL` as it's expensive; release CI builds it explicitly. | |
| ExternalProject_Add( | |
| con-leche | |
| PREFIX con-leche | |
| EXCLUDE_FROM_ALL ON | |
| GIT_REPOSITORY https://github.com/leanprover/con-leche | |
| GIT_TAG 6810492c1ccc325b4a2bbf9dfe70bd07fd933da2 | |
| CONFIGURE_COMMAND "" | |
| BUILD_COMMAND lake build con-leche | |
| BUILD_IN_SOURCE ON | |
| INSTALL_COMMAND | |
| ${CMAKE_COMMAND} -E copy .lake/build/bin/con-leche${CMAKE_EXECUTABLE_SUFFIX} | |
| ${CMAKE_BINARY_DIR}/bin | |
| ) | |
| # Windows distribution support is deferred pending runtime validation. | |
| if(STAGE GREATER 0 AND NOT WIN32) | |
| # Beam's plugin must be built with the toolchain that will load it. | |
| ExternalProject_Add( | |
| lean-beam | |
| PREFIX lean-beam-native | |
| EXCLUDE_FROM_ALL ON | |
| GIT_REPOSITORY https://github.com/leanprover/lean-beam | |
| GIT_TAG a9645d2033815fea9a91ce67f7e5f5c8b9eacc85 | |
| CONFIGURE_COMMAND "" | |
| BUILD_COMMAND ${CMAKE_COMMAND} | |
| -DLEAN_PREFIX=${CMAKE_BINARY_DIR} | |
| -P <SOURCE_DIR>/scripts/build-toolchain.cmake | |
| BUILD_IN_SOURCE ON | |
| BUILD_ALWAYS ON | |
| INSTALL_COMMAND ${CMAKE_COMMAND} | |
| -DLEAN_PREFIX=${CMAKE_BINARY_DIR} | |
| -DPREFIX=${CMAKE_BINARY_DIR} | |
| -P <SOURCE_DIR>/scripts/install-toolchain.cmake | |
| ) | |
| endif() | |
| # Not in `ALL` as it's expensive; release CI builds it explicitly. Its only submodule is the | |
| # Aeneas verifier, which the binary does not need. | |
| ExternalProject_Add( | |
| con-ron | |
| PREFIX con-ron | |
| EXCLUDE_FROM_ALL ON | |
| GIT_REPOSITORY https://github.com/leanprover/con-ron | |
| GIT_TAG 42a086407cab3c400b359f75b5d24942825626b5 | |
| GIT_SUBMODULES "" | |
| CONFIGURE_COMMAND "" | |
| BUILD_COMMAND ${CARGO_BUILD_ENV} cargo build --release --locked | |
| BUILD_IN_SOURCE ON | |
| INSTALL_COMMAND | |
| ${CMAKE_COMMAND} -E copy target/release/con-ron${CMAKE_EXECUTABLE_SUFFIX} | |
| ${CMAKE_BINARY_DIR}/bin | |
| ) | |
| if(CARGO_LINK_WITH_LEANC) | |
| add_dependencies(con-ron leanc) | |
| endif() | |
| # Not `ALL` as it's big; release CI builds it explicitly. | |
| if(USE_MIMALLOC) | |
| add_custom_target( | |
| leanchecker-paranoid | |
| WORKING_DIRECTORY ${LEAN_SOURCE_DIR} | |
| DEPENDS lake_shared mimalloc_paranoid initialize_minimal | |
| COMMAND $(MAKE) -f ${CMAKE_BINARY_DIR}/stdlib.make leanchecker-paranoid | |
| VERBATIM | |
| ) | |
| endif() | |
| endif() | |
| if(PREV_STAGE) | |
| add_custom_target( | |
| update-stage0 | |
| COMMAND bash -c 'CSRCS=${CMAKE_BINARY_DIR}/lib/temp script/lib/update-stage0' | |
| DEPENDS make_stdlib | |
| WORKING_DIRECTORY "${LEAN_SOURCE_DIR}/.." | |
| ) | |
| add_custom_target(update-stage0-commit COMMAND git commit -m "chore: update stage0" DEPENDS update-stage0) | |
| endif() | |
| if(NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten") | |
| add_custom_target(leanir ALL DEPENDS leanshared initialize_minimal_shim COMMAND $(MAKE) -f ${CMAKE_BINARY_DIR}/stdlib.make leanir VERBATIM) | |
| add_custom_target(leanexport ALL DEPENDS leanshared COMMAND $(MAKE) -f ${CMAKE_BINARY_DIR}/stdlib.make leanexport VERBATIM) | |
| endif() | |
| # use Bash version for building, use Lean version in bin/ for tests & distribution | |
| configure_file("${LEAN_SOURCE_DIR}/bin/leanc.in" "${CMAKE_BINARY_DIR}/leanc.sh" @ONLY) | |
| if(STAGE GREATER 0 AND NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten") | |
| configure_file("${LEAN_SOURCE_DIR}/Leanc.lean" "${CMAKE_BINARY_DIR}/leanc/Leanc.lean" @ONLY) | |
| add_custom_target( | |
| leanc | |
| ALL | |
| WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/leanc | |
| DEPENDS leanshared | |
| COMMAND $(MAKE) -f ${CMAKE_BINARY_DIR}/stdlib.make leanc | |
| VERBATIM | |
| ) | |
| if(TARGET lean-beam) | |
| add_dependencies(lean-beam lake leanc) | |
| endif() | |
| endif() | |
| file(COPY ${LEAN_SOURCE_DIR}/bin/leanmake DESTINATION ${CMAKE_BINARY_DIR}/bin) | |
| install(DIRECTORY "${CMAKE_BINARY_DIR}/bin/" USE_SOURCE_PERMISSIONS DESTINATION bin) | |
| if(STAGE GREATER 0 AND CADICAL AND INSTALL_CADICAL) | |
| install(PROGRAMS "${CADICAL}" DESTINATION bin) | |
| endif() | |
| if(LEANTAR AND INSTALL_LEANTAR) | |
| install(PROGRAMS "${LEANTAR}" DESTINATION bin) | |
| endif() | |
| add_custom_target( | |
| clean-stdlib | |
| COMMAND rm -rf "${CMAKE_BINARY_DIR}/lib" || true | |
| ) | |
| add_custom_target(clean-olean DEPENDS clean-stdlib) | |
| if(USE_LAKE_CACHE) | |
| add_custom_target( | |
| cache-get | |
| COMMAND ${PREV_STAGE}/bin/lake${CMAKE_EXECUTABLE_SUFFIX} cache get --repo=leanprover/lean4 $$LAKE_CACHE_EXTRA_ARGS | |
| ) | |
| endif() | |
| install( | |
| DIRECTORY "${CMAKE_BINARY_DIR}/lib/" | |
| DESTINATION lib | |
| PATTERN temp EXCLUDE | |
| PATTERN "*.export" EXCLUDE | |
| PATTERN "*.hash" EXCLUDE | |
| PATTERN "*.trace" EXCLUDE | |
| PATTERN "*.rsp" EXCLUDE | |
| PATTERN "*.filelist" EXCLUDE | |
| ) | |
| # symlink source into expected installation location for go-to-definition, if file system allows it | |
| file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/src) | |
| # get rid of all files in `src/lean` that may have been loaded from the cache | |
| # (at the time of writing this, this is the case for some lake test .c files) | |
| file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/src/lean) | |
| if(STAGE EQUAL 0) | |
| file(CREATE_LINK ${CMAKE_SOURCE_DIR}/../../src ${CMAKE_BINARY_DIR}/src/lean RESULT _IGNORE_RES SYMBOLIC) | |
| else() | |
| file(CREATE_LINK ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}/src/lean RESULT _IGNORE_RES SYMBOLIC) | |
| endif() | |
| install( | |
| DIRECTORY "${CMAKE_SOURCE_DIR}/" | |
| DESTINATION src/lean | |
| FILES_MATCHING | |
| PATTERN "*.lean" | |
| PATTERN "*.md" | |
| PATTERN examples EXCLUDE | |
| ) | |
| if(STAGE GREATER 0 AND INSTALL_LICENSE) | |
| install(FILES "${CMAKE_SOURCE_DIR}/../LICENSE" "${CMAKE_SOURCE_DIR}/../LICENSES" DESTINATION ".") | |
| endif() | |
| # The runtime headers are included from the build tree, both by the C++ sources and by the C files | |
| # `lean` generates, and are installed from there. Copying them as a build step instead of at | |
| # configure time keeps the copy current without a reconfiguration. | |
| add_custom_target( | |
| lean-headers | |
| COMMAND | |
| ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_SOURCE_DIR}/include/lean/lean.h" | |
| "${CMAKE_SOURCE_DIR}/include/lean/lean_gmp.h" "${CMAKE_SOURCE_DIR}/include/lean/lean_libuv.h" | |
| "${CMAKE_BINARY_DIR}/include/lean" | |
| ) | |
| set(LEAN_HEADER_CONSUMERS initialize leanshell make_stdlib) | |
| if(NOT STAGE GREATER 1) | |
| # Later stages reuse the previous stage's C++ artifacts instead of compiling them | |
| list(APPEND LEAN_HEADER_CONSUMERS leanrt leanrt_initial-exec util kernel library constructions) | |
| endif() | |
| foreach(T ${LEAN_HEADER_CONSUMERS}) | |
| add_dependencies(${T} lean-headers) | |
| endforeach() | |
| set(LEAN_INSTALL_PREFIX "" CACHE STRING "If set, set CMAKE_INSTALL_PREFIX to this value + version name") | |
| if(LEAN_INSTALL_PREFIX) | |
| string(TOLOWER ${CMAKE_SYSTEM_NAME} LOWER_SYSTEM_NAME) | |
| set( | |
| LEAN_INSTALL_SUFFIX | |
| "-${LOWER_SYSTEM_NAME}" | |
| CACHE STRING | |
| "If LEAN_INSTALL_PREFIX is set, append this value to CMAKE_INSTALL_PREFIX" | |
| ) | |
| set(CMAKE_INSTALL_PREFIX "${LEAN_INSTALL_PREFIX}/lean-${LEAN_VERSION_STRING}${LEAN_INSTALL_SUFFIX}") | |
| endif() | |
| if(USE_LAKE_CACHE AND STAGE EQUAL 1) | |
| set(LAKE_ARTIFACT_CACHE_TOML "true") | |
| else() | |
| # The build of stage2+ may depend on local changes made to src/ that are not reflected by the | |
| # commit hash in stage1/bin/lean, so we make sure to disable the global cache | |
| set(LAKE_ARTIFACT_CACHE_TOML "false") | |
| endif() | |
| # Escape for `make`. Yes, twice. | |
| string(REPLACE "$" "\\\$$" CMAKE_EXE_LINKER_FLAGS_MAKE "${CMAKE_EXE_LINKER_FLAGS}") | |
| string(REPLACE "$" "\\\$$" CMAKE_DYN_EXE_LINKER_FLAGS_MAKE "${CMAKE_DYN_EXE_LINKER_FLAGS}") | |
| string( | |
| REPLACE | |
| "$" | |
| "\\\$$" | |
| LEANCHECKER_PARANOID_LINKER_FLAGS_MAKE | |
| "${LEANCHECKER_PARANOID_LINKER_FLAGS}" | |
| ) | |
| configure_file(${LEAN_SOURCE_DIR}/stdlib.make.in ${CMAKE_BINARY_DIR}/stdlib.make) | |
| # hacky | |
| function(toml_escape IN OUTVAR) | |
| string(STRIP "${IN}" OUT) | |
| if(OUT) | |
| string(REPLACE " " "\", \"" OUT "${OUT}") | |
| set(${OUTVAR} "\"${OUT}\"" PARENT_SCOPE) | |
| endif() | |
| endfunction() | |
| string(REPLACE "ROOT" "${CMAKE_BINARY_DIR}" LEANC_CC "${LEANC_CC}") | |
| string(REPLACE "ROOT" "${CMAKE_BINARY_DIR}" LEANC_INTERNAL_FLAGS "${LEANC_INTERNAL_FLAGS}") | |
| toml_escape("${LEAN_EXTRA_OPTS}" LEAN_EXTRA_OPTS_TOML) | |
| toml_escape("${LEANC_EXTRA_CC_FLAGS}" LEAN_MORE_LEANC_ARGS_TOML) | |
| # These are passed as untraced arguments because they include the sysroot path. | |
| # However, as they are untraced, Lake will not invalidate build artifacts if the sysroot changes. | |
| # As such, care must be taken when changing sysroots. Locally, artifacts can be manually cleaned via | |
| # `clean-stdlib` and `lake cache clean`. In CI, this behavior is currently desirable. The portable | |
| # release build populates the cache, and non-release builds using the host sysroot consume it. | |
| # If this changed or reversed, the setup here would also need changing. | |
| toml_escape("${LEANC_INTERNAL_FLAGS}" LEAN_WEAK_LEANC_ARGS_TOML) | |
| if(CMAKE_BUILD_TYPE MATCHES "Debug|Release|RelWithDebInfo|MinSizeRel") | |
| set(CMAKE_BUILD_TYPE_TOML "${CMAKE_BUILD_TYPE}") | |
| else() | |
| set(CMAKE_BUILD_TYPE_TOML "Release") | |
| endif() | |
| if(USE_LAKE) | |
| configure_file(${LEAN_SOURCE_DIR}/lakefile.toml.in ${CMAKE_BINARY_DIR}/lakefile.toml) | |
| # copy for editing | |
| if(STAGE EQUAL 1) | |
| configure_file(${LEAN_SOURCE_DIR}/lakefile.toml.in ${LEAN_SOURCE_DIR}/lakefile.toml) | |
| endif() | |
| endif() |