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