1b1994897Sopenharmony_ci# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
2b1994897Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
3b1994897Sopenharmony_ci# you may not use this file except in compliance with the License.
4b1994897Sopenharmony_ci# You may obtain a copy of the License at
5b1994897Sopenharmony_ci#
6b1994897Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0
7b1994897Sopenharmony_ci#
8b1994897Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
9b1994897Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
10b1994897Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11b1994897Sopenharmony_ci# See the License for the specific language governing permissions and
12b1994897Sopenharmony_ci# limitations under the License.
13b1994897Sopenharmony_ci
14b1994897Sopenharmony_ci# To build parts of the platform written in Panda Assembly,
15b1994897Sopenharmony_ci# we need to have the assembler binary. If we are not
16b1994897Sopenharmony_ci# cross-compiling, we are good to go. Otherwise a subset
17b1994897Sopenharmony_ci# of source tree is exposed as a separate project for
18b1994897Sopenharmony_ci# "host tools", which is built solely for host.
19b1994897Sopenharmony_ci
20b1994897Sopenharmony_cifunction(panda_configure_host_tools)
21b1994897Sopenharmony_ci    if(NOT CMAKE_CROSSCOMPILING)
22b1994897Sopenharmony_ci        return()
23b1994897Sopenharmony_ci    endif()
24b1994897Sopenharmony_ci
25b1994897Sopenharmony_ci    include(ExternalProject)
26b1994897Sopenharmony_ci
27b1994897Sopenharmony_ci    set(host_tools_build_dir "${CMAKE_CURRENT_BINARY_DIR}/host-tools-build")
28b1994897Sopenharmony_ci
29b1994897Sopenharmony_ci
30b1994897Sopenharmony_ci    if ($ENV{NPROC_PER_JOB})
31b1994897Sopenharmony_ci        set(PANDA_HOST_TOOLS_JOBS_NUMBER $ENV{NPROC_PER_JOB})
32b1994897Sopenharmony_ci    else()
33b1994897Sopenharmony_ci        set(PANDA_HOST_TOOLS_JOBS_NUMBER 16)
34b1994897Sopenharmony_ci    endif()
35b1994897Sopenharmony_ci    if (PANDA_TARGET_AMD64)
36b1994897Sopenharmony_ci        set(ARCH_NAME X86_64)
37b1994897Sopenharmony_ci        set(IRTOC_TARGET x86_64)
38b1994897Sopenharmony_ci    elseif(PANDA_TARGET_ARM64)
39b1994897Sopenharmony_ci        set(ARCH_NAME AARCH64)
40b1994897Sopenharmony_ci        set(IRTOC_TARGET arm64)
41b1994897Sopenharmony_ci    elseif(PANDA_TARGET_ARM32)
42b1994897Sopenharmony_ci        set(ARCH_NAME AARCH32)
43b1994897Sopenharmony_ci        set(IRTOC_TARGET arm32)
44b1994897Sopenharmony_ci    else()
45b1994897Sopenharmony_ci        message(FATAL_ERROR "Unsupported target")
46b1994897Sopenharmony_ci    endif()
47b1994897Sopenharmony_ci
48b1994897Sopenharmony_ci    add_custom_target(host_tools_depends)
49b1994897Sopenharmony_ci
50b1994897Sopenharmony_ci    if (PANDA_TARGET_ARM64)
51b1994897Sopenharmony_ci        set(TARGET_ARCH "AARCH64")
52b1994897Sopenharmony_ci    elseif (PANDA_TARGET_ARM32)
53b1994897Sopenharmony_ci        set(TARGET_ARCH "AARCH32")
54b1994897Sopenharmony_ci    else()
55b1994897Sopenharmony_ci        set(TARGET_ARCH "UNKNOWN")
56b1994897Sopenharmony_ci    endif()
57b1994897Sopenharmony_ci
58b1994897Sopenharmony_ci    set(HOST_TOOLS_CMAKE_ARGS
59b1994897Sopenharmony_ci        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
60b1994897Sopenharmony_ci        -DPANDA_ROOT=${PANDA_ROOT}
61b1994897Sopenharmony_ci        -DPANDA_BINARY_ROOT=${PANDA_BINARY_ROOT}
62b1994897Sopenharmony_ci        -DPANDA_ROOT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
63b1994897Sopenharmony_ci        -DPANDA_THIRD_PARTY_SOURCES_DIR=${PANDA_THIRD_PARTY_SOURCES_DIR}
64b1994897Sopenharmony_ci        -DPANDA_THIRD_PARTY_CONFIG_DIR=${PANDA_THIRD_PARTY_CONFIG_DIR}
65b1994897Sopenharmony_ci        -DPANDA_PRODUCT_BUILD=true
66b1994897Sopenharmony_ci        -DPANDA_WITH_JAVA=${PANDA_WITH_JAVA}
67b1994897Sopenharmony_ci        -DPANDA_TARGET_MOBILE_WITH_MANAGED_LIBS=${PANDA_TARGET_MOBILE_WITH_MANAGED_LIBS}
68b1994897Sopenharmony_ci        -DPANDA_WITH_ECMASCRIPT=${PANDA_WITH_ECMASCRIPT}
69b1994897Sopenharmony_ci        -DPANDA_WITH_ACCORD=${PANDA_WITH_ACCORD}
70b1994897Sopenharmony_ci        -DPANDA_WITH_CANGJIE=${PANDA_WITH_CANGJIE}
71b1994897Sopenharmony_ci        -DIRTOC_TARGET=${IRTOC_TARGET}
72b1994897Sopenharmony_ci        -DPANDA_ENABLE_ADDRESS_SANITIZER=${PANDA_ENABLE_ADDRESS_SANITIZER}
73b1994897Sopenharmony_ci        -DPANDA_ENABLE_THREAD_SANITIZER=${PANDA_ENABLE_THREAD_SANITIZER}
74b1994897Sopenharmony_ci        -DPANDA_ENABLE_CLANG_TIDY=false
75b1994897Sopenharmony_ci        -DPANDA_WITH_BYTECODE_OPTIMIZER=false
76b1994897Sopenharmony_ci        -DPANDA_WITH_TESTS=false
77b1994897Sopenharmony_ci        -DHOST_TOOLS=true
78b1994897Sopenharmony_ci        -DPANDA_HOST_TOOLS_TARGET_TOOLCHAIN=${CMAKE_TOOLCHAIN_FILE}
79b1994897Sopenharmony_ci        -DPANDA_HOST_TOOLS_TARGET_ARCH=${TARGET_ARCH}
80b1994897Sopenharmony_ci        -DTOOLCHAIN_CLANG_ROOT=${TOOLCHAIN_CLANG_ROOT}
81b1994897Sopenharmony_ci        -DTOOLCHAIN_SYSROOT=${TOOLCHAIN_SYSROOT}
82b1994897Sopenharmony_ci        -DMOBILE_NATIVE_LIBS_SOURCE_PATH=${MOBILE_NATIVE_LIBS_SOURCE_PATH}
83b1994897Sopenharmony_ci        -DPANDA_TARGET_ARM32_ABI_HARD=${PANDA_TARGET_ARM32_ABI_HARD}
84b1994897Sopenharmony_ci        -DPANDA_RELEASE_BUILD=${PANDA_RELEASE_BUILD}
85b1994897Sopenharmony_ci        --no-warn-unused-cli
86b1994897Sopenharmony_ci    )
87b1994897Sopenharmony_ci
88b1994897Sopenharmony_ci    set(HOST_TOOLS_TARGETS
89b1994897Sopenharmony_ci        cross_values
90b1994897Sopenharmony_ci        irtoc_fastpath
91b1994897Sopenharmony_ci        irtoc_interpreter
92b1994897Sopenharmony_ci        irtoc_tests
93b1994897Sopenharmony_ci        ark_asm
94b1994897Sopenharmony_ci        c_secshared
95b1994897Sopenharmony_ci        zlib
96b1994897Sopenharmony_ci    )
97b1994897Sopenharmony_ci
98b1994897Sopenharmony_ci    add_custom_target(irtoc_fastpath)
99b1994897Sopenharmony_ci    add_custom_target(irtoc_interpreter)
100b1994897Sopenharmony_ci    add_custom_target(irtoc_tests)
101b1994897Sopenharmony_ci
102b1994897Sopenharmony_ci    if(PANDA_WITH_JAVA)
103b1994897Sopenharmony_ci        list(APPEND HOST_TOOLS_TARGETS c2p irtoc_java_fastpath)
104b1994897Sopenharmony_ci        if(DEFINED PANDA_JAVA_LIBCORE_VERSION)
105b1994897Sopenharmony_ci            list(APPEND HOST_TOOLS_CMAKE_ARGS -DPANDA_JAVA_LIBCORE_VERSION=${PANDA_JAVA_LIBCORE_VERSION})
106b1994897Sopenharmony_ci        endif()
107b1994897Sopenharmony_ci    endif()
108b1994897Sopenharmony_ci
109b1994897Sopenharmony_ci    if (PANDA_WITH_ECMASCRIPT)
110b1994897Sopenharmony_ci        list(APPEND HOST_TOOLS_TARGETS es2panda)
111b1994897Sopenharmony_ci    endif()
112b1994897Sopenharmony_ci
113b1994897Sopenharmony_ci    set(BUILD_COMMAND)
114b1994897Sopenharmony_ci
115b1994897Sopenharmony_ci    # After CMake 3.15 all targets can be specified in a single `--target` option. But we use CMake 3.10 at the moment.
116b1994897Sopenharmony_ci    foreach(target ${HOST_TOOLS_TARGETS})
117b1994897Sopenharmony_ci        string(APPEND BUILD_COMMAND "${CMAKE_COMMAND} --build ${host_tools_build_dir} --target ${target} -- -j${PANDA_HOST_TOOLS_JOBS_NUMBER} && ")
118b1994897Sopenharmony_ci    endforeach()
119b1994897Sopenharmony_ci    string(APPEND BUILD_COMMAND " true")
120b1994897Sopenharmony_ci    separate_arguments(BUILD_COMMAND UNIX_COMMAND ${BUILD_COMMAND})
121b1994897Sopenharmony_ci
122b1994897Sopenharmony_ci    # Ensuring that cross-values from host-tools and from the main project are the same:
123b1994897Sopenharmony_ci    set(TARGET_CROSS_VALUES ${PANDA_BINARY_ROOT}/cross_values/generated_values/${ARCH_NAME}_values_gen.h)
124b1994897Sopenharmony_ci    set(HOST_CROSS_VALUES ${host_tools_build_dir}/cross_values/generated_values/${ARCH_NAME}_values_gen.h)
125b1994897Sopenharmony_ci    set(CHECK_COMMAND "${PANDA_ROOT}/cross_values/diff_check_values.sh ${TARGET_CROSS_VALUES} ${HOST_CROSS_VALUES}")
126b1994897Sopenharmony_ci    separate_arguments(CHECK_COMMAND UNIX_COMMAND ${CHECK_COMMAND})
127b1994897Sopenharmony_ci
128b1994897Sopenharmony_ci    ExternalProject_Add(panda_host_tools
129b1994897Sopenharmony_ci        DEPENDS           host_tools_depends
130b1994897Sopenharmony_ci        SOURCE_DIR        "${PANDA_ROOT}"
131b1994897Sopenharmony_ci        BINARY_DIR        "${host_tools_build_dir}"
132b1994897Sopenharmony_ci        BUILD_IN_SOURCE   FALSE
133b1994897Sopenharmony_ci        CONFIGURE_COMMAND ${CMAKE_COMMAND} <SOURCE_DIR> -G "${CMAKE_GENERATOR}"
134b1994897Sopenharmony_ci                          ${HOST_TOOLS_CMAKE_ARGS}
135b1994897Sopenharmony_ci        BUILD_COMMAND     ${BUILD_COMMAND}
136b1994897Sopenharmony_ci        INSTALL_COMMAND   ${CMAKE_COMMAND} -E echo "Skipping install step"
137b1994897Sopenharmony_ci        TEST_COMMAND      ${CHECK_COMMAND}
138b1994897Sopenharmony_ci    )
139b1994897Sopenharmony_ci
140b1994897Sopenharmony_ci    add_custom_target(build_host_tools
141b1994897Sopenharmony_ci        COMMAND           ${BUILD_COMMAND}
142b1994897Sopenharmony_ci        COMMAND           ${CHECK_COMMAND}
143b1994897Sopenharmony_ci        WORKING_DIRECTORY ${host_tools_build_dir}
144b1994897Sopenharmony_ci        DEPENDS           panda_host_tools
145b1994897Sopenharmony_ci    )
146b1994897Sopenharmony_ciendfunction()
147b1994897Sopenharmony_ci
148b1994897Sopenharmony_cipanda_configure_host_tools()
149