Link postgres to rust statically

This commit is contained in:
Arthur Petukhovsky
2023-05-31 13:19:41 +00:00
parent ac82b34c64
commit b6a80bc269
13 changed files with 90 additions and 16 deletions

View File

@@ -14,3 +14,6 @@ opt-level = 1
[alias]
build_testing = ["build", "--features", "testing"]
[build]
rustflags = ["-C", "default-linker-libraries"]

View File

@@ -39,6 +39,8 @@ endif
# been no changes to the files. Changing the mtime triggers an
# unnecessary rebuild of 'postgres_ffi'.
PG_CONFIGURE_OPTS += INSTALL='$(ROOT_PROJECT_DIR)/scripts/ninstall.sh -C'
PG_CONFIGURE_OPTS += CC=clang
PG_CONFIGURE_OPTS += CCX=clang++
# Choose whether we should be silent or verbose
CARGO_BUILD_FLAGS += --$(if $(filter s,$(MAKEFLAGS)),quiet,verbose)

0
libs/walproposer/.gitignore vendored Normal file
View File

View File

@@ -8,3 +8,9 @@ Entrypoint header file is `bindgen_deps.h`.
## C -> Rust
We use `cbindgen` to generate C bindings for the Rust code. They are stored in `rust_bindings.h`.
## How to run the tests
```
export RUSTFLAGS="-C default-linker-libraries"
```

View File

@@ -16,19 +16,51 @@ fn main() -> anyhow::Result<()> {
.write_to_file("rust_bindings.h");
// Tell cargo to invalidate the built crate whenever the wrapper changes
println!("cargo:rerun-if-changed=bindgen_deps.h,walproposer.c,walproposer.h,test.c");
println!("cargo:rustc-link-lib=walproposer");
println!("cargo:rustc-link-search=/home/admin/simulator/libs/walproposer");
println!("cargo:rerun-if-changed=bindgen_deps.h,walproposer.c,walproposer.h,test.c,libpostgres.a,../../pgxn/neon/walproposer.c,build.sh");
// println!("cargo:rustc-link-lib=walproposer");
// println!("cargo:rustc-link-lib=ext");
// println!("cargo:rustc-link-lib=pgport_srv");
// println!("cargo:rustc-link-lib=postgres");
// println!("cargo:rustc-link-lib=pgcommon_srv");
// println!("cargo:rustc-link-lib=pgport_srv");
println!("cargo:rustc-link-arg=-Wl,--start-group");
println!("cargo:rustc-link-arg=-lwalproposer");
println!("cargo:rustc-link-arg=-lext");
println!("cargo:rustc-link-arg=-lpgport_srv");
println!("cargo:rustc-link-arg=-lpostgres");
println!("cargo:rustc-link-arg=-lpgcommon_srv");
println!("cargo:rustc-link-arg=-lssl");
println!("cargo:rustc-link-arg=-lcrypto");
println!("cargo:rustc-link-arg=-lz");
println!("cargo:rustc-link-arg=-lpthread");
println!("cargo:rustc-link-arg=-lrt");
println!("cargo:rustc-link-arg=-ldl");
println!("cargo:rustc-link-arg=-lm");
println!("cargo:rustc-link-arg=-Wl,--end-group");
// println!("cargo:rustc-flags=-C default-linker-libraries=y");
if !std::process::Command::new("./build.sh")
.output()
.expect("could not spawn `clang`")
.status
.success()
{
// Panic if the command was not successful.
panic!("could not compile object file");
}
// echo -lseccomp -lssl -lcrypto -lz -lpthread -lrt -ldl -lm
// println!("cargo:rustc-link-lib=ssl");
// println!("cargo:rustc-link-lib=crypto");
// println!("cargo:rustc-link-lib=walproposer2");
// println!("cargo:rustc-link-lib=postgres");
// println!("cargo:rustc-link-lib=pq");
// println!("cargo:rustc-link-lib=ssl");
// println!("cargo:rustc-link-lib=crypto");
println!("cargo:rustc-link-search=/home/admin/simulator/libs/walproposer");
// disable fPIE
println!("cargo:rustc-link-arg=-no-pie");
// if !std::process::Command::new("./build.sh")
// .output()
// .expect("could not spawn `clang`")
// .status
// .success()
// {
// // Panic if the command was not successful.
// panic!("could not compile object file");
// }
// println!("cargo:rustc-link-lib=dylib=neon");
// println!("cargo:rustc-link-search=/Users/arthur/zen/zenith/pg_install/build/neon-v15");
@@ -94,6 +126,7 @@ fn main() -> anyhow::Result<()> {
// included header files changed.
.parse_callbacks(Box::new(CargoCallbacks))
.allowlist_function("TestFunc")
// .allowlist_function("WalProposerRust")
// .clang_arg(format!("-I{inc_server_path}"))
// .clang_arg(format!("-I{inc_pgxn_path}"))
// Finish the builder and generate the bindings.

View File

@@ -1,4 +1,5 @@
#!/bin/bash
set -e
# cc -c -o walproposer.o \
# -static \
@@ -16,5 +17,18 @@
# clang -c -o walproposer.o walproposer.c -ferror-limit=1
# ar rcs libwalproposer.a walproposer.o
clang -c -o test.o test.c -ferror-limit=1
make -C ../../ neon-pg-ext-v15 -s
make -C ../../pg_install/build/v15/src/backend postgres-lib -s
cp ../../pg_install/build/v15/src/backend/libpostgres.a .
cp ../../pg_install/build/v15/src/common/libpgcommon_srv.a .
cp ../../pg_install/build/v15/src/port/libpgport_srv.a .
cp ../../pg_install/build/v15/src/interfaces/libpq/libpq.a .
# -lseccomp -lssl -lcrypto -lz -lpthread -lrt -ldl -lm
clang -c -o test.o test.c -ferror-limit=1 -I ../../pg_install/v15/include/postgresql/server
rm -rf libwalproposer.a
ar rcs libwalproposer.a test.o
rm -rf libwalproposer2.a
ar rcs libwalproposer2.a ../../pg_install/build/neon-v15/*.o

3
libs/walproposer/ext.c Normal file
View File

@@ -0,0 +1,3 @@
int hohoho(int a, int b) {
return a + b;
}

Binary file not shown.

View File

@@ -6,7 +6,7 @@ pub mod bindings {
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}
pub use bindings::TestFunc;
pub use bindings::{TestFunc};
use std::cell::RefCell;

View File

@@ -3,5 +3,6 @@ use crate::{TestFunc, TMP_TEST};
#[test]
fn run_test() {
let res = unsafe { TestFunc(1, 2) };
// unsafe { WalProposerRust(); }
println!("res: {}", res);
}

View File

@@ -1,9 +1,16 @@
#include "bindgen_deps.h"
#include "rust_bindings.h"
#include <stdio.h>
#include "postgres.h"
// From src/backend/main/main.c
const char *progname = "fakepostgres";
int hohoho(int a, int b);
int TestFunc(int a, int b) {
printf("TestFunc: %d + %d = %d\n", a, b, a + b);
// elog(LOG, "postgres elog test");
rust_function(0);
return a + b;
return hohoho(a, b);
}

View File

@@ -316,6 +316,11 @@ nwp_shmem_startup_hook(void)
WalproposerShmemInit();
}
void WalProposerRust()
{
elog(LOG, "WalProposerRust");
}
/*
* WAL proposer bgworker entry point.
*/