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 14# To build parts of the platform written in Panda Assembly, 15# we need to have the assembler binary. If we are not 16# cross-compiling, we are good to go. Otherwise a subset 17# of source tree is exposed as a separate project for 18# "host tools", which is built solely for host. 19 20function(panda_configure_host_tools) 21 if(NOT CMAKE_CROSSCOMPILING) 22 return() 23 endif() 24 25 include(ExternalProject) 26 27 set(host_tools_build_dir "${CMAKE_CURRENT_BINARY_DIR}/host-tools-build") 28 29 30 if ($ENV{NPROC_PER_JOB}) 31 set(PANDA_HOST_TOOLS_JOBS_NUMBER $ENV{NPROC_PER_JOB}) 32 else() 33 set(PANDA_HOST_TOOLS_JOBS_NUMBER 16) 34 endif() 35 if (PANDA_TARGET_AMD64) 36 set(TARGET_ARCH X86_64) 37 set(IRTOC_TARGET x86_64) 38 elseif(PANDA_TARGET_ARM64) 39 set(TARGET_ARCH AARCH64) 40 set(IRTOC_TARGET arm64) 41 elseif(PANDA_TARGET_ARM32) 42 set(TARGET_ARCH AARCH32) 43 set(IRTOC_TARGET arm32) 44 else() 45 message(FATAL_ERROR "Unsupported target") 46 endif() 47 48 add_custom_target(host_tools_depends) 49 50 set(HOST_TOOLS_CMAKE_ARGS 51 -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} 52 -DPANDA_ROOT=${PANDA_ROOT} 53 -DPANDA_BINARY_ROOT=${PANDA_BINARY_ROOT} 54 -DPANDA_ROOT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR} 55 -DPANDA_THIRD_PARTY_SOURCES_DIR=${PANDA_THIRD_PARTY_SOURCES_DIR} 56 -DPANDA_THIRD_PARTY_CONFIG_DIR=${PANDA_THIRD_PARTY_CONFIG_DIR} 57 -DPANDA_PRODUCT_BUILD=${PANDA_PRODUCT_BUILD} 58 -DPANDA_TARGET_MOBILE_WITH_MANAGED_LIBS=${PANDA_TARGET_MOBILE_WITH_MANAGED_LIBS} 59 -DPANDA_TARGET_CPU_FEATURES=${PANDA_TARGET_CPU_FEATURES} 60 -DIRTOC_TARGET=${IRTOC_TARGET} 61 -DPANDA_ENABLE_ADDRESS_SANITIZER=${PANDA_ENABLE_ADDRESS_SANITIZER} 62 -DPANDA_ENABLE_THREAD_SANITIZER=${PANDA_ENABLE_THREAD_SANITIZER} 63 -DPANDA_WITH_BYTECODE_OPTIMIZER=true 64 -DPANDA_LLVM_BACKEND=${PANDA_LLVM_BACKEND} 65 -DPANDA_LLVM_INTERPRETER=${PANDA_LLVM_INTERPRETER} 66 -DPANDA_LLVM_FASTPATH=${PANDA_LLVM_FASTPATH} 67 -DPANDA_BUILD_LLVM_BINARIES=${PANDA_BUILD_LLVM_BINARIES} 68 -DPANDA_BUILD_LLVM_BINARIES_PATH_ROOT=${PANDA_BUILD_LLVM_BINARIES_PATH_ROOT} 69 # LLVM AOT is used only by target build, cross-tools do not compile anything by llvm aot 70 -DPANDA_LLVM_AOT=false 71 -DLLVM_TARGET_PATH=${LLVM_HOST_PATH} 72 -DPANDA_WITH_TESTS=false 73 -DHOST_TOOLS=true 74 -DPANDA_HOST_TOOLS_TARGET_TOOLCHAIN=${CMAKE_TOOLCHAIN_FILE} 75 -DPANDA_HOST_TOOLS_TARGET_ARCH=${TARGET_ARCH} 76 -DTOOLCHAIN_CLANG_ROOT=${TOOLCHAIN_CLANG_ROOT} 77 # The file contains list of bitcode files to create inline module 78 -DIRTOC_INT_LLVM_INL_MODULE_FILES_LIST=${IRTOC_INT_LLVM_INL_MODULE_FILES_LIST} 79 -DPANDA_LLVM_INTERPRETER_INLINING=${PANDA_LLVM_INTERPRETER_INLINING} 80 -DTOOLCHAIN_SYSROOT=${TOOLCHAIN_SYSROOT} 81 -DANDROID_ABI=${ANDROID_ABI} 82 -DANDROID_PLATFORM=${ANDROID_PLATFORM} 83 -DMOBILE_NATIVE_LIBS_SOURCE_PATH=${MOBILE_NATIVE_LIBS_SOURCE_PATH} 84 -DPANDA_TARGET_ARM32_ABI_HARD=${PANDA_TARGET_ARM32_ABI_HARD} 85 -DPANDA_MINIMAL_VIXL=false 86 --no-warn-unused-cli 87 ) 88 89 set(HOST_TOOLS_TARGETS 90 cross_values 91 irtoc_fastpath 92 irtoc_interpreter 93 irtoc_tests 94 ark_asm 95 arkbytecodeopt 96 arkcompiler 97 c_secshared 98 zlib 99 aspt_converter 100 ) 101 102 set(HOST_TOOLS_BYPRODUCTS 103 "${host_tools_build_dir}/irtoc/irtoc_fastpath/irtoc_fastpath.o" 104 "${host_tools_build_dir}/irtoc/irtoc_fastpath/irtoc_fastpath_llvm.o" 105 "${host_tools_build_dir}/irtoc/irtoc_interpreter/irtoc_interpreter.o" 106 "${host_tools_build_dir}/irtoc/irtoc_interpreter/irtoc_interpreter_llvm.o" 107 ) 108 109 if (PANDA_WITH_QUICKENER) 110 set(HOST_TOOLS_TARGETS ${HOST_TOOLS_TARGETS} arkquick) 111 endif() 112 113 add_custom_target(irtoc_fastpath) 114 add_custom_target(irtoc_interpreter) 115 add_custom_target(irtoc_tests) 116 117 if(EXISTS ${ES2PANDA_PATH}) 118 list(APPEND HOST_TOOLS_TARGETS es2panda) 119 endif() 120 121 foreach(plugin ${PLUGINS}) 122 string(TOUPPER ${plugin} plugin_name_upper) 123 string(CONCAT PANDA_WITH_PLUGIN "PANDA_WITH_" ${plugin_name_upper}) 124 list(APPEND HOST_TOOLS_CMAKE_ARGS -D${PANDA_WITH_PLUGIN}=${${PANDA_WITH_PLUGIN}}) 125 if(${${PANDA_WITH_PLUGIN}}) 126 string(CONCAT PLUGIN_SOURCE "PANDA_" ${plugin_name_upper} "_PLUGIN_SOURCE") 127 if(EXISTS ${${PLUGIN_SOURCE}}/HostTools.cmake) 128 include(${${PLUGIN_SOURCE}}/HostTools.cmake) 129 endif() 130 endif() 131 endforeach() 132 133 set(BUILD_COMMAND) 134 135 if(PANDA_USE_PREBUILT_TARGETS) 136 # Skip building when we use brebuilt targets, because we already have all needed binaries unstashed in host_tools_build_dir 137 set(BUILD_COMMAND "${CMAKE_COMMAND} -E echo Skipping build step") 138 else() 139 # After CMake 3.15 all targets can be specified in a single `--target` option. But we use CMake 3.10 at the moment. 140 foreach(target ${HOST_TOOLS_TARGETS}) 141 string(APPEND BUILD_COMMAND "${CMAKE_COMMAND} --build ${host_tools_build_dir} --target ${target} -- -j${PANDA_HOST_TOOLS_JOBS_NUMBER} && ") 142 endforeach() 143 string(APPEND BUILD_COMMAND " true") 144 endif() 145 separate_arguments(BUILD_COMMAND UNIX_COMMAND ${BUILD_COMMAND}) 146 147 # Ensuring that cross-values from host-tools and from the main project are the same: 148 set(TARGET_CROSS_VALUES ${PANDA_BINARY_ROOT}/cross_values/generated_values/${TARGET_ARCH}_values_gen.h) 149 set(HOST_CROSS_VALUES ${host_tools_build_dir}/cross_values/generated_values/${TARGET_ARCH}_values_gen.h) 150 set(CHECK_COMMAND "${PANDA_ROOT}/cross_values/diff_check_values.sh ${TARGET_CROSS_VALUES} ${HOST_CROSS_VALUES}") 151 separate_arguments(CHECK_COMMAND UNIX_COMMAND ${CHECK_COMMAND}) 152 153 # In case of dependency on panda_host_tools it's mandatory to use the panda_host_tools-build target as the dependency target 154 ExternalProject_Add(panda_host_tools 155 DEPENDS host_tools_depends 156 SOURCE_DIR "${PANDA_ROOT}" 157 BINARY_DIR "${host_tools_build_dir}" 158 BUILD_IN_SOURCE FALSE 159 CONFIGURE_COMMAND ${CMAKE_COMMAND} <SOURCE_DIR> -G "${CMAKE_GENERATOR}" 160 ${HOST_TOOLS_CMAKE_ARGS} 161 BUILD_COMMAND ${BUILD_COMMAND} 162 INSTALL_COMMAND ${CMAKE_COMMAND} -E echo "Skipping install step" 163 TEST_COMMAND ${CHECK_COMMAND} 164 BUILD_BYPRODUCTS ${HOST_TOOLS_BYPRODUCTS} 165 BUILD_ALWAYS TRUE 166 STEP_TARGETS build 167 ) 168endfunction() 169 170panda_configure_host_tools() 171