Skip to content

Commit c5ba372

Browse files
committed
preliminary rust/C++ bridge
1 parent da846c9 commit c5ba372

14 files changed

+513
-3
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
sudo apt-get -y install --no-install-recommends apt-utils dialog git iproute2 procps lsb-release
5252
- name: install tool chain
5353
run: |
54-
sudo apt-get -y install libstdc++-8-dev clang-format-10 ccache
54+
sudo apt-get -y install libstdc++-8-dev clang-format-10 ccache cargo rustc
5555
if test "${{ matrix.toolchain }}" = "gcc" ; then
5656
sudo apt-get -y install cpp-8 gcc-8 g++-8
5757
else

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ fuzz-findings/
8686
min-testcases/
8787
/my-history/
8888

89+
# Rust specific
90+
src/rust/bin
91+
src/rust/target
92+
src/rust/bridge.cpp
93+
src/rust/bridge.h
94+
src/rust/.crates.toml
95+
src/rust/.crates2.json
96+
8997
# Windows specific
9098
**/Build/*
9199
**/generated/*

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[workspace]
2+
members = ["src/rust"]

INSTALL.md

+19
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,22 @@ The GUI depends on the `capstone`, `freetype` and `glfw` libraries and their hea
165165

166166
# On MacOS
167167
$ brew install capstone freetype2 glfw
168+
169+
## Building with Rust
170+
171+
Configuring with `--enable-next-protocol-version-unsafe-for-production` will build and embed components written in the [Rust](https://rust-lang.org) programming language. These components are currently only enabled when building the "next" protocol, not the "current" one.
172+
173+
Building the Rust components requires the `cargo` package manager and build system, as well as the `rustc` compiler, both version 1.57 or later.
174+
Currently we recommend using the system packages provided on Ubuntu, and the rust project's `rustup` installer on other systems.
175+
176+
# On Ubuntu
177+
$ sudo apt-get install cargo
178+
179+
# On MacOS
180+
$ brew install rustup-init
181+
$ rustup-init
182+
183+
To use an IDE with `rust-analyzer`, additional packages are required on Ubuntu:
184+
185+
# On Ubuntu
186+
$ sudo apt-get install rust-src

common.mk

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ AM_CPPFLAGS += -isystem "$(top_srcdir)/lib" \
1010
-isystem "$(top_srcdir)/lib/fmt/include" \
1111
-isystem "$(top_srcdir)/lib/soci/src/core" \
1212
-isystem "$(top_srcdir)/lib/tracy" \
13-
-isystem "$(top_srcdir)/lib/spdlog/include"
13+
-isystem "$(top_srcdir)/lib/spdlog/include" \
14+
-isystem "$(top_srcdir)/rust/src"
1415

1516
if USE_POSTGRES
1617
AM_CPPFLAGS += -DUSE_POSTGRES=1 $(libpq_CFLAGS)

configure.ac

+26
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,32 @@ else
425425
AC_MSG_NOTICE([not using libunwind as it was not requested])
426426
fi
427427

428+
# When we shift to having Rust in the current protocol version, this block should be
429+
# made unconditional. For the time being Rust is still "in the next protocol".
430+
if test x"$enable_next_protocol_version_unsafe_for_production" = xyes; then
431+
AC_PATH_PROG(CARGO, cargo)
432+
if test x"$CARGO" = x; then
433+
AC_MSG_ERROR([cannot find cargo, needed for rust code])
434+
fi
435+
CARGO_VERSION="$(${CARGO} --version)"
436+
CARGO_VERSION="${CARGO_VERSION#cargo}"
437+
AX_COMPARE_VERSION([${CARGO_VERSION}],[ge],[1.57],[],[
438+
AC_MSG_ERROR([cargo version too old (need >= 1.57)])
439+
])
440+
AC_ARG_VAR(CARGO)
441+
442+
AC_PATH_PROG(RUSTC, rustc)
443+
if test x"$RUSTC" = x; then
444+
AC_MSG_ERROR([cannot find rustc, needed for rust code])
445+
fi
446+
RUSTC_VERSION="$(${RUSTC} --version)"
447+
RUSTC_VERSION="${RUSTC_VERSION#rustc}"
448+
AX_COMPARE_VERSION([${RUSTC_VERSION}],[ge],[1.57],[],[
449+
AC_MSG_ERROR([rustc version too old (need >= 1.57)])
450+
])
451+
AC_ARG_VAR(RUSTC)
452+
fi
453+
428454
# Need this to pass through ccache for xdrpp, libsodium
429455
esc() {
430456
out=

m4/ax_compare_version.m4

+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# ===========================================================================
2+
# https://www.gnu.org/software/autoconf-archive/ax_compare_version.html
3+
# ===========================================================================
4+
#
5+
# SYNOPSIS
6+
#
7+
# AX_COMPARE_VERSION(VERSION_A, OP, VERSION_B, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
8+
#
9+
# DESCRIPTION
10+
#
11+
# This macro compares two version strings. Due to the various number of
12+
# minor-version numbers that can exist, and the fact that string
13+
# comparisons are not compatible with numeric comparisons, this is not
14+
# necessarily trivial to do in a autoconf script. This macro makes doing
15+
# these comparisons easy.
16+
#
17+
# The six basic comparisons are available, as well as checking equality
18+
# limited to a certain number of minor-version levels.
19+
#
20+
# The operator OP determines what type of comparison to do, and can be one
21+
# of:
22+
#
23+
# eq - equal (test A == B)
24+
# ne - not equal (test A != B)
25+
# le - less than or equal (test A <= B)
26+
# ge - greater than or equal (test A >= B)
27+
# lt - less than (test A < B)
28+
# gt - greater than (test A > B)
29+
#
30+
# Additionally, the eq and ne operator can have a number after it to limit
31+
# the test to that number of minor versions.
32+
#
33+
# eq0 - equal up to the length of the shorter version
34+
# ne0 - not equal up to the length of the shorter version
35+
# eqN - equal up to N sub-version levels
36+
# neN - not equal up to N sub-version levels
37+
#
38+
# When the condition is true, shell commands ACTION-IF-TRUE are run,
39+
# otherwise shell commands ACTION-IF-FALSE are run. The environment
40+
# variable 'ax_compare_version' is always set to either 'true' or 'false'
41+
# as well.
42+
#
43+
# Examples:
44+
#
45+
# AX_COMPARE_VERSION([3.15.7],[lt],[3.15.8])
46+
# AX_COMPARE_VERSION([3.15],[lt],[3.15.8])
47+
#
48+
# would both be true.
49+
#
50+
# AX_COMPARE_VERSION([3.15.7],[eq],[3.15.8])
51+
# AX_COMPARE_VERSION([3.15],[gt],[3.15.8])
52+
#
53+
# would both be false.
54+
#
55+
# AX_COMPARE_VERSION([3.15.7],[eq2],[3.15.8])
56+
#
57+
# would be true because it is only comparing two minor versions.
58+
#
59+
# AX_COMPARE_VERSION([3.15.7],[eq0],[3.15])
60+
#
61+
# would be true because it is only comparing the lesser number of minor
62+
# versions of the two values.
63+
#
64+
# Note: The characters that separate the version numbers do not matter. An
65+
# empty string is the same as version 0. OP is evaluated by autoconf, not
66+
# configure, so must be a string, not a variable.
67+
#
68+
# The author would like to acknowledge Guido Draheim whose advice about
69+
# the m4_case and m4_ifvaln functions make this macro only include the
70+
# portions necessary to perform the specific comparison specified by the
71+
# OP argument in the final configure script.
72+
#
73+
# LICENSE
74+
#
75+
# Copyright (c) 2008 Tim Toolan <[email protected]>
76+
#
77+
# Copying and distribution of this file, with or without modification, are
78+
# permitted in any medium without royalty provided the copyright notice
79+
# and this notice are preserved. This file is offered as-is, without any
80+
# warranty.
81+
82+
#serial 13
83+
84+
dnl #########################################################################
85+
AC_DEFUN([AX_COMPARE_VERSION], [
86+
AC_REQUIRE([AC_PROG_AWK])
87+
88+
# Used to indicate true or false condition
89+
ax_compare_version=false
90+
91+
# Convert the two version strings to be compared into a format that
92+
# allows a simple string comparison. The end result is that a version
93+
# string of the form 1.12.5-r617 will be converted to the form
94+
# 0001001200050617. In other words, each number is zero padded to four
95+
# digits, and non digits are removed.
96+
AS_VAR_PUSHDEF([A],[ax_compare_version_A])
97+
A=`echo "$1" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \
98+
-e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \
99+
-e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \
100+
-e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \
101+
-e 's/[[^0-9]]//g'`
102+
103+
AS_VAR_PUSHDEF([B],[ax_compare_version_B])
104+
B=`echo "$3" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \
105+
-e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \
106+
-e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \
107+
-e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \
108+
-e 's/[[^0-9]]//g'`
109+
110+
dnl # In the case of le, ge, lt, and gt, the strings are sorted as necessary
111+
dnl # then the first line is used to determine if the condition is true.
112+
dnl # The sed right after the echo is to remove any indented white space.
113+
m4_case(m4_tolower($2),
114+
[lt],[
115+
ax_compare_version=`echo "x$A
116+
x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/false/;s/x${B}/true/;1q"`
117+
],
118+
[gt],[
119+
ax_compare_version=`echo "x$A
120+
x$B" | sed 's/^ *//' | sort | sed "s/x${A}/false/;s/x${B}/true/;1q"`
121+
],
122+
[le],[
123+
ax_compare_version=`echo "x$A
124+
x$B" | sed 's/^ *//' | sort | sed "s/x${A}/true/;s/x${B}/false/;1q"`
125+
],
126+
[ge],[
127+
ax_compare_version=`echo "x$A
128+
x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/true/;s/x${B}/false/;1q"`
129+
],[
130+
dnl Split the operator from the subversion count if present.
131+
m4_bmatch(m4_substr($2,2),
132+
[0],[
133+
# A count of zero means use the length of the shorter version.
134+
# Determine the number of characters in A and B.
135+
ax_compare_version_len_A=`echo "$A" | $AWK '{print(length)}'`
136+
ax_compare_version_len_B=`echo "$B" | $AWK '{print(length)}'`
137+
138+
# Set A to no more than B's length and B to no more than A's length.
139+
A=`echo "$A" | sed "s/\(.\{$ax_compare_version_len_B\}\).*/\1/"`
140+
B=`echo "$B" | sed "s/\(.\{$ax_compare_version_len_A\}\).*/\1/"`
141+
],
142+
[[0-9]+],[
143+
# A count greater than zero means use only that many subversions
144+
A=`echo "$A" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"`
145+
B=`echo "$B" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"`
146+
],
147+
[.+],[
148+
AC_WARNING(
149+
[invalid OP numeric parameter: $2])
150+
],[])
151+
152+
# Pad zeros at end of numbers to make same length.
153+
ax_compare_version_tmp_A="$A`echo $B | sed 's/./0/g'`"
154+
B="$B`echo $A | sed 's/./0/g'`"
155+
A="$ax_compare_version_tmp_A"
156+
157+
# Check for equality or inequality as necessary.
158+
m4_case(m4_tolower(m4_substr($2,0,2)),
159+
[eq],[
160+
test "x$A" = "x$B" && ax_compare_version=true
161+
],
162+
[ne],[
163+
test "x$A" != "x$B" && ax_compare_version=true
164+
],[
165+
AC_WARNING([invalid OP parameter: $2])
166+
])
167+
])
168+
169+
AS_VAR_POPDEF([A])dnl
170+
AS_VAR_POPDEF([B])dnl
171+
172+
dnl # Execute ACTION-IF-TRUE / ACTION-IF-FALSE.
173+
if test "$ax_compare_version" = "true" ; then
174+
m4_ifvaln([$4],[$4],[:])dnl
175+
m4_ifvaln([$5],[else $5])dnl
176+
fi
177+
]) dnl AX_COMPARE_VERSION

make-mks

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ message="# This file was generated by make-mks; don't edit it by hand."
2323
echo "SRC_X_FILES" = $(git ls-files '*.x' | tr " " "\n" | sort | uniq | tr "\n" " ")
2424
echo "SRC_TEST_H_FILES" = $(git ls-files '*.h' '*.[ih]pp' | tr " " "\n" | egrep '(test|simulation)/' | tr "\n" " ")
2525
echo "SRC_TEST_CXX_FILES" = $(git ls-files '*.cpp' | tr " " "\n" | sort | uniq | egrep '(test|simulation)/' | tr "\n" " ")
26+
echo "SRC_RUST_FILES" = $(git ls-files '*.rs' | tr " " "\n" | sort | uniq | tr "\n" " ")
2627
) > src/src.mk
2728

2829

src/Makefile.am

+37-1
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,46 @@ TEST_FILES = $(TESTDATA_DIR)/stellar-core_example.cfg $(TESTDATA_DIR)/stellar-co
2828
BUILT_SOURCES = $(SRC_X_FILES:.x=.h) main/StellarCoreVersion.cpp $(TEST_FILES)
2929

3030
$(SRC_X_FILES:.x=.h): $(XDRC)
31-
SUFFIXES = .x .h
31+
SUFFIXES = .x .h .rs
3232
.x.h:
3333
$(XDRC) -hh -pedantic -o $@ $<
3434

35+
36+
# This needs to be outside the next-protocol block
37+
# because, weirdly, RUST_BUILD_DIR winds up embedded
38+
# in generated Makefile for tracking bridge.Po even
39+
# when we are not building the bridge. Automake is odd.
40+
RUST_BUILD_DIR=$(top_builddir)/src/rust
41+
42+
if ENABLE_NEXT_PROTOCOL_VERSION_UNSAFE_FOR_PRODUCTION
43+
RUST_SRC_DIR=$(top_srcdir)/src/rust/src
44+
RUST_BIN_DIR=$(RUST_BUILD_DIR)/bin
45+
RUST_TARGET_DIR=$(RUST_BUILD_DIR)/target
46+
RUST_CXXBRIDGE=$(RUST_BIN_DIR)/cxxbridge
47+
RUST_PROFILE=release
48+
LIBRUST_STELLAR_CORE=$(RUST_TARGET_DIR)/$(RUST_PROFILE)/librust_stellar_core.a
49+
BUILT_SOURCES += $(RUST_BUILD_DIR)/bridge.h $(RUST_BUILD_DIR)/bridge.cpp
50+
stellar_core_SOURCES += $(RUST_BUILD_DIR)/bridge.h $(RUST_BUILD_DIR)/bridge.cpp
51+
stellar_core_LDADD += $(LIBRUST_STELLAR_CORE) -ldl
52+
53+
$(RUST_CXXBRIDGE):
54+
mkdir -p $(RUST_BIN_DIR)
55+
$(CARGO) install --root $(RUST_BUILD_DIR) cxxbridge-cmd
56+
57+
$(RUST_BUILD_DIR)/bridge.h: $(RUST_SRC_DIR)/lib.rs $(SRC_RUST_FILES) $(top_srcdir)/src/Makefile.am $(RUST_CXXBRIDGE)
58+
$(RUST_CXXBRIDGE) $< --header --output $@.tmp
59+
if cmp -s $@.tmp $@; then rm -v $@.tmp; else mv -v $@.tmp $@; fi
60+
61+
$(RUST_BUILD_DIR)/bridge.cpp: $(RUST_SRC_DIR)/lib.rs $(SRC_RUST_FILES) $(top_srcdir)/src/Makefile.am $(RUST_CXXBRIDGE)
62+
$(RUST_CXXBRIDGE) $< --output $@.tmp
63+
if cmp -s $@.tmp $@; then rm -v $@.tmp; else mv -v $@.tmp $@; fi
64+
65+
$(LIBRUST_STELLAR_CORE): $(SRC_RUST_FILES) $(top_srcdir)/src/Makefile.am
66+
cd $(RUST_BUILD_DIR) && cargo build --$(RUST_PROFILE) --target-dir $(abspath $(RUST_TARGET_DIR))
67+
ranlib $@
68+
endif # ENABLE_NEXT_PROTOCOL_VERSION_UNSAFE_FOR_PRODUCTION
69+
70+
3571
$(srcdir)/src.mk: $(top_srcdir)/make-mks
3672
cd $(top_srcdir) && ./make-mks
3773

0 commit comments

Comments
 (0)