blob: 8c711ea3c18b0b09755886bfd9befe32163beb9f [file] [log] [blame]
Armando Montanez70095662020-01-09 14:25:04 -08001# 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
15import("$dir_pw_build/exec.gni")
16
17# Preprocess a linker script and turn it into a target.
18#
19# Note: to use this template, pw_cc_command must be specified in your target's
20# config. It should match the name of the C compiler your target uses (e.g.
21# arm-none-eabi-gcc).
22#
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#
50template("pw_linker_script") {
51 assert(
52 defined(pw_cc_command) && pw_cc_command != "",
53 "pw_cc_command has not been properly configured. This variable must be " +
54 "defined to enable linker script preprocessing.")
55
56 assert(
57 defined(invoker.linker_script) && invoker.linker_script != "",
58 "$target_name did not set `linker_script` to refer to a valid linker " +
59 "script. This variable is required for linker script targets.")
60
61 _final_linker_script = "${target_gen_dir}/${target_name}_final.ld"
62
63 # This action invokes the C compiler provided by the target to preprocess the
64 # linker script.
65 pw_exec("${target_name}_preprocess") {
66 program = pw_cc_command
67 inputs = [
68 invoker.linker_script,
69 ]
70 args = [
71 # Run compiler in preprocessor-only mode.
72 "-E",
73
74 # Do not generate linemarkers in output.
75 "-P",
76
77 # Do not discard comments.
78 "-C",
79
80 # Treat the following file as a C file.
81 "-x",
82 "c",
Alexei Frolovee945602020-01-23 12:51:49 -080083 get_path_info(invoker.linker_script, "abspath"),
Armando Montanez70095662020-01-09 14:25:04 -080084 ]
85
86 # Include any explicitly listed c flags.
87 if (defined(invoker.cflags)) {
88 args += cflags
89 }
90
91 # Add defines.
92 if (defined(invoker.defines)) {
93 args += process_file_template(invoker.defines, "-D{{source_name_part}}")
94 }
95
96 # Set output file.
97 args += [
98 "-o",
99 _final_linker_script,
100 ]
101 outputs = [
102 _final_linker_script,
103 ]
104 }
105
106 # This config adds a the linker script produced by the preprocess action to
107 # the linker flags.
108 config("${target_name}_config") {
109 inputs = [
110 invoker.linker_script,
111 ]
112 if (!defined(invoker.ldflags)) {
113 ldflags = []
114 }
115 ldflags += [ "-T" + rebase_path(_final_linker_script) ]
116 }
117
118 # The target that adds the linker script config to this library and everything
119 # that depends on it.
120 source_set(target_name) {
121 inputs = [
122 _final_linker_script,
123 ]
124 if (defined(invoker.inputs)) {
125 inputs += invoker.inputs
126 }
127 all_dependent_configs = [ ":${target_name}_config" ]
128 deps = [
129 ":${target_name}_preprocess",
130 ]
131 }
132}