xref: /aosp_15_r20/external/openthread/CMakeLists.txt (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
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)
59
60include(TestBigEndian)
61TEST_BIG_ENDIAN(OT_BIG_ENDIAN)
62if(OT_BIG_ENDIAN)
63    target_compile_definitions(ot-config INTERFACE "BYTE_ORDER_BIG_ENDIAN=1")
64endif()
65
66include("${PROJECT_SOURCE_DIR}/etc/cmake/options.cmake")
67include("${PROJECT_SOURCE_DIR}/etc/cmake/functions.cmake")
68
69if(NOT CMAKE_BUILD_TYPE)
70    # Check if this is a top-level CMake.
71    # If it is not, do not set the CMAKE_BUILD_TYPE because OpenThread is a part of something bigger.
72    if ("${CMAKE_PROJECT_NAME}" STREQUAL "openthread")
73        set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "default build type: Debug" FORCE)
74    endif ()
75endif()
76
77if (CMAKE_BUILD_TYPE)
78    message(STATUS "OpenThread CMake build type: ${CMAKE_BUILD_TYPE}")
79endif ()
80
81if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "(Apple)?[Cc]lang")
82    option(OT_COMPILE_WARNING_AS_ERROR "whether to include -Werror -pedantic-errors with gcc-compatible compilers")
83    if (OT_COMPILE_WARNING_AS_ERROR)
84        set(OT_CFLAGS -Werror -pedantic-errors)
85    endif()
86
87    if(OT_COVERAGE)
88        target_compile_definitions(ot-config INTERFACE "OPENTHREAD_ENABLE_COVERAGE=1")
89        target_compile_options(ot-config INTERFACE -g -O0 --coverage)
90        target_link_libraries(ot-config INTERFACE --coverage)
91    endif()
92
93    set(OT_CFLAGS
94        $<$<COMPILE_LANGUAGE:C>:${OT_CFLAGS} -Wall -Wextra -Wshadow>
95        $<$<COMPILE_LANGUAGE:CXX>:${OT_CFLAGS} -Wall -Wextra -Wshadow -Wno-c++14-compat -fno-exceptions>
96        $<$<CXX_COMPILER_ID:Clang>:-Wc99-extensions>
97    )
98endif()
99
100set(OT_PACKAGE_NAME "OPENTHREAD" CACHE STRING "OpenThread Package Name")
101target_compile_definitions(ot-config INTERFACE "PACKAGE_NAME=\"${OT_PACKAGE_NAME}\"")
102message(STATUS "Package Name: ${OT_PACKAGE_NAME}")
103
104set(OT_PACKAGE_VERSION "" CACHE STRING "OpenThread Package Version")
105if(OT_PACKAGE_VERSION STREQUAL "")
106    ot_git_version(OT_PACKAGE_VERSION)
107    message(STATUS "Setting default package version: ${OT_PACKAGE_VERSION}")
108endif()
109message(STATUS "Package Version: ${OT_PACKAGE_VERSION}")
110
111# Deprecated
112set(OT_CONFIG "" CACHE STRING "OpenThread config header file (deprecated, use `OT_PROJECT_CONFIG` or `OT_PLATFORM_CONFIG` instead")
113
114set(OT_PROJECT_CONFIG "" CACHE STRING "OpenThread project-specific config header file")
115set(OT_PLATFORM_CONFIG "" CACHE STRING "OpenThread platform-specific config header file")
116
117list(APPEND OT_PUBLIC_INCLUDES ${PROJECT_BINARY_DIR}/etc/cmake)
118list(APPEND OT_PUBLIC_INCLUDES ${PROJECT_SOURCE_DIR}/etc/cmake)
119list(APPEND OT_PUBLIC_INCLUDES ${PROJECT_SOURCE_DIR}/include)
120
121if(OT_PLATFORM STREQUAL "posix")
122    target_include_directories(ot-config INTERFACE ${PROJECT_SOURCE_DIR}/src/posix/platform)
123    target_compile_definitions(ot-config INTERFACE OPENTHREAD_PLATFORM_POSIX=1)
124    add_subdirectory("${PROJECT_SOURCE_DIR}/src/posix/platform")
125elseif(OT_PLATFORM STREQUAL "external")
126    # skip in this case
127else()
128    target_include_directories(ot-config INTERFACE ${PROJECT_SOURCE_DIR}/examples/platforms/${OT_PLATFORM})
129    add_subdirectory("${PROJECT_SOURCE_DIR}/examples/platforms/${OT_PLATFORM}")
130endif()
131
132if(OT_CONFIG)
133    target_compile_definitions(ot-config INTERFACE "OPENTHREAD_CONFIG_FILE=\"${OT_CONFIG}\"")
134    message(WARNING "OT_CONFIG is deprecated - use `OT_PROJECT_CONFIG` and `OT_PLATFORM_CONFIG` instead")
135    message(STATUS "OT_CONFIG=\"${OT_CONFIG}\"")
136endif()
137
138if (OT_PROJECT_CONFIG)
139    target_compile_definitions(ot-config INTERFACE "OPENTHREAD_PROJECT_CORE_CONFIG_FILE=\"${OT_PROJECT_CONFIG}\"")
140    message(STATUS "OT_PROJECT_CONFIG=\"${OT_PROJECT_CONFIG}\"")
141endif()
142
143if (OT_PLATFORM_CONFIG)
144    target_compile_definitions(ot-config INTERFACE "OPENTHREAD_PLATFORM_CORE_CONFIG_FILE=\"${OT_PLATFORM_CONFIG}\"")
145    message(STATUS "OT_PLATFORM_CONFIG=\"${OT_PLATFORM_CONFIG}\"")
146endif()
147
148target_compile_definitions(ot-config INTERFACE ${OT_PLATFORM_DEFINES})
149
150if(OT_PLATFORM STREQUAL "posix")
151    if(OT_BUILD_EXECUTABLES)
152        add_subdirectory(src/posix)
153    else()
154        add_subdirectory(src/posix EXCLUDE_FROM_ALL)
155    endif()
156elseif(OT_PLATFORM)
157    add_subdirectory(examples)
158endif()
159
160if (OT_DOC)
161    add_subdirectory(doc)
162endif()
163
164add_subdirectory(src)
165add_subdirectory(third_party EXCLUDE_FROM_ALL)
166
167add_subdirectory(tests)
168add_subdirectory(tools)
169
170add_custom_target(print-ot-config ALL
171                  COMMAND ${CMAKE_COMMAND}
172                  -DLIST="$<TARGET_PROPERTY:ot-config,INTERFACE_COMPILE_DEFINITIONS>"
173                  -P ${PROJECT_SOURCE_DIR}/etc/cmake/print.cmake
174)
175