Skip to content

Commit 2b6b598

Browse files
committed
use std::env::consts, instead of cfg and hardcored constants
1 parent bd1ece2 commit 2b6b598

3 files changed

Lines changed: 18 additions & 54 deletions

File tree

cargo-pgrx/src/command/install.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,12 @@ pub(crate) fn install_extension(
184184
};
185185
// Since Postgres 16, the shared library extension on macOS is `dylib`, not `so`.
186186
// Ref https://github.com/postgres/postgres/commit/b55f62abb2c2e07dfae99e19a2b3d7ca9e58dc1a
187-
let so_ext = 'e: {
188-
if cfg!(target_os = "macos") {
189-
break 'e if pg_config.major_version().unwrap() >= 16 { "dylib" } else { "so" };
190-
}
191-
if cfg!(target_os = "windows") {
192-
break 'e "dll";
193-
}
194-
"so"
187+
let so_suffix = if cfg!(target_os = "macos") && pg_config.major_version().unwrap() < 16 {
188+
".so"
189+
} else {
190+
std::env::consts::DLL_SUFFIX
195191
};
196-
let filename = format!("{so_name}.{so_ext}");
192+
let filename = format!("{so_name}{so_suffix}");
197193

198194
let dest = pkglibdir.join(filename);
199195

@@ -408,18 +404,11 @@ pub(crate) fn find_library_file(
408404
manifest: &Manifest,
409405
build_command_messages: &Vec<CargoMessage>,
410406
) -> eyre::Result<PathBuf> {
407+
use std::env::consts::{DLL_EXTENSION, DLL_SUFFIX};
408+
411409
// cargo sometimes decides to change whether targets are kebab-case or snake_case in metadata,
412410
// so normalize away the difference
413411
let target_name = manifest.target_name()?.replace('-', "_");
414-
let so_ext = 'so_ext: {
415-
if cfg!(target_os = "macos") {
416-
break 'so_ext "dylib";
417-
}
418-
if cfg!(target_os = "windows") {
419-
break 'so_ext "dll";
420-
}
421-
"so"
422-
};
423412

424413
// no hard and fast rule for the lib.so output filename exists, so we implement this routine
425414
// which is essentially a cope for cargo's disinterest in writing down any docs so far.
@@ -437,11 +426,11 @@ pub(crate) fn find_library_file(
437426
artifact
438427
.filenames
439428
.iter()
440-
.find(|filename| filename.extension() == Some(so_ext))
429+
.find(|filename| filename.extension() == Some(DLL_EXTENSION))
441430
.map(|filename| filename.to_string())
442431
})
443432
.ok_or_else(|| {
444-
eyre!("Could not get shared object file `{target_name}.{so_ext}` from Cargo output.")
433+
eyre!("Could not get shared object file `{target_name}{DLL_SUFFIX}` from Cargo output.")
445434
})?;
446435
let library_file_path = PathBuf::from(library_file);
447436

pgrx-pg-config/src/cargo.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,9 @@ impl PgrxManifestExt for Manifest {
8888
}
8989

9090
fn lib_filename(&self) -> eyre::Result<String> {
91+
use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
9192
let lib_name = &self.lib_name()?;
92-
let (prefix, extension) = 'pe: {
93-
if cfg!(target_os = "macos") {
94-
break 'pe ("lib", "dylib");
95-
}
96-
if cfg!(target_os = "windows") {
97-
break 'pe ("", "dll");
98-
}
99-
("lib", "so")
100-
};
101-
Ok(format!("{prefix}{}.{}", lib_name.replace('-', "_"), extension))
93+
Ok(format!("{DLL_PREFIX}{}{DLL_SUFFIX}", lib_name.replace('-', "_")))
10294
}
10395
}
10496

pgrx-pg-config/src/lib.rs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use eyre::{eyre, WrapErr};
1212
use owo_colors::OwoColorize;
1313
use serde::{Deserialize, Serialize};
1414
use std::collections::{BTreeMap, HashMap};
15+
use std::env::consts::EXE_SUFFIX;
1516
use std::ffi::OsString;
1617
use std::fmt::{self, Debug, Display, Formatter};
1718
use std::io::ErrorKind;
@@ -334,55 +335,37 @@ impl PgConfig {
334335

335336
pub fn postmaster_path(&self) -> eyre::Result<PathBuf> {
336337
let mut path = self.bin_dir()?;
337-
#[cfg(not(target_os = "windows"))]
338-
path.push("postgres");
339-
#[cfg(target_os = "windows")]
340-
path.push("postgres.exe");
338+
path.push(format!("postgres{EXE_SUFFIX}"));
341339
Ok(path)
342340
}
343341

344342
pub fn initdb_path(&self) -> eyre::Result<PathBuf> {
345343
let mut path = self.bin_dir()?;
346-
#[cfg(not(target_os = "windows"))]
347-
path.push("initdb");
348-
#[cfg(target_os = "windows")]
349-
path.push("initdb.exe");
344+
path.push(format!("initdb{EXE_SUFFIX}"));
350345
Ok(path)
351346
}
352347

353348
pub fn createdb_path(&self) -> eyre::Result<PathBuf> {
354349
let mut path = self.bin_dir()?;
355-
#[cfg(not(target_os = "windows"))]
356-
path.push("createdb");
357-
#[cfg(target_os = "windows")]
358-
path.push("createdb.exe");
350+
path.push(format!("createdb{EXE_SUFFIX}"));
359351
Ok(path)
360352
}
361353

362354
pub fn dropdb_path(&self) -> eyre::Result<PathBuf> {
363355
let mut path = self.bin_dir()?;
364-
#[cfg(not(target_os = "windows"))]
365-
path.push("dropdb");
366-
#[cfg(target_os = "windows")]
367-
path.push("dropdb.exe");
356+
path.push(format!("dropdb{EXE_SUFFIX}"));
368357
Ok(path)
369358
}
370359

371360
pub fn pg_ctl_path(&self) -> eyre::Result<PathBuf> {
372361
let mut path = self.bin_dir()?;
373-
#[cfg(not(target_os = "windows"))]
374-
path.push("pg_ctl");
375-
#[cfg(target_os = "windows")]
376-
path.push("pg_ctl.exe");
362+
path.push(format!("pg_ctl{EXE_SUFFIX}"));
377363
Ok(path)
378364
}
379365

380366
pub fn psql_path(&self) -> eyre::Result<PathBuf> {
381367
let mut path = self.bin_dir()?;
382-
#[cfg(not(target_os = "windows"))]
383-
path.push("psql");
384-
#[cfg(target_os = "windows")]
385-
path.push("psql.exe");
368+
path.push(format!("psql{EXE_SUFFIX}"));
386369
Ok(path)
387370
}
388371

0 commit comments

Comments
 (0)