junrushao commented on code in PR #92: URL: https://github.com/apache/tvm-ffi/pull/92#discussion_r2411904248
########## cmake/Utils/MachineTriple.cmake: ########## @@ -0,0 +1,303 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# ~~~ +# get_target_triple(out_var) +# Determine the target machine triple and store it in the variable named by +# `out_var`. +# +# The result is determined by (in order of preference): +# - querying CMake's own configuration, +# - asking the compiler directly via `-dumpmachine` or `--print-target-triple`, +# - using CMake's `CMAKE_LIBRARY_ARCHITECTURE` variable (Debian/Ubuntu multiarch hint), +# - synthesizing from system information. +# ~~~ +# machine_triple.cmake +function (get_target_triple out_var) + # cmake-lint: disable=R0911,R0912,R0915 + # --- 1) Prefer CMake's own notion (e.g. when --target was used) --- + foreach (lang C CXX) + if (CMAKE_${lang}_COMPILER_TARGET) + set(${out_var} + "${CMAKE_${lang}_COMPILER_TARGET}" + PARENT_SCOPE + ) + return() + endif () + endforeach () + + # --- 2) Ask the compiler directly (works for Clang/GCC, Android NDK, Emscripten) --- + set(cc "${CMAKE_C_COMPILER}") + if (NOT cc AND CMAKE_CXX_COMPILER) + set(cc "${CMAKE_CXX_COMPILER}") + endif () + if (cc) + execute_process( + COMMAND "${cc}" -dumpmachine + OUTPUT_VARIABLE ret + OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET + ) + if (NOT ret) + execute_process( + COMMAND "${cc}" --print-target-triple + OUTPUT_VARIABLE ret + OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET + ) + endif () + if (ret) + set(${out_var} + "${ret}" + PARENT_SCOPE + ) + return() + endif () + endif () + + # --- 3) Platform-specific construction --- + + # 3a) Emscripten (toolchains usually set CMAKE_SYSTEM_NAME to Emscripten) + if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten" OR EMSCRIPTEN) + set(${out_var} + "wasm32-unknown-emscripten" + PARENT_SCOPE + ) + return() + endif () + + # 3b) Android (derive from ANDROID_ABI / ANDROID_PLATFORM) + if (ANDROID OR CMAKE_SYSTEM_NAME STREQUAL "Android") + set(arch "") + set(abi "${ANDROID_ABI}") + if (abi STREQUAL "armeabi-v7a") + set(arch "armv7a") + set(base "linux-androideabi") + elseif (abi STREQUAL "arm64-v8a") + set(arch "aarch64") + set(base "linux-android") + elseif (abi STREQUAL "x86") + set(arch "i686") + set(base "linux-android") + elseif (abi STREQUAL "x86_64") + set(arch "x86_64") + set(base "linux-android") + elseif (abi STREQUAL "riscv64") + set(arch "riscv64") + set(base "linux-android") + else () + # Fallback from processor if ABI isn't set + set(arch "${CMAKE_SYSTEM_PROCESSOR}") + string(TOLOWER "${arch}" arch) + if (arch MATCHES "armv7") + set(arch "armv7a") + set(base "linux-androideabi") + elseif (arch MATCHES "aarch64|arm64") + set(arch "aarch64") + set(base "linux-android") + elseif (arch MATCHES "x86_64|amd64") + set(arch "x86_64") + set(base "linux-android") + elseif (arch MATCHES "i[3-6]86|x86") + set(arch "i686") + set(base "linux-android") + elseif (arch MATCHES "riscv64") + set(arch "riscv64") + set(base "linux-android") + endif () + endif () + + # Append API level if we can (e.g. aarch64-linux-android21) + set(api "") + if (DEFINED ANDROID_PLATFORM AND NOT "${ANDROID_PLATFORM}" STREQUAL "") + string(REGEX REPLACE "android-?" "" api "${ANDROID_PLATFORM}") + endif () + + if (arch STREQUAL "armv7a") + set(ret "${arch}-${base}") + else () + set(ret "${arch}-${base}") + endif () + if (api) + set(ret "${ret}${api}") + endif () + set(${out_var} + "${ret}" + PARENT_SCOPE + ) + return() + endif () + + # 3c) Apple iOS (device & simulator). Works for Xcode + toolchains. + if (APPLE AND (CMAKE_SYSTEM_NAME STREQUAL "iOS" OR CMAKE_OSX_SYSROOT MATCHES "[iI]phone")) + # Choose first arch if multi-arch is set + set(archs "${CMAKE_OSX_ARCHITECTURES}") + if (NOT archs) + set(archs "${CMAKE_SYSTEM_PROCESSOR}") + endif () + list(GET archs 0 arch) + string(TOLOWER "${arch}" _arch_l) + if (_arch_l MATCHES "aarch64|arm64|arm64e") # iOS uses 'arm64' in triples + set(arch "arm64") + elseif (_arch_l MATCHES "x86_64|amd64") + set(arch "x86_64") + endif () + + # Simulator? + set(is_sim OFF) + if (CMAKE_OSX_SYSROOT MATCHES "simulator" OR CMAKE_XCODE_EFFECTIVE_PLATFORMS MATCHES + "simulator" + ) + set(is_sim ON) + endif () + + # Deployment target (best-effort) + set(ios_ver "") + foreach (maybe_ver CMAKE_OSX_DEPLOYMENT_TARGET CMAKE_IOS_DEPLOYMENT_TARGET + IOS_DEPLOYMENT_TARGET + ) + if (DEFINED ${maybe_ver} AND NOT "${${maybe_ver}}" STREQUAL "") + set(ios_ver "${${maybe_ver}}") + break() + endif () + endforeach () + + if (is_sim) + set(ret "${arch}-apple-ios${ios_ver}-simulator") Review Comment: should this be `-simulator` or `-sim`? looks like `-simulator` exists: https://github.com/search?q=repo%3Allvm%2Fllvm-project+arm64-apple-ios-simulator&type=code, while many other projects such as `tokenizer-cpp` uses `-sim` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
