|
| 1 | +use std::env; |
| 2 | +use std::fs; |
| 3 | +use std::path::{Path, PathBuf}; |
| 4 | +use serde::Deserialize; |
| 5 | + |
| 6 | +#[derive(Debug, Deserialize)] |
| 7 | +struct CompileCommand { |
| 8 | + directory: String, |
| 9 | + command: String, |
| 10 | + file: String, |
| 11 | +} |
| 12 | + |
| 13 | +fn main() { |
| 14 | + // Get the directory for compile_commands.json from an environment variable |
| 15 | + let compile_commands_dir = env::var("COMPILE_COMMANDS_DIR") |
| 16 | + .unwrap_or_else(|_| ".".to_string()); // Default to current directory |
| 17 | + |
| 18 | + // Construct the path to the compile_commands.json file |
| 19 | + let compile_commands_path = Path::new(&compile_commands_dir).join("compile_commands.json"); |
| 20 | + |
| 21 | + // Parse compile_commands.json |
| 22 | + let compile_commands: Vec<CompileCommand> = { |
| 23 | + let data = fs::read_to_string(&compile_commands_path) |
| 24 | + .expect("Failed to read compile_commands.json"); |
| 25 | + serde_json::from_str(&data) |
| 26 | + .expect("Failed to parse compile_commands.json") |
| 27 | + }; |
| 28 | + |
| 29 | + // Directory of compile_commands.json, used to resolve relative paths |
| 30 | + let base_dir = compile_commands_path |
| 31 | + .parent() |
| 32 | + .expect("Failed to get base directory of compile_commands.json"); |
| 33 | + |
| 34 | + // List of C files to compile (only base names) |
| 35 | + let files_to_compile = vec![ |
| 36 | + "mdns_networking_socket.c", |
| 37 | + "log_write.c", |
| 38 | + "log_timestamp.c", |
| 39 | + "esp_netif_linux.c", |
| 40 | + "freertos_linux.c", |
| 41 | + "tag_log_level.c", |
| 42 | + "log_linked_list.c", |
| 43 | + "log_lock.c", |
| 44 | + "log_level.c", |
| 45 | + "log_binary_heap.c", |
| 46 | + "esp_system_linux2.c", |
| 47 | + "heap_caps_linux.c", |
| 48 | + "mdns_stub.c", |
| 49 | + "log_buffers.c", |
| 50 | + "util.c" |
| 51 | + ]; |
| 52 | + |
| 53 | + // Initialize the build |
| 54 | + let mut build = cc::Build::new(); |
| 55 | + |
| 56 | +for file in &files_to_compile { |
| 57 | + // Extract the base name from `file` for comparison |
| 58 | + let target_base_name = Path::new(file) |
| 59 | + .file_name() |
| 60 | + .expect("Failed to extract base name from target file") |
| 61 | + .to_str() |
| 62 | + .expect("Target file name is not valid UTF-8"); |
| 63 | + |
| 64 | + // Find the entry in compile_commands.json by matching the base name |
| 65 | + let cmd = compile_commands.iter() |
| 66 | + .find(|entry| { |
| 67 | + let full_path = Path::new(&entry.directory).join(&entry.file); // Resolve relative paths |
| 68 | + if let Some(base_name) = full_path.file_name().and_then(|name| name.to_str()) { |
| 69 | +// println!("Checking file: {} against {}", base_name, target_base_name); // Debug information |
| 70 | + base_name == target_base_name |
| 71 | + } else { |
| 72 | + false |
| 73 | + } |
| 74 | + }) |
| 75 | + .unwrap_or_else(|| panic!("{} not found in compile_commands.json", target_base_name)); |
| 76 | + |
| 77 | + // Add the file to the build |
| 78 | + build.file(&cmd.file); |
| 79 | + |
| 80 | + // Parse flags and include paths from the command |
| 81 | + for part in cmd.command.split_whitespace() { |
| 82 | + if part.starts_with("-I") { |
| 83 | + // Handle include directories |
| 84 | + let include_path = &part[2..]; |
| 85 | + let full_include_path = if Path::new(include_path).is_relative() { |
| 86 | + base_dir.join(include_path).canonicalize() |
| 87 | + .expect("Failed to resolve relative include path") |
| 88 | + } else { |
| 89 | + PathBuf::from(include_path) |
| 90 | + }; |
| 91 | + build.include(full_include_path); |
| 92 | + } else if part.starts_with("-D") || part.starts_with("-std") { |
| 93 | + // Add other compilation flags |
| 94 | + build.flag(part); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + // Compile with the gathered information |
| 103 | + build.compile("mdns"); |
| 104 | +} |
0 commit comments