build.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2019 Parity Technologies (UK) Ltd.
  2. // This file is part of Substrate.
  3. // Substrate is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. // Substrate is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with Substrate. If not, see <http://www.gnu.org/licenses/>.
  13. use std::{env, process::Command, string::String};
  14. use wasm_builder_runner::{WasmBuilder, WasmBuilderSource};
  15. fn main() {
  16. if !in_real_cargo_environment() {
  17. env::set_var("BUILD_DUMMY_WASM_BINARY", "1");
  18. println!("Building DUMMY Wasm binary");
  19. }
  20. let file_name = "wasm_binary.rs";
  21. let wasm_builder_source = WasmBuilderSource::Crates("1.0.9");
  22. // This instructs LLD to export __heap_base as a global variable, which is used by the
  23. // external memory allocator.
  24. let default_rust_flags = "-Clink-arg=--export=__heap_base";
  25. WasmBuilder::new()
  26. .with_current_project()
  27. .with_wasm_builder_source(wasm_builder_source)
  28. .append_to_rust_flags(default_rust_flags)
  29. .set_file_name(file_name)
  30. .build()
  31. }
  32. fn in_real_cargo_environment() -> bool {
  33. let cargo =
  34. env::var("CARGO").expect("`CARGO` env variable is always set when executing `build.rs`.");
  35. let mut cmd = Command::new(cargo);
  36. cmd.arg("--version");
  37. let output = cmd.output().expect("failed to get cargo version");
  38. let version = String::from_utf8(output.stdout).unwrap();
  39. println!("{}", version);
  40. // if we are building in an IDE (Code or Atom) with RLS extension the version
  41. // would start with "rls"
  42. version.starts_with("cargo")
  43. }