1#[path = "support/objects.rs"]
16mod objects;
17
18use std::sync::{Arc, Mutex};
19
20use qld::args::{InputAttrs, InputKind, LinkOptions, OutputKind, TextOutput};
21use qld::diag::Collect;
22
23fn main() -> qld::Result<()> {
24 let mut options = LinkOptions::new();
25 options.kind = OutputKind::StaticExecutable;
26 options.push_input(
27 InputKind::bytes("main.o", objects::main_object()),
28 InputAttrs::default(),
29 );
30 options.push_input(
31 InputKind::bytes("answer.o", objects::answer_object(42)),
32 InputAttrs::default(),
33 );
34
35 options.print_map = true;
37 options.cref = true;
38 let map = Arc::new(Mutex::new(String::new()));
39 let collected = Arc::clone(&map);
40 options.map_output = Some(TextOutput::new(move |text| {
41 collected.lock().unwrap().push_str(text);
42 }));
43
44 let image = qld::link_to_memory(&options, &Collect::new())?;
45 let map = map.lock().unwrap();
46 println!("linked {} bytes, {} of map", image.len(), map.len());
47 for line in map.lines().take(8) {
48 println!(" {line}");
49 }
50
51 let mut as_the_binary = options.clone();
53 as_the_binary.use_process_defaults();
54 println!(
55 "\nhermetic: map_output {}, LD_LIBRARY_PATH {} entries, timing {}",
56 if options.map_output.is_some() {
57 "set by this program"
58 } else {
59 "unset"
60 },
61 options.env_library_path.len(),
62 if options.timing.is_some() {
63 "on"
64 } else {
65 "off"
66 },
67 );
68 println!(
69 "as the binary: LD_LIBRARY_PATH {} entries, timing {}",
70 as_the_binary.env_library_path.len(),
71 if as_the_binary.timing.is_some() {
72 "on"
73 } else {
74 "off"
75 },
76 );
77 Ok(())
78}