1# Copyright (c) 2021 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14# Description: The configuration of toolchain file supplied to cmake, which specifies 15# locations for compilers and toolchain utilities, and other target 16# platform and compiler related information. 17 18cmake_minimum_required(VERSION 3.6.0) 19set(CMAKE_SYSTEM_VERSION 1) 20set(CMAKE_ASM_COMPILER_VERSION 15.0.4) 21 22if(DEFINED OHOS_SDK_NATIVE_TOOLCHAIN_DEFINED) 23 return() 24endif() 25set(OHOS_SDK_NATIVE_TOOLCHAIN_DEFINED true) 26 27# Set OHOS_SDK_NATIVE 28get_filename_component(OHOS_SDK_NATIVE "${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE) 29file(TO_CMAKE_PATH "${OHOS_SDK_NATIVE}" OHOS_SDK_NATIVE) 30 31# Sdk native version 32file(STRINGS "${OHOS_SDK_NATIVE}/oh-uni-package.json" NATIVE_VER REGEX "\"version\":.*") 33string(REGEX REPLACE "\"version\":(.*)$" "\\1" SDK_NATIVE_VERSION "${NATIVE_VER}") 34string(STRIP "${SDK_NATIVE_VERSION}" SDK_NATIVE_VERSION) 35 36# Common default settings 37set(OHOS OHOS) 38set(CMAKE_SYSTEM_NAME OHOS) 39 40if(NOT DEFINED OHOS_PLATFORM_LEVEL) 41 set(OHOS_PLATFORM_LEVEL 1) 42endif() 43 44if(NOT DEFINED OHOS_TOOLCHAIN) 45 set(OHOS_TOOLCHAIN clang) 46endif() 47 48if(NOT DEFINED OHOS_STL) 49 set(OHOS_STL c++_shared) 50endif() 51 52if(NOT DEFINED OHOS_PIE) 53 set(OHOS_PIE TRUE) 54endif() 55 56if(NOT DEFINED OHOS_ARM_NEON) 57 set(OHOS_ARM_NEON thumb) 58endif() 59 60# set the ABI 61if(NOT DEFINED OHOS_ARCH) 62 set(OHOS_ARCH arm64-v8a) 63endif() 64 65# set the undefined symbols 66if(DEFINED OHOS_NO_UNDEFINED) 67 if(NOT DEFINED OHOS_ALLOW_UNDEFINED_SYMBOLS) 68 set(OHOS_ALLOW_UNDEFINED_SYMBOLS "${OHOS_NO_UNDEFINED}") 69 endif() 70endif() 71 72# set the ccache 73if(DEFINED SDK_NATIVE_CCACHE AND NOT DEFINED OHOS_CCACHE) 74 set(OHOS_CCACHE "${SDK_NATIVE_CCACHE}") 75endif() 76 77# set the sdk native platform 78include(${CMAKE_CURRENT_LIST_DIR}/sdk_native_platforms.cmake) 79if(NOT DEFINED OHOS_SDK_NATIVE_PLATFORM) 80 set(OHOS_SDK_NATIVE_PLATFORM "ohos-${SDK_NATIVE_MIN_PLATFORM_LEVEL}") 81endif() 82 83# set the sdk native platform level 84string(REPLACE "ohos-" "" OHOS_SDK_NATIVE_PLATFORM_LEVEL ${OHOS_SDK_NATIVE_PLATFORM}) 85 86# set find executable programs on the host system path 87set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) 88set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) 89set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) 90set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) 91list(APPEND CMAKE_FIND_ROOT_PATH "${OHOS_SDK_NATIVE}") 92 93# set the arch abi 94set(CMAKE_OHOS_ARCH_ABI ${OHOS_ARCH}) 95 96# set arch diff property ... 97if(OHOS_ARCH STREQUAL arm64-v8a) 98 set(OHOS_TOOLCHAIN_NAME aarch64-linux-ohos) 99 set(OHOS_LLVM ${OHOS_TOOLCHAIN_NAME}) 100 set(CMAKE_SYSTEM_PROCESSOR aarch64) 101elseif(OHOS_ARCH STREQUAL armeabi-v7a) 102 set(OHOS_TOOLCHAIN_NAME arm-linux-ohos) 103 set(OHOS_LLVM ${OHOS_TOOLCHAIN_NAME}) 104 set(CMAKE_SYSTEM_PROCESSOR arm) 105elseif(OHOS_ARCH STREQUAL x86_64) 106 set(OHOS_TOOLCHAIN_NAME x86_64-linux-ohos) 107 set(OHOS_LLVM ${OHOS_TOOLCHAIN_NAME}) 108 set(CMAKE_SYSTEM_PROCESSOR x86_64) 109else() 110 message(FATAL_ERROR "unrecognized ${OHOS_ARCH}") 111endif() 112 113set(CMAKE_C_COMPILER_TARGET ${OHOS_LLVM}) 114set(CMAKE_CXX_COMPILER_TARGET ${OHOS_LLVM}) 115set(CMAKE_ASM_COMPILER_TARGET ${OHOS_LLVM}) 116 117# Export configurable variables for the try_compile() command. 118set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES 119 OHOS_SDK_NATIVE 120 OHOS_TOOLCHAIN 121 OHOS_ARCH 122 OHOS_PLATFORM) 123 124# Set the common c flags 125set(OHOS_C_COMPILER_FLAGS) 126list(APPEND OHOS_C_COMPILER_FLAGS 127 -fdata-sections 128 -ffunction-sections 129 -funwind-tables 130 -fstack-protector-strong 131 -no-canonical-prefixes 132 -fno-addrsig 133 -Wa,--noexecstack) 134if(OHOS_DISABLE_FORMAT_STRING_CHECKS) 135 list(APPEND OHOS_C_COMPILER_FLAGS -Wno-error=format-security) 136else() 137 list(APPEND OHOS_C_COMPILER_FLAGS -Wformat -Werror=format-security) 138endif() 139if (OHOS_ARCH STREQUAL armeabi-v7a) 140 list(APPEND OHOS_C_COMPILER_FLAGS -march=armv7a) 141endif() 142if (CMAKE_BUILD_TYPE STREQUAL normal) 143 list(APPEND OHOS_C_COMPILER_FLAGS -g) 144endif() 145if(OHOS_ENABLE_ASAN STREQUAL ON) 146 list(APPEND OHOS_C_COMPILER_FLAGS 147 -shared-libasan 148 -fsanitize=address 149 -fno-omit-frame-pointer 150 -fsanitize-recover=address) 151 if(DEFINED OHOS_ASAN_BLACKLIST) 152 list(APPEND OHOS_C_COMPILER_FLAGS -fsanitize-blacklist="${OHOS_ASAN_BLACKLIST}") 153 endif() 154endif() 155 156if (OHOS_ENABLE_HWASAN STREQUAL ON AND OHOS_ARCH STREQUAL arm64-v8a) 157 list(APPEND OHOS_C_COMPILER_FLAGS 158 -shared-libasan 159 -fsanitize=hwaddress 160 -fno-emulated-tls 161 -fno-omit-frame-pointer) 162 if (DEFINED OHOS_ASAN_BLACKLIST) 163 list(APPEND OHOS_C_COMPILER_FLAGS -fsanitize-blacklist="${OHOS_ASAN_BLACKLIST}") 164 endif() 165endif() 166 167if(OHOS_ENABLE_TSAN STREQUAL ON AND OHOS_ARCH STREQUAL arm64-v8a) 168 list(APPEND OHOS_C_COMPILER_FLAGS 169 -shared-libsan 170 -fsanitize=thread 171 -fno-omit-frame-pointer) 172 if(DEFINED OHOS_TSAN_BLACKLIST) 173 list(APPEND OHOS_C_COMPILER_FLAGS -fsanitize-blacklist="${OHOS_TSAN_BLACKLIST}") 174 endif() 175endif() 176 177string(REPLACE ";" " " OHOS_C_COMPILER_FLAGS "${OHOS_C_COMPILER_FLAGS}") 178 179# set the common c++ flags 180set(OHOS_CXX_COMPILER_FLAGS) 181 182# set the common asm flags 183set(OHOS_ASM_COMPILER_FLAGS "${OHOS_C_COMPILER_FLAGS}") 184 185# set the debug variant flags 186set(OHOS_DEBUG_COMPILER_FLAGS) 187list(APPEND OHOS_DEBUG_COMPILER_FLAGS -O0 -g -fno-limit-debug-info) 188string(REPLACE ";" " " OHOS_DEBUG_COMPILER_FLAGS "${OHOS_DEBUG_COMPILER_FLAGS}") 189 190# set the release variant flags 191set(OHOS_RELEASE_COMPILER_FLAGS) 192list(APPEND OHOS_RELEASE_COMPILER_FLAGS -O2) 193list(APPEND OHOS_RELEASE_COMPILER_FLAGS -DNDEBUG) 194string(REPLACE ";" " " OHOS_RELEASE_COMPILER_FLAGS "${OHOS_RELEASE_COMPILER_FLAGS}") 195 196# set the common link flags 197set(OHOS_COMMON_LINKER_FLAGS) 198list(APPEND OHOS_COMMON_LINKER_FLAGS --rtlib=compiler-rt) 199list(APPEND OHOS_COMMON_LINKER_FLAGS -fuse-ld=lld) 200 201if(OHOS_STL STREQUAL c++_static) 202 list(APPEND OHOS_COMMON_LINKER_FLAGS "-static-libstdc++") 203elseif(OHOS_STL STREQUAL none) 204 list(APPEND OHOS_CXX_COMPILER_FLAGS "-nostdinc++") 205 list(APPEND OHOS_COMMON_LINKER_FLAGS "-nostdlib++") 206elseif(OHOS_STL STREQUAL c++_shared) 207else() 208 message(FATAL_ERROR "Unsupported STL configuration: ${OHOS_STL}.") 209endif() 210 211list(APPEND OHOS_COMMON_LINKER_FLAGS 212 -Wl,--build-id=sha1 213 -Wl,--warn-shared-textrel 214 -Wl,--fatal-warnings 215 -lunwind) 216if(NOT OHOS_ALLOW_UNDEFINED_SYMBOLS) 217 list(APPEND OHOS_COMMON_LINKER_FLAGS -Wl,--no-undefined) 218endif() 219list(APPEND OHOS_COMMON_LINKER_FLAGS -Qunused-arguments -Wl,-z,noexecstack) 220string(REPLACE ";" " " OHOS_COMMON_LINKER_FLAGS "${OHOS_COMMON_LINKER_FLAGS}") 221 222# set the executable link flags 223set(OHOS_EXE_LINKER_FLAGS) 224list(APPEND OHOS_EXE_LINKER_FLAGS -Wl,--gc-sections) 225string(REPLACE ";" " " OHOS_EXE_LINKER_FLAGS "${OHOS_EXE_LINKER_FLAGS}") 226 227# set the other flags 228set(CMAKE_C_STANDARD_LIBRARIES_INIT "-lm") 229set(CMAKE_CXX_STANDARD_LIBRARIES_INIT "-lm") 230set(CMAKE_POSITION_INDEPENDENT_CODE TRUE) 231 232# set the cmake global cflags 233set(CMAKE_C_FLAGS "" CACHE STRING "Flags for all build types.") 234set(CMAKE_C_FLAGS "${OHOS_C_COMPILER_FLAGS} ${CMAKE_C_FLAGS} -D__MUSL__") 235 236set(CMAKE_C_FLAGS_DEBUG "" CACHE STRING "Flags for debug variant builds.") 237set(CMAKE_C_FLAGS_DEBUG "${OHOS_DEBUG_COMPILER_FLAGS} ${CMAKE_C_FLAGS_DEBUG}") 238 239set(CMAKE_C_FLAGS_RELEASE "" CACHE STRING "Flags for release variant builds.") 240set(CMAKE_C_FLAGS_RELEASE "${OHOS_RELEASE_COMPILER_FLAGS} ${CMAKE_C_FLAGS_RELEASE}") 241 242# set the cmake global cppflags 243set(CMAKE_CXX_FLAGS "" CACHE STRING "Flags for all build types.") 244set(CMAKE_CXX_FLAGS "${OHOS_C_COMPILER_FLAGS} ${OHOS_CXX_COMPILER_FLAGS} ${CMAKE_CXX_FLAGS} -D__MUSL__") 245 246set(CMAKE_CXX_FLAGS_DEBUG "" CACHE STRING "Flags for debug variant builds.") 247set(CMAKE_CXX_FLAGS_DEBUG "${OHOS_DEBUG_COMPILER_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG}") 248 249set(CMAKE_CXX_FLAGS_RELEASE "" CACHE STRING "Flags for release variant builds.") 250set(CMAKE_CXX_FLAGS_RELEASE "${OHOS_RELEASE_COMPILER_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE}") 251 252# set the cmake global asmflags 253set(CMAKE_ASM_FLAGS "" CACHE STRING "Flags for all build types.") 254set(CMAKE_ASM_FLAGS "${OHOS_ASM_COMPILER_FLAGS} ${CMAKE_ASM_FLAGS} -D__MUSL__") 255 256set(CMAKE_ASM_FLAGS_DEBUG "" CACHE STRING "Flags for debug variant builds.") 257set(CMAKE_ASM_FLAGS_DEBUG "${OHOS_DEBUG_COMPILER_FLAGS} ${CMAKE_ASM_FLAGS_DEBUG}") 258 259set(CMAKE_ASM_FLAGS_RELEASE "" CACHE STRING "Flags for release variant builds.") 260set(CMAKE_ASM_FLAGS_RELEASE "${OHOS_RELEASE_COMPILER_FLAGS} ${CMAKE_ASM_FLAGS_RELEASE}") 261 262# set the link flags 263set(CMAKE_SHARED_LINKER_FLAGS "" CACHE STRING "Linker flags to be used to create shared libraries.") 264set(CMAKE_SHARED_LINKER_FLAGS "${OHOS_COMMON_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS}") 265 266set(CMAKE_MODULE_LINKER_FLAGS "" CACHE STRING "Linker flags to be used to create modules.") 267set(CMAKE_MODULE_LINKER_FLAGS "${OHOS_COMMON_LINKER_FLAGS} ${CMAKE_MODULE_LINKER_FLAGS}") 268 269set(CMAKE_EXE_LINKER_FLAGS "" CACHE STRING "Linker flags to be used to create executables.") 270set(CMAKE_EXE_LINKER_FLAGS "${OHOS_COMMON_LINKER_FLAGS} ${OHOS_EXE_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}") 271 272# set the executable suffix 273set(HOST_SYSTEM_EXE_SUFFIX) 274if(CMAKE_HOST_SYSTEM_NAME STREQUAL Windows) 275 set(HOST_SYSTEM_EXE_SUFFIX .exe) 276endif() 277 278# set the toolchain config. 279set(TOOLCHAIN_ROOT_PATH "${OHOS_SDK_NATIVE}/llvm") 280set(TOOLCHAIN_BIN_PATH "${OHOS_SDK_NATIVE}/llvm/bin") 281 282set(CMAKE_SYSROOT "${OHOS_SDK_NATIVE}/sysroot") 283set(CMAKE_LIBRARY_ARCHITECTURE "${OHOS_TOOLCHAIN_NAME}") 284list(APPEND CMAKE_SYSTEM_LIBRARY_PATH "/usr/lib/${OHOS_TOOLCHAIN_NAME}") 285set(CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN "${TOOLCHAIN_ROOT_PATH}") 286set(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN "${TOOLCHAIN_ROOT_PATH}") 287set(CMAKE_ASM_COMPILER_EXTERNAL_TOOLCHAIN "${TOOLCHAIN_ROOT_PATH}") 288set(CMAKE_C_COMPILER "${TOOLCHAIN_BIN_PATH}/clang${HOST_SYSTEM_EXE_SUFFIX}") 289set(CMAKE_CXX_COMPILER "${TOOLCHAIN_BIN_PATH}/clang++${HOST_SYSTEM_EXE_SUFFIX}") 290 291set(OHOS_AR "${TOOLCHAIN_BIN_PATH}/llvm-ar${HOST_SYSTEM_EXE_SUFFIX}") 292set(OHOS_RANLIB "${TOOLCHAIN_BIN_PATH}/llvm-ranlib${HOST_SYSTEM_EXE_SUFFIX}") 293set(CMAKE_AR "${OHOS_AR}" CACHE FILEPATH "Archiver") 294set(CMAKE_RANLIB "${OHOS_RANLIB}" CACHE FILEPATH "Ranlib") 295set(UNIX TRUE CACHE BOOL FROCE) 296