1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
#! /usr/bin/env python3
"""Installation script for brz.
Run it with
'./setup.py install', or
'./setup.py --help' for more options.
"""
import importlib.util
import os
import os.path
import sys
if not importlib.util.find_spec("setuptools"):
sys.stderr.write("[ERROR] Please install setuptools ({})\n".format(e))
sys.exit(1)
try:
from setuptools_rust import Binding, RustExtension, Strip
except ModuleNotFoundError as e:
sys.stderr.write("[ERROR] Please install setuptools_rust ({})\n".format(e))
sys.exit(1)
from setuptools import setup
try:
from packaging.version import Version
except ImportError:
from distutils.version import LooseVersion as Version
try:
from setuptools_gettext import build_mo
except ImportError:
sys.stderr.write(
"[ERROR] Please install setuptools_gettext to build translations.\n"
)
sys.exit(1)
from distutils.command.build_scripts import build_scripts
from setuptools import Command
###############################
# Overridden distutils actions
###############################
class brz_build_scripts(build_scripts):
"""Fixup Rust extension binary files to live under scripts."""
def run(self):
build_scripts.run(self)
self.run_command("build_ext")
build_ext = self.get_finalized_command("build_ext")
for ext in self.distribution.rust_extensions:
if ext.binding == Binding.Exec:
# GZ 2021-08-19: Not handling multiple binaries yet.
os.replace(
os.path.join(build_ext.build_lib, ext.name),
os.path.join(self.build_dir, ext.name),
)
class build_man(Command):
"""Generate brz.1."""
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
from tools import generate_docs
generate_docs.main(argv=["brz", "man"])
########################
## Setup
########################
from setuptools.command.build import build
build.sub_commands.append(("build_mo", lambda _: True))
command_classes = {
"build_man": build_man,
}
from distutils.extension import Extension
ext_modules = []
try:
from Cython.Compiler.Version import version as cython_version
from Cython.Distutils import build_ext
except ModuleNotFoundError:
have_cython = False
# try to build the extension from the prior generated source.
print("")
print(
"The python package 'Cython' is not available. If the .c files are available,"
)
print("they will be built, but modifying the .pyx files will not rebuild them.")
print("")
from distutils.command.build_ext import build_ext
else:
minimum_cython_version = "0.29"
cython_version_info = Version(cython_version)
if cython_version_info < Version(minimum_cython_version):
print(
"Version of Cython is too old. Current is {}, need at least {}.".format(
cython_version, minimum_cython_version
)
)
print(
"If the .c files are available, they will be built,"
" but modifying the .pyx files will not rebuild them."
)
have_cython = False
else:
have_cython = True
# Override the build_ext if we have Cython available
command_classes["build_ext"] = build_ext
unavailable_files = []
def add_cython_extension(module_name, libraries=None, extra_source=None):
"""Add a cython module to build.
This will use Cython to auto-generate the .c file if it is available.
Otherwise it will fall back on the .c file. If the .c file is not
available, it will warn, and not add anything.
You can pass any extra options to Extension through kwargs. One example is
'libraries = []'.
:param module_name: The python path to the module. This will be used to
determine the .pyx and .c files to use.
"""
if extra_source is None:
extra_source = []
path = module_name.replace(".", "/")
cython_name = path + ".pyx"
c_name = path + ".c"
define_macros = []
if sys.platform == "win32":
# cython uses the macro WIN32 to detect the platform, even though it
# should be using something like _WIN32 or MS_WINDOWS, oh well, we can
# give it the right value.
define_macros.append(("WIN32", None))
if have_cython:
source = [cython_name]
else:
if not os.path.isfile(c_name):
unavailable_files.append(c_name)
return
else:
source = [c_name]
source.extend(extra_source)
include_dirs = ["breezy"]
ext_modules.append(
Extension(
module_name,
source,
define_macros=define_macros,
libraries=libraries,
include_dirs=include_dirs,
optional=os.environ.get("CIBUILDWHEEL", "0") != "1",
)
)
add_cython_extension("breezy._annotator_pyx")
add_cython_extension("breezy._chunks_to_lines_pyx")
add_cython_extension(
"breezy.bzr._groupcompress_pyx", extra_source=["breezy/bzr/diff-delta.c"]
)
add_cython_extension("breezy.bzr._knit_load_data_pyx")
add_cython_extension("breezy._known_graph_pyx")
add_cython_extension("breezy.bzr._rio_pyx")
if sys.platform == "win32":
add_cython_extension("breezy.bzr._dirstate_helpers_pyx", libraries=["Ws2_32"])
else:
add_cython_extension("breezy.bzr._dirstate_helpers_pyx")
add_cython_extension("breezy._readdir_pyx")
add_cython_extension("breezy.bzr._chk_map_pyx")
add_cython_extension("breezy.bzr._btree_serializer_pyx")
if unavailable_files:
print("C extension(s) not found:")
print(" {}".format("\n ".join(unavailable_files)))
print("The python versions will be used instead.")
print("")
if "editable_wheel" not in sys.argv:
command_classes["build_scripts"] = brz_build_scripts
# ad-hoc for easy_install
DATA_FILES = []
if (
"bdist_egg" not in sys.argv
and "bdist_wheel" not in sys.argv
and "editable_wheel" not in sys.argv
):
# generate and install brz.1 only with plain install, not the
# easy_install one
build.sub_commands.append(("build_man", lambda _: True))
DATA_FILES = [("man/man1", ["brz.1", "breezy/git/git-remote-bzr.1"])]
import site
site.ENABLE_USER_SITE = "--user" in sys.argv
rust_extensions = [
RustExtension("breezy.bzr._rio_rs", "lib-rio/Cargo.toml", binding=Binding.PyO3),
]
entry_points = {}
if (
os.environ.get("CIBUILDWHEEL", "0") == "0"
and "__pypy__" not in sys.builtin_module_names
and sys.platform != "win32"
):
rust_extensions.append(RustExtension("brz", binding=Binding.Exec, strip=Strip.All))
else:
# Fall back to python main on cibuildwheels, since it doesn't provide
# -lpython3.7 to link binaries against
# also, disable it for PyPy. See https://foss.heptapod.net/pypy/pypy/-/issues/3286
entry_points.setdefault("console_scripts", []).append("brz=breezy.__main__:main")
# std setup
setup(
scripts=[ # TODO(jelmer): Only install the git scripts if
# Dulwich was found.
"breezy/git/git-remote-bzr",
"breezy/git/bzr-receive-pack",
"breezy/git/bzr-upload-pack",
],
data_files=DATA_FILES,
cmdclass=command_classes,
ext_modules=ext_modules,
entry_points=entry_points,
rust_extensions=rust_extensions,
)
|