1if(CONFIG_OPENTHREAD_SOURCES)
2
3set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
4
5# OpenThread options
6set(OT_BUILD_EXECUTABLES OFF CACHE BOOL "Disable OpenThread samples")
7set(OT_BUILTIN_MBEDTLS_MANAGEMENT OFF CACHE BOOL "Use Zephyr's mbedTLS heap")
8set(OT_PLATFORM "zephyr" CACHE STRING "Zephyr as a target platform")
9set(OT_THREAD_VERSION ${CONFIG_OPENTHREAD_THREAD_VERSION} CACHE STRING "User selected Thread stack version")
10set(OT_CLI_TRANSPORT "CONSOLE" CACHE STRING "Set CLI to use console interpreter")
11
12string(REPLACE " " ";" OT_MBEDTLS_LIB_LIST " ${CONFIG_OPENTHREAD_MBEDTLS_LIB_NAME}")
13
14set(
15    OT_EXTERNAL_MBEDTLS
16    ${OT_MBEDTLS_LIB_LIST}
17    CACHE STRING
18    "Specify external mbedtls library"
19    FORCE
20)
21
22if(CONFIG_ASSERT)
23  set(OT_ASSERT ON CACHE BOOL "Enable assert function OT_ASSERT()" FORCE)
24else()
25  set(OT_ASSERT OFF CACHE BOOL "Enable assert function OT_ASSERT()" FORCE)
26endif()
27
28if(CONFIG_OPENTHREAD_BACKBONE_ROUTER)
29  set(OT_BACKBONE_ROUTER ON CACHE BOOL "Enable backbone router functionality" FORCE)
30else()
31  set(OT_BACKBONE_ROUTER OFF CACHE BOOL "Enable backbone router functionality" FORCE)
32endif()
33
34if(CONFIG_OPENTHREAD_BORDER_AGENT)
35  set(OT_BORDER_AGENT ON CACHE BOOL "Enable Border Agent" FORCE)
36else()
37  set(OT_BORDER_AGENT OFF CACHE BOOL "Enable Border Agent" FORCE)
38endif()
39
40if(CONFIG_OPENTHREAD_BORDER_ROUTER)
41  set(OT_BORDER_ROUTER ON CACHE BOOL "Enable Border Router" FORCE)
42else()
43  set(OT_BORDER_ROUTER OFF CACHE BOOL "Enable Border Router" FORCE)
44endif()
45
46if(CONFIG_OPENTHREAD_COAP)
47  set(OT_COAP ON CACHE BOOL "Enable CoAP API" FORCE)
48else()
49  set(OT_COAP OFF CACHE BOOL "Enable CoAP API" FORCE)
50endif()
51
52if(CONFIG_OPENTHREAD_COAPS)
53  set(OT_COAPS ON CACHE BOOL "Enable secure CoAP API support" FORCE)
54else()
55  set(OT_COAPS OFF CACHE BOOL "Enable secure CoAP API support" FORCE)
56endif()
57
58if(CONFIG_OPENTHREAD_COAP_BLOCK)
59  set(OT_COAP_BLOCK ON CACHE BOOL "Enable CoAP Block-wise option support" FORCE)
60else()
61  set(OT_COAP_BLOCK OFF CACHE BOOL "Enable CoAP Block-wise option support" FORCE)
62endif()
63
64if(CONFIG_OPENTHREAD_COAP_OBSERVE)
65  set(OT_COAP_OBSERVE ON CACHE BOOL "Enable CoAP Observe option support" FORCE)
66else()
67  set(OT_COAP_OBSERVE OFF CACHE BOOL "Enable CoAP Observe option support" FORCE)
68endif()
69
70if(CONFIG_OPENTHREAD_COMMISSIONER)
71  set(OT_COMMISSIONER ON CACHE BOOL "Enable Commissioner" FORCE)
72else()
73  set(OT_COMMISSIONER OFF CACHE BOOL "Enable Commissioner" FORCE)
74endif()
75
76if(CONFIG_OPENTHREAD_CHANNEL_MANAGER)
77  set(OT_CHANNEL_MANAGER ON CACHE BOOL "Enable channel manager support" FORCE)
78else()
79  set(OT_CHANNEL_MANAGER OFF CACHE BOOL "Enable channel manager support" FORCE)
80endif()
81
82if(CONFIG_OPENTHREAD_CHANNEL_MONITOR)
83  set(OT_CHANNEL_MONITOR ON CACHE BOOL "Enable channel monitor support" FORCE)
84else()
85  set(OT_CHANNEL_MONITOR OFF CACHE BOOL "Enable channel monitor support" FORCE)
86endif()
87
88if(CONFIG_OPENTHREAD_CHILD_SUPERVISION)
89  set(OT_CHILD_SUPERVISION ON CACHE BOOL "Enable child supervision support" FORCE)
90else()
91  set(OT_CHILD_SUPERVISION OFF CACHE BOOL "Enable child supervision support" FORCE)
92endif()
93
94if(CONFIG_OPENTHREAD_CSL_AUTO_SYNC)
95  set(OT_CSL_AUTO_SYNC ON CACHE BOOL "Enable csl autosync" FORCE)
96else()
97  set(OT_CSL_AUTO_SYNC OFF CACHE BOOL "Enable csl autosync" FORCE)
98endif()
99
100if(CONFIG_OPENTHREAD_CSL_RECEIVER)
101  set(OT_CSL_RECEIVER ON CACHE BOOL "Enable CSL receiver feature for Thread 1.2" FORCE)
102else()
103  set(OT_CSL_RECEIVER OFF CACHE BOOL "Enable CSL receiver feature for Thread 1.2" FORCE)
104endif()
105
106if(CONFIG_OPENTHREAD_CSL_DEBUG)
107  set(OT_CSL_DEBUG ON CACHE BOOL "Enable CSL debug" FORCE)
108else()
109  set(OT_CSL_DEBUG OFF CACHE BOOL "Enable CSL debug" FORCE)
110endif()
111
112if(CONFIG_OPENTHREAD_DATSET_UPDATER)
113  set(OT_DATASET_UPDATER ON CACHE BOOL "Enable Dataset updater" FORCE)
114else()
115  set(OT_DATASET_UPDATER OFF CACHE BOOL "Enable Dataset updater" FORCE)
116endif()
117
118if(CONFIG_OPENTHREAD_DHCP6_CLIENT)
119  set(OT_DHCP6_CLIENT ON CACHE BOOL "Enable DHCPv6 Client" FORCE)
120else()
121  set(OT_DHCP6_CLIENT OFF CACHE BOOL "Enable DHCPv6 Client" FORCE)
122endif()
123
124if(CONFIG_OPENTHREAD_DHCP6_SERVER)
125  set(OT_DHCP6_SERVER ON CACHE BOOL "Enable DHCPv6 Server" FORCE)
126else()
127  set(OT_DHCP6_SERVER OFF CACHE BOOL "Enable DHCPv6 Server" FORCE)
128endif()
129
130if(CONFIG_OPENTHREAD_DIAG)
131  set(OT_DIAGNOSTIC ON CACHE BOOL "Enable Diagnostics support" FORCE)
132else()
133  set(OT_DIAGNOSTIC OFF CACHE BOOL "Enable Diagnostics support" FORCE)
134endif()
135
136if(CONFIG_OPENTHREAD_DNS_CLIENT)
137  set(OT_DNS_CLIENT ON CACHE BOOL "Enable DNS client support" FORCE)
138else()
139  set(OT_DNS_CLIENT OFF CACHE BOOL "Enable DNS client support" FORCE)
140endif()
141
142if(CONFIG_OPENTHREAD_DNSSD_SERVER)
143  set(OT_DNSSD_SERVER ON CACHE BOOL "Enable DNS-SD server support" FORCE)
144else()
145  set(OT_DNSSD_SERVER OFF CACHE BOOL "Enable DNS-SD server support" FORCE)
146endif()
147
148if(CONFIG_OPENTHREAD_DUA)
149  set(OT_DUA ON CACHE BOOL "Enable Domain Unicast Address feature for Thread 1.2" FORCE)
150else()
151  set(OT_DUA OFF CACHE BOOL "Enable Domain Unicast Address feature for Thread 1.2" FORCE)
152endif()
153
154if(CONFIG_OPENTHREAD_ECDSA)
155  set(OT_ECDSA ON CACHE BOOL "Enable ECDSA support" FORCE)
156else()
157  set(OT_ECDSA OFF CACHE BOOL "Enable ECDSA support" FORCE)
158endif()
159
160if(CONFIG_OPENTHREAD_ENABLE_SERVICE)
161  set(OT_SERVICE ON CACHE BOOL "Enable Service entries in Thread Network Data" FORCE)
162else()
163  set(OT_SERVICE OFF CACHE BOOL "Enable Service entries in Thread Network Data" FORCE)
164endif()
165
166if(CONFIG_OPENTHREAD_EXTERNAL_HEAP)
167  set(OT_EXTERNAL_HEAP ON CACHE BOOL "Enable external heap support" FORCE)
168else()
169  set(OT_EXTERNAL_HEAP OFF CACHE BOOL "Enable external heap support" FORCE)
170endif()
171
172if(CONFIG_OPENTHREAD_HISTORY_TRACKER)
173  set(OT_HISTORY_TRACKER ON CACHE BOOL "Enable history tracker support." FORCE)
174else()
175  set(OT_HISTORY_TRACKER OFF CACHE BOOL "Enable history tracker support." FORCE)
176endif()
177
178if(CONFIG_OPENTHREAD_IP6_FRAGM)
179  set(OT_IP6_FRAGM ON CACHE BOOL "Enable IPv6 fragmentation support" FORCE)
180else()
181  set(OT_IP6_FRAGM OFF CACHE BOOL "Enable IPv6 fragmentation support" FORCE)
182endif()
183
184if(CONFIG_OPENTHREAD_JAM_DETECTION)
185  set(OT_JAM_DETECTION ON CACHE BOOL "Enable Jam Detection" FORCE)
186else()
187  set(OT_JAM_DETECTION OFF CACHE BOOL "Enable Jam Detection" FORCE)
188endif()
189
190if(CONFIG_OPENTHREAD_JOINER)
191  set(OT_JOINER ON CACHE BOOL "Enable Joiner" FORCE)
192else()
193  set(OT_JOINER OFF CACHE BOOL "Enable Joiner" FORCE)
194endif()
195
196if(CONFIG_OPENTHREAD_LEGACY)
197  set(OT_LEGACY ON CACHE BOOL "Enable legacy network support" FORCE)
198else()
199  set(OT_LEGACY OFF CACHE BOOL "Enable legacy network support" FORCE)
200endif()
201
202if(CONFIG_OPENTHREAD_LINK_METRICS_INITIATOR)
203  set(OT_LINK_METRICS_INITIATOR ON CACHE BOOL "Enable Link Metrics initiator for Thread 1.2" FORCE)
204else()
205  set(OT_LINK_METRICS_INITIATOR OFF CACHE BOOL "Enable Link Metrics initiator for Thread 1.2" FORCE)
206endif()
207
208if(CONFIG_OPENTHREAD_LINK_METRICS_SUBJECT)
209  set(OT_LINK_METRICS_SUBJECT ON CACHE BOOL "Enable Link Metrics subject for Thread 1.2" FORCE)
210else()
211  set(OT_LINK_METRICS_SUBJECT OFF CACHE BOOL "Enable Link Metrics subject for Thread 1.2" FORCE)
212endif()
213
214if(CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC)
215  set(OT_LOG_LEVEL_DYNAMIC ON CACHE BOOL "Enable dynamic log level control" FORCE)
216else()
217  set(OT_LOG_LEVEL_DYNAMIC OFF CACHE BOOL "Enable dynamic log level control" FORCE)
218endif()
219
220if(CONFIG_OPENTHREAD_MAC_FILTER)
221  set(OT_MAC_FILTER ON CACHE BOOL "Enable MAC filter support" FORCE)
222else()
223  set(OT_MAC_FILTER OFF CACHE BOOL "Enable MAC filter support" FORCE)
224endif()
225
226if(CONFIG_OPENTHREAD_MLE_LONG_ROUTES)
227  set(OT_MLE_LONG_ROUTES ON CACHE BOOL "Enable MLE long routes support (Experimental)" FORCE)
228else()
229  set(OT_MLE_LONG_ROUTES OFF CACHE BOOL "Enable MLE long routes support (Experimental)" FORCE)
230endif()
231
232if(CONFIG_OPENTHREAD_MLR)
233  set(OT_MLR ON CACHE BOOL "Enable Multicast Listener Registration feature for Thread 1.2" FORCE)
234else()
235  set(OT_MLR OFF CACHE BOOL "Enable Multicast Listener Registration feature for Thread 1.2" FORCE)
236endif()
237
238if(CONFIG_OPENTHREAD_MTD_NETDIAG)
239  set(OT_MTD_NETDIAG ON CACHE BOOL "Enable TMF network diagnostics on MTDs" FORCE)
240else()
241  set(OT_MTD_NETDIAG OFF CACHE BOOL "Enable TMF network diagnostics on MTDs" FORCE)
242endif()
243
244if(CONFIG_OPENTHREAD_MULTIPLE_INSTANCE)
245  set(OT_MULTIPLE_INSTANCE ON CACHE BOOL "Enable multiple instances" FORCE)
246else()
247  set(OT_MULTIPLE_INSTANCE OFF CACHE BOOL "Enable multiple instances" FORCE)
248endif()
249
250if(CONFIG_OPENTHREAD_NEIGHBOR_DISCOVERY_AGENT)
251  set(OT_NEIGHBOR_DISCOVERY_AGENT ON CACHE BOOL "Enable neighbor discovery agent support" FORCE)
252else()
253  set(OT_NEIGHBOR_DISCOVERY_AGENT OFF CACHE BOOL "Enable neighbor discovery agent support" FORCE)
254endif()
255
256if(CONFIG_OPENTHREAD_NETDATA_PUBLISHER)
257  set(OT_NETDATA_PUBLISHER ON CACHE BOOL "Enable Thread Network Data publisher" FORCE)
258else()
259  set(OT_NETDATA_PUBLISHER OFF CACHE BOOL "Enable Thread Network Data publisher" FORCE)
260endif()
261
262if(CONFIG_OPENTHREAD_PING_SENDER)
263  set(OT_PING_SENDER ON CACHE BOOL "Enable ping sender support" FORCE)
264else()
265  set(OT_PING_SENDER OFF CACHE BOOL "Enable ping sender support" FORCE)
266endif()
267
268if(CONFIG_OPENTHREAD_PLATFORM_NETIF)
269  set(OT_PLATFORM_NETIF ON CACHE BOOL "Enable platform netif support" FORCE)
270else()
271  set(OT_PLATFORM_NETIF OFF CACHE BOOL "Enable platform netif support" FORCE)
272endif()
273
274if(CONFIG_OPENTHREAD_PLATFORM_UDP)
275  set(OT_PLATFORM_UDP ON CACHE BOOL "Enable platform UDP support" FORCE)
276else()
277  set(OT_PLATFORM_UDP OFF CACHE BOOL "Enable platform UDP support" FORCE)
278endif()
279
280if(CONFIG_OPENTHREAD_RAW)
281  set(OT_LINK_RAW ON CACHE BOOL "Enable Link Raw" FORCE)
282else()
283  set(OT_LINK_RAW OFF CACHE BOOL "Enable Link Raw" FORCE)
284endif()
285
286if(CONFIG_OPENTHREAD_REFERENCE_DEVICE)
287  set(OT_REFERENCE_DEVICE ON CACHE BOOL "Enable Thread Certification Reference Device" FORCE)
288else()
289  set(OT_REFERENCE_DEVICE OFF CACHE BOOL "Enable Thread Certification Reference Device" FORCE)
290endif()
291
292if(CONFIG_OPENTHREAD_SETTINGS_RAM)
293  set(OT_SETTINGS_RAM ON CACHE BOOL "Enable volatile-only storage of settings" FORCE)
294else()
295  set(OT_SETTINGS_RAM OFF CACHE BOOL "Enable volatile-only storage of settings" FORCE)
296endif()
297
298if(CONFIG_OPENTHREAD_SLAAC)
299  set(OT_SLAAC ON CACHE BOOL "Enable SLAAC" FORCE)
300else()
301  set(OT_SLAAC OFF CACHE BOOL "Enable SLAAC" FORCE)
302endif()
303
304if(CONFIG_OPENTHREAD_SNTP_CLIENT)
305  set(OT_SNTP_CLIENT ON CACHE BOOL "Enable SNTP Client support" FORCE)
306else()
307  set(OT_SNTP_CLIENT OFF CACHE BOOL "Enable SNTP Client support" FORCE)
308endif()
309
310if(CONFIG_OPENTHREAD_SRP_CLIENT)
311  set(OT_SRP_CLIENT ON CACHE BOOL "Enable SRP Client support" FORCE)
312else()
313  set(OT_SRP_CLIENT OFF CACHE BOOL "Enable SRP Client support" FORCE)
314endif()
315
316if(CONFIG_OPENTHREAD_SRP_SERVER)
317  set(OT_SRP_SERVER ON CACHE BOOL "Enable SRP Server support" FORCE)
318else()
319  set(OT_SRP_SERVER OFF CACHE BOOL "Enable SRP Server support" FORCE)
320endif()
321
322if(CONFIG_OPENTHREAD_TREL)
323  set(OT_TREL ON CACHE BOOL "Enable TREL radio link for Thread over Infrastructure feature" FORCE)
324else()
325  set(OT_TREL OFF CACHE BOOL "Enable TREL radio link for Thread over Infrastructure feature" FORCE)
326endif()
327
328if(CONFIG_OPENTHREAD_TIME_SYNC)
329  set(OT_TIME_SYNC ON CACHE BOOL "Enable the time synchronization service feature" FORCE)
330else()
331  set(OT_TIME_SYNC OFF CACHE BOOL "Enable the time synchronization service feature" FORCE)
332endif()
333
334if(CONFIG_OPENTHREAD_UDP_FORWARD)
335  set(OT_UDP_FORWARD ON CACHE BOOL "Enable UDP forward feature" FORCE)
336else()
337  set(OT_UDP_FORWARD OFF CACHE BOOL "Enable UDP forward feature" FORCE)
338endif()
339
340if(CONFIG_OPENTHREAD_OTNS)
341  set(OT_OTNS ON CACHE BOOL "Enable OTNS support" FORCE)
342else()
343  set(OT_OTNS OFF CACHE BOOL "Enable OTNS support" FORCE)
344endif()
345
346if(CONFIG_OPENTHREAD_FULL_LOGS)
347  set(OT_FULL_LOGS ON CACHE BOOL "Enable full logs" FORCE)
348else()
349  set(OT_FULL_LOGS OFF CACHE BOOL "Enable full logs" FORCE)
350endif()
351
352if(CONFIG_OPENTHREAD_COPROCESSOR_VENDOR_HOOK_SOURCE)
353  set(OT_NCP_VENDOR_HOOK_SOURCE ${CONFIG_OPENTHREAD_COPROCESSOR_VENDOR_HOOK_SOURCE} CACHE STRING "NCP vendor hook source file name" FORCE)
354endif()
355
356# Zephyr logging options
357
358if(CONFIG_LOG_BACKEND_SPINEL)
359  add_definitions(
360      -DOPENTHREAD_CONFIG_LOG_OUTPUT=OPENTHREAD_CONFIG_LOG_OUTPUT_APP
361  )
362endif()
363
364# Other options
365add_definitions(
366    -DOPENTHREAD_CONFIG_LOG_LEVEL=${CONFIG_OPENTHREAD_LOG_LEVEL}
367    -DOPENTHREAD_PROJECT_CORE_CONFIG_FILE="openthread-core-zephyr-config.h"
368)
369
370# Need to specify build directory as well
371add_subdirectory(.. build)
372
373zephyr_get_targets(${CMAKE_CURRENT_LIST_DIR}/../ "STATIC_LIBRARY;OBJECT_LIBRARY" ALL_TARGETS)
374foreach(target ${ALL_TARGETS})
375  # We don't want to build all openthread libraries per default.
376  # Setting EXCLUDE_FROM_ALL ensures that only libraries that are linked
377  # into Zephyr will be built due to dependencies.
378  set_property(TARGET ${target} PROPERTY EXCLUDE_FROM_ALL TRUE)
379endforeach()
380
381string(REPLACE " " ";" OT_PARAM_LIST " ${CONFIG_OPENTHREAD_CUSTOM_PARAMETERS}")
382target_compile_definitions(ot-config INTERFACE ${OT_PARAM_LIST})
383
384# Zephyr compiler options
385target_include_directories(ot-config INTERFACE
386    $<TARGET_PROPERTY:zephyr_interface,INTERFACE_INCLUDE_DIRECTORIES>
387)
388
389target_include_directories(ot-config SYSTEM INTERFACE
390    $<TARGET_PROPERTY:zephyr_interface,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
391)
392
393target_compile_definitions(ot-config INTERFACE
394    $<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_DEFINITIONS>
395)
396
397target_compile_options(ot-config INTERFACE
398    $<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_OPTIONS> -fno-builtin
399)
400
401# Openthread depends on errno.h, which includes errno_private.h in minimal libc.
402# errno_private.h is generated as part of ${SYSCALL_LIST_H_TARGET} target.
403add_dependencies(ot-config ${SYSCALL_LIST_H_TARGET})
404
405# Make sure C library is linked after OpenThread libraries (to prevent linker
406# errors)
407target_link_libraries(ot-config INTERFACE -lc)
408
409# Include OpenThread headers
410zephyr_system_include_directories(../include)
411zephyr_system_include_directories(../examples/platforms)
412
413# Determine which libs should be linked in
414set(ot_libs "")
415
416if(CONFIG_OPENTHREAD_FTD)
417set(cli_lib openthread-cli-ftd)
418elseif(CONFIG_OPENTHREAD_MTD)
419set(cli_lib openthread-cli-mtd)
420endif()
421
422if(CONFIG_OPENTHREAD_SHELL)
423list(APPEND ot_libs ${cli_lib})
424endif()
425
426if(CONFIG_OPENTHREAD_COPROCESSOR_RCP)
427list(APPEND ot_libs openthread-rcp)
428endif()
429
430if(CONFIG_OPENTHREAD_COPROCESSOR_NCP)
431if(CONFIG_OPENTHREAD_FTD)
432list(APPEND ot_libs openthread-ncp-ftd)
433elseif(CONFIG_OPENTHREAD_MTD)
434list(APPEND ot_libs openthread-ncp-mtd)
435endif()
436endif()
437
438if(NOT CONFIG_OPENTHREAD_COPROCESSOR_RCP)
439if(CONFIG_OPENTHREAD_FTD)
440list(APPEND ot_libs openthread-ftd)
441elseif(CONFIG_OPENTHREAD_MTD)
442list(APPEND ot_libs openthread-mtd)
443endif()
444endif()
445
446zephyr_link_libraries(${ot_libs})
447
448endif()
449