Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use cc to generate cshim #1854

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pgrx-bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ bindgen = { version = "0.70.1", default-features = false, features = ["experimen
clang-sys = { version = "1", features = ["clang_6_0", "runtime"] }
quote = "1.0.33"
shlex = "1.3" # shell lexing, also used by many of our deps
cc = "1.0"
usamoi marked this conversation as resolved.
Show resolved Hide resolved
73 changes: 15 additions & 58 deletions pgrx-bindgen/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bindgen::callbacks::{DeriveTrait, EnumVariantValue, ImplementsTrait, MacroPa
use bindgen::NonCopyUnionStyle;
use eyre::{eyre, WrapErr};
use pgrx_pg_config::{
is_supported_major_version, prefix_path, PgConfig, PgConfigSelector, Pgrx, SUPPORTED_VERSIONS,
is_supported_major_version, PgConfig, PgConfigSelector, Pgrx, SUPPORTED_VERSIONS,
};
use quote::{quote, ToTokens};
use std::cell::RefCell;
Expand Down Expand Up @@ -284,7 +284,7 @@ fn emit_rerun_if_changed() {
println!("cargo:rerun-if-env-changed=PGRX_PG_SYS_GENERATE_BINDINGS_FOR_RELEASE");

println!("cargo:rerun-if-changed=include");
println!("cargo:rerun-if-changed=cshim");
println!("cargo:rerun-if-changed=pgrx-cshim.c");

if let Ok(pgrx_config) = Pgrx::config_toml() {
println!("cargo:rerun-if-changed={}", pgrx_config.display());
Expand Down Expand Up @@ -356,9 +356,9 @@ struct BuildPaths {
out_dir: PathBuf,
/// {manifest_dir}/src
src_dir: PathBuf,
/// {manifest_dir}/cshim
/// {manifest_dir}/pgrx-cshim.c
shim_src: PathBuf,
/// {out_dir}/cshim
/// {out_dir}/pgrx-cshim.c
shim_dst: PathBuf,
}

Expand All @@ -369,8 +369,8 @@ impl BuildPaths {
let out_dir = env_tracked("OUT_DIR").map(PathBuf::from).unwrap();
Self {
src_dir: manifest_dir.join("src/include"),
shim_src: manifest_dir.join("cshim"),
shim_dst: out_dir.join("cshim"),
shim_src: manifest_dir.join("pgrx-cshim.c"),
shim_dst: out_dir.join("pgrx-cshim.c"),
out_dir,
manifest_dir,
}
Expand Down Expand Up @@ -793,7 +793,7 @@ fn run_bindgen(
.layout_tests(false)
.default_non_copy_union_style(NonCopyUnionStyle::ManuallyDrop)
.wrap_static_fns(enable_cshim)
.wrap_static_fns_path(out_path.join("wrap_static_fns"))
.wrap_static_fns_path(out_path.join("pgrx-cshim-static"))
.generate()
.wrap_err_with(|| format!("Unable to generate bindings for pg{major_version}"))?;
let mut binding_str = bindings.to_string();
Expand Down Expand Up @@ -949,60 +949,17 @@ fn build_shim(
) -> eyre::Result<()> {
let major_version = pg_config.major_version()?;

// then build the shim for the version feature currently being built
build_shim_for_version(shim_src, shim_dst, pg_config)?;
std::fs::copy(shim_src, shim_dst).unwrap();

// no matter what, tell rustc to link to the library that was built for the feature we're currently building
let envvar_name = format!("CARGO_FEATURE_PG{major_version}");
if env_tracked(&envvar_name).is_some() {
println!("cargo:rustc-link-search={}", shim_dst.display());
println!("cargo:rustc-link-lib=static=pgrx-cshim-{major_version}");
let mut build = cc::Build::new();
if let Some(flag) = pg_target_include_flags(major_version, pg_config)? {
build.flag(&flag);
}

Ok(())
}

fn build_shim_for_version(
shim_src: &path::Path,
shim_dst: &path::Path,
pg_config: &PgConfig,
) -> eyre::Result<()> {
let path_env = prefix_path(pg_config.parent_path());
let major_version = pg_config.major_version()?;

eprintln!("PATH for build_shim={path_env}");
eprintln!("shim_src={}", shim_src.display());
eprintln!("shim_dst={}", shim_dst.display());

fs::create_dir_all(shim_dst).unwrap();

let makefile_dst = path::Path::join(shim_dst, "./Makefile");
if !makefile_dst.try_exists()? {
fs::copy(path::Path::join(shim_src, "./Makefile"), makefile_dst).unwrap();
}

let cshim_dst = path::Path::join(shim_dst, "./pgrx-cshim.c");
if !cshim_dst.try_exists()? {
fs::copy(path::Path::join(shim_src, "./pgrx-cshim.c"), cshim_dst).unwrap();
}

let make = env_tracked("MAKE").unwrap_or_else(|| "make".to_string());
let rc = run_command(
Command::new(make)
.arg("clean")
.arg(&format!("libpgrx-cshim-{major_version}.a"))
.env("PG_TARGET_VERSION", format!("{major_version}"))
.env("PATH", path_env)
.env_remove("TARGET")
.env_remove("HOST")
.current_dir(shim_dst),
&format!("shim for PG v{major_version}"),
)?;

if rc.status.code().unwrap() != 0 {
return Err(eyre!("failed to make pgrx-cshim for v{major_version}"));
for flag in extra_bindgen_clang_args(pg_config)? {
build.flag(&flag);
}

build.file(shim_dst);
build.compile("pgrx-cshim");
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion pgrx-pg-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ repository = "https://github.com/pgcentralfoundation/pgrx/"
documentation = "https://docs.rs/pgrx-pg-sys"
readme = "README.md"
edition = "2021"
include = ["src/**/*", "README.md", "include/*", "cshim/*", "bindgen.rs"]
include = ["src/**/*", "README.md", "include/*", "pgrx-cshim.c", "bindgen.rs"]
build = "bindgen.rs"

[features]
Expand Down
26 changes: 0 additions & 26 deletions pgrx-pg-sys/cshim/Makefile

This file was deleted.

17 changes: 1 addition & 16 deletions pgrx-pg-sys/cshim/pgrx-cshim.c → pgrx-pg-sys/pgrx-cshim.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,7 @@
//LICENSE
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.


#include "postgres.h"

#define IS_PG_12 (PG_VERSION_NUM >= 120000 && PG_VERSION_NUM < 130000)
#define IS_PG_13 (PG_VERSION_NUM >= 130000 && PG_VERSION_NUM < 140000)

#include "access/tableam.h"
#include "executor/executor.h"
#include "executor/tuptable.h"
#include "nodes/pathnodes.h"
#include "nodes/pg_list.h"
#include "parser/parsetree.h"
#include "storage/spin.h"
#include "storage/bufpage.h"
#include "pgrx-cshim-static.c"

PGDLLEXPORT void *pgrx_list_nth(List *list, int nth);
void *pgrx_list_nth(List *list, int nth) {
Expand Down Expand Up @@ -62,5 +49,3 @@ PGDLLEXPORT bool pgrx_SpinLockFree(slock_t *lock);
bool pgrx_SpinLockFree(slock_t *lock) {
return SpinLockFree(lock);
}

#include "../wrap_static_fns.c"
Loading