aboutsummaryrefslogtreecommitdiff
path: root/pw_system/system_target.gni
blob: 55dbcf1b780c3a242a59e2171fe826d71433a070 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# Copyright 2021 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

import("//build_overrides/pigweed.gni")

import("$dir_pw_assert/backend.gni")
import("$dir_pw_bloat/bloat.gni")
import("$dir_pw_boot/backend.gni")
import("$dir_pw_build/cc_library.gni")
import("$dir_pw_chrono/backend.gni")
import("$dir_pw_interrupt/backend.gni")
import("$dir_pw_log/backend.gni")
import("$dir_pw_log_string/backend.gni")
import("$dir_pw_malloc/backend.gni")
import("$dir_pw_sync/backend.gni")
import("$dir_pw_sys_io/backend.gni")
import("$dir_pw_thread/backend.gni")
import("$dir_pw_toolchain/arm_gcc/toolchains.gni")
import("$dir_pw_toolchain/generate_toolchain.gni")
import("$dir_pw_toolchain/host_clang/toolchains.gni")
import("$dir_pw_toolchain/host_gcc/toolchains.gni")
import("$dir_pw_trace/backend.gni")
import("$dir_pw_trace_tokenized/config.gni")
import("$dir_pw_unit_test/test.gni")
import("backend.gni")
import("freertos_backends.gni")
import("stl_backends.gni")

# This scope is essentially an enum for pw_system_target's `cpu` selection.
PW_SYSTEM_CPU = {
  CORTEX_M0PLUS = "cortex-m0plus"
  CORTEX_M4F = "cortex-m4f"
  CORTEX_M3 = "cortex-m3"
  CORTEX_M7F = "cortex-m7f"
  CORTEX_M33 = "cortex-m33"

  # Native builds for the host CPU.
  NATIVE = "native"
}

# This scope is essentially an enum for pw_system_target's `scheduler`
# selection.
PW_SYSTEM_SCHEDULER = {
  FREERTOS = "freertos"

  # Native uses the host OS's native scheduler and OS primitives as provided
  # through the Standard Template Library.
  NATIVE = "native"
}

declare_args() {
  # This argument is intended to be user-facing and should NOT be set by a
  # toolchain. This switches ALL pw_system_target toolchains to use the
  # multi_endpoint_rpc_config config to illustrate a multi-endpoint mode that
  # isolates logging and RPC traffic via HDLC multiplexing.
  #
  # If you would like to use this in production, it is strongly recommended that
  # you instead just add the appropriate defines to your target's toolchain
  # definition.
  pw_system_USE_MULTI_ENDPOINT_CONFIG = false
}

# Defines a target toolchain, automatically setting many required build
# arguments to simplify instantiation of a target.
#
# Args:
#  cpu: (required) The architecture to target.
#    Supported choices: PW_SYSTEM_CPU.CORTEX_M7F, PW_SYSTEM_CPU.CORTEX_M4F, PW_SYSTEM_CPU.CORTEX_M3,
#    PW_SYSTEM_CPU.CORTEX_M33, PW_SYSTEM_CPU.NATIVE
#  scheduler: (required) The scheduler implementation and API to use for this
#    target.
#    Supported choices: PW_SYSTEM_SCHEDULER.FREERTOS, PW_SYSTEM_SCHEDULER.NATIVE
#  system_toolchain: Override the default toolchain selection.
#  use_pw_malloc: Whether or not to replace the default malloc implementation
#    with pw_malloc. Defaults enabled for supported targets.
#  link_deps: Additional link-time dependencies required for all executables.
#    This is a list of source sets.
#  build_args: Additional overrides for GN build arguments.
#  global_configs: Configs that will be globally applied to all pw_source_set,
#    pw_static_library, and pw_executable targets.
template("pw_system_target") {
  _OPTIMIZATION_LEVELS = {
    SIZE_OPTIMIZED = "size_optimized"
    SPEED_OPTIMIZED = "speed_optimized"
    DEBUG = "debug"
  }

  # Generic defaults.
  _default_configs = [ "$dir_pw_build:extra_strict_warnings" ]
  if (defined(invoker.global_configs)) {
    foreach(cfg, invoker.global_configs) {
      _default_configs += [ get_path_info(cfg, "abspath") ]
    }
  }

  _link_deps = [
    "$dir_pw_assert:impl",
    "$dir_pw_log:impl",
  ]
  if (defined(invoker.link_deps)) {
    _link_deps += invoker.link_deps
  }
  _final_binary_extension = ""

  _default_build_args = {
    pw_system_RPC_SERVER_BACKEND = "$dir_pw_system:hdlc_rpc_server"
    pw_system_IO_BACKEND = "$dir_pw_system:sys_io_target_io"

    # TODO(amontanez): This should be set to pw_assert_log ASAP.
    pw_assert_BACKEND = dir_pw_assert_basic

    # TODO(amontanez): This should be set to pw_log_tokenized when support
    # is added.
    pw_log_BACKEND = dir_pw_log_basic

    # TODO(amontanez): This should be set to a "$dir_pw_unit_test:rpc_main"
    # when RPC is working.
    pw_unit_test_MAIN = "$dir_pw_unit_test:logging_main"

    pw_trace_BACKEND = "$dir_pw_trace_tokenized"

    if (pw_system_USE_MULTI_ENDPOINT_CONFIG) {
      pw_system_CONFIG = "$dir_pw_system:multi_endpoint_rpc_config"
    }
  }

  # Populate architecture-specific build args.
  assert(
      defined(invoker.cpu),
      "Please select a `cpu` for $target_name. Options: PW_SYSTEM_CPU.CORTEX_M7, PW_SYSTEM_CPU.CORTEX_M4F, PW_SYSTEM_CPU.CORTEX_M3, PW_SYSTEM_CPU.CORTEX_M33, PW_SYSTEM_CPU.NATIVE")
  if (invoker.cpu == PW_SYSTEM_CPU.CORTEX_M33) {
    _current_cpu = "arm"
    _default_configs += [ "$dir_pw_toolchain/arm_gcc:enable_float_printf" ]
    _arch_build_args = {
      pw_bloat_BLOATY_CONFIG = "$dir_pw_boot_cortex_m/bloaty_config.bloaty"
      pw_boot_BACKEND = "$dir_pw_boot_cortex_m:pw_boot_cortex_m"
      pw_interrupt_CONTEXT_BACKEND = "$dir_pw_interrupt_cortex_m:context"
    }
    _link_deps += [ "$dir_pw_toolchain/arm_gcc:arm_none_eabi_gcc_support" ]

    if (defined(invoker.system_toolchain)) {
      _system_toolchain = invoker.system_toolchain
    } else {
      _system_toolchain = pw_toolchain_arm_gcc
    }

    _final_binary_extension = ".elf"

    _toolchains = [
      {
        toolchain_base = _system_toolchain.cortex_m33_debug
        level_name = _OPTIMIZATION_LEVELS.DEBUG
      },
      {
        toolchain_base = _system_toolchain.cortex_m33_size_optimized
        level_name = _OPTIMIZATION_LEVELS.SIZE_OPTIMIZED
      },
      {
        toolchain_base = _system_toolchain.cortex_m33_speed_optimized
        level_name = _OPTIMIZATION_LEVELS.SPEED_OPTIMIZED
      },
    ]
  } else if (invoker.cpu == PW_SYSTEM_CPU.CORTEX_M7F) {
    _current_cpu = "arm"
    _default_configs += [ "$dir_pw_toolchain/arm_gcc:enable_float_printf" ]
    _arch_build_args = {
      pw_bloat_BLOATY_CONFIG = "$dir_pw_boot_cortex_m/bloaty_config.bloaty"
      pw_boot_BACKEND = "$dir_pw_boot_cortex_m:armv7m"
      pw_interrupt_CONTEXT_BACKEND = "$dir_pw_interrupt_cortex_m:context_armv7m"
    }
    _link_deps += [ "$dir_pw_toolchain/arm_gcc:arm_none_eabi_gcc_support" ]

    if (defined(invoker.system_toolchain)) {
      _system_toolchain = invoker.system_toolchain
    } else {
      _system_toolchain = pw_toolchain_arm_gcc
    }

    _final_binary_extension = ".elf"

    _toolchains = [
      {
        toolchain_base = _system_toolchain.cortex_m7f_debug
        level_name = _OPTIMIZATION_LEVELS.DEBUG
      },
      {
        toolchain_base = _system_toolchain.cortex_m7f_size_optimized
        level_name = _OPTIMIZATION_LEVELS.SIZE_OPTIMIZED
      },
      {
        toolchain_base = _system_toolchain.cortex_m7f_speed_optimized
        level_name = _OPTIMIZATION_LEVELS.SPEED_OPTIMIZED
      },
    ]
  } else if (invoker.cpu == PW_SYSTEM_CPU.CORTEX_M4F) {
    _current_cpu = "arm"
    _default_configs += [ "$dir_pw_toolchain/arm_gcc:enable_float_printf" ]
    _arch_build_args = {
      pw_bloat_BLOATY_CONFIG = "$dir_pw_boot_cortex_m/bloaty_config.bloaty"
      pw_boot_BACKEND = "$dir_pw_boot_cortex_m:armv7m"
      pw_interrupt_CONTEXT_BACKEND = "$dir_pw_interrupt_cortex_m:context_armv7m"
    }
    _link_deps += [ "$dir_pw_toolchain/arm_gcc:arm_none_eabi_gcc_support" ]

    if (defined(invoker.system_toolchain)) {
      _system_toolchain = invoker.system_toolchain
    } else {
      _system_toolchain = pw_toolchain_arm_gcc
    }

    _final_binary_extension = ".elf"

    _toolchains = [
      {
        toolchain_base = _system_toolchain.cortex_m4f_debug
        level_name = _OPTIMIZATION_LEVELS.DEBUG
      },
      {
        toolchain_base = _system_toolchain.cortex_m4f_size_optimized
        level_name = _OPTIMIZATION_LEVELS.SIZE_OPTIMIZED
      },
      {
        toolchain_base = _system_toolchain.cortex_m4f_speed_optimized
        level_name = _OPTIMIZATION_LEVELS.SPEED_OPTIMIZED
      },
    ]
  } else if (invoker.cpu == PW_SYSTEM_CPU.CORTEX_M3) {
    _current_cpu = "arm"
    _arch_build_args = {
      pw_bloat_BLOATY_CONFIG = "$dir_pw_boot_cortex_m/bloaty_config.bloaty"
      pw_boot_BACKEND = "$dir_pw_boot_cortex_m:armv7m"
      pw_interrupt_CONTEXT_BACKEND = "$dir_pw_interrupt_cortex_m:context_armv7m"
    }
    _link_deps += [ "$dir_pw_toolchain/arm_gcc:arm_none_eabi_gcc_support" ]

    if (defined(invoker.system_toolchain)) {
      _system_toolchain = invoker.system_toolchain
    } else {
      _system_toolchain = pw_toolchain_arm_gcc
    }

    _final_binary_extension = ".elf"

    _toolchains = [
      {
        toolchain_base = _system_toolchain.cortex_m3_debug
        level_name = _OPTIMIZATION_LEVELS.DEBUG
      },
      {
        toolchain_base = _system_toolchain.cortex_m3_size_optimized
        level_name = _OPTIMIZATION_LEVELS.SIZE_OPTIMIZED
      },
      {
        toolchain_base = _system_toolchain.cortex_m3_speed_optimized
        level_name = _OPTIMIZATION_LEVELS.SPEED_OPTIMIZED
      },
    ]
  } else if (invoker.cpu == PW_SYSTEM_CPU.CORTEX_M0PLUS) {
    _current_cpu = "arm"
    _arch_build_args = {
      pw_bloat_BLOATY_CONFIG = "$dir_pw_boot_cortex_m/bloaty_config.bloaty"
      pw_boot_BACKEND = "$dir_pw_boot_cortex_m:armv7m"
      pw_interrupt_CONTEXT_BACKEND = "$dir_pw_interrupt_cortex_m:context_armv7m"
    }
    _link_deps += [ "$dir_pw_toolchain/arm_gcc:arm_none_eabi_gcc_support" ]

    if (defined(invoker.system_toolchain)) {
      _system_toolchain = invoker.system_toolchain
    } else {
      _system_toolchain = pw_toolchain_arm_gcc
    }

    # This creates a double .elf.elf extension for the rp2040 target.
    # _final_binary_extension = ".elf"

    _toolchains = [
      {
        toolchain_base = _system_toolchain.cortex_m0plus_debug
        level_name = _OPTIMIZATION_LEVELS.DEBUG
      },
      {
        toolchain_base = _system_toolchain.cortex_m0plus_size_optimized
        level_name = _OPTIMIZATION_LEVELS.SIZE_OPTIMIZED
      },
      {
        toolchain_base = _system_toolchain.cortex_m0plus_speed_optimized
        level_name = _OPTIMIZATION_LEVELS.SPEED_OPTIMIZED
      },
    ]
  } else if (invoker.cpu == PW_SYSTEM_CPU.NATIVE) {
    _current_cpu = host_cpu
    _arch_build_args = {
      pw_log_BACKEND = dir_pw_log_string
      pw_log_string_HANDLER_BACKEND = "$dir_pw_system:log_backend"
      pw_sys_io_BACKEND = "$dir_pw_sys_io_stdio"
      pw_system_IO_BACKEND = "$dir_pw_system:socket_target_io"
    }
    _link_deps += [ "$dir_pw_log_string:handler.impl" ]

    if (defined(invoker.system_toolchain)) {
      _system_toolchain = invoker.system_toolchain
    } else if (host_os == "win") {
      _system_toolchain = pw_toolchain_host_gcc
    } else {
      _system_toolchain = pw_toolchain_host_clang
    }

    _toolchains = [
      {
        toolchain_base = _system_toolchain.debug
        level_name = _OPTIMIZATION_LEVELS.DEBUG
      },
      {
        toolchain_base = _system_toolchain.size_optimized
        level_name = _OPTIMIZATION_LEVELS.SIZE_OPTIMIZED
      },
      {
        toolchain_base = _system_toolchain.speed_optimized
        level_name = _OPTIMIZATION_LEVELS.SPEED_OPTIMIZED
      },
    ]
  }
  assert(defined(_arch_build_args),
         "Unknown cpu choice for $target_name: `${invoker.cpu}`")

  # Populate OS-specific build args.
  assert(
      defined(invoker.scheduler),
      "Please select an `scheduler` for $target_name. Options: PW_SYSTEM_SCHEDULER.FREERTOS, PW_SYSTEM_SCHEDULER.NATIVE")
  if (invoker.scheduler == PW_SYSTEM_SCHEDULER.FREERTOS) {
    _current_os = "freertos"
    _os_build_args = PW_SYSTEM_FREERTOS_BACKENDS
  } else if (invoker.scheduler == PW_SYSTEM_SCHEDULER.NATIVE) {
    _current_os = host_os
    _os_build_args = PW_SYSTEM_STL_BACKENDS
  }
  assert(defined(_os_build_args),
         "Unknown scheduler choice for $target_name: `${invoker.scheduler}`")

  # Configure malloc defaults.
  _use_pw_malloc = false
  if (defined(invoker.use_pw_malloc)) {
    _use_pw_malloc = invoker.use_pw_malloc
  } else if (invoker.cpu != PW_SYSTEM_CPU.NATIVE) {
    _use_pw_malloc = true
  }

  if (_use_pw_malloc) {
    _default_configs += [ "$dir_pw_malloc:pw_malloc_wrapper_config" ]
    _link_deps += [ dir_pw_malloc ]
    _malloc_build_args = {
      pw_malloc_BACKEND = dir_pw_malloc_freelist
    }
  } else {
    _malloc_build_args = {
    }
  }

  foreach(toolchain_and_level, _toolchains) {
    # Clear from previous iteration.
    _base = {
    }
    _base = toolchain_and_level.toolchain_base

    generate_toolchain("${target_name}.${toolchain_and_level.level_name}") {
      forward_variables_from(_base,
                             "*",
                             [
                               "defaults",
                               "name",
                             ])
      final_binary_extension = _final_binary_extension
      defaults = {
        current_os = _current_os
        current_cpu = _current_cpu
        forward_variables_from(_base.defaults, "*")
        forward_variables_from(_default_build_args, "*")
        forward_variables_from(_arch_build_args, "*")
        forward_variables_from(_os_build_args, "*")
        forward_variables_from(_malloc_build_args, "*")
        default_configs += _default_configs
        if (!defined(pw_build_LINK_DEPS)) {
          pw_build_LINK_DEPS = []
        }
        pw_build_LINK_DEPS += _link_deps

        if (defined(invoker.build_args)) {
          forward_variables_from(invoker.build_args, "*")
        }
      }
    }
  }
}