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 167string(REPLACE ";" " " OHOS_C_COMPILER_FLAGS "${OHOS_C_COMPILER_FLAGS}") 168 169# set the common c++ flags 170set(OHOS_CXX_COMPILER_FLAGS) 171 172# set the common asm flags 173set(OHOS_ASM_COMPILER_FLAGS "${OHOS_C_COMPILER_FLAGS}") 174 175# set the debug variant flags 176set(OHOS_DEBUG_COMPILER_FLAGS) 177list(APPEND OHOS_DEBUG_COMPILER_FLAGS -O0 -g -fno-limit-debug-info) 178string(REPLACE ";" " " OHOS_DEBUG_COMPILER_FLAGS "${OHOS_DEBUG_COMPILER_FLAGS}") 179 180# set the release variant flags 181set(OHOS_RELEASE_COMPILER_FLAGS) 182list(APPEND OHOS_RELEASE_COMPILER_FLAGS -O2) 183list(APPEND OHOS_RELEASE_COMPILER_FLAGS -DNDEBUG) 184string(REPLACE ";" " " OHOS_RELEASE_COMPILER_FLAGS "${OHOS_RELEASE_COMPILER_FLAGS}") 185 186# set the common link flags 187set(OHOS_COMMON_LINKER_FLAGS) 188list(APPEND OHOS_COMMON_LINKER_FLAGS --rtlib=compiler-rt) 189list(APPEND OHOS_COMMON_LINKER_FLAGS -fuse-ld=lld) 190 191if(OHOS_STL STREQUAL c++_static) 192 list(APPEND OHOS_COMMON_LINKER_FLAGS "-static-libstdc++") 193elseif(OHOS_STL STREQUAL none) 194 list(APPEND OHOS_CXX_COMPILER_FLAGS "-nostdinc++") 195 list(APPEND OHOS_COMMON_LINKER_FLAGS "-nostdlib++") 196elseif(OHOS_STL STREQUAL c++_shared) 197else() 198 message(FATAL_ERROR "Unsupported STL configuration: ${OHOS_STL}.") 199endif() 200 201list(APPEND OHOS_COMMON_LINKER_FLAGS 202 -Wl,--build-id=sha1 203 -Wl,--warn-shared-textrel 204 -Wl,--fatal-warnings 205 -lunwind) 206if(NOT OHOS_ALLOW_UNDEFINED_SYMBOLS) 207 list(APPEND OHOS_COMMON_LINKER_FLAGS -Wl,--no-undefined) 208endif() 209list(APPEND OHOS_COMMON_LINKER_FLAGS -Qunused-arguments -Wl,-z,noexecstack) 210string(REPLACE ";" " " OHOS_COMMON_LINKER_FLAGS "${OHOS_COMMON_LINKER_FLAGS}") 211 212# set the executable link flags 213set(OHOS_EXE_LINKER_FLAGS) 214list(APPEND OHOS_EXE_LINKER_FLAGS -Wl,--gc-sections) 215string(REPLACE ";" " " OHOS_EXE_LINKER_FLAGS "${OHOS_EXE_LINKER_FLAGS}") 216 217# set the other flags 218set(CMAKE_C_STANDARD_LIBRARIES_INIT "-lm") 219set(CMAKE_CXX_STANDARD_LIBRARIES_INIT "-lm") 220set(CMAKE_POSITION_INDEPENDENT_CODE TRUE) 221 222# set the cmake global cflags 223set(CMAKE_C_FLAGS "" CACHE STRING "Flags for all build types.") 224set(CMAKE_C_FLAGS "${OHOS_C_COMPILER_FLAGS} ${CMAKE_C_FLAGS} -D__MUSL__") 225 226set(CMAKE_C_FLAGS_DEBUG "" CACHE STRING "Flags for debug variant builds.") 227set(CMAKE_C_FLAGS_DEBUG "${OHOS_DEBUG_COMPILER_FLAGS} ${CMAKE_C_FLAGS_DEBUG}") 228 229set(CMAKE_C_FLAGS_RELEASE "" CACHE STRING "Flags for release variant builds.") 230set(CMAKE_C_FLAGS_RELEASE "${OHOS_RELEASE_COMPILER_FLAGS} ${CMAKE_C_FLAGS_RELEASE}") 231 232# set the cmake global cppflags 233set(CMAKE_CXX_FLAGS "" CACHE STRING "Flags for all build types.") 234set(CMAKE_CXX_FLAGS "${OHOS_C_COMPILER_FLAGS} ${OHOS_CXX_COMPILER_FLAGS} ${CMAKE_CXX_FLAGS} -D__MUSL__") 235 236set(CMAKE_CXX_FLAGS_DEBUG "" CACHE STRING "Flags for debug variant builds.") 237set(CMAKE_CXX_FLAGS_DEBUG "${OHOS_DEBUG_COMPILER_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG}") 238 239set(CMAKE_CXX_FLAGS_RELEASE "" CACHE STRING "Flags for release variant builds.") 240set(CMAKE_CXX_FLAGS_RELEASE "${OHOS_RELEASE_COMPILER_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE}") 241 242# set the cmake global asmflags 243set(CMAKE_ASM_FLAGS "-mrelax-relocations=no" CACHE STRING "Flags for all build types.") 244set(CMAKE_ASM_FLAGS "${OHOS_ASM_COMPILER_FLAGS} ${CMAKE_ASM_FLAGS} -D__MUSL__") 245 246set(CMAKE_ASM_FLAGS_DEBUG "" CACHE STRING "Flags for debug variant builds.") 247set(CMAKE_ASM_FLAGS_DEBUG "${OHOS_DEBUG_COMPILER_FLAGS} ${CMAKE_ASM_FLAGS_DEBUG}") 248 249set(CMAKE_ASM_FLAGS_RELEASE "" CACHE STRING "Flags for release variant builds.") 250set(CMAKE_ASM_FLAGS_RELEASE "${OHOS_RELEASE_COMPILER_FLAGS} ${CMAKE_ASM_FLAGS_RELEASE}") 251 252# set the link flags 253set(CMAKE_SHARED_LINKER_FLAGS "" CACHE STRING "Linker flags to be used to create shared libraries.") 254set(CMAKE_SHARED_LINKER_FLAGS "${OHOS_COMMON_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS}") 255 256set(CMAKE_MODULE_LINKER_FLAGS "" CACHE STRING "Linker flags to be used to create modules.") 257set(CMAKE_MODULE_LINKER_FLAGS "${OHOS_COMMON_LINKER_FLAGS} ${CMAKE_MODULE_LINKER_FLAGS}") 258 259set(CMAKE_EXE_LINKER_FLAGS "" CACHE STRING "Linker flags to be used to create executables.") 260set(CMAKE_EXE_LINKER_FLAGS "${OHOS_COMMON_LINKER_FLAGS} ${OHOS_EXE_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}") 261 262# set the executable suffix 263set(HOST_SYSTEM_EXE_SUFFIX) 264if(CMAKE_HOST_SYSTEM_NAME STREQUAL Windows) 265 set(HOST_SYSTEM_EXE_SUFFIX .exe) 266endif() 267 268# set the toolchain config. 269set(TOOLCHAIN_ROOT_PATH "${OHOS_SDK_NATIVE}/llvm") 270set(TOOLCHAIN_BIN_PATH "${OHOS_SDK_NATIVE}/llvm/bin") 271 272set(CMAKE_SYSROOT "${OHOS_SDK_NATIVE}/sysroot") 273set(CMAKE_LIBRARY_ARCHITECTURE "${OHOS_TOOLCHAIN_NAME}") 274list(APPEND CMAKE_SYSTEM_LIBRARY_PATH "/usr/lib/${OHOS_TOOLCHAIN_NAME}") 275set(CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN "${TOOLCHAIN_ROOT_PATH}") 276set(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN "${TOOLCHAIN_ROOT_PATH}") 277set(CMAKE_ASM_COMPILER_EXTERNAL_TOOLCHAIN "${TOOLCHAIN_ROOT_PATH}") 278set(CMAKE_C_COMPILER "${TOOLCHAIN_BIN_PATH}/clang${HOST_SYSTEM_EXE_SUFFIX}") 279set(CMAKE_CXX_COMPILER "${TOOLCHAIN_BIN_PATH}/clang++${HOST_SYSTEM_EXE_SUFFIX}") 280 281set(OHOS_AR "${TOOLCHAIN_BIN_PATH}/llvm-ar${HOST_SYSTEM_EXE_SUFFIX}") 282set(OHOS_RANLIB "${TOOLCHAIN_BIN_PATH}/llvm-ranlib${HOST_SYSTEM_EXE_SUFFIX}") 283set(CMAKE_AR "${OHOS_AR}" CACHE FILEPATH "Archiver") 284set(CMAKE_RANLIB "${OHOS_RANLIB}" CACHE FILEPATH "Ranlib") 285