blob: 3ab0527c29eb538ab744993e57bf9da4fa7bc3f5 [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
Armando Montanezfb3d3fb2020-06-09 18:12:12 -070015# gn-format disable
16import("//build_overrides/pigweed.gni")
17
18import("$dir_pigweed/legacy_target.gni")
Armando Montanez70095662020-01-09 14:25:04 -080019import("$dir_pw_build/exec.gni")
Alexei Frolovedd2f142020-06-09 19:11:27 -070020import("$dir_pw_build/target_types.gni")
Armando Montanez70095662020-01-09 14:25:04 -080021
22# Preprocess a linker script and turn it into a target.
23#
24# Note: to use this template, pw_cc_command must be specified in your target's
25# config. It should match the name of the C compiler your target uses (e.g.
26# arm-none-eabi-gcc).
27#
28# In lieu of direct GN support for linker scripts, this template makes it
29# possible to run the C Preprocessor on a linker script file so defines can
30# be properly evaluated before the linker script is passed to the dir_pw_build
31#
32# TODO(pwbug/53): This template serves as a stand-in until native GN support for
33# linker scripts is added.
34#
35# Args:
36# linker_script: The linker script to send through the C preprocessor.
37#
38# defines: Preprocessor defines to apply when running the C preprocessor.
39#
40# cflags: Flags to pass to the C compiler.
41#
42# inputs: Files that, when changed, should trigger a re-build of the linker
43# script. linker_script is implicitly added to this by the template.
44#
45# Example:
46#
47# pw_linker_script("generic_linker_script") {
48# defines = [
49# "PW_HEAP_SIZE=1K",
50# "PW_NOINIT_SIZE=512"
51# ]
52# linker_script = "basic_script.ld"
53# }
54#
55template("pw_linker_script") {
56 assert(
57 defined(pw_cc_command) && pw_cc_command != "",
58 "pw_cc_command has not been properly configured. This variable must be " +
59 "defined to enable linker script preprocessing.")
60
61 assert(
62 defined(invoker.linker_script) && invoker.linker_script != "",
63 "$target_name did not set `linker_script` to refer to a valid linker " +
64 "script. This variable is required for linker script targets.")
65
66 _final_linker_script = "${target_gen_dir}/${target_name}_final.ld"
67
68 # This action invokes the C compiler provided by the target to preprocess the
69 # linker script.
70 pw_exec("${target_name}_preprocess") {
71 program = pw_cc_command
Rob Mohra0ba54f2020-02-27 11:43:49 -080072 inputs = [ invoker.linker_script ]
Armando Montanez70095662020-01-09 14:25:04 -080073 args = [
74 # Run compiler in preprocessor-only mode.
75 "-E",
76
77 # Do not generate linemarkers in output.
78 "-P",
79
80 # Do not discard comments.
81 "-C",
82
83 # Treat the following file as a C file.
84 "-x",
85 "c",
Alexei Frolovee945602020-01-23 12:51:49 -080086 get_path_info(invoker.linker_script, "abspath"),
Armando Montanez70095662020-01-09 14:25:04 -080087 ]
88
89 # Include any explicitly listed c flags.
90 if (defined(invoker.cflags)) {
91 args += cflags
92 }
93
94 # Add defines.
95 if (defined(invoker.defines)) {
96 args += process_file_template(invoker.defines, "-D{{source_name_part}}")
97 }
98
99 # Set output file.
100 args += [
101 "-o",
102 _final_linker_script,
103 ]
Rob Mohra0ba54f2020-02-27 11:43:49 -0800104 outputs = [ _final_linker_script ]
Armando Montanez70095662020-01-09 14:25:04 -0800105 }
106
107 # This config adds a the linker script produced by the preprocess action to
108 # the linker flags.
109 config("${target_name}_config") {
Rob Mohra0ba54f2020-02-27 11:43:49 -0800110 inputs = [ invoker.linker_script ]
Armando Montanez70095662020-01-09 14:25:04 -0800111 if (!defined(invoker.ldflags)) {
112 ldflags = []
113 }
114 ldflags += [ "-T" + rebase_path(_final_linker_script) ]
115 }
116
117 # The target that adds the linker script config to this library and everything
118 # that depends on it.
Alexei Frolovedd2f142020-06-09 19:11:27 -0700119 pw_source_set(target_name) {
Rob Mohra0ba54f2020-02-27 11:43:49 -0800120 inputs = [ _final_linker_script ]
Armando Montanez70095662020-01-09 14:25:04 -0800121 if (defined(invoker.inputs)) {
122 inputs += invoker.inputs
123 }
124 all_dependent_configs = [ ":${target_name}_config" ]
Rob Mohra0ba54f2020-02-27 11:43:49 -0800125 deps = [ ":${target_name}_preprocess" ]
Armando Montanez70095662020-01-09 14:25:04 -0800126 }
127}