diff --git a/rust/treedirstate/.cargo/config b/.cargo/config rename from rust/treedirstate/.cargo/config rename to .cargo/config diff --git a/Makefile b/Makefile --- a/Makefile +++ b/Makefile @@ -15,7 +15,8 @@ $(PYTHON) setup.py \ build_py -c -d . \ build_clib \ - build_ext -i + build_ext -i \ + build_rust_ext -i install: $(PYTHON) setup.py $(PURE) install --prefix="$(PREFIX)" --force diff --git a/setup.py b/setup.py --- a/setup.py +++ b/setup.py @@ -2,6 +2,7 @@ from distutils.cmd import Command from distutils.core import setup, Extension import distutils +from distutils_rust import RustExtension, BuildRustExt import fnmatch from glob import glob @@ -22,6 +23,25 @@ OPTIMIZATION = "" if iswindows else "-O2" PRODUCEDEBUGSYMBOLS = "/DEBUG:FULL" if iswindows else "-g" +if 'USERUST' in os.environ: + USERUST = int(os.environ['USERUST']) +else: + import subprocess + USERUST = False + try: + cargo_version = subprocess.check_output(['cargo', '--version']).split()[1] + except Exception: + sys.stderr.write("not compiling Rust extensions: cargo is not available\n") + else: + required_cargo_version = '0.21' + if (LooseVersion(cargo_version) >= LooseVersion(required_cargo_version)): + USERUST = True + else: + sys.stderr.write( + "not compiling Rust extensions: cargo is too old " + + "(found %s, need %s or higher)\n" + % (cargo_version, required_cargo_version)) + # whether to use Cython to recompile .pyx to .c/.cpp at build time. # if False, fallback to .c/.cpp in the repo and .pyx files are ignored. # if True, re-compile .c/.cpp from .pyx files, require cython at build time. @@ -212,6 +232,10 @@ 'treemanifest', 'linelog', ] + if USERUST: + availablepackages += [ + 'treedirstate', + ] def distutils_dir_name(dname): """Returns the name of a distutils build directory""" @@ -496,6 +520,15 @@ for name in fnmatch.filter(files, patten): yield os.path.join(dirname, name) +rust_ext_modules = [] +if USERUST: + rust_ext_modules.extend([ + RustExtension('rusttreedirstate', + package='treedirstate', + manifest='rust/treedirstate/Cargo.toml', + ), + ]) + setup( name='fbhgext', version='1.0', @@ -510,9 +543,11 @@ packages=packages, install_requires=requires, py_modules=py_modules, - ext_modules = ext_modules, + ext_modules=ext_modules, libraries=libraries, + rust_ext_modules=rust_ext_modules, cmdclass={ 'clean_ext': CleanExtCommand, + 'build_rust_ext': BuildRustExt, } ) diff --git a/treedirstate/Makefile b/treedirstate/Makefile deleted file mode 100644 --- a/treedirstate/Makefile +++ /dev/null @@ -1,44 +0,0 @@ -NAME := rusttreedirstate - -# Cargo's depfiles use full paths, so we must include the full path -# to the rust library. -DIR := $(realpath $(join $(dir $(firstword $(MAKEFILE_LIST))),../rust/treedirstate)) - -MODE ?= release - -ifeq ($(MODE),release) -CARGO_ARGS += --release -endif - -ifeq ($(shell uname),Darwin) -LIBSUFFIX := .dylib -else -LIBSUFFIX := .so -endif - -RUSTLIB := $(DIR)/target/$(MODE)/lib$(NAME)$(LIBSUFFIX) -RUSTDEP := $(DIR)/target/$(MODE)/lib$(NAME).d - -.PHONY: all -all: $(NAME).so - -$(NAME).so: $(RUSTLIB) - cp $< $@ - -$(RUSTLIB): - cd $(DIR) && cargo build $(CARGO_ARGS) - -.PHONY: test -test: - cd $(DIR) && cargo test - -.PHONY: clean -clean: - $(RM) $(NAME).so - cd $(DIR) && cargo clean - -# This pattern rule for Rust source files forces a rebuild if any source file -# is deleted. -$(DIR)/src/%.rs: ; - --include $(RUSTDEP)