Skip to content

Commit e6030ee

Browse files
fix(publish): change directory to OUT_DIR (#8)
* fix(publish): change directory to OUT_DIR * improve: more clean build.rs
1 parent 4f3930c commit e6030ee

File tree

2 files changed

+36
-47
lines changed

2 files changed

+36
-47
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ keywords = ["Spot", "TimeSeries", "AnomalyDetection"]
1414
include = [
1515
"src/**/*",
1616
"examples/**/*",
17+
"tests/**/*",
18+
"build.rs",
1719
"libspot/Makefile",
1820
"libspot/LICENSE.md",
1921
"libspot/README.md",

build.rs

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,41 @@ use std::path::Path;
44
use std::process::Command;
55

66
fn main() {
7-
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
8-
9-
// Path to the libspot directory
10-
let libspot_dir = Path::new(&manifest_dir).join("libspot");
11-
let dist_dir = libspot_dir.join("dist");
12-
13-
// Build libspot using make
14-
let output = Command::new("make")
15-
.current_dir(&libspot_dir)
16-
.output()
17-
.expect("Failed to build libspot. Make sure you have make installed.");
18-
19-
if !output.status.success() {
20-
panic!(
21-
"Failed to build libspot: {}",
22-
String::from_utf8_lossy(&output.stderr)
23-
);
24-
}
25-
26-
// Create symlinks without version numbers so Rust can find them
27-
let static_lib_versioned = dist_dir.join("libspot.a.2.0b3");
28-
let static_lib = dist_dir.join("libspot.a");
29-
let shared_lib_versioned = dist_dir.join("libspot.so.2.0b3");
30-
let shared_lib = dist_dir.join("libspot.so");
31-
32-
// Remove old symlinks if they exist
33-
let _ = fs::remove_file(&static_lib);
34-
let _ = fs::remove_file(&shared_lib);
35-
36-
// Create new symlinks
37-
#[cfg(unix)]
38-
{
39-
std::os::unix::fs::symlink(&static_lib_versioned, &static_lib)
40-
.expect("Failed to create symlink for libspot.a");
41-
std::os::unix::fs::symlink(&shared_lib_versioned, &shared_lib)
42-
.expect("Failed to create symlink for libspot.so");
43-
}
44-
45-
println!("cargo:rerun-if-changed=libspot/");
46-
println!("cargo:rerun-if-changed=libspot/src/");
47-
println!("cargo:rerun-if-changed=libspot/Makefile");
48-
49-
// Tell cargo where to find the library
50-
println!("cargo:rustc-link-search=native={}", dist_dir.display());
51-
52-
// Link against the static library (prefer static over shared for portability)
7+
let out_dir = env::var("OUT_DIR").unwrap();
8+
let libspot_dir = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).join("libspot");
9+
let build_dir = Path::new(&out_dir).join("libspot");
10+
11+
// Copy source and build in OUT_DIR
12+
Command::new("cp")
13+
.args([
14+
"-r",
15+
&libspot_dir.to_string_lossy(),
16+
&build_dir.to_string_lossy(),
17+
])
18+
.status()
19+
.expect("Failed to copy libspot source");
20+
21+
Command::new("make")
22+
.current_dir(&build_dir)
23+
.status()
24+
.expect("Failed to build libspot");
25+
26+
// Copy built library to OUT_DIR
27+
fs::copy(
28+
build_dir.join("dist/libspot.a.2.0b3"),
29+
Path::new(&out_dir).join("libspot.a"),
30+
)
31+
.expect("Failed to copy library");
32+
33+
// Tell cargo where to find native libraries (search in OUT_DIR)
34+
println!("cargo:rustc-link-search=native={out_dir}");
35+
36+
// Link against the static libspot library
5337
println!("cargo:rustc-link-lib=static=spot");
5438

55-
// Also need to link math library since libspot uses it
39+
// Link against the math library (required by libspot)
5640
println!("cargo:rustc-link-lib=m");
41+
42+
// Rerun build script if libspot source files change
43+
println!("cargo:rerun-if-changed=libspot/");
5744
}

0 commit comments

Comments
 (0)