1# 2# Copyright (c) 2019, The OpenThread Authors. 3# All rights reserved. 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are met: 7# 1. Redistributions of source code must retain the above copyright 8# notice, this list of conditions and the following disclaimer. 9# 2. Redistributions in binary form must reproduce the above copyright 10# notice, this list of conditions and the following disclaimer in the 11# documentation and/or other materials provided with the distribution. 12# 3. Neither the name of the copyright holder nor the 13# names of its contributors may be used to endorse or promote products 14# derived from this software without specific prior written permission. 15# 16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26# POSSIBILITY OF SUCH DAMAGE. 27# 28 29cmake_policy(SET CMP0048 NEW) 30cmake_minimum_required(VERSION 3.10.2) 31 32file(READ .default-version OT_DEFAULT_VERSION) 33string(STRIP ${OT_DEFAULT_VERSION} OT_DEFAULT_VERSION) 34 35project(openthread VERSION ${OT_DEFAULT_VERSION}) 36include(CTest) 37 38option(OT_BUILD_EXECUTABLES "Build executables" ON) 39option(OT_COVERAGE "enable coverage" OFF) 40set(OT_EXTERNAL_MBEDTLS "" CACHE STRING "Specify external mbedtls library") 41option(OT_MBEDTLS_THREADING "enable mbedtls threading" OFF) 42 43add_library(ot-config INTERFACE) 44add_library(ot-config-ftd INTERFACE) 45add_library(ot-config-mtd INTERFACE) 46add_library(ot-config-radio INTERFACE) 47set(CMAKE_CXX_EXTENSIONS OFF) 48set(CMAKE_CXX_STANDARD 11) 49set(CMAKE_C_EXTENSIONS OFF) 50set(CMAKE_C_STANDARD 99) 51 52message(STATUS "OpenThread Source Directory: ${PROJECT_SOURCE_DIR}") 53 54target_include_directories(ot-config INTERFACE 55 ${PROJECT_SOURCE_DIR}/include 56 ${PROJECT_SOURCE_DIR}/src 57 ${PROJECT_SOURCE_DIR}/src/core 58 ${PROJECT_SOURCE_DIR}/src/include 59) 60 61include(TestBigEndian) 62TEST_BIG_ENDIAN(OT_BIG_ENDIAN) 63if(OT_BIG_ENDIAN) 64 target_compile_definitions(ot-config INTERFACE "BYTE_ORDER_BIG_ENDIAN=1") 65endif() 66 67include("${PROJECT_SOURCE_DIR}/etc/cmake/options.cmake") 68include("${PROJECT_SOURCE_DIR}/etc/cmake/functions.cmake") 69 70if(NOT CMAKE_BUILD_TYPE) 71 # Check if this is a top-level CMake. 72 # If it is not, do not set the CMAKE_BUILD_TYPE because OpenThread is a part of something bigger. 73 if ("${CMAKE_PROJECT_NAME}" STREQUAL "openthread") 74 set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "default build type: Debug" FORCE) 75 endif () 76endif() 77 78if (CMAKE_BUILD_TYPE) 79 message(STATUS "OpenThread CMake build type: ${CMAKE_BUILD_TYPE}") 80endif () 81 82if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "(Apple)?[Cc]lang") 83 option(OT_COMPILE_WARNING_AS_ERROR "whether to include -Werror -pedantic-errors with gcc-compatible compilers") 84 if (OT_COMPILE_WARNING_AS_ERROR) 85 set(OT_CFLAGS -Werror -pedantic-errors) 86 endif() 87 88 if(OT_COVERAGE) 89 target_compile_definitions(ot-config INTERFACE "OPENTHREAD_ENABLE_COVERAGE=1") 90 target_compile_options(ot-config INTERFACE -g -O0 --coverage) 91 target_link_libraries(ot-config INTERFACE --coverage) 92 endif() 93 94 set(OT_CFLAGS 95 $<$<COMPILE_LANGUAGE:C>:${OT_CFLAGS} -Wall -Wextra -Wshadow> 96 $<$<COMPILE_LANGUAGE:CXX>:${OT_CFLAGS} -Wall -Wextra -Wshadow -Wno-c++14-compat -fno-exceptions> 97 $<$<CXX_COMPILER_ID:Clang>:-Wc99-extensions> 98 ) 99endif() 100 101set(OT_PACKAGE_NAME "OPENTHREAD" CACHE STRING "OpenThread Package Name") 102target_compile_definitions(ot-config INTERFACE "PACKAGE_NAME=\"${OT_PACKAGE_NAME}\"") 103message(STATUS "Package Name: ${OT_PACKAGE_NAME}") 104 105set(OT_PACKAGE_VERSION "" CACHE STRING "OpenThread Package Version") 106if(OT_PACKAGE_VERSION STREQUAL "") 107 ot_git_version(OT_PACKAGE_VERSION) 108 message(STATUS "Setting default package version: ${OT_PACKAGE_VERSION}") 109endif() 110message(STATUS "Package Version: ${OT_PACKAGE_VERSION}") 111 112# Deprecated 113set(OT_CONFIG "" CACHE STRING "OpenThread config header file (deprecated, use `OT_PROJECT_CONFIG` or `OT_PLATFORM_CONFIG` instead") 114 115set(OT_PROJECT_CONFIG "" CACHE STRING "OpenThread project-specific config header file") 116set(OT_PLATFORM_CONFIG "" CACHE STRING "OpenThread platform-specific config header file") 117 118list(APPEND OT_PUBLIC_INCLUDES ${PROJECT_BINARY_DIR}/etc/cmake) 119list(APPEND OT_PUBLIC_INCLUDES ${PROJECT_SOURCE_DIR}/etc/cmake) 120list(APPEND OT_PUBLIC_INCLUDES ${PROJECT_SOURCE_DIR}/include) 121 122if(OT_PLATFORM STREQUAL "posix") 123 target_include_directories(ot-config INTERFACE ${PROJECT_SOURCE_DIR}/src/posix/platform) 124 target_compile_definitions(ot-config INTERFACE OPENTHREAD_PLATFORM_POSIX=1) 125 add_subdirectory("${PROJECT_SOURCE_DIR}/src/posix/platform") 126elseif(OT_PLATFORM STREQUAL "external") 127 # skip in this case 128elseif(OT_PLATFORM STREQUAL "nexus") 129 if (OT_APP_CLI OR OT_APP_NCP OR OT_APP_RCP) 130 message(FATAL_ERROR "no app (cli/ncp/rcp) should be enabled with nexus simulation platform") 131 endif() 132 target_compile_definitions(ot-config INTERFACE OPENTHREAD_PLATFORM_NEXUS=1) 133else() 134 target_include_directories(ot-config INTERFACE ${PROJECT_SOURCE_DIR}/examples/platforms/${OT_PLATFORM}) 135 add_subdirectory("${PROJECT_SOURCE_DIR}/examples/platforms/${OT_PLATFORM}") 136endif() 137 138if(OT_CONFIG) 139 target_compile_definitions(ot-config INTERFACE "OPENTHREAD_CONFIG_FILE=\"${OT_CONFIG}\"") 140 message(WARNING "OT_CONFIG is deprecated - use `OT_PROJECT_CONFIG` and `OT_PLATFORM_CONFIG` instead") 141 message(STATUS "OT_CONFIG=\"${OT_CONFIG}\"") 142endif() 143 144if (OT_PROJECT_CONFIG) 145 target_compile_definitions(ot-config INTERFACE "OPENTHREAD_PROJECT_CORE_CONFIG_FILE=\"${OT_PROJECT_CONFIG}\"") 146 message(STATUS "OT_PROJECT_CONFIG=\"${OT_PROJECT_CONFIG}\"") 147endif() 148 149if (OT_PLATFORM_CONFIG) 150 target_compile_definitions(ot-config INTERFACE "OPENTHREAD_PLATFORM_CORE_CONFIG_FILE=\"${OT_PLATFORM_CONFIG}\"") 151 message(STATUS "OT_PLATFORM_CONFIG=\"${OT_PLATFORM_CONFIG}\"") 152endif() 153 154target_compile_definitions(ot-config INTERFACE ${OT_PLATFORM_DEFINES}) 155 156if(OT_PLATFORM STREQUAL "posix") 157 if(OT_BUILD_EXECUTABLES) 158 add_subdirectory(src/posix) 159 else() 160 add_subdirectory(src/posix EXCLUDE_FROM_ALL) 161 endif() 162elseif(OT_PLATFORM) 163 add_subdirectory(examples) 164endif() 165 166if (OT_DOC) 167 add_subdirectory(doc) 168endif() 169 170add_subdirectory(src) 171add_subdirectory(third_party EXCLUDE_FROM_ALL) 172 173add_subdirectory(tests) 174add_subdirectory(tools) 175 176add_custom_target(print-ot-config ALL 177 COMMAND ${CMAKE_COMMAND} 178 -DLIST="$<TARGET_PROPERTY:ot-config,INTERFACE_COMPILE_DEFINITIONS>" 179 -P ${PROJECT_SOURCE_DIR}/etc/cmake/print.cmake 180) 181