use std::fs;
use std::path::Path;
use crate::common::{make_executable, run_rsconstruct, write_file};
const FAKE_MAKE: &str = r#"#!/usr/bin/env python3
import os
import sys
m_dir = None
subcommand = None
for arg in sys.argv[1:]:
if arg.startswith("M="):
m_dir = arg[2:]
elif arg in ("modules", "clean"):
subcommand = arg
if subcommand == "modules":
kbuild = os.path.join(m_dir, "Kbuild")
name = None
with open(kbuild) as handle:
for line in handle:
if line.startswith("obj-m"):
# obj-m := <name>.o
name = line.split(":=", 1)[1].strip()[:-len(".o")]
elif "-objs" in line:
for obj in line.split(":=", 1)[1].split():
with open(os.path.join(m_dir, obj), "wb") as handle_o:
handle_o.write(b"FAKE-OBJ")
if os.path.exists(os.path.join(m_dir, ".fail-modules")):
sys.stderr.write("fake make: error: compilation failed\n")
sys.exit(1)
with open(os.path.join(m_dir, name + ".ko"), "wb") as handle:
handle.write(b"FAKE-KO-BYTES")
elif subcommand == "clean":
for root, _dirs, files in os.walk(m_dir):
for filename in files:
if filename.endswith((".ko", ".o")):
os.unlink(os.path.join(root, filename))
sys.exit(0)
"#;
fn install_fake_make_named(project: &Path, name: &str) -> String {
let make_path = project.join("bin").join(name);
fs::create_dir_all(make_path.parent().unwrap()).unwrap();
fs::write(&make_path, FAKE_MAKE).unwrap();
make_executable(&make_path);
make_path.to_string_lossy().into_owned()
}
fn install_fake_make(project: &Path) -> String {
install_fake_make_named(project, "make")
}
#[test]
fn linux_module_root_level_manifest_output_survives_clean() {
let temp = tempfile::TempDir::new().unwrap();
let project = temp.path();
let make = install_fake_make(project);
fs::create_dir_all(project.join("kdir")).unwrap();
let kdir = project.join("kdir").to_string_lossy().into_owned();
write_file(project, "top.c", "int top;\n");
write_file(project, "helper.c", "int helper;\n");
write_file(
project,
"linux-module.yaml",
&format!(
"make: {make}\nkdir: {kdir}\nmodules:\n - name: mymod\n sources: [top.c, helper.c]\n"
),
);
write_file(
project,
"rsconstruct.toml",
"[processor.mass_generator.linux_module]\nsrc_dirs = [\".\"]\n",
);
let output = run_rsconstruct(project, &["build"]);
assert!(
output.status.success(),
"build failed: {}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
);
let ko = project.join("out/processor.mass_generator.linux_module/mymod.ko");
assert!(
ko.exists(),
"output module not produced at {}",
ko.display()
);
assert_eq!(fs::read(&ko).unwrap(), b"FAKE-KO-BYTES");
assert!(
!project.join("mymod.ko").exists(),
"source .ko not cleaned up"
);
assert!(
!project.join("Kbuild").exists(),
"generated Kbuild not removed"
);
}
#[test]
fn linux_module_subdir_manifest_builds() {
let temp = tempfile::TempDir::new().unwrap();
let project = temp.path();
let make = install_fake_make(project);
fs::create_dir_all(project.join("kdir")).unwrap();
let kdir = project.join("kdir").to_string_lossy().into_owned();
write_file(project, "drivers/hello/main.c", "int hello;\n");
write_file(
project,
"drivers/hello/linux-module.yaml",
&format!("make: {make}\nkdir: {kdir}\nmodules:\n - name: hello\n sources: [main.c]\n"),
);
write_file(
project,
"rsconstruct.toml",
"[processor.mass_generator.linux_module]\nsrc_dirs = [\"drivers\"]\n",
);
let output = run_rsconstruct(project, &["build"]);
assert!(
output.status.success(),
"build failed: {}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
);
let ko = project.join("out/processor.mass_generator.linux_module/drivers/hello/hello.ko");
assert!(
ko.exists(),
"output module not produced at {}",
ko.display()
);
assert_eq!(fs::read(&ko).unwrap(), b"FAKE-KO-BYTES");
assert!(
!project.join("drivers/hello/hello.ko").exists(),
"source .ko not cleaned up"
);
assert!(
!project.join("drivers/hello/Kbuild").exists(),
"generated Kbuild not removed"
);
}
#[test]
fn linux_module_failed_build_cleans_the_source_directory() {
let temp = tempfile::TempDir::new().unwrap();
let project = temp.path();
let make = install_fake_make(project);
fs::create_dir_all(project.join("kdir")).unwrap();
let kdir = project.join("kdir").to_string_lossy().into_owned();
write_file(project, "drivers/bad/main.c", "int bad;\n");
write_file(project, "drivers/bad/.fail-modules", "");
write_file(
project,
"drivers/bad/linux-module.yaml",
&format!("make: {make}\nkdir: {kdir}\nmodules:\n - name: bad\n sources: [main.c]\n"),
);
write_file(
project,
"rsconstruct.toml",
"[processor.mass_generator.linux_module]\nsrc_dirs = [\"drivers\"]\n",
);
let output = run_rsconstruct(project, &["build"]);
assert!(!output.status.success(), "the build must fail with make");
let text = format!(
"{}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
assert!(text.contains("make modules for bad failed"), "{text}");
assert!(text.contains("compilation failed"), "{text}");
assert!(
!project.join("drivers/bad/Kbuild").exists(),
"generated Kbuild left behind after a failed build"
);
assert!(
!project.join("drivers/bad/main.o").exists(),
"kbuild intermediate left behind after a failed build"
);
assert!(
project.join("drivers/bad/main.c").exists(),
"the source itself must survive the cleanup"
);
}
#[test]
fn linux_module_manifest_make_is_the_declared_tool() {
let temp = tempfile::TempDir::new().unwrap();
let project = temp.path();
let kmake = install_fake_make_named(project, "kmake");
fs::create_dir_all(project.join("kdir")).unwrap();
let kdir = project.join("kdir").to_string_lossy().into_owned();
write_file(project, "drivers/hello/main.c", "int hello;\n");
write_file(
project,
"drivers/hello/linux-module.yaml",
&format!("make: {kmake}\nkdir: {kdir}\nmodules:\n - name: hello\n sources: [main.c]\n"),
);
write_file(
project,
"rsconstruct.toml",
"[processor.mass_generator.linux_module]\nsrc_dirs = [\"drivers\"]\n",
);
let output = run_rsconstruct(project, &["build"]);
assert!(
output.status.success(),
"build failed: {}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
);
assert_eq!(
fs::read(project.join("out/processor.mass_generator.linux_module/drivers/hello/hello.ko"))
.unwrap(),
b"FAKE-KO-BYTES"
);
}
#[test]
fn linux_module_dep_auto_file_is_an_input_of_every_module() {
let temp = tempfile::TempDir::new().unwrap();
let project = temp.path();
let make = install_fake_make(project);
fs::create_dir_all(project.join("kdir")).unwrap();
let kdir = project.join("kdir").to_string_lossy().into_owned();
write_file(project, "drivers/hello/main.c", "int hello;\n");
write_file(
project,
"drivers/hello/linux-module.yaml",
&format!("make: {make}\nkdir: {kdir}\nmodules:\n - name: hello\n sources: [main.c]\n"),
);
write_file(project, "kernel.cfg", "CONFIG_A=y\n");
write_file(
project,
"rsconstruct.toml",
"[processor.mass_generator.linux_module]\nsrc_dirs = [\"drivers\"]\ndep_auto = [\"kernel.cfg\"]\n",
);
let first = run_rsconstruct(project, &["build", "-v"]);
assert!(
first.status.success(),
"first build failed: {}\n{}",
String::from_utf8_lossy(&first.stdout),
String::from_utf8_lossy(&first.stderr),
);
let unchanged = run_rsconstruct(project, &["build", "-v"]);
assert!(unchanged.status.success());
let stdout = String::from_utf8_lossy(&unchanged.stdout);
assert!(
stdout.contains("Skipping (unchanged):"),
"nothing changed, so nothing rebuilds: {stdout}"
);
write_file(project, "kernel.cfg", "CONFIG_A=n\n");
let edited = run_rsconstruct(project, &["build", "-v"]);
assert!(edited.status.success());
let stdout = String::from_utf8_lossy(&edited.stdout);
assert!(
stdout.contains("Processing:"),
"an edit to the dep_auto file must rebuild the module: {stdout}"
);
}