Skip to content

Commit d5dbcca

Browse files
committed
Clean up build scripts
This includes: - Move all build artifacts to target directory, so they will be cleaned with a `cargo clean` - Remove shell script for building `libgcrypt` and `libgpg-error`. Instead building it within `build.rs` - Use cargo instructions to link builds correctly. This assures, that the linkage is always correct, even when we change the structure
1 parent 6d96df1 commit d5dbcca

File tree

7 files changed

+94
-68
lines changed

7 files changed

+94
-68
lines changed

rust/Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cross.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build.env]
2-
passthrough = ["IN_CROSS=1", "CLEAN=1"]
2+
passthrough = ["IN_CROSS=1"]
33
[target.aarch64-unknown-linux-gnu]
44
dockerfile = "cross_aarch64.Dockerfile"
55
[target.x86_64-unknown-linux-gnu]

rust/crates/nasl-c-lib/build.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
//
33
// SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception
44

5+
use std::env;
56
use std::fs::canonicalize;
67

78
fn main() {
8-
println!(
9-
"cargo:rustc-link-search={}",
10-
canonicalize("./lib").unwrap().to_string_lossy()
11-
);
12-
println!("cargo:rustc-link-lib=static=gcrypt");
13-
println!("cargo:rustc-link-lib=static=gpg-error");
149
println!("cargo:rerun-if-changed=c/cryptographic/gcrypt_mac.c");
1510
println!("cargo:rerun-if-changed=c/cryptographic/gcrypt_error.c");
16-
println!("cargo:rerun-if-changed=lib/libgcrypt.a");
17-
println!("cargo:rerun-if-changed=lib/libgpg-error.a");
11+
12+
let include_dir = env::var("DEP_GCRYPT_INCLUDE").unwrap();
13+
let lib_dir = env::var("DEP_GCRYPT_LIB").unwrap();
14+
15+
println!("cargo:rustc-link-search=native={lib_dir}");
16+
println!("cargo:rustc-link-lib=static=gcrypt");
17+
println!("cargo:rustc-link-lib=static=gpg-error");
1818

1919
cc::Build::new()
2020
.file("c/cryptographic/gcrypt_mac.c")
2121
.file("c/cryptographic/gcrypt_error.c")
22-
.include(canonicalize("./include").unwrap())
22+
.include(canonicalize(include_dir).unwrap())
2323
.opt_level(2)
2424
.compile("crypt");
2525
}

rust/crates/nasl-c-lib/libgcrypt-sys/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ name = "libgcrypt-sys"
33
version = "0.1.0"
44
edition = "2024"
55
links = "gcrypt"
6+
7+
[build-dependencies]
8+
autotools = "0.2"

rust/crates/nasl-c-lib/libgcrypt-sys/build.rs

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,76 @@
22
//
33
// SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception
44

5-
use std::{env, process::Command};
5+
use std::{
6+
env,
7+
path::{Path, PathBuf},
8+
process::Command,
9+
};
10+
11+
const GPG_ERROR_VERSION: &str = "1.54";
12+
const LIBGCRYPT_VERSION: &str = "1.10.2";
613

714
fn main() {
8-
println!("cargo:rerun-if-changed=install-gcrypt.sh");
9-
let target = env::var("TARGET").unwrap_or_default();
10-
let cross = env::var("IN_CROSS").unwrap_or_default();
11-
let clean = env::var("CLEAN").unwrap_or_default();
12-
let out = Command::new("sh")
13-
.arg("install-gcrypt.sh")
14-
.env("TARGET", target)
15-
.env("IN_CROSS", cross)
16-
.env("CLEAN", clean)
17-
.output();
18-
match out {
19-
Ok(out) => {
20-
match out.status.code() {
21-
Some(0) | None => {
22-
//everything is dandy
23-
}
24-
Some(status) => {
25-
panic!(
26-
"Script exited with {status}:\nstdout:\n{}\nstderr:\n{}",
27-
String::from_utf8_lossy(&out.stdout),
28-
String::from_utf8_lossy(&out.stderr)
29-
);
30-
}
31-
}
15+
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
16+
let target = env::var("TARGET").unwrap();
17+
18+
if let Ok(val) = env::var("IN_CROSS")
19+
&& val != "0"
20+
&& target == "aarch64-unknown-linux-gnu"
21+
{
22+
unsafe {
23+
env::set_var("CC", "aarch64-linux-gnu-gcc");
24+
env::set_var("CHOST", "arm64");
3225
}
33-
Err(e) => panic!("Unexpected error: {e}"),
3426
}
27+
28+
let path = fetch_source("libgpg-error", GPG_ERROR_VERSION, &out_dir);
29+
build_autotools_project(&path, &target);
30+
31+
let path = fetch_source("libgcrypt", LIBGCRYPT_VERSION, &out_dir);
32+
build_autotools_project(&path, &target);
33+
34+
let include_dir = out_dir.join("include");
35+
let lib_dir = out_dir.join("lib");
36+
37+
println!("cargo:include={}", include_dir.display());
38+
println!("cargo:lib={}", lib_dir.display());
39+
println!("cargo::rerun-if-env-changed=TARGET");
40+
}
41+
42+
fn fetch_source(name: &str, version: &str, destination: &Path) -> PathBuf {
43+
let folder_name = format!("{}-{}", name, version);
44+
let extract_path = destination.join(&folder_name);
45+
let tar_path = extract_path.with_added_extension("tar.bz2");
46+
47+
if !extract_path.exists() {
48+
if !tar_path.exists() {
49+
let url = format!("https://gnupg.org/ftp/gcrypt/{name}/{folder_name}.tar.bz2");
50+
51+
Command::new("curl")
52+
.args(["--fail", "-O", &url])
53+
.current_dir(destination)
54+
.status()
55+
.unwrap();
56+
}
57+
58+
Command::new("tar")
59+
.args(["-xf", &tar_path.to_string_lossy()])
60+
.current_dir(destination)
61+
.status()
62+
.unwrap();
63+
}
64+
65+
extract_path
66+
}
67+
68+
fn build_autotools_project(src: &Path, target: &str) -> PathBuf {
69+
let mut config = autotools::Config::new(src);
70+
config
71+
.enable_static()
72+
.disable_shared()
73+
.config_option("with-pic", None)
74+
.host(&target);
75+
76+
config.build()
3577
}

rust/crates/nasl-c-lib/libgcrypt-sys/install-gcrypt.sh

Lines changed: 0 additions & 30 deletions
This file was deleted.

rust/src/nasl/builtin/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ mod isotime;
1616
mod knowledge_base;
1717
pub mod misc;
1818
pub mod network;
19-
mod snmp;
20-
2119
mod preferences;
2220
#[cfg(feature = "nasl-builtin-raw-ip")]
2321
pub mod raw_ip;
2422
mod regex;
2523
mod report_functions;
24+
mod snmp;
2625
mod ssh;
2726
mod string;
2827
mod sys;

0 commit comments

Comments
 (0)