|
| 1 | +""" |
| 2 | +Copyright (c) 2023 by FlashInfer team. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +import os |
| 18 | +import shutil |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +from setuptools import build_meta as orig |
| 22 | +from build_utils import get_git_version |
| 23 | + |
| 24 | +_root = Path(__file__).parent.resolve() |
| 25 | +_data_dir = _root / "flashinfer" / "data" |
| 26 | + |
| 27 | + |
| 28 | +def _create_build_metadata(): |
| 29 | + """Create build metadata file with version information.""" |
| 30 | + version_file = _root / "version.txt" |
| 31 | + if version_file.exists(): |
| 32 | + with open(version_file, "r") as f: |
| 33 | + version = f.read().strip() |
| 34 | + else: |
| 35 | + version = "0.0.0+unknown" |
| 36 | + |
| 37 | + # Add dev suffix if specified |
| 38 | + dev_suffix = os.environ.get("FLASHINFER_DEV_RELEASE_SUFFIX", "") |
| 39 | + if dev_suffix: |
| 40 | + version = f"{version}.dev{dev_suffix}" |
| 41 | + |
| 42 | + # Get git version |
| 43 | + git_version = get_git_version(cwd=_root) |
| 44 | + |
| 45 | + # Create build metadata in the source tree |
| 46 | + package_dir = Path(__file__).parent / "flashinfer" |
| 47 | + build_meta_file = package_dir / "_build_meta.py" |
| 48 | + |
| 49 | + # Check if we're in a git repository |
| 50 | + git_dir = Path(__file__).parent / ".git" |
| 51 | + in_git_repo = git_dir.exists() |
| 52 | + |
| 53 | + # If file exists and not in git repo (installing from sdist), keep existing file |
| 54 | + if build_meta_file.exists() and not in_git_repo: |
| 55 | + print("Build metadata file already exists (not in git repo), keeping it") |
| 56 | + return version |
| 57 | + |
| 58 | + # In git repo (editable) or file doesn't exist, create/update it |
| 59 | + with open(build_meta_file, "w") as f: |
| 60 | + f.write('"""Build metadata for flashinfer package."""\n') |
| 61 | + f.write(f'__version__ = "{version}"\n') |
| 62 | + f.write(f'__git_version__ = "{git_version}"\n') |
| 63 | + |
| 64 | + print(f"Created build metadata file with version {version}") |
| 65 | + return version |
| 66 | + |
| 67 | + |
| 68 | +# Create build metadata as soon as this module is imported |
| 69 | +_create_build_metadata() |
| 70 | + |
| 71 | + |
| 72 | +def write_if_different(path: Path, content: str) -> None: |
| 73 | + if path.exists() and path.read_text() == content: |
| 74 | + return |
| 75 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 76 | + path.write_text(content) |
| 77 | + |
| 78 | + |
| 79 | +def _create_data_dir(use_symlinks=True): |
| 80 | + _data_dir.mkdir(parents=True, exist_ok=True) |
| 81 | + |
| 82 | + def ln(source: str, target: str) -> None: |
| 83 | + src = _root / source |
| 84 | + dst = _data_dir / target |
| 85 | + if dst.exists(): |
| 86 | + if dst.is_symlink(): |
| 87 | + dst.unlink() |
| 88 | + elif dst.is_dir(): |
| 89 | + shutil.rmtree(dst) |
| 90 | + else: |
| 91 | + dst.unlink() |
| 92 | + |
| 93 | + if use_symlinks: |
| 94 | + dst.symlink_to(src, target_is_directory=True) |
| 95 | + else: |
| 96 | + # For wheel/sdist, copy actual files instead of symlinks |
| 97 | + if src.exists(): |
| 98 | + shutil.copytree(src, dst, symlinks=False, dirs_exist_ok=True) |
| 99 | + |
| 100 | + ln("3rdparty/cutlass", "cutlass") |
| 101 | + ln("3rdparty/spdlog", "spdlog") |
| 102 | + ln("csrc", "csrc") |
| 103 | + ln("include", "include") |
| 104 | + |
| 105 | + |
| 106 | +def _prepare_for_wheel(): |
| 107 | + # For wheel, copy actual files instead of symlinks so they are included in the wheel |
| 108 | + if _data_dir.exists(): |
| 109 | + shutil.rmtree(_data_dir) |
| 110 | + _create_data_dir(use_symlinks=False) |
| 111 | + |
| 112 | + |
| 113 | +def _prepare_for_editable(): |
| 114 | + # For editable install, use symlinks so changes are reflected immediately |
| 115 | + if _data_dir.exists(): |
| 116 | + shutil.rmtree(_data_dir) |
| 117 | + _create_data_dir(use_symlinks=True) |
| 118 | + |
| 119 | + |
| 120 | +def _prepare_for_sdist(): |
| 121 | + # For sdist, copy actual files instead of symlinks so they are included in the tarball |
| 122 | + if _data_dir.exists(): |
| 123 | + shutil.rmtree(_data_dir) |
| 124 | + _create_data_dir(use_symlinks=False) |
| 125 | + |
| 126 | + |
| 127 | +def get_requires_for_build_wheel(config_settings=None): |
| 128 | + _prepare_for_wheel() |
| 129 | + return [] |
| 130 | + |
| 131 | + |
| 132 | +def get_requires_for_build_sdist(config_settings=None): |
| 133 | + _prepare_for_sdist() |
| 134 | + return [] |
| 135 | + |
| 136 | + |
| 137 | +def get_requires_for_build_editable(config_settings=None): |
| 138 | + _prepare_for_editable() |
| 139 | + return [] |
| 140 | + |
| 141 | + |
| 142 | +def prepare_metadata_for_build_wheel(metadata_directory, config_settings=None): |
| 143 | + _prepare_for_wheel() |
| 144 | + return orig.prepare_metadata_for_build_wheel(metadata_directory, config_settings) |
| 145 | + |
| 146 | + |
| 147 | +def prepare_metadata_for_build_editable(metadata_directory, config_settings=None): |
| 148 | + _prepare_for_editable() |
| 149 | + return orig.prepare_metadata_for_build_editable(metadata_directory, config_settings) |
| 150 | + |
| 151 | + |
| 152 | +def build_editable(wheel_directory, config_settings=None, metadata_directory=None): |
| 153 | + _prepare_for_editable() |
| 154 | + return orig.build_editable(wheel_directory, config_settings, metadata_directory) |
| 155 | + |
| 156 | + |
| 157 | +def build_sdist(sdist_directory, config_settings=None): |
| 158 | + _prepare_for_sdist() |
| 159 | + return orig.build_sdist(sdist_directory, config_settings) |
| 160 | + |
| 161 | + |
| 162 | +def build_wheel(wheel_directory, config_settings=None, metadata_directory=None): |
| 163 | + _prepare_for_wheel() |
| 164 | + return orig.build_wheel(wheel_directory, config_settings, metadata_directory) |
0 commit comments