Skip to content

Commit 89054d4

Browse files
committed
libbpf-cargo: Clean up crate-structure mess
It goes against every Cargo project preconception to have certain modules be compiled from both lib.rs and main.rs and it leads to shenanigans such as #[allow(dead_code)] tags scattered all over the place, which is its own can of worms when done at such a high level. Do it properly and export necessary functionality from the library, for consumption by the binary. To prevent any misconceptions of there being a stable API contract for functionality *only* being used by the binary, put it into an hidden module. Signed-off-by: Daniel Müller <deso@posteo.net>
1 parent d8d13eb commit 89054d4

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

libbpf-cargo/src/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ fn extract_clang_or_default(clang: Option<&PathBuf>) -> PathBuf {
279279
}
280280

281281
#[allow(clippy::too_many_arguments)]
282-
pub(crate) fn build(
282+
pub fn build(
283283
debug: bool,
284284
manifest_path: Option<&PathBuf>,
285285
clang: Option<&PathBuf>,

libbpf-cargo/src/gen/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub mod btf;
1+
mod btf;
22

33
use std::borrow::Cow;
44
use std::collections::BTreeMap;
@@ -1352,7 +1352,7 @@ fn gen_project(
13521352
Ok(())
13531353
}
13541354

1355-
pub(crate) fn gen(
1355+
pub fn gen(
13561356
debug: bool,
13571357
manifest_path: Option<&PathBuf>,
13581358
rustfmt_path: Option<&PathBuf>,

libbpf-cargo/src/lib.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,15 @@ use anyhow::Result;
7777
use tempfile::tempdir;
7878
use tempfile::TempDir;
7979

80-
// libbpf-cargo binary is the primary consumer of the following modules. As such,
81-
// we do not use all the symbols. Silence any unused code warnings.
82-
#[allow(dead_code)]
8380
mod build;
84-
#[allow(dead_code)]
8581
mod gen;
86-
#[allow(dead_code)]
8782
mod make;
88-
#[allow(dead_code)]
8983
mod metadata;
9084

9185
#[cfg(test)]
9286
mod test;
9387

88+
9489
/// `SkeletonBuilder` builds and generates a single skeleton.
9590
///
9691
/// This interface is meant to be used in build scripts.
@@ -285,3 +280,20 @@ impl SkeletonBuilder {
285280
Ok(())
286281
}
287282
}
283+
284+
285+
/// Implementation details shared with the binary.
286+
///
287+
/// NOT PART OF PUBLIC API SURFACE!
288+
#[doc(hidden)]
289+
pub mod __private {
290+
pub mod build {
291+
pub use crate::build::build;
292+
}
293+
pub mod gen {
294+
pub use crate::gen::gen;
295+
}
296+
pub mod make {
297+
pub use crate::make::make;
298+
}
299+
}

libbpf-cargo/src/main.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ use clap::Args;
99
use clap::Parser;
1010
use clap::Subcommand;
1111

12-
#[doc(hidden)]
13-
mod build;
14-
mod gen;
15-
mod make;
16-
mod metadata;
12+
use libbpf_cargo::__private::build;
13+
use libbpf_cargo::__private::gen;
14+
use libbpf_cargo::__private::make;
15+
1716

1817
#[doc(hidden)]
1918
#[derive(Debug, Parser)]

libbpf-cargo/src/metadata.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct PackageMetadata {
2424
}
2525

2626
#[derive(Debug, Clone)]
27-
pub struct UnprocessedObj {
27+
pub(crate) struct UnprocessedObj {
2828
/// Package the object belongs to
2929
pub package: String,
3030
/// Path to .c
@@ -142,7 +142,10 @@ fn get_package(
142142
}
143143

144144
/// Returns the `target_directory` and a list of objects to compile.
145-
pub fn get(debug: bool, manifest_path: Option<&PathBuf>) -> Result<(PathBuf, Vec<UnprocessedObj>)> {
145+
pub(crate) fn get(
146+
debug: bool,
147+
manifest_path: Option<&PathBuf>,
148+
) -> Result<(PathBuf, Vec<UnprocessedObj>)> {
146149
let mut cmd = MetadataCommand::new();
147150

148151
if let Some(path) = manifest_path {

0 commit comments

Comments
 (0)