aboutsummaryrefslogtreecommitdiff
path: root/pw_build/copy_from_cipd.gni
blob: 2f70c73d285325afa4650548ea6943e01cbf4660 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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/packages/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: b/234884827 - 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" ]
  }
}