xref: /aosp_15_r20/external/pigweed/pw_build/copy_from_cipd.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2021 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("python_action.gni")
16import("target_types.gni")
17
18# Creates a pw_source_set that pulls in a static library hosted in CIPD.
19#
20# Let's say you have a package with a static library:
21# CIPD package path: `pigweed/third_party/libsomething`
22# Files:
23#   ./libsomething/include/something.h
24#   ./libsomething/libsomething.a
25# Installed by your manifest at //tools/my_packages.json.
26#
27# Your usage might look like this:
28#
29#   import("$dir_pw_build/copy_from_cipd.gni")
30#
31#   # This target links in the .a file.
32#   pw_cipd_static_library("libsomething") {
33#     manifest = "//tools/my_packages.json"
34#     library_path = "libsomething/libsomething.a"
35#     cipd_package = "pigweed/third_party/libsomething"
36#   }
37#
38#   # This target needs libsomething.a to be linked in.
39#   pw_source_set("math_stuff") {
40#     sources = [ "optimized_math.cc" ]
41#     ...
42#     deps = [ ":libsomething" ]
43#   }
44#
45# Args:
46#   manifest: (required) GN-style path to the JSON manifest file that the
47#     package of interest is defined in.
48#   library_path: (required) The path of the static library of interest,
49#     relative to the root where the manifest's CIPD packages were installed to.
50#     e.g. if the package is installed by pigweed.json, and the .a file is at
51#     //environment/cipd/packages/pigweed/libs/libsomething.a, this argument
52#     should be "libs/libsomething.a".
53#   cipd_package: (required) The name of the CIPD package. This is the "path" of
54#     the package in the manifest file.
55#
56template("pw_cipd_static_library") {
57  _out_dir = "${target_out_dir}/cipd/$target_name"
58  _out_relative = rebase_path(_out_dir, root_build_dir)
59  _out_file = "$_out_dir/${invoker.library_path}"
60  _manifest_path = rebase_path(invoker.manifest, root_build_dir)
61  pw_python_action("$target_name.check_copy") {
62    module = "pw_build.copy_from_cipd"
63    args = [
64      "--package-name=${invoker.cipd_package}",
65      "--manifest=${_manifest_path}",
66      "--file=${invoker.library_path}",
67      "--out-dir=${_out_relative}",
68    ]
69
70    # Parallel calls might both be invoking CIPD on the same directory which
71    # might work but is redundant, so serialize calls.
72    pool = "$dir_pw_build/pool:copy_from_cipd($default_toolchain)"
73
74    # TODO: b/234884827 - This should somehow track the actual .a for changes as
75    # well.
76    inputs = [ invoker.manifest ]
77    outputs = [ _out_file ]
78
79    python_deps = [ "$dir_pw_build/py" ]
80  }
81
82  pw_source_set(target_name) {
83    forward_variables_from(invoker,
84                           "*",
85                           [
86                             "manifest",
87                             "library_path",
88                             "cipd_package",
89                           ])
90    if (!defined(libs)) {
91      libs = []
92    }
93    if (!defined(deps)) {
94      deps = []
95    }
96    libs += [ _out_file ]
97    deps += [ ":$target_name.check_copy" ]
98  }
99}
100