| # Copyright 2020 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") |
| |
| import("$dir_pw_build/input_group.gni") |
| import("$dir_pw_build/python_action.gni") |
| |
| # Defines a Python package. GN Python packages contain several GN targets: |
| # |
| # - $name - Provides the Python files in the build, but does not take any |
| # actions. All subtargets depend on this target. |
| # - $name.lint - Runs static analyis tools on the Python code. This is a group |
| # of two subtargets: |
| # - $name.lint.mypy - Runs mypy. |
| # - $name.lint.pylint - Runs pylint. |
| # - $name.tests - Runs all tests for this package. |
| # - $name.install - Installs the package in a venv. (Not implemented.) |
| # - $name.wheel - Builds a Python wheel for the package. (Not implemented.) |
| # |
| # TODO(pwbug/239): Implement installation and wheel building. |
| # |
| # Args: |
| # setup: List of setup file paths (setup.py or pyproject.toml & setup.cfg), |
| # which must all be in the same directory. |
| # sources: Python sources files in the package. |
| # tests: Test files for this Python package. |
| # python_deps: Dependencies on other pw_python_packages in the GN build. |
| # other_deps: Dependencies on GN targets that are not pw_python_packages. |
| # inputs: Other files to track, such as package_data. |
| # |
| template("pw_python_package") { |
| if (defined(invoker.sources)) { |
| _all_py_files = invoker.sources |
| } else { |
| _all_py_files = [] |
| } |
| |
| if (defined(invoker.tests)) { |
| _test_sources = invoker.tests |
| } else { |
| _test_sources = [] |
| } |
| |
| _all_py_files += _test_sources |
| |
| assert(_all_py_files != [], "At least one source or test must be provided") |
| |
| # pw_python_script uses pw_python_package, but with a limited set of features. |
| # _pw_standalone signals that this target is actually a pw_python_script. |
| _is_package = !(defined(invoker._pw_standalone) && invoker._pw_standalone) |
| |
| if (_is_package) { |
| assert(defined(invoker.setup) && invoker.setup != [], |
| "pw_python_package requires 'setup' to point to a setup.py file " + |
| "or pyproject.toml and setup.cfg files") |
| |
| _all_py_files += invoker.setup |
| |
| # Get the directories of the setup files. All files must be in the same dir. |
| _setup_dirs = get_path_info(invoker.setup, "dir") |
| _setup_dir = _setup_dirs[0] |
| |
| foreach(dir, _setup_dirs) { |
| assert(dir == _setup_dir, |
| "All files in 'setup' must be in the same directory") |
| } |
| |
| # If sources are provided, make sure there is an __init__.py file. |
| if (defined(invoker.sources)) { |
| assert(filter_include(invoker.sources, [ "*\b__init__.py" ]) != [], |
| "Python packages must have at least one __init__.py file") |
| } |
| } |
| |
| _python_deps = [] |
| if (defined(invoker.python_deps)) { |
| foreach(dep, invoker.python_deps) { |
| # Use the fully qualified name so the subtarget can be appended as needed. |
| _python_deps += [ get_label_info(dep, "label_no_toolchain") ] |
| } |
| } |
| |
| # Declare the main Python package group. This represents the Python files, but |
| # does not take any actions. GN targets can depend on the package name to run |
| # when any files in the package change. |
| pw_input_group(target_name) { |
| inputs = _all_py_files |
| if (defined(invoker.inputs)) { |
| inputs += invoker.inputs |
| } |
| |
| deps = _python_deps |
| |
| if (defined(invoker.other_deps)) { |
| deps += invoker.other_deps |
| } |
| } |
| |
| _package_target = ":$target_name" |
| |
| if (_is_package) { |
| # Install this Python package and its dependencies in the current Python |
| # environment. |
| pw_python_action("$target_name.install") { |
| module = "pip" |
| args = [ |
| "install", |
| "--editable", |
| rebase_path(_setup_dir), |
| ] |
| |
| stamp = true |
| |
| # Parallel pip installations don't work, so serialize pip invocations. |
| pool = "$dir_pw_build:pip_pool" |
| |
| deps = [ _package_target ] |
| foreach(dep, _python_deps) { |
| deps += [ "$dep.install" ] |
| } |
| } |
| |
| # TODO(pwbug/239): Add support for building groups of wheels. The code below |
| # is incomplete and untested. |
| pw_python_action("$target_name.wheel") { |
| script = "$dir_pw_build/py/pw_build/python_wheels.py" |
| |
| args = [ |
| "--out_dir", |
| rebase_path(target_out_dir), |
| ] |
| args += rebase_path(_all_py_files) |
| |
| deps = [ _package_target ] |
| stamp = true |
| } |
| } else { |
| # If this is not a package, install or build wheels for its deps only. |
| group("$target_name.install") { |
| deps = [] |
| foreach(dep, _python_deps) { |
| deps += [ "$dep.install" ] |
| } |
| } |
| group("$target_name.wheel") { |
| deps = [] |
| foreach(dep, _python_deps) { |
| deps += [ "$dep.wheel" ] |
| } |
| } |
| } |
| |
| # Define the static analysis targets for this package. |
| group("$target_name.lint") { |
| deps = [ |
| "$_package_target.lint.mypy", |
| "$_package_target.lint.pylint", |
| ] |
| } |
| |
| pw_python_action("$target_name.lint.mypy") { |
| module = "mypy" |
| args = [ |
| "--pretty", |
| "--show-error-codes", |
| ] |
| if (_is_package) { |
| args += [ rebase_path(_setup_dir) ] |
| } else { |
| args += rebase_path(_all_py_files) |
| } |
| |
| # Use this environment variable to force mypy to colorize output. |
| # See https://github.com/python/mypy/issues/7771 |
| environment = [ "MYPY_FORCE_COLOR=1" ] |
| |
| stamp = true |
| |
| deps = [ _package_target ] |
| foreach(dep, _python_deps) { |
| deps += [ "$dep.lint.mypy" ] |
| } |
| } |
| |
| pw_python_action_foreach("$target_name.lint.pylint") { |
| module = "pylint" |
| args = [ |
| "{{source_root_relative_dir}}/{{source_file_part}}", |
| "--jobs=1", |
| "--output-format=colorized", |
| ] |
| |
| if (host_os == "win") { |
| # Allow CRLF on Windows, in case Git is set to switch line endings. |
| args += [ "--disable=unexpected-line-ending-format" ] |
| } |
| |
| sources = _all_py_files |
| |
| stamp = "$target_gen_dir/{{source_target_relative}}.pylint.pw_pystamp" |
| |
| # Run pylint from the source root so that pylint detects rcfiles (.pylintrc) |
| # in the source tree. |
| directory = rebase_path("//") |
| |
| deps = [ _package_target ] |
| foreach(dep, _python_deps) { |
| deps += [ "$dep.lint.pylint" ] |
| } |
| } |
| |
| # Create a target for each test file. |
| _test_targets = [] |
| |
| foreach(test, _test_sources) { |
| _test_name = string_replace(test, "/", "_") |
| _test_target = "$target_name.tests.$_test_name" |
| |
| pw_python_action(_test_target) { |
| script = test |
| stamp = true |
| |
| deps = [ _package_target ] |
| foreach(dep, _python_deps) { |
| deps += [ "$dep.tests" ] |
| } |
| } |
| |
| _test_targets += [ ":$_test_target" ] |
| } |
| |
| group("$target_name.tests") { |
| deps = _test_targets |
| } |
| } |
| |
| # Declares a group of Python packages or other Python groups. pw_python_groups |
| # expose the same set of subtargets as pw_python_package (e.g. |
| # "$group_name.lint" and "$group_name.tests"), but these apply to all packages |
| # in deps and their dependencies. |
| template("pw_python_group") { |
| if (defined(invoker.python_deps)) { |
| _python_deps = invoker.python_deps |
| } else { |
| _python_deps = [] |
| } |
| |
| group(target_name) { |
| deps = _python_deps |
| } |
| |
| _subtargets = [ |
| "tests", |
| "lint", |
| "lint.mypy", |
| "lint.pylint", |
| "install", |
| "wheel", |
| ] |
| |
| foreach(subtarget, _subtargets) { |
| group("$target_name.$subtarget") { |
| deps = [] |
| foreach(dep, _python_deps) { |
| # Split out the toolchain to support deps with a toolchain specified. |
| _target = get_label_info(dep, "label_no_toolchain") |
| _toolchain = get_label_info(dep, "toolchain") |
| deps += [ "$_target.$subtarget($_toolchain)" ] |
| } |
| } |
| } |
| } |
| |
| # Declares Python scripts or tests that are not part of a Python package. |
| # Similar to pw_python_package, but only supports a subset of its features. |
| # |
| # pw_python_script accepts the same arguments as pw_python_package, except |
| # `setup` cannot be provided. |
| # |
| # pw_python_script provides the same subtargets as pw_python_package, but |
| # $target_name.install and $target_name.wheel only affect the python_deps of |
| # this GN target, not the target itself. |
| template("pw_python_script") { |
| _supported_variables = [ |
| "sources", |
| "tests", |
| "python_deps", |
| "other_deps", |
| "inputs", |
| ] |
| |
| pw_python_package(target_name) { |
| _pw_standalone = true |
| forward_variables_from(invoker, _supported_variables) |
| } |
| } |