11cb0ef41Sopenharmony_ci# Copyright 2015 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci# Redistribution and use in source and binary forms, with or without
31cb0ef41Sopenharmony_ci# modification, are permitted provided that the following conditions are
41cb0ef41Sopenharmony_ci# met:
51cb0ef41Sopenharmony_ci#
61cb0ef41Sopenharmony_ci#     * Redistributions of source code must retain the above copyright
71cb0ef41Sopenharmony_ci#       notice, this list of conditions and the following disclaimer.
81cb0ef41Sopenharmony_ci#     * Redistributions in binary form must reproduce the above
91cb0ef41Sopenharmony_ci#       copyright notice, this list of conditions and the following
101cb0ef41Sopenharmony_ci#       disclaimer in the documentation and/or other materials provided
111cb0ef41Sopenharmony_ci#       with the distribution.
121cb0ef41Sopenharmony_ci#     * Neither the name of Google Inc. nor the names of its
131cb0ef41Sopenharmony_ci#       contributors may be used to endorse or promote products derived
141cb0ef41Sopenharmony_ci#       from this software without specific prior written permission.
151cb0ef41Sopenharmony_ci#
161cb0ef41Sopenharmony_ci# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171cb0ef41Sopenharmony_ci# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181cb0ef41Sopenharmony_ci# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191cb0ef41Sopenharmony_ci# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201cb0ef41Sopenharmony_ci# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211cb0ef41Sopenharmony_ci# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221cb0ef41Sopenharmony_ci# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231cb0ef41Sopenharmony_ci# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241cb0ef41Sopenharmony_ci# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251cb0ef41Sopenharmony_ci# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261cb0ef41Sopenharmony_ci# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciimport("//build/config/v8_target_cpu.gni")
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cideclare_args() {
311cb0ef41Sopenharmony_ci  # The v8 snapshot needs to be built by code that is compiled with a
321cb0ef41Sopenharmony_ci  # toolchain that matches the bit-width of the target CPU, but runs on
331cb0ef41Sopenharmony_ci  # the host.
341cb0ef41Sopenharmony_ci  v8_snapshot_toolchain = ""
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci# Try to infer the appropriate snapshot toolchain for the v8_current_cpu
381cb0ef41Sopenharmony_ci# where possible.
391cb0ef41Sopenharmony_ci#
401cb0ef41Sopenharmony_ci# Assume that v8_target_cpu (and hence v8_current_cpu) has been validated
411cb0ef41Sopenharmony_ci# as supported on the current host CPU and OS in v8_target_cpu.gni. The
421cb0ef41Sopenharmony_ci# logic below is complicated enough without also needing to do input
431cb0ef41Sopenharmony_ci# validation.
441cb0ef41Sopenharmony_ci#
451cb0ef41Sopenharmony_ci# There are test cases for this code posted as an attachment to
461cb0ef41Sopenharmony_ci# https://crbug.com/625353.
471cb0ef41Sopenharmony_ci#
481cb0ef41Sopenharmony_ci# TODO(GYP): Currently only regular (non-cross) compiles, and cross-compiles
491cb0ef41Sopenharmony_ci# from x64 hosts to Intel, ARM, or MIPS targets, are implemented. Add support
501cb0ef41Sopenharmony_ci# for the other supported configurations.
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciif (v8_snapshot_toolchain == "") {
531cb0ef41Sopenharmony_ci  if (current_os == host_os && current_cpu == host_cpu) {
541cb0ef41Sopenharmony_ci    # This is not a cross-compile, so build the snapshot with the current
551cb0ef41Sopenharmony_ci    # toolchain.
561cb0ef41Sopenharmony_ci    v8_snapshot_toolchain = current_toolchain
571cb0ef41Sopenharmony_ci  } else if (current_os == host_os && current_cpu == "x86" &&
581cb0ef41Sopenharmony_ci             host_cpu == "x64") {
591cb0ef41Sopenharmony_ci    # This is an x64 -> x86 cross-compile, but x64 hosts can usually run x86
601cb0ef41Sopenharmony_ci    # binaries built for the same OS, so build the snapshot with the current
611cb0ef41Sopenharmony_ci    # toolchain here, too.
621cb0ef41Sopenharmony_ci    v8_snapshot_toolchain = current_toolchain
631cb0ef41Sopenharmony_ci  } else if (current_os == host_os && host_cpu == "arm64" &&
641cb0ef41Sopenharmony_ci             current_cpu == "arm") {
651cb0ef41Sopenharmony_ci    # Trying to compile 32-bit arm on arm64. Good luck!
661cb0ef41Sopenharmony_ci    v8_snapshot_toolchain = current_toolchain
671cb0ef41Sopenharmony_ci  } else if (host_cpu == "x64" &&
681cb0ef41Sopenharmony_ci             (v8_current_cpu == "mips" || v8_current_cpu == "mips64")) {
691cb0ef41Sopenharmony_ci    # We don't support snapshot generation for big-endian targets,
701cb0ef41Sopenharmony_ci    # therefore snapshots will need to be built using native mksnapshot
711cb0ef41Sopenharmony_ci    # in combination with qemu
721cb0ef41Sopenharmony_ci    v8_snapshot_toolchain = current_toolchain
731cb0ef41Sopenharmony_ci  } else if (host_cpu == "arm64" && current_cpu == "x64") {
741cb0ef41Sopenharmony_ci    # Cross-build from arm64 to intel (likely on an Apple Silicon mac).
751cb0ef41Sopenharmony_ci    v8_snapshot_toolchain =
761cb0ef41Sopenharmony_ci        "//build/toolchain/${host_os}:clang_arm64_v8_$v8_current_cpu"
771cb0ef41Sopenharmony_ci  } else if (host_cpu == "x64") {
781cb0ef41Sopenharmony_ci    # This is a cross-compile from an x64 host to either a non-Intel target
791cb0ef41Sopenharmony_ci    # cpu or a different target OS. Clang will always be used by default on the
801cb0ef41Sopenharmony_ci    # host, unless this is a ChromeOS build, in which case the same toolchain
811cb0ef41Sopenharmony_ci    # (Clang or GCC) will be used for target and host by default.
821cb0ef41Sopenharmony_ci    if (is_chromeos && !is_clang) {
831cb0ef41Sopenharmony_ci      _clang = ""
841cb0ef41Sopenharmony_ci    } else {
851cb0ef41Sopenharmony_ci      _clang = "clang_"
861cb0ef41Sopenharmony_ci    }
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci    if (v8_current_cpu == "x64" || v8_current_cpu == "x86") {
891cb0ef41Sopenharmony_ci      _cpus = v8_current_cpu
901cb0ef41Sopenharmony_ci    } else if (v8_current_cpu == "arm64" || v8_current_cpu == "mips64el" ||
911cb0ef41Sopenharmony_ci               v8_current_cpu == "riscv64" || v8_current_cpu == "loong64") {
921cb0ef41Sopenharmony_ci      if (is_win && v8_current_cpu == "arm64") {
931cb0ef41Sopenharmony_ci        # set _cpus to blank for Windows ARM64 so host_toolchain could be
941cb0ef41Sopenharmony_ci        # selected as snapshot toolchain later.
951cb0ef41Sopenharmony_ci        _cpus = ""
961cb0ef41Sopenharmony_ci      } else {
971cb0ef41Sopenharmony_ci        _cpus = "x64_v8_${v8_current_cpu}"
981cb0ef41Sopenharmony_ci      }
991cb0ef41Sopenharmony_ci    } else if (v8_current_cpu == "arm" || v8_current_cpu == "mipsel") {
1001cb0ef41Sopenharmony_ci      _cpus = "x86_v8_${v8_current_cpu}"
1011cb0ef41Sopenharmony_ci    } else {
1021cb0ef41Sopenharmony_ci      # This branch should not be reached; leave _cpus blank so the assert
1031cb0ef41Sopenharmony_ci      # below will fail.
1041cb0ef41Sopenharmony_ci      _cpus = ""
1051cb0ef41Sopenharmony_ci    }
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci    if (_cpus != "") {
1081cb0ef41Sopenharmony_ci      v8_snapshot_toolchain = "//build/toolchain/${host_os}:${_clang}${_cpus}"
1091cb0ef41Sopenharmony_ci    } else if (is_win && v8_current_cpu == "arm64") {
1101cb0ef41Sopenharmony_ci      # cross compile Windows arm64 with host toolchain.
1111cb0ef41Sopenharmony_ci      v8_snapshot_toolchain = host_toolchain
1121cb0ef41Sopenharmony_ci    }
1131cb0ef41Sopenharmony_ci  }
1141cb0ef41Sopenharmony_ci}
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ciassert(v8_snapshot_toolchain != "",
1171cb0ef41Sopenharmony_ci       "Do not know how to build a snapshot for $current_toolchain " +
1181cb0ef41Sopenharmony_ci           "on $host_os $host_cpu")
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci# We reuse the snapshot toolchain for building torque and other generators to
1211cb0ef41Sopenharmony_ci# avoid building v8_libbase on the host more than once. On mips with big endian,
1221cb0ef41Sopenharmony_ci# the snapshot toolchain is the target toolchain and, hence, can't be used.
1231cb0ef41Sopenharmony_civ8_generator_toolchain = v8_snapshot_toolchain
1241cb0ef41Sopenharmony_ciif (host_cpu == "x64" &&
1251cb0ef41Sopenharmony_ci    (v8_current_cpu == "mips" || v8_current_cpu == "mips64")) {
1261cb0ef41Sopenharmony_ci  v8_generator_toolchain = "//build/toolchain/linux:clang_x64"
1271cb0ef41Sopenharmony_ci}
128