From 095239ce2b217854a6df3e56aca3b5f29b56aeb9 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Wed, 20 Oct 2021 12:58:48 +0200 Subject: [PATCH] Add datadir support --- src/build.rs | 20 +++++++++++++++++++- src/build_targets.rs | 2 ++ src/cli.rs | 7 +++++++ src/install.rs | 21 +++++++++++++++++++++ src/pkg_config_gen.rs | 4 +--- 5 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/build.rs b/src/build.rs index 7a83e96c..a17d27fa 100644 --- a/src/build.rs +++ b/src/build.rs @@ -410,9 +410,10 @@ pub struct LibraryCApiConfig { pub rustflags: Vec, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct InstallCApiConfig { pub include: Vec, + pub data: Vec, } #[derive(Debug)] @@ -696,10 +697,27 @@ fn load_manifest_capi_config(pkg: &Package) -> anyhow::Result { } } } + // TODO: Add data paths customizations } + let default_assets_data = InstallTargetPaths { + from: "assets/capi/share/**/*".to_string(), + to: name.clone(), + }; + + let default_generated_data = InstallTargetPaths { + from: "capi/share/**/*".to_string(), + to: name.clone(), + }; + + let data_targets = vec![ + InstallTarget::Asset(default_assets_data), + InstallTarget::Generated(default_generated_data), + ]; + let install = InstallCApiConfig { include: include_targets, + data: data_targets, }; Ok(CApiConfig { diff --git a/src/build_targets.rs b/src/build_targets.rs index 99d920c6..c0e5240e 100644 --- a/src/build_targets.rs +++ b/src/build_targets.rs @@ -6,6 +6,7 @@ use crate::target::Target; #[derive(Debug, Default, Clone)] pub struct ExtraTargets { pub include: Vec<(PathBuf, PathBuf)>, + pub data: Vec<(PathBuf, PathBuf)>, } impl ExtraTargets { @@ -16,6 +17,7 @@ impl ExtraTargets { out_dir: Option<&Path>, ) -> anyhow::Result<()> { self.include = extra_targets(&capi_config.install.include, root_dir, out_dir)?; + self.data = extra_targets(&capi_config.install.data, root_dir, out_dir)?; Ok(()) } diff --git a/src/cli.rs b/src/cli.rs index 99c45cf5..7ca3e70d 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -30,6 +30,13 @@ struct Common { /// Path to directory for installing generated pkg-config .pc files #[structopt(long = "pkgconfigdir", parse(from_os_str))] pkgconfigdir: Option, + /// Path to directory for installing read-only data (defaults to {prefix}/share) + #[structopt(long = "datarootdir", parse(from_os_str))] + datarootdir: Option, + /// Path to directory for installing read-only application-specific data (defaults to + /// {datarootdir}/{crate-name}) + #[structopt(long = "datadir", parse(from_os_str))] + datadir: Option, #[structopt(long = "dlltool", parse(from_os_str))] /// Use the provided dlltool when building for the windows-gnu targets. dlltool: Option, diff --git a/src/install.rs b/src/install.rs index 4e313195..c5fd95a1 100644 --- a/src/install.rs +++ b/src/install.rs @@ -175,6 +175,7 @@ pub fn cinstall(ws: &Workspace, packages: &[CPackage]) -> anyhow::Result<()> { let install_path_lib = append_to_destdir(destdir, &install_path_lib); let install_path_pc = append_to_destdir(destdir, &paths.pkgconfigdir); let install_path_include = append_to_destdir(destdir, &paths.includedir); + let install_path_data = append_to_destdir(destdir, &paths.datadir); create_dir_all(&install_path_lib)?; create_dir_all(&install_path_pc)?; @@ -197,6 +198,15 @@ pub fn cinstall(ws: &Workspace, packages: &[CPackage]) -> anyhow::Result<()> { } } + if !build_targets.extra.data.is_empty() { + ws.config().shell().status("Installing", "data file")?; + for (from, to) in build_targets.extra.data.iter() { + let to = install_path_data.join(to); + create_dir_all(to.parent().unwrap())?; + copy(from, to)?; + } + } + if let Some(ref static_lib) = build_targets.static_lib { ws.config().shell().status("Installing", "static library")?; copy( @@ -243,6 +253,7 @@ pub struct InstallPaths { pub prefix: PathBuf, pub libdir: PathBuf, pub includedir: PathBuf, + pub datadir: PathBuf, pub bindir: PathBuf, pub pkgconfigdir: PathBuf, } @@ -265,6 +276,15 @@ impl InstallPaths { .value_of("includedir") .map(PathBuf::from) .unwrap_or_else(|| prefix.join("include")); + let datarootdir = args + .value_of("datarootdir") + .map(PathBuf::from) + .unwrap_or_else(|| prefix.join("share")); + let datadir = args + .value_of("datadir") + .map(PathBuf::from) + .unwrap_or_else(|| datarootdir.clone()); + let subdir_name = PathBuf::from(&capi_config.header.subdirectory); let bindir = args @@ -282,6 +302,7 @@ impl InstallPaths { prefix, libdir, includedir, + datadir, bindir, pkgconfigdir, } diff --git a/src/pkg_config_gen.rs b/src/pkg_config_gen.rs index 14d620d0..274de9bb 100644 --- a/src/pkg_config_gen.rs +++ b/src/pkg_config_gen.rs @@ -287,9 +287,7 @@ mod test { versioning: true, rustflags: Vec::default(), }, - install: crate::build::InstallCApiConfig { - include: Vec::new(), - }, + install: Default::default(), }, ); pkg.add_lib("-lbar").add_cflag("-DFOO");