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