-
Notifications
You must be signed in to change notification settings - Fork 416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add C++ library compilation support. #31
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,7 @@ pub struct Config { | |
objects: Vec<PathBuf>, | ||
flags: Vec<String>, | ||
files: Vec<PathBuf>, | ||
cpp: bool, | ||
} | ||
|
||
fn getenv(v: &str) -> Option<String> { | ||
|
@@ -106,6 +107,7 @@ impl Config { | |
objects: Vec::new(), | ||
flags: Vec::new(), | ||
files: Vec::new(), | ||
cpp: false, | ||
} | ||
} | ||
|
||
|
@@ -139,6 +141,12 @@ impl Config { | |
self | ||
} | ||
|
||
/// Set C++ support | ||
pub fn cpp(&mut self, cpp: bool) -> &mut Config { | ||
self.cpp = cpp; | ||
self | ||
} | ||
|
||
/// Run the compiler, generating the file `output` | ||
/// | ||
/// The name `output` must begin with `lib` and end with `.a` | ||
|
@@ -151,11 +159,11 @@ impl Config { | |
let dst = PathBuf::from(getenv_unwrap("OUT_DIR")); | ||
let mut objects = Vec::new(); | ||
for file in self.files.iter() { | ||
let mut cmd = self.gcc(); | ||
let mut cmd = self.compile_cmd(); | ||
let obj = dst.join(file).with_extension("o"); | ||
fs::create_dir_all(&obj.parent().unwrap()).unwrap(); | ||
run(cmd.arg(&src.join(file)).arg("-o").arg(&obj), | ||
&gcc(&target)); | ||
&self.compiler(&target)); | ||
objects.push(obj); | ||
} | ||
|
||
|
@@ -167,19 +175,35 @@ impl Config { | |
println!("cargo:rustc-link-search=native={}", dst.display()); | ||
println!("cargo:rustc-link-lib=static={}", | ||
&output[3..output.len() - 2]); | ||
|
||
// Add specific C++ libraries, if enabled. | ||
if self.cpp { | ||
println!("cargo:rustc-link-lib=static=stdc++"); | ||
} | ||
} | ||
|
||
fn compiler(&self, target: &str) -> String { | ||
let compiler_fn: fn(&str) -> String = if self.cpp { gxx } else { gcc }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of creating a function pointer, It may also be ok to just say: if self.cpp {gxx(target)} else {gcc(target)} |
||
compiler_fn(target) | ||
} | ||
|
||
fn compile_flags(&self) -> Vec<String> { | ||
let flags_fn: fn() -> Vec<String> = if self.cpp { cxxflags } else { cflags }; | ||
flags_fn() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly to above, I think it's fine to avoid the function pointer and do: if self.cpp {cxxflags()} else {cflags()} |
||
} | ||
|
||
fn gcc(&self) -> Command { | ||
fn compile_cmd(&self) -> Command { | ||
let target = getenv_unwrap("TARGET"); | ||
let opt_level = getenv_unwrap("OPT_LEVEL"); | ||
let profile = getenv_unwrap("PROFILE"); | ||
println!("{} {}", profile, opt_level); | ||
|
||
let mut cmd = Command::new(&gcc(&target)); | ||
cmd.arg(&format!("-O{}", opt_level)); | ||
let mut cmd = Command::new(self.compiler(&target)); | ||
|
||
cmd.arg(format!("-O{}", opt_level)); | ||
cmd.arg("-c"); | ||
cmd.arg("-ffunction-sections").arg("-fdata-sections"); | ||
cmd.args(&cflags()); | ||
cmd.args(&self.compile_flags()); | ||
|
||
if target.contains("-ios") { | ||
cmd.args(&ios_flags(&target)); | ||
|
@@ -261,6 +285,18 @@ fn gcc(target: &str) -> String { | |
}) | ||
} | ||
|
||
fn gxx(target: &str) -> String { | ||
let is_android = target.find("android").is_some(); | ||
|
||
get_var("CXX").unwrap_or(if cfg!(windows) { | ||
"g++".to_string() | ||
} else if is_android { | ||
format!("{}-g++", target) | ||
} else { | ||
"c++".to_string() | ||
}) | ||
} | ||
|
||
fn ar(target: &str) -> String { | ||
let is_android = target.find("android").is_some(); | ||
|
||
|
@@ -271,13 +307,21 @@ fn ar(target: &str) -> String { | |
}) | ||
} | ||
|
||
fn cflags() -> Vec<String> { | ||
get_var("CFLAGS").unwrap_or(String::new()) | ||
fn envflags(name: &str) -> Vec<String> { | ||
get_var(name).unwrap_or(String::new()) | ||
.split(|c: char| c.is_whitespace()).filter(|s| !s.is_empty()) | ||
.map(|s| s.to_string()) | ||
.collect() | ||
} | ||
|
||
fn cflags() -> Vec<String> { | ||
envflags("CFLAGS") | ||
} | ||
|
||
fn cxxflags() -> Vec<String> { | ||
envflags("CXXFLAGS") | ||
} | ||
|
||
fn ios_flags(target: &str) -> Vec<String> { | ||
enum ArchSpec { | ||
Device(&'static str), | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems a little surprising to me, I would expect this to be dynamically linked by default. Perhaps this could be its own configuration option?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually it doesn't compile if I omit this line, so I suppose rustc doesn't link the C++ standard library by default. :p
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I'd definitely believe that, but I think it may be best done as a dynamic dependency (e.g. removing
static=
) instead of a static one. Statically linking libstdc++ is pretty rare I believe.