xref: /aosp_15_r20/external/pigweed/pw_toolchain/arm_clang/clang_config.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2021 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15_script_path =
16    get_path_info("../py/pw_toolchain/clang_arm_toolchain.py", "abspath")
17
18# This template generates a config that can be used to target ARM cores using
19# a clang compiler.
20#
21# Clang isn't a plug-and-play experience for Cortex-M baremetal targets; it's
22# missing C runtime libraries, C/C++ standard libraries, and a few other
23# things. This template uses the provided cflags, asmflags, ldflags, etc. to
24# generate a config that pulls the missing components from an arm-none-eabi-gcc
25# compiler on the system PATH. The end result is a clang-based compiler that
26# pulls in gcc-provided headers and libraries to complete the toolchain.
27#
28# Args:
29#   - asmflags, cflags, cflags_c, cflags_cc, ldflags: These flags are used to
30#         locate the correct architecture-specific libraries/headers. To
31#         properly drive the script, provide all architecture flags (e.g. -mcpu,
32#         -mabi, -mthumb, -mfloat-abi, -mfpu) in at least one of these
33#         variables.
34#
35# Generated targets:
36#   - $target_name: The final config to use with your clang toolchain.
37template("pw_clang_arm_config") {
38  config(target_name) {
39    # Pull all the compiler flags into a single list.
40    _compiler_flags = []
41    forward_variables_from(invoker, "*")
42    if (defined(asmflags)) {
43      _compiler_flags += asmflags
44    } else {
45      asmflags = []
46    }
47    if (defined(cflags)) {
48      _compiler_flags += cflags
49    } else {
50      cflags = []
51    }
52    if (defined(cflags_c)) {
53      _compiler_flags += cflags_c
54    } else {
55      cflags_c = []
56    }
57    if (defined(cflags_cc)) {
58      _compiler_flags += cflags_cc
59    } else {
60      cflags_cc = []
61    }
62    if (defined(ldflags)) {
63      _compiler_flags += ldflags
64    } else {
65      ldflags = []
66    }
67
68    if (!defined(rustflags)) {
69      rustflags = []
70    }
71
72    # Invoke the script that will generate clang flags based on the current
73    # compiler version and desired arch.
74    _script_flags = [
75      "--gn-scope",
76      "--cflags",
77      "--ldflags",
78      "--",
79    ]
80    _script_flags += _compiler_flags
81    _arm_flags = exec_script(_script_path, _script_flags, "scope")
82
83    cflags += _arm_flags.cflags
84    ldflags += _arm_flags.cflags
85    ldflags += _arm_flags.ldflags
86    foreach(f, ldflags) {
87      rustflags += [ "-Clink-arg=$f" ]
88    }
89
90    libs = [
91      "c_nano",
92      "m",
93      "gcc",
94      "stdc++_nano",
95    ]
96  }
97}
98