Skip to content

Commit e39742d

Browse files
Forward include_dirs to -I or /I
Closes #80
1 parent ac8be53 commit e39742d

File tree

4 files changed

+63
-42
lines changed

4 files changed

+63
-42
lines changed

src/lib.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ pub fn compile<T: AsRef<Path>,
345345
P: Into<ArgumentBundle<Ms, Mi, Is, Ii>>>(
346346
resource_file: T, parameters: P)
347347
-> CompilationResult {
348-
let (prefix, out_dir, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), parameters.into().macros));
348+
let (prefix, out_dir, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), parameters.into()));
349349
let hasbins = fs::read_to_string("Cargo.toml")
350350
.unwrap_or_else(|err| {
351351
eprintln!("Couldn't read Cargo.toml: {}; assuming src/main.rs or S_ISDIR(src/bin/)", err);
@@ -395,7 +395,7 @@ pub fn compile_for<T: AsRef<Path>,
395395
P: Into<ArgumentBundle<Ms, Mi, Is, Ii>>>(
396396
resource_file: T, for_bins: I, parameters: P)
397397
-> CompilationResult {
398-
let (_, _, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), parameters.into().macros));
398+
let (_, _, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), parameters.into()));
399399
for bin in for_bins {
400400
println!("cargo:rustc-link-arg-bin={}={}", bin, out_file);
401401
}
@@ -414,7 +414,7 @@ pub fn compile_for_tests<T: AsRef<Path>,
414414
P: Into<ArgumentBundle<Ms, Mi, Is, Ii>>>(
415415
resource_file: T, parameters: P)
416416
-> CompilationResult {
417-
let (_, _, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), parameters.into().macros));
417+
let (_, _, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), parameters.into()));
418418
println!("cargo:rustc-link-arg-tests={}", out_file);
419419
CompilationResult::Ok
420420
}
@@ -430,7 +430,7 @@ pub fn compile_for_benchmarks<T: AsRef<Path>,
430430
P: Into<ArgumentBundle<Ms, Mi, Is, Ii>>>(
431431
resource_file: T, parameters: P)
432432
-> CompilationResult {
433-
let (_, _, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), parameters.into().macros));
433+
let (_, _, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), parameters.into()));
434434
println!("cargo:rustc-link-arg-benches={}", out_file);
435435
CompilationResult::Ok
436436
}
@@ -446,7 +446,7 @@ pub fn compile_for_examples<T: AsRef<Path>,
446446
P: Into<ArgumentBundle<Ms, Mi, Is, Ii>>>(
447447
resource_file: T, parameters: P)
448448
-> CompilationResult {
449-
let (_, _, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), parameters.into().macros));
449+
let (_, _, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), parameters.into()));
450450
println!("cargo:rustc-link-arg-examples={}", out_file);
451451
CompilationResult::Ok
452452
}
@@ -463,7 +463,7 @@ pub fn compile_for_everything<T: AsRef<Path>,
463463
P: Into<ArgumentBundle<Ms, Mi, Is, Ii>>>(
464464
resource_file: T, parameters: P)
465465
-> CompilationResult {
466-
let (_, _, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), parameters.into().macros));
466+
let (_, _, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), parameters.into()));
467467
println!("cargo:rustc-link-arg={}", out_file);
468468
CompilationResult::Ok
469469
}
@@ -482,18 +482,24 @@ fn compile_impl<Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>, Is: AsRef<OsStr>,
482482
let prefix = &resource_file.file_stem().expect("resource_file has no stem").to_str().expect("resource_file's stem not UTF-8");
483483
let out_dir = env::var("OUT_DIR").expect("No OUT_DIR env var");
484484

485-
let out_file = comp.compile_resource(&out_dir,
486-
&prefix,
487-
resource_file.to_str().expect("resource_file not UTF-8"),
488-
parameters.into().macros)
485+
let out_file = comp.compile_resource(&out_dir, &prefix, resource_file.to_str().expect("resource_file not UTF-8"), parameters.into())
489486
.map_err(CompilationResult::Failed)?;
490487
Ok((prefix, out_dir, out_file))
491488
}
492489
}
493490

494-
fn apply_macros<'t, Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>>(to: &'t mut Command, pref: &str, macros: Mi) -> &'t mut Command {
495-
for m in macros {
496-
to.arg(pref).arg(m);
491+
fn apply_parameters<'t, Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>, Is: AsRef<OsStr>, Ii: IntoIterator<Item = Is>>(to: &'t mut Command, macro_pref: &str,
492+
include_dir_pref: &str,
493+
parameters: ArgumentBundle<Ms,
494+
Mi,
495+
Is,
496+
Ii>)
497+
-> &'t mut Command {
498+
for m in parameters.macros {
499+
to.arg(macro_pref).arg(m);
500+
}
501+
for id in parameters.include_dirs {
502+
to.arg(include_dir_pref).arg(id);
497503
}
498504
to
499505
}

src/non_windows.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use self::super::{ArgumentBundle, apply_parameters};
12
use std::process::{Command, Stdio};
23
use std::path::{PathBuf, Path};
3-
use self::super::apply_macros;
44
use std::{env, fs, mem};
55
use std::borrow::Cow;
66
use std::ffi::OsStr;
@@ -28,9 +28,10 @@ impl ResourceCompiler {
2828
}
2929
}
3030

31-
pub fn compile_resource<Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>>(&self, out_dir: &str, prefix: &str, resource: &str, macros: Mi)
32-
-> Result<String, Cow<'static, str>> {
33-
self.compiler.as_ref().expect("Not supported but we got to compile_resource()?").compile(out_dir, prefix, resource, macros)
31+
pub fn compile_resource<Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>, Is: AsRef<OsStr>, Ii: IntoIterator<Item = Is>>(
32+
&self, out_dir: &str, prefix: &str, resource: &str, parameters: ArgumentBundle<Ms, Mi, Is, Ii>)
33+
-> Result<String, Cow<'static, str>> {
34+
self.compiler.as_ref().expect("Not supported but we got to compile_resource()?").compile(out_dir, prefix, resource, parameters)
3435
}
3536
}
3637

@@ -92,14 +93,16 @@ impl Compiler {
9293
Err("".into())
9394
}
9495

95-
pub fn compile<Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>>(&self, out_dir: &str, prefix: &str, resource: &str, macros: Mi)
96-
-> Result<String, Cow<'static, str>> {
96+
pub fn compile<Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>, Is: AsRef<OsStr>, Ii: IntoIterator<Item = Is>>(&self, out_dir: &str, prefix: &str,
97+
resource: &str,
98+
parameters: ArgumentBundle<Ms, Mi, Is, Ii>)
99+
-> Result<String, Cow<'static, str>> {
97100
let out_file = format!("{}/{}.lib", out_dir, prefix);
98101
match self.tp {
99102
CompilerType::LlvmRc { has_no_preprocess } => {
100103
let preprocessed_path = format!("{}/{}-preprocessed.rc", out_dir, prefix);
101104
fs::write(&preprocessed_path,
102-
cc_xc(apply_macros_cc(cc::Build::new().define("RC_INVOKED", None), macros))
105+
cc_xc(apply_parameters_cc(cc::Build::new().define("RC_INVOKED", None), parameters))
103106
.file(resource)
104107
.cargo_metadata(false)
105108
.include(out_dir)
@@ -125,10 +128,11 @@ impl Compiler {
125128
&out_file)?;
126129
}
127130
CompilerType::WindRes => {
128-
try_command(apply_macros(Command::new(&self.executable[..])
129-
.args(&["--input", resource, "--output-format=coff", "--output", &out_file, "--include-dir", out_dir]),
130-
"-D",
131-
macros),
131+
try_command(apply_parameters(Command::new(&self.executable[..])
132+
.args(&["--input", resource, "--output-format=coff", "--output", &out_file, "--include-dir", out_dir]),
133+
"-D",
134+
"-I",
135+
parameters),
132136
Path::new(&self.executable[..]),
133137
"compile",
134138
resource,
@@ -139,11 +143,19 @@ impl Compiler {
139143
}
140144
}
141145

142-
fn apply_macros_cc<'t, Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>>(to: &'t mut cc::Build, macros: Mi) -> &'t mut cc::Build {
143-
for m in macros {
146+
fn apply_parameters_cc<'t, Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>, Is: AsRef<OsStr>, Ii: IntoIterator<Item = Is>>(to: &'t mut cc::Build,
147+
parameters: ArgumentBundle<Ms,
148+
Mi,
149+
Is,
150+
Ii>)
151+
-> &'t mut cc::Build {
152+
for m in parameters.macros {
144153
let mut m = m.as_ref().to_str().expect("macros must be UTF-8 in this configuration").splitn(2, '=');
145154
to.define(m.next().unwrap(), m.next());
146155
}
156+
for id in parameters.include_dirs {
157+
to.include(id);
158+
}
147159
to
148160
}
149161

src/windows_msvc.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use self::super::{ArgumentBundle, apply_parameters};
12
use std::path::{PathBuf, Path, MAIN_SEPARATOR};
23
use std::sync::atomic::Ordering::SeqCst;
34
use std::sync::atomic::AtomicBool;
4-
use self::super::apply_macros;
55
use std::process::Command;
66
use vswhom::VsFindResult;
77
use std::borrow::Cow;
@@ -26,14 +26,16 @@ impl ResourceCompiler {
2626
None
2727
}
2828

29-
pub fn compile_resource<Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>>(&self, out_dir: &str, prefix: &str, resource: &str, macros: Mi)
30-
-> Result<String, Cow<'static, str>> {
29+
pub fn compile_resource<Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>, Is: AsRef<OsStr>, Ii: IntoIterator<Item = Is>>(
30+
&self, out_dir: &str, prefix: &str, resource: &str, parameters: ArgumentBundle<Ms, Mi, Is, Ii>)
31+
-> Result<String, Cow<'static, str>> {
3132
let out_file = format!("{}{}{}.lib", out_dir, MAIN_SEPARATOR, prefix);
3233
// `.res`es are linkable under MSVC as well as normal libraries.
33-
if !apply_macros(Command::new(find_windows_sdk_tool_impl("rc.exe").as_ref().map_or(Path::new("rc.exe"), Path::new))
34-
.args(&["/fo", &out_file, "/I", out_dir]),
35-
"/D",
36-
macros)
34+
if !apply_parameters(Command::new(find_windows_sdk_tool_impl("rc.exe").as_ref().map_or(Path::new("rc.exe"), Path::new))
35+
.args(&["/fo", &out_file, "/I", out_dir]),
36+
"/D",
37+
"/I",
38+
parameters)
3739
.arg(resource)
3840
.status()
3941
.map_err(|_| Cow::from("Are you sure you have RC.EXE in your $PATH?"))?

src/windows_not_msvc.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use self::super::{ArgumentBundle, apply_parameters};
12
use std::path::{PathBuf, MAIN_SEPARATOR};
2-
use self::super::apply_macros;
33
use std::process::Command;
44
use std::borrow::Cow;
55
use std::ffi::OsStr;
@@ -21,8 +21,9 @@ impl ResourceCompiler {
2121
None
2222
}
2323

24-
pub fn compile_resource<Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>>(&self, out_dir: &str, prefix: &str, resource: &str, macros: Mi)
25-
-> Result<String, Cow<'static, str>> {
24+
pub fn compile_resource<Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>, Is: AsRef<OsStr>, Ii: IntoIterator<Item = Is>>(
25+
&self, out_dir: &str, prefix: &str, resource: &str, parameters: ArgumentBundle<Ms, Mi, Is, Ii>)
26+
-> Result<String, Cow<'static, str>> {
2627
let out_file = format!("{}{}lib{}.a", out_dir, MAIN_SEPARATOR, prefix);
2728

2829
// Under some msys2 environments, $MINGW_CHOST has the correct target for
@@ -38,12 +39,12 @@ impl ResourceCompiler {
3839
.into()
3940
});
4041

41-
match apply_macros(Command::new("windres")
42-
.args(&["--input", resource, "--output-format=coff", "--target"])
43-
.arg(target)
44-
.args(&["--output", &out_file, "--include-dir", out_dir]),
45-
"-D",
46-
macros)
42+
match apply_parameters(Command::new("windres")
43+
.args(&["--input", resource, "--output-format=coff", "--target"])
44+
.arg(target)
45+
.args(&["--output", &out_file, "--include-dir", out_dir]),
46+
"-D","-I",
47+
parameters)
4748
.status() {
4849
Ok(stat) if stat.success() => Ok(out_file),
4950
Ok(stat) => Err(format!("windres failed to compile \"{}\" into \"{}\" with {}", resource, out_file, stat).into()),

0 commit comments

Comments
 (0)