# Maps the CMake target (not the host) to a Rust target triple for kokoro-server. # MSVC ABI: cl, clang-cl, and GNU-frontend clang++ targeting *-windows-msvc (where CMake leaves MSVC false). function(_kokoro_msvc_abi out_var) if (MSVC OR CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC") set(${out_var} TRUE PARENT_SCOPE) else() set(${out_var} FALSE PARENT_SCOPE) endif() endfunction() # Rust arch for the target, or "" when unknown or when several macOS architectures are requested. function(_kokoro_rust_arch out_var) _kokoro_msvc_abi(msvc_abi) if (msvc_abi) set(arch_id "${MSVC_CXX_ARCHITECTURE_ID}") elseif (APPLE AND CMAKE_OSX_ARCHITECTURES) set(arch_id "${CMAKE_OSX_ARCHITECTURES}") else() set(arch_id "${CMAKE_SYSTEM_PROCESSOR}") endif() string(TOLOWER "${arch_id}" arch_id) if (arch_id MATCHES "^(x64|x86_64|amd64)$") set(arch x86_64) elseif (arch_id MATCHES "^(arm64|aarch64)$") set(arch aarch64) elseif (arch_id MATCHES "^(x86|i[3-6]86)$") set(arch i686) else() set(arch "") endif() set(${out_var} "${arch}" PARENT_SCOPE) endfunction() # C library of the Linux target, from the toolchain's own triple: musl, else gnu. function(_kokoro_linux_libc out_var) set(triple "${CMAKE_C_COMPILER_TARGET}") if (NOT triple AND CMAKE_C_COMPILER AND NOT MSVC) execute_process(COMMAND "${CMAKE_C_COMPILER}" -dumpmachine OUTPUT_VARIABLE triple OUTPUT_STRIP_TRAILING_WHITESPACE RESULT_VARIABLE status ERROR_QUIET) if (NOT status EQUAL 0) set(triple "") endif() endif() if (triple MATCHES "musl") set(libc musl) else() set(libc gnu) # glibc, or a toolchain triple that does not say endif() set(${out_var} "${libc}" PARENT_SCOPE) endfunction() # Rust vendor-os(-env) suffix for the target, or "" when unsupported (Android, iOS, BSDs, ...). function(_kokoro_rust_os out_var) _kokoro_msvc_abi(msvc_abi) _kokoro_rust_arch(arch) if (WIN32 AND msvc_abi) set(os pc-windows-msvc) elseif (WIN32 AND MINGW) if (arch STREQUAL "aarch64") set(os pc-windows-gnullvm) else() set(os pc-windows-gnu) endif() elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin") set(os apple-darwin) elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux") _kokoro_linux_libc(libc) set(os "unknown-linux-${libc}") else() set(os "") endif() set(${out_var} "${os}" PARENT_SCOPE) endfunction() # Sets out_var to the Rust triple for the CMake target, or "" when there is no mapping. function(kokoro_rust_target out_var) _kokoro_rust_arch(arch) _kokoro_rust_os(os) if (arch AND os) set(${out_var} "${arch}-${os}" PARENT_SCOPE) else() set(${out_var} "" PARENT_SCOPE) endif() endfunction() # Sets out_var to the rustc Cargo will use: an explicit RUSTC, else the rustc on PATH, else the one beside cargo # (exported to Cargo through out_export, since Cargo would not find that one). A build.rustc in .cargo/config.toml is # not visible here, so the probe can only be stricter than the build. function(kokoro_rustc out_var cargo out_export) if (DEFINED ENV{RUSTC} AND NOT "$ENV{RUSTC}" STREQUAL "") set(rustc "$ENV{RUSTC}") set(export FALSE) else() find_program(_kokoro_path_rustc rustc) if (_kokoro_path_rustc) set(rustc "${_kokoro_path_rustc}") set(export FALSE) else() get_filename_component(cargo_dir "${cargo}" DIRECTORY) find_program(_kokoro_beside_rustc rustc HINTS "${cargo_dir}") if (NOT _kokoro_beside_rustc) message(FATAL_ERROR "rustc not found: ${cargo} is present but no rustc is on PATH or beside it") endif() set(rustc "${_kokoro_beside_rustc}") set(export TRUE) endif() endif() set(${out_var} "${rustc}" PARENT_SCOPE) set(${out_export} "${export}" PARENT_SCOPE) endfunction() # Fails configuration unless rustc has the standard library for the triple. function(kokoro_require_rust_std rustc triple) execute_process(COMMAND "${rustc}" --print target-libdir --target "${triple}" OUTPUT_VARIABLE libdir OUTPUT_STRIP_TRAILING_WHITESPACE RESULT_VARIABLE status ERROR_QUIET) if (NOT status EQUAL 0 OR NOT IS_DIRECTORY "${libdir}") message(FATAL_ERROR "Rust has no standard library for ${triple}: run `rustup target add ${triple}`, " "set -DKOKORO_RUST_TARGET=, or configure with -DKOKORO_BUILD_SERVER=OFF") endif() endfunction()