Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 1 | # Copyright 2021 The Pigweed Authors |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 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 | |
| 15 | import("//build_overrides/pigweed.gni") |
| 16 | |
Wyatt Hepler | b8e1360 | 2020-10-19 11:20:36 -0700 | [diff] [blame] | 17 | import("$dir_pw_build/input_group.gni") |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 18 | import("$dir_pw_build/mirror_tree.gni") |
Wyatt Hepler | b8e1360 | 2020-10-19 11:20:36 -0700 | [diff] [blame] | 19 | import("$dir_pw_build/python_action.gni") |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 20 | import("$dir_pw_protobuf_compiler/toolchain.gni") |
| 21 | |
| 22 | declare_args() { |
| 23 | # Python tasks, such as running tests and Pylint, are done in a single GN |
| 24 | # toolchain to avoid unnecessary duplication in the build. |
| 25 | pw_build_PYTHON_TOOLCHAIN = "$dir_pw_build/python_toolchain:python" |
Michael Spang | 01faaf7 | 2021-06-09 22:38:40 -0400 | [diff] [blame] | 26 | |
| 27 | # Constraints file selection (arguments to pip install --constraint). |
| 28 | # See pip help install. |
Ted Pudlik | 961706b | 2021-11-22 22:30:59 +0000 | [diff] [blame] | 29 | pw_build_PIP_CONSTRAINTS = |
| 30 | [ "$dir_pw_env_setup/py/pw_env_setup/virtualenv_setup/constraint.list" ] |
Anthony DiGirolamo | 6ab145b | 2022-01-20 20:52:50 -0800 | [diff] [blame] | 31 | |
| 32 | # If true, GN will run each Python test using the coverage command. A separate |
| 33 | # coverage data file for each test will be saved. To generate reports from |
| 34 | # this information run: pw presubmit --step gn_python_test_coverage |
| 35 | pw_build_PYTHON_TEST_COVERAGE = false |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 36 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 37 | |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 38 | # Python packages provide the following targets as $target_name.$subtarget. |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 39 | pw_python_package_subtargets = [ |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 40 | "tests", |
| 41 | "lint", |
| 42 | "lint.mypy", |
| 43 | "lint.pylint", |
| 44 | "install", |
| 45 | "wheel", |
Wyatt Hepler | 446f24c | 2021-03-24 09:29:35 -0700 | [diff] [blame] | 46 | |
| 47 | # Internal targets that directly depend on one another. |
| 48 | "_run_pip_install", |
| 49 | "_build_wheel", |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 50 | ] |
| 51 | |
Wyatt Hepler | b85cda4 | 2021-04-07 13:13:15 -0700 | [diff] [blame] | 52 | # Create aliases for subsargets when the target name matches the directory name. |
| 53 | # This allows //foo:foo.tests to be accessed as //foo:tests, for example. |
| 54 | template("_pw_create_aliases_if_name_matches_directory") { |
| 55 | not_needed([ "invoker" ]) |
| 56 | |
| 57 | if (get_label_info(":$target_name", "name") == |
| 58 | get_path_info(get_label_info(":$target_name", "dir"), "name")) { |
| 59 | foreach(subtarget, pw_python_package_subtargets) { |
| 60 | group(subtarget) { |
| 61 | public_deps = [ ":${invoker.target_name}.$subtarget" ] |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 67 | # Internal template that runs Mypy. |
| 68 | template("_pw_python_static_analysis_mypy") { |
| 69 | pw_python_action(target_name) { |
| 70 | module = "mypy" |
| 71 | args = [ |
| 72 | "--pretty", |
| 73 | "--show-error-codes", |
| 74 | ] |
| 75 | |
| 76 | if (defined(invoker.mypy_ini)) { |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 77 | args += |
| 78 | [ "--config-file=" + rebase_path(invoker.mypy_ini, root_build_dir) ] |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 79 | inputs = [ invoker.mypy_ini ] |
| 80 | } |
| 81 | |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 82 | args += rebase_path(invoker.sources, root_build_dir) |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 83 | |
| 84 | # Use this environment variable to force mypy to colorize output. |
| 85 | # See https://github.com/python/mypy/issues/7771 |
| 86 | environment = [ "MYPY_FORCE_COLOR=1" ] |
| 87 | |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 88 | stamp = true |
| 89 | |
| 90 | deps = invoker.deps |
| 91 | |
| 92 | foreach(dep, invoker.python_deps) { |
| 93 | deps += [ string_replace(dep, "(", ".lint.mypy(") ] |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | # Internal template that runs Pylint. |
| 99 | template("_pw_python_static_analysis_pylint") { |
| 100 | # Create a target to run pylint on each of the Python files in this |
| 101 | # package and its dependencies. |
| 102 | pw_python_action_foreach(target_name) { |
| 103 | module = "pylint" |
| 104 | args = [ |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 105 | rebase_path(".", root_build_dir) + "/{{source_target_relative}}", |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 106 | "--jobs=1", |
| 107 | "--output-format=colorized", |
| 108 | ] |
| 109 | |
| 110 | if (defined(invoker.pylintrc)) { |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 111 | args += [ "--rcfile=" + rebase_path(invoker.pylintrc, root_build_dir) ] |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 112 | inputs = [ invoker.pylintrc ] |
| 113 | } |
| 114 | |
| 115 | if (host_os == "win") { |
| 116 | # Allow CRLF on Windows, in case Git is set to switch line endings. |
| 117 | args += [ "--disable=unexpected-line-ending-format" ] |
| 118 | } |
| 119 | |
| 120 | sources = invoker.sources |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 121 | |
| 122 | stamp = "$target_gen_dir/{{source_target_relative}}.pylint.passed" |
| 123 | |
| 124 | public_deps = invoker.deps |
| 125 | |
| 126 | foreach(dep, invoker.python_deps) { |
| 127 | public_deps += [ string_replace(dep, "(", ".lint.pylint(") ] |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 132 | # Defines a Python package. GN Python packages contain several GN targets: |
| 133 | # |
| 134 | # - $name - Provides the Python files in the build, but does not take any |
| 135 | # actions. All subtargets depend on this target. |
| 136 | # - $name.lint - Runs static analyis tools on the Python code. This is a group |
| 137 | # of two subtargets: |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 138 | # - $name.lint.mypy - Runs mypy (if enabled). |
| 139 | # - $name.lint.pylint - Runs pylint (if enabled). |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 140 | # - $name.tests - Runs all tests for this package. |
Rob Mohr | fcf6037 | 2020-11-04 11:41:36 -0800 | [diff] [blame] | 141 | # - $name.install - Installs the package in a venv. |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 142 | # - $name.wheel - Builds a Python wheel for the package. |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 143 | # |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 144 | # All Python packages are instantiated with in pw_build_PYTHON_TOOLCHAIN, |
| 145 | # regardless of the current toolchain. This prevents Python-specific work, like |
| 146 | # running Pylint, from occurring multiple times in a build. |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 147 | # |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 148 | # Args: |
| 149 | # setup: List of setup file paths (setup.py or pyproject.toml & setup.cfg), |
| 150 | # which must all be in the same directory. |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 151 | # generate_setup: As an alternative to 'setup', generate setup files with the |
| 152 | # keywords in this scope. 'name' is required. |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 153 | # sources: Python sources files in the package. |
| 154 | # tests: Test files for this Python package. |
| 155 | # python_deps: Dependencies on other pw_python_packages in the GN build. |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 156 | # python_test_deps: Test-only pw_python_package dependencies. |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 157 | # other_deps: Dependencies on GN targets that are not pw_python_packages. |
| 158 | # inputs: Other files to track, such as package_data. |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 159 | # proto_library: A pw_proto_library target to embed in this Python package. |
| 160 | # generate_setup is required in place of setup if proto_library is used. |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 161 | # static_analysis: List of static analysis tools to run; "*" (default) runs |
| 162 | # all tools. The supported tools are "mypy" and "pylint". |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 163 | # pylintrc: Path to a pylintrc configuration file to use. If not |
| 164 | # provided, Pylint's default rcfile search is used. As this may |
| 165 | # use the the local user's configuration file, it is highly |
| 166 | # recommended to pass this option to specify the rcfile explicitly. |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 167 | # mypy_ini: Optional path to a mypy configuration file to use. If not |
| 168 | # provided, mypy's default configuration file search is used. mypy is |
| 169 | # executed from the package's setup directory, so mypy.ini files in that |
| 170 | # directory will take precedence over others. |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 171 | # |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 172 | template("pw_python_package") { |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 173 | # The Python targets are always instantiated in pw_build_PYTHON_TOOLCHAIN. Use |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 174 | # fully qualified labels so that the toolchain is not lost. |
| 175 | _other_deps = [] |
| 176 | if (defined(invoker.other_deps)) { |
| 177 | foreach(dep, invoker.other_deps) { |
| 178 | _other_deps += [ get_label_info(dep, "label_with_toolchain") ] |
| 179 | } |
| 180 | } |
| 181 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 182 | _python_deps = [] |
| 183 | if (defined(invoker.python_deps)) { |
| 184 | foreach(dep, invoker.python_deps) { |
| 185 | _python_deps += [ get_label_info(dep, "label_with_toolchain") ] |
| 186 | } |
| 187 | } |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 188 | |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 189 | # pw_python_script uses pw_python_package, but with a limited set of features. |
| 190 | # _pw_standalone signals that this target is actually a pw_python_script. |
| 191 | _is_package = !(defined(invoker._pw_standalone) && invoker._pw_standalone) |
| 192 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 193 | _generate_package = false |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 194 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 195 | # Check the generate_setup and import_protos args to determine if this package |
| 196 | # is generated. |
| 197 | if (_is_package) { |
| 198 | assert(defined(invoker.generate_setup) != defined(invoker.setup), |
| 199 | "Either 'setup' or 'generate_setup' (but not both) must provided") |
| 200 | |
| 201 | if (defined(invoker.proto_library)) { |
| 202 | assert(invoker.proto_library != "", "'proto_library' cannot be empty") |
| 203 | assert(defined(invoker.generate_setup), |
| 204 | "Python packages that import protos with 'proto_library' must " + |
| 205 | "use 'generate_setup' instead of 'setup'") |
| 206 | |
| 207 | _import_protos = [ invoker.proto_library ] |
Wyatt Hepler | cf184e8 | 2021-05-11 18:15:35 -0700 | [diff] [blame] | 208 | |
| 209 | # Depend on the dependencies of the proto library. |
| 210 | _proto = get_label_info(invoker.proto_library, "label_no_toolchain") |
| 211 | _toolchain = get_label_info(invoker.proto_library, "toolchain") |
| 212 | _python_deps += [ "$_proto.python._deps($_toolchain)" ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 213 | } else if (defined(invoker.generate_setup)) { |
| 214 | _import_protos = [] |
| 215 | } |
| 216 | |
| 217 | if (defined(invoker.generate_setup)) { |
| 218 | _generate_package = true |
| 219 | _setup_dir = "$target_gen_dir/$target_name.generated_python_package" |
| 220 | |
| 221 | if (defined(invoker.strip_prefix)) { |
| 222 | _source_root = invoker.strip_prefix |
| 223 | } else { |
| 224 | _source_root = "." |
| 225 | } |
| 226 | } else { |
| 227 | # Non-generated packages with sources provided need an __init__.py. |
| 228 | assert(!defined(invoker.sources) || invoker.sources == [] || |
| 229 | filter_include(invoker.sources, [ "*\b__init__.py" ]) != [], |
| 230 | "Python packages must have at least one __init__.py file") |
| 231 | |
| 232 | # Get the directories of the setup files. All must be in the same dir. |
| 233 | _setup_dirs = get_path_info(invoker.setup, "dir") |
| 234 | _setup_dir = _setup_dirs[0] |
| 235 | |
| 236 | foreach(dir, _setup_dirs) { |
| 237 | assert(dir == _setup_dir, |
| 238 | "All files in 'setup' must be in the same directory") |
| 239 | } |
| 240 | |
| 241 | assert(!defined(invoker.strip_prefix), |
| 242 | "'strip_prefix' may only be given if 'generate_setup' is provided") |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | # Process arguments defaults and set defaults. |
| 247 | |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 248 | _supported_static_analysis_tools = [ |
| 249 | "mypy", |
| 250 | "pylint", |
| 251 | ] |
| 252 | not_needed([ "_supported_static_analysis_tools" ]) |
| 253 | |
| 254 | # Argument: static_analysis (list of tool names or "*"); default = "*" (all) |
| 255 | if (!defined(invoker.static_analysis) || invoker.static_analysis == "*") { |
| 256 | _static_analysis = _supported_static_analysis_tools |
Keir Mierle | e65ddc8 | 2020-12-02 17:59:52 -0800 | [diff] [blame] | 257 | } else { |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 258 | _static_analysis = invoker.static_analysis |
| 259 | } |
| 260 | |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 261 | foreach(_tool, _static_analysis) { |
| 262 | assert(_supported_static_analysis_tools + [ _tool ] - [ _tool ] != |
| 263 | _supported_static_analysis_tools, |
| 264 | "'$_tool' is not a supported static analysis tool") |
Keir Mierle | e65ddc8 | 2020-12-02 17:59:52 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 267 | # Argument: sources (list) |
| 268 | _sources = [] |
| 269 | if (defined(invoker.sources)) { |
| 270 | if (_generate_package) { |
| 271 | foreach(source, rebase_path(invoker.sources, _source_root)) { |
| 272 | _sources += [ "$_setup_dir/$source" ] |
| 273 | } |
| 274 | } else { |
| 275 | _sources += invoker.sources |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 278 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 279 | # Argument: tests (list) |
| 280 | _test_sources = [] |
| 281 | if (defined(invoker.tests)) { |
| 282 | if (_generate_package) { |
| 283 | foreach(source, rebase_path(invoker.tests, _source_root)) { |
| 284 | _test_sources += [ "$_setup_dir/$source" ] |
| 285 | } |
| 286 | } else { |
| 287 | _test_sources += invoker.tests |
Wyatt Hepler | d7dc655 | 2020-10-21 16:16:21 -0700 | [diff] [blame] | 288 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 291 | # Argument: setup (list) |
| 292 | _setup_sources = [] |
| 293 | if (defined(invoker.setup)) { |
| 294 | _setup_sources = invoker.setup |
| 295 | } else if (_generate_package) { |
Anthony DiGirolamo | afb861c | 2021-08-12 16:48:04 -0700 | [diff] [blame] | 296 | _setup_sources = [ |
| 297 | "$_setup_dir/pyproject.toml", |
| 298 | "$_setup_dir/setup.cfg", |
| 299 | ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | # Argument: python_test_deps (list) |
| 303 | _python_test_deps = _python_deps # include all deps in test deps |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 304 | if (defined(invoker.python_test_deps)) { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 305 | foreach(dep, invoker.python_test_deps) { |
| 306 | _python_test_deps += [ get_label_info(dep, "label_with_toolchain") ] |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | if (_test_sources == []) { |
| 311 | assert(!defined(invoker.python_test_deps), |
| 312 | "python_test_deps was provided, but there are no tests in " + |
| 313 | get_label_info(":$target_name", "label_no_toolchain")) |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 314 | not_needed([ "_python_test_deps" ]) |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Anthony DiGirolamo | afb861c | 2021-08-12 16:48:04 -0700 | [diff] [blame] | 317 | _all_py_files = |
| 318 | _sources + _test_sources + filter_include(_setup_sources, [ "*.py" ]) |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 319 | |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 320 | # The pw_python_package subtargets are only instantiated in |
| 321 | # pw_build_PYTHON_TOOLCHAIN. Targets in other toolchains just refer to the |
| 322 | # targets in this toolchain. |
| 323 | if (current_toolchain == pw_build_PYTHON_TOOLCHAIN) { |
Anthony DiGirolamo | 211dce4 | 2021-08-12 16:48:43 -0700 | [diff] [blame] | 324 | # Create the package_metadata.json file. This is used by the |
| 325 | # pw_create_python_source_tree template. |
| 326 | _package_metadata_json_file = |
| 327 | "$target_gen_dir/$target_name/package_metadata.json" |
| 328 | |
| 329 | # Get Python package metadata and write to disk as JSON. |
| 330 | _package_metadata = { |
| 331 | gn_target_name = get_label_info(invoker.target_name, "label_no_toolchain") |
| 332 | |
| 333 | # Get package source files |
| 334 | sources = rebase_path(_sources, root_build_dir) |
| 335 | |
| 336 | # Get setup.cfg, pyproject.toml, or setup.py file |
| 337 | setup_sources = rebase_path(_setup_sources, root_build_dir) |
| 338 | |
| 339 | # Get test source files |
| 340 | tests = rebase_path(_test_sources, root_build_dir) |
| 341 | |
| 342 | # Get package input files (package data) |
| 343 | inputs = [] |
| 344 | if (defined(invoker.inputs)) { |
| 345 | inputs = rebase_path(invoker.inputs, root_build_dir) |
| 346 | } |
| 347 | |
| 348 | # Get generate_setup |
| 349 | if (defined(invoker.generate_setup)) { |
| 350 | generate_setup = invoker.generate_setup |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | # Finally, write out the json |
| 355 | write_file(_package_metadata_json_file, _package_metadata, "json") |
| 356 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 357 | # Declare the main Python package group. This represents the Python files, |
| 358 | # but does not take any actions. GN targets can depend on the package name |
| 359 | # to run when any files in the package change. |
| 360 | if (_generate_package) { |
| 361 | # If this package is generated, mirror the sources to the final directory. |
| 362 | pw_mirror_tree("$target_name._mirror_sources_to_out_dir") { |
| 363 | directory = _setup_dir |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 364 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 365 | sources = [] |
| 366 | if (defined(invoker.sources)) { |
| 367 | sources += invoker.sources |
| 368 | } |
| 369 | if (defined(invoker.tests)) { |
| 370 | sources += invoker.tests |
| 371 | } |
Wyatt Hepler | de20d74 | 2021-06-02 23:34:14 -0700 | [diff] [blame] | 372 | if (defined(invoker.inputs)) { |
| 373 | sources += invoker.inputs |
| 374 | } |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 375 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 376 | source_root = _source_root |
| 377 | public_deps = _python_deps + _other_deps |
Wyatt Hepler | 285636f | 2020-12-15 13:13:11 -0800 | [diff] [blame] | 378 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 379 | |
Anthony DiGirolamo | afb861c | 2021-08-12 16:48:04 -0700 | [diff] [blame] | 380 | # Get generated_setup scope and write it to disk as JSON. |
| 381 | |
| 382 | # Expected setup.cfg structure: |
| 383 | # https://setuptools.readthedocs.io/en/latest/userguide/declarative_config.html |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 384 | _gen_setup = invoker.generate_setup |
Anthony DiGirolamo | afb861c | 2021-08-12 16:48:04 -0700 | [diff] [blame] | 385 | assert(defined(_gen_setup.metadata), |
| 386 | "'metadata = {}' is required in generate_package") |
| 387 | |
| 388 | # Get metadata which should contain at least name. |
| 389 | _gen_metadata = { |
| 390 | } |
| 391 | _gen_metadata = _gen_setup.metadata |
| 392 | assert( |
| 393 | defined(_gen_metadata.name), |
| 394 | "metadata = { name = 'package_name' } is required in generate_package") |
| 395 | |
| 396 | # Get options which should not have packages or package_data. |
| 397 | if (defined(_gen_setup.options)) { |
| 398 | _gen_options = { |
| 399 | } |
| 400 | _gen_options = _gen_setup.options |
| 401 | assert(!defined(_gen_options.packages) && |
| 402 | !defined(_gen_options.package_data), |
| 403 | "'packages' and 'package_data' may not be provided " + |
| 404 | "in 'generate_package' options.") |
| 405 | } |
| 406 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 407 | write_file("$_setup_dir/setup.json", _gen_setup, "json") |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 408 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 409 | # Generate the setup.py, py.typed, and __init__.py files as needed. |
| 410 | action(target_name) { |
Anthony DiGirolamo | 211dce4 | 2021-08-12 16:48:43 -0700 | [diff] [blame] | 411 | metadata = { |
| 412 | pw_python_package_metadata_json = [ _package_metadata_json_file ] |
| 413 | } |
| 414 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 415 | script = "$dir_pw_build/py/pw_build/generate_python_package.py" |
| 416 | args = [ |
| 417 | "--label", |
| 418 | get_label_info(":$target_name", "label_no_toolchain"), |
Wyatt Hepler | cf184e8 | 2021-05-11 18:15:35 -0700 | [diff] [blame] | 419 | "--generated-root", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 420 | rebase_path(_setup_dir, root_build_dir), |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 421 | "--setup-json", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 422 | rebase_path("$_setup_dir/setup.json", root_build_dir), |
| 423 | ] + rebase_path(_sources, root_build_dir) |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 424 | |
Wyatt Hepler | cf184e8 | 2021-05-11 18:15:35 -0700 | [diff] [blame] | 425 | # Pass in the .json information files for the imported proto libraries. |
| 426 | foreach(proto, _import_protos) { |
| 427 | _label = get_label_info(proto, "label_no_toolchain") + |
| 428 | ".python($pw_protobuf_compiler_TOOLCHAIN)" |
| 429 | _file = get_label_info(_label, "target_gen_dir") + "/" + |
| 430 | get_label_info(_label, "name") + ".json" |
| 431 | args += [ |
| 432 | "--proto-library", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 433 | rebase_path(_file, root_build_dir), |
Wyatt Hepler | cf184e8 | 2021-05-11 18:15:35 -0700 | [diff] [blame] | 434 | ] |
| 435 | } |
| 436 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 437 | if (defined(invoker._pw_module_as_package) && |
| 438 | invoker._pw_module_as_package) { |
| 439 | args += [ "--module-as-package" ] |
| 440 | } |
| 441 | |
Wyatt Hepler | 0e1f5e4 | 2021-03-16 22:51:57 -0700 | [diff] [blame] | 442 | inputs = [ "$_setup_dir/setup.json" ] |
| 443 | |
Wyatt Hepler | cf184e8 | 2021-05-11 18:15:35 -0700 | [diff] [blame] | 444 | public_deps = [ ":$target_name._mirror_sources_to_out_dir" ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 445 | |
| 446 | outputs = _setup_sources |
| 447 | } |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 448 | } else { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 449 | # If the package is not generated, use an input group for the sources. |
| 450 | pw_input_group(target_name) { |
Anthony DiGirolamo | 211dce4 | 2021-08-12 16:48:43 -0700 | [diff] [blame] | 451 | metadata = { |
| 452 | pw_python_package_metadata_json = [ _package_metadata_json_file ] |
| 453 | } |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 454 | inputs = _all_py_files |
| 455 | if (defined(invoker.inputs)) { |
| 456 | inputs += invoker.inputs |
| 457 | } |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 458 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 459 | deps = _python_deps + _other_deps |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 460 | } |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 461 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 462 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 463 | if (_is_package) { |
| 464 | # Install this Python package and its dependencies in the current Python |
Wyatt Hepler | 446f24c | 2021-03-24 09:29:35 -0700 | [diff] [blame] | 465 | # environment using pip. |
| 466 | pw_python_action("$target_name._run_pip_install") { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 467 | module = "pip" |
| 468 | public_deps = [] |
Joe Ethier | e93ccb1 | 2021-08-20 15:59:53 -0700 | [diff] [blame] | 469 | if (defined(invoker.public_deps)) { |
| 470 | public_deps += invoker.public_deps |
| 471 | } |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 472 | |
Anthony DiGirolamo | cd40577 | 2021-10-18 16:29:26 -0700 | [diff] [blame] | 473 | args = [ |
| 474 | "install", |
| 475 | |
| 476 | # This speeds up pip installs. At this point in the gn build the |
| 477 | # virtualenv is already activated so build isolation isn't required. |
| 478 | # This requires that pip, setuptools, and wheel packages are |
| 479 | # installed. |
| 480 | "--no-build-isolation", |
| 481 | ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 482 | |
Michael Spang | 01faaf7 | 2021-06-09 22:38:40 -0400 | [diff] [blame] | 483 | inputs = pw_build_PIP_CONSTRAINTS |
| 484 | foreach(_constraints_file, pw_build_PIP_CONSTRAINTS) { |
| 485 | args += [ |
| 486 | "--constraint", |
| 487 | rebase_path(_constraints_file, root_build_dir), |
| 488 | ] |
| 489 | } |
| 490 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 491 | # For generated packages, reinstall when any files change. For regular |
| 492 | # packages, only reinstall when setup.py changes. |
| 493 | if (_generate_package) { |
| 494 | public_deps += [ ":${invoker.target_name}" ] |
| 495 | } else { |
Michael Spang | 01faaf7 | 2021-06-09 22:38:40 -0400 | [diff] [blame] | 496 | inputs += invoker.setup |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 497 | |
| 498 | # Install with --editable since the complete package is in source. |
| 499 | args += [ "--editable" ] |
| 500 | } |
| 501 | |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 502 | args += [ rebase_path(_setup_dir, root_build_dir) ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 503 | |
| 504 | stamp = true |
| 505 | |
| 506 | # Parallel pip installations don't work, so serialize pip invocations. |
Michael Spang | da057f7 | 2021-06-10 00:50:36 -0400 | [diff] [blame] | 507 | pool = "$dir_pw_build/pool:pip($default_toolchain)" |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 508 | |
| 509 | foreach(dep, _python_deps) { |
| 510 | # We need to add a suffix to the target name, but the label is |
| 511 | # formatted as "//path/to:target(toolchain)", so we can't just append |
| 512 | # ".subtarget". Instead, we replace the opening parenthesis of the |
| 513 | # toolchain with ".suffix(". |
Wyatt Hepler | 446f24c | 2021-03-24 09:29:35 -0700 | [diff] [blame] | 514 | public_deps += [ string_replace(dep, "(", "._run_pip_install(") ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 518 | # Builds a Python wheel for this package. Records the output directory |
| 519 | # in the pw_python_package_wheels metadata key. |
Wyatt Hepler | 446f24c | 2021-03-24 09:29:35 -0700 | [diff] [blame] | 520 | pw_python_action("$target_name._build_wheel") { |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 521 | metadata = { |
| 522 | pw_python_package_wheels = [ "$target_out_dir/$target_name" ] |
| 523 | } |
| 524 | |
| 525 | module = "build" |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 526 | |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 527 | args = |
| 528 | [ |
| 529 | rebase_path(_setup_dir, root_build_dir), |
| 530 | "--wheel", |
| 531 | "--no-isolation", |
| 532 | "--outdir", |
| 533 | ] + rebase_path(metadata.pw_python_package_wheels, root_build_dir) |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 534 | |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 535 | deps = [ ":${invoker.target_name}" ] |
| 536 | foreach(dep, _python_deps) { |
| 537 | deps += [ string_replace(dep, "(", ".wheel(") ] |
| 538 | } |
| 539 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 540 | stamp = true |
| 541 | } |
| 542 | } else { |
Wyatt Hepler | 446f24c | 2021-03-24 09:29:35 -0700 | [diff] [blame] | 543 | # Stubs for non-package targets. |
| 544 | group("$target_name._run_pip_install") { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 545 | } |
Wyatt Hepler | 446f24c | 2021-03-24 09:29:35 -0700 | [diff] [blame] | 546 | group("$target_name._build_wheel") { |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | # Create the .install and .wheel targets. To limit unnecessary pip |
| 551 | # executions, non-generated packages are only reinstalled when their |
| 552 | # setup.py changes. However, targets that depend on the .install subtarget |
| 553 | # re-run whenever any source files change. |
| 554 | # |
| 555 | # These targets just represent the source files if this isn't a package. |
| 556 | group("$target_name.install") { |
| 557 | public_deps = [ ":${invoker.target_name}" ] |
| 558 | |
| 559 | if (_is_package) { |
| 560 | public_deps += [ ":${invoker.target_name}._run_pip_install" ] |
| 561 | } |
| 562 | |
| 563 | foreach(dep, _python_deps) { |
| 564 | public_deps += [ string_replace(dep, "(", ".install(") ] |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | group("$target_name.wheel") { |
| 569 | public_deps = [ ":${invoker.target_name}.install" ] |
| 570 | |
| 571 | if (_is_package) { |
| 572 | public_deps += [ ":${invoker.target_name}._build_wheel" ] |
| 573 | } |
| 574 | |
| 575 | foreach(dep, _python_deps) { |
| 576 | public_deps += [ string_replace(dep, "(", ".wheel(") ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
| 580 | # Define the static analysis targets for this package. |
| 581 | group("$target_name.lint") { |
Wyatt Hepler | 446f24c | 2021-03-24 09:29:35 -0700 | [diff] [blame] | 582 | deps = [] |
| 583 | foreach(_tool, _supported_static_analysis_tools) { |
| 584 | deps += [ ":${invoker.target_name}.lint.$_tool" ] |
| 585 | } |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 586 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 587 | |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 588 | if (_static_analysis != [] || _test_sources != []) { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 589 | # All packages to install for either general use or test running. |
| 590 | _test_install_deps = [ ":$target_name.install" ] |
| 591 | foreach(dep, _python_test_deps + [ "$dir_pw_build:python_lint" ]) { |
| 592 | _test_install_deps += [ string_replace(dep, "(", ".install(") ] |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | # For packages that are not generated, create targets to run mypy and pylint. |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 597 | foreach(_tool, _static_analysis) { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 598 | # Run lint tools from the setup or target directory so that the tools detect |
| 599 | # config files (e.g. pylintrc or mypy.ini) in that directory. Config files |
| 600 | # may be explicitly specified with the pylintrc or mypy_ini arguments. |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 601 | target("_pw_python_static_analysis_$_tool", "$target_name.lint.$_tool") { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 602 | sources = _all_py_files |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 603 | deps = _test_install_deps |
| 604 | python_deps = _python_deps |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 605 | |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 606 | _optional_variables = [ |
| 607 | "mypy_ini", |
| 608 | "pylintrc", |
| 609 | ] |
| 610 | forward_variables_from(invoker, _optional_variables) |
| 611 | not_needed(_optional_variables) |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 612 | } |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | foreach(_unused_tool, _supported_static_analysis_tools - _static_analysis) { |
| 616 | pw_input_group("$target_name.lint.$_unused_tool") { |
| 617 | inputs = [] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 618 | if (defined(invoker.pylintrc)) { |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 619 | inputs += [ invoker.pylintrc ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 620 | } |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 621 | if (defined(invoker.mypy_ini)) { |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 622 | inputs += [ invoker.mypy_ini ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 623 | } |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 624 | } |
Wyatt Hepler | af853ea | 2021-03-09 19:09:54 -0800 | [diff] [blame] | 625 | |
| 626 | # Generated packages with linting disabled never need the whole file list. |
| 627 | not_needed([ "_all_py_files" ]) |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 628 | } |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 629 | } else { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 630 | # Create groups with the public target names ($target_name, $target_name.lint, |
| 631 | # $target_name.install, etc.). These are actually wrappers around internal |
| 632 | # Python actions instantiated with the default toolchain. This ensures there |
| 633 | # is only a single copy of each Python action in the build. |
| 634 | # |
| 635 | # The $target_name.tests group is created separately below. |
| 636 | group("$target_name") { |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 637 | deps = [ ":$target_name($pw_build_PYTHON_TOOLCHAIN)" ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | foreach(subtarget, pw_python_package_subtargets - [ "tests" ]) { |
| 641 | group("$target_name.$subtarget") { |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 642 | deps = |
| 643 | [ ":${invoker.target_name}.$subtarget($pw_build_PYTHON_TOOLCHAIN)" ] |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 644 | } |
Wyatt Hepler | b8e1360 | 2020-10-19 11:20:36 -0700 | [diff] [blame] | 645 | } |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 646 | |
| 647 | # Everything Python-related is only instantiated in the default toolchain. |
| 648 | # Silence not-needed warnings except for in the default toolchain. |
| 649 | not_needed("*") |
| 650 | not_needed(invoker, "*") |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | # Create a target for each test file. |
| 654 | _test_targets = [] |
| 655 | |
| 656 | foreach(test, _test_sources) { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 657 | if (_is_package) { |
| 658 | _name = rebase_path(test, _setup_dir) |
| 659 | } else { |
| 660 | _name = test |
| 661 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 662 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 663 | _test_target = "$target_name.tests." + string_replace(_name, "/", "_") |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 664 | |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 665 | if (current_toolchain == pw_build_PYTHON_TOOLCHAIN) { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 666 | pw_python_action(_test_target) { |
Anthony DiGirolamo | 6ab145b | 2022-01-20 20:52:50 -0800 | [diff] [blame] | 667 | if (pw_build_PYTHON_TEST_COVERAGE) { |
| 668 | module = "coverage" |
| 669 | working_directory = |
| 670 | rebase_path(get_path_info(test, "dir"), root_build_dir) |
| 671 | args = [ |
| 672 | "run", |
| 673 | "--branch", |
| 674 | |
| 675 | # Include all source files in the working_directory when calculating coverage. |
| 676 | "--source=.", |
| 677 | |
| 678 | # Test file to run. |
| 679 | get_path_info(test, "file"), |
| 680 | ] |
| 681 | |
| 682 | # Set the coverage file to a location in out/python/gen/ |
| 683 | _coverage_data_file = "$target_gen_dir/$target_name.coverage" |
| 684 | outputs = [ _coverage_data_file ] |
| 685 | |
| 686 | # The coverage tool only allows setting the output with an environment variable. |
| 687 | environment = |
| 688 | [ "COVERAGE_FILE=" + |
| 689 | rebase_path(_coverage_data_file, get_path_info(test, "dir")) ] |
| 690 | } else { |
| 691 | script = test |
| 692 | } |
| 693 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 694 | stamp = true |
Wyatt Hepler | 620b3e0 | 2021-01-22 09:14:45 -0800 | [diff] [blame] | 695 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 696 | deps = _test_install_deps |
| 697 | |
| 698 | foreach(dep, _python_test_deps) { |
| 699 | deps += [ string_replace(dep, "(", ".tests(") ] |
| 700 | } |
| 701 | } |
| 702 | } else { |
| 703 | # Create a public version of each test target, so tests can be executed as |
| 704 | # //path/to:package.tests.foo.py. |
| 705 | group(_test_target) { |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 706 | deps = [ ":$_test_target($pw_build_PYTHON_TOOLCHAIN)" ] |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 710 | _test_targets += [ ":$_test_target" ] |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | group("$target_name.tests") { |
| 714 | deps = _test_targets |
| 715 | } |
Wyatt Hepler | b85cda4 | 2021-04-07 13:13:15 -0700 | [diff] [blame] | 716 | |
| 717 | _pw_create_aliases_if_name_matches_directory(target_name) { |
| 718 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | # Declares a group of Python packages or other Python groups. pw_python_groups |
| 722 | # expose the same set of subtargets as pw_python_package (e.g. |
| 723 | # "$group_name.lint" and "$group_name.tests"), but these apply to all packages |
| 724 | # in deps and their dependencies. |
| 725 | template("pw_python_group") { |
| 726 | if (defined(invoker.python_deps)) { |
| 727 | _python_deps = invoker.python_deps |
| 728 | } else { |
| 729 | _python_deps = [] |
Wyatt Hepler | a3ca62a | 2021-05-04 16:21:43 -0700 | [diff] [blame] | 730 | not_needed([ "invoker" ]) # Allow empty groups. |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | group(target_name) { |
| 734 | deps = _python_deps |
Wyatt Hepler | cf184e8 | 2021-05-11 18:15:35 -0700 | [diff] [blame] | 735 | |
| 736 | if (defined(invoker.other_deps)) { |
| 737 | deps += invoker.other_deps |
| 738 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 741 | foreach(subtarget, pw_python_package_subtargets) { |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 742 | group("$target_name.$subtarget") { |
Wyatt Hepler | b85cda4 | 2021-04-07 13:13:15 -0700 | [diff] [blame] | 743 | public_deps = [] |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 744 | foreach(dep, _python_deps) { |
| 745 | # Split out the toolchain to support deps with a toolchain specified. |
| 746 | _target = get_label_info(dep, "label_no_toolchain") |
| 747 | _toolchain = get_label_info(dep, "toolchain") |
Wyatt Hepler | b85cda4 | 2021-04-07 13:13:15 -0700 | [diff] [blame] | 748 | public_deps += [ "$_target.$subtarget($_toolchain)" ] |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | } |
Wyatt Hepler | b85cda4 | 2021-04-07 13:13:15 -0700 | [diff] [blame] | 752 | |
| 753 | _pw_create_aliases_if_name_matches_directory(target_name) { |
| 754 | } |
Wyatt Hepler | b16dfd3 | 2020-10-12 08:46:38 -0700 | [diff] [blame] | 755 | } |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 756 | |
| 757 | # Declares Python scripts or tests that are not part of a Python package. |
| 758 | # Similar to pw_python_package, but only supports a subset of its features. |
| 759 | # |
| 760 | # pw_python_script accepts the same arguments as pw_python_package, except |
| 761 | # `setup` cannot be provided. |
| 762 | # |
| 763 | # pw_python_script provides the same subtargets as pw_python_package, but |
| 764 | # $target_name.install and $target_name.wheel only affect the python_deps of |
| 765 | # this GN target, not the target itself. |
Wyatt Hepler | 473efe0 | 2021-05-04 14:15:16 -0700 | [diff] [blame] | 766 | # |
| 767 | # pw_python_script allows creating a pw_python_action associated with the |
| 768 | # script. This is provided by passing an 'action' scope to pw_python_script. |
| 769 | # This functions like a normal action, with a few additions: the action uses the |
| 770 | # pw_python_script's python_deps and defaults to using the source file as its |
| 771 | # 'script' argument, if there is only a single source file. |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 772 | template("pw_python_script") { |
Wyatt Hepler | 473efe0 | 2021-05-04 14:15:16 -0700 | [diff] [blame] | 773 | _package_variables = [ |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 774 | "sources", |
| 775 | "tests", |
| 776 | "python_deps", |
| 777 | "other_deps", |
| 778 | "inputs", |
Wyatt Hepler | 8ce9013 | 2020-12-03 10:57:20 -0800 | [diff] [blame] | 779 | "pylintrc", |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 780 | "mypy_ini", |
| 781 | "static_analysis", |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 782 | ] |
| 783 | |
| 784 | pw_python_package(target_name) { |
| 785 | _pw_standalone = true |
Wyatt Hepler | 473efe0 | 2021-05-04 14:15:16 -0700 | [diff] [blame] | 786 | forward_variables_from(invoker, _package_variables) |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 787 | } |
Wyatt Hepler | b85cda4 | 2021-04-07 13:13:15 -0700 | [diff] [blame] | 788 | |
| 789 | _pw_create_aliases_if_name_matches_directory(target_name) { |
| 790 | } |
Wyatt Hepler | 473efe0 | 2021-05-04 14:15:16 -0700 | [diff] [blame] | 791 | |
| 792 | if (defined(invoker.action)) { |
| 793 | pw_python_action("$target_name.action") { |
| 794 | forward_variables_from(invoker.action, "*", [ "python_deps" ]) |
| 795 | python_deps = [ ":${invoker.target_name}" ] |
| 796 | |
| 797 | if (!defined(script) && !defined(module) && defined(invoker.sources)) { |
| 798 | _sources = invoker.sources |
| 799 | assert(_sources != [] && _sources == [ _sources[0] ], |
| 800 | "'script' must be specified unless there is only one source " + |
| 801 | "in 'sources'") |
| 802 | script = _sources[0] |
| 803 | } |
| 804 | } |
| 805 | } |
Wyatt Hepler | 45af57b | 2020-10-23 08:05:28 -0700 | [diff] [blame] | 806 | } |
Wyatt Hepler | 4d1e6aa | 2021-01-14 08:39:42 -0800 | [diff] [blame] | 807 | |
| 808 | # Represents a list of Python requirements, as in a requirements.txt. |
| 809 | # |
| 810 | # Args: |
| 811 | # files: One or more requirements.txt files. |
| 812 | # requirements: A list of requirements.txt-style requirements. |
| 813 | template("pw_python_requirements") { |
| 814 | assert(defined(invoker.files) || defined(invoker.requirements), |
| 815 | "pw_python_requirements requires a list of requirements.txt files " + |
| 816 | "in the 'files' arg or requirements in 'requirements'") |
| 817 | |
| 818 | _requirements_files = [] |
| 819 | |
| 820 | if (defined(invoker.files)) { |
| 821 | _requirements_files += invoker.files |
| 822 | } |
| 823 | |
| 824 | if (defined(invoker.requirements)) { |
| 825 | _requirements_file = "$target_gen_dir/$target_name.requirements.txt" |
| 826 | write_file(_requirements_file, invoker.requirements) |
| 827 | _requirements_files += [ _requirements_file ] |
| 828 | } |
| 829 | |
| 830 | # The default target represents the requirements themselves. |
| 831 | pw_input_group(target_name) { |
| 832 | inputs = _requirements_files |
| 833 | } |
| 834 | |
| 835 | # Use the same subtargets as pw_python_package so these targets can be listed |
| 836 | # as python_deps of pw_python_packages. |
| 837 | pw_python_action("$target_name.install") { |
| 838 | inputs = _requirements_files |
| 839 | |
| 840 | module = "pip" |
| 841 | args = [ "install" ] |
| 842 | |
| 843 | foreach(_requirements_file, inputs) { |
| 844 | args += [ |
| 845 | "--requirement", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 846 | rebase_path(_requirements_file, root_build_dir), |
Wyatt Hepler | 4d1e6aa | 2021-01-14 08:39:42 -0800 | [diff] [blame] | 847 | ] |
| 848 | } |
| 849 | |
Michael Spang | 01faaf7 | 2021-06-09 22:38:40 -0400 | [diff] [blame] | 850 | inputs += pw_build_PIP_CONSTRAINTS |
| 851 | foreach(_constraints_file, pw_build_PIP_CONSTRAINTS) { |
| 852 | args += [ |
| 853 | "--constraint", |
| 854 | rebase_path(_constraints_file, root_build_dir), |
| 855 | ] |
| 856 | } |
| 857 | |
Michael Spang | da057f7 | 2021-06-10 00:50:36 -0400 | [diff] [blame] | 858 | pool = "$dir_pw_build/pool:pip($default_toolchain)" |
Wyatt Hepler | 4d1e6aa | 2021-01-14 08:39:42 -0800 | [diff] [blame] | 859 | stamp = true |
| 860 | } |
| 861 | |
| 862 | # Create stubs for the unused subtargets so that pw_python_requirements can be |
| 863 | # used as python_deps. |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 864 | foreach(subtarget, pw_python_package_subtargets - [ "install" ]) { |
Wyatt Hepler | 4d1e6aa | 2021-01-14 08:39:42 -0800 | [diff] [blame] | 865 | group("$target_name.$subtarget") { |
| 866 | } |
| 867 | } |
Wyatt Hepler | b85cda4 | 2021-04-07 13:13:15 -0700 | [diff] [blame] | 868 | |
| 869 | _pw_create_aliases_if_name_matches_directory(target_name) { |
| 870 | } |
Wyatt Hepler | 4d1e6aa | 2021-01-14 08:39:42 -0800 | [diff] [blame] | 871 | } |