cmake_file_api/
lib.rs

1//! Library for interacting with the [cmake-file-api](https://cmake.org/cmake/help/latest/manual/cmake-file-api.7.html)
2//! - Writing queries
3//! - Reading replies
4//!
5//! # Example
6//!
7//! Build query and parse cmake-file-api
8//!
9//! ```no_run
10//! # use std::error::Error;
11//! # use std::path::Path;
12//! use cmake_file_api::{query, reply, objects};
13//!
14//! #
15//! # fn try_main() -> Result<(), Box<dyn Error>> {
16//! #     let source_dir = Path::new(".");
17//! #     let build_dir = Path::new(".");
18//!
19//! // generate query
20//! query::Writer::default()
21//!   .request_object::<objects::CodeModelV2>()
22//!   .write_stateless(&build_dir)?;
23//!
24//! // run cmake
25//! assert!(std::process::Command::new("cmake")
26//!   .arg("-S")
27//!   .arg(&source_dir)
28//!   .arg("-B")
29//!   .arg(&build_dir)
30//!   .status()?
31//!   .success());
32//!
33//! // parse cmake-file-api
34//! let reader = reply::Reader::from_build_dir(build_dir)?;
35//!
36//! // interact with api objects
37//! let codemodel: objects::CodeModelV2 = reader.read_object()?;
38//! for config in &codemodel.configurations{
39//!   for target in &config.targets {
40//!     println!("{}", target.name);
41//!     println!("{:#?}", target.sources)
42//!   }
43//! }
44//! #   Ok(())
45//! # }
46//! ```
47//!
48//! # cmake-file-api
49//! The `CMake File API` is a new feature in `CMake` 3.14 that provides a rich interface for querying `CMake's` configuration and project information.
50//! As the name suggests, the API is based on files, which are written to disk by `CMake` and read by client tools.
51//! `CMake` generates these files in a directory named `.cmake/api/v1` in the build directory. The API is versioned, and the current version is v1.
52//! The V1 API is a collection of JSON files that describe the configuration of the `CMake` project it always contains an `index-*.json` file which lists all available objects.
53//! The objects are also versioned on their own, e.g. `codemodel-v2.json`. `CMake` will generate the files on demand,
54//! and expects clients to first write a query file to the query directory `.cmake/api/v1/query` before configuration step.
55//! The query describes which objects the client is interested in. With stateful queries, the client can also provide additional client data which is available in the reply.
56//! The API is designed to be used by tools that need to interact with `CMake` (IDE) but can also be used for other tooling purposes e.g. generate `compile_commands.json`.
57//!
58//!
59//!
60
61#![forbid(unsafe_code)]
62#![forbid(clippy::panic)]
63#![forbid(clippy::shadow_reuse)]
64#![forbid(clippy::shadow_unrelated)]
65#![forbid(clippy::exhaustive_enums)]
66
67pub mod index;
68pub mod objects;
69pub mod query;
70pub mod reply;