blob: f7f9962c97f0f417156b6d4a8c7255d6c5152086 [file] [log] [blame]
Alexei Frolov1a82c142019-10-31 17:37:12 -07001# Copyright 2019 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
15import("arm_gcc.gni")
Keir Mierle58671912019-11-14 00:15:41 -080016import("host_clang.gni")
Wyatt Heplercca0daf2019-11-14 11:29:04 -080017import("host_gcc.gni")
Alexei Frolov1a82c142019-10-31 17:37:12 -070018
19# Creates a series of toolchain targets with common compiler options.
20#
21# Args:
22# toolchain_template: The target template to use to create the toolchains.
23# common_toolchain_cflags: cflags to be shared by all toolchains.
24# common_toolchain_ldflags: ldflags to be shared by all toolchains.
25# toolchains: List of scopes defining each of the desired toolchains.
26# Each scope contains up to three variables:
27# toolchain_name: The full target name of the toolchain.
28# additional_cflags: Optional list of extra cflags for the toolchain.
29# additional_ldflags: Optional list of extra ldflags for the toolchain.
30template("generate_toolchains") {
31 not_needed([ "target_name" ])
32
33 assert(defined(invoker.toolchain_template),
34 "generate_toolchains requires a toolchain template")
35 assert(defined(invoker.toolchains),
36 "generate_toolchains must be called with a list of toolchains")
37
38 if (defined(invoker.common_toolchain_cflags)) {
39 _common_cflags = invoker.common_toolchain_cflags
40 } else {
41 _common_cflags = []
42 }
43
44 if (defined(invoker.common_toolchain_ldflags)) {
45 _common_ldflags = invoker.common_toolchain_ldflags
46 } else {
47 _common_ldflags = []
48 }
49
50 # Create a target for each of the desired toolchains, appending its own cflags
51 # and ldflags to the common ones.
52 foreach(toolchain_config, invoker.toolchains) {
53 # GN does not allow assigning a non-empty array to a non-empty array.
54 # This must be done as two assignments, first clearing the original value.
55 _toolchain_cflags = []
56 _toolchain_cflags = _common_cflags
57 if (defined(toolchain_config.additional_cflags)) {
58 _toolchain_cflags += toolchain_config.additional_cflags
59 }
60
61 _toolchain_ldflags = []
62 _toolchain_ldflags = _common_ldflags
63 if (defined(toolchain_config.additional_ldflags)) {
64 _toolchain_ldflags += toolchain_config.additional_ldflags
65 }
66
67 target(invoker.toolchain_template, toolchain_config.toolchain_name) {
68 toolchain_cflags = _toolchain_cflags
69 toolchain_ldflags = _toolchain_ldflags
70 }
71 }
72}
73
74generate_toolchains("cortex_m4") {
75 toolchain_template = "arm_gcc_toolchain"
76
Armando Montanez050eae82019-12-10 14:23:36 -080077 software_fpu_cflags = [ "-mfloat-abi=soft" ]
78
79 hardware_fpu_cflags = [
80 # When hardware FPU is enabled, PW_ARMV7M_ENABLE_FPU is set to 1.
81 # TODO(pwbug/17): Replace when there's a more sophisticated configuration
82 # system.
83 "-DPW_ARMV7M_ENABLE_FPU=1",
84 "-mfloat-abi=hard",
85 "-mfpu=fpv4-sp-d16",
86 ]
87
Alexei Frolov1a82c142019-10-31 17:37:12 -070088 common_toolchain_cflags = [
89 "-mabi=aapcs",
90 "-mcpu=cortex-m4",
Alexei Frolov1a82c142019-10-31 17:37:12 -070091 "-mthumb",
Armando Montanez4f803342019-11-18 11:14:12 -080092 "-specs=nano.specs",
93 "-specs=nosys.specs",
Alexei Frolov1a82c142019-10-31 17:37:12 -070094 ]
95
96 common_toolchain_ldflags = [
Alexei Frolov1a82c142019-10-31 17:37:12 -070097 "-lnosys",
98 "-lc",
99 ]
100
101 toolchains = [
Armando Montanez050eae82019-12-10 14:23:36 -0800102 # Cortex-M4 toolchains that use software-emulated floating point.
Alexei Frolov1a82c142019-10-31 17:37:12 -0700103 {
104 toolchain_name = "arm_gcc_cortex_m4_og"
105 additional_cflags = [ "-Og" ]
Armando Montanez050eae82019-12-10 14:23:36 -0800106 additional_cflags += software_fpu_cflags
Alexei Frolov1a82c142019-10-31 17:37:12 -0700107 },
108 {
109 toolchain_name = "arm_gcc_cortex_m4_o1"
110 additional_cflags = [ "-O1" ]
Armando Montanez050eae82019-12-10 14:23:36 -0800111 additional_cflags += software_fpu_cflags
Alexei Frolov1a82c142019-10-31 17:37:12 -0700112 },
113 {
114 toolchain_name = "arm_gcc_cortex_m4_o2"
115 additional_cflags = [ "-O2" ]
Armando Montanez050eae82019-12-10 14:23:36 -0800116 additional_cflags += software_fpu_cflags
Alexei Frolov1a82c142019-10-31 17:37:12 -0700117 },
118 {
119 toolchain_name = "arm_gcc_cortex_m4_os"
120 additional_cflags = [ "-Os" ]
Armando Montanez050eae82019-12-10 14:23:36 -0800121 additional_cflags += software_fpu_cflags
122 },
123
124 # Cortex-M4 toolchains that use hardware FPU instructions.
125 {
126 toolchain_name = "arm_gcc_cortex_m4f_og"
127 additional_cflags = [ "-Og" ]
128 additional_cflags += hardware_fpu_cflags
129 },
130 {
131 toolchain_name = "arm_gcc_cortex_m4f_o1"
132 additional_cflags = [ "-O1" ]
133 additional_cflags += hardware_fpu_cflags
134 },
135 {
136 toolchain_name = "arm_gcc_cortex_m4f_o2"
137 additional_cflags = [ "-O2" ]
138 additional_cflags += hardware_fpu_cflags
139 },
140 {
141 toolchain_name = "arm_gcc_cortex_m4f_os"
142 additional_cflags = [ "-Os" ]
143 additional_cflags += hardware_fpu_cflags
Alexei Frolov1a82c142019-10-31 17:37:12 -0700144 },
145 ]
146}
Alexei Frolovc10c8122019-11-01 16:31:19 -0700147
Wyatt Heplercca0daf2019-11-14 11:29:04 -0800148generate_toolchains("host_gcc_suite") {
149 toolchain_template = "host_gcc"
Alexei Frolovc10c8122019-11-01 16:31:19 -0700150
151 toolchains = [
152 {
Wyatt Heplercca0daf2019-11-14 11:29:04 -0800153 toolchain_name = "host_gcc_og"
154 additional_cflags = [ "-Og" ]
155 },
156 {
157 toolchain_name = "host_gcc_o2"
Alexei Frolovc10c8122019-11-01 16:31:19 -0700158 additional_cflags = [ "-O2" ]
159 },
Wyatt Heplercca0daf2019-11-14 11:29:04 -0800160 {
161 toolchain_name = "host_gcc_os"
162 additional_cflags = [ "-Os" ]
163 },
Alexei Frolovc10c8122019-11-01 16:31:19 -0700164 ]
165}
Keir Mierle58671912019-11-14 00:15:41 -0800166
167generate_toolchains("host_clang_suite") {
168 toolchain_template = "host_clang"
Wyatt Heplerbecb4312019-12-04 09:12:15 -0800169 common_toolchain_cflags = [ "-g3" ]
Keir Mierle58671912019-11-14 00:15:41 -0800170
171 toolchains = [
172 {
173 toolchain_name = "host_clang_og"
174 additional_cflags = [ "-Og" ]
175 },
176 {
177 toolchain_name = "host_clang_o2"
178 additional_cflags = [ "-O2" ]
179 },
180 {
181 toolchain_name = "host_clang_os"
182 additional_cflags = [ "-Os" ]
183 },
184 ]
185}