1.. _module-pw_build_mcuxpresso: 2 3=================== 4pw_build_mcuxpresso 5=================== 6.. pigweed-module:: 7 :name: pw_build_mcuxpresso 8 9The ``pw_build_mcuxpresso`` module provides helper utilities for building a 10target based on an NXP MCUXpresso SDK. 11 12The GN build files live in ``third_party/mcuxpresso`` but are documented here. 13The rationale for keeping the build files in ``third_party`` is that code 14depending on an MCUXpresso SDK can clearly see that their dependency is on 15third party, not pigweed code. 16 17----------------------- 18Using an MCUXpresso SDK 19----------------------- 20An MCUXpresso SDK consists of a number of components, each of which has a set 21of sources, headers, preprocessor defines, and dependencies on other 22components. These are all described in an XML "manifest" file included in the 23SDK package. 24 25To use the SDK within a Pigweed project, the set of components you need must be 26combined into a library that you can depend on. This library will include all of 27the sources and headers, along with necessary preprocessor defines, for those 28components and their dependencies. 29 30Optional components 31=================== 32Including components will include all of their required dependencies. Where the 33components you include have optional dependencies, they must be satisfied by the 34set of components you include otherwise the library generation will fail with an 35error. 36 37Excluding components 38==================== 39Components can be excluded from the generated source set, for example to 40suppress errors about optional dependencies your project does not need, or to 41prevent an unwanted component dependency from being introduced into your 42project. 43 44mcuxpresso_builder 45================== 46``mcuxpresso_builder`` is a utility installed into the environment that is used 47by the GN build scripts in ``third_party/mcuxpresso``, or directly by you to 48generate rules for the Bazel build. 49 50Usage is documented for each build system in the relevant section. 51 52------------ 53The GN build 54------------ 55Using an MCUxpresso SDK within a Pigweed project that uses the GN Build system 56involves the creation of one or more ``pw_source_set`` targets you can depend on 57in your executable targets. 58 59These source sets sets are defined using the ``pw_mcuxpresso_sdk`` template. 60Provide the path to the ``manifest`` XML, along with the names of the components 61you wish to ``include``. 62 63For boards with multiple cores, pass the specific core to filter components for 64in ``device_core``. 65 66.. code-block:: text 67 68 import("$dir_pw_third_party/mcuxpresso/mcuxpresso.gni") 69 70 pw_mcuxpresso_sdk("sample_project_sdk") { 71 manifest = "$dir_pw_third_party/mcuxpresso/evkmimxrt595/EVK-MIMXRT595_manifest_v3_13.xml" 72 include = [ 73 "component.serial_manager_uart.MIMXRT595S", 74 "project_template.evkmimxrt595.MIMXRT595S", 75 "utility.debug_console.MIMXRT595S", 76 ] 77 device_core = "cm33_MIMXRT595S" 78 } 79 80 pw_executable("hello_world") { 81 sources = [ "hello_world.cc" ] 82 deps = [ ":sample_project_sdk" ] 83 } 84 85To exclude components, provide the list to ``exclude`` as an argument to the 86template. For example to replace the FreeRTOS kernel bundled with the MCUXpresso 87SDK with the Pigweed third-party target: 88 89.. code-block:: text 90 91 pw_mcuxpresso_sdk("freertos_project_sdk") { 92 // manifest and includes ommitted for clarity 93 exclude = [ "middleware.freertos-kernel.MIMXRT595S" ] 94 public_deps = [ "$dir_pw_third_party/freertos" ] 95 } 96 97Introducing dependencies 98======================== 99As seen above, the generated source set can have dependencies added by passing 100the ``public_deps`` (or ``deps``) arguments to the template. 101 102You can also pass the ``allow_circular_includes_from``, ``configs``, and 103``public_configs`` arguments to augment the generated source set. 104 105For example it is very common to replace the ``project_template`` component with 106a source set of your own that provides modified copies of the files from the 107SDK. 108 109To resolve circular dependencies, in addition to the generated source set, two 110configs named with the ``__defines`` and ``__includes`` suffixes on the template 111name are generated, to provide the preprocessor defines and include paths that 112the source set uses. 113 114.. code-block:: text 115 116 pw_mcuxpresso_sdk("my_project_sdk") { 117 manifest = "$dir_pw_third_party/mcuxpresso/evkmimxrt595/EVK-MIMXRT595_manifest_v3_13.xml" 118 include = [ 119 "component.serial_manager_uart.MIMXRT595S", 120 "utility.debug_console.MIMXRT595S", 121 ] 122 public_deps = [ ":my_project_config" ] 123 allow_circular_includes_from = [ ":my_project_config" ] 124 } 125 126 pw_source_set("my_project_config") { 127 sources = [ "board.c", "clock_config.c", "pin_mux.c" ] 128 public = [ "board.h", "clock_config.h", "pin_mux.h "] 129 public_configs = [ 130 ":my_project_sdk__defines", 131 ":my_project_sdk__includes" 132 ] 133 } 134 135mcuxpresso_builder 136================== 137For the GN build, this utility is invoked by the ``pw_mcuxpresso_sdk`` template. 138You should only need to interact with ``mcuxpresso_builder`` directly if you are 139doing something custom. 140 141This command generates repository that contains BUILD rules for both GN and Bazel. 142You can use `--skip-bazel` or `--skip-gn` to skip generating rules for respective 143build system. 144 145.. code-block:: bash 146 147 mcuxpresso_builder EVK-MIMXRT595_manifest_v3_14.xml \ 148 --include project_template.evkmimxrt595.MIMXRT595S \ 149 utility.debug_console.MIMXRT595S \ 150 component.serial_manager_uart.MIMXRT595S \ 151 --exclude middleware.freertos-kernel.MIMXRT595S \ 152 --device-core cm33_MIMXRT595S \ 153 --output-path gn_out_sdk \ 154 --mcuxpresso-repo https://github.com/nxp-mcuxpresso/mcux-sdk \ 155 --mcuxpresso-rev MCUX_2.16.000 156 157--------------- 158The Bazel build 159--------------- 160Using an MCUxpresso SDK within a Pigweed project that uses the Bazel build 161system involves the creation of one or more ``cc_library`` targets you can 162depend on in your executable targets. 163 164These targets should select required components from the SDK using the pre-generated 165``BUILD.bazel`` file created from SDK manifest. 166 167Out of the box, Pigweed provides rules for basic components from 168the MCUXpresso SDK. You can list those components out by running 169 170.. code-block:: sh 171 172 bazelisk query @mcuxpresso//... 173 174 175To use those components, simply specify them as deps in your code. 176 177.. code-block:: python 178 179 cc_library( 180 name = "mcuxpresso_sdk", 181 target_compatible_with = [ 182 "@platforms//cpu:armv8-m", 183 ], 184 deps = [ 185 "@mcuxpresso//:component.serial_manager_uart.MIMXRT595S", 186 "@mcuxpresso//:utility.debug_console.MIMXRT595S", 187 ], 188 ) 189 190In addition, you might want to pass some additional configuration 191to SDK rules. You can do that by overriding the ``@mcuxpresso//:user_config`` 192option to point to your custom rule 193 194.. code-block:: python 195 196 config_setting( 197 name = "debug", 198 flag_values = {"@mcuxpresso//:user_config": "//:my_sdk_config"} 199 ) 200 201 cc_library( 202 name = "my_sdk_config", 203 defines = [ 204 "CPU_MIMXRT595SFFOC_cm33", 205 "SDK_DEBUGCONSOLE=1", 206 ], 207 ) 208 209 210Generating the SDK 211================== 212If your use case requires you to use components that are not provided 213by Pigweed, you will have to use the ``mcuxpresso_builder`` script 214to generate additional targets for these components. 215 216Provide the path to the manifest XML, url to MCUxpresso SDK repository 217along with the names of the components you wish to 218``--include`` or ``--exclude``. 219 220This command generates repository that contains BUILD rules for both GN and Bazel. 221You can use `--skip-bazel` or `--skip-gn` to skip generating rules for respective 222build system. 223 224.. code-block:: bash 225 226 bazelisk run //pw_build_mcuxpresso/py:mcuxpresso_builder -- EVK-MIMXRT595_manifest_v3_14.xml \ 227 --mcuxpresso-repo=https://github.com/nxp-mcuxpresso/mcux-sdk \ 228 --mcuxpresso-rev=MCUX_2.16.000 \ 229 --device-core=cm33_MIMXRT595S \ 230 --output-path=bazel-out/k8-fastbuild/bin/mcuxpresso-sdk \ 231 --clean \ 232 --include \ 233 project_template.evkmimxrt595.MIMXRT595S \ 234 utility.debug_console.MIMXRT595S \ 235 component.serial_manager_uart.MIMXRT595S \ 236 --exclude \ 237 middleware.freertos-kernel.MIMXRT595S 238 239 240This will generate a new SDK together with a Bazel build file containing rules 241for each of the specified components (and their dependencies) and 242a ``README.md`` file with additional information. 243 244After that, update ``MODULE.bazel`` to point to your 245generated SDK. 246 247.. code-block:: python 248 249 new_git_repository( 250 name = "mcuxpresso", 251 commit = "your_commit_sha", 252 remote = "your_remote", 253 ) 254 255Directly modifying the generated SDK is not recommended. 256