Guest User

DPDK meson.build

a guest
May 26th, 2025
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 10.93 KB | None | 0 0
  1. use std::env;
  2. use std::fs;
  3. use std::path::{Path, PathBuf};
  4. use std::collections::HashMap;
  5.  
  6. fn main() {
  7.     println!("cargo:rerun-if-changed=build.rs");
  8.     println!("cargo:rerun-if-changed=VERSION");
  9.     println!("cargo:rerun-if-changed=ABI_VERSION");
  10.    
  11.     let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
  12.     let out_dir = env::var("OUT_DIR").unwrap();
  13.     let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
  14.     let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
  15.    
  16.     // Check for developer mode
  17.     let developer_mode = if env::var("DPDK_DEVELOPER_MODE").is_ok() {
  18.         env::var("DPDK_DEVELOPER_MODE") == Ok("true".to_string())
  19.     } else {
  20.         Path::new(&format!("{}/.git", manifest_dir)).exists()
  21.     };
  22.    
  23.     if developer_mode {
  24.         println!("cargo:warning=## Building in Developer Mode ##");
  25.     }
  26.    
  27.     // Set up global configuration
  28.     let dpdk_source_root = manifest_dir.clone();
  29.     let dpdk_build_root = out_dir.clone();
  30.     let mut dpdk_conf = HashMap::new();
  31.     let mut dpdk_includes = Vec::new();
  32.     let mut dpdk_libraries = Vec::new();
  33.     let mut dpdk_static_libraries = Vec::new();
  34.     let mut dpdk_shared_lib_deps = Vec::new();
  35.     let mut dpdk_static_lib_deps = Vec::new();
  36.     let mut dpdk_chkinc_headers = Vec::new();
  37.     let mut dpdk_driver_classes = Vec::new();
  38.     let mut dpdk_drivers = Vec::new();
  39.     let mut dpdk_extra_ldflags = Vec::new();
  40.     let mut dpdk_libs_deprecated = Vec::new();
  41.     let mut dpdk_apps_disabled = Vec::new();
  42.     let mut dpdk_apps_enabled = Vec::new();
  43.     let mut dpdk_libs_disabled = Vec::new();
  44.     let mut dpdk_libs_enabled = Vec::new();
  45.     let mut dpdk_drvs_disabled = Vec::new();
  46.     let mut testpmd_drivers_sources = Vec::new();
  47.     let mut testpmd_drivers_deps = Vec::new();
  48.    
  49.     // Determine architecture subdirectory
  50.     let arch_subdir = match target_arch.as_str() {
  51.         arch if arch.starts_with("x86") => "x86",
  52.         arch if arch.starts_with("arm") || arch.starts_with("aarch") => "arm",
  53.         arch if arch.starts_with("loongarch") => "loongarch",
  54.         arch if arch.starts_with("powerpc") => "ppc",
  55.         arch if arch.starts_with("riscv") => "riscv",
  56.         _ => "unknown",
  57.     };
  58.    
  59.     // Configure global include directories
  60.     let global_inc = vec![
  61.         format!("{}", dpdk_source_root),
  62.         format!("{}/config", dpdk_source_root),
  63.         format!("{}/lib/eal/include", dpdk_source_root),
  64.         format!("{}/lib/eal/{}/include", dpdk_source_root, target_os),
  65.         format!("{}/lib/eal/{}/include", dpdk_source_root, arch_subdir),
  66.     ];
  67.    
  68.     for inc_dir in &global_inc {
  69.         println!("cargo:include={}", inc_dir);
  70.     }
  71.    
  72.     // Add Linux-specific includes
  73.     if target_os == "linux" {
  74.         println!("cargo:include={}/kernel/linux", dpdk_source_root);
  75.     }
  76.    
  77.     // Configure build tools
  78.     configure_buildtools(&dpdk_source_root, &mut dpdk_conf);
  79.    
  80.     // Configure main build options
  81.     configure_build(&dpdk_source_root, &mut dpdk_conf, arch_subdir, &target_os);
  82.    
  83.     // Build libraries
  84.     build_libraries(&dpdk_source_root, &mut dpdk_libs_enabled, &mut dpdk_libs_disabled);
  85.    
  86.     // Build drivers
  87.     build_drivers(&dpdk_source_root, &mut dpdk_driver_classes, &mut dpdk_drivers, &mut dpdk_drvs_disabled);
  88.    
  89.     // Build user tools
  90.     build_usertools(&dpdk_source_root, &mut dpdk_apps_enabled, &mut dpdk_apps_disabled);
  91.    
  92.     // Build applications
  93.     build_applications(&dpdk_source_root, &mut dpdk_apps_enabled, &mut dpdk_apps_disabled);
  94.    
  95.     // Build documentation (stub)
  96.     build_documentation(&dpdk_source_root);
  97.    
  98.     // Build examples (stub)
  99.     build_examples(&dpdk_source_root);
  100.    
  101.     // Build kernel modules (stub)
  102.     build_kernel_modules(&dpdk_source_root);
  103.    
  104.     // Check header includes if requested
  105.     if env::var("DPDK_CHECK_INCLUDES").is_ok() {
  106.         check_header_includes(&dpdk_source_root);
  107.     }
  108.    
  109.     // Generate build configuration header
  110.     generate_build_config(&out_dir, &dpdk_conf);
  111.    
  112.     // Generate pkg-config files
  113.     generate_pkg_config(&dpdk_source_root, &out_dir);
  114.    
  115.     // Print final build summary
  116.     print_build_summary(&dpdk_apps_enabled, &dpdk_libs_enabled, &dpdk_driver_classes,
  117.                        &dpdk_apps_disabled, &dpdk_libs_disabled, &dpdk_drvs_disabled);
  118.    
  119.     println!("DPDK build config complete:");
  120.     println!("  source path = \"{}\"", dpdk_source_root);
  121.     println!("  build path  = \"{}\"", dpdk_build_root);
  122. }
  123.  
  124. fn configure_buildtools(source_root: &str, conf: &mut HashMap<String, String>) {
  125.     // Stub for buildtools configuration
  126.     conf.insert("BUILDTOOLS_CONFIGURED".to_string(), "true".to_string());
  127. }
  128.  
  129. fn configure_build(source_root: &str, conf: &mut HashMap<String, String>, arch: &str, os: &str) {
  130.     // Read VERSION file
  131.     if let Ok(version) = fs::read_to_string(format!("{}/VERSION", source_root)) {
  132.         conf.insert("RTE_VERSION".to_string(), version.trim().to_string());
  133.     }
  134.    
  135.     // Read ABI_VERSION file
  136.     if let Ok(abi_version) = fs::read_to_string(format!("{}/ABI_VERSION", source_root)) {
  137.         conf.insert("RTE_ABI_VERSION".to_string(), abi_version.trim().to_string());
  138.     }
  139.    
  140.     // Set architecture and OS specific configurations
  141.     conf.insert("RTE_ARCH".to_string(), arch.to_string());
  142.     conf.insert("RTE_OS".to_string(), os.to_string());
  143.     conf.insert("RTE_COMPILE_TIME_CPUFLAGS".to_string(), "0".to_string());
  144. }
  145.  
  146. fn build_libraries(source_root: &str, enabled: &mut Vec<String>, disabled: &mut Vec<String>) {
  147.     // Stub implementation - would scan lib/ directory and determine which libraries to build
  148.     let lib_dirs = ["eal", "ring", "mempool", "mbuf", "timer", "pci", "cmdline"];
  149.    
  150.     for lib in &lib_dirs {
  151.         let lib_path = format!("{}/lib/{}", source_root, lib);
  152.         if Path::new(&lib_path).exists() {
  153.             enabled.push(lib.to_string());
  154.             println!("cargo:rustc-link-lib=static=dpdk_{}", lib);
  155.         } else {
  156.             disabled.push(lib.to_string());
  157.         }
  158.     }
  159. }
  160.  
  161. fn build_drivers(source_root: &str, classes: &mut Vec<String>, drivers: &mut Vec<String>, disabled: &mut Vec<String>) {
  162.     // Stub implementation - would scan drivers/ directory and build driver classes
  163.     let driver_classes = ["net", "crypto", "compress", "vdpa", "event", "baseband", "raw"];
  164.    
  165.     for class in &driver_classes {
  166.         let class_path = format!("{}/drivers/{}", source_root, class);
  167.         if Path::new(&class_path).exists() {
  168.             classes.push(class.to_string());
  169.            
  170.             // Scan for individual drivers in each class
  171.             if let Ok(entries) = fs::read_dir(&class_path) {
  172.                 for entry in entries.flatten() {
  173.                     if entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false) {
  174.                         let driver_name = entry.file_name().to_string_lossy().to_string();
  175.                         drivers.push(format!("{}_{}", class, driver_name));
  176.                         println!("cargo:rustc-link-lib=static=dpdk_{}_{}", class, driver_name);
  177.                     }
  178.                 }
  179.             }
  180.         }
  181.     }
  182. }
  183.  
  184. fn build_usertools(source_root: &str, enabled: &mut Vec<String>, disabled: &mut Vec<String>) {
  185.     // Stub for usertools build
  186.     let usertools_path = format!("{}/usertools", source_root);
  187.     if Path::new(&usertools_path).exists() {
  188.         enabled.push("usertools".to_string());
  189.     }
  190. }
  191.  
  192. fn build_applications(source_root: &str, enabled: &mut Vec<String>, disabled: &mut Vec<String>) {
  193.     // Stub implementation - would scan app/ directory
  194.     let app_dirs = ["testpmd", "proc-info", "pdump", "test"];
  195.    
  196.     for app in &app_dirs {
  197.         let app_path = format!("{}/app/{}", source_root, app);
  198.         if Path::new(&app_path).exists() {
  199.             enabled.push(app.to_string());
  200.         } else {
  201.             disabled.push(app.to_string());
  202.         }
  203.     }
  204. }
  205.  
  206. fn build_documentation(source_root: &str) {
  207.     // Stub for documentation build
  208. }
  209.  
  210. fn build_examples(source_root: &str) {
  211.     // Stub for examples build
  212. }
  213.  
  214. fn build_kernel_modules(source_root: &str) {
  215.     // Stub for kernel modules build
  216. }
  217.  
  218. fn check_header_includes(source_root: &str) {
  219.     // Stub for header include checking
  220. }
  221.  
  222. fn generate_build_config(out_dir: &str, conf: &HashMap<String, String>) {
  223.     let config_path = format!("{}/rte_build_config.h", out_dir);
  224.     let mut config_content = String::new();
  225.    
  226.     config_content.push_str("/* Auto-generated build configuration */\n");
  227.     config_content.push_str("#ifndef RTE_BUILD_CONFIG_H\n");
  228.     config_content.push_str("#define RTE_BUILD_CONFIG_H\n\n");
  229.    
  230.     for (key, value) in conf {
  231.         config_content.push_str(&format!("#define {} {}\n", key, value));
  232.     }
  233.    
  234.     config_content.push_str("\n#endif /* RTE_BUILD_CONFIG_H */\n");
  235.    
  236.     fs::write(config_path, config_content).expect("Failed to write build config");
  237. }
  238.  
  239. fn generate_pkg_config(source_root: &str, out_dir: &str) {
  240.     // Stub for pkg-config generation
  241. }
  242.  
  243. fn print_build_summary(apps_enabled: &[String], libs_enabled: &[String],
  244.                       driver_classes: &[String], apps_disabled: &[String],
  245.                       libs_disabled: &[String], drivers_disabled: &[String]) {
  246.    
  247.     println!("\n=================");
  248.     println!("Applications Enabled");
  249.     println!("=================");
  250.     print!("\napps:\n\t");
  251.    
  252.     for (i, app) in apps_enabled.iter().enumerate() {
  253.         print!("{}, ", app);
  254.         if (i + 1) % 8 == 0 {
  255.             print!("\n\t");
  256.         }
  257.     }
  258.     println!();
  259.    
  260.     println!("\n=================");
  261.     println!("Libraries Enabled");
  262.     println!("=================");
  263.     print!("\nlibs:\n\t");
  264.    
  265.     for (i, lib) in libs_enabled.iter().enumerate() {
  266.         print!("{}, ", lib);
  267.         if (i + 1) % 8 == 0 {
  268.             print!("\n\t");
  269.         }
  270.     }
  271.     println!();
  272.    
  273.     println!("\n===============");
  274.     println!("Drivers Enabled");
  275.     println!("===============");
  276.    
  277.     for class in driver_classes {
  278.         println!("\n{}:", class);
  279.         print!("\t");
  280.         // Would iterate through actual drivers in each class
  281.         println!("(drivers would be listed here)");
  282.     }
  283.    
  284.     println!("\n=================");
  285.     println!("Content Skipped");
  286.     println!("=================");
  287.    
  288.     if !apps_disabled.is_empty() {
  289.         print!("\napps:\n\t");
  290.         for app in apps_disabled {
  291.             println!("{}:\t(reason would be shown here)", app);
  292.         }
  293.     }
  294.    
  295.     if !libs_disabled.is_empty() {
  296.         print!("\nlibs:\n\t");
  297.         for lib in libs_disabled {
  298.             println!("{}:\t(reason would be shown here)", lib);
  299.         }
  300.     }
  301.    
  302.     if !drivers_disabled.is_empty() {
  303.         print!("\ndrivers:\n\t");
  304.         for driver in drivers_disabled {
  305.             println!("{}:\t(reason would be shown here)", driver);
  306.         }
  307.     }
  308. }
Advertisement
Add Comment
Please, Sign In to add comment