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 14function(panda_set_flag flag) 15 set(${flag} 1 PARENT_SCOPE) 16 add_definitions("-D${flag}") 17endfunction() 18 19# For each CMake variable name, add a corresponding preprocessor definition 20# if variable evaluates to True. 21function(panda_promote_to_definitions) 22 foreach(var_name ${ARGV}) 23 if(${var_name}) 24 add_definitions("-D${var_name}") 25 endif() 26 endforeach() 27endfunction() 28 29if(CMAKE_SYSTEM_NAME STREQUAL Linux) 30 panda_set_flag(PANDA_TARGET_LINUX) 31 panda_set_flag(PANDA_TARGET_UNIX) 32 if (NOT PANDA_ENABLE_ADDRESS_SANITIZER) 33 panda_set_flag(PANDA_USE_FUTEX) 34 endif() 35elseif(CMAKE_SYSTEM_NAME STREQUAL OHOS) 36 panda_set_flag(PANDA_TARGET_OHOS) 37 panda_set_flag(PANDA_TARGET_UNIX) 38elseif(CMAKE_SYSTEM_NAME STREQUAL Windows) 39 panda_set_flag(PANDA_TARGET_WINDOWS) 40elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin) 41 panda_set_flag(PANDA_TARGET_MACOS) 42 panda_set_flag(PANDA_TARGET_UNIX) 43else() 44 message(FATAL_ERROR "Platform ${CMAKE_SYSTEM_NAME} is not supported") 45endif() 46 47if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64") 48 if(NOT PANDA_CROSS_AMD64_X86) 49 panda_set_flag(PANDA_TARGET_AMD64) 50 else() 51 panda_set_flag(PANDA_TARGET_X86) 52 endif() 53elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "i[356]86") 54 panda_set_flag(PANDA_TARGET_X86) 55elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64") 56 panda_set_flag(PANDA_TARGET_ARM64) 57elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm") 58 panda_set_flag(PANDA_TARGET_ARM32) 59 panda_promote_to_definitions( 60 PANDA_TARGET_ARM32_ABI_SOFT 61 PANDA_TARGET_ARM32_ABI_SOFTFP 62 PANDA_TARGET_ARM32_ABI_HARD 63 ) 64 if((PANDA_TARGET_ARM32_ABI_SOFT AND (PANDA_TARGET_ARM32_ABI_SOFTFP OR PANDA_TARGET_ARM32_ABI_HARD)) OR 65 (PANDA_TARGET_ARM32_ABI_SOFTFP AND (PANDA_TARGET_ARM32_ABI_SOFT OR PANDA_TARGET_ARM32_ABI_HARD)) OR 66 (PANDA_TARGET_ARM32_ABI_HARD AND (PANDA_TARGET_ARM32_ABI_SOFT OR PANDA_TARGET_ARM32_ABI_SOFTFP))) 67 message(FATAL_ERROR "Only one PANDA_TARGET_ARM32_ABI_* should be set. 68 PANDA_TARGET_ARM32_ABI_SOFT = ${PANDA_TARGET_ARM32_ABI_SOFT} 69 PANDA_TARGET_ARM32_ABI_SOFTFP = ${PANDA_TARGET_ARM32_ABI_SOFTFP} 70 PANDA_TARGET_ARM32_ABI_HARD = ${PANDA_TARGET_ARM32_ABI_HARD}") 71 elseif(NOT (PANDA_TARGET_ARM32_ABI_SOFT OR PANDA_TARGET_ARM32_ABI_SOFTFP OR PANDA_TARGET_ARM32_ABI_HARD)) 72 message(FATAL_ERROR "PANDA_TARGET_ARM32_ABI_* is not set") 73 endif() 74else() 75 message(FATAL_ERROR "Processor ${CMAKE_SYSTEM_PROCESSOR} is not supported") 76endif() 77 78if(PANDA_TARGET_AMD64 OR PANDA_TARGET_ARM64) 79 panda_set_flag(PANDA_TARGET_64) 80elseif(PANDA_TARGET_X86 OR PANDA_TARGET_ARM32) 81 panda_set_flag(PANDA_TARGET_32) 82else() 83 message(FATAL_ERROR "Unknown bitness of the target platform") 84endif() 85 86if (PANDA_TRACK_INTERNAL_ALLOCATIONS) 87 message(STATUS "Track internal allocations") 88 add_definitions(-DTRACK_INTERNAL_ALLOCATIONS=${PANDA_TRACK_INTERNAL_ALLOCATIONS}) 89endif() 90 91# Enable global register variables usage only for clang >= 9.0.0 and gcc >= 8.0.0. 92# Clang 8.0.0 doesn't support all necessary options -ffixed-<reg>. Gcc 7.5.0 freezes 93# when compiling release interpreter. 94# 95# Also calling conventions of functions that use global register variables are different: 96# clang stores and restores registers that are used for global variables in the prolog 97# and epilog of such functions and gcc doesn't do it. So it's necessary to inline all 98# function that refers to global register variables to interpreter loop. 99 100# For this reason we disable global register variables usage for clang debug builds as 101# ALWAYS_INLINE macro expands to nothing in this mode and we cannot guarantee that all 102# necessary function will be inlined. 103# 104if(PANDA_TARGET_ARM64 AND ((CMAKE_CXX_COMPILER_ID STREQUAL "Clang" 105 AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0.0 106 AND CMAKE_BUILD_TYPE MATCHES Release) 107 OR 108 (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" 109 AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0.0))) 110 panda_set_flag(PANDA_ENABLE_GLOBAL_REGISTER_VARIABLES) 111endif() 112 113if(CMAKE_BUILD_TYPE MATCHES Debug) 114 # Additional debug information about fp in each frame 115 add_compile_options(-fno-omit-frame-pointer) 116endif() 117 118if (PANDA_TARGET_MOBILE AND PANDA_TARGET_ARM64) 119 set(PANDA_ENABLE_LTO true) 120 set(PANDA_LLVM_REGALLOC pbqp) 121endif() 122 123if (PANDA_PGO_INSTRUMENT OR PANDA_PGO_OPTIMIZE) 124 if (NOT PANDA_TARGET_MOBILE OR NOT PANDA_TARGET_ARM64) 125 message(FATAL_ERROR "PGO supported only for aarch64") 126 endif() 127 128 set(PANDA_ENABLE_LTO true) 129endif() 130 131# TODO(v.cherkashi): Remove PANDA_TARGET_MOBILE_WITH_MANAGED_LIBS when the managed libs are separated form the Panda 132if(PANDA_TARGET_MOBILE_WITH_MANAGED_LIBS) 133 add_definitions(-DPANDA_TARGET_MOBILE_WITH_MANAGED_LIBS=1) 134 if(PANDA_TARGET_MOBILE) 135 panda_set_flag(PANDA_TARGET_MOBILE_WITH_NATIVE_LIBS) 136 endif() 137else() 138 add_definitions(-DPANDA_TARGET_MOBILE_WITH_MANAGED_LIBS=0) 139endif() 140 141if(PANDA_TARGET_MOBILE_WITH_NATIVE_LIBS) 142 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-gnu-statement-expression") 143 if(MOBILE_NATIVE_LIBS_SOURCE_PATH) 144 include_directories(${MOBILE_NATIVE_LIBS_SOURCE_PATH}/libc) 145 else() 146 message(FATAL_ERROR "MOBILE_NATIVE_LIBS_SOURCE_PATH is not set") 147 endif() 148endif() 149 150if(PANDA_TARGET_64) 151 panda_set_flag(PANDA_USE_32_BIT_POINTER) 152endif() 153 154if(PANDA_TARGET_LINUX) 155 execute_process(COMMAND grep PRETTY_NAME= /etc/os-release 156 OUTPUT_VARIABLE PANDA_TARGET_LINUX_DISTRO 157 OUTPUT_STRIP_TRAILING_WHITESPACE 158 ) 159 if(PANDA_TARGET_LINUX_DISTRO MATCHES "Ubuntu") 160 panda_set_flag(PANDA_TARGET_LINUX_UBUNTU) 161 endif() 162 163 if(PANDA_TARGET_LINUX_DISTRO MATCHES "Ubuntu 18\\.04") 164 panda_set_flag(PANDA_TARGET_LINUX_UBUNTU_18_04) 165 elseif(PANDA_TARGET_LINUX_DISTRO MATCHES "Ubuntu 20\\.04") 166 panda_set_flag(PANDA_TARGET_LINUX_UBUNTU_20_04) 167 endif() 168endif() 169 170set(PANDA_WITH_RUNTIME true) 171set(PANDA_WITH_COMPILER true) 172set(PANDA_WITH_TOOLCHAIN true) 173if (NOT DEFINED PANDA_WITH_BENCHMARKS) 174 set(PANDA_WITH_BENCHMARKS true) 175endif() 176set(PANDA_DEFAULT_LIB_TYPE "SHARED") 177 178option(PANDA_WITH_TESTS "Enable test targets" true) 179option(PANDA_WITH_BYTECODE_OPTIMIZER "Enable bytecode optimizer" true) 180option(PANDA_WITH_JAVA "Enable java plugin" ON) 181option(PANDA_WITH_ECMASCRIPT "Enable ecmascript plugin" ON) 182option(PANDA_WITH_ACCORD "Enable accord plugin" ON) 183option(PANDA_WITH_CANGJIE "Enable cangjie plugin" ON) 184option(PANDA_COMPILER_CFI "Support CFI information") 185option(PANDA_ENABLE_RELAYOUT_PROFILE "Enable relayout profile" OFF) 186 187if(PANDA_TARGET_WINDOWS) 188 set(PANDA_WITH_BENCHMARKS false) 189 set(PANDA_DEFAULT_LIB_TYPE "STATIC") 190 # Need to fix windows build for ecmascript. 191 set(PANDA_WITH_ECMASCRIPT false) 192endif() 193 194if(PANDA_TARGET_MACOS) 195 set(PANDA_DEFAULT_LIB_TYPE "STATIC") 196 #introduced for "std::filesystem::create_directories" 197 add_compile_options(-mmacosx-version-min=10.15) 198endif() 199 200if(PANDA_TARGET_OHOS) 201 set(PANDA_WITH_BENCHMARKS false) 202endif() 203 204if(CMAKE_BUILD_TYPE STREQUAL Debug) 205 add_definitions(-DPANDA_ENABLE_SLOW_DEBUG) 206endif() 207 208if(CMAKE_BUILD_TYPE STREQUAL FastVerify) 209 add_definitions(-DPANDA_FAST_VERIFY) 210endif() 211 212# The define is set for the build which will be delivered to customers. 213# Currently this build doesn't contain dependencies to debug libraries 214# (like libdwarf.so) 215option(PANDA_PRODUCT_BUILD "Build which will be delivered to customers" false) 216 217# TODO: merge with PANDA_PRODUCT_BUILD 218# PANDA_RELEASE_BUILD should set all relevant flags for benchmarking 219option(PANDA_RELEASE_BUILD "Build for benchmarking. Must be run with CMAKE_BUILD_TYPE=Release" false) 220 221if(PANDA_RELEASE_BUILD AND NOT (CMAKE_BUILD_TYPE STREQUAL "Release")) 222 message(FATAL_ERROR "PANDA_RELEASE_BUILD must be run with the Release build type only!") 223endif() 224 225if (PANDA_RELEASE_BUILD) 226 set(PANDA_WITH_JAVA ON) 227 set(PANDA_WITH_ECMASCRIPT ON) 228 set(PANDA_WITH_ACCORD OFF) 229 set(PANDA_WITH_CANGJIE OFF) 230endif() 231 232if (PANDA_PRODUCT_BUILD OR PANDA_RELEASE_BUILD) 233 # VIXL aarch64 with Encoder only (with no Decoder or Simulator provided) 234 set(PANDA_MINIMAL_VIXL true) 235endif() 236 237panda_promote_to_definitions( 238 PANDA_PRODUCT_BUILD 239 PANDA_WITH_COMPILER 240 PANDA_WITH_BYTECODE_OPTIMIZER 241 PANDA_MINIMAL_VIXL 242) 243 244if (("${CMAKE_BUILD_TYPE}" STREQUAL "Debug" OR 245 "${CMAKE_BUILD_TYPE}" STREQUAL "DebugDetailed" OR 246 "${CMAKE_BUILD_TYPE}" STREQUAL "FastVerify" OR 247 "${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo") AND 248 (NOT PANDA_TARGET_WINDOWS) AND 249 (NOT PANDA_ENABLE_ADDRESS_SANITIZER) AND 250 (NOT PANDA_ENABLE_UNDEFINED_BEHAVIOR_SANITIZER) AND 251 (NOT PANDA_ENABLE_THREAD_SANITIZER)) 252 # Windows do not have elf and dwarf libraries 253 # Sanitizers do not work properly with gdb 254 set(PANDA_COMPILER_CFI ON) 255endif() 256 257# libdwarf-dev lib (version: 20180129-1) in Ubuntu 18.04 has memory leaks 258# TODO(asidorov): delete the workaround when the problem is fixed 259if (PANDA_ENABLE_ADDRESS_SANITIZER AND PANDA_TARGET_LINUX_UBUNTU_18_04) 260 set(PANDA_COMPILER_CFI OFF) 261endif() 262 263# TODO: Ensure libdwarf is available when building with OHOS toolchain 264if (PANDA_TARGET_OHOS) 265 set(PANDA_COMPILER_CFI OFF) 266endif() 267 268if (PANDA_PRODUCT_BUILD) 269 set(PANDA_COMPILER_CFI OFF) 270endif() 271 272if (PANDA_COMPILER_CFI) 273 add_definitions(-DPANDA_COMPILER_CFI) 274endif() 275 276option(PANDA_CROSS_COMPILER "Enable compiler cross-compilation support" ON) 277option(PANDA_COMPILER_TARGET_X86 "Build x86-backend") 278option(PANDA_COMPILER_TARGET_X86_64 "Build x86_64-backend") 279option(PANDA_COMPILER_TARGET_AARCH32 "Build aarch32-backend") 280option(PANDA_COMPILER_TARGET_AARCH64 "Build aarch64-backend") 281# User-specified cross-toolchains: 282option(PANDA_CROSS_X86_64_TOOLCHAIN_FILE "Absolute path to X86_64 target toolchain" OFF) 283option(PANDA_CROSS_AARCH64_TOOLCHAIN_FILE "Absolute path to AARCH64 target toolchain" OFF) 284option(PANDA_CROSS_AARCH32_TOOLCHAIN_FILE "Absolute path to AARCH32 target toolchain" OFF) 285 286# true if current target supports JIT/AOT native compilation 287# TODO (asidorov, runtime): replace all uses of this option by PANDA_WITH_COMPILER 288set(PANDA_COMPILER_ENABLE TRUE) 289 290if (PANDA_TARGET_AMD64) 291 if (PANDA_CROSS_COMPILER) 292 if (HOST_TOOLS) 293 # For host-tools build support only single-target backend (with the same toolchain): 294 message(STATUS "set ${PANDA_HOST_TOOLS_TARGET_ARCH}") 295 set(PANDA_COMPILER_TARGET_${PANDA_HOST_TOOLS_TARGET_ARCH} ON) 296 else() 297 set(PANDA_COMPILER_TARGET_X86_64 ON) 298 # If `PANDA_CROSS_${arch}_TOOLCHAIN_FILE` wasn't specified, gcc-toolchain is used: 299 find_program(GCC_AARCH64_CXX "aarch64-linux-gnu-g++") 300 find_program(GCC_ARM_CXX "arm-linux-gnueabi-g++") 301 302 if (PANDA_CROSS_AARCH64_TOOLCHAIN_FILE) 303 set(PANDA_COMPILER_TARGET_AARCH64 ON) 304 message(STATUS "Specified AARCH64 toolchain: ${PANDA_CROSS_AARCH64_TOOLCHAIN_FILE}") 305 elseif (GCC_AARCH64_CXX) 306 set(PANDA_COMPILER_TARGET_AARCH64 ON) 307 message(STATUS "Detected default AARCH64 toolchain") 308 else() 309 message(STATUS "No AARCH64 toolchain found") 310 endif() 311 312 if (PANDA_CROSS_AARCH32_TOOLCHAIN_FILE) 313 set(PANDA_COMPILER_TARGET_AARCH32 ON) 314 message(STATUS "Specified AARCH32 toolchain: ${PANDA_CROSS_AARCH32_TOOLCHAIN_FILE}") 315 elseif (GCC_ARM_CXX) 316 set(PANDA_COMPILER_TARGET_AARCH32 ON) 317 message(STATUS "Detected default AARCH32 toolchain") 318 else() 319 message(STATUS "No AARCH32 toolchain found") 320 endif() 321 # TODO(dkofanov): cross-values do not support x86 322 set(PANDA_COMPILER_TARGET_X86 OFF) 323 endif() 324 else() 325 set(PANDA_COMPILER_TARGET_X86_64 ON) 326 endif() 327endif() 328 329if (PANDA_TARGET_X86) 330 set(PANDA_COMPILER_TARGET_X86 ON) 331endif() 332 333if (PANDA_TARGET_ARM32) 334 if(PANDA_TARGET_ARM32_ABI_SOFT) 335 set(PANDA_COMPILER_ENABLE FALSE) 336 else() 337 set(PANDA_COMPILER_TARGET_AARCH32 ON) 338 endif() 339endif() 340 341if (PANDA_TARGET_ARM64) 342 set(PANDA_COMPILER_TARGET_AARCH64 ON) 343endif() 344 345panda_promote_to_definitions( 346 PANDA_COMPILER_TARGET_X86 347 PANDA_COMPILER_TARGET_X86_64 348 PANDA_COMPILER_TARGET_AARCH32 349 PANDA_COMPILER_TARGET_AARCH64 350 PANDA_COMPILER_ENABLE 351) 352 353message(STATUS "PANDA_TARGET_MOBILE_WITH_MANAGED_LIBS = ${PANDA_TARGET_MOBILE_WITH_MANAGED_LIBS}") 354message(STATUS "PANDA_TARGET_UNIX = ${PANDA_TARGET_UNIX}") 355message(STATUS "PANDA_TARGET_LINUX = ${PANDA_TARGET_LINUX}") 356message(STATUS "PANDA_TARGET_MOBILE = ${PANDA_TARGET_MOBILE}") 357message(STATUS "PANDA_USE_FUTEX = ${PANDA_USE_FUTEX}") 358message(STATUS "PANDA_TARGET_WINDOWS = ${PANDA_TARGET_WINDOWS}") 359message(STATUS "PANDA_TARGET_OHOS = ${PANDA_TARGET_OHOS}") 360message(STATUS "PANDA_TARGET_MACOS = ${PANDA_TARGET_MACOS}") 361message(STATUS "PANDA_CROSS_COMPILER = ${PANDA_CROSS_COMPILER}") 362message(STATUS "PANDA_CROSS_AMD64_X86 = ${PANDA_CROSS_AMD64_X86}") 363message(STATUS "PANDA_TARGET_AMD64 = ${PANDA_TARGET_AMD64}") 364message(STATUS "PANDA_TARGET_X86 = ${PANDA_TARGET_X86}") 365message(STATUS "PANDA_TARGET_ARM64 = ${PANDA_TARGET_ARM64}") 366message(STATUS "PANDA_TARGET_ARM32 = ${PANDA_TARGET_ARM32}") 367if(PANDA_TARGET_ARM32) 368message(STATUS "PANDA_TARGET_ARM32_ABI_SOFT = ${PANDA_TARGET_ARM32_ABI_SOFT}") 369message(STATUS "PANDA_TARGET_ARM32_ABI_SOFTFP = ${PANDA_TARGET_ARM32_ABI_SOFTFP}") 370message(STATUS "PANDA_TARGET_ARM32_ABI_HARD = ${PANDA_TARGET_ARM32_ABI_HARD}") 371endif() 372message(STATUS "PANDA_TARGET_64 = ${PANDA_TARGET_64}") 373message(STATUS "PANDA_TARGET_32 = ${PANDA_TARGET_32}") 374message(STATUS "PANDA_ENABLE_GLOBAL_REGISTER_VARIABLES = ${PANDA_ENABLE_GLOBAL_REGISTER_VARIABLES}") 375message(STATUS "PANDA_ENABLE_LTO = ${PANDA_ENABLE_LTO}") 376if(PANDA_TARGET_MOBILE) 377message(STATUS "PANDA_LLVM_REGALLOC = ${PANDA_LLVM_REGALLOC}") 378endif() 379if(PANDA_TARGET_MOBILE_WITH_NATIVE_LIBS) 380message(STATUS "PANDA_TARGET_MOBILE_WITH_NATIVE_LIBS = ${PANDA_TARGET_MOBILE_WITH_NATIVE_LIBS}") 381endif() 382message(STATUS "PANDA_WITH_RUNTIME = ${PANDA_WITH_RUNTIME}") 383message(STATUS "PANDA_WITH_COMPILER = ${PANDA_WITH_COMPILER}") 384message(STATUS "PANDA_COMPILER_ENABLE = ${PANDA_COMPILER_ENABLE}") 385message(STATUS "PANDA_WITH_TOOLCHAIN = ${PANDA_WITH_TOOLCHAIN}") 386message(STATUS "PANDA_WITH_TESTS = ${PANDA_WITH_TESTS}") 387message(STATUS "PANDA_WITH_BENCHMARKS = ${PANDA_WITH_BENCHMARKS}") 388message(STATUS "PANDA_WITH_BYTECODE_OPTIMIZER = ${PANDA_WITH_BYTECODE_OPTIMIZER}") 389message(STATUS "PANDA_PGO_INSTRUMENT = ${PANDA_PGO_INSTRUMENT}") 390message(STATUS "PANDA_PGO_OPTIMIZE = ${PANDA_PGO_OPTIMIZE}") 391message(STATUS "PANDA_PRODUCT_BUILD = ${PANDA_PRODUCT_BUILD}") 392message(STATUS "PANDA_WITH_JAVA = ${PANDA_WITH_JAVA}") 393message(STATUS "PANDA_WITH_ECMASCRIPT = ${PANDA_WITH_ECMASCRIPT}") 394message(STATUS "PANDA_WITH_ACCORD = ${PANDA_WITH_ACCORD}") 395message(STATUS "PANDA_WITH_CANGJIE = ${PANDA_WITH_CANGJIE}") 396message(STATUS "PANDA_ENABLE_RELAYOUT_PROFILE = ${PANDA_ENABLE_RELAYOUT_PROFILE}") 397