Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl Default for BuildMode {
pub struct Build {
files: Vec<PathBuf>,
env: HashMap<OsString, OsString>,
work_dir: Option<PathBuf>,
out_dir: Option<PathBuf>,
buildmode: BuildMode,
compiler: PathBuf,
Expand Down Expand Up @@ -166,6 +167,7 @@ impl Build {
Self {
files: Vec::new(),
env: HashMap::new(),
work_dir: None,
out_dir: None,
buildmode: BuildMode::CArchive,
compiler: PathBuf::from("go"),
Expand Down Expand Up @@ -214,6 +216,16 @@ impl Build {
self
}

/// Configures the working directory where the `go build` command is going
/// to run
pub fn work_dir<P: AsRef<Path>>(
&mut self,
work_dir: P,
) -> &mut Build {
self.work_dir = Some(work_dir.as_ref().to_owned());
self
}

/// Configures the build mode. See `go help buildmode for more details.
///
/// Build mode `c-archive` is used by default.
Expand Down Expand Up @@ -284,10 +296,14 @@ impl Build {
})?;

let mut command = process::Command::new(&self.compiler);
if let Some(work_dir) = self.work_dir.as_ref() {
let full_work_dir = env::current_dir().unwrap().join(work_dir);
command.current_dir(full_work_dir);
}
command.arg("build");
command.args(&["-buildmode", &self.buildmode.to_string()]);
command.args(&["-o", &out.display().to_string()]);
command.args(self.files.iter());
command.args(self.files.iter().map(|p| env::current_dir().unwrap().join(p)));
command.env("CGO_ENABLED", "1");
command.env("CC", ccompiler.path());

Expand Down