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
29set(OT_PLATFORM_LIB "openthread-posix" PARENT_SCOPE)
30
31add_library(ot-posix-config INTERFACE)
32
33option(OT_DAEMON "Enable daemon mode" OFF)
34if(OT_DAEMON)
35    target_compile_definitions(ot-posix-config
36        INTERFACE "OPENTHREAD_POSIX_CONFIG_DAEMON_ENABLE=1"
37    )
38
39    # We have to add this definition to `ot-config` because openthread core
40    # libraries will set config file to "openthrad-core-posix-config.h" which
41    # depends on `OPENTHREAD_POSIX_CONFIG_DAEMON_ENABLE` to correctly enable
42    # PLATFORM_NETIF and PLATFORM_UDP features. Otherwise, openthread core and
43    # posix libraries will use different feature definitions.
44    list(APPEND OT_PLATFORM_DEFINES "OPENTHREAD_POSIX_CONFIG_DAEMON_ENABLE=1")
45    set(OT_PLATFORM_DEFINES ${OT_PLATFORM_DEFINES} PARENT_SCOPE)
46endif()
47
48if(OT_MULTIPAN_RCP)
49    target_compile_definitions(ot-posix-config
50        INTERFACE "OPENTHREAD_CONFIG_MULTIPAN_RCP_ENABLE=1"
51    )
52endif()
53
54
55option(OT_POSIX_INSTALL_EXTERNAL_ROUTES "Install External Routes as IPv6 routes" ON)
56if(OT_POSIX_INSTALL_EXTERNAL_ROUTES)
57    target_compile_definitions(ot-posix-config
58        INTERFACE "OPENTHREAD_POSIX_CONFIG_INSTALL_EXTERNAL_ROUTES_ENABLE=1"
59    )
60else()
61    target_compile_definitions(ot-posix-config
62        INTERFACE "OPENTHREAD_POSIX_CONFIG_INSTALL_EXTERNAL_ROUTES_ENABLE=0"
63    )
64endif()
65
66option(OT_POSIX_VIRTUAL_TIME "enable virtual time" OFF)
67if(OT_POSIX_VIRTUAL_TIME)
68    target_compile_definitions(ot-posix-config
69        INTERFACE "OPENTHREAD_POSIX_VIRTUAL_TIME=1"
70    )
71endif()
72
73option(OT_POSIX_MAX_POWER_TABLE  "enable max power table" OFF)
74if(OT_POSIX_MAX_POWER_TABLE)
75    target_compile_definitions(ot-posix-config
76        INTERFACE "OPENTHREAD_POSIX_CONFIG_MAX_POWER_TABLE_ENABLE=1"
77    )
78endif()
79
80option(OT_POSIX_SECURE_SETTINGS "enable secure settings" OFF)
81if (OT_POSIX_SECURE_SETTINGS)
82    target_compile_definitions(ot-posix-config
83        INTERFACE "OPENTHREAD_POSIX_CONFIG_SECURE_SETTINGS_ENABLE=1"
84    )
85endif()
86
87option(OT_POSIX_RCP_HDLC_BUS "enable RCP HDLC bus" OFF)
88if(OT_POSIX_RCP_HDLC_BUS)
89    target_compile_definitions(ot-posix-config
90        INTERFACE "OPENTHREAD_POSIX_CONFIG_SPINEL_HDLC_INTERFACE_ENABLE=1"
91    )
92endif()
93
94option(OT_POSIX_RCP_SPI_BUS "enable RCP SPI bus" OFF)
95if(OT_POSIX_RCP_SPI_BUS)
96    target_compile_definitions(ot-posix-config
97        INTERFACE "OPENTHREAD_POSIX_CONFIG_SPINEL_SPI_INTERFACE_ENABLE=1"
98    )
99endif()
100
101option(OT_POSIX_RCP_VENDOR_BUS "enable RCP vendor bus" OFF)
102if(OT_POSIX_RCP_VENDOR_BUS)
103    target_compile_definitions(ot-posix-config
104        INTERFACE "OPENTHREAD_POSIX_CONFIG_SPINEL_VENDOR_INTERFACE_ENABLE=1"
105    )
106endif()
107
108set(OT_POSIX_NAT64_CIDR "192.168.255.0/24" CACHE STRING "NAT64 CIDR for OpenThread NAT64")
109if(OT_POSIX_NAT64_CIDR)
110    target_compile_definitions(ot-posix-config
111        INTERFACE "OPENTHREAD_POSIX_CONFIG_NAT64_CIDR=\"${OT_POSIX_NAT64_CIDR}\""
112    )
113endif()
114
115if(NOT OT_PLATFORM_CONFIG)
116    set(OT_PLATFORM_CONFIG "openthread-core-posix-config.h" PARENT_SCOPE)
117endif()
118
119set(CMAKE_EXE_LINKER_FLAGS "-rdynamic ${CMAKE_EXE_LINKER_FLAGS}" PARENT_SCOPE)
120
121if(OT_ANDROID_NDK)
122    target_compile_options(ot-posix-config INTERFACE -Wno-sign-compare)
123endif()
124
125add_library(openthread-posix
126    alarm.cpp
127    backtrace.cpp
128    ble.cpp
129    configuration.cpp
130    config_file.cpp
131    daemon.cpp
132    entropy.cpp
133    firewall.cpp
134    hdlc_interface.cpp
135    infra_if.cpp
136    logging.cpp
137    mainloop.cpp
138    memory.cpp
139    misc.cpp
140    multicast_routing.cpp
141    netif.cpp
142    power.cpp
143    radio.cpp
144    radio_url.cpp
145    resolver.cpp
146    settings.cpp
147    spi_interface.cpp
148    system.cpp
149    trel.cpp
150    udp.cpp
151    utils.cpp
152    virtual_time.cpp
153)
154
155include(vendor.cmake)
156
157target_link_libraries(openthread-posix
158    PUBLIC
159        openthread-platform
160    PRIVATE
161        openthread-cli-ftd
162        openthread-ftd
163        openthread-hdlc
164        openthread-radio-spinel
165        openthread-spinel-rcp
166        openthread-url
167        ot-config-ftd
168        ot-config
169        ot-posix-config
170        $<$<NOT:$<BOOL:${OT_ANDROID_NDK}>>:util>
171        $<$<STREQUAL:${CMAKE_SYSTEM_NAME},Linux>:rt>
172)
173
174option(OT_TARGET_OPENWRT "enable openthread posix for OpenWRT" OFF)
175if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND NOT OT_TARGET_OPENWRT)
176    target_compile_definitions(ot-posix-config
177        INTERFACE "OPENTHREAD_POSIX_CONFIG_NAT64_AIL_PREFIX_ENABLE=1"
178    )
179    target_link_libraries(openthread-posix PRIVATE anl)
180endif()
181
182target_compile_definitions(openthread-posix
183    PUBLIC
184        ${OT_PUBLIC_DEFINES}
185    PRIVATE
186        OPENTHREAD_FTD=1
187        OPENTHREAD_MTD=0
188        OPENTHREAD_RADIO=0
189)
190
191target_compile_options(openthread-posix PRIVATE
192    ${OT_CFLAGS}
193)
194
195target_include_directories(openthread-posix PRIVATE
196    ${OT_PUBLIC_INCLUDES}
197    ${PROJECT_SOURCE_DIR}/src
198    ${PROJECT_SOURCE_DIR}/src/core
199    ${PROJECT_SOURCE_DIR}/third_party/mbedtls/repo/include
200    ${OT_SPINEL_VENDOR_HOOK_SOURCE_DIR}
201    PUBLIC
202        ${PROJECT_SOURCE_DIR}/src/posix/platform/include
203)
204
205add_executable(ot-posix-test-settings
206    settings.cpp
207)
208target_compile_definitions(ot-posix-test-settings
209    PRIVATE -DSELF_TEST=1 -DOPENTHREAD_CONFIG_LOG_PLATFORM=0
210)
211target_include_directories(ot-posix-test-settings
212    PRIVATE
213        ${PROJECT_SOURCE_DIR}/include
214        ${PROJECT_SOURCE_DIR}/src
215        ${PROJECT_SOURCE_DIR}/src/core
216        ${PROJECT_SOURCE_DIR}/src/posix/platform/include
217)
218add_test(NAME ot-posix-test-settings COMMAND ot-posix-test-settings)
219