• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://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,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15load("@bazel_skylib//rules:build_test.bzl", "build_test")
16load("//examples/wheel/private:wheel_utils.bzl", "directory_writer", "make_variable_tags")
17load("//python:defs.bzl", "py_library", "py_test")
18load("//python:packaging.bzl", "py_package", "py_wheel")
19load("//python:versions.bzl", "gen_python_config_settings")
20
21package(default_visibility = ["//visibility:public"])
22
23licenses(["notice"])  # Apache 2.0
24
25py_library(
26    name = "main",
27    srcs = ["main.py"],
28    deps = [
29        "//examples/wheel/lib:simple_module",
30        "//examples/wheel/lib:module_with_data",
31        # Example dependency which is not packaged in the wheel
32        # due to "packages" filter on py_package rule.
33        "//tests/load_from_macro:foo",
34    ],
35)
36
37py_library(
38    name = "main_with_gen_data",
39    srcs = ["main.py"],
40    data = [
41        ":gen_dir",
42    ],
43)
44
45directory_writer(
46    name = "gen_dir",
47    out = "someDir",
48    files = {"foo.py": ""},
49)
50
51# Package just a specific py_libraries, without their dependencies
52py_wheel(
53    name = "minimal_with_py_library",
54    testonly = True,  # Set this to verify the generated .dist target doesn't break things
55    # Package data. We're building "example_minimal_library-0.0.1-py3-none-any.whl"
56    distribution = "example_minimal_library",
57    python_tag = "py3",
58    version = "0.0.1",
59    deps = [
60        "//examples/wheel/lib:module_with_data",
61        "//examples/wheel/lib:simple_module",
62    ],
63)
64
65# Populate a rule with "Make Variable" arguments for
66# abi, python_tag and version. You might want to do this
67# for the following use cases:
68#  - abi, python_tag: introspect a toolchain to map to appropriate cpython tags
69#  - version: populate given this or a dependent module's version
70make_variable_tags(
71    name = "make_variable_tags",
72)
73
74py_wheel(
75    name = "minimal_with_py_library_with_make_variables",
76    testonly = True,
77    abi = "$(ABI)",
78    distribution = "example_minimal_library",
79    python_tag = "$(PYTHON_TAG)",
80    toolchains = ["//examples/wheel:make_variable_tags"],
81    version = "$(VERSION)",
82    deps = [
83        "//examples/wheel/lib:module_with_data",
84        "//examples/wheel/lib:simple_module",
85    ],
86)
87
88build_test(
89    name = "dist_build_tests",
90    targets = [":minimal_with_py_library.dist"],
91)
92
93# Package just a specific py_libraries, without their dependencies
94py_wheel(
95    name = "minimal_with_py_library_with_stamp",
96    # Package data. We're building "example_minimal_library-0.0.1-py3-none-any.whl"
97    distribution = "example_minimal_library{BUILD_USER}",
98    python_tag = "py3",
99    stamp = 1,
100    version = "0.1.{BUILD_TIMESTAMP}",
101    deps = [
102        "//examples/wheel/lib:module_with_data",
103        "//examples/wheel/lib:simple_module",
104    ],
105)
106
107# Use py_package to collect all transitive dependencies of a target,
108# selecting just the files within a specific python package.
109py_package(
110    name = "example_pkg",
111    # Only include these Python packages.
112    packages = ["examples.wheel"],
113    deps = [":main"],
114)
115
116py_package(
117    name = "example_pkg_with_data",
118    packages = ["examples.wheel"],
119    deps = [":main_with_gen_data"],
120)
121
122py_wheel(
123    name = "minimal_with_py_package",
124    # Package data. We're building "example_minimal_package-0.0.1-py3-none-any.whl"
125    distribution = "example_minimal_package",
126    python_tag = "py3",
127    version = "0.0.1",
128    deps = [":example_pkg"],
129)
130
131# An example that uses all features provided by py_wheel.
132py_wheel(
133    name = "customized",
134    author = "Example Author with non-ascii characters: żółw",
135    author_email = "example@example.com",
136    classifiers = [
137        "License :: OSI Approved :: Apache Software License",
138        "Intended Audience :: Developers",
139    ],
140    console_scripts = {
141        "customized_wheel": "examples.wheel.main:main",
142    },
143    description_file = "README.md",
144    # Package data. We're building "example_customized-0.0.1-py3-none-any.whl"
145    distribution = "example_customized",
146    entry_points = {
147        "console_scripts": ["another = foo.bar:baz"],
148        "group2": [
149            "second = second.main:s",
150            "first = first.main:f",
151        ],
152    },
153    extra_distinfo_files = {
154        "//examples/wheel:NOTICE": "NOTICE",
155        # Rename the file when packaging to show we can.
156        "//examples/wheel:README.md": "README",
157    },
158    homepage = "www.example.com",
159    license = "Apache 2.0",
160    project_urls = {
161        "Bug Tracker": "www.example.com/issues",
162        "Documentation": "www.example.com/docs",
163    },
164    python_tag = "py3",
165    # Requirements embedded into the wheel metadata.
166    requires = ["pytest"],
167    summary = "A one-line summary of this test package",
168    version = "0.0.1",
169    deps = [":example_pkg"],
170)
171
172# An example of how to change the wheel package root directory using 'strip_path_prefixes'.
173py_wheel(
174    name = "custom_package_root",
175    # Package data. We're building "examples_custom_package_root-0.0.1-py3-none-any.whl"
176    distribution = "examples_custom_package_root",
177    entry_points = {
178        "console_scripts": ["main = foo.bar:baz"],
179    },
180    python_tag = "py3",
181    strip_path_prefixes = [
182        "examples",
183    ],
184    version = "0.0.1",
185    deps = [
186        ":example_pkg",
187    ],
188)
189
190py_wheel(
191    name = "custom_package_root_multi_prefix",
192    # Package data. We're building "custom_custom_package_root_multi_prefix-0.0.1-py3-none-any.whl"
193    distribution = "example_custom_package_root_multi_prefix",
194    python_tag = "py3",
195    strip_path_prefixes = [
196        "examples/wheel/lib",
197        "examples/wheel",
198    ],
199    version = "0.0.1",
200    deps = [
201        ":example_pkg",
202    ],
203)
204
205py_wheel(
206    name = "custom_package_root_multi_prefix_reverse_order",
207    # Package data. We're building "custom_custom_package_root_multi_prefix_reverse_order-0.0.1-py3-none-any.whl"
208    distribution = "example_custom_package_root_multi_prefix_reverse_order",
209    python_tag = "py3",
210    strip_path_prefixes = [
211        "examples/wheel",
212        "examples/wheel/lib",  # this is not effective, because the first prefix takes priority
213    ],
214    version = "0.0.1",
215    deps = [
216        ":example_pkg",
217    ],
218)
219
220py_wheel(
221    name = "python_requires_in_a_package",
222    distribution = "example_python_requires_in_a_package",
223    python_requires = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*",
224    python_tag = "py3",
225    version = "0.0.1",
226    deps = [
227        ":example_pkg",
228    ],
229)
230
231py_wheel(
232    name = "use_rule_with_dir_in_outs",
233    distribution = "use_rule_with_dir_in_outs",
234    python_tag = "py3",
235    version = "0.0.1",
236    deps = [
237        ":example_pkg_with_data",
238    ],
239)
240
241gen_python_config_settings()
242
243py_wheel(
244    name = "python_abi3_binary_wheel",
245    abi = "abi3",
246    distribution = "example_python_abi3_binary_wheel",
247    # these platform strings must line up with test_python_abi3_binary_wheel() in wheel_test.py
248    platform = select({
249        ":aarch64-apple-darwin": "macosx_11_0_arm64",
250        ":aarch64-unknown-linux-gnu": "manylinux2014_aarch64",
251        ":x86_64-apple-darwin": "macosx_11_0_x86_64",  # this is typically macosx_10_9_x86_64?
252        ":x86_64-pc-windows-msvc": "win_amd64",
253        ":x86_64-unknown-linux-gnu": "manylinux2014_x86_64",
254    }),
255    python_requires = ">=3.8",
256    python_tag = "cp38",
257    version = "0.0.1",
258)
259
260py_wheel(
261    name = "filename_escaping",
262    # Per https://www.python.org/dev/peps/pep-0427/#escaping-and-unicode
263    # runs of non-alphanumeric, non-digit symbols should be replaced with a single underscore.
264    # Unicode non-ascii letters should *not* be replaced with underscore.
265    distribution = "file~~name-escaping",
266    python_tag = "py3",
267    version = "0.0.1-r7",
268    deps = [":example_pkg"],
269)
270
271py_test(
272    name = "wheel_test",
273    srcs = ["wheel_test.py"],
274    data = [
275        ":custom_package_root",
276        ":custom_package_root_multi_prefix",
277        ":custom_package_root_multi_prefix_reverse_order",
278        ":customized",
279        ":filename_escaping",
280        ":minimal_with_py_library",
281        ":minimal_with_py_library_with_stamp",
282        ":minimal_with_py_package",
283        ":python_abi3_binary_wheel",
284        ":python_requires_in_a_package",
285        ":use_rule_with_dir_in_outs",
286    ],
287)
288