Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cpp-tools committed Aug 8, 2024
1 parent ef40a38 commit 886e48d
Show file tree
Hide file tree
Showing 19 changed files with 152 additions and 84 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ unsafe_code = "forbid"
shadow_reuse = "forbid"
exhaustive_enums = "forbid"
panic = "forbid"
shadow_unrelated = "forbid"


4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Library for interacting with the [cmake-file-api](https://cmake.org/cmake/help/l
- Writing queries
- Reading replies

Dual-licensed under MIT or the [UNLICENSE](https://unlicense.org).

### Usage

Add this to your `Cargo.toml`:
Expand Down Expand Up @@ -45,7 +47,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let reader = reply::Reader::from_build_dir(build_dir)?;

// read and print codemodel-v2
let codemodel = reader.read_codemodel_v2()?;
let codemodel: objects::CodeModelV2 = reader.read_object()?;
codemodel.configurations.iter().for_each(|config| {
config.targets.iter().for_each(|target| {
println!("{}", target.name);
Expand Down
24 changes: 24 additions & 0 deletions UNLICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
2 changes: 1 addition & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
let reader = reply::Reader::from_build_dir(&build_dir)?;

// get the codemodel
let codemodel = reader.read_code_model_v2()?;
let codemodel: objects::CodeModelV2 = reader.read_object()?;

// print all source files and their compile flags
for target in &codemodel.configurations[0].targets {
Expand Down
2 changes: 1 addition & 1 deletion examples/example_from_readme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let reader = reply::Reader::from_build_dir(build_dir)?;

// read and print codemodel-v2
let codemodel = reader.read_code_model_v2()?;
let codemodel: objects::CodeModelV2 = reader.read_object()?;
codemodel.configurations.iter().for_each(|config| {
config.targets.iter().for_each(|target| {
println!("{}", target.name);
Expand Down
8 changes: 8 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::path::PathBuf;
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct Index {
/// information about the instance of `CMake` that generated the reply
pub cmake: CMake,
Expand All @@ -21,6 +22,7 @@ pub struct Index {
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct CMake {
pub version: CMakeVersion,
pub paths: CMakePaths,
Expand All @@ -31,6 +33,7 @@ pub struct CMake {
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct CMakeVersion {
/// specifying the major version component
pub major: i32,
Expand All @@ -55,6 +58,7 @@ pub struct CMakeVersion {
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct CMakePaths {
/// absolute path to cmake tool
pub cmake: PathBuf,
Expand All @@ -73,6 +77,7 @@ pub struct CMakePaths {
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct CMakeGenerator {
/// specifying whether the generator supports multiple output configurations
pub multi_config: bool,
Expand All @@ -88,6 +93,7 @@ pub struct CMakeGenerator {
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct ReplyFileReference {
/// specifying one of the Object Kinds
pub kind: ObjectKind,
Expand All @@ -101,6 +107,7 @@ pub struct ReplyFileReference {

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct Error {
pub error: String,
}
Expand All @@ -127,6 +134,7 @@ pub enum ReplyField {

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct QueryJson {
pub client: Option<Value>,
pub requests: Option<Value>,
Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
//! let reader = reply::Reader::from_build_dir(build_dir)?;
//!
//! // interact with api objects
//! let codemodel = reader.read_code_model_v2()?;
//! let codemodel: objects::CodeModelV2 = reader.read_object()?;
//! for config in &codemodel.configurations{
//! for target in &config.targets {
//! println!("{}", target.name);
Expand All @@ -55,6 +55,13 @@
//! 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.
//! 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`.
//!
//!
#![allow(clippy::implicit_return)]
#![allow(clippy::missing_inline_in_public_items)]
#![allow(clippy::question_mark_used)]
#![allow(clippy::missing_docs_in_private_items)]
#![allow(clippy::missing_trait_methods)]
#[allow(clippy::self_named_module_files)]

pub mod index;
pub mod objects;
Expand Down
18 changes: 17 additions & 1 deletion src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub use codemodel_v2::CodeModel as CodeModelV2;
pub use configure_log_v1::ConfigureLog as ConfigureLogV1;
pub use toolchains_v1::Toolchains as ToolchainsV1;

use crate::reply;

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
Expand All @@ -20,8 +22,8 @@ pub struct MajorMinor {
pub minor: u32,
}

#[non_exhaustive]
#[derive(Default, Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
#[non_exhaustive]
pub enum ObjectKind {
#[default]
#[serde(rename = "codemodel")]
Expand Down Expand Up @@ -52,4 +54,18 @@ impl ObjectKind {
pub trait Object {
fn kind() -> ObjectKind;
fn major() -> u32;

/// Resolve references in the object
///
/// Some objects contain references to other json files. This method is called after the object
/// is deserialized to resolve these references.
/// Currently only the codemodel-v2 object has references (targets, directories) that need to be resolved.
///
/// # Errors
///
/// `ReaderError::IO`: if an IO error occurs while reading the object file
/// `ReaderError::Parse`: if an error occurs while parsing the object file
fn resolve_references(&mut self, _: &reply::Reader) -> Result<(), reply::ReaderError> {
Ok(())
}
}
3 changes: 3 additions & 0 deletions src/objects/cache_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
/// These are the Variables stored in the persistent cache (CMakeCache.txt) for the build tree.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Cache {
/// Kind of the cache object
pub kind: ObjectKind,
Expand All @@ -19,6 +20,7 @@ pub struct Cache {
/// Entry in the cache
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Entry {
/// Name of the entry
pub name: String,
Expand All @@ -36,6 +38,7 @@ pub struct Entry {

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Property {
/// Name of the property
pub name: String,
Expand Down
3 changes: 3 additions & 0 deletions src/objects/cmake_files_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::path::PathBuf;
/// These include the CMakeLists.txt files as well as included .cmake files.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CMakeFiles {
/// Kind of the CMakeFiles object.
pub kind: ObjectKind,
Expand All @@ -22,6 +23,7 @@ pub struct CMakeFiles {

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Paths {
/// Absolute path to the top-level source directory, represented with forward slashes.
pub build: PathBuf,
Expand All @@ -32,6 +34,7 @@ pub struct Paths {

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Input {
/// path to an input file to CMake, represented with forward slashes.
/// If the file is inside the top-level source directory then the path is specified relative to that directory.
Expand Down
2 changes: 2 additions & 0 deletions src/objects/codemodel_v2/backtrace_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::path::PathBuf;
/// Its nodes are referenced from backtrace members elsewhere in the containing object.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct BacktraceGraph {
/// Backtrace nodes.
pub nodes: Vec<Node>,
Expand All @@ -23,6 +24,7 @@ pub struct BacktraceGraph {

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Node {
/// An unsigned integer 0-based index into the backtrace files array.
pub file: usize,
Expand Down
29 changes: 29 additions & 0 deletions src/objects/codemodel_v2/codemodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use crate::objects::codemodel_v2::{Directory, Target};
use crate::objects::{MajorMinor, Object, ObjectKind};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use crate::reply;

/// The codemodel object kind describes the build system structure as modeled by `CMake`.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CodeModel {
/// Kind of the codemodel object.
pub kind: ObjectKind,
Expand All @@ -26,6 +28,7 @@ pub struct CodeModel {

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CodemodelPaths {
/// Absolute path to the top-level source directory, represented with forward slashes.
pub build: PathBuf,
Expand All @@ -36,6 +39,7 @@ pub struct CodemodelPaths {

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Configuration {
/// A string specifying the name of the configuration, e.g. Debug.
pub name: String,
Expand Down Expand Up @@ -72,6 +76,7 @@ pub struct Configuration {

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DirectoryReference {
/// Path to the source directory, represented with forward slashes.
/// If the directory is inside the top-level source directory then the path is specified
Expand Down Expand Up @@ -121,6 +126,7 @@ pub struct DirectoryReference {

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct MinimumCmakeVersion {
/// A string specifying the minimum required version in the format
/// \<major\>.\<minor\>.\[\<patch\>\[.\<tweak\>]]\[\<suffix\>]
Expand All @@ -131,6 +137,7 @@ pub struct MinimumCmakeVersion {

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Project {
/// A string specifying the name given to the project() command.
pub name: String,
Expand Down Expand Up @@ -160,6 +167,7 @@ pub struct Project {

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TargetReference {
/// A string specifying the target name.
pub name: String,
Expand Down Expand Up @@ -188,6 +196,27 @@ impl Object for CodeModel {
fn major() -> u32 {
2
}

fn resolve_references(&mut self, reader: &reply::Reader) -> Result<(), reply::ReaderError> {
let reply_dir = reply::dir(reader.build_dir());

// resolve targets and directories references
for config in &mut self.configurations {
for target_ref in &config.target_refs {
config
.targets
.push(reply::Reader::parse_reply(reply_dir.join(&target_ref.json_file))?);
}

for directory_ref in &config.directory_refs {
config.directories.push(reply::Reader::parse_reply(
reply_dir.join(&directory_ref.json_file),
)?);
}
}

Ok(())
}
}

#[cfg(test)]
Expand Down
5 changes: 5 additions & 0 deletions src/objects/codemodel_v2/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::path::PathBuf;
/// A codemodel "directory" object is referenced by a "codemodel" version 2 object's directories array.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Directory {
/// Paths of the directory object.
pub paths: DirectoryPaths,
Expand All @@ -21,6 +22,7 @@ pub struct Directory {

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DirectoryPaths {
/// A string specifying the path to the source directory, represented with forward slashes.
/// If the directory is inside the top-level source directory then the path is specified
Expand All @@ -37,6 +39,7 @@ pub struct DirectoryPaths {

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Installer {
/// A string specifying the component selected by the corresponding to install() command invocation.
pub component: String,
Expand Down Expand Up @@ -145,6 +148,7 @@ pub struct Installer {
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TargetIdAndIndex {
/// A string uniquely identifying the target.
/// This matches the id member of the target in the main "codemodel" object's targets array.
Expand All @@ -156,6 +160,7 @@ pub struct TargetIdAndIndex {

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct FromToPaths {
/// Path from which a file or directory is to be installed.
pub from: PathBuf,
Expand Down
Loading

0 comments on commit 886e48d

Please sign in to comment.