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_STANDARD 99)
50
51message(STATUS "OpenThread Source Directory: ${PROJECT_SOURCE_DIR}")
52
53target_include_directories(ot-config INTERFACE
54    ${PROJECT_SOURCE_DIR}/include
55    ${PROJECT_SOURCE_DIR}/src
56    ${PROJECT_SOURCE_DIR}/src/core
57)
58
59include(TestBigEndian)
60TEST_BIG_ENDIAN(OT_BIG_ENDIAN)
61if(OT_BIG_ENDIAN)
62    target_compile_definitions(ot-config INTERFACE "BYTE_ORDER_BIG_ENDIAN=1")
63endif()
64
65include("${PROJECT_SOURCE_DIR}/etc/cmake/options.cmake")
66include("${PROJECT_SOURCE_DIR}/etc/cmake/functions.cmake")
67
68if(NOT CMAKE_BUILD_TYPE)
69    # Check if this is a top-level CMake.
70    # If it is not, do not set the CMAKE_BUILD_TYPE because OpenThread is a part of something bigger.
71    if ("${CMAKE_PROJECT_NAME}" STREQUAL "openthread")
72        set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "default build type: Debug" FORCE)
73    endif ()
74endif()
75
76if (CMAKE_BUILD_TYPE)
77    message(STATUS "OpenThread CMake build type: ${CMAKE_BUILD_TYPE}")
78endif ()
79
80if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "(Apple)?[Cc]lang")
81    option(OT_COMPILE_WARNING_AS_ERROR "whether to include -Werror -pedantic-errors with gcc-compatible compilers")
82    if (OT_COMPILE_WARNING_AS_ERROR)
83        set(OT_CFLAGS -Werror -pedantic-errors)
84    endif()
85
86    if(OT_COVERAGE)
87        target_compile_definitions(ot-config INTERFACE "OPENTHREAD_ENABLE_COVERAGE=1")
88        target_compile_options(ot-config INTERFACE -g -O0 --coverage)
89        target_link_libraries(ot-config INTERFACE --coverage)
90    endif()
91
92    set(OT_CFLAGS
93        $<$<COMPILE_LANGUAGE:C>:${OT_CFLAGS} -Wall -Wextra -Wshadow>
94        $<$<COMPILE_LANGUAGE:CXX>:${OT_CFLAGS} -Wall -Wextra -Wshadow -Wno-c++14-compat -fno-exceptions>
95    )
96endif()
97
98set(OT_PACKAGE_NAME "OPENTHREAD" CACHE STRING "OpenThread Package Name")
99target_compile_definitions(ot-config INTERFACE "PACKAGE_NAME=\"${OT_PACKAGE_NAME}\"")
100message(STATUS "Package Name: ${OT_PACKAGE_NAME}")
101
102set(OT_PACKAGE_VERSION "" CACHE STRING "OpenThread Package Version")
103if(OT_PACKAGE_VERSION STREQUAL "")
104    ot_git_version(OT_PACKAGE_VERSION)
105    message(STATUS "Setting default package version: ${OT_PACKAGE_VERSION}")
106endif()
107message(STATUS "Package Version: ${OT_PACKAGE_VERSION}")
108
109set(OT_THREAD_VERSION "1.3" CACHE STRING "Thread version chosen by the user at configure time")
110set_property(CACHE OT_THREAD_VERSION PROPERTY STRINGS "1.1" "1.2" "1.3" "1.3.1")
111if(${OT_THREAD_VERSION} EQUAL "1.1")
112    target_compile_definitions(ot-config INTERFACE "OPENTHREAD_CONFIG_THREAD_VERSION=OT_THREAD_VERSION_1_1")
113elseif(${OT_THREAD_VERSION} EQUAL "1.2")
114    target_compile_definitions(ot-config INTERFACE "OPENTHREAD_CONFIG_THREAD_VERSION=OT_THREAD_VERSION_1_2")
115elseif(${OT_THREAD_VERSION} EQUAL "1.3")
116    target_compile_definitions(ot-config INTERFACE "OPENTHREAD_CONFIG_THREAD_VERSION=OT_THREAD_VERSION_1_3")
117elseif(${OT_THREAD_VERSION} EQUAL "1.3.1")
118    target_compile_definitions(ot-config INTERFACE "OPENTHREAD_CONFIG_THREAD_VERSION=OT_THREAD_VERSION_1_3_1")
119else()
120    message(FATAL_ERROR "Thread version unknown: ${OT_THREAD_VERSION}")
121endif()
122
123set(OT_PLATFORM "NO" CACHE STRING "Target platform chosen by the user at configure time")
124ot_get_platforms(OT_PLATFORMS)
125set_property(CACHE OT_PLATFORM PROPERTY STRINGS ${OT_PLATFORMS})
126if(NOT OT_PLATFORM IN_LIST OT_PLATFORMS)
127    message(FATAL_ERROR "Platform unknown: ${OT_PLATFORM}")
128endif()
129
130set(OT_LOG_LEVEL "" CACHE STRING "set OpenThread log level")
131set(OT_LOG_LEVEL_VALUES
132    "NONE"
133    "CRIT"
134    "WARN"
135    "NOTE"
136    "INFO"
137    "DEBG"
138)
139set_property(CACHE OT_LOG_LEVEL PROPERTY STRINGS ${OT_LOG_LEVEL_VALUES})
140if(OT_LOG_LEVEL)
141    target_compile_definitions(ot-config INTERFACE "OPENTHREAD_CONFIG_LOG_LEVEL=OT_LOG_LEVEL_${OT_LOG_LEVEL}")
142endif()
143
144set(OT_LOG_OUTPUT_VALUES
145    "APP"
146    "DEBUG_UART"
147    "NONE"
148    "PLATFORM_DEFINED"
149)
150if(OT_REFERENCE_DEVICE AND NOT OT_PLATFORM STREQUAL "posix")
151    set(OT_LOG_OUTPUT "APP" CACHE STRING "Set log output to application for reference device")
152else()
153    set(OT_LOG_OUTPUT "" CACHE STRING "Where log output goes to")
154endif()
155set_property(CACHE OT_LOG_OUTPUT PROPERTY STRINGS ${OT_LOG_OUTPUT_VALUES})
156if(OT_LOG_OUTPUT)
157    if(NOT OT_LOG_OUTPUT IN_LIST OT_LOG_OUTPUT_VALUES)
158        message(FATAL_ERROR "Log output unknown: ${OT_LOG_OUTPUT}")
159    endif()
160    target_compile_definitions(ot-config INTERFACE "OPENTHREAD_CONFIG_LOG_OUTPUT=OPENTHREAD_CONFIG_LOG_OUTPUT_${OT_LOG_OUTPUT}")
161    message(STATUS "Log output: ${OT_LOG_OUTPUT}")
162endif()
163
164# OT_CONFIG allows users to specify the path to a customized OpenThread
165# config header file. The default value of this parameter is empty string.
166# When not specified by user (value is ""), a platform cmake file may
167# choose to change this variable to provide its own OpenThread config header
168# file instead.
169
170set(OT_CONFIG "" CACHE STRING "OpenThread project-specific config header file chosen by user at configure time")
171
172list(APPEND OT_PUBLIC_INCLUDES ${PROJECT_BINARY_DIR}/etc/cmake)
173list(APPEND OT_PUBLIC_INCLUDES ${PROJECT_SOURCE_DIR}/etc/cmake)
174list(APPEND OT_PUBLIC_INCLUDES ${PROJECT_SOURCE_DIR}/include)
175
176if(OT_PLATFORM STREQUAL "posix")
177    target_include_directories(ot-config INTERFACE ${PROJECT_SOURCE_DIR}/src/posix/platform)
178    target_compile_definitions(ot-config INTERFACE OPENTHREAD_PLATFORM_POSIX=1)
179    add_subdirectory("${PROJECT_SOURCE_DIR}/src/posix/platform")
180elseif(OT_PLATFORM STREQUAL "external")
181    # skip in this case
182else()
183    target_include_directories(ot-config INTERFACE ${PROJECT_SOURCE_DIR}/examples/platforms/${OT_PLATFORM})
184    add_subdirectory("${PROJECT_SOURCE_DIR}/examples/platforms/${OT_PLATFORM}")
185endif()
186
187if(OT_CONFIG)
188    target_compile_definitions(ot-config INTERFACE "OPENTHREAD_CONFIG_FILE=\"${OT_CONFIG}\"")
189    message(STATUS "OpenThread Config File: \"${OT_CONFIG}\"")
190endif()
191
192target_compile_definitions(ot-config INTERFACE ${OT_PLATFORM_DEFINES})
193
194if(OT_PLATFORM STREQUAL "posix")
195    if(OT_BUILD_EXECUTABLES)
196        add_subdirectory(src/posix)
197    else()
198        add_subdirectory(src/posix EXCLUDE_FROM_ALL)
199    endif()
200elseif(OT_PLATFORM)
201    add_subdirectory(examples)
202endif()
203
204if (OT_DOC)
205    add_subdirectory(doc)
206endif()
207
208add_subdirectory(src)
209add_subdirectory(third_party EXCLUDE_FROM_ALL)
210
211add_subdirectory(tests)
212add_subdirectory(tools)
213
214add_custom_target(print-ot-config ALL
215                  COMMAND ${CMAKE_COMMAND}
216                  -DLIST="$<TARGET_PROPERTY:ot-config,INTERFACE_COMPILE_DEFINITIONS>"
217                  -P ${PROJECT_SOURCE_DIR}/etc/cmake/print.cmake
218)
219