Skip to content

Commit

Permalink
Call "cmake" via "emcmake"/"emmake" for Emscripten
Browse files Browse the repository at this point in the history
Emscripten toolchain has "emcmake" helper which sets the proper
CMAKE_TOOLCHAIN_FILE and other CMake variables that get CMake
to produce build files for cross-compiling via Emscripten.

There is also "emmake" helper that sets the environment variables
for cross-compilation. It should not be necessary for CMake when
used with Makefiles and Ninja, but Emscripten recommends it anyway.
  • Loading branch information
ilammy committed Jan 9, 2022
1 parent c7c8c9c commit a7bc012
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ impl Config {
let cmake_prefix_path = env::join_paths(&cmake_prefix_path).unwrap();

// Build up the first cmake command to build the build system.
let mut cmd = self.cmake_configure_command();
let mut cmd = self.cmake_configure_command(&target);

if self.verbose_cmake {
cmd.arg("-Wdev");
Expand Down Expand Up @@ -784,7 +784,7 @@ impl Config {
}

// And build!
let mut cmd = self.cmake_build_command();
let mut cmd = self.cmake_build_command(&target);
cmd.current_dir(&build);

for &(ref k, ref v) in c_compiler.env().iter().chain(&self.env) {
Expand Down Expand Up @@ -841,12 +841,34 @@ impl Config {
.unwrap_or(OsString::from("cmake"))
}

fn cmake_configure_command(&mut self) -> Command {
Command::new(self.cmake_executable())
// If we are building for Emscripten, wrap the calls to CMake
// as "emcmake cmake ..." and "emmake cmake --build ...".
// https://emscripten.org/docs/compiling/Building-Projects.html

fn cmake_configure_command(&mut self, target: &str) -> Command {
if target.contains("emscripten") {
let emcmake = self
.getenv_target_os("EMCMAKE")
.unwrap_or(OsString::from("emcmake"));
let mut cmd = Command::new(emcmake);
cmd.arg(self.cmake_executable());
cmd
} else {
Command::new(self.cmake_executable())
}
}

fn cmake_build_command(&mut self) -> Command {
Command::new(self.cmake_executable())
fn cmake_build_command(&mut self, target: &str) -> Command {
if target.contains("emscripten") {
let emmake = self
.getenv_target_os("EMMAKE")
.unwrap_or(OsString::from("emmake"));
let mut cmd = Command::new(emmake);
cmd.arg(self.cmake_executable());
cmd
} else {
Command::new(self.cmake_executable())
}
}

fn getenv_os(&mut self, v: &str) -> Option<OsString> {
Expand Down

0 comments on commit a7bc012

Please sign in to comment.