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