From 854aeab8f0cd25341fd09125b2aeeac9c080b37c Mon Sep 17 00:00:00 2001 From: Riley Lyman Date: Sun, 15 May 2022 15:29:51 -0700 Subject: [PATCH] capnpc: add option to build `capnp` exe from src --- .gitmodules | 3 +++ capnpc/Cargo.toml | 7 +++++++ capnpc/build.rs | 14 ++++++++++++++ capnpc/capnproto | 1 + capnpc/src/lib.rs | 15 ++++++++++++++- 5 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 100644 capnpc/build.rs create mode 160000 capnpc/capnproto diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 000000000..4e14ea831 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "capnpc/capnproto"] + path = capnpc/capnproto + url = https://github.com/capnproto/capnproto.git diff --git a/capnpc/Cargo.toml b/capnpc/Cargo.toml index f021d9e06..36e5d3b5f 100644 --- a/capnpc/Cargo.toml +++ b/capnpc/Cargo.toml @@ -13,6 +13,9 @@ readme = "README.md" keywords = ["encoding", "protocol", "serialization"] +[features] +build-capnp = [] + [lib] name = "capnpc" @@ -37,3 +40,7 @@ path = "../capnp" # Fortunately, this is pretty easy, and is just an internal implementation detail. default-features=false features=[] + +[build-dependencies] +cmake = "0.1" +which = "4.2" diff --git a/capnpc/build.rs b/capnpc/build.rs new file mode 100644 index 000000000..ad5dc72f6 --- /dev/null +++ b/capnpc/build.rs @@ -0,0 +1,14 @@ +#[cfg(feature = "build-capnp")] +fn main() { + cmake::build("capnproto"); +} + +#[cfg(not(feature = "build-capnp"))] +fn main() { + if !which::which("capnp").is_ok() { + panic!( + "capnp executable not found. install it with your package manager or enable the \ + \"build-capnp\" feature to build it from source" + ); + } +} diff --git a/capnpc/capnproto b/capnpc/capnproto new file mode 160000 index 000000000..b49431c48 --- /dev/null +++ b/capnpc/capnproto @@ -0,0 +1 @@ +Subproject commit b49431c48d40490ef979247d308af63345376cee diff --git a/capnpc/src/lib.rs b/capnpc/src/lib.rs index e8355e769..725a00fda 100644 --- a/capnpc/src/lib.rs +++ b/capnpc/src/lib.rs @@ -66,6 +66,19 @@ mod pointer_constants; use std::path::{Path, PathBuf}; +#[cfg(feature = "build-capnp")] +fn capnp_exe() -> PathBuf { + let mut p = PathBuf::from(env!("OUT_DIR")); + p.push("bin"); + p.push("capnp"); + p +} + +#[cfg(not(feature = "build-capnp"))] +fn capnp_exe() -> PathBuf { + PathBuf::from("capnp") +} + // Copied from capnp/src/lib.rs, where this conversion lives behind the "std" feature flag, // which we don't want to depend on here. pub(crate) fn convert_io_err(err: std::io::Error) -> capnp::Error { @@ -218,7 +231,7 @@ impl CompilerCommand { let mut command = if let Some(executable) = &self.executable_path { ::std::process::Command::new(executable) } else { - ::std::process::Command::new("capnp") + ::std::process::Command::new(&capnp_exe()) }; command.arg("compile").arg("-o").arg("-");