|
| 1 | +# Invoke OpenThread's external autoconf-based build system. |
| 2 | +include(ExternalProject) |
| 3 | + |
| 4 | +set(ep_base ${PROJECT_BINARY_DIR}/ext_proj) |
| 5 | +set_property(DIRECTORY PROPERTY "EP_BASE" ${ep_base}) |
| 6 | + |
| 7 | +# Construct a list of commands to give to ExternalProject_Add() |
| 8 | +# |
| 9 | +# See https://cmake.org/cmake/help/latest/module/ExternalProject.html |
| 10 | +# for documentation on ExternalProject_Add |
| 11 | +set(cmd "") |
| 12 | + |
| 13 | +set(ot_name ot) |
| 14 | +list(APPEND cmd |
| 15 | + ${ot_name} |
| 16 | + ) |
| 17 | + |
| 18 | +set(ot_STAMP_DIR ${ep_base}/Stamp/${ot_name}) |
| 19 | +set(ot_INSTALL_DIR ${ep_base}/Install/${ot_name}) |
| 20 | + |
| 21 | +#--Download step-------------- |
| 22 | +if(NOT EXTERNAL_PROJECT_PATH_OPENTHREAD) |
| 23 | + # TODO: Point to a Zephyr fork |
| 24 | + # Nov. 7 |
| 25 | + set_ifndef(ot_GIT_REPOSITORY "https://github.com/openthread/openthread.git") |
| 26 | + set_ifndef(ot_GIT_TAG a89eb887488dcbab7f5e9237e2bbcaad38140690) |
| 27 | + set_ifndef(ot_GIT_PROGRESS 1) |
| 28 | + |
| 29 | + list(APPEND cmd |
| 30 | + GIT_REPOSITORY ${ot_GIT_REPOSITORY} |
| 31 | + GIT_TAG ${ot_GIT_TAG} |
| 32 | + GIT_PROGRESS ${ot_GIT_PROGRESS} |
| 33 | + ) |
| 34 | +endif() |
| 35 | + |
| 36 | +#--Update/Patch step------------- |
| 37 | + |
| 38 | +# An update step is not necessary because we are using a commit hash |
| 39 | +# as a tag, and the code behind a hash cannot change. |
| 40 | + |
| 41 | +# UPDATE_DISCONNECTED should be used when updates aren't needed, but |
| 42 | +# for some reason we were not able to get this to work, so we use a |
| 43 | +# dummy step to fake updating. This ensures that the git repo is not |
| 44 | +# downloaded on every 'make' invocation. |
| 45 | +list(APPEND cmd |
| 46 | + UPDATE_COMMAND |
| 47 | + ${CMAKE_COMMAND} -E touch ${ot_STAMP_DIR}/${ot_name}-update |
| 48 | + ) |
| 49 | + |
| 50 | +#--Configure step------------- |
| 51 | +if(EXTERNAL_PROJECT_PATH_OPENTHREAD) |
| 52 | + set(ot_SOURCE_DIR ${EXTERNAL_PROJECT_PATH_OPENTHREAD}) |
| 53 | + |
| 54 | + list(APPEND cmd |
| 55 | + SOURCE_DIR ${ot_SOURCE_DIR} |
| 56 | + ) |
| 57 | + |
| 58 | +else() |
| 59 | + set(ot_SOURCE_DIR ${ep_base}/Source/${ot_name}) # TODO: Download dir? |
| 60 | + |
| 61 | + # "If SOURCE_DIR is explicitly set to an existing directory the |
| 62 | + # project will be built from it. Otherwise a download step must be |
| 63 | + # specified using one of the DOWNLOAD_COMMAND, CVS_*, SVN_*, or URL |
| 64 | + # options." -- CMake docs |
| 65 | +endif() |
| 66 | + |
| 67 | +# TODO: Can we omit this, does OpenThread need to use install? |
| 68 | +# TODO: Move this to host-tools? |
| 69 | +find_program(INSTALL install) |
| 70 | + |
| 71 | +# It looks like OpenThread requires a *nix system to build for |
| 72 | +# embedded. |
| 73 | +# |
| 74 | +# https://github.com/openthread/openthread/blob/master/examples/drivers/windows/README.md |
| 75 | + |
| 76 | +# TODO: Use different includes for C and CXX |
| 77 | +zephyr_get_include_directories_for_lang_as_string( C includes) |
| 78 | +zephyr_get_system_include_directories_for_lang_as_string(C system_includes) |
| 79 | +zephyr_get_compile_definitions_for_lang_as_string( C definitions) |
| 80 | + |
| 81 | +zephyr_get_compile_options_for_lang_as_string(C c_options) |
| 82 | +zephyr_get_compile_options_for_lang_as_string(CXX cxx_options) |
| 83 | + |
| 84 | +# TODO: What happens if a CFLAG is added after this build script has |
| 85 | +# been run? |
| 86 | + |
| 87 | +# TODO: Does OpenThread need all flags, or just some? Should we |
| 88 | +# whitelist, or blacklist flags? |
| 89 | + |
| 90 | + |
| 91 | +# TODO: What is all this? $(dir $(realpath $(firstword $(MAKEFILE_LIST)))) |
| 92 | + |
| 93 | +set(exec_prefix zephyr) |
| 94 | + |
| 95 | +set(commoncflags "-DOPENTHREAD_CONFIG_LOG_LEVEL=${CONFIG_OPENTHREAD_LOG_LEVEL}") |
| 96 | +set(commoncflags "${commoncflags} -DOPENTHREAD_PROJECT_CORE_CONFIG_FILE=\\\"openthread-core-zephyr-config.h\\\"") |
| 97 | +set(commoncflags "${commoncflags} -I${CMAKE_CURRENT_LIST_DIR}/platform") |
| 98 | + |
| 99 | +set(configure_flags |
| 100 | + "INSTALL=${INSTALL} -p" |
| 101 | + "CPP=${CMAKE_C_COMPILER} -E" # TODO: Find CPP in toolchain-gcc.cmake and use that instead? |
| 102 | + "CC=${CMAKE_C_COMPILER}" |
| 103 | + "CXX=${CMAKE_CXX_COMPILER}" |
| 104 | + OBJC="" # TODO: Omit this? |
| 105 | + "OBJCXX=${OBJCXX}" # TODO: Omit this? |
| 106 | + "AR=${CMAKE_AR}" |
| 107 | + "RANLIB=${CMAKE_RANLILB}" |
| 108 | + "NM=${CROSS_COMPILE}nm" # TODO: Find NM in toolchain-gcc.cmake and use that instead? |
| 109 | + "STRIP=${CMAKE_STRIP}" |
| 110 | + "CPPFLAGS=${definitions} ${commoncflags} ${includes} ${system_includes}" |
| 111 | + "CFLAGS=${c_options} ${commoncflags} ${includes} ${system_includes}" |
| 112 | + "CXXFLAGS=${cxx_options} ${commoncflags} ${includes} ${system_includes}" # TODO: Do we need includes here? |
| 113 | + LDFLAGS="" # TODO: What does a networking stack need to use the linker for? |
| 114 | + |
| 115 | + --host=arm-none-eabi |
| 116 | + --prefix=/ |
| 117 | + --exec-prefix=/${exec_prefix} |
| 118 | + --target=arm-none-eabi # TODO: Is Kconfig expressing that OT is ARM-only? |
| 119 | + --enable-no-executables-hack |
| 120 | + --disable-docs |
| 121 | + --with-platform-info=zephyr |
| 122 | +) |
| 123 | + |
| 124 | +# TODO: Simplify |
| 125 | +set(ZEPHYR_MBEDTLS_CPPFLAGS "${ZEPHYR_MBEDTLS_CPPFLAGS} ") |
| 126 | +set(ZEPHYR_MBEDTLS_CPPFLAGS "-DMBEDTLS_CONFIG_FILE='\"mbedtls-config.h\"'") |
| 127 | +set(ZEPHYR_MBEDTLS_CPPFLAGS "${ZEPHYR_MBEDTLS_CPPFLAGS} -DMBEDTLS_USER_CONFIG_FILE='\"${CMAKE_CURRENT_SOURCE_DIR}/zephyr-mbedtls-config.h\"'") |
| 128 | +set(ZEPHYR_MBEDTLS_CPPFLAGS "${ZEPHYR_MBEDTLS_CPPFLAGS} -I${ot_SOURCE_DIR}/third_party/mbedtls") |
| 129 | +set(ZEPHYR_MBEDTLS_CPPFLAGS "${ZEPHYR_MBEDTLS_CPPFLAGS} -I${ot_SOURCE_DIR}/third_party/mbedtls/repo.patched/include") |
| 130 | +set(ZEPHYR_MBEDTLS_CPPFLAGS "${ZEPHYR_MBEDTLS_CPPFLAGS} -I${ot_SOURCE_DIR}/third_party/mbedtls/repo.patched/include/mbedtls") |
| 131 | + |
| 132 | +list(APPEND configure_flags |
| 133 | + "MBEDTLS_CPPFLAGS=${ZEPHYR_MBEDTLS_CPPFLAGS}" |
| 134 | + ) |
| 135 | + |
| 136 | +if(CONFIG_OPENTHREAD_COMMISSIONER) |
| 137 | + list(APPEND configure_flags |
| 138 | + --enable-commissioner |
| 139 | + ) |
| 140 | +endif() |
| 141 | + |
| 142 | +if(CONFIG_OPENTHREAD_JAM_DETECTION) |
| 143 | + list(APPEND configure_flags |
| 144 | + --enable-jam-detection |
| 145 | + ) |
| 146 | +endif() |
| 147 | + |
| 148 | +if(CONFIG_OPENTHREAD_JOINER) |
| 149 | + list(APPEND configure_flags |
| 150 | + --enable-joiner |
| 151 | + ) |
| 152 | +endif() |
| 153 | + |
| 154 | +if(CONFIG_OPENTHREAD_SHELL) |
| 155 | + list(APPEND configure_flags |
| 156 | + --enable-cli-app=all |
| 157 | + ) |
| 158 | +endif() |
| 159 | + |
| 160 | +if(CONFIG_OPENTHREAD_DIAG) |
| 161 | + list(APPEND configure_flags |
| 162 | + --enable-diag |
| 163 | + ) |
| 164 | +endif() |
| 165 | + |
| 166 | +list(APPEND cmd |
| 167 | + CONFIGURE_COMMAND ./configure ${configure_flags} |
| 168 | +) |
| 169 | + |
| 170 | +#--Build step----------------- |
| 171 | + |
| 172 | +# Invoke OpenThread's build system from the root of it's source |
| 173 | +# directory |
| 174 | +# TODO: Support out-of-source builds |
| 175 | +set(ot_BINARY_DIR ${ot_SOURCE_DIR}) |
| 176 | +list(APPEND cmd |
| 177 | + BINARY_DIR ${ot_BINARY_DIR} |
| 178 | + INSTALL_DIR ${ot_INSTALL_DIR} |
| 179 | + ) |
| 180 | + |
| 181 | +set(make_flags |
| 182 | + -j99 # TODO: Why 99? |
| 183 | + --no-print-directory |
| 184 | + ) |
| 185 | + |
| 186 | +list(APPEND cmd |
| 187 | + BUILD_COMMAND make ${make_flags} all |
| 188 | + INSTALL_COMMAND make ${make_flags} DESTDIR=${ot_INSTALL_DIR} install |
| 189 | + ) |
| 190 | + |
| 191 | +# TODO: Find out how to make this work. |
| 192 | +set(ot_include_dir ${ot_SOURCE_DIR}/include) |
| 193 | + |
| 194 | +# TODO: Is this only needed by alarm.c? |
| 195 | +zephyr_system_include_directories(${ot_include_dir}) |
| 196 | + |
| 197 | +# TODO: Why doesn't app get this path from the above function call? |
| 198 | +target_include_directories(app SYSTEM PRIVATE ${ot_include_dir}) |
| 199 | + |
| 200 | +#set_target_properties(ot_lib PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${ot_include_dir}) |
| 201 | +zephyr_include_directories(${ot_include_dir}) |
| 202 | + |
| 203 | +# Determine which libs should be linked in |
| 204 | +set(ot_libs |
| 205 | + openthread-platform-utils |
| 206 | + mbedcrypto |
| 207 | + ) |
| 208 | +if(CONFIG_OPENTHREAD_FTD) |
| 209 | + list(APPEND ot_libs openthread-ftd) |
| 210 | + set(cli_lib openthread-cli-ftd) |
| 211 | +elseif(CONFIG_OPENTHREAD_MTD) |
| 212 | + list(APPEND ot_libs openthread-mtd) |
| 213 | + set(cli_lib openthread-cli-ftd) |
| 214 | +endif() |
| 215 | + |
| 216 | +if(CONFIG_OPENTHREAD_SHELL) |
| 217 | + list(APPEND ot_libs ${cli_lib}) |
| 218 | +endif() |
| 219 | + |
| 220 | +foreach(ot_lib ${ot_libs}) |
| 221 | + set(build_byproducts ${build_byproducts} ${ot_INSTALL_DIR}/${exec_prefix}/lib/lib${ot_lib}.a) |
| 222 | +endforeach() |
| 223 | + |
| 224 | +list(APPEND cmd |
| 225 | + BUILD_BYPRODUCTS ${build_byproducts} |
| 226 | + ) |
| 227 | + |
| 228 | +ExternalProject_Add(${cmd}) |
| 229 | + |
| 230 | +ExternalProject_Add_Step( |
| 231 | + ${ot_name} bootstrap # Names of project and custom step |
| 232 | + COMMAND ./bootstrap # Command line invoked by this step |
| 233 | + COMMENT "bootstrapping..." # Text printed when step executes |
| 234 | + DEPENDEES download # Steps on which this step depends |
| 235 | + DEPENDERS configure # Steps that depend on this step |
| 236 | + WORKING_DIRECTORY ${ot_SOURCE_DIR} |
| 237 | + ) |
| 238 | + |
| 239 | +# Create wrapper CMake libraries |
| 240 | +foreach(ot_lib ${ot_libs}) |
| 241 | + add_library(${ot_lib} STATIC IMPORTED GLOBAL) |
| 242 | + add_dependencies( # TODO: Necessary? |
| 243 | + ${ot_lib} |
| 244 | + ${ot_name} |
| 245 | + ) |
| 246 | + set_target_properties(${ot_lib} PROPERTIES IMPORTED_LOCATION |
| 247 | + ${ot_INSTALL_DIR}/${exec_prefix}/lib/lib${ot_lib}.a |
| 248 | + ) |
| 249 | + zephyr_append_cmake_library(${ot_lib}) |
| 250 | +endforeach() |
| 251 | + |
| 252 | +add_subdirectory(platform) |
0 commit comments