From b6a80bc269b10583b177a344e870c9c5a556ff0f Mon Sep 17 00:00:00 2001 From: Arthur Petukhovsky Date: Wed, 31 May 2023 13:19:41 +0000 Subject: [PATCH] Link postgres to rust statically --- .cargo/config.toml | 3 ++ Makefile | 2 ++ libs/walproposer/.gitignore | 0 libs/walproposer/README.md | 6 ++++ libs/walproposer/build.rs | 57 +++++++++++++++++++++++------- libs/walproposer/build.sh | 16 ++++++++- libs/walproposer/ext.c | 3 ++ libs/walproposer/libwalproposer.a | Bin 1498 -> 0 bytes libs/walproposer/src/lib.rs | 2 +- libs/walproposer/src/test.rs | 1 + libs/walproposer/test.c | 9 ++++- pgxn/neon/walproposer.c | 5 +++ vendor/postgres-v15 | 2 +- 13 files changed, 90 insertions(+), 16 deletions(-) create mode 100644 libs/walproposer/.gitignore create mode 100644 libs/walproposer/ext.c delete mode 100644 libs/walproposer/libwalproposer.a 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 2c0eb87e356fc941aff4775f78639e77002ccd89..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1498 zcmbVL&1(};5T8wKDzQos1wp|T@gUMQ+g2%}C8eh3P(6szlO)^CCJiRLW%E+2;0FYX zAs6pnJbLu(MMeJ=p$9L5ha3WNW_QLs*XGiJ`TFh5yqWiAFWhuOcWWhk)%=(8nj71? zG);S10;*X6FpwWU@H%R}>$?C7Ei0JRoL24n(o$5$Uc9yt*Q;h<6*tyX1{I9SfR(7c z_@dvW!~!TpW&w_2WVzJ;QR@B7^>2E$=ehbn*=HXdKg`hG#U~WSl0@rVEKXatULG0^Bv{5Prz!mcG?xER>LQ0z;@X5Rh`RtTa|j~v^|hl zjknVUtD{2YY@&YFj&vB*9OV#fTQ+xhc-6pE{eGlUiih13fc0eKV}S08Fw!~|*pc^> zZ^<{L-2b-#;5{Ey;zas=sU_LLGmJk)O`)>a3`36%i4&a5MmdDdArqD-N27OapVu%l zlld1lOb`8}JDM%|0^LOev2A^~=-U?(NXxPMD`kEG=zb2mzkNPP=BN9cMV5TQY46h; i;{erb +#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