2020-01-09 22:25:04 +00:00
|
|
|
# Copyright 2020 The Pigweed Authors
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
# use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
# the License at
|
|
|
|
#
|
|
|
|
# https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations under
|
|
|
|
# the License.
|
|
|
|
|
2020-06-10 01:12:12 +00:00
|
|
|
# gn-format disable
|
|
|
|
import("//build_overrides/pigweed.gni")
|
|
|
|
|
|
|
|
import("$dir_pigweed/legacy_target.gni")
|
2020-01-09 22:25:04 +00:00
|
|
|
import("$dir_pw_build/exec.gni")
|
2020-06-10 02:11:27 +00:00
|
|
|
import("$dir_pw_build/target_types.gni")
|
2020-01-09 22:25:04 +00:00
|
|
|
|
|
|
|
# Preprocess a linker script and turn it into a target.
|
|
|
|
#
|
|
|
|
# Note: to use this template, pw_cc_command must be specified in your target's
|
|
|
|
# config. It should match the name of the C compiler your target uses (e.g.
|
|
|
|
# arm-none-eabi-gcc).
|
|
|
|
#
|
|
|
|
# In lieu of direct GN support for linker scripts, this template makes it
|
|
|
|
# possible to run the C Preprocessor on a linker script file so defines can
|
|
|
|
# be properly evaluated before the linker script is passed to the dir_pw_build
|
|
|
|
#
|
|
|
|
# TODO(pwbug/53): This template serves as a stand-in until native GN support for
|
|
|
|
# linker scripts is added.
|
|
|
|
#
|
|
|
|
# Args:
|
|
|
|
# linker_script: The linker script to send through the C preprocessor.
|
|
|
|
#
|
|
|
|
# defines: Preprocessor defines to apply when running the C preprocessor.
|
|
|
|
#
|
|
|
|
# cflags: Flags to pass to the C compiler.
|
|
|
|
#
|
|
|
|
# inputs: Files that, when changed, should trigger a re-build of the linker
|
|
|
|
# script. linker_script is implicitly added to this by the template.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# pw_linker_script("generic_linker_script") {
|
|
|
|
# defines = [
|
|
|
|
# "PW_HEAP_SIZE=1K",
|
|
|
|
# "PW_NOINIT_SIZE=512"
|
|
|
|
# ]
|
|
|
|
# linker_script = "basic_script.ld"
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
template("pw_linker_script") {
|
|
|
|
assert(
|
|
|
|
defined(pw_cc_command) && pw_cc_command != "",
|
|
|
|
"pw_cc_command has not been properly configured. This variable must be " +
|
|
|
|
"defined to enable linker script preprocessing.")
|
|
|
|
|
|
|
|
assert(
|
|
|
|
defined(invoker.linker_script) && invoker.linker_script != "",
|
|
|
|
"$target_name did not set `linker_script` to refer to a valid linker " +
|
|
|
|
"script. This variable is required for linker script targets.")
|
|
|
|
|
|
|
|
_final_linker_script = "${target_gen_dir}/${target_name}_final.ld"
|
|
|
|
|
|
|
|
# This action invokes the C compiler provided by the target to preprocess the
|
|
|
|
# linker script.
|
|
|
|
pw_exec("${target_name}_preprocess") {
|
|
|
|
program = pw_cc_command
|
2020-02-27 19:43:49 +00:00
|
|
|
inputs = [ invoker.linker_script ]
|
2020-01-09 22:25:04 +00:00
|
|
|
args = [
|
|
|
|
# Run compiler in preprocessor-only mode.
|
|
|
|
"-E",
|
|
|
|
|
|
|
|
# Do not generate linemarkers in output.
|
|
|
|
"-P",
|
|
|
|
|
|
|
|
# Do not discard comments.
|
|
|
|
"-C",
|
|
|
|
|
|
|
|
# Treat the following file as a C file.
|
|
|
|
"-x",
|
|
|
|
"c",
|
2020-01-23 20:51:49 +00:00
|
|
|
get_path_info(invoker.linker_script, "abspath"),
|
2020-01-09 22:25:04 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# Include any explicitly listed c flags.
|
|
|
|
if (defined(invoker.cflags)) {
|
|
|
|
args += cflags
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add defines.
|
|
|
|
if (defined(invoker.defines)) {
|
|
|
|
args += process_file_template(invoker.defines, "-D{{source_name_part}}")
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set output file.
|
|
|
|
args += [
|
|
|
|
"-o",
|
|
|
|
_final_linker_script,
|
|
|
|
]
|
2020-02-27 19:43:49 +00:00
|
|
|
outputs = [ _final_linker_script ]
|
2020-01-09 22:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# This config adds a the linker script produced by the preprocess action to
|
|
|
|
# the linker flags.
|
|
|
|
config("${target_name}_config") {
|
2020-02-27 19:43:49 +00:00
|
|
|
inputs = [ invoker.linker_script ]
|
2020-01-09 22:25:04 +00:00
|
|
|
if (!defined(invoker.ldflags)) {
|
|
|
|
ldflags = []
|
|
|
|
}
|
|
|
|
ldflags += [ "-T" + rebase_path(_final_linker_script) ]
|
|
|
|
}
|
|
|
|
|
|
|
|
# The target that adds the linker script config to this library and everything
|
|
|
|
# that depends on it.
|
2020-06-10 02:11:27 +00:00
|
|
|
pw_source_set(target_name) {
|
2020-02-27 19:43:49 +00:00
|
|
|
inputs = [ _final_linker_script ]
|
2020-01-09 22:25:04 +00:00
|
|
|
if (defined(invoker.inputs)) {
|
|
|
|
inputs += invoker.inputs
|
|
|
|
}
|
|
|
|
all_dependent_configs = [ ":${target_name}_config" ]
|
2020-02-27 19:43:49 +00:00
|
|
|
deps = [ ":${target_name}_preprocess" ]
|
2020-01-09 22:25:04 +00:00
|
|
|
}
|
|
|
|
}
|