From 30ec6b745bfdf9680b6e190e9e932d9776582dfd Mon Sep 17 00:00:00 2001 From: Lukas Fittl Date: Sat, 6 Jan 2024 19:40:21 -0800 Subject: [PATCH] Build MSVC without requiring make/nmake --- Cargo.lock | 2 ++ Cargo.toml | 2 ++ build.rs | 33 +++++++++++++++++++-------------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fde4156..e4e5e80 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -393,9 +393,11 @@ name = "pg_query" version = "5.0.0" dependencies = [ "bindgen", + "cc", "clippy", "easy-parallel", "fs_extra", + "glob", "itertools", "pretty_assertions", "prost", diff --git a/Cargo.toml b/Cargo.toml index c73e21a..f8399ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/build.rs b/build.rs index 0acba90..3bcea93 100644 --- a/build.rs +++ b/build.rs @@ -5,6 +5,7 @@ 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"; @@ -12,13 +13,11 @@ static LIBRARY_NAME: &str = "pg_query"; fn main() -> Result<(), Box> { 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}"); @@ -36,24 +35,30 @@ fn main() -> Result<(), Box> { fs_extra::copy_items(&source_paths, &out_dir, ©_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