From 75cdfb011790bec4b5fe9e01982b99e0b5a1b532 Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Fri, 5 Sep 2025 09:19:51 +0200 Subject: [PATCH] misc: arch: drop `extern crate`, use modern rust This commit is the first in a series of similar commits to clean up obsolete `extern crate` statements Since Rust 1.30, normal macros can be imported via `use`, and with Rust 1.31 and edition 2018 this has become the preferred approach. `extern crate` is only needed for `alloc` in `no_std` crates, which does not apply here. By dropping these (often redundant or odd) `extern crate` lines, we expose the actual dependencies more clearly and reduce technical debt. ## Auto-generation of the series Most of this series was produced automatically: 1. Removed all "extern crate" references 2. Run the script [0] to add missing `use` statements 3. Run `cargo +nightly fmt --all` 4. Fix the remaining problems manually The treewide changes were then split into per-folder commits. [0] ```python import os import re # Mapping of macro/function usage to imports MACRO_IMPORTS = { "info!": "use log::info;\n", "debug!": "use log::debug;\n", "error!": "use log::error;\n", "trace!": "use log::trace;\n", "warn!": "use log::warn;\n", "event!": "use event_monitor::event;\n", "anyhow!(": "use anyhow::anyhow;\n", "bitflags!(": "use bitflags::bitflags;\n", "ioctl_ior_nr!": "use vmm_sys_util::{ioctl_ior_nr};\n", "ioctl_iow_nr!": "use vmm_sys_util::{ioctl_iow_nr};\n", } # Regex for finding the first use statement USE_REGEX = re.compile(r"^\s*(use|pub use) .+?;") def process_file(path): with open(path, "r", encoding="utf-8") as f: lines = f.readlines() content = "".join(lines) existing_imports = set(lines) needed_imports = set() # Check macros/functions against mapping, only add if not already present for key, import_stmt in MACRO_IMPORTS.items(): if key in content and import_stmt not in existing_imports: needed_imports.add(import_stmt) if not needed_imports: print(f"Unmodified {path} (no new imports needed)") return # Nothing to do # Find first use or pub use statement for i, line in enumerate(lines): if USE_REGEX.match(line): insertion_index = i + 1 break else: print(f"Unmodified {path} (no use or pub use statement found)") return # No use statement found, skip file # Insert imports lines[insertion_index:insertion_index] = list(needed_imports) # Write back file with open(path, "w", encoding="utf-8") as f: f.writelines(lines) print(f"Modified {path}, added imports: {''.join(needed_imports).strip()}") for root, _, files in os.walk("."): for file in files: if file.endswith(".rs"): process_file(os.path.join(root, file)) ``` Signed-off-by: Philipp Schuster On-behalf-of: SAP philipp.schuster@sap.com --- arch/src/aarch64/fdt.rs | 1 + arch/src/lib.rs | 3 --- arch/src/riscv64/fdt.rs | 1 + arch/src/x86_64/mod.rs | 15 ++++++++++----- arch/src/x86_64/mptable.rs | 1 + arch/src/x86_64/tdx/mod.rs | 1 + 6 files changed, 14 insertions(+), 8 deletions(-) diff --git a/arch/src/aarch64/fdt.rs b/arch/src/aarch64/fdt.rs index ce3551cc7..ea3f842eb 100644 --- a/arch/src/aarch64/fdt.rs +++ b/arch/src/aarch64/fdt.rs @@ -19,6 +19,7 @@ use hypervisor::arch::aarch64::regs::{ AARCH64_ARCH_TIMER_HYP_IRQ, AARCH64_ARCH_TIMER_PHYS_NONSECURE_IRQ, AARCH64_ARCH_TIMER_PHYS_SECURE_IRQ, AARCH64_ARCH_TIMER_VIRT_IRQ, AARCH64_PMU_IRQ, }; +use log::{debug, warn}; use thiserror::Error; use vm_fdt::{FdtWriter, FdtWriterResult}; use vm_memory::{Address, Bytes, GuestMemory, GuestMemoryError, GuestMemoryRegion}; diff --git a/arch/src/lib.rs b/arch/src/lib.rs index 36fa20f13..a7c0ffdca 100644 --- a/arch/src/lib.rs +++ b/arch/src/lib.rs @@ -8,9 +8,6 @@ //! Implements platform specific functionality. //! Supported platforms: x86_64, aarch64, riscv64. -#[macro_use] -extern crate log; - use std::collections::BTreeMap; use std::sync::Arc; use std::{fmt, result}; diff --git a/arch/src/riscv64/fdt.rs b/arch/src/riscv64/fdt.rs index b30d2ce19..9ad66d367 100644 --- a/arch/src/riscv64/fdt.rs +++ b/arch/src/riscv64/fdt.rs @@ -15,6 +15,7 @@ use std::{cmp, result, str}; use byteorder::{BigEndian, ByteOrder}; use hypervisor::arch::riscv64::aia::Vaia; +use log::debug; use thiserror::Error; use vm_fdt::{FdtWriter, FdtWriterResult}; use vm_memory::{Address, Bytes, GuestMemory, GuestMemoryError, GuestMemoryRegion}; diff --git a/arch/src/x86_64/mod.rs b/arch/src/x86_64/mod.rs index 51c2d02f0..e2cb1eab2 100644 --- a/arch/src/x86_64/mod.rs +++ b/arch/src/x86_64/mod.rs @@ -6,11 +6,19 @@ // Portions Copyright 2017 The Chromium OS Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE-BSD-3-Clause file. + pub mod interrupts; pub mod layout; +pub mod regs; + +#[cfg(feature = "tdx")] +pub mod tdx; + mod mpspec; mod mptable; -pub mod regs; +mod smbios; + +use std::arch::x86_64; use std::mem; use hypervisor::arch::x86::{CPUID_FLAG_VALID_INDEX, CpuIdEntry}; @@ -19,6 +27,7 @@ use linux_loader::loader::bootparam::{boot_params, setup_header}; use linux_loader::loader::elf::start_info::{ hvm_memmap_table_entry, hvm_modlist_entry, hvm_start_info, }; +use log::{debug, error, info}; use thiserror::Error; use vm_memory::{ Address, Bytes, GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryAtomic, @@ -26,10 +35,6 @@ use vm_memory::{ }; use crate::{GuestMemoryMmap, InitramfsConfig, RegionType}; -mod smbios; -use std::arch::x86_64; -#[cfg(feature = "tdx")] -pub mod tdx; // While modern architectures support more than 255 CPUs via x2APIC, // legacy devices such as mptable support at most 254 CPUs. diff --git a/arch/src/x86_64/mptable.rs b/arch/src/x86_64/mptable.rs index a0e6dc47e..c31c9e411 100644 --- a/arch/src/x86_64/mptable.rs +++ b/arch/src/x86_64/mptable.rs @@ -8,6 +8,7 @@ use std::{mem, result, slice}; use libc::c_uchar; +use log::{info, warn}; use thiserror::Error; use vm_memory::{Address, ByteValued, Bytes, GuestAddress, GuestMemory, GuestMemoryError}; diff --git a/arch/src/x86_64/tdx/mod.rs b/arch/src/x86_64/tdx/mod.rs index 4ce1aa9a0..a6ce96aae 100644 --- a/arch/src/x86_64/tdx/mod.rs +++ b/arch/src/x86_64/tdx/mod.rs @@ -5,6 +5,7 @@ use std::fs::File; use std::io::{Read, Seek, SeekFrom}; use std::str::FromStr; +use log::{debug, info}; use thiserror::Error; use uuid::Uuid; use vm_memory::{ByteValued, Bytes, GuestAddress, GuestMemoryError};