Repository navigation
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
190 lines (170 loc) · 7.6 KB
/
Copy pathCMakeLists.txt
File metadata and controls
190 lines (170 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Interpreting C++(ICPP) - Run C++ anywhere, just like a script.
# Copyright (c) 2026 Jesse Liu <neoliu2011@gmail.com>
# SPDX-License-Identifier: Apache License, Version 2.0
# See LICENSE file in the root directory for full license text.
# cmake
cmake_minimum_required(VERSION 3.20)
# to make CMAKE_MSVC_RUNTIME_LIBRARY take effect
if(MSVC)
cmake_policy(SET CMP0091 NEW)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()
project(icpp CXX C ASM)
# is there any kind of defined variable like APPLE/MSVC for Linux ?
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
set(LINUX TRUE)
endif()
# workaround to fix wrong compiler on macOS
if(APPLE)
set(CMAKE_C_COMPILER "/usr/bin/clang" CACHE FILEPATH "" FORCE)
endif()
# build LLVM as shared library by default
set(LLVM_SHLIB ON)
# workaround to process the different python/build command line name
if(MSVC)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
# build LLVM as static library on Windows in Debug mode to make life easier
set(LLVM_SHLIB OFF)
endif()
# python3 on Windows should be invoked as python
set(PYTHON python)
set(BUILDCMD ninja)
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MD /O2 /Ob2 /Z7 -DNDEBUG")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MD /O1 /Z7")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MDd")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD /O2 /Ob2 /Z7 -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MD /O1 /Z7")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd")
set(protobuf_MSVC_STATIC_RUNTIME OFF)
#
# To make the clang-cl of Visual Studio 2026 happy...
#
# suppress clang's incompatible-pointer-types diagnostic when it is treated as an error
add_compile_options(
$<$<COMPILE_LANGUAGE:CXX>:-Wno-incompatible-pointer-types>
$<$<COMPILE_LANGUAGE:CXX>:-Wno-error=incompatible-pointer-types>
$<$<COMPILE_LANGUAGE:C>:-Wno-incompatible-pointer-types>
$<$<COMPILE_LANGUAGE:C>:-Wno-error=incompatible-pointer-types>)
# /EHsc enables standard synchronous C++ exception handling
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/EHsc>)
# apply our prebuilt libc++
set(ICXXROOT ${CMAKE_BINARY_DIR}/llvm)
set(ICXXINCLUDE ${ICXXROOT}/include)
set(ICXXLIB ${ICXXROOT}/lib)
# apply our custom built libc++ compile/link flags
string(APPEND CMAKE_CXX_FLAGS " -Xclang -stdlib=libc++ -I${ICXXINCLUDE}/c++/v1")
add_link_options(/LIBPATH:${ICXXLIB} c++.lib ${CMAKE_BINARY_DIR}/clangconf/cxxabi_msvc.lib /DEBUG)
else()
set(PYTHON python3)
set(BUILDCMD make)
# make all the symbols hidden by default
add_compile_options(-fvisibility=hidden -fvisibility-inlines-hidden)
endif()
if(LINUX)
set(ICPPCLANG ${CMAKE_BINARY_DIR}/llvm)
set(ICXXINCLUDE ${ICPPCLANG}/include)
set(ICXXLIB ${ICPPCLANG}/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-unknown-linux-gnu)
# apply our custom built libc++ compile/link flags
add_compile_options(-nostdinc++ -nostdlib++ -fPIC -I${ICXXINCLUDE}/c++/v1
-I${ICXXINCLUDE}/${CMAKE_HOST_SYSTEM_PROCESSOR}-unknown-linux-gnu/c++/v1)
add_link_options(-L${ICXXLIB} -lc -lm -lc++ @${CMAKE_SOURCE_DIR}/config/ld-${CMAKE_HOST_SYSTEM_PROCESSOR}.txt)
# as we're already using the latest clang, any further checks are redundant
set(LLVM_COMPILER_CHECKED TRUE)
# set this variable to disable llvm's cmake to do any more checks, it's really annoying...
set(LLVM_COMPILER_IS_GCC_COMPATIBLE FALSE)
endif()
# check the dependent commands whether in system PATH
set(BUILDTOOLS ${PYTHON} ${BUILDCMD})
foreach(tool IN LISTS BUILDTOOLS)
find_program(toolpath ${tool})
if(NOT toolpath)
message(FATAL_ERROR "Dependent tool '${tool}' not found, make sure it's in your system PATH.")
else()
message(STATUS "Found dependent tool '${tool}' at '${toolpath}'.")
unset(toolpath CACHE)
endif()
endforeach()
# build all dependent libraries as static
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
#
# third-party projects
#
# boost
message(STATUS "Configuring boost...")
if(MSVC)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "ARM64|aarch64")
set(MSVC_ARM64 TRUE)
# exclude both context and coroutine from Boost CMake build
set(BOOST_EXCLUDE_LIBRARIES "context;coroutine;fiber" CACHE STRING "" FORCE)
set(BOOST_CONTEXT_ENABLE_CMAKE OFF CACHE BOOL "" FORCE)
# tell Asio not to look for boost::coroutine / boost::context
add_compile_definitions(BOOST_ASIO_DISABLE_BOOST_COROUTINE)
add_compile_definitions(BOOST_ASIO_NO_DEPRECATED)
# tell Boost.Asio to avoid standalone coroutine/context binaries
set(BOOST_ASIO_NO_DEPRECATED ON CACHE BOOL "" FORCE)
set(BOOST_ASIO_HAS_BOOST_CONTEXT OFF CACHE BOOL "" FORCE)
# force Boost.Context to use C++11 std::thread implementation instead of assembly
set(BOOST_CONTEXT_IMPLEMENTATION "std" CACHE STRING "" FORCE)
# create dummy interface targets so Boost's CMake dependency checks pass
add_library(Boost::context INTERFACE IMPORTED GLOBAL)
add_library(Boost::coroutine INTERFACE IMPORTED GLOBAL)
else()
set(BOOST_CONTEXT_IMPLEMENTATION winfib)
endif()
endif()
add_subdirectory(third/boost)
# clang
message(STATUS "Configuring llvm/clang...")
# since v0.3.0, icpp has switched to use a separate clang backend to do the heavy
# compilation works in an incremental mode, so no need to patch anymore
#
# patch llvm cmake to link icpp as a shared library,
# it'll be much easier to integrate icpp to other native plugin system because of this
#execute_process(COMMAND ${PYTHON}
# ${CMAKE_CURRENT_SOURCE_DIR}/tool/llvm_cmake_patcher.py
# ${CMAKE_CURRENT_SOURCE_DIR}/third/llvm-project/llvm/cmake/modules/AddLLVM.cmake)
# icpp only supports x86_64/arm64 currently, but we're building LLVM which can be
# reused by AetherVM
set(LLVM_TARGETS_TO_BUILD "X86;ARM;AArch64;Sparc" CACHE STRING "" FORCE)
set(LLVM_ENABLE_PROJECTS "clang" CACHE STRING "" FORCE)
set(LLVM_INCLUDE_UTILS OFF CACHE BOOL "" FORCE)
set(LLVM_INCLUDE_RUNTIMES OFF CACHE BOOL "" FORCE)
set(LLVM_INCLUDE_EXAMPLES OFF CACHE BOOL "" FORCE)
set(LLVM_INCLUDE_TESTS OFF CACHE BOOL "" FORCE)
set(LLVM_INCLUDE_BENCHMARKS OFF CACHE BOOL "" FORCE)
set(LLVM_ENABLE_ZLIB "OFF" CACHE STRING "" FORCE)
set(LLVM_ENABLE_ZSTD "OFF" CACHE STRING "" FORCE)
# build llvm as a shared library to massively reduce the final package size
set(LLVM_BUILD_LLVM_DYLIB ${LLVM_SHLIB} CACHE BOOL "" FORCE)
set(LLVM_LINK_LLVM_DYLIB ${LLVM_SHLIB} CACHE BOOL "" FORCE)
set(CLANG_LINK_CLANG_DYLIB OFF CACHE BOOL "" FORCE)
if(MSVC)
set(LLVM_BUILD_LLVM_DYLIB_VIS ${LLVM_SHLIB} CACHE BOOL "" FORCE)
# fix the Support/BLAKE3/CMakeLists.txt assembler error when building llvm with ninja
set(LLVM_DISABLE_ASSEMBLY_FILES TRUE)
# remove all the non ascii characters to make cl.exe working
execute_process(COMMAND ${PYTHON}
${CMAKE_CURRENT_SOURCE_DIR}/tool/llvm_unicode_patcher.py
${CMAKE_CURRENT_SOURCE_DIR}/third/llvm-project/clang/lib/Lex/UnicodeCharSets.h)
# avoid finding vctool multiple times
execute_process(COMMAND ${PYTHON}
${CMAKE_CURRENT_SOURCE_DIR}/tool/llvm_vctool_patcher.py
${CMAKE_CURRENT_SOURCE_DIR}/third/llvm-project/llvm/lib/WindowsDriver/MSVCPaths.cpp)
endif()
add_subdirectory(third/llvm-project/llvm)
# protobuf
message(STATUS "Configuring protobuf...")
if(MSVC)
# patch abseil random cmake to cmake correctly on Windows, otherwise it fails to cmake
execute_process(COMMAND ${PYTHON}
${CMAKE_CURRENT_SOURCE_DIR}/tool/abseil_cmake_patcher.py
${CMAKE_CURRENT_SOURCE_DIR}/third/protobuf/third_party/abseil-cpp/absl/random/CMakeLists.txt)
endif()
add_subdirectory(third/protobuf)
# brotli
message(STATUS "Configuring brotli...")
add_subdirectory(third/brotli)
#
# icpp projects
#
add_subdirectory(src)