third_party.pigweed.src/pw_toolchain/non_c_toolchain.gni

107 lines
3.6 KiB
Plaintext
Raw Normal View History

# Copyright 2021 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.
import("//build_overrides/pigweed.gni")
# Creates a toolchain that provides no C/C++ compiler. It can be used for
# non-C/C++ languages or actions that should only happen once across all builds.
# The toolchain cannot compile C/C++, and trying to use it to is an error.
#
# Args:
# command: Run this command if this toolchain is used to build C/C++ code.
#
template("pw_non_c_toolchain") {
# Import the universal stamp & copy tools.
import("$dir_pw_toolchain/universal_tools.gni")
_label = get_label_info(":$target_name", "label_no_toolchain")
# If the user tries to build a target with this toolchain, run a script that
# prints out an error.
_message =
"Attempted to use the $target_name toolchain to compile {{source}}.\n" +
"This toolchain cannot be used to compile C/C++ source code.\n\n" +
"This toolchain was either explicitly specified in a deps list with\n" +
"GN's :target($_label) syntax or was set as the\n" +
"default toolchain in the BUILDCONFIG.gn file.\n\n" +
"Ensure that no C/C++ GN targets are referred to with this toolchain,\n" +
"even transitively.\n\n" +
"See https://pigweed.dev/pw_toolchain for more information."
_command = string_join(" ",
[
"python",
Remove absolute paths from build command lines The GN convention is to specify paths in command lines relative to the build directory. Unfortunately and contrary to pigweed's expectations this is not what rebase_path(path) does; that outputs an absolute path. Absolute paths are not desirable in most circumstances as they contain sources of nondeterminism such as the developer's home directory. Using them can for example reduce hit rate in build caches. Replace rebase_path(path) with rebase_path(path, root_build_dir) which is the correct idiom and matches GN's builtin behavior (e.g. for sources, include_dirs, etc). This also removes the --directory argument to python_action(). Changing the directory during the build while using relative paths is likely to result in confusion and should be discouraged. There's a couple more things to do on top of this for identical binaries between build directories / machines / developers: - pass options to avoid embedding the working directory - pass options to use relative paths for the vendored clang & libc++ See [1]-[2] for how to do that. [1] https://source.chromium.org/chromium/chromium/src/+/main:build/config/compiler/BUILD.gn;l=1170-1239;drc=ab531c265c533cba1c2f6d8240cc0bf7679f605a [2] https://cs.opensource.google/fuchsia/fuchsia/+/main:build/config/BUILD.gn;l=145-216;drc=f6d705f0937c778d5d5f807a4580113612b02f5a Change-Id: I17708102c03d6488d68c8571b6e9343191fd47de Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/47461 Reviewed-by: Wyatt Hepler <hepler@google.com> Commit-Queue: Michael Spang <spang@google.com>
2021-05-30 19:53:56 +00:00
rebase_path("$dir_pw_build/py/pw_build/error.py",
root_build_dir),
"--message \"$_message\"",
"--target",
_label,
"--root",
Remove absolute paths from build command lines The GN convention is to specify paths in command lines relative to the build directory. Unfortunately and contrary to pigweed's expectations this is not what rebase_path(path) does; that outputs an absolute path. Absolute paths are not desirable in most circumstances as they contain sources of nondeterminism such as the developer's home directory. Using them can for example reduce hit rate in build caches. Replace rebase_path(path) with rebase_path(path, root_build_dir) which is the correct idiom and matches GN's builtin behavior (e.g. for sources, include_dirs, etc). This also removes the --directory argument to python_action(). Changing the directory during the build while using relative paths is likely to result in confusion and should be discouraged. There's a couple more things to do on top of this for identical binaries between build directories / machines / developers: - pass options to avoid embedding the working directory - pass options to use relative paths for the vendored clang & libc++ See [1]-[2] for how to do that. [1] https://source.chromium.org/chromium/chromium/src/+/main:build/config/compiler/BUILD.gn;l=1170-1239;drc=ab531c265c533cba1c2f6d8240cc0bf7679f605a [2] https://cs.opensource.google/fuchsia/fuchsia/+/main:build/config/BUILD.gn;l=145-216;drc=f6d705f0937c778d5d5f807a4580113612b02f5a Change-Id: I17708102c03d6488d68c8571b6e9343191fd47de Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/47461 Reviewed-by: Wyatt Hepler <hepler@google.com> Commit-Queue: Michael Spang <spang@google.com>
2021-05-30 19:53:56 +00:00
rebase_path("//", root_build_dir),
"--out",
Remove absolute paths from build command lines The GN convention is to specify paths in command lines relative to the build directory. Unfortunately and contrary to pigweed's expectations this is not what rebase_path(path) does; that outputs an absolute path. Absolute paths are not desirable in most circumstances as they contain sources of nondeterminism such as the developer's home directory. Using them can for example reduce hit rate in build caches. Replace rebase_path(path) with rebase_path(path, root_build_dir) which is the correct idiom and matches GN's builtin behavior (e.g. for sources, include_dirs, etc). This also removes the --directory argument to python_action(). Changing the directory during the build while using relative paths is likely to result in confusion and should be discouraged. There's a couple more things to do on top of this for identical binaries between build directories / machines / developers: - pass options to avoid embedding the working directory - pass options to use relative paths for the vendored clang & libc++ See [1]-[2] for how to do that. [1] https://source.chromium.org/chromium/chromium/src/+/main:build/config/compiler/BUILD.gn;l=1170-1239;drc=ab531c265c533cba1c2f6d8240cc0bf7679f605a [2] https://cs.opensource.google/fuchsia/fuchsia/+/main:build/config/BUILD.gn;l=145-216;drc=f6d705f0937c778d5d5f807a4580113612b02f5a Change-Id: I17708102c03d6488d68c8571b6e9343191fd47de Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/47461 Reviewed-by: Wyatt Hepler <hepler@google.com> Commit-Queue: Michael Spang <spang@google.com>
2021-05-30 19:53:56 +00:00
".",
])
if (defined(invoker.command)) {
_command = invoker.command
} else {
not_needed([ "invoker" ])
}
toolchain(target_name) {
tool("stamp") {
forward_variables_from(pw_universal_stamp, "*")
}
tool("copy") {
forward_variables_from(pw_universal_copy, "*")
}
tool("asm") {
command = _command
outputs =
[ "{{source_out_dir}}/{{target_output_name}}.{{source_file_part}}.o" ]
}
tool("cc") {
command = _command
outputs =
[ "{{source_out_dir}}/{{target_output_name}}.{{source_file_part}}.o" ]
}
tool("cxx") {
command = _command
outputs =
[ "{{source_out_dir}}/{{target_output_name}}.{{source_file_part}}.o" ]
}
# Can't use {{source}} for the linker, so replace it if it's in the command.
_command_no_source = string_replace(_command, "{{source}}", "C/C++ sources")
tool("link") {
command = _command_no_source
outputs = [ "{{output_dir}}/{{target_output_name}}{{output_extension}}" ]
}
tool("alink") {
command = _command_no_source
outputs = [ "{{output_dir}}/{{target_output_name}}{{output_extension}}" ]
}
tool("solink") {
command = _command_no_source
outputs = [ "{{output_dir}}/{{target_output_name}}{{output_extension}}" ]
}
}
}