Alexei Frolov | 1a82c14 | 2019-10-31 17:37:12 -0700 | [diff] [blame] | 1 | # 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 | |
| 15 | import("arm_gcc.gni") |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 16 | import("host_clang.gni") |
Wyatt Hepler | cca0daf | 2019-11-14 11:29:04 -0800 | [diff] [blame] | 17 | import("host_gcc.gni") |
Alexei Frolov | 1a82c14 | 2019-10-31 17:37:12 -0700 | [diff] [blame] | 18 | |
| 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. |
| 30 | template("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 | |
| 74 | generate_toolchains("cortex_m4") { |
| 75 | toolchain_template = "arm_gcc_toolchain" |
| 76 | |
Armando Montanez | 050eae8 | 2019-12-10 14:23:36 -0800 | [diff] [blame] | 77 | 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 Frolov | 1a82c14 | 2019-10-31 17:37:12 -0700 | [diff] [blame] | 88 | common_toolchain_cflags = [ |
| 89 | "-mabi=aapcs", |
| 90 | "-mcpu=cortex-m4", |
Alexei Frolov | 1a82c14 | 2019-10-31 17:37:12 -0700 | [diff] [blame] | 91 | "-mthumb", |
Armando Montanez | 4f80334 | 2019-11-18 11:14:12 -0800 | [diff] [blame] | 92 | "-specs=nano.specs", |
| 93 | "-specs=nosys.specs", |
Alexei Frolov | 1a82c14 | 2019-10-31 17:37:12 -0700 | [diff] [blame] | 94 | ] |
| 95 | |
| 96 | common_toolchain_ldflags = [ |
Alexei Frolov | 1a82c14 | 2019-10-31 17:37:12 -0700 | [diff] [blame] | 97 | "-lnosys", |
| 98 | "-lc", |
| 99 | ] |
| 100 | |
| 101 | toolchains = [ |
Armando Montanez | 050eae8 | 2019-12-10 14:23:36 -0800 | [diff] [blame] | 102 | # Cortex-M4 toolchains that use software-emulated floating point. |
Alexei Frolov | 1a82c14 | 2019-10-31 17:37:12 -0700 | [diff] [blame] | 103 | { |
| 104 | toolchain_name = "arm_gcc_cortex_m4_og" |
| 105 | additional_cflags = [ "-Og" ] |
Armando Montanez | 050eae8 | 2019-12-10 14:23:36 -0800 | [diff] [blame] | 106 | additional_cflags += software_fpu_cflags |
Alexei Frolov | 1a82c14 | 2019-10-31 17:37:12 -0700 | [diff] [blame] | 107 | }, |
| 108 | { |
| 109 | toolchain_name = "arm_gcc_cortex_m4_o1" |
| 110 | additional_cflags = [ "-O1" ] |
Armando Montanez | 050eae8 | 2019-12-10 14:23:36 -0800 | [diff] [blame] | 111 | additional_cflags += software_fpu_cflags |
Alexei Frolov | 1a82c14 | 2019-10-31 17:37:12 -0700 | [diff] [blame] | 112 | }, |
| 113 | { |
| 114 | toolchain_name = "arm_gcc_cortex_m4_o2" |
| 115 | additional_cflags = [ "-O2" ] |
Armando Montanez | 050eae8 | 2019-12-10 14:23:36 -0800 | [diff] [blame] | 116 | additional_cflags += software_fpu_cflags |
Alexei Frolov | 1a82c14 | 2019-10-31 17:37:12 -0700 | [diff] [blame] | 117 | }, |
| 118 | { |
| 119 | toolchain_name = "arm_gcc_cortex_m4_os" |
| 120 | additional_cflags = [ "-Os" ] |
Armando Montanez | 050eae8 | 2019-12-10 14:23:36 -0800 | [diff] [blame] | 121 | 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 Frolov | 1a82c14 | 2019-10-31 17:37:12 -0700 | [diff] [blame] | 144 | }, |
| 145 | ] |
| 146 | } |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 147 | |
Wyatt Hepler | cca0daf | 2019-11-14 11:29:04 -0800 | [diff] [blame] | 148 | generate_toolchains("host_gcc_suite") { |
| 149 | toolchain_template = "host_gcc" |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 150 | |
| 151 | toolchains = [ |
| 152 | { |
Wyatt Hepler | cca0daf | 2019-11-14 11:29:04 -0800 | [diff] [blame] | 153 | toolchain_name = "host_gcc_og" |
| 154 | additional_cflags = [ "-Og" ] |
| 155 | }, |
| 156 | { |
| 157 | toolchain_name = "host_gcc_o2" |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 158 | additional_cflags = [ "-O2" ] |
| 159 | }, |
Wyatt Hepler | cca0daf | 2019-11-14 11:29:04 -0800 | [diff] [blame] | 160 | { |
| 161 | toolchain_name = "host_gcc_os" |
| 162 | additional_cflags = [ "-Os" ] |
| 163 | }, |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 164 | ] |
| 165 | } |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 166 | |
| 167 | generate_toolchains("host_clang_suite") { |
| 168 | toolchain_template = "host_clang" |
Wyatt Hepler | becb431 | 2019-12-04 09:12:15 -0800 | [diff] [blame] | 169 | common_toolchain_cflags = [ "-g3" ] |
Keir Mierle | 5867191 | 2019-11-14 00:15:41 -0800 | [diff] [blame] | 170 | |
| 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 | } |