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