diff --git a/.cargo/config.toml b/.cargo/config.toml index c40783bc1b..212c9ea782 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -14,3 +14,6 @@ opt-level = 1 [alias] build_testing = ["build", "--features", "testing"] + +[build] +rustflags = ["-C", "default-linker-libraries"] diff --git a/Makefile b/Makefile index e04a82c7c9..2611886f73 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/libs/walproposer/.gitignore b/libs/walproposer/.gitignore new file mode 100644 index 0000000000..e69de29bb2 diff --git a/libs/walproposer/README.md b/libs/walproposer/README.md index 4644a214be..9382a4605c 100644 --- a/libs/walproposer/README.md +++ b/libs/walproposer/README.md @@ -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" +``` \ No newline at end of file diff --git a/libs/walproposer/build.rs b/libs/walproposer/build.rs index bfcd088ee7..e864276d7c 100644 --- a/libs/walproposer/build.rs +++ b/libs/walproposer/build.rs @@ -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. diff --git a/libs/walproposer/build.sh b/libs/walproposer/build.sh index 4b8bad115e..6fa3bf3cf1 100755 --- a/libs/walproposer/build.sh +++ b/libs/walproposer/build.sh @@ -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 diff --git a/libs/walproposer/ext.c b/libs/walproposer/ext.c new file mode 100644 index 0000000000..45e073a10a --- /dev/null +++ b/libs/walproposer/ext.c @@ -0,0 +1,3 @@ +int hohoho(int a, int b) { + return a + b; +} \ No newline at end of file diff --git a/libs/walproposer/libwalproposer.a b/libs/walproposer/libwalproposer.a deleted file mode 100644 index 2c0eb87e35..0000000000 Binary files a/libs/walproposer/libwalproposer.a and /dev/null differ diff --git a/libs/walproposer/src/lib.rs b/libs/walproposer/src/lib.rs index 40284897c6..2815de1e65 100644 --- a/libs/walproposer/src/lib.rs +++ b/libs/walproposer/src/lib.rs @@ -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; diff --git a/libs/walproposer/src/test.rs b/libs/walproposer/src/test.rs index 31efe738b9..bda40356c1 100644 --- a/libs/walproposer/src/test.rs +++ b/libs/walproposer/src/test.rs @@ -3,5 +3,6 @@ use crate::{TestFunc, TMP_TEST}; #[test] fn run_test() { let res = unsafe { TestFunc(1, 2) }; + // unsafe { WalProposerRust(); } println!("res: {}", res); } diff --git a/libs/walproposer/test.c b/libs/walproposer/test.c index 98a74804f2..e146c9ecdc 100644 --- a/libs/walproposer/test.c +++ b/libs/walproposer/test.c @@ -1,9 +1,16 @@ #include "bindgen_deps.h" #include "rust_bindings.h" #include +#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); } diff --git a/pgxn/neon/walproposer.c b/pgxn/neon/walproposer.c index bf8bb02493..1b34e5291d 100644 --- a/pgxn/neon/walproposer.c +++ b/pgxn/neon/walproposer.c @@ -316,6 +316,11 @@ nwp_shmem_startup_hook(void) WalproposerShmemInit(); } +void WalProposerRust() +{ + elog(LOG, "WalProposerRust"); +} + /* * WAL proposer bgworker entry point. */ diff --git a/vendor/postgres-v15 b/vendor/postgres-v15 index 919851e781..298cdce9ab 160000 --- a/vendor/postgres-v15 +++ b/vendor/postgres-v15 @@ -1 +1 @@ -Subproject commit 919851e7811fcb2ecfc67f35bfd63a35639c73b5 +Subproject commit 298cdce9ab7aa88e3e0da4701e3e66a5ea4fbd0e