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