Armando Montanez | 7009566 | 2020-01-09 14:25:04 -0800 | [diff] [blame] | 1 | # Copyright 2020 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 | |
Armando Montanez | fb3d3fb | 2020-06-09 18:12:12 -0700 | [diff] [blame] | 15 | import("//build_overrides/pigweed.gni") |
| 16 | |
Armando Montanez | 7009566 | 2020-01-09 14:25:04 -0800 | [diff] [blame] | 17 | import("$dir_pw_build/exec.gni") |
Alexei Frolov | edd2f14 | 2020-06-09 19:11:27 -0700 | [diff] [blame] | 18 | import("$dir_pw_build/target_types.gni") |
Armando Montanez | fd5de70 | 2020-06-10 16:51:01 -0700 | [diff] [blame] | 19 | import("$dir_pw_toolchain/generate_toolchain.gni") |
Armando Montanez | 7009566 | 2020-01-09 14:25:04 -0800 | [diff] [blame] | 20 | |
| 21 | # Preprocess a linker script and turn it into a target. |
| 22 | # |
Armando Montanez | 7009566 | 2020-01-09 14:25:04 -0800 | [diff] [blame] | 23 | # In lieu of direct GN support for linker scripts, this template makes it |
| 24 | # possible to run the C Preprocessor on a linker script file so defines can |
| 25 | # be properly evaluated before the linker script is passed to the dir_pw_build |
| 26 | # |
| 27 | # TODO(pwbug/53): This template serves as a stand-in until native GN support for |
| 28 | # linker scripts is added. |
| 29 | # |
| 30 | # Args: |
| 31 | # linker_script: The linker script to send through the C preprocessor. |
| 32 | # |
| 33 | # defines: Preprocessor defines to apply when running the C preprocessor. |
| 34 | # |
| 35 | # cflags: Flags to pass to the C compiler. |
| 36 | # |
| 37 | # inputs: Files that, when changed, should trigger a re-build of the linker |
| 38 | # script. linker_script is implicitly added to this by the template. |
| 39 | # |
| 40 | # Example: |
| 41 | # |
| 42 | # pw_linker_script("generic_linker_script") { |
| 43 | # defines = [ |
| 44 | # "PW_HEAP_SIZE=1K", |
| 45 | # "PW_NOINIT_SIZE=512" |
| 46 | # ] |
| 47 | # linker_script = "basic_script.ld" |
| 48 | # } |
| 49 | # |
| 50 | template("pw_linker_script") { |
| 51 | assert( |
Armando Montanez | 7009566 | 2020-01-09 14:25:04 -0800 | [diff] [blame] | 52 | defined(invoker.linker_script) && invoker.linker_script != "", |
| 53 | "$target_name did not set `linker_script` to refer to a valid linker " + |
| 54 | "script. This variable is required for linker script targets.") |
| 55 | |
| 56 | _final_linker_script = "${target_gen_dir}/${target_name}_final.ld" |
| 57 | |
| 58 | # This action invokes the C compiler provided by the target to preprocess the |
| 59 | # linker script. |
| 60 | pw_exec("${target_name}_preprocess") { |
Armando Montanez | fd5de70 | 2020-06-10 16:51:01 -0700 | [diff] [blame] | 61 | program = pw_toolchain_SCOPE.cxx |
Rob Mohr | a0ba54f | 2020-02-27 11:43:49 -0800 | [diff] [blame] | 62 | inputs = [ invoker.linker_script ] |
Armando Montanez | 7009566 | 2020-01-09 14:25:04 -0800 | [diff] [blame] | 63 | args = [ |
| 64 | # Run compiler in preprocessor-only mode. |
| 65 | "-E", |
| 66 | |
| 67 | # Do not generate linemarkers in output. |
| 68 | "-P", |
| 69 | |
| 70 | # Do not discard comments. |
| 71 | "-C", |
| 72 | |
| 73 | # Treat the following file as a C file. |
| 74 | "-x", |
| 75 | "c", |
Wyatt Hepler | 8224a64 | 2020-07-29 08:55:56 -0700 | [diff] [blame] | 76 | rebase_path(invoker.linker_script), |
Armando Montanez | 7009566 | 2020-01-09 14:25:04 -0800 | [diff] [blame] | 77 | ] |
| 78 | |
| 79 | # Include any explicitly listed c flags. |
| 80 | if (defined(invoker.cflags)) { |
| 81 | args += cflags |
| 82 | } |
| 83 | |
| 84 | # Add defines. |
| 85 | if (defined(invoker.defines)) { |
Armando Montanez | 4eb4218 | 2020-10-15 16:59:17 -0700 | [diff] [blame] | 86 | foreach(compiler_define, invoker.defines) { |
| 87 | args += [ "-D${compiler_define}" ] |
| 88 | } |
Armando Montanez | 7009566 | 2020-01-09 14:25:04 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | # Set output file. |
| 92 | args += [ |
| 93 | "-o", |
Wyatt Hepler | 8224a64 | 2020-07-29 08:55:56 -0700 | [diff] [blame] | 94 | rebase_path(_final_linker_script), |
Armando Montanez | 7009566 | 2020-01-09 14:25:04 -0800 | [diff] [blame] | 95 | ] |
Rob Mohr | a0ba54f | 2020-02-27 11:43:49 -0800 | [diff] [blame] | 96 | outputs = [ _final_linker_script ] |
Armando Montanez | 7009566 | 2020-01-09 14:25:04 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | # This config adds a the linker script produced by the preprocess action to |
| 100 | # the linker flags. |
| 101 | config("${target_name}_config") { |
Rob Mohr | a0ba54f | 2020-02-27 11:43:49 -0800 | [diff] [blame] | 102 | inputs = [ invoker.linker_script ] |
Armando Montanez | 7009566 | 2020-01-09 14:25:04 -0800 | [diff] [blame] | 103 | if (!defined(invoker.ldflags)) { |
| 104 | ldflags = [] |
| 105 | } |
| 106 | ldflags += [ "-T" + rebase_path(_final_linker_script) ] |
| 107 | } |
| 108 | |
| 109 | # The target that adds the linker script config to this library and everything |
| 110 | # that depends on it. |
Alexei Frolov | edd2f14 | 2020-06-09 19:11:27 -0700 | [diff] [blame] | 111 | pw_source_set(target_name) { |
Rob Mohr | a0ba54f | 2020-02-27 11:43:49 -0800 | [diff] [blame] | 112 | inputs = [ _final_linker_script ] |
Armando Montanez | 7009566 | 2020-01-09 14:25:04 -0800 | [diff] [blame] | 113 | if (defined(invoker.inputs)) { |
| 114 | inputs += invoker.inputs |
| 115 | } |
| 116 | all_dependent_configs = [ ":${target_name}_config" ] |
Rob Mohr | a0ba54f | 2020-02-27 11:43:49 -0800 | [diff] [blame] | 117 | deps = [ ":${target_name}_preprocess" ] |
Armando Montanez | 7009566 | 2020-01-09 14:25:04 -0800 | [diff] [blame] | 118 | } |
| 119 | } |