|
| 1 | +# Copyright 2025 The JAX Authors. |
| 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 | +# 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, |
| 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 | + |
| 15 | +# Script that builds a JAX wheel, intended to be run via bazel run as part |
| 16 | +# of the JAX build process. |
| 17 | + |
| 18 | +import argparse |
| 19 | +import os |
| 20 | +import pathlib |
| 21 | +import shutil |
| 22 | +import tempfile |
| 23 | + |
| 24 | +from jaxlib.tools import build_utils |
| 25 | + |
| 26 | +parser = argparse.ArgumentParser(fromfile_prefix_chars="@") |
| 27 | +parser.add_argument( |
| 28 | + "--sources_path", |
| 29 | + default=None, |
| 30 | + help=( |
| 31 | + "Path in which the wheel's sources should be prepared. Optional. If " |
| 32 | + "omitted, a temporary directory will be used." |
| 33 | + ), |
| 34 | +) |
| 35 | +parser.add_argument( |
| 36 | + "--output_path", |
| 37 | + default=None, |
| 38 | + required=True, |
| 39 | + help="Path to which the output wheel should be written. Required.", |
| 40 | +) |
| 41 | +parser.add_argument( |
| 42 | + "--jaxlib_git_hash", |
| 43 | + default="", |
| 44 | + required=True, |
| 45 | + help="Git hash. Empty if unknown. Optional.", |
| 46 | +) |
| 47 | +parser.add_argument( |
| 48 | + "--srcs", help="source files for the wheel", action="append" |
| 49 | +) |
| 50 | +args = parser.parse_args() |
| 51 | + |
| 52 | + |
| 53 | +def copy_file( |
| 54 | + src_file: str, |
| 55 | + dst_dir: str, |
| 56 | +) -> None: |
| 57 | + """Copy a file to the destination directory. |
| 58 | +
|
| 59 | + Args: |
| 60 | + src_file: file to be copied |
| 61 | + dst_dir: destination directory |
| 62 | + """ |
| 63 | + |
| 64 | + dest_dir_path = os.path.join(dst_dir, os.path.dirname(src_file)) |
| 65 | + os.makedirs(dest_dir_path, exist_ok=True) |
| 66 | + shutil.copy(src_file, dest_dir_path) |
| 67 | + os.chmod(os.path.join(dst_dir, src_file), 0o644) |
| 68 | + |
| 69 | + |
| 70 | +def prepare_srcs(deps: list[str], srcs_dir: str) -> None: |
| 71 | + """Filter the sources and copy them to the destination directory. |
| 72 | +
|
| 73 | + Args: |
| 74 | + deps: a list of paths to files. |
| 75 | + srcs_dir: target directory where files are copied to. |
| 76 | + """ |
| 77 | + |
| 78 | + for file in deps: |
| 79 | + if not (file.startswith("bazel-out") or file.startswith("external")): |
| 80 | + copy_file(file, srcs_dir) |
| 81 | + |
| 82 | + |
| 83 | +tmpdir = None |
| 84 | +sources_path = args.sources_path |
| 85 | +if sources_path is None: |
| 86 | + tmpdir = tempfile.TemporaryDirectory(prefix="jax") |
| 87 | + sources_path = tmpdir.name |
| 88 | + |
| 89 | +try: |
| 90 | + os.makedirs(args.output_path, exist_ok=True) |
| 91 | + prepare_srcs(args.srcs, pathlib.Path(sources_path)) |
| 92 | + build_utils.build_wheel( |
| 93 | + sources_path, |
| 94 | + args.output_path, |
| 95 | + package_name="jax", |
| 96 | + git_hash=args.jaxlib_git_hash, |
| 97 | + ) |
| 98 | +finally: |
| 99 | + if tmpdir: |
| 100 | + tmpdir.cleanup() |
0 commit comments