Skip to content

Commit f4bab8d

Browse files
authored
Fix libvharness build for docs rs (#3548)
* fix libvharness build for docs rs * fmt
1 parent a812faa commit f4bab8d

File tree

1 file changed

+139
-153
lines changed
  • crates/libafl_qemu/libvharness_sys

1 file changed

+139
-153
lines changed

crates/libafl_qemu/libvharness_sys/build.rs

Lines changed: 139 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const LIBVHARNESS_DIRNAME: &str = "libvharness";
55
const LIBVHARNESS_COMMIT: &str = "9a316966ce7aa4bd9f733491511e6ac4be6dd980";
66

77
fn main() {
8+
let runs_in_docs_rs = env::var("DOCS_RS").is_ok();
9+
810
let src_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
911
let src_dir = PathBuf::from(src_dir).join("src");
1012

@@ -17,165 +19,149 @@ fn main() {
1719
target_dir.pop();
1820
target_dir.pop();
1921

20-
println!("cargo:rerun-if-env-changed=VHARNESS_DIR");
21-
let vharness_dir = if let Some(vharness_dir) = env::var_os("VHARNESS_DIR") {
22-
println!("cargo:rerun-if-env-changed={}", vharness_dir.display());
23-
PathBuf::from(&vharness_dir)
24-
} else {
25-
let vharness_dir = target_dir.join(LIBVHARNESS_DIRNAME);
26-
27-
let vharness_rev = vharness_dir.join("QEMU_REVISION");
28-
if !vharness_rev.exists()
29-
|| fs::read_to_string(&vharness_rev).expect("Failed to read QEMU_REVISION")
30-
!= LIBVHARNESS_COMMIT
31-
{
32-
drop(fs::remove_dir_all(&vharness_dir));
33-
}
34-
35-
if !vharness_dir.exists() {
36-
fs::create_dir_all(&vharness_dir).unwrap();
37-
assert!(
38-
Command::new("git")
39-
.current_dir(&vharness_dir)
40-
.arg("init")
41-
.status()
42-
.unwrap()
43-
.success()
44-
);
45-
assert!(
46-
Command::new("git")
47-
.current_dir(&vharness_dir)
48-
.arg("remote")
49-
.arg("add")
50-
.arg("origin")
51-
.arg(LIBVHARNESS_URL)
52-
.status()
53-
.unwrap()
54-
.success()
55-
);
56-
assert!(
57-
Command::new("git")
58-
.current_dir(&vharness_dir)
59-
.arg("fetch")
60-
.arg("--depth")
61-
.arg("1")
62-
.arg("origin")
63-
.arg(LIBVHARNESS_COMMIT)
64-
.status()
65-
.unwrap()
66-
.success()
67-
);
68-
assert!(
69-
Command::new("git")
70-
.current_dir(&vharness_dir)
71-
.arg("checkout")
72-
.arg("FETCH_HEAD")
73-
.status()
74-
.unwrap()
75-
.success()
76-
);
77-
78-
fs::write(&vharness_rev, LIBVHARNESS_COMMIT).unwrap();
79-
}
80-
81-
vharness_dir
82-
};
83-
84-
let vharness_out_dir = target_dir.join("vharness_out");
85-
let toolchains_dir = vharness_dir.join("toolchains");
86-
let vharness_stub = src_dir.join("stub.rs");
87-
8822
let gen_binding = out_dir.join("bindings.rs");
23+
let vharness_stub = src_dir.join("stub.rs");
8924

90-
let api = if cfg!(feature = "nyx") {
91-
"nyx".to_string()
92-
} else {
93-
"lqemu".to_string()
94-
};
95-
96-
let platform = if cfg!(feature = "linux") {
97-
"linux".to_string()
98-
} else if cfg!(feature = "linux-kernel") {
99-
"linux-kernel".to_string()
100-
} else {
101-
"generic".to_string()
102-
};
103-
104-
let cpu_target = if cfg!(feature = "x86_64") {
105-
"x86_64".to_string()
106-
} else if cfg!(feature = "arm") {
107-
"arm".to_string()
108-
} else if cfg!(feature = "aarch64") {
109-
"aarch64".to_string()
110-
} else if cfg!(feature = "i386") {
111-
"i386".to_string()
112-
} else if cfg!(feature = "mips") {
113-
"mips".to_string()
114-
} else if cfg!(feature = "ppc") {
115-
"ppc".to_string()
116-
} else if cfg!(feature = "riscv32") {
117-
"riscv32".to_string()
118-
} else if cfg!(feature = "riscv64") {
119-
"riscv64".to_string()
120-
} else if cfg!(feature = "hexagon") {
121-
"hexagon".to_string()
25+
if runs_in_docs_rs || cfg!(feature = "clippy") {
26+
fs::copy(vharness_stub, gen_binding).unwrap();
12227
} else {
123-
env::var("CPU_TARGET").unwrap_or_else(|_| "x86_64".to_string())
124-
};
125-
126-
let toolchain_file = toolchains_dir.join(format!("{cpu_target}-{platform}.cmake"));
28+
println!("cargo:rerun-if-env-changed=LIBVHARNESS_GEN_STUBS");
29+
println!("cargo:rerun-if-env-changed=VHARNESS_DIR");
30+
31+
let vharness_dir = if let Some(vharness_dir) = env::var_os("VHARNESS_DIR") {
32+
println!("cargo:rerun-if-env-changed={}", vharness_dir.display());
33+
PathBuf::from(&vharness_dir)
34+
} else {
35+
let vharness_dir = target_dir.join(LIBVHARNESS_DIRNAME);
36+
37+
let vharness_rev = vharness_dir.join("QEMU_REVISION");
38+
if !vharness_rev.exists()
39+
|| fs::read_to_string(&vharness_rev).expect("Failed to read QEMU_REVISION")
40+
!= LIBVHARNESS_COMMIT
41+
{
42+
drop(fs::remove_dir_all(&vharness_dir));
43+
}
44+
45+
if !vharness_dir.exists() {
46+
fs::create_dir_all(&vharness_dir).unwrap();
47+
assert!(
48+
Command::new("git")
49+
.current_dir(&vharness_dir)
50+
.arg("init")
51+
.status()
52+
.unwrap()
53+
.success()
54+
);
55+
assert!(
56+
Command::new("git")
57+
.current_dir(&vharness_dir)
58+
.arg("remote")
59+
.arg("add")
60+
.arg("origin")
61+
.arg(LIBVHARNESS_URL)
62+
.status()
63+
.unwrap()
64+
.success()
65+
);
66+
assert!(
67+
Command::new("git")
68+
.current_dir(&vharness_dir)
69+
.arg("fetch")
70+
.arg("--depth")
71+
.arg("1")
72+
.arg("origin")
73+
.arg(LIBVHARNESS_COMMIT)
74+
.status()
75+
.unwrap()
76+
.success()
77+
);
78+
assert!(
79+
Command::new("git")
80+
.current_dir(&vharness_dir)
81+
.arg("checkout")
82+
.arg("FETCH_HEAD")
83+
.status()
84+
.unwrap()
85+
.success()
86+
);
87+
88+
fs::write(&vharness_rev, LIBVHARNESS_COMMIT).unwrap();
89+
}
90+
91+
vharness_dir
92+
};
93+
94+
let vharness_out_dir = target_dir.join("vharness_out");
95+
let toolchains_dir = vharness_dir.join("toolchains");
96+
97+
let api = if cfg!(feature = "nyx") {
98+
"nyx".to_string()
99+
} else {
100+
"lqemu".to_string()
101+
};
102+
103+
let platform = if cfg!(feature = "linux") {
104+
"linux".to_string()
105+
} else if cfg!(feature = "linux-kernel") {
106+
"linux-kernel".to_string()
107+
} else {
108+
"generic".to_string()
109+
};
110+
111+
let cpu_target = if cfg!(feature = "x86_64") {
112+
"x86_64".to_string()
113+
} else if cfg!(feature = "arm") {
114+
"arm".to_string()
115+
} else if cfg!(feature = "aarch64") {
116+
"aarch64".to_string()
117+
} else if cfg!(feature = "i386") {
118+
"i386".to_string()
119+
} else if cfg!(feature = "mips") {
120+
"mips".to_string()
121+
} else if cfg!(feature = "ppc") {
122+
"ppc".to_string()
123+
} else if cfg!(feature = "riscv32") {
124+
"riscv32".to_string()
125+
} else if cfg!(feature = "riscv64") {
126+
"riscv64".to_string()
127+
} else if cfg!(feature = "hexagon") {
128+
"hexagon".to_string()
129+
} else {
130+
env::var("CPU_TARGET").unwrap_or_else(|_| "x86_64".to_string())
131+
};
132+
133+
let toolchain_file = toolchains_dir.join(format!("{cpu_target}-{platform}.cmake"));
134+
135+
if !toolchain_file.exists() {
136+
println!("Unsupported toolchain: target CPU {cpu_target} - platform {platform}");
137+
}
127138

128-
if !toolchain_file.exists() {
129-
println!("Unsupported toolchain: target CPU {cpu_target} - platform {platform}");
130-
}
139+
if vharness_out_dir.exists() {
140+
fs::remove_dir_all(&vharness_out_dir).unwrap();
141+
}
131142

132-
if vharness_out_dir.exists() {
133-
fs::remove_dir_all(&vharness_out_dir).unwrap();
134-
}
143+
// target vharness compilation
144+
cmake::Config::new(&vharness_dir)
145+
.define("CMAKE_TOOLCHAIN_FILE", &toolchain_file)
146+
.define("VHARNESS_API", &api)
147+
.define("VHARNESS_TESTS", "OFF")
148+
.define("VHARNESS_INCLUDE_ONLY", "ON")
149+
.out_dir(&vharness_out_dir)
150+
.build();
151+
152+
// host vharness_compilation
153+
let vharness_out_dir = cmake::Config::new(&vharness_dir)
154+
.define(
155+
"CMAKE_TOOLCHAIN_FILE",
156+
toolchains_dir.join(format!("{cpu_target}-generic.cmake")),
157+
)
158+
.define("VHARNESS_API", &api)
159+
.define("VHARNESS_TESTS", "OFF")
160+
.define("VHARNESS_INCLUDE_ONLY", "ON")
161+
.build();
162+
163+
let vharness_include_dir = vharness_out_dir.join("include");
135164

136-
// target vharness compilation
137-
cmake::Config::new(&vharness_dir)
138-
.define("CMAKE_TOOLCHAIN_FILE", &toolchain_file)
139-
.define("VHARNESS_API", &api)
140-
.define("VHARNESS_TESTS", "OFF")
141-
.define("VHARNESS_INCLUDE_ONLY", "ON")
142-
.out_dir(&vharness_out_dir)
143-
.build();
144-
145-
// host vharness_compilation
146-
let vharness_out_dir = cmake::Config::new(&vharness_dir)
147-
.define(
148-
"CMAKE_TOOLCHAIN_FILE",
149-
toolchains_dir.join(format!("{cpu_target}-generic.cmake")),
150-
)
151-
.define("VHARNESS_API", &api)
152-
.define("VHARNESS_TESTS", "OFF")
153-
.define("VHARNESS_INCLUDE_ONLY", "ON")
154-
.build();
155-
156-
let vharness_include_dir = vharness_out_dir.join("include");
157-
158-
// if cfg!(feature = "static") && cfg!(feature = "shared") {
159-
// panic!("Both static and dynamic features are set.");
160-
// }
161-
162-
// let link_kind = if cfg!(feature = "shared") {
163-
// "dylib"
164-
// } else {
165-
// // fall back to static linking.
166-
// "static"
167-
// };
168-
169-
println!("cargo:rerun-if-env-changed=LIBVHARNESS_GEN_STUBS");
170-
// println!(
171-
// "cargo:rustc-link-search={}/build",
172-
// vharness_out_dir.display()
173-
// );
174-
// println!("cargo:rustc-link-lib={link_kind}=vharness");
175-
176-
if env::var("DOCS_RS").is_ok() || cfg!(feature = "clippy") {
177-
fs::copy(vharness_stub, gen_binding).unwrap();
178-
} else {
179165
bindgen::Builder::default()
180166
.header(format!("{}/{api}.h", vharness_include_dir.display()))
181167
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))

0 commit comments

Comments
 (0)