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 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 -DIRTOC_TARGET=${IRTOC_TARGET} 60 -DPANDA_ENABLE_ADDRESS_SANITIZER=${PANDA_ENABLE_ADDRESS_SANITIZER} 61 -DPANDA_ENABLE_THREAD_SANITIZER=${PANDA_ENABLE_THREAD_SANITIZER} 62 -DPANDA_WITH_BYTECODE_OPTIMIZER=true 63 -DPANDA_LLVM_BACKEND=${PANDA_LLVM_BACKEND} 64 -DPANDA_LLVM_INTERPRETER=${PANDA_LLVM_INTERPRETER} 65 -DPANDA_LLVM_FASTPATH=${PANDA_LLVM_FASTPATH} 66 # LLVM AOT is used only by target build, cross-tools do not compile anything by llvm aot 67 -DPANDA_LLVM_AOT=false 68 -DLLVM_TARGET_PATH=${LLVM_HOST_PATH} 69 -DPANDA_WITH_TESTS=false 70 -DHOST_TOOLS=true 71 -DPANDA_HOST_TOOLS_TARGET_TOOLCHAIN=${CMAKE_TOOLCHAIN_FILE} 72 -DPANDA_HOST_TOOLS_TARGET_ARCH=${TARGET_ARCH} 73 -DTOOLCHAIN_CLANG_ROOT=${TOOLCHAIN_CLANG_ROOT} 74 # The file contains list of bitcode files to create inline module 75 -DIRTOC_INT_LLVM_INL_MODULE_FILES_LIST=${IRTOC_INT_LLVM_INL_MODULE_FILES_LIST} 76 -DPANDA_LLVM_INTERPRETER_INLINING=${PANDA_LLVM_INTERPRETER_INLINING} 77 -DTOOLCHAIN_SYSROOT=${TOOLCHAIN_SYSROOT} 78 -DANDROID_ABI=${ANDROID_ABI} 79 -DANDROID_PLATFORM=${ANDROID_PLATFORM} 80 -DMOBILE_NATIVE_LIBS_SOURCE_PATH=${MOBILE_NATIVE_LIBS_SOURCE_PATH} 81 -DPANDA_TARGET_ARM32_ABI_HARD=${PANDA_TARGET_ARM32_ABI_HARD} 82 -DPANDA_MINIMAL_VIXL=false 83 --no-warn-unused-cli 84 ) 85 86 set(HOST_TOOLS_TARGETS 87 cross_values 88 irtoc_fastpath 89 irtoc_interpreter 90 irtoc_tests 91 ark_asm 92 arkbytecodeopt 93 arkcompiler 94 c_secshared 95 zlib 96 aspt_converter 97 ) 98 99 set(HOST_TOOLS_BYPRODUCTS 100 "${host_tools_build_dir}/irtoc/irtoc_fastpath/irtoc_fastpath.o" 101 "${host_tools_build_dir}/irtoc/irtoc_fastpath/irtoc_fastpath_llvm.o" 102 "${host_tools_build_dir}/irtoc/irtoc_interpreter/irtoc_interpreter.o" 103 "${host_tools_build_dir}/irtoc/irtoc_interpreter/irtoc_interpreter_llvm.o" 104 ) 105 106 if (PANDA_WITH_QUICKENER) 107 set(HOST_TOOLS_TARGETS ${HOST_TOOLS_TARGETS} arkquick) 108 endif() 109 110 add_custom_target(irtoc_fastpath) 111 add_custom_target(irtoc_interpreter) 112 add_custom_target(irtoc_tests) 113 114 if(EXISTS ${ES2PANDA_PATH}) 115 list(APPEND HOST_TOOLS_TARGETS es2panda) 116 endif() 117 118 foreach(plugin ${PLUGINS}) 119 string(TOUPPER ${plugin} plugin_name_upper) 120 string(CONCAT PANDA_WITH_PLUGIN "PANDA_WITH_" ${plugin_name_upper}) 121 list(APPEND HOST_TOOLS_CMAKE_ARGS -D${PANDA_WITH_PLUGIN}=${${PANDA_WITH_PLUGIN}}) 122 if(${${PANDA_WITH_PLUGIN}}) 123 string(CONCAT PLUGIN_SOURCE "PANDA_" ${plugin_name_upper} "_PLUGIN_SOURCE") 124 if(EXISTS ${${PLUGIN_SOURCE}}/HostTools.cmake) 125 include(${${PLUGIN_SOURCE}}/HostTools.cmake) 126 endif() 127 endif() 128 endforeach() 129 130 set(BUILD_COMMAND) 131 132 if(PANDA_USE_PREBUILT_TARGETS) 133 # Skip building when we use brebuilt targets, because we already have all needed binaries unstashed in host_tools_build_dir 134 set(BUILD_COMMAND "${CMAKE_COMMAND} -E echo Skipping build step") 135 else() 136 # After CMake 3.15 all targets can be specified in a single `--target` option. But we use CMake 3.10 at the moment. 137 foreach(target ${HOST_TOOLS_TARGETS}) 138 string(APPEND BUILD_COMMAND "${CMAKE_COMMAND} --build ${host_tools_build_dir} --target ${target} -- -j${PANDA_HOST_TOOLS_JOBS_NUMBER} && ") 139 endforeach() 140 string(APPEND BUILD_COMMAND " true") 141 endif() 142 separate_arguments(BUILD_COMMAND UNIX_COMMAND ${BUILD_COMMAND}) 143 144 # Ensuring that cross-values from host-tools and from the main project are the same: 145 set(TARGET_CROSS_VALUES ${PANDA_BINARY_ROOT}/cross_values/generated_values/${TARGET_ARCH}_values_gen.h) 146 set(HOST_CROSS_VALUES ${host_tools_build_dir}/cross_values/generated_values/${TARGET_ARCH}_values_gen.h) 147 set(CHECK_COMMAND "${PANDA_ROOT}/cross_values/diff_check_values.sh ${TARGET_CROSS_VALUES} ${HOST_CROSS_VALUES}") 148 separate_arguments(CHECK_COMMAND UNIX_COMMAND ${CHECK_COMMAND}) 149 150 # In case of dependency on panda_host_tools it's mandatory to use the panda_host_tools-build target as the dependency target 151 ExternalProject_Add(panda_host_tools 152 DEPENDS host_tools_depends 153 SOURCE_DIR "${PANDA_ROOT}" 154 BINARY_DIR "${host_tools_build_dir}" 155 BUILD_IN_SOURCE FALSE 156 CONFIGURE_COMMAND ${CMAKE_COMMAND} <SOURCE_DIR> -G "${CMAKE_GENERATOR}" 157 ${HOST_TOOLS_CMAKE_ARGS} 158 BUILD_COMMAND ${BUILD_COMMAND} 159 INSTALL_COMMAND ${CMAKE_COMMAND} -E echo "Skipping install step" 160 TEST_COMMAND ${CHECK_COMMAND} 161 BUILD_BYPRODUCTS ${HOST_TOOLS_BYPRODUCTS} 162 BUILD_ALWAYS TRUE 163 STEP_TARGETS build 164 ) 165endfunction() 166 167panda_configure_host_tools() 168