Skip to content
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

refactor(build script): rewrite the main build script #2319

Merged
merged 7 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ cmake-build-debug
/wasm-build.log

# Opt out from history in order to speed the `COPY .` up.
# Note that we should create and/or update the MM_VERSION file when using `COPY .` to build a custom version.
/.git
164 changes: 67 additions & 97 deletions .github/workflows/dev-build.yml

Large diffs are not rendered by default.

150 changes: 60 additions & 90 deletions .github/workflows/release-build.yml

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ scripts/mm2/seed/unparsed.txt
/js/.kdf.*

# Rust artefacts
/MM_DATETIME
/MM_DATETIME.tmp
/MM_VERSION
/MM_VERSION.tmp
/target
/targettest
/clippytarget
Expand Down
18 changes: 6 additions & 12 deletions docs/ANDROID.md
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to revise our docs... This is extremely outdated.

Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,34 @@ We need a Unix operating system (the build has been tested on Linux and Mac).

We need a free access to the Docker (`docker run hello-world` should work).

We need the Nightly revision of Rust, such as

rustup default nightly-2021-05-17

### Install cross

cargo install cross

### Get the source code

git clone --depth=1 git@gitlab.com:KomodoPlatform/supernet.git -b mm2.1-cross
cd supernet
git log --pretty=format:'%h' -n 1 > MM_VERSION
git log --pretty=format:'%cI' -n 1 > MM_DATETIME
git clone --depth=1 https://github.com/KomodoPlatform/komodo-defi-framework
cd komodo-defi-framework

### Install extra packages into the Docker image

The [Android NDK installer](https://github.com/rust-embedded/cross/tree/master/docker/android-ndk.sh) used by the ['cross' docker image](https://github.com/rust-embedded/cross/tree/master/docker/armv7-linux-androideabi) for the cross-compilation uses out-of-date NDK version. So we're going to build a patched up image.

#### armeabi-v7a ABI Docker image

(cd supernet && docker build --tag armv7-linux-androideabi-aga -f .docker/Dockerfile.armv7-linux-androideabi .)
(cd komodo-defi-framework && docker build --tag armv7-linux-androideabi-aga -f .docker/Dockerfile.armv7-linux-androideabi .)

#### arm64-v8a ABI Docker image

(cd supernet && docker build --tag aarch64-linux-android-aga -f .docker/Dockerfile.aarch64-linux-android .)
(cd komodo-defi-framework && docker build --tag aarch64-linux-android-aga -f .docker/Dockerfile.aarch64-linux-android .)

### x86 ABI Docker image

(cd supernet && docker build --tag i686-linux-android-aga -f .docker/Dockerfile.i686-linux-android .)
(cd komodo-defi-framework && docker build --tag i686-linux-android-aga -f .docker/Dockerfile.i686-linux-android .)

### x86_64 ABI Docker image

(cd supernet && docker build --tag x86_64-linux-android-aga -f .docker/Dockerfile.x86_64-linux-android .)
(cd komodo-defi-framework && docker build --tag x86_64-linux-android-aga -f .docker/Dockerfile.x86_64-linux-android .)

### Setup the NDK_HOME variable

Expand Down
133 changes: 33 additions & 100 deletions mm2src/mm2_bin_lib/build.rs
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is re-written from scratch, I suggest you to review this by opening the file directly as the diff view will make it likely impossible to understand the logic.

Original file line number Diff line number Diff line change
@@ -1,114 +1,47 @@
use chrono::DateTime;
use gstuff::slurp;
use chrono::Utc;
use regex::Regex;
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str::from_utf8;
use std::{env, process::Command};

fn path2s(path: PathBuf) -> String {
path.to_str()
.unwrap_or_else(|| panic!("Non-stringy path {:?}", path))
.into()
}

/// AtomicDEX's root.
fn root() -> PathBuf {
let super_net = Path::new(env!("CARGO_MANIFEST_DIR"));
let super_net = match super_net.canonicalize() {
Ok(p) => p,
Err(err) => panic!("Can't canonicalize {:?}: {}", super_net, err),
};
// On Windows we're getting these "\\?\" paths from canonicalize but they aren't any good for CMake.
if cfg!(windows) {
let s = path2s(super_net);
let stripped = match s.strip_prefix(r"\\?\") {
Some(stripped) => stripped,
None => &s,
};
Path::new(stripped).into()
} else {
super_net
}
}

/// This function ensures that we have the “MM_VERSION” and “MM_DATETIME” variables during the build.
///
/// The build script will usually help us by putting the MarketMaker version into the “MM_VERSION” file
/// and the corresponding ISO 8601 time into the “MM_DATETIME” file
///
/// For the nightly builds the version contains the short commit hash.
///
/// We're also trying to get the hash and the time from Git.
///
/// Git information isn't always available during the build (for instance, when a build server is used,
/// we might skip synchronizing the Git repository there),
/// but if it is, then we're going to check if the “MM_DATETIME” and the Git data match.
fn mm_version() -> String {
// Reading version of `mm2_bin_lib` from cargo manifest
let mut version = env!("CARGO_PKG_VERSION").to_owned();

let mm_version_p = root().join("../../MM_VERSION");
let v_file = String::from_utf8(slurp(&mm_version_p)).unwrap();
fn crate_version() -> &'static str { env!("CARGO_PKG_VERSION") }

// if there is MM_VERSION file, that means CI wants to put a tag to version
if !v_file.is_empty() {
version = format!("{}_{}", version, v_file.trim());
fn version_tag() -> Result<String, String> {
if let Ok(tag) = env::var("KDF_BUILD_TAG") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not important get_version_tag seems like better naming

return Ok(tag);
}
// put commit tag to the version
else {
let mut command = Command::new("git");
command.arg("log").arg("--pretty=format:%h").arg("-n1");
if let Ok(go) = command.output() {
if go.status.success() {
let commit_hash = from_utf8(&go.stdout).unwrap().trim().to_string();
if !Regex::new(r"^\w+$").unwrap().is_match(&commit_hash) {
panic!("{}", commit_hash)
}

version = format!("{version}_{commit_hash}");
}
}
let output = Command::new("git")
.args(["log", "--pretty=format:%h", "-n1"])
.output()
.map_err(|e| format!("Failed to run git command: {e}\nSet `KDF_BUILD_TAG` manually instead.",))?;

let commit_hash = String::from_utf8(output.stdout)
.map_err(|e| format!("Invalid UTF-8 sequence: {e}"))?
.trim()
.to_string();

if !Regex::new(r"^\w+$")
.expect("Failed to compile regex")
.is_match(&commit_hash)
{
return Err(format!("Invalid tag: {commit_hash}"));
}

println!("cargo:rustc-env=MM_VERSION={}", version);

let mut dt_git = None;
let mut command = Command::new("git");
command.arg("log").arg("--pretty=format:%cI").arg("-n1"); // ISO 8601
if let Ok(go) = command.output() {
if go.status.success() {
let got = from_utf8(&go.stdout).unwrap().trim();
let _dt_check = DateTime::parse_from_rfc3339(got).unwrap();
dt_git = Some(got.to_string());
}
}
Ok(commit_hash)
}

let mm_datetime_p = root().join("../../MM_DATETIME");
let dt_file = String::from_utf8(slurp(&mm_datetime_p)).unwrap();
let mut dt_file = dt_file.trim().to_string();
if let Some(ref dt_git) = dt_git {
if dt_git[..] != dt_file[..] {
// Create or update the “MM_DATETIME” file in order to appease the Cargo dependency management.
let mut mm_datetime_f = fs::File::create(&mm_datetime_p).unwrap();
mm_datetime_f.write_all(dt_git.as_bytes()).unwrap();
dt_file = dt_git.to_string();
}
}
fn version() -> Result<String, String> { version_tag().map(|tag| format!("{}_{}", crate_version(), tag)) }

println!("cargo:rustc-env=MM_DATETIME={}", dt_file);
fn build_datetime() -> String { Utc::now().to_rfc3339() }

version
fn set_build_variables() -> Result<(), String> {
println!("cargo:rustc-env=KDF_VERSION={}", version()?);
println!("cargo:rustc-env=KDF_DATETIME={}", build_datetime());
Ok(())
}

fn main() {
println!("cargo:rerun-if-env-changed=MANUAL_MM_VERSION");
println!("cargo:rerun-if-changed=../../MM_VERSION");
println!("cargo:rerun-if-changed=../../MM_DATETIME");
if std::env::var("MANUAL_MM_VERSION").is_err() {
// This allows build script to run even if no source code files change as rerun-if-changed checks for a file that doesn't exist
println!("cargo:rerun-if-changed=NON_EXISTING_FILE");
}
mm_version();
println!("cargo:rerun-if-env-changed=CARGO_PKG_VERSION");
println!("cargo:rerun-if-env-changed=KDF_BUILD_TAG");

set_build_variables().expect("Failed to set build variables");
}
shamardy marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions mm2src/mm2_bin_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
#[cfg(not(target_arch = "wasm32"))] mod mm2_native_lib;
#[cfg(target_arch = "wasm32")] mod mm2_wasm_lib;

const MM_VERSION: &str = env!("MM_VERSION");
const MM_DATETIME: &str = env!("MM_DATETIME");
const KDF_VERSION: &str = env!("KDF_VERSION");
const KDF_DATETIME: &str = env!("KDF_DATETIME");

static LP_MAIN_RUNNING: AtomicBool = AtomicBool::new(false);
static CTX: AtomicU32 = AtomicU32::new(0);
Expand Down
6 changes: 3 additions & 3 deletions mm2src/mm2_bin_lib/src/mm2_bin.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#[cfg(not(target_arch = "wasm32"))] use mm2_main::mm2_main;

#[cfg(not(target_arch = "wasm32"))]
const MM_VERSION: &str = env!("MM_VERSION");
const KDF_VERSION: &str = env!("KDF_VERSION");

#[cfg(not(target_arch = "wasm32"))]
const MM_DATETIME: &str = env!("MM_DATETIME");
const KDF_DATETIME: &str = env!("KDF_DATETIME");

#[cfg(all(target_os = "linux", target_arch = "x86_64", target_env = "gnu"))]
#[global_allocator]
Expand All @@ -13,6 +13,6 @@ static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;
fn main() {
#[cfg(not(target_arch = "wasm32"))]
{
mm2_main(MM_VERSION.into(), MM_DATETIME.into())
mm2_main(KDF_VERSION.into(), KDF_DATETIME.into())
}
}
3 changes: 2 additions & 1 deletion mm2src/mm2_bin_lib/src/mm2_native_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ pub unsafe extern "C" fn mm2_main(conf: *const c_char, log_cb: extern "C" fn(lin
return;
}
let ctx_cb = &|ctx| CTX.store(ctx, Ordering::Relaxed);
match catch_unwind(move || mm2_main::run_lp_main(Some(&conf), ctx_cb, MM_VERSION.into(), MM_DATETIME.into())) {
match catch_unwind(move || mm2_main::run_lp_main(Some(&conf), ctx_cb, KDF_VERSION.into(), KDF_DATETIME.into()))
{
Ok(Ok(_)) => log!("run_lp_main finished"),
Ok(Err(err)) => log!("run_lp_main error: {}", err),
Err(err) => log!("run_lp_main panic: {:?}", any_to_str(&*err)),
Expand Down
6 changes: 3 additions & 3 deletions mm2src/mm2_bin_lib/src/mm2_wasm_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub fn mm2_main(params: JsValue, log_cb: js_sys::Function) -> Result<(), JsValue
// Ok(Err(err)) => console_err!("run_lp_main error: {}", err),
// Err(err) => console_err!("run_lp_main panic: {:?}", any_to_str(&*err)),
// };
match mm2_main::lp_main(params, &ctx_cb, MM_VERSION.into(), MM_DATETIME.into()).await {
match mm2_main::lp_main(params, &ctx_cb, KDF_VERSION.into(), KDF_DATETIME.into()).await {
Ok(()) => console_info!("run_lp_main finished"),
Err(err) => console_err!("run_lp_main error: {}", err),
};
Expand Down Expand Up @@ -242,8 +242,8 @@ pub async fn mm2_rpc(payload: JsValue) -> Result<JsValue, JsValue> {
#[wasm_bindgen]
pub fn mm2_version() -> JsValue {
serialize_to_js(&MmVersionResponse {
result: MM_VERSION.into(),
datetime: MM_DATETIME.into(),
result: KDF_VERSION.into(),
datetime: KDF_DATETIME.into(),
})
.expect("expected serialization to succeed")
}
Expand Down
Loading