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

Enable cross compiling extensions #1994

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 12 additions & 7 deletions cargo-pgrx/src/command/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ pub(crate) struct Init {
base_testing_port: Option<u16>,
#[clap(long, help = "Additional flags to pass to the configure script")]
configure_flag: Vec<String>,
/// Do not attempt to run any compiled postgresql binaries. Useful for cross compiling.
#[clap(long)]
no_run: bool,
/// Compile PostgreSQL with the necessary flags to detect a good amount of
/// memory errors when run under Valgrind.
///
Expand Down Expand Up @@ -219,13 +222,15 @@ pub(crate) fn init_pgrx(pgrx: &Pgrx, init: &Init) -> eyre::Result<()> {
for pg_config in output_configs.iter() {
validate_pg_config(pg_config)?;

if is_root_user() {
println!("{} initdb as current user is root user", " Skipping".bold().green());
} else {
let datadir = pg_config.data_dir()?;
let bindir = pg_config.bin_dir()?;
if !datadir.try_exists()? {
initdb(&bindir, &datadir)?;
if !init.no_run {
if is_root_user() {
println!("{} initdb as current user is root user", " Skipping".bold().green());
} else {
let datadir = pg_config.data_dir()?;
let bindir = pg_config.bin_dir()?;
if !datadir.try_exists()? {
initdb(&bindir, &datadir)?;
}
}
}
}
Expand Down
15 changes: 14 additions & 1 deletion cargo-pgrx/src/command/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub(crate) struct Install {
sudo: bool,
#[clap(flatten)]
pub(crate) features: clap_cargo::Features,
#[clap(long)]
pub(crate) target: Option<String>,
#[clap(from_global, action = ArgAction::Count)]
pub(crate) verbose: u8,
}
Expand Down Expand Up @@ -106,6 +108,7 @@ impl CommandExecute for Install {
self.test,
None,
&self.features,
self.target.as_ref().map(|x| x.as_str()),
)?;
Ok(())
}
Expand All @@ -127,6 +130,7 @@ pub(crate) fn install_extension(
is_test: bool,
base_directory: Option<PathBuf>,
features: &clap_cargo::Features,
target: Option<&str>,
) -> eyre::Result<Vec<PathBuf>> {
let mut output_tracking = Vec::new();
let base_directory = base_directory.unwrap_or_else(|| PathBuf::from("/"));
Expand All @@ -139,7 +143,7 @@ pub(crate) fn install_extension(
let versioned_so = get_property(&package_manifest_path, "module_pathname")?.is_none();

let build_command_output =
build_extension(user_manifest_path.as_ref(), user_package, profile, features)?;
build_extension(user_manifest_path.as_ref(), user_package, profile, features, target)?;
let build_command_bytes = build_command_output.stdout;
let build_command_reader = BufReader::new(build_command_bytes.as_slice());
let build_command_stream = CargoMessage::parse_stream(build_command_reader);
Expand Down Expand Up @@ -223,6 +227,7 @@ pub(crate) fn install_extension(
profile,
is_test,
features,
target,
&extdir,
&base_directory,
true,
Expand Down Expand Up @@ -287,6 +292,7 @@ pub(crate) fn build_extension(
user_package: Option<&String>,
profile: &CargoProfile,
features: &clap_cargo::Features,
target: Option<&str>,
) -> eyre::Result<std::process::Output> {
let flags = std::env::var("PGRX_BUILD_FLAGS").unwrap_or_default();

Expand Down Expand Up @@ -325,6 +331,11 @@ pub(crate) fn build_extension(
command.arg(arg);
}

if let Some(target) = target {
command.arg("--target");
command.arg(target);
}

let command = command.stderr(Stdio::inherit());
let command_str = format!("{command:?}");
println!("{} extension with features {}", " Building".bold().green(), features_arg.cyan());
Expand Down Expand Up @@ -362,6 +373,7 @@ fn copy_sql_files(
profile: &CargoProfile,
is_test: bool,
features: &clap_cargo::Features,
target: Option<&str>,
extdir: &Path,
base_directory: &Path,
skip_build: bool,
Expand All @@ -378,6 +390,7 @@ fn copy_sql_files(
profile,
is_test,
features,
target,
Some(&dest),
Option::<String>::None,
None,
Expand Down
11 changes: 10 additions & 1 deletion cargo-pgrx/src/command/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub(crate) struct Package {
pub(crate) out_dir: Option<PathBuf>,
#[clap(flatten)]
pub(crate) features: clap_cargo::Features,
#[clap(long)]
pub(crate) target: Option<String>,
#[clap(from_global, action = ArgAction::Count)]
pub(crate) verbose: u8,
}
Expand Down Expand Up @@ -79,7 +81,7 @@ impl Package {
let out_dir = if let Some(out_dir) = self.out_dir {
out_dir
} else {
build_base_path(&pg_config, &package_manifest_path, &profile)?
build_base_path(&pg_config, &package_manifest_path, &profile, self.target.as_ref().map(|x| x.as_str()))?
};

let output_files = package_extension(
Expand All @@ -91,6 +93,7 @@ impl Package {
&profile,
self.test,
&self.features,
self.target.as_ref().map(|x| x.as_str()),
)?;

Ok((out_dir, output_files))
Expand Down Expand Up @@ -119,6 +122,7 @@ pub(crate) fn package_extension(
profile: &CargoProfile,
is_test: bool,
features: &clap_cargo::Features,
target: Option<&str>,
) -> eyre::Result<Vec<PathBuf>> {
let out_dir_exists = out_dir.try_exists().wrap_err_with(|| {
format!("failed to access {} while packaging extension", out_dir.display())
Expand All @@ -137,18 +141,23 @@ pub(crate) fn package_extension(
is_test,
Some(out_dir),
features,
target,
)
}

pub(crate) fn build_base_path(
pg_config: &PgConfig,
manifest_path: impl AsRef<Path>,
profile: &CargoProfile,
target: Option<&str>,
) -> eyre::Result<PathBuf> {
let mut target_dir = get_target_dir()?;
let pgver = pg_config.major_version()?;
let extname = get_property(manifest_path, "extname")?
.ok_or(eyre!("could not determine extension name"))?;
if let Some(target) = target {
target_dir.push(target);
}
target_dir.push(profile.target_subdir());
target_dir.push(format!("{extname}-pg{pgver}"));
Ok(target_dir)
Expand Down
5 changes: 5 additions & 0 deletions cargo-pgrx/src/command/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub(crate) struct Run {
profile: Option<String>,
#[clap(flatten)]
features: clap_cargo::Features,
#[clap(long)]
target: Option<String>,
#[clap(from_global, action = ArgAction::Count)]
verbose: u8,
/// Use an existing `pgcli` on the $PATH.
Expand Down Expand Up @@ -90,6 +92,7 @@ impl CommandExecute for Run {
self.pgcli,
&self.features,
self.install_only,
self.target.as_ref().map(|x| x.as_str())
)
}
}
Expand All @@ -109,6 +112,7 @@ pub(crate) fn run(
pgcli: bool,
features: &clap_cargo::Features,
install_only: bool,
target: Option<&str>,
) -> eyre::Result<()> {
// stop postgres
stop_postgres(pg_config)?;
Expand All @@ -123,6 +127,7 @@ pub(crate) fn run(
false,
None,
features,
target,
)?;

if install_only {
Expand Down
18 changes: 16 additions & 2 deletions cargo-pgrx/src/command/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub(crate) struct Schema {
/// A path to output a produced GraphViz DOT file
#[clap(long, short, value_parser)]
dot: Option<PathBuf>,
#[clap(long)]
target: Option<String>,
#[clap(from_global, action = ArgAction::Count)]
verbose: u8,
/// Skip building a fresh extension shared object.
Expand Down Expand Up @@ -101,6 +103,7 @@ impl CommandExecute for Schema {
&profile,
self.test,
&self.features,
self.target.as_ref().map(|x| x.as_str()),
self.out.as_ref(),
self.dot,
log_level,
Expand All @@ -126,6 +129,7 @@ pub(crate) fn generate_schema(
profile: &CargoProfile,
is_test: bool,
features: &clap_cargo::Features,
target: Option<&str>,
path: Option<impl AsRef<std::path::Path>>,
dot: Option<impl AsRef<std::path::Path>>,
log_level: Option<String>,
Expand Down Expand Up @@ -157,11 +161,12 @@ pub(crate) fn generate_schema(
is_test,
&features_arg,
&flags,
target,
&package_name,
)?;
};

let symbols = compute_symbols(profile, &lib_filename)?;
let symbols = compute_symbols(profile, &lib_filename, target)?;

let mut out_path = None;
if let Some(path) = path.as_ref() {
Expand Down Expand Up @@ -217,12 +222,15 @@ pub(crate) fn generate_schema(
Ok(())
}

fn compute_symbols(profile: &CargoProfile, lib_filename: &str) -> eyre::Result<Vec<String>> {
fn compute_symbols(profile: &CargoProfile, lib_filename: &str, target: Option<&str>) -> eyre::Result<Vec<String>> {
use object::Object;
use std::collections::HashSet;

// Inspect the symbol table for a list of `__pgrx_internals` we should have the generator call
let mut lib_so = get_target_dir()?;
if let Some(target) = target {
lib_so.push(target);
}
lib_so.push(profile.target_subdir());
lib_so.push(lib_filename);

Expand Down Expand Up @@ -313,6 +321,7 @@ fn first_build(
is_test: bool,
features_arg: &str,
flags: &str,
target: Option<&str>,
package_name: &str,
) -> eyre::Result<()> {
let mut command = crate::env::cargo();
Expand Down Expand Up @@ -359,6 +368,11 @@ fn first_build(
command.arg(arg);
}

if let Some(target) = target {
command.arg("--target");
command.arg(target);
}

let command_str = format!("{command:?}");
eprintln!(
"{} for SQL generation with features `{}`",
Expand Down
3 changes: 3 additions & 0 deletions cargo-pgrx/src/command/sudo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub(crate) struct SudoInstall {
pg_config: Option<PathBuf>,
out_dir: Option<PathBuf>,
features: clap_cargo::Features,
target: Option<String>,
verbose: u8,
}

Expand All @@ -32,6 +33,7 @@ impl From<Install> for SudoInstall {
pg_config: value.pg_config.map(PathBuf::from),
out_dir: None,
features: value.features,
target: value.target,
verbose: value.verbose,
}
}
Expand All @@ -49,6 +51,7 @@ impl From<SudoInstall> for Package {
out_dir: value.out_dir,
features: value.features,
verbose: value.verbose,
target: value.target,
}
}
}
Expand Down