Skip to content

Commit 9a2de89

Browse files
committed
Update Rust detection and binding generation
This commit updates the build system to automatically detect cargo and enable/disable _base64 without needing to pass a flag. It also updates cpython-sys to use a hand written header (which is what Linux seems to do) and splits off the parser bindings to be handled in the future (since the files are included differently).
1 parent 35cdedb commit 9a2de89

9 files changed

Lines changed: 80 additions & 125 deletions

File tree

Makefile.pre.in

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,10 +1652,6 @@ Makefile Modules/config.c: Makefile.pre \
16521652
@mv config.c Modules
16531653
@echo "The Makefile was updated, you may need to re-run make."
16541654

1655-
.PHONY: regen-rust-wrapper-h
1656-
regen-rust-wrapper-h: $(PYTHON_HEADERS)
1657-
PYTHON_HEADERS="$(PYTHON_HEADERS)" $(PYTHON_FOR_REGEN) $(srcdir)/Tools/build/regen-rust-wrapper-h.py
1658-
16591655
.PHONY: regen-test-frozenmain
16601656
regen-test-frozenmain: $(BUILDPYTHON)
16611657
# Regenerate Programs/test_frozenmain.h
@@ -3376,6 +3372,9 @@ Python/thread.o: @THREADHEADERS@ $(srcdir)/Python/condvar.h
33763372
##########################################################################
33773373
# Module dependencies and platform-specific files
33783374

3375+
cpython-sys: Modules/cpython-sys/Cargo.toml Modules/cpython-sys/build.rs Modules/cpython-sys/wrapper.h Modules/cpython-sys/parser.h
3376+
cargo build --lib --locked --package cpython-sys --profile $(CARGO_PROFILE)
3377+
33793378
# force rebuild when header file or module build flavor (static/shared) is changed
33803379
MODULE_DEPS_STATIC=Modules/config.c
33813380
MODULE_DEPS_SHARED=@MODULE_DEPS_SHARED@

Modules/cpython-sys/build.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@ use std::path::{Path, PathBuf};
44
fn main() {
55
let curdir = std::env::current_dir().unwrap();
66
let srcdir = curdir.parent().and_then(Path::parent).unwrap();
7+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
8+
generate_c_api_bindings(srcdir, &out_path.as_path());
9+
// TODO(emmatyping): generate bindings to the internal parser API
10+
// The parser includes things slightly differently, so we should generate
11+
// it's bindings independently
12+
//generate_parser_bindings(srcdir, &out_path.as_path());
13+
}
14+
15+
fn generate_c_api_bindings(srcdir: &Path, out_path: &Path) {
716
let bindings = bindgen::Builder::default()
817
.header("wrapper.h")
918
.clang_arg(format!("-I{}", srcdir.as_os_str().to_str().unwrap()))
1019
.clang_arg(format!("-I{}/Include", srcdir.as_os_str().to_str().unwrap()))
11-
.clang_arg(format!("-I{}/Include/internal", srcdir.as_os_str().to_str().unwrap()))
12-
.allowlist_function("Py.*")
13-
.allowlist_function("_Py.*")
14-
.allowlist_type("Py.*")
15-
.allowlist_type("_Py.*")
16-
.allowlist_var("Py.*")
17-
.allowlist_var("_Py.*")
20+
.allowlist_function("_?Py.*")
21+
.allowlist_type("_?Py.*")
22+
.allowlist_var("_?Py.*")
1823
.blocklist_type("^PyMethodDef$")
1924
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
2025
.generate()
2126
.expect("Unable to generate bindings");
2227

23-
// Write the bindings to the $OUT_DIR/bindings.rs file.
24-
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
28+
// Write the bindings to the $OUT_DIR/c_api.rs file.
2529
bindings
26-
.write_to_file(out_path.join("bindings.rs"))
30+
.write_to_file(out_path.join("c_api.rs"))
2731
.expect("Couldn't write bindings!");
28-
}
32+
}

Modules/cpython-sys/parser.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* Private APIs */
2+
#define Py_BUILD_CORE
3+
4+
// Parser
5+
#include "Parser/pegen.h"
6+
#include "Parser/string_parser.h"
7+
#include "Parser/lexer/buffer.h"
8+
#include "Parser/lexer/lexer.h"
9+
#include "Parser/lexer/state.h"
10+
#include "Parser/tokenizer/tokenizer.h"
11+
#include "Parser/tokenizer/helpers.h"

Modules/cpython-sys/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
use std::ffi::{c_char, c_int, c_void};
88

9-
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
9+
include!(concat!(env!("OUT_DIR"), "/c_api.rs"));
10+
11+
// TODO(emmatyping): include parser bindings (see build.rs)
12+
//include!(concat!(env!("OUT_DIR"), "/parser.rs"));
1013
/* Flag passed to newmethodobject */
1114
/* #define METH_OLDARGS 0x0000 -- unsupported now */
1215
pub const METH_VARARGS: c_int = 0x0001;

Modules/makesetup

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' |
286286
done
287287
libs=
288288
# depends on the headers through cpython-sys
289-
rule="$objs: \$(srcdir)/Cargo.toml \$(srcdir)/Cargo.lock \$(srcdir)/$srcdir/$manifest Modules/cpython-sys/wrapper.h $prefixed_srcs \$(PYTHON_HEADERS)"
289+
rule="$objs: cpython-sys \$(srcdir)/Cargo.toml \$(srcdir)/Cargo.lock \$(srcdir)/$srcdir/$manifest $prefixed_srcs \$(PYTHON_HEADERS)"
290290
rule="$rule; cargo build --lib --locked --package ${mods} --profile \$(CARGO_PROFILE)"
291291
echo "$rule" >>$rulesf
292292
for mod in $mods

Python/remote_debug.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ extern "C" {
2929

3030
#include "pyconfig.h"
3131
#include "internal/pycore_ceval.h"
32-
#include "internal/pycore_debug_offsets.h"
3332

3433
#ifdef __linux__
3534
# include <elf.h>

Tools/build/regen-rust-wrapper-h.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

configure

Lines changed: 22 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4309,40 +4309,29 @@ AC_SUBST([LIBMPDEC_INTERNAL])
43094309

43104310
dnl Try to detect cargo in the environment. Cargo and rustup
43114311
dnl install into CARGO_HOME and RUSTUP_HOME, so check for those initially
4312-
AC_MSG_CHECKING([for --with-rust-base64])
4313-
AC_ARG_WITH(
4314-
[rust_base64],
4315-
[AS_HELP_STRING(
4316-
[--with-rust-base64],
4317-
[build _base64 module using the SIMD accelerated Rust implementation]
4318-
)],
4319-
[rust_base64="yes"],
4320-
[rust_base64="no"])
4321-
AC_MSG_RESULT([$rust_base64])
4322-
4323-
AS_VAR_IF(
4324-
[rust_base64], [yes],
4325-
[
4326-
if test "$CARGO_HOME+set" != "set"; then
4327-
dnl try to guess the default UNIX value of ~/.cargo
4328-
CARGO_HOME="$HOME/.cargo"
4329-
fi
4330-
AC_CHECK_PROG(HAS_CARGO, [cargo], ["$CARGO_HOME"], [found], [not-found])
4331-
if test $HAS_CARGO = "not-found"; then
4332-
AC_MSG_ERROR([Could not find cargo. Please re-run configure with \$CARGO_HOME set])
4333-
fi
4334-
if test "$Py_OPT" = 'true'; then
4335-
CARGO_TARGET_DIR='release'
4336-
CARGO_PROFILE='release'
4337-
else
4338-
CARGO_TARGET_DIR='debug'
4339-
CARGO_PROFILE='dev'
4340-
fi
4341-
AC_SUBST([CARGO_HOME])
4342-
AC_SUBST([CARGO_TARGET_DIR])
4343-
AC_SUBST([CARGO_PROFILE])
4344-
]
4345-
)
4312+
AC_MSG_CHECKING([for Rust])
4313+
CARGO_TARGET_DIR=
4314+
CARGO_PROFILE=
4315+
if test "x$CARGO_HOME" = "x"; then
4316+
dnl try to guess the default UNIX value of ~/.cargo
4317+
CARGO_HOME="$HOME/.cargo"
4318+
fi
4319+
AC_CHECK_PROG(HAS_CARGO, [cargo], ["$CARGO_HOME"], [found], [not-found])
4320+
if test $HAS_CARGO = "not-found"; then
4321+
AC_MSG_RESULT([n/a])
4322+
AC_MSG_WARN([Could not find the cargo executable. It can be installed via rustup])
4323+
else
4324+
if test "$Py_OPT" = 'true'; then
4325+
CARGO_TARGET_DIR='release'
4326+
CARGO_PROFILE='release'
4327+
else
4328+
CARGO_TARGET_DIR='debug'
4329+
CARGO_PROFILE='dev'
4330+
fi
4331+
fi
4332+
AC_SUBST([CARGO_HOME])
4333+
AC_SUBST([CARGO_TARGET_DIR])
4334+
AC_SUBST([CARGO_PROFILE])
43464335

43474336

43484337
dnl detect sqlite3 from Emscripten emport
@@ -8194,7 +8183,7 @@ PY_STDLIB_MOD([_uuid],
81948183
[$LIBUUID_CFLAGS], [$LIBUUID_LIBS])
81958184

81968185
PY_STDLIB_MOD([_base64],
8197-
[], [test "$rust_base64" = "yes"],
8186+
[], [test "$HAS_CARGO" = "found"],
81988187
[], [])
81998188

82008189
dnl compression libs

0 commit comments

Comments
 (0)