1# Copyright (c) 2021-2024 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.10 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}/third_party) 46set(PANDA_THIRD_PARTY_CONFIG_DIR ${PANDA_ROOT}/cmake/third_party) 47 48add_definitions(-DBUILD_FOLDER="${CMAKE_CURRENT_BINARY_DIR}") 49 50# List for accumulation of all core gtests binary paths. 51# It's used by CI to archive these binaries into a single artifact 52# and send it to second stage where unit tests will use them. 53set_property(GLOBAL PROPERTY stash_list "") 54 55# ----- Policies --------------------------------------------------------------- 56# Allows the target_link_libraries() command to be called from any directory 57# to add link dependencies and link interface libraries to targets created in 58# other directories 59if(POLICY CMP0079) 60 cmake_policy(SET CMP0079 NEW) 61endif() 62 63# ----- Platform definitions --------------------------------------------------- 64include(cmake/Definitions.cmake) 65 66if (NOT "${CMAKE_BUILD_TYPE}" MATCHES "Release" AND NOT PANDA_TARGET_WINDOWS) 67 # Needed for stacktrace printing 68 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -rdynamic") 69 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -rdynamic") 70endif() 71 72set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Wextra -Werror -Wshadow") 73set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -fno-exceptions") 74 75# CC-OFFNXT(bc-40028) FP 76include(cmake/toolchain/coverage/unit_tests_lcov.cmake) 77 78if(ENABLE_UNIT_TESTS_FULL_COVERAGE) 79 add_custom_target(coverage_full DEPENDS cts-assembly tests benchmarks) 80 collect_coverage_for_target( 81 TARGET_NAME coverage_full 82 EXCLUDE_DIR_PATTERN \"/usr*\";\"*/third_party/*\";\"*/build/*\"; 83 ) 84else() 85 message(STATUS "Full coverage will not be calculated (may be enabled by -DENABLE_UNIT_TESTS_FULL_COVERAGE=true ).") 86endif(ENABLE_UNIT_TESTS_FULL_COVERAGE) 87 88if(PANDA_TARGET_MACOS) 89 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mmacosx-version-min=10.13") 90endif() 91 92set(PANDA_PGO_PROFGEN_PATH "/data/local/tmp") 93 94if (PANDA_TARGET_MOBILE AND (PANDA_TARGET_ARM64 OR PANDA_TARGET_ARM32)) 95 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld") 96 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld") 97endif() 98 99if (PANDA_PGO_INSTRUMENT) 100 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-generate=${PANDA_PGO_PROFGEN_PATH}") 101 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fprofile-generate=${PANDA_PGO_PROFGEN_PATH}") 102 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-generate=${PANDA_PGO_PROFGEN_PATH}") 103endif() 104 105if (PANDA_PGO_OPTIMIZE) 106 if (NOT PANDA_PGO_PROFILE_DATA) 107 message(FATAL_ERROR "PANDA_PGO_PROFILE_DATA is not set") 108 endif() 109 110 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-use=${PANDA_PGO_PROFILE_DATA}") 111 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fprofile-use=${PANDA_PGO_PROFILE_DATA}") 112 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-use=${PANDA_PGO_PROFILE_DATA}") 113endif() 114 115if (PANDA_ENABLE_LTO) 116 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto=thin") 117 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flto=thin") 118 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto=thin") 119endif() 120 121if (PANDA_LLVM_REGALLOC) 122 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mllvm -regalloc=${PANDA_LLVM_REGALLOC}") 123endif() 124 125if ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo") 126 add_compile_options(-O2 -ggdb3 -fno-omit-frame-pointer) 127elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "FastVerify") 128 add_compile_options(-O2 -ggdb3 -fno-omit-frame-pointer) 129elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "DebugDetailed") 130 add_compile_options(-Og -ggdb3 -fno-omit-frame-pointer) 131endif() 132 133if (PANDA_THREAD_SAFETY) 134 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wthread-safety") 135endif() 136 137include(cmake/RegisterPlugins.cmake) 138 139panda_promote_to_definitions( 140 PANDA_ENABLE_RELAYOUT_PROFILE 141) 142 143# ----- Deliverable executables and libraries ---------------------------------- 144# Please override with per-target properties if your artifact should reside 145# elsewhere, like this: 146# set_target_properties(... PROPERTIES RUNTIME_OUTPUT_DIRECTORY ...) 147# Applicable for tests and all "internal" artifacts. 148if(NOT HOST_TOOLS) 149 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PANDA_BINARY_ROOT}/lib) 150 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PANDA_BINARY_ROOT}/lib) 151 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PANDA_BINARY_ROOT}/bin) 152endif() 153 154# ----- Alias for source generating targets ------------------------------------ 155add_custom_target(panda_gen_files COMMENT "Generate all sources") 156 157# ----- Panda CMake functions -------------------------------------------------- 158include(cmake/PandaCmakeFunctions.cmake) 159 160if (PANDA_LLVM_BACKEND) 161 if (PANDA_LLVM_INTERPRETER) 162 # ----- Enable LLVM Inline modules ------------------------------------- 163 include(libllvmbackend/cmake/LLVMInlineModules.cmake) 164 endif() 165 # ----- Enable LLVM Backend compiler --------------------------------------- 166 if (PANDA_BUILD_LLVM_BACKEND) 167 include(libllvmbackend/cmake/LLVM.cmake) 168 endif() 169endif () 170 171# ----- Bootstrapping (for parts of platform written in managed code ) --------- 172include(cmake/HostTools.cmake) 173 174if (PANDA_ENABLE_CCACHE) 175 # ----- Enable CCache ---------------------------------------------------------- 176 include(cmake/PandaCCache.cmake) 177 178 # ----- Enable SCCache --------------------------------------------------------- 179 include(cmake/PandaSCCache.cmake) 180endif() 181 182# ----- Documentation generation ----------------------------------------------- 183include(cmake/Doxygen.cmake) 184 185# ----- Code analysis and style ------------------------------------------------ 186include(cmake/ClangTidy.cmake) 187include(cmake/CodeStyle.cmake) 188 189# ----- Sanitizers testing ----------------------------------------------------- 190include(cmake/Sanitizers.cmake) 191 192# ----- Enable testing --------------------------------------------------------- 193 194# Umbrella target for testing: 195add_custom_target(tests COMMENT "Running all test suites") 196 197define_property(TARGET 198 PROPERTY first-level-tests-dependency 199 BRIEF_DOCS "Whether the target is a first-level dependency of tests" 200 FULL_DOCS "Whether the target is a first-level dependency of tests") 201 202add_custom_target(core_tests COMMENT "Running core test suites") 203 204# NB! ADDING THIS PROPERTY IS ALLOWED ONLY IN SPECIAL CASES. DO NOT COPY-PASTE IT. 205set_target_properties(core_tests PROPERTIES first-level-tests-dependency TRUE) 206add_dependencies(tests core_tests) 207 208include(cmake/Testing.cmake) 209 210#-----Test code quality--------------------------------------------------------- 211add_custom_target(es2panda-pre-test 212 COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/tools/es2panda/scripts/es2panda_pre_test.py --working-dir ${PANDA_ROOT} --build-root ${CMAKE_CURRENT_BINARY_DIR} 213 COMMENT "Run parser, runtime, ets-cts, function test cases, clang-tidy and clang-format." 214 JOB_POOL console 215 ) 216 217# ----- Template Based Generator ----------------------------------------------- 218include(cmake/TemplateBasedGen.cmake) 219 220# ----- Enable panda assemblies building --------------------------------------- 221include(cmake/PandaAssembly.cmake) 222 223# Some compilers use x87 fp instructions by default in 32-bit mode. 224# We need to use SSE one to correspond x86_64 build. 225if (PANDA_TARGET_X86) 226 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpmath=sse -msse3") 227endif() 228 229# ----- Targets ---------------------------------------------------------------- 230 231execute_process(COMMAND ${PANDA_ROOT}/scripts/install-third-party 232 WORKING_DIRECTORY ${PANDA_ROOT} 233 RESULT_VARIABLE THIRD_PARTY_OK) 234if (NOT THIRD_PARTY_OK EQUAL 0) 235 message(FATAL_ERROR "Unable to install required third-party dependencies") 236endif() 237 238if(PANDA_WITH_TOOLCHAIN) 239 add_subdirectory(isa) 240 241 set(SECUREC_ROOT ${PANDA_THIRD_PARTY_SOURCES_DIR}/utils_native/base) 242 add_subdirectory(${PANDA_THIRD_PARTY_CONFIG_DIR}/securec) 243 set(OPENSSL_ROOT ${PANDA_THIRD_PARTY_SOURCES_DIR}/openssl) 244 add_subdirectory(${PANDA_THIRD_PARTY_CONFIG_DIR}/openssl) 245 set(TOOLCHAIN_ROOT ${PANDA_THIRD_PARTY_SOURCES_DIR}/arkcompiler/toolchain) 246 add_subdirectory(${PANDA_THIRD_PARTY_CONFIG_DIR}/toolchain_websocket) 247 add_subdirectory(libpandabase) 248 249 set(ZLIB_ROOT ${PANDA_THIRD_PARTY_SOURCES_DIR}/zlib) 250 add_subdirectory(${PANDA_THIRD_PARTY_CONFIG_DIR}/zlib) 251 add_subdirectory(libziparchive) 252 253 add_subdirectory(libpandafile) 254 if(NOT PANDA_TARGET_WINDOWS) 255 add_subdirectory(libpandafile/external) 256 endif() 257 258 add_subdirectory(abc2program) 259 add_subdirectory(assembler) 260 add_subdirectory(disassembler) 261 262 if(PANDA_WITH_RUNTIME) 263 add_subdirectory(verification) 264 endif() 265 266 if(PANDA_WITH_COMPILER) 267 add_subdirectory(bytecode_optimizer) 268 endif() 269endif() 270 271if(PANDA_WITH_COMPILER) 272 add_compile_definitions(PANDA_WITH_IRTOC PANDA_WITH_CODEGEN) 273 add_subdirectory(cross_values) 274 if (PANDA_TARGET_X86 OR PANDA_TARGET_AMD64) 275 set(ASMJIT_STATIC TRUE) 276 set(PREV_CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 277 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-bitwise-instead-of-logical -Wno-unused-but-set-variable -Wno-deprecated-copy -Wno-unknown-warning-option") 278 add_subdirectory(${PANDA_THIRD_PARTY_SOURCES_DIR}/asmjit) 279 add_subdirectory(${PANDA_THIRD_PARTY_SOURCES_DIR}/zydis) 280 set(CMAKE_CXX_FLAGS "${PREV_CMAKE_CXX_FLAGS}") 281 endif() 282 283 if (PANDA_BUILD_LLVM_BACKEND) 284 add_subdirectory(libllvmbackend) 285 endif() 286 287 set(PREV_CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 288 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-bitwise-instead-of-logical -Wno-unknown-warning-option") 289 add_subdirectory(${PANDA_THIRD_PARTY_SOURCES_DIR}/vixl) 290 set(CMAKE_CXX_FLAGS "${PREV_CMAKE_CXX_FLAGS}") 291 add_subdirectory(irtoc) 292 add_subdirectory(compiler/optimizer/code_generator) 293 add_subdirectory(compiler) 294 set(IRTOC_INTRINSICS_YAML ${PANDA_ROOT}/irtoc/intrinsics.yaml) 295 # Irtoc is built within the host tools in cross-compiling mode. 296 if(NOT (CMAKE_CROSSCOMPILING OR PANDA_TARGET_OHOS)) 297 add_subdirectory(irtoc/backend) 298 add_subdirectory(${PANDA_THIRD_PARTY_SOURCES_DIR}/elfio) 299 endif() 300else() 301 panda_add_library(arkcompiler ${PANDA_DEFAULT_LIB_TYPE}) 302 panda_add_library(arkbytecodeopt ${PANDA_DEFAULT_LIB_TYPE}) 303 panda_add_library(arkaotmanager ${PANDA_DEFAULT_LIB_TYPE}) 304endif() 305 306if(PANDA_WITH_RUNTIME) 307 add_subdirectory(pandastdlib) 308 309 if(NOT PANDA_TARGET_WINDOWS) 310 add_subdirectory(dprof) 311 endif() 312 313 add_subdirectory(runtime) 314 315 add_subdirectory(panda) 316 317 add_subdirectory(verification/verifier) 318 319 set(ICU_ROOT ${PANDA_THIRD_PARTY_SOURCES_DIR}/icu) 320 add_subdirectory(${PANDA_THIRD_PARTY_CONFIG_DIR}/icu) 321endif() 322 323# ----- Testing ---------------------------------------------------------------- 324 325if(PANDA_WITH_TESTS) 326 add_subdirectory(${PANDA_THIRD_PARTY_SOURCES_DIR}/googletest) 327 set_target_properties(gtest PROPERTIES POSITION_INDEPENDENT_CODE ON) 328 set_target_properties(gtest_main PROPERTIES POSITION_INDEPENDENT_CODE ON) 329 set_target_properties(gmock PROPERTIES POSITION_INDEPENDENT_CODE ON) 330 331 option(RC_ENABLE_RTTI OFF) 332 if(NOT PANDA_USE_PREBUILT_TARGETS) 333 # Skip rapidcheck building when using prebuilt targets, because we don't need static 334 # libraries at this stage 335 add_subdirectory(${PANDA_THIRD_PARTY_SOURCES_DIR}/rapidcheck) 336 add_subdirectory(${PANDA_THIRD_PARTY_SOURCES_DIR}/rapidcheck/extras/gtest) 337 panda_target_compile_options(rapidcheck PUBLIC "-fexceptions" "-fno-rtti" "-fPIC") 338 endif() 339 340 add_subdirectory(tests) 341 342 add_custom_target(tests_full COMMENT "Running all test suites and code checks") 343 add_dependencies(tests_full 344 tests 345 cmake-checker 346 test-cmake-checker 347 ) 348 349 if (NOT PANDA_TARGET_MACOS) 350 add_dependencies(tests_full code-style-check) 351 endif() 352 353 add_subdirectory(scripts) 354 355 set(TYPESCRIPT_ROOT ${PANDA_THIRD_PARTY_SOURCES_DIR}/typescript) 356 add_subdirectory(${PANDA_THIRD_PARTY_CONFIG_DIR}/typescript) 357endif() 358 359# ----- Aliases ---------------------------------------------------------------- 360 361add_custom_target(panda_bins COMMENT "Build all common Panda binaries") 362add_dependencies(panda_bins panda pandasm ark_disasm ark_link paoc verifier aspt_converter) 363 364# ----- Benchmarking ----------------------------------------------------------- 365 366if(PANDA_WITH_BENCHMARKS) 367 # NB! Please do not merge benchmarks and tests unless you want to mess with 368 # slow builds, etc. If you want some coupling, you might want to make benchmarks 369 # depend on tests some day. 370 371 add_custom_target(benchmarks COMMENT "Running all benchmark suites") 372 add_subdirectory(tests/benchmarks) 373endif() 374 375# ----- Panda tools ----------------------------------------------- 376 377add_subdirectory(tools) 378 379# ----- Plugins ---------------------------------------------------------------- 380 381add_subdirectory(plugins) 382include(cmake/PostPlugins.cmake) 383 384# ----- Platforms -------------------------------------------------------------- 385 386add_subdirectory(platforms) 387 388# ----- Extras ---------------------------------------------------------------- 389 390add_subdirectory(extras) 391 392# ----- Quickening tool -------------------------------------------------------- 393 394add_subdirectory(quickener) 395 396# ----- Abc linker tool -------------------------------------------------------- 397 398if(PANDA_WITH_TOOLCHAIN) 399 add_subdirectory(static_linker) 400endif() 401 402# ----- Collecting core gtests paths for CI stash --------------------------------- 403# NB! This must be the last section! 404 405# Check that the tests target has only first-level groups as dependencies. 406# If you want to create a new first-level group of tests, mark it as 'first-level-tests-dependency' 407# using the following command set_target_properties(<target> PROPERTIES first-level-tests-dependency TRUE) 408# and don't forget to handle it on CI. 409get_target_property(dependencies tests MANUALLY_ADDED_DEPENDENCIES) 410foreach(dep IN LISTS dependencies) 411 get_target_property(first_level_dep ${dep} first-level-tests-dependency) 412 if(NOT first_level_dep) 413 message(FATAL_ERROR "Target ${dep} must not be added to tests dependencies directly.") 414 endif() 415endforeach() 416 417# Write to a file the list of all binaries 418get_property(stash_files GLOBAL PROPERTY stash_list) 419list(JOIN stash_files "\n" file_content) 420file(WRITE ${PANDA_BINARY_ROOT}/core_stash_files.txt "${file_content}\n") 421