From 5a915964a2d278a7e18156100c826f1ece543924 Mon Sep 17 00:00:00 2001 From: Adeel <3840695+am11@users.noreply.github.com> Date: Sat, 25 Dec 2021 02:10:15 +0200 Subject: [PATCH] Fix libs.tests subset build on tizen arm64 In our cross-build setup, we use different heuristics to identify the target platform first for ourselves, to be then able to set "toolchain file" settings. Sometimes it is a file that is known to exist in platform's sysroot directory pointed by `ROOTFS_DIR` (FreeBSD and illumos), and for Tizen and Android it is `__DistroRid` environment variable which is used for detection. This environment variable is set only during the non-portable build. In NativeExport.csproj, we call toolchain.cmake via `output-toolchain-info.cmake` using `Exec` task to prepare native compiler and linker arguments for DNNE project. It is invoked in script mode where cmake engine does not initialize its built-in modules. For the handful of build-in modules that we use in `toolchain.cmake`, we have custom macros in `output-toolchain-info.cmake`. Tizen build was broken because a) `__DistroRid` was not propagated in the `Exec` task, b) we were missing custom macro for `include_directories`. This delta addresses the issue a) by unifying how different platforms are detected in toolchain file and forgoes reliance of `__DistroRid` which is an archiac concept when non-portable builds were the only option, and issue b) by defining `include_directores` macro. --- eng/common/cross/toolchain.cmake | 34 +++++++++++++++++--------- eng/native/output-toolchain-info.cmake | 14 ++++++++++- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/eng/common/cross/toolchain.cmake b/eng/common/cross/toolchain.cmake index 6501c3a955f78..9d22fc9269303 100644 --- a/eng/common/cross/toolchain.cmake +++ b/eng/common/cross/toolchain.cmake @@ -3,18 +3,26 @@ set(CROSS_ROOTFS $ENV{ROOTFS_DIR}) set(TARGET_ARCH_NAME $ENV{TARGET_BUILD_ARCH}) if(EXISTS ${CROSS_ROOTFS}/bin/freebsd-version) set(CMAKE_SYSTEM_NAME FreeBSD) + set(FREEBSD 1) elseif(EXISTS ${CROSS_ROOTFS}/usr/platform/i86pc) set(CMAKE_SYSTEM_NAME SunOS) set(ILLUMOS 1) else() set(CMAKE_SYSTEM_NAME Linux) + set(LINUX 1) endif() set(CMAKE_SYSTEM_VERSION 1) +if(EXISTS ${CROSS_ROOTFS}/etc/tizen-release) + set(TIZEN 1) +elseif(EXISTS ${CROSS_ROOTFS}/android_platform) + set(ANDROID 1) +endif() + if(TARGET_ARCH_NAME STREQUAL "armel") set(CMAKE_SYSTEM_PROCESSOR armv7l) set(TOOLCHAIN "arm-linux-gnueabi") - if("$ENV{__DistroRid}" MATCHES "tizen.*") + if(TIZEN) set(TIZEN_TOOLCHAIN "armv7l-tizen-linux-gnueabi/9.2.0") endif() elseif(TARGET_ARCH_NAME STREQUAL "arm") @@ -33,7 +41,7 @@ elseif(TARGET_ARCH_NAME STREQUAL "arm64") else() set(TOOLCHAIN "aarch64-linux-gnu") endif() - if("$ENV{__DistroRid}" MATCHES "tizen.*") + if(TIZEN) set(TIZEN_TOOLCHAIN "aarch64-tizen-linux-gnu/9.2.0") endif() elseif(TARGET_ARCH_NAME STREQUAL "s390x") @@ -42,7 +50,7 @@ elseif(TARGET_ARCH_NAME STREQUAL "s390x") elseif(TARGET_ARCH_NAME STREQUAL "x86") set(CMAKE_SYSTEM_PROCESSOR i686) set(TOOLCHAIN "i686-linux-gnu") -elseif (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") +elseif (FREEBSD) set(CMAKE_SYSTEM_PROCESSOR "x86_64") set(triple "x86_64-unknown-freebsd12") elseif (ILLUMOS) @@ -57,7 +65,7 @@ if(DEFINED ENV{TOOLCHAIN}) endif() # Specify include paths -if(DEFINED TIZEN_TOOLCHAIN) +if(TIZEN) if(TARGET_ARCH_NAME STREQUAL "armel") include_directories(SYSTEM ${CROSS_ROOTFS}/usr/lib/gcc/${TIZEN_TOOLCHAIN}/include/c++/) include_directories(SYSTEM ${CROSS_ROOTFS}/usr/lib/gcc/${TIZEN_TOOLCHAIN}/include/c++/armv7l-tizen-linux-gnueabi) @@ -68,7 +76,7 @@ if(DEFINED TIZEN_TOOLCHAIN) endif() endif() -if("$ENV{__DistroRid}" MATCHES "android.*") +if(ANDROID) if(TARGET_ARCH_NAME STREQUAL "arm") set(ANDROID_ABI armeabi-v7a) elseif(TARGET_ARCH_NAME STREQUAL "arm64") @@ -76,7 +84,9 @@ if("$ENV{__DistroRid}" MATCHES "android.*") endif() # extract platform number required by the NDK's toolchain - string(REGEX REPLACE ".*\\.([0-9]+)-.*" "\\1" ANDROID_PLATFORM "$ENV{__DistroRid}") + file(READ "${CROSS_ROOTFS}/android_platform" RID_FILE_CONTENTS) + string(REPLACE "RID=" "" ANDROID_RID "${RID_FILE_CONTENTS}") + string(REGEX REPLACE ".*\\.([0-9]+)-.*" "\\1" ANDROID_PLATFORM "${ANDROID_RID}") set(ANDROID_TOOLCHAIN clang) set(FEATURE_EVENT_TRACE 0) # disable event trace as there is no lttng-ust package in termux repository @@ -85,7 +95,7 @@ if("$ENV{__DistroRid}" MATCHES "android.*") # include official NDK toolchain script include(${CROSS_ROOTFS}/../build/cmake/android.toolchain.cmake) -elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") +elseif(FREEBSD) # we cross-compile by instructing clang set(CMAKE_C_COMPILER_TARGET ${triple}) set(CMAKE_CXX_COMPILER_TARGET ${triple}) @@ -145,20 +155,20 @@ function(add_toolchain_linker_flag Flag) set("CMAKE_SHARED_LINKER_FLAGS${CONFIG_SUFFIX}_INIT" "${CMAKE_SHARED_LINKER_FLAGS${CONFIG_SUFFIX}_INIT} ${Flag}" PARENT_SCOPE) endfunction() -if(CMAKE_SYSTEM_NAME STREQUAL "Linux") +if(LINUX) add_toolchain_linker_flag("-Wl,--rpath-link=${CROSS_ROOTFS}/lib/${TOOLCHAIN}") add_toolchain_linker_flag("-Wl,--rpath-link=${CROSS_ROOTFS}/usr/lib/${TOOLCHAIN}") endif() if(TARGET_ARCH_NAME STREQUAL "armel") - if(DEFINED TIZEN_TOOLCHAIN) # For Tizen only + if(TIZEN) add_toolchain_linker_flag("-B${CROSS_ROOTFS}/usr/lib/gcc/${TIZEN_TOOLCHAIN}") add_toolchain_linker_flag("-L${CROSS_ROOTFS}/lib") add_toolchain_linker_flag("-L${CROSS_ROOTFS}/usr/lib") add_toolchain_linker_flag("-L${CROSS_ROOTFS}/usr/lib/gcc/${TIZEN_TOOLCHAIN}") endif() elseif(TARGET_ARCH_NAME STREQUAL "arm64") - if(DEFINED TIZEN_TOOLCHAIN) # For Tizen only + if(TIZEN) add_toolchain_linker_flag("-B${CROSS_ROOTFS}/usr/lib64/gcc/${TIZEN_TOOLCHAIN}") add_toolchain_linker_flag("-L${CROSS_ROOTFS}/lib64") add_toolchain_linker_flag("-L${CROSS_ROOTFS}/usr/lib64") @@ -177,7 +187,7 @@ endif() # Specify compile options -if((TARGET_ARCH_NAME MATCHES "^(arm|armel|arm64|s390x)$" AND NOT "$ENV{__DistroRid}" MATCHES "android.*") OR ILLUMOS) +if((TARGET_ARCH_NAME MATCHES "^(arm|armel|arm64|s390x)$" AND NOT ANDROID) OR ILLUMOS) set(CMAKE_C_COMPILER_TARGET ${TOOLCHAIN}) set(CMAKE_CXX_COMPILER_TARGET ${TOOLCHAIN}) set(CMAKE_ASM_COMPILER_TARGET ${TOOLCHAIN}) @@ -204,7 +214,7 @@ elseif(TARGET_ARCH_NAME STREQUAL "x86") add_compile_options(-Wno-error=unused-command-line-argument) endif() -if(DEFINED TIZEN_TOOLCHAIN) +if(TIZEN) if(TARGET_ARCH_NAME MATCHES "^(armel|arm64)$") add_compile_options(-Wno-deprecated-declarations) # compile-time option add_compile_options(-D__extern_always_inline=inline) # compile-time option diff --git a/eng/native/output-toolchain-info.cmake b/eng/native/output-toolchain-info.cmake index 8eed988e598ac..fc0033b887bf2 100644 --- a/eng/native/output-toolchain-info.cmake +++ b/eng/native/output-toolchain-info.cmake @@ -10,6 +10,18 @@ if (CMAKE_SCRIPT_MODE_FILE) macro(add_definitions) list(APPEND ADDED_COMPILE_OPTIONS ${ARGV}) endmacro() + macro(include_directories) + cmake_parse_arguments(INCLUDE_DIRECTORIES "" "" "SYSTEM" ${ARGN}) + if (INCLUDE_DIRECTORIES_SYSTEM) + foreach(dir ${INCLUDE_DIRECTORIES_SYSTEM}) + list(APPEND ADDED_COMPILE_OPTIONS "-isystem ${dir}") + endforeach() + else() + foreach(arg IN LISTS ARGN) + list(APPEND ADDED_COMPILE_OPTIONS "-I${arg}") + endforeach() + endif() + endmacro() endif() include(${CMAKE_CURRENT_LIST_DIR}/../common/cross/toolchain.cmake) @@ -18,4 +30,4 @@ message("") message("${TOOLCHAIN}") message("${CMAKE_SHARED_LINKER_FLAGS_INIT}") message("${ADDED_COMPILE_OPTIONS}") -message("") \ No newline at end of file +message("")