blob: aff8afac431389f41523c40f8d164068d37d6693 [file] [log] [blame]
# 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("python_action.gni")
import("target_types.gni")
# Creates a pw_source_set that pulls in a static library hosted in CIPD.
#
# Let's say you have a package with a static library:
# CIPD package path: `pigweed/third_party/libsomething`
# Files:
# ./libsomething/include/something.h
# ./libsomething/libsomething.a
# Installed by your manifest at //tools/my_packages.json.
#
# Your usage might look like this:
#
# import("$dir_pw_build/copy_from_cipd.gni")
#
# # This target links in the .a file.
# pw_cipd_static_library("libsomething") {
# manifest = "//tools/my_packages.json"
# library_path = "libsomething/libsomething.a"
# cipd_package = "pigweed/third_party/libsomething"
# }
#
# # This target needs libsomething.a to be linked in.
# pw_source_set("math_stuff") {
# sources = [ "optimized_math.cc" ]
# ...
# deps = [ ":libsomething" ]
# }
#
# Args:
# manifest: (required) GN-style path to the JSON manifest file that the
# package of interest is defined in.
# library_path: (required) The path of the static library of interest,
# relative to the root where the manifest's CIPD packages were installed to.
# e.g. if the package is installed by pigweed.json, and the .a file is at
# //.environment/cipd/pigweed/libs/libsomething.a, this argument should be
# "libs/libsomething.a".
# cipd_package: (required) The name of the CIPD package. This is the "path" of
# the package in the manifest file.
#
template("pw_cipd_static_library") {
_out_dir = "${target_out_dir}/cipd/$target_name"
_out_relative = rebase_path(_out_dir, root_build_dir)
_out_file = "$_out_dir/${invoker.library_path}"
_manifest_path = rebase_path(invoker.manifest, root_build_dir)
pw_python_action("$target_name.check_copy") {
module = "pw_build.copy_from_cipd"
args = [
"--package-name=${invoker.cipd_package}",
"--manifest=${_manifest_path}",
"--file=${invoker.library_path}",
"--out-dir=${_out_relative}",
]
# Parallel calls might both be invoking CIPD on the same directory which
# might work but is redundant, so serialize calls.
pool = "$dir_pw_build/pool:copy_from_cipd($default_toolchain)"
# TODO(pwbug/335): This should somehow track the actual .a for changes as
# well.
inputs = [ invoker.manifest ]
outputs = [ _out_file ]
python_deps = [ "$dir_pw_build/py" ]
}
pw_source_set(target_name) {
forward_variables_from(invoker,
"*",
[
"manifest",
"library_path",
"cipd_package",
])
if (!defined(libs)) {
libs = []
}
if (!defined(deps)) {
deps = []
}
libs += [ _out_file ]
deps += [ ":$target_name.check_copy" ]
}
}