Skip to content
Merged
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
137 changes: 94 additions & 43 deletions neptune/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
use std::{fs, path::Path};
use core::panic;
use std::{
fs,
path::{Path, PathBuf},
};

use cmake::Config;

/// copy `mega-app/` to `libs/ztm/agent/apps/mega`
fn copy_mega_apps() {
let src = Path::new("mega_app");
let dst = Path::new("libs/ztm/agent/apps/mega");
if src.exists() {
if dst.exists() {
fs::remove_dir_all(dst).expect("failed to remove origin agent/apps");
}
// std::fs::copy(src, dst).expect("failed to copy mega to agent/apps");
copy_dir_all(src, dst).expect("failed to copy mega to agent/apps");
}
}

/// use npm to build agent ui in `libs/ztm/agent/gui`
fn build_agent_ui() {
let ui = Path::new("libs/ztm/agent/gui");
if cfg!(feature = "agent-ui") {
Expand Down Expand Up @@ -36,63 +55,84 @@ fn parse_args_to_rustc(dst: &Path) {
);

// add the path to the library to the linker search path, used in build
#[cfg(any(target_os = "macos", target_os = "linux"))]
println!("cargo:rustc-link-search={}/build", dst.display());
#[cfg(target_os = "windows")] // windows's path is diffirent
{
let mut build_type = "Release";
if cfg!(debug_assertions) {
build_type = "Debug";
// copy pipyd.lib to pipy.lib
let src = dst.join("build/Debug/pipyd.lib");
let dst = dst.join("build/Debug/pipy.lib");
if src.exists() {
fs::copy(src, dst).expect("failed to copy pipyd.lib to pipy.lib");
}
}
println!(
"cargo:rustc-link-search={}/build/{}",
dst.display(),
build_type
);
}

println!("cargo:rustc-link-lib=pipy");
}

/// copy `mega-app/` to `libs/ztm/agent/apps/mega`
fn copy_mega_apps() {
let src = Path::new("mega_app");
let dst = Path::new("libs/ztm/agent/apps/mega");
if src.exists() {
if dst.exists() {
fs::remove_dir_all(dst).expect("failed to remove origin agent/apps");
/// copy finally lib to target/debug or target/release
/// for example, in macos and debug mode, it copy target/debug/build/neptune-xxxx/out/libpipy.dylib to target/debug/libpipy.dylib
fn copy_lib_to_target(dst: &Path) {
let source = {
let _source: PathBuf = dst.join("build");
if cfg!(target_os = "macos") {
_source.join("libpipy.dylib")
} else if cfg!(target_os = "linux") {
_source.join("libpipy.so")
} else if cfg!(target_os = "windows") {
if cfg!(debug_assertions) {
_source.join("pipyd.dll")
} else {
_source.join("pipy.dll")
}
} else {
panic!("unsupported target os");
}
// std::fs::copy(src, dst).expect("failed to copy mega to agent/apps");
copy_dir_all(src, dst).expect("failed to copy mega to agent/apps");
};

// !hack, only work directory is `$workspace/neptune`
let target = {
let _target = if cfg!(debug_assertions) {
Path::new("../target/debug")
} else {
Path::new("../target/release")
};
_target.join(source.file_name().unwrap())
};
if target.exists() {
fs::remove_file(&target).expect("failed to remove origin lib");
}
fs::copy(&source, &target).expect("failed to copy lib to target");
}

/// run npm install in the libs/ztm
fn run_npm_install() {
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn build() -> PathBuf {
build_agent_ui();

/* compile ztm & pipy */
// run npm install in the libs/ztm
let _ = std::process::Command::new("npm")
.current_dir("libs/ztm/pipy")
.arg("install")
.output()
.expect("failed to run npm install in ztm/pipy");

#[cfg(target_os = "windows")]
let _ = std::process::Command::new("cmd.exe")
.current_dir("libs/ztm/pipy")
// .arg("install")
.arg("/C")
.arg("npm install")
.output()
.expect("failed to run npm install in ztm/pipy");
}

fn main() {
// check submodule exists
let check_file = Path::new("libs/ztm/pipy/CMakeLists.txt");
if !check_file.exists() {
panic!("Please run `git submodule update --init --recursive` to get the submodule");
}

copy_mega_apps();

build_agent_ui();

run_npm_install();

/* compile ztm & pipy */
let mut config = Config::new("libs/ztm/pipy");

// set to use clang/clang++ to compile
config.define("CMAKE_C_COMPILER", "clang");
config.define("CMAKE_CXX_COMPILER", "clang++");

// set to use clang/clang++ to compile if not in windows
#[cfg(not(target_os = "windows"))]
{
config.define("CMAKE_C_COMPILER", "clang");
config.define("CMAKE_CXX_COMPILER", "clang++");
}
// compile ztm in pipy
config.define("PIPY_SHARED", "ON");
config.define("PIPY_GUI", "OFF");
Expand All @@ -107,9 +147,20 @@ fn main() {
// build, with half of the cpu
let cups = num_cpus::get() - num_cpus::get() / 2;
std::env::set_var("CMAKE_BUILD_PARALLEL_LEVEL", cups.to_string());
let dst = config.build();
config.build()
}

fn main() {
// check submodule exists
let check_file = Path::new("libs/ztm/pipy/CMakeLists.txt");
if !check_file.exists() {
panic!("Please run `git submodule update --init --recursive` to get the submodule");
}

copy_mega_apps();
let dst = build();
parse_args_to_rustc(&dst);
copy_lib_to_target(&dst);
}

fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> {
Expand All @@ -124,4 +175,4 @@ fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result
}
}
Ok(())
}
}