kde_frameworks/lib.rs
1//! # Introduction
2//! This crate serves as the base for all the KDE Frameworks Crate written by me. It sets important
3//! environment variables and provides helpful methods required in most KDE Frameworks.
4//!
5//! Currently, mostly supposed to be used as a build dependency.
6//!
7//! # Install kf5-config
8//! ## Ubuntu
9//! ```sh
10//! sudo apt install libkf5kdelibs4support5-bin
11//! ```
12//! ## Fedroa
13//! ```sh
14//! sudo dnf install kf5-kdelibs4support
15//! ```
16//!
17//! # Environment Variables Read By this Crate
18//! - It is optional to provide these variables. If these variables are not present, then `kf5-config`
19//! must be present in the path.
20//! 1. `KF_LIBRARY_PATH` : Path for KDE Frameworks Library Location.
21//! 2. `KF_INCLUDE_PATH` : Path for KDE Frameworks Header Files Location.
22//! 3. `KF_VERSION` : KDE Frameworks Version. Format `<major>.<minor>.<patch>`.
23//!
24//! # Environment Variables Set By this Crate
25//! 1. `KF_LIBRARY_PATH` : Path for KDE Frameworks Library Location.
26//! 2. `KF_INCLUDE_PATH` : Path for KDE Frameworks Header Files Location.
27//! 3. `KF_VERSION` : KDE Frameworks Version Detected.
28//! 4. `KF_FOUND` : Flag to specify if KDE Frameworks is found or not.
29
30pub mod helpers;
31pub mod kf5_config;
32
33use semver::Version;
34use std::path::{Path, PathBuf};
35
36/// Function to link a KDE Library.
37/// Eg: For linking `KI18n`:
38/// ```no_run
39/// use kde_frameworks::link_lib;
40///
41/// link_lib("I18n");
42/// ```
43pub fn link_lib(lib: &str, major_version: u64) -> Result<(), semver::Error> {
44 let lib_name = helpers::get_lib_name(lib, major_version);
45 println!("cargo:rustc-link-lib={}", lib_name?);
46 Ok(())
47}
48
49/// Function to Return the Include Path for the KDE Library.
50/// This is the path where the header files for the Library are located.
51/// ```no_run
52/// use kde_frameworks::get_lib_include_path;
53///
54/// get_lib_include_path("I18n");
55/// ```
56pub fn get_lib_include_path(
57 lib: &str,
58 major_version: u64,
59 include_path: &Path,
60) -> Result<PathBuf, semver::Error> {
61 Ok(include_path
62 .join(format!("KF{}", major_version))
63 .join(format!("K{}", lib)))
64}
65
66/// Function to have Conditional Compilation based on KDE Frameworks version.
67pub fn set_version_cfg(version: Version, least_minor_version: u64) {
68 let mut minor = least_minor_version;
69 while version >= Version::new(5, minor, 0) {
70 println!("cargo:rustc-cfg=kf_{}_{}", 5, minor);
71 minor += 1;
72 }
73}
74
75/// Function to check if Version, Include Path and Library Path Environment variables have been set
76pub fn check_env_variables(library_name: &str) -> Option<(Version, PathBuf, PathBuf)> {
77 println!("cargo:rerun-if-env-changed={}_VERSION", library_name);
78 println!("cargo:rerun-if-env-changed={}_INCLUDE_PATH", library_name);
79 println!("cargo:rerun-if-env-changed={}_LIBRARY_PATH", library_name);
80
81 match (
82 std::env::var(format!("{}_VERSION", library_name)).ok(),
83 std::env::var(format!("{}_INCLUDE_PATH", library_name)).ok(),
84 std::env::var(format!("{}_LIBRARY_PATH", library_name)).ok(),
85 ) {
86 (Some(version), Some(include_path), Some(library_path)) => {
87 let v = version.parse().ok()?;
88 Some((v, PathBuf::from(include_path), PathBuf::from(library_path)))
89 }
90 _ => None,
91 }
92}
93
94/// Function to probe KDE using kf5-config tool.
95pub fn check_kf5_config() -> Result<(Version, PathBuf, PathBuf), &'static str> {
96 Ok((
97 kf5_config::get_version()?,
98 kf5_config::get_include_path()?,
99 kf5_config::get_library_path()?,
100 ))
101}