cmake_package/lib.rs
1// SPDX-FileCopyrightText: 2024 Daniel Vrátil <dvratil@kde.org>
2//
3// SPDX-License-Identifier: MIT
4
5//! A simple CMake package finder.
6//!
7//! This crate is intended to be used in [cargo build scripts][cargo_build_script] to obtain
8//! information about and existing system [CMake package][cmake_package] and [CMake targets][cmake_target]
9//! defined in the package, such as include directories and link libraries for individual
10//! CMake targets defined in the package.
11//!
12//! The crate runs the `cmake` command in the background to query the system for the package
13//! and to extract the necessary information, which means that in order for this crate to work,
14//! the `cmake` executable must be located the system [`PATH`][wiki_path]. CMake version
15//! [3.19][CMAKE_MIN_VERSION] or newer is required for this crate to work.
16//!
17//! The entry point for the crate is the [`find_package()`] function that returns a builder,
18//! which you can use to specify further constraints on the package ([version][FindPackageBuilder::version]
19//! or [components][FindPackageBuilder::components]). Once you call the [`find()`][FindPackageBuilder::find]
20//! method on the builder, the crate will try to find the package on the system or return an
21//! error. If the package is found, an instance of the [`CMakePackage`] struct is returned that
22//! contains information about the package. Using its [`target()`][CMakePackage::target] method,
23//! you can query information about individual CMake targets defined in the package.
24//!
25//! If you want to make your dependency on CMake optional, you can use the [`find_cmake()`]
26//! function to check that a suitable version of CMake is found on the system and then decide
27//! how to proceed yourself. It is not necessary to call the function before using [`find_package()`].
28//!
29//! # Example
30//! ```no_run
31//! use cmake_package::find_package;
32//!
33//! let package = find_package("OpenSSL").version("1.0").find();
34//! let target = match package {
35//! Err(_) => panic!("OpenSSL>=1.0 not found"),
36//! Ok(package) => {
37//! package.target("OpenSSL::SSL").unwrap()
38//! }
39//! };
40//!
41//! println!("Include directories: {:?}", target.include_directories);
42//! target.link();
43//! ```
44//!
45//! # How Does it Work?
46//!
47//! When you call [`FindPackageBuilder::find()`], the crate will create a temporary directory
48//! with a `CMakeLists.txt` file that contains actual [`find_package()`][cmake_find_package]
49//! command to search for the package. The crate will then run actual `cmake` command in the
50//! temporary directory to let CMake find the package. The `CMakeLists.txt` then writes the
51//! information about the package into a JSON file that is then read by this crate to produce
52//! the [`CMakePackage`].
53//!
54//! When a target is queried using the [`CMakePackage::target()`] method, the crate runs the
55//! CMake command again the same directory, but this time the `CMakeLists.txt` attempts to locate
56//! the specified CMake target and list all its (relevant) properties and properties of all its
57//! transitive dependencies. The result is again written into a JSON file that is then processed
58//! by the crate to produce the [`CMakeTarget`] instance.
59//!
60//! # Known Limitations
61//!
62//! The crate currently supports primarily linking against shared libraries. Linking against
63//! static libraries is not tested and may not work as expected. The crate currently does not
64//! support linking against MacOS frameworks.
65//!
66//! [CMake generator expressions][cmake_generator_expr] are not supported in property values
67//! right now, because they are evaluated at later stage of the build, not during the "configure"
68//! phase of CMake, which is what this crate does. Some generator expressions could be supported
69//! by the crate in the future (e.g. by evaluating them ourselves).
70//!
71//! There's currently no way to customize the `CMakeLists.txt` file that is used to query the
72//! package or the target in order to extract non-standard properties or variables set by
73//! the CMake package. This may be addressed in the future.
74//!
75//! [wiki_path]: https://en.wikipedia.org/wiki/PATH_(variable)
76//! [cmake_package]: https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html
77//! [cmake_target]: https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#target-build-specification
78//! [cargo_build_script]: https://doc.rust-lang.org/cargo/reference/build-scripts.html
79//! [cmake_find_package]: https://cmake.org/cmake/help/latest/command/find_package.html
80//! [cmake_generator_expr]: https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html
81
82use std::io::Write;
83use std::path::PathBuf;
84
85#[cfg(any(target_os = "linux", target_os = "macos"))]
86use once_cell::sync::Lazy;
87#[cfg(any(target_os = "linux", target_os = "macos"))]
88use regex::Regex;
89use tempfile::TempDir;
90
91mod cmake;
92mod version;
93
94pub use cmake::{find_cmake, CMakeProgram, Error, CMAKE_MIN_VERSION};
95pub use version::{Version, VersionError};
96
97/// A CMake package found on the system.
98///
99/// Represents a CMake package found on the system. To find a package, use the [`find_package()`] function.
100/// The package can be queried for information about its individual CMake targets by [`CMakePackage::target()`].
101///
102/// # Example
103/// ```no_run
104/// use cmake_package::{CMakePackage, find_package};
105///
106/// let package: CMakePackage = find_package("OpenSSL").version("1.0").find().unwrap();
107/// ```
108#[derive(Debug)]
109pub struct CMakePackage {
110 cmake: CMakeProgram,
111 working_directory: TempDir,
112 verbose: bool,
113
114 /// Name of the CMake package
115 pub name: String,
116 /// Version of the package found on the system
117 pub version: Option<Version>,
118 /// Components of the package, as requested by the user in [`find_package()`]
119 pub components: Option<Vec<String>>,
120 /// Alternative names for the package, as requested by the user in [`find_package()`]
121 pub names: Option<Vec<String>>,
122}
123
124impl CMakePackage {
125 fn new(
126 cmake: CMakeProgram,
127 working_directory: TempDir,
128 name: String,
129 version: Option<Version>,
130 components: Option<Vec<String>>,
131 names: Option<Vec<String>>,
132 verbose: bool,
133 ) -> Self {
134 Self {
135 cmake,
136 working_directory,
137 name,
138 version,
139 components,
140 names,
141 verbose,
142 }
143 }
144
145 /// Queries the CMake package for information about a specific [CMake target][cmake_target].
146 /// Returns `None` if the target is not found in the package.
147 ///
148 /// [cmake_target]: https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#imported-targets
149 pub fn target(&self, target: impl Into<String>) -> Option<CMakeTarget> {
150 cmake::find_target(self, target)
151 }
152
153 /// Queries the CMake target for the value of a specific property.
154 ///
155 /// Returns `None` if the property is not defined on the target.
156 ///
157 /// This is equivalent to calling [`get_target_property()`][cmake_get_target_property] in CMake.
158 ///
159 /// [cmake_get_target_property]: https://cmake.org/cmake/help/latest/command/get_target_property.html
160 pub fn target_property(
161 &self,
162 target: &CMakeTarget,
163 property: impl Into<String>,
164 ) -> Option<String> {
165 cmake::target_property(self, target, property)
166 }
167}
168
169/// Describes a CMake target found in a CMake package.
170///
171/// The target can be obtained by calling the [`target()`][CMakePackage::target()] method on a [`CMakePackage`] instance.
172///
173/// Use [`link()`][Self::link()] method to instruct cargo to link the final binary against the target.
174/// There's currently no way to automatically apply compiler arguments or include directories, since
175/// that depends on how the C/C++ code in your project is compiled (e.g. using the [cc][cc_crate] crate).
176/// Optional support for this may be added in the future.
177///
178/// # Example
179/// ```no_run
180/// use cmake_package::find_package;
181///
182/// let package = find_package("OpenSSL").version("1.0").find().unwrap();
183/// let target = package.target("OpenSSL::SSL").unwrap();
184/// println!("Include directories: {:?}", target.include_directories);
185/// println!("Link libraries: {:?}", target.link_libraries);
186/// target.link();
187/// ```
188///
189/// [cc_crate]: https://crates.io/crates/cc
190#[derive(Debug, Default, Clone)]
191pub struct CMakeTarget {
192 /// Name of the CMake target
193 pub name: String,
194 /// List of public compile definitions requirements for a library.
195 ///
196 /// Contains preprocessor definitions provided by the target and all its transitive dependencies
197 /// via their [`INTERFACE_COMPILE_DEFINITIONS`][cmake_interface_compile_definitions] target properties.
198 ///
199 /// [cmake_interface_compile_definitions]: https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_COMPILE_DEFINITIONS.html
200 pub compile_definitions: Vec<String>,
201 /// List of options to pass to the compiler.
202 ///
203 /// Contains compiler options provided by the target and all its transitive dependencies via
204 /// their [`INTERFACE_COMPILE_OPTIONS`][cmake_interface_compile_options] target properties.
205 ///
206 /// [cmake_interface_compile_options]: https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_COMPILE_OPTIONS.html
207 pub compile_options: Vec<String>,
208 /// List of include directories required to build the target.
209 ///
210 /// Contains include directories provided by the target and all its transitive dependencies via
211 /// their [`INTERFACE_INCLUDE_DIRECTORIES`][cmake_interface_include_directories] target properties.
212 ///
213 /// [cmake_interface_include_directories]: https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_INCLUDE_DIRECTORIES.html
214 pub include_directories: Vec<String>,
215 /// List of directories to use for the link step of shared library, module and executable targets.
216 ///
217 /// Contains link directories provided by the target and all its transitive dependencies via
218 /// their [`INTERFACE_LINK_DIRECTORIES`][cmake_interface_link_directories] target properties.
219 ///
220 /// [cmake_interface_link_directories]: https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_LINK_DIRECTORIES.html
221 pub link_directories: Vec<String>,
222 /// List of target's direct link dependencies, followed by indirect dependencies from the transitive closure of the direct
223 /// dependencies' [`INTERFACE_LINK_LIBRARIES`][cmake_interface_link_libraries] properties
224 ///
225 /// [cmake_interface_link_libraries]: https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_LINK_LIBRARIES.html
226 pub link_libraries: Vec<String>,
227 /// List of options to use for the link step of shared library, module and executable targets as well as the device link step.
228 ///
229 /// Contains link options provided by the target and all its transitive dependencies via
230 /// their [`INTERFACE_LINK_OPTIONS`][cmake_interface_link_options] target properties.
231 ///
232 /// [cmake_interface_link_options]: https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_LINK_OPTIONS.html
233 pub link_options: Vec<String>,
234 /// The location of the target on disk.
235 ///
236 /// [cmake_interface_location]: https://cmake.org/cmake/help/latest/prop_tgt/LOCATION.html
237 pub location: Option<String>,
238}
239
240/// Turns /usr/lib/libfoo.so.5 into foo, so that -lfoo rather than -l/usr/lib/libfoo.so.5
241/// is passed to the linker. Leaves "foo" untouched.
242#[cfg(any(target_os = "linux", target_os = "macos"))]
243fn link_name(lib: &str) -> &str {
244 static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"lib([^/]+)\.(?:so|dylib|a).*").unwrap());
245 match RE.captures(lib) {
246 Some(captures) => captures.get(1).map(|f| f.as_str()).unwrap_or(lib),
247 None => lib,
248 }
249}
250
251#[cfg(target_os = "windows")]
252fn link_name(lib: &str) -> &str {
253 lib
254}
255
256#[cfg(any(target_os = "linux", target_os = "macos"))]
257fn link_dir(lib: &str) -> Option<&str> {
258 static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(.*)/lib[^/]+\.so.*").unwrap());
259 RE.captures(lib)?.get(1).map(|f| f.as_str())
260}
261
262#[cfg(target_os = "windows")]
263fn link_dir(_lib: &str) -> Option<&str> {
264 None
265}
266
267impl CMakeTarget {
268 /// Instructs cargo to link the final binary against the target.
269 ///
270 /// This method prints the necessary [`cargo:rustc-link-search=native={}`][cargo_rustc_link_search],
271 /// [`cargo:rustc-link-arg={}`][cargo_rustc_link_arg], and [`cargo:rustc-link-lib=dylib={}`][cargo_rustc_link_lib]
272 /// directives to the standard output for each of the target's [`link_directories`][Self::link_directories],
273 /// [`link_options`][Self::link_options], and [`link_libraries`][Self::link_libraries] respectively.
274 ///
275 /// [cargo_rustc_link_search]: https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-link-search
276 /// [cargo_rustc_link_arg]: https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-link-arg
277 /// [cargo_rustc_link_lib]: https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-link-lib]
278 pub fn link(&self) {
279 self.link_write(&mut std::io::stdout());
280 }
281
282 fn link_write<W: Write>(&self, io: &mut W) {
283 self.link_directories.iter().for_each(|dir| {
284 writeln!(io, "cargo:rustc-link-search=native={}", dir).unwrap();
285 });
286 self.link_options.iter().for_each(|opt| {
287 writeln!(io, "cargo:rustc-link-arg={}", opt).unwrap();
288 });
289 self.link_libraries.iter().for_each(|lib| {
290 if lib.starts_with("-") {
291 writeln!(io, "cargo:rustc-link-arg={}", lib).unwrap();
292 } else {
293 writeln!(io, "cargo:rustc-link-lib=dylib={}", link_name(lib)).unwrap();
294 }
295
296 if let Some(lib) = link_dir(lib) {
297 writeln!(io, "cargo:rustc-link-search=native={}", lib).unwrap();
298 }
299 });
300 }
301}
302
303/// A builder for creating a [`CMakePackage`] instance. An instance of the builder is created by calling
304/// the [`find_package()`] function. Once the package is configured, [`FindPackageBuilder::find()`] will actually
305/// try to find the CMake package and return a [`CMakePackage`] instance (or error if the package is not found
306/// or an error occurs during the search).
307#[derive(Debug, Clone)]
308pub struct FindPackageBuilder {
309 name: String,
310 version: Option<Version>,
311 components: Option<Vec<String>>,
312 names: Option<Vec<String>>,
313 verbose: bool,
314 prefix_paths: Option<Vec<PathBuf>>,
315}
316
317impl FindPackageBuilder {
318 fn new(name: String) -> Self {
319 Self {
320 name,
321 version: None,
322 components: None,
323 names: None,
324 verbose: false,
325 prefix_paths: None,
326 }
327 }
328
329 /// Optionally specifies the minimum required version for the package to find.
330 /// If the package is not found or the version is too low, the `find()` method will return
331 /// [`Error::Version`] with the version of the package found on the system.
332 pub fn version(self, version: impl TryInto<Version>) -> Self {
333 Self {
334 version: Some(
335 version
336 .try_into()
337 .unwrap_or_else(|_| panic!("Invalid version specified!")),
338 ),
339 ..self
340 }
341 }
342
343 /// Optionally specifies the required components to locate in the package.
344 /// If the package is found, but any of the components is missing, the package is considered
345 /// as not found and the `find()` method will return [`Error::PackageNotFound`].
346 /// See the documentation on CMake's [`find_package()`][cmake_find_package] function and how it
347 /// treats the `COMPONENTS` argument.
348 ///
349 /// [cmake_find_package]: https://cmake.org/cmake/help/latest/command/find_package.html
350 pub fn components(self, components: impl Into<Vec<String>>) -> Self {
351 Self {
352 components: Some(components.into()),
353 ..self
354 }
355 }
356
357 /// Optionally specifies alternative package names to search for.
358 /// See the documentation on CMake's [`find_package()`][cmake_find_package] function
359 /// and how it treats the `NAMES` argument.
360 ///
361 /// [cmake_find_package]: https://cmake.org/cmake/help/latest/command/find_package.html
362 pub fn names<S, I>(self, names: I) -> Self
363 where
364 S: Into<String>,
365 I: IntoIterator<Item = S>,
366 {
367 let names: Vec<_> = names.into_iter().map(Into::into).collect();
368 if names.is_empty() {
369 return self;
370 }
371
372 Self {
373 names: Some(names),
374 ..self
375 }
376 }
377
378 /// Enable verbose output.
379 /// This will redirect output from actual execution of the `cmake` command to the standard output
380 /// and standard error of the build script.
381 pub fn verbose(self) -> Self {
382 Self {
383 verbose: true,
384 ..self
385 }
386 }
387
388 // Specify prefix paths.
389 // This sets directories to be searched for the package.
390 // [cmake_prefix_path]: https://cmake.org/cmake/help/latest/variable/CMAKE_PREFIX_PATH.html
391 pub fn prefix_paths(self, prefix_paths: Vec<PathBuf>) -> Self {
392 Self {
393 prefix_paths: Some(prefix_paths),
394 ..self
395 }
396 }
397
398 /// Tries to find the CMake package on the system.
399 /// Returns a [`CMakePackage`] instance if the package is found, otherwise an error.
400 pub fn find(self) -> Result<CMakePackage, cmake::Error> {
401 cmake::find_package(
402 self.name,
403 self.version,
404 self.components,
405 self.names,
406 self.verbose,
407 self.prefix_paths,
408 )
409 }
410}
411
412/// Find a CMake package on the system.
413///
414/// This function is the main entrypoint for the crate. It returns a builder object that you
415/// can use to specify further constraints on the package to find, such as the [version][FindPackageBuilder::version]
416/// or [components][FindPackageBuilder::components]. Once you call the [`find()`][FindPackageBuilder::find]
417/// method on the builder, the crate will try to find the package on the system or return an
418/// error if the package does not exist or does not satisfy some of the constraints. If the package
419/// is found, an instance of the [`CMakePackage`] struct is returned that can be used to further
420/// query the package for information about its individual CMake targets.
421///
422/// See the documentation for [`FindPackageBuilder`], [`CMakePackage`], and [`CMakeTarget`] for more
423/// information and the example in the crate documentation for a simple usage example.
424pub fn find_package(name: impl Into<String>) -> FindPackageBuilder {
425 FindPackageBuilder::new(name.into())
426}
427
428#[cfg(test)]
429mod testing {
430 use super::*;
431
432 // Note: requires cmake to be installed on the system
433 #[test]
434 fn test_find_package() {
435 let package = find_package("totallynonexistentpackage").find();
436 match package {
437 Ok(_) => panic!("Package should not be found"),
438 Err(cmake::Error::PackageNotFound) => (),
439 Err(err) => panic!("Unexpected error: {:?}", err),
440 }
441 }
442
443 // Note: requires cmake to be installed on the system
444 #[test]
445 fn test_find_package_with_version() {
446 let package = find_package("foo").version("1.0").find();
447 match package {
448 Ok(_) => panic!("Package should not be found"),
449 Err(cmake::Error::PackageNotFound) => (),
450 Err(err) => panic!("Unexpected error: {:?}", err),
451 }
452 }
453
454 #[test]
455 #[cfg(any(target_os = "linux", target_os = "macos"))]
456 fn test_link_to() {
457 let target = CMakeTarget {
458 name: "foo".into(),
459 compile_definitions: vec![],
460 compile_options: vec![],
461 include_directories: vec![],
462 link_directories: vec!["/usr/lib64".into()],
463 link_libraries: vec!["/usr/lib/libbar.so".into(), "/usr/lib64/libfoo.so.5".into()],
464 link_options: vec![],
465 location: None,
466 };
467
468 let mut buf = Vec::new();
469 target.link_write(&mut buf);
470 let output = String::from_utf8(buf).unwrap();
471 assert_eq!(
472 output.lines().collect::<Vec<&str>>(),
473 vec![
474 "cargo:rustc-link-search=native=/usr/lib64",
475 "cargo:rustc-link-lib=dylib=bar",
476 "cargo:rustc-link-search=native=/usr/lib",
477 "cargo:rustc-link-lib=dylib=foo",
478 "cargo:rustc-link-search=native=/usr/lib64"
479 ]
480 );
481 }
482}