From 2c2dc2dcb8aeb2bb578025910b13c096f1ebd6e2 Mon Sep 17 00:00:00 2001 From: usamoi Date: Tue, 18 Mar 2025 18:27:26 +0800 Subject: [PATCH] tell C compiler to put code and data in separate sections --- .cargo/config.toml | 3 --- cargo-pgrx/src/templates/cargo_config_toml | 3 --- pgrx-bindgen/src/build.rs | 13 ++++++++----- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index 5ef5ccd240..51de468659 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -7,6 +7,3 @@ r = "run --features" [target.'cfg(target_os="macos")'] # Postgres symbols won't be available until runtime rustflags = ["-Clink-arg=-Wl,-undefined,dynamic_lookup"] - -[target.'cfg(target_env="msvc")'] -linker = "lld-link" diff --git a/cargo-pgrx/src/templates/cargo_config_toml b/cargo-pgrx/src/templates/cargo_config_toml index 1dd47369a8..13c456b5dd 100644 --- a/cargo-pgrx/src/templates/cargo_config_toml +++ b/cargo-pgrx/src/templates/cargo_config_toml @@ -1,6 +1,3 @@ [target.'cfg(target_os="macos")'] # Postgres symbols won't be available until runtime rustflags = ["-Clink-arg=-Wl,-undefined,dynamic_lookup"] - -[target.'cfg(target_env="msvc")'] -linker = "lld-link" diff --git a/pgrx-bindgen/src/build.rs b/pgrx-bindgen/src/build.rs index b8903975fb..34b2d1fe3f 100644 --- a/pgrx-bindgen/src/build.rs +++ b/pgrx-bindgen/src/build.rs @@ -1009,11 +1009,14 @@ fn build_shim( std::fs::copy(shim_src, shim_dst).unwrap(); let mut build = cc::Build::new(); - if let Some("msvc") = env_tracked("CARGO_CFG_TARGET_ENV").as_deref() { - // without this, pgrx_embed would be linked to postgres.lib - build.compiler("clang"); - build.archiver("llvm-lib"); - build.flag("-flto=thin"); + let compiler = build.get_compiler(); + if compiler.is_like_gnu() || compiler.is_like_clang() { + build.flag("-ffunction-sections"); + build.flag("-fdata-sections"); + } + if compiler.is_like_msvc() { + build.flag("/Gy"); + build.flag("/Gw"); } for pg_target_include in pg_target_includes(major_version, pg_config)?.iter() { build.flag(&format!("-I{pg_target_include}"));