Skip to content

Commit 6603d7b

Browse files
committed
Allow building on windows with vcpkg
1 parent 1bab52f commit 6603d7b

5 files changed

Lines changed: 163 additions & 150 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ Cargo.lock
1010
/target/
1111

1212
# Editor and IDE environments
13-
.vscode/
13+
.vscode/
14+
/target-wsl
15+
/vcpkg_installed

Cargo.toml

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
[package]
2-
name = "xmlsec"
3-
version = "0.2.3"
4-
authors = ["Leonhard Weber <leonhard.weber@voipir.cl>"]
5-
edition = "2021"
6-
readme = "README.md"
7-
license = "MIT"
8-
description = "Wrapper for xmlsec1 library"
9-
homepage = "https://github.com/voipir/rust-xmlsec"
10-
repository = "https://github.com/voipir/rust-xmlsec"
11-
keywords = ["xml", "xmlsec", "dsig"]
2+
name = "xmlsec"
3+
version = "0.2.3"
4+
authors = ["Leonhard Weber <leonhard.weber@voipir.cl>"]
5+
edition = "2021"
6+
readme = "README.md"
7+
license = "MIT"
8+
description = "Wrapper for xmlsec1 library"
9+
homepage = "https://github.com/voipir/rust-xmlsec"
10+
repository = "https://github.com/voipir/rust-xmlsec"
11+
keywords = ["xml", "xmlsec", "dsig"]
1212

1313
build = "bindings.rs"
1414

1515
[dependencies]
16-
libc = {version = "^0.2"}
17-
libxml = {version = "^0.3"}
18-
lazy_static = {version = "^1.4"}
16+
libc = { version = "^0.2" }
17+
libxml = { version = "^0.3" }
18+
lazy_static = { version = "^1.4" }
1919

2020
[build-dependencies]
21-
pkg-config = {version = "^0.3"}
22-
bindgen = {version = "^0.65"}
21+
pkg-config = { version = "^0.3" }
22+
bindgen = { version = "^0.71" }
23+
24+
[target.'cfg(windows)'.build-dependencies]
25+
vcpkg = { version = "0.2", git = "https://github.com/mcgoo/vcpkg-rs.git", branch = "master" }

bindings.rs

Lines changed: 63 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,94 @@
11
//!
22
//! XmlSec Bindings Generation
33
//!
4-
use bindgen::Builder as BindgenBuilder;
4+
use bindgen::Builder as BindgenBuilder;
55
use bindgen::Formatter as BindgenFormatter;
66

7-
use pkg_config::Config as PkgConfig;
8-
7+
use std::collections::HashMap;
98
use std::env;
109
use std::path::PathBuf;
11-
use std::process::Command;
12-
1310

1411
const BINDINGS: &str = "bindings.rs";
1512

13+
fn main() {
14+
let dependencies = locate_and_link_dependencies();
1615

17-
fn main()
18-
{
19-
println!("cargo:rustc-link-lib=xmlsec1-openssl"); // -lxmlsec1-openssl
20-
println!("cargo:rustc-link-lib=xmlsec1"); // -lxmlsec1
21-
println!("cargo:rustc-link-lib=xml2"); // -lxml2
22-
println!("cargo:rustc-link-lib=ssl"); // -lssl
23-
println!("cargo:rustc-link-lib=crypto"); // -lcrypto
24-
25-
let path_out = PathBuf::from(env::var("OUT_DIR").unwrap());
16+
let path_out = PathBuf::from(env::var("OUT_DIR").unwrap());
2617
let path_bindings = path_out.join(BINDINGS);
2718

28-
if !path_bindings.exists()
29-
{
30-
PkgConfig::new()
31-
.probe("xmlsec1")
32-
.expect("Could not find xmlsec1 using pkg-config");
33-
19+
if !path_bindings.exists() {
3420
let bindbuild = BindgenBuilder::default()
3521
.header("bindings.h")
36-
.clang_args(fetch_xmlsec_config_flags())
37-
.clang_args(fetch_xmlsec_config_libs())
22+
.allowlist_type("xml.*")
23+
.allowlist_function("xml.*")
24+
.allowlist_var("xml.*")
25+
.clang_args(dependencies.clang_args())
3826
.layout_tests(true)
3927
.formatter(BindgenFormatter::default())
4028
.generate_comments(true);
4129

42-
let bindings = bindbuild.generate()
43-
.expect("Unable to generate bindings");
30+
let bindings = bindbuild.generate().expect("Unable to generate bindings");
4431

45-
bindings.write_to_file(path_bindings)
32+
bindings
33+
.write_to_file(path_bindings)
4634
.expect("Couldn't write bindings!");
4735
}
4836
}
4937

50-
51-
fn fetch_xmlsec_config_flags() -> Vec<String>
52-
{
53-
let out = Command::new("xmlsec1-config")
54-
.arg("--cflags")
55-
.output()
56-
.expect("Failed to get --cflags from xmlsec1-config. Is xmlsec1 installed?")
57-
.stdout;
58-
59-
args_from_output(out)
38+
struct LocatedDependencies {
39+
include_paths: Vec<PathBuf>,
40+
defines: HashMap<String, Option<String>>,
6041
}
6142

62-
63-
fn fetch_xmlsec_config_libs() -> Vec<String>
64-
{
65-
let out = Command::new("xmlsec1-config")
66-
.arg("--libs")
67-
.output()
68-
.expect("Failed to get --libs from xmlsec1-config. Is xmlsec1 installed?")
69-
.stdout;
70-
71-
args_from_output(out)
43+
impl LocatedDependencies {
44+
fn clang_args(&self) -> Vec<String> {
45+
let mut result = Vec::new();
46+
for include_path in &self.include_paths {
47+
result.push(format!("-I{}", include_path.display()));
48+
}
49+
for (define, value) in &self.defines {
50+
match value {
51+
Some(value) => result.push(format!("-D{}={}", define, value)),
52+
None => result.push(format!("-D{}", define)),
53+
}
54+
}
55+
result
56+
}
7257
}
7358

59+
#[cfg(not(windows))]
60+
fn locate_and_link_dependencies() -> LocatedDependencies {
61+
let library =
62+
pkg_config::probe_library("xmlsec1").expect("Could not find xmlsec1 using pkg-config");
7463

75-
fn args_from_output(args: Vec<u8>) -> Vec<String>
76-
{
77-
let decoded = String::from_utf8(args)
78-
.expect("Got invalid UTF8 from xmlsec1-config");
79-
80-
let args = decoded.split_whitespace()
81-
.map(|p| p.to_owned())
82-
.collect::<Vec<String>>();
64+
LocatedDependencies {
65+
include_paths: library.include_paths,
66+
defines: library.defines,
67+
}
68+
}
8369

84-
args
70+
#[cfg(windows)]
71+
fn locate_and_link_dependencies() -> LocatedDependencies {
72+
let library =
73+
vcpkg::find_package("xmlsec").expect("Failed to find xmlsec using vcpkg. Is it installed?");
74+
75+
println!("cargo:rustc-link-lib=crypt32");
76+
println!("cargo:rustc-link-lib=user32");
77+
println!("cargo:rustc-link-lib=bcrypt");
78+
79+
// vcpkg does not provide the defines, so we have to provide them ourselves
80+
// -DXMLSEC_DL_LIBLTDL=1 -DXMLSEC_CRYPTO_OPENSSL=1
81+
let mut defines = HashMap::new();
82+
defines.insert("__XMLSEC_FUNCTION__".into(), Some("__func__".into()));
83+
defines.insert("XMLSEC_NO_SIZE_T".into(), None);
84+
defines.insert("XMLSEC_DL_LIBLTDL".into(), Some("1".into()));
85+
defines.insert("XMLSEC_CRYPTO_OPENSSL".into(), Some("1".into()));
86+
defines.insert("XMLSEC_NO_CRYPTO_DYNAMIC_LOADING".into(), Some("1".into()));
87+
defines.insert("XMLSEC_NO_GOST".into(), Some("1".into()));
88+
defines.insert("XMLSEC_NO_GOST2012".into(), Some("1".into()));
89+
90+
LocatedDependencies {
91+
include_paths: library.include_paths,
92+
defines,
93+
}
8594
}

0 commit comments

Comments
 (0)