• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14cmake_minimum_required(VERSION 3.14.1 FATAL_ERROR)
15project(PANDA NONE)
16
17# Add our custom configuration types to
18# multi-configuration generators (i.e. Visual Studio):
19if(CMAKE_CONFIGURATION_TYPES)
20    list(APPEND CMAKE_CONFIGURATION_TYPES "FastVerify" "DebugDetailed")
21    list(REMOVE_DUPLICATES CMAKE_CONFIGURATION_TYPES)
22    set(CMAKE_CONFIGURATION_TYPES ${CMAKE_CONFIGURATION_TYPES}
23        CACHE STRING "CMake configuration types" FORCE)
24endif()
25
26enable_language(CXX)
27
28# NB! For God's sake do not touch the command below.
29# See https://gitlab.kitware.com/cmake/cmake/issues/16588.
30# and https://clang.llvm.org/docs/JSONCompilationDatabase.html
31set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
32
33# ----- Default flags ----------------------------------------------------------
34
35set(CMAKE_CXX_STANDARD 17)
36set(CMAKE_CXX_STANDARD_REQUIRED ON)
37set(CMAKE_CXX_EXTENSIONS OFF)
38
39# ----- Global variables -------------------------------------------------------
40# Please don't use CMAKE_SOURCE_DIR and CMAKE_BINARY_DIR to allow building
41# Panda as a cmake subdirectory. You can use the following variables if you
42# need to refer the Panda root directories
43set(PANDA_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
44set(PANDA_BINARY_ROOT ${CMAKE_CURRENT_BINARY_DIR})
45set(PANDA_THIRD_PARTY_SOURCES_DIR ${PANDA_ROOT}/ark-third-party)
46set(PANDA_THIRD_PARTY_CONFIG_DIR ${PANDA_ROOT}/cmake/ark-third-party)
47
48# ----- Platform definitions ---------------------------------------------------
49include(cmake/Definitions.cmake)
50
51if (NOT "${CMAKE_BUILD_TYPE}" MATCHES "Release" AND NOT PANDA_TARGET_WINDOWS)
52    # Needed for stacktrace printing
53    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -rdynamic")
54    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -rdynamic")
55endif()
56
57set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Wextra -Werror -Wshadow")
58set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -fno-exceptions")
59
60find_program(
61    LCOV
62    NAMES "lcov"
63    DOC "Path to lcov executable")
64if(NOT LCOV)
65    set(PANDA_ENABLE_COVERAGE false)
66endif()
67
68find_program(
69    GENHTML
70    NAMES "genhtml"
71    DOC "Path to genhtml executable")
72if(NOT GENHTML)
73    set(PANDA_ENABLE_COVERAGE false)
74endif()
75
76if(PANDA_ENABLE_COVERAGE)
77    # Set coverage options
78    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
79    set(ENABLE_BYTECODE_OPTIMIZER_COVERAGE true)
80    set(ENABLE_COMPILER_COVERAGE true)
81    add_custom_target(coverage_full DEPENDS cts-assembly tests benchmarks)  # Execute tests
82    set(ADD_COV_FLAGS --quiet --rc lcov_branch_coverage=1)
83    set(COVERAGE_DIRECTORY coverage_report)
84    set(COVERAGE_INTERMEDIATE_REPORT coverage_full.info)
85    set(COVERAGE_FULL_REPORT cov.zip)
86    add_custom_command(TARGET coverage_full POST_BUILD
87        WORKING_DIRECTORY ${PANDA_BINARY_ROOT}
88        # Update current coverage info
89        COMMAND lcov --no-external -b ${PANDA_ROOT} -d  ${PANDA_BINARY_ROOT} -c -o ${COVERAGE_INTERMEDIATE_REPORT} ${ADD_COV_FLAGS}
90        # Generate html report
91        COMMAND genhtml -o ${COVERAGE_DIRECTORY} ${COVERAGE_INTERMEDIATE_REPORT} --ignore-errors source ${ADD_COV_FLAGS}
92        COMMAND echo "Coverage report: ${PANDA_BINARY_ROOT}/${COVERAGE_DIRECTORY}/index.html"
93        # Delete temporary files with collected statistics
94        COMMAND rm ${COVERAGE_INTERMEDIATE_REPORT}
95        COMMAND find ${PANDA_BINARY_ROOT}/* -iname "*.gcda" -delete
96    )
97else()
98    message(STATUS "Full coverage will not be calculated (may be enabled by -DPANDA_ENABLE_COVERAGE=true ).")
99endif(PANDA_ENABLE_COVERAGE)
100
101if(PANDA_TARGET_MACOS)
102    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mmacosx-version-min=10.13")
103endif()
104
105set(PANDA_PGO_PROFGEN_PATH "/data/local/tmp")
106
107if (PANDA_TARGET_MOBILE AND (PANDA_TARGET_ARM64 OR PANDA_TARGET_ARM32))
108    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld")
109    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
110endif()
111
112if (PANDA_PGO_INSTRUMENT)
113    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-generate=${PANDA_PGO_PROFGEN_PATH}")
114    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fprofile-generate=${PANDA_PGO_PROFGEN_PATH}")
115    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-generate=${PANDA_PGO_PROFGEN_PATH}")
116endif()
117
118if (PANDA_PGO_OPTIMIZE)
119    if (NOT PANDA_PGO_PROFILE_DATA)
120        message(FATAL_ERROR "PANDA_PGO_PROFILE_DATA is not set")
121    endif()
122
123    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-use=${PANDA_PGO_PROFILE_DATA}")
124    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fprofile-use=${PANDA_PGO_PROFILE_DATA}")
125    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-use=${PANDA_PGO_PROFILE_DATA}")
126endif()
127
128if (PANDA_ENABLE_LTO)
129    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto=thin")
130    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flto=thin")
131    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto=thin")
132endif()
133
134if (PANDA_LLVM_REGALLOC)
135    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mllvm -regalloc=${PANDA_LLVM_REGALLOC}")
136endif()
137
138if ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
139    add_compile_options(-O2 -ggdb3 -fno-omit-frame-pointer)
140elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "FastVerify")
141    add_compile_options(-O2 -ggdb3 -fno-omit-frame-pointer)
142elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "DebugDetailed")
143    add_compile_options(-Og -ggdb3 -fno-omit-frame-pointer)
144endif()
145
146if (PANDA_THREAD_SAFETY)
147    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wthread-safety")
148endif()
149
150if (PANDA_WITH_JAVA)
151    set(PANDA_JAVA_PLUGIN_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/plugins/java")
152    set(PANDA_JAVA_PLUGIN_BINARY "${CMAKE_CURRENT_BINARY_DIR}/plugins/java")
153endif()
154
155if (PANDA_WITH_ECMASCRIPT)
156    set(PANDA_ECMASCRIPT_PLUGIN_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/plugins/ecmascript")
157    set(PANDA_ECMASCRIPT_PLUGIN_BINARY "${CMAKE_CURRENT_BINARY_DIR}/plugins/ecmascript")
158    add_definitions(-DENABLE_BYTECODE_OPT)
159    add_definitions(-DARK_INTRINSIC_SET)
160endif()
161
162if (PANDA_WITH_ACCORD)
163    set(PANDA_ACCORD_PLUGIN_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/plugins/accord")
164    set(PANDA_ACCORD_PLUGIN_BINARY "${CMAKE_CURRENT_BINARY_DIR}/plugins/accord")
165endif()
166
167if (PANDA_WITH_CANGJIE)
168    set(PANDA_CANGJIE_PLUGIN_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/plugins/cangjie")
169    set(PANDA_CANGJIE_PLUGIN_BINARY "${CMAKE_CURRENT_BINARY_DIR}/plugins/cangjie")
170endif()
171
172panda_promote_to_definitions(
173    PANDA_WITH_JAVA
174    PANDA_WITH_ECMASCRIPT
175    PANDA_WITH_ACCORD
176    PANDA_WITH_CANGJIE
177    PANDA_ENABLE_RELAYOUT_PROFILE
178)
179
180# ----- Deliverable executables and libraries ----------------------------------
181# Please override with per-target properties if your artifact should reside
182# elsewhere, like this:
183# set_target_properties(... PROPERTIES RUNTIME_OUTPUT_DIRECTORY ...)
184# Applicable for tests and all "internal" artifacts.
185if(NOT HOST_TOOLS)
186    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PANDA_BINARY_ROOT}/lib)
187    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PANDA_BINARY_ROOT}/lib)
188    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PANDA_BINARY_ROOT}/bin)
189endif()
190
191# ----- Panda CMake functions --------------------------------------------------
192include(cmake/PandaCmakeFunctions.cmake)
193
194# ----- Bootstrapping (for parts of platform written in managed code ) ---------
195include(cmake/HostTools.cmake)
196
197# ----- Enable CCache ----------------------------------------------------------
198include(cmake/PandaCCache.cmake)
199
200# ----- Documentation generation -----------------------------------------------
201include(cmake/Doxygen.cmake)
202
203# ----- Code analysis and style ------------------------------------------------
204include(cmake/ClangTidy.cmake)
205include(cmake/CodeStyle.cmake)
206
207# ----- Sanitizers testing -----------------------------------------------------
208include(cmake/Sanitizers.cmake)
209
210# ----- Enable testing ---------------------------------------------------------
211
212# Umbrella target for testing:
213add_custom_target(tests COMMENT "Running all test suites")
214include(cmake/Testing.cmake)
215
216# ----- Template Based Generator -----------------------------------------------
217include(cmake/TemplateBasedGen.cmake)
218
219# ----- Enable panda assemblies building ---------------------------------------
220include(cmake/PandaAssembly.cmake)
221
222# Some compilers use x87 fp instructions by default in 32-bit mode.
223# We need to use SSE one to correspond x86_64 build.
224if (PANDA_TARGET_X86)
225    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpmath=sse -msse3")
226endif()
227
228# ----- Targets ----------------------------------------------------------------
229
230execute_process(COMMAND ${PANDA_ROOT}/scripts/install-third-party
231                WORKING_DIRECTORY ${PANDA_ROOT}
232                RESULT_VARIABLE THIRD_PARTY_OK)
233if (NOT THIRD_PARTY_OK EQUAL 0)
234    message(FATAL_ERROR "Unable to install required third-party dependencies")
235endif()
236
237if(PANDA_WITH_TOOLCHAIN)
238    add_subdirectory(isa)
239
240    set(SECUREC_ROOT ${PANDA_THIRD_PARTY_SOURCES_DIR}/utils_native/base)
241    add_subdirectory(${PANDA_THIRD_PARTY_CONFIG_DIR}/securec)
242    add_subdirectory(libpandabase)
243
244    set(ZLIB_ROOT ${PANDA_THIRD_PARTY_SOURCES_DIR}/zlib)
245    add_subdirectory(${PANDA_THIRD_PARTY_CONFIG_DIR}/zlib)
246    add_subdirectory(libziparchive)
247
248    add_subdirectory(libpandafile)
249    if(NOT PANDA_TARGET_WINDOWS)
250        add_subdirectory(libpandafile/external)
251    endif()
252
253    add_subdirectory(assembler)
254    add_subdirectory(disassembler)
255
256    if(PANDA_WITH_RUNTIME)
257        add_subdirectory(verification)
258    endif()
259
260    if(PANDA_WITH_COMPILER)
261        add_subdirectory(bytecode_optimizer)
262    endif()
263endif()
264
265if(PANDA_WITH_COMPILER)
266    add_subdirectory(cross_values)
267    if (PANDA_TARGET_X86 OR PANDA_TARGET_AMD64)
268        set(ASMJIT_STATIC TRUE)
269        add_subdirectory(${PANDA_THIRD_PARTY_SOURCES_DIR}/asmjit)
270        add_subdirectory(${PANDA_THIRD_PARTY_SOURCES_DIR}/zydis)
271    endif()
272
273    add_subdirectory(${PANDA_THIRD_PARTY_SOURCES_DIR}/vixl)
274    add_subdirectory(irtoc)
275    add_subdirectory(compiler/optimizer/code_generator)
276    add_subdirectory(compiler)
277    set(IRTOC_INTRINSICS_YAML ${PANDA_ROOT}/irtoc/intrinsics.yaml)
278    # Irtoc is built within the host tools in cross-compiling mode.
279    if(NOT (CMAKE_CROSSCOMPILING OR PANDA_TARGET_OHOS))
280        add_subdirectory(irtoc/backend)
281        add_subdirectory(${PANDA_THIRD_PARTY_SOURCES_DIR}/elfio)
282    endif()
283else()
284    add_library(arkcompiler ${PANDA_DEFAULT_LIB_TYPE})
285    add_library(arkbytecodeopt ${PANDA_DEFAULT_LIB_TYPE})
286    add_library(arkaotmanager ${PANDA_DEFAULT_LIB_TYPE})
287endif()
288
289if(PANDA_WITH_RUNTIME)
290    add_subdirectory(pandastdlib)
291
292    if(NOT PANDA_TARGET_WINDOWS)
293        add_subdirectory(dprof)
294    endif()
295
296    add_subdirectory(runtime)
297
298    add_subdirectory(panda)
299
300    add_subdirectory(verification/verifier)
301endif()
302
303# ----- Testing ----------------------------------------------------------------
304
305if(PANDA_WITH_TESTS)
306    add_subdirectory(${PANDA_THIRD_PARTY_SOURCES_DIR}/googletest)
307    if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND PANDA_USE_32_BIT_POINTER AND PANDA_TARGET_AMD64)
308        set_target_properties(gtest PROPERTIES POSITION_INDEPENDENT_CODE ON)
309        set_target_properties(gtest_main PROPERTIES POSITION_INDEPENDENT_CODE ON)
310        set_target_properties(gmock PROPERTIES POSITION_INDEPENDENT_CODE ON)
311    endif()
312
313    add_subdirectory(${PANDA_THIRD_PARTY_SOURCES_DIR}/rapidcheck)
314    add_subdirectory(${PANDA_THIRD_PARTY_SOURCES_DIR}/rapidcheck/extras/catch)
315    target_compile_options(rapidcheck PUBLIC "-fexceptions" "-frtti" "-fPIC")
316
317    add_subdirectory(tests)
318    add_subdirectory(tools)
319
320    add_custom_target(tests_full COMMENT "Running all test suites and code checks")
321    add_dependencies(tests_full
322        check_concurrency_format
323        check_atomic_format
324        tests
325        tools
326    )
327    if(NOT PANDA_TARGET_MACOS)
328        add_dependencies(tests_full clang_format)
329    endif()
330
331endif()
332
333# ----- Aliases ----------------------------------------------------------------
334
335add_custom_target(panda_bins COMMENT "Build all common Panda binaries")
336add_dependencies(panda_bins panda pandasm ark_disasm paoc verifier)
337
338
339# ----- Benchmarking -----------------------------------------------------------
340
341if(PANDA_WITH_BENCHMARKS)
342    # NB! Please do not merge benchmarks and tests unless you want to mess with
343    # slow builds, etc. If you want some coupling, you might want to make benchmarks
344    # depend on tests some day.
345
346    add_custom_target(benchmarks COMMENT "Running all benchmark suites")
347    add_subdirectory(tests/benchmarks)
348    add_subdirectory(experiments/benchmarks)
349endif()
350
351# ----- Plugins ----------------------------------------------------------------
352
353add_subdirectory(plugins)
354include(cmake/PostPlugins.cmake)
355
356# ----- Platforms --------------------------------------------------------------
357
358add_subdirectory(platforms)
359
360# ----- Fuzzing ----------------------------------------------------------------
361
362add_subdirectory(fuzzing)
363
364# ----- Quickening tool --------------------------------------------------------
365
366add_subdirectory(quickener)
367