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
109# Deprecated
110set(OT_CONFIG "" CACHE STRING "OpenThread config header file (deprecated, use `OT_PROJECT_CONFIG` or `OT_PLATFORM_CONFIG` instead")
111
112set(OT_PROJECT_CONFIG "" CACHE STRING "OpenThread project-specific config header file")
113set(OT_PLATFORM_CONFIG "" CACHE STRING "OpenThread platform-specific config header file")
114
115list(APPEND OT_PUBLIC_INCLUDES ${PROJECT_BINARY_DIR}/etc/cmake)
116list(APPEND OT_PUBLIC_INCLUDES ${PROJECT_SOURCE_DIR}/etc/cmake)
117list(APPEND OT_PUBLIC_INCLUDES ${PROJECT_SOURCE_DIR}/include)
118
119if(OT_PLATFORM STREQUAL "posix")
120    target_include_directories(ot-config INTERFACE ${PROJECT_SOURCE_DIR}/src/posix/platform)
121    target_compile_definitions(ot-config INTERFACE OPENTHREAD_PLATFORM_POSIX=1)
122    add_subdirectory("${PROJECT_SOURCE_DIR}/src/posix/platform")
123elseif(OT_PLATFORM STREQUAL "external")
124    # skip in this case
125else()
126    target_include_directories(ot-config INTERFACE ${PROJECT_SOURCE_DIR}/examples/platforms/${OT_PLATFORM})
127    add_subdirectory("${PROJECT_SOURCE_DIR}/examples/platforms/${OT_PLATFORM}")
128endif()
129
130if(OT_CONFIG)
131    target_compile_definitions(ot-config INTERFACE "OPENTHREAD_CONFIG_FILE=\"${OT_CONFIG}\"")
132    message(WARNING "OT_CONFIG is deprecated - use `OT_PROJECT_CONFIG` and `OT_PLATFORM_CONFIG` instead")
133    message(STATUS "OT_CONFIG=\"${OT_CONFIG}\"")
134endif()
135
136if (OT_PROJECT_CONFIG)
137    target_compile_definitions(ot-config INTERFACE "OPENTHREAD_PROJECT_CORE_CONFIG_FILE=\"${OT_PROJECT_CONFIG}\"")
138    message(STATUS "OT_PROJECT_CONFIG=\"${OT_PROJECT_CONFIG}\"")
139endif()
140
141if (OT_PLATFORM_CONFIG)
142    target_compile_definitions(ot-config INTERFACE "OPENTHREAD_PLATFORM_CORE_CONFIG_FILE=\"${OT_PLATFORM_CONFIG}\"")
143    message(STATUS "OT_PLATFORM_CONFIG=\"${OT_PLATFORM_CONFIG}\"")
144endif()
145
146target_compile_definitions(ot-config INTERFACE ${OT_PLATFORM_DEFINES})
147
148if(OT_PLATFORM STREQUAL "posix")
149    if(OT_BUILD_EXECUTABLES)
150        add_subdirectory(src/posix)
151    else()
152        add_subdirectory(src/posix EXCLUDE_FROM_ALL)
153    endif()
154elseif(OT_PLATFORM)
155    add_subdirectory(examples)
156endif()
157
158if (OT_DOC)
159    add_subdirectory(doc)
160endif()
161
162add_subdirectory(src)
163add_subdirectory(third_party EXCLUDE_FROM_ALL)
164
165add_subdirectory(tests)
166add_subdirectory(tools)
167
168add_custom_target(print-ot-config ALL
169                  COMMAND ${CMAKE_COMMAND}
170                  -DLIST="$<TARGET_PROPERTY:ot-config,INTERFACE_COMPILE_DEFINITIONS>"
171                  -P ${PROJECT_SOURCE_DIR}/etc/cmake/print.cmake
172)
173