Mercurial > hg
comparison rust/hgcli/Cargo.lock @ 35569:964212780daf
rust: implementation of `hg`
This commit provides a mostly-working implementation of the
`hg` script in Rust along with scaffolding to support Rust in
the repository.
If you are familiar with Rust, the contents of the added rust/
directory should be pretty straightforward. We create an "hgcli"
package that implements a binary application to run Mercurial.
The output of this package is an "hg" binary.
Our Rust `hg` (henceforth "rhg") essentially is a port of the existing
`hg` Python script. The main difference is the creation of the embedded
CPython interpreter is handled by the binary itself instead of relying
on the shebang. In that sense, rhg is more similar to the "exe wrapper"
we currently use on Windows. However, unlike the exe wrapper, rhg does
not call the `hg` Python script. Instead, it uses the CPython APIs to
import mercurial modules and call appropriate functions. The amount of
code here is surprisingly small.
It is my intent to replace the existing C-based exe wrapper with rhg.
Preferably in the next Mercurial release. This should be achievable -
at least for some Mercurial distributions. The future/timeline for
rhg on other platforms is less clear. We already ship a hg.exe on
Windows. So if we get the quirks with Rust worked out, shipping a
Rust-based hg.exe should hopefully not be too contentious.
Now onto the implementation.
We're using python27-sys and the cpython crates for talking to the
CPython API. We currently don't use too much functionality of the
cpython crate and could have probably cut it out. However, it does
provide a reasonable abstraction over unsafe {} CPython function
calls. While we still have our fair share of those, at least we're
not dealing with too much refcounting, error checking, etc. So I
think the use of the cpython crate is justified. Plus, there is
not-yet-implemented functionality that could benefit from cpython. I
see our use of this crate only increasing.
The cpython and python27-sys crates are not without their issues.
The cpython crate didn't seem to account for the embedding use case
in its design. Instead, it seems to assume that you are building
a Python extension. It is making some questionable decisions around
certain CPython APIs. For example, it insists that
PyEval_ThreadsInitialized() is called and that the Python code
likely isn't the main thread in the underlying application. It
is also missing some functionality that is important for embedded
use cases (such as exporting the path to the Python interpreter
from its build script). After spending several hours trying to
wrangle python27-sys and cpython, I gave up and forked the project
on GitHub. Our Cargo.toml tracks this fork. I'm optimistic that
the upstream project will accept our contributions and we can
eventually unfork.
There is a non-trivial amount of code in our custom Cargo build
script. Our build.rs (which is called as part of building the hgcli
crate):
* Validates that the Python interpreter that was detected by the
python27-sys crate provides a shared library (we only support
shared library linking at this time - although this restriction
could be loosened).
* Validates that the Python is built with UCS-4 support. This ensures
maximum Unicode compatibility.
* Exports variables to the crate build allowing the built crate to e.g.
find the path to the Python interpreter.
The produced rhg should be considered alpha quality. There are several
known deficiencies. Many of these are documented with inline TODOs.
Probably the biggest limitation of rhg is that it assumes it is
running from the ./rust/target/<target> directory of a source
distribution. So, rhg is currently not very practical for real-world
use. But, if you can `cargo build` it, running the binary *should*
yield a working Mercurial CLI.
In order to support using rhg with the test harness, we needed to hack
up run-tests.py so the path to Mercurial's Python files is set properly.
The change is extremely hacky and is only intended to be a stop-gap
until the test harness gains first-class support for installing rhg.
This will likely occur after we support running rhg outside the
source directory.
Despite its officially alpha quality, rhg copes extremely well with
the test harness (at least on Linux). Using
`run-tests.py --with-hg ../rust/target/debug/hg`, I only encounter
the following failures:
* test-run-tests.t -- Warnings emitted about using an unexpected
Mercurial library. This is due to the hacky nature of setting the
Python directory when run-tests.py detected rhg.
* test-devel-warnings.t -- Expected stack trace missing frame for `hg`
(This is expected since we no longer have an `hg` script!)
* test-convert.t -- Test running `$PYTHON "$BINDIR"/hg`, which obviously
assumes `hg` is a Python script.
* test-merge-tools.t -- Same assumption about `hg` being executable with
Python.
* test-http-bad-server.t -- Seeing exit code 255 instead of 1 around
line 358.
* test-blackbox.t -- Exit code 255 instead of 1.
* test-basic.t -- Exit code 255 instead of 1.
It certainly looks like we have a bug around exit code handling. I
don't think it is severe enough to hold up review and landing of this
initial implementation. Perfect is the enemy of good.
Differential Revision: https://phab.mercurial-scm.org/D1581
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Wed, 10 Jan 2018 08:53:22 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
35568:ebf14075a5c1 | 35569:964212780daf |
---|---|
1 [[package]] | |
2 name = "aho-corasick" | |
3 version = "0.5.3" | |
4 source = "registry+https://github.com/rust-lang/crates.io-index" | |
5 dependencies = [ | |
6 "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", | |
7 ] | |
8 | |
9 [[package]] | |
10 name = "cpython" | |
11 version = "0.1.0" | |
12 source = "git+https://github.com/indygreg/rust-cpython.git?rev=c90d65cf84abfffce7ef54476bbfed56017a2f52#c90d65cf84abfffce7ef54476bbfed56017a2f52" | |
13 dependencies = [ | |
14 "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", | |
15 "num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", | |
16 "python27-sys 0.1.2 (git+https://github.com/indygreg/rust-cpython.git?rev=c90d65cf84abfffce7ef54476bbfed56017a2f52)", | |
17 ] | |
18 | |
19 [[package]] | |
20 name = "hgcli" | |
21 version = "0.1.0" | |
22 dependencies = [ | |
23 "cpython 0.1.0 (git+https://github.com/indygreg/rust-cpython.git?rev=c90d65cf84abfffce7ef54476bbfed56017a2f52)", | |
24 "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", | |
25 "python27-sys 0.1.2 (git+https://github.com/indygreg/rust-cpython.git?rev=c90d65cf84abfffce7ef54476bbfed56017a2f52)", | |
26 ] | |
27 | |
28 [[package]] | |
29 name = "kernel32-sys" | |
30 version = "0.2.2" | |
31 source = "registry+https://github.com/rust-lang/crates.io-index" | |
32 dependencies = [ | |
33 "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", | |
34 "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", | |
35 ] | |
36 | |
37 [[package]] | |
38 name = "libc" | |
39 version = "0.2.34" | |
40 source = "registry+https://github.com/rust-lang/crates.io-index" | |
41 | |
42 [[package]] | |
43 name = "memchr" | |
44 version = "0.1.11" | |
45 source = "registry+https://github.com/rust-lang/crates.io-index" | |
46 dependencies = [ | |
47 "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", | |
48 ] | |
49 | |
50 [[package]] | |
51 name = "num-traits" | |
52 version = "0.1.41" | |
53 source = "registry+https://github.com/rust-lang/crates.io-index" | |
54 | |
55 [[package]] | |
56 name = "python27-sys" | |
57 version = "0.1.2" | |
58 source = "git+https://github.com/indygreg/rust-cpython.git?rev=c90d65cf84abfffce7ef54476bbfed56017a2f52#c90d65cf84abfffce7ef54476bbfed56017a2f52" | |
59 dependencies = [ | |
60 "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", | |
61 "regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)", | |
62 ] | |
63 | |
64 [[package]] | |
65 name = "regex" | |
66 version = "0.1.80" | |
67 source = "registry+https://github.com/rust-lang/crates.io-index" | |
68 dependencies = [ | |
69 "aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", | |
70 "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", | |
71 "regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", | |
72 "thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", | |
73 "utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", | |
74 ] | |
75 | |
76 [[package]] | |
77 name = "regex-syntax" | |
78 version = "0.3.9" | |
79 source = "registry+https://github.com/rust-lang/crates.io-index" | |
80 | |
81 [[package]] | |
82 name = "thread-id" | |
83 version = "2.0.0" | |
84 source = "registry+https://github.com/rust-lang/crates.io-index" | |
85 dependencies = [ | |
86 "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", | |
87 "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", | |
88 ] | |
89 | |
90 [[package]] | |
91 name = "thread_local" | |
92 version = "0.2.7" | |
93 source = "registry+https://github.com/rust-lang/crates.io-index" | |
94 dependencies = [ | |
95 "thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", | |
96 ] | |
97 | |
98 [[package]] | |
99 name = "utf8-ranges" | |
100 version = "0.1.3" | |
101 source = "registry+https://github.com/rust-lang/crates.io-index" | |
102 | |
103 [[package]] | |
104 name = "winapi" | |
105 version = "0.2.8" | |
106 source = "registry+https://github.com/rust-lang/crates.io-index" | |
107 | |
108 [[package]] | |
109 name = "winapi-build" | |
110 version = "0.1.1" | |
111 source = "registry+https://github.com/rust-lang/crates.io-index" | |
112 | |
113 [metadata] | |
114 "checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66" | |
115 "checksum cpython 0.1.0 (git+https://github.com/indygreg/rust-cpython.git?rev=c90d65cf84abfffce7ef54476bbfed56017a2f52)" = "<none>" | |
116 "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" | |
117 "checksum libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)" = "36fbc8a8929c632868295d0178dd8f63fc423fd7537ad0738372bd010b3ac9b0" | |
118 "checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20" | |
119 "checksum num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "cacfcab5eb48250ee7d0c7896b51a2c5eec99c1feea5f32025635f5ae4b00070" | |
120 "checksum python27-sys 0.1.2 (git+https://github.com/indygreg/rust-cpython.git?rev=c90d65cf84abfffce7ef54476bbfed56017a2f52)" = "<none>" | |
121 "checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f" | |
122 "checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957" | |
123 "checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03" | |
124 "checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5" | |
125 "checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f" | |
126 "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" | |
127 "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" |