Skip to content

Commit 7b98dda

Browse files
committed
Make the build process more friendly when called outside of build scripts
- Automatically set host - Set debug - Set optimization level (based of debug)
1 parent 79d379a commit 7b98dda

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ members = ["testcrate"]
1919

2020
[dependencies]
2121
cc = "1.2"
22+
23+
[dev-dependencies]
24+
target-lexicon = "0.13"
25+
tempfile = "3.0"

src/lib.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct Build {
1919
debug: Option<bool>,
2020
}
2121

22+
#[derive(Clone, Debug)]
2223
pub struct Artifacts {
2324
include_dir: PathBuf,
2425
lib_dir: PathBuf,
@@ -67,7 +68,7 @@ impl Build {
6768
self
6869
}
6970

70-
pub fn build(&mut self, version: Version) -> Artifacts {
71+
pub fn build(&self, version: Version) -> Artifacts {
7172
let target = &self.target.as_ref().expect("TARGET is not set")[..];
7273
let out_dir = self.out_dir.as_ref().expect("OUT_DIR is not set");
7374
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
@@ -79,7 +80,19 @@ impl Build {
7980
}
8081

8182
let mut config = cc::Build::new();
82-
config.warnings(false).cargo_metadata(false);
83+
config.warnings(false).cargo_metadata(false).target(target);
84+
85+
match &self.host {
86+
Some(host) => {
87+
config.host(host);
88+
}
89+
// Host will be taken from the environment variable
90+
None if env::var("HOST").is_ok() => {}
91+
None => {
92+
// If called outside of build script, set default host
93+
config.host(target);
94+
}
95+
}
8396

8497
match target {
8598
_ if target.contains("linux") => {
@@ -141,17 +154,22 @@ impl Build {
141154
config.define("LUA_UCID", None);
142155
}
143156

144-
if self.debug.unwrap_or(cfg!(debug_assertions)) {
157+
let debug = self.debug.unwrap_or(cfg!(debug_assertions));
158+
if debug {
145159
config.define("LUA_USE_APICHECK", None);
146160
config.debug(true);
147161
}
148162

149-
if let Some(host) = &self.host {
150-
config.host(host);
151-
}
152-
153-
if let Some(opt_level) = &self.opt_level {
154-
config.opt_level_str(opt_level);
163+
match &self.opt_level {
164+
Some(opt_level) => {
165+
config.opt_level_str(opt_level);
166+
}
167+
// Opt level will be taken from the environment variable
168+
None if env::var("OPT_LEVEL").is_ok() => {}
169+
None => {
170+
// If called outside of build script, set default opt level
171+
config.opt_level(if debug { 0 } else { 2 });
172+
}
155173
}
156174

157175
config

0 commit comments

Comments
 (0)