Skip to content

Commit

Permalink
Build MSVC without requiring make/nmake
Browse files Browse the repository at this point in the history
  • Loading branch information
lfittl committed Jan 7, 2024
1 parent 022fbd5 commit 30ec6b7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ bindgen = "0.66.1"
clippy = { version = "0.0.302", optional = true }
prost-build = "0.10.4"
fs_extra = "1.2.0"
cc = "1.0.83"
glob = "0.3.1"

[dev-dependencies]
easy-parallel = "3.2.0"
Expand Down
33 changes: 19 additions & 14 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ use fs_extra::dir::CopyOptions;
use std::env;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use glob::glob;

static SOURCE_DIRECTORY: &str = "libpg_query";
static LIBRARY_NAME: &str = "pg_query";

fn main() -> Result<(), Box<dyn std::error::Error>> {
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
let build_path = Path::new(".").join(SOURCE_DIRECTORY);
let makefile_path = build_path.join("Makefile");
let out_header_path = out_dir.join(LIBRARY_NAME).with_extension("h");
let out_protobuf_path = out_dir.join("protobuf");
let target = env::var("TARGET").unwrap();

// Configure cargo through stdout
println!("cargo:rerun-if-changed={}", makefile_path.display()); // Includes version number
println!("cargo:rustc-link-search=native={}", out_dir.display());
println!("cargo:rustc-link-lib=static={LIBRARY_NAME}");

Expand All @@ -36,24 +35,30 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
fs_extra::copy_items(&source_paths, &out_dir, &copy_options)?;

// Compile the C library.
let mut make = Command::new("make");

if target.contains("msvc") {
make = Command::new("nmake");
make.arg("/F Makefile.msvc");
make.current_dir(&out_dir);
cc::Build::new()
.files(glob(out_dir.join("src/*.c").to_str().unwrap()).unwrap().map(|p| p.unwrap()))
.files(glob(out_dir.join("src/postgres/*.c").to_str().unwrap()).unwrap().map(|p| p.unwrap()))
.file(out_dir.join("vendor/protobuf-c/protobuf-c.c"))
.file(out_dir.join("vendor/xxhash/xxhash.c"))
.file(out_dir.join("protobuf/pg_query.pb-c.c"))
.include(out_dir.join("."))
.include(out_dir.join("./vendor"))
.include(out_dir.join("./src/postgres/include"))
.include(out_dir.join("./src/include"))
.include(out_dir.join("./src/postgres/include/port/win32"))
.include(out_dir.join("./src/postgres/include/port/win32_msvc"))
.compile(LIBRARY_NAME);
} else {
let mut make = Command::new("make");
make.env_remove("PROFILE").arg("-C").arg(&out_dir).arg("build");

if env::var("PROFILE").unwrap() == "debug" {
make.arg("DEBUG=1");
}
}

let status = make.stdin(Stdio::null()).stdout(Stdio::inherit()).stderr(Stdio::inherit()).status()?;

if !status.success() {
return Err("libpg_query compilation failed".into());
let status = make.stdin(Stdio::null()).stdout(Stdio::inherit()).stderr(Stdio::inherit()).status()?;
if !status.success() {
return Err("libpg_query compilation failed".into());
}
}

// Generate bindings for Rust
Expand Down

0 comments on commit 30ec6b7

Please sign in to comment.