Skip to content

Commit

Permalink
aggregating of cmake files
Browse files Browse the repository at this point in the history
  • Loading branch information
tanneberger committed May 31, 2024
1 parent 7edc74a commit 1ad74ed
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 114 deletions.
6 changes: 1 addition & 5 deletions src/backends/lfc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,7 @@ struct LfcJsonArgs<'a> {
impl<'a> LfcJsonArgs<'a> {
pub fn new(app: &'a App, compile_target_code: bool) -> Self {
let mut hash_map: HashMap<&str, serde_json::Value> = HashMap::new();

app.properties.fast.map(|value| {
hash_map.insert("fast", serde_json::Value::Bool(value));
});

hash_map.insert("fast", serde_json::Value::Bool(app.properties.fast));

Self {
src: &app.main_reactor,
Expand Down
3 changes: 2 additions & 1 deletion src/backends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ pub fn execute_command<'a>(command: &CommandSpec, config: &'a Config) -> BatchBu

match command {
CommandSpec::Build(build) => {
println!("found {} libraries ... cloning", dependencies.len());
let manager = DependencyManager::from_dependencies(
dependencies.clone(),
&PathBuf::new(),
&PathBuf::from("./target"),
);

}
Expand Down
79 changes: 69 additions & 10 deletions src/package/lock.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use crate::package::management::{copy_dir_all, LocationDescription};
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::fs;
use std::path::{Path, PathBuf};
use std::thread::sleep;
use colored::Colorize;
use crate::package::ConfigFile;
use crate::package::target_properties::{LibraryTargetProperties, MergeTargetProperties};
use crate::package::tree::{DependencyTreeNode, PackageDetails, ProjectSource};


#[derive(Deserialize, Serialize)]
Expand All @@ -12,30 +18,83 @@ pub struct LockedResource {

#[derive(Deserialize, Serialize)]
pub struct DependencyLock {
dependencies: HashMap<String, LocationDescription>,
package_locations: HashMap<String, LockedResource>,

#[serde(skip)]
loaded_dependencies: Vec<DependencyTreeNode>
}

impl DependencyLock {
pub fn new() -> DependencyLock {
Self {
dependencies: HashMap::new(),
package_locations: HashMap::new(),
loaded_dependencies: Vec::new()
}
}

pub fn from(dependencies: HashMap<String, LocationDescription>) -> DependencyLock {
Self { dependencies }
pub fn from(mut unlocked_locations: HashMap<String, LocationDescription>, loaded_dependencies: Vec<DependencyTreeNode>) -> DependencyLock {
let mut package_locations = HashMap::new();
for package in &loaded_dependencies {
let location = unlocked_locations.get_mut(&package.name).expect("cannot find package");

package_locations.insert(package.name.clone(), LockedResource {
location: location.clone(),
hash: package.hash.clone(),
});
}

Self { package_locations, loaded_dependencies }
}

pub fn init(&mut self, lfc_include_folder: &Path) -> anyhow::Result<()> {
for lock in self.package_locations.iter() {
let temp = lfc_include_folder.clone().join(lock.0);

print!("{} reading ... {}", lock.0.to_string().green().bold(), &temp.display());

let lingo_toml_text = fs::read_to_string(&temp.join("Lingo.toml")).expect("cannot read toml");
let read_toml = toml::from_str::<ConfigFile>(&lingo_toml_text).expect("cannot parse toml").to_config(&temp);

self.loaded_dependencies.push(DependencyTreeNode {
name: read_toml.package.name,
version: read_toml.package.version,
package: PackageDetails {
version: Default::default(),
mutual_exclusive: ProjectSource::Empty,
},
location: temp,
hash: "".parse()?,
dependencies: vec![],
properties: read_toml.library.unwrap().properties
})
}

Ok(())
}

pub fn create_library_folder(&self, target_path: &PathBuf) -> anyhow::Result<()> {
pub fn create_library_folder(&self, source_path: &PathBuf, target_path: &PathBuf) -> anyhow::Result<()> {
std::fs::create_dir_all(target_path)?;

for (name, lock) in self.dependencies.iter() {
//let local_source = lock.local.clone();
//let find_source = target_path.clone().join(name);
for (name, lock) in self.package_locations.iter() {
let local_source = source_path.clone().join(lock.hash.clone());
let find_source = target_path.clone().join(name);

//copy_dir_all(&local_source, &find_source)?;
println!("copying {} -> {}", &local_source.display(), &find_source.display());
std::fs::create_dir_all(&find_source)?;
copy_dir_all(&local_source, &find_source)?;
}

Ok(())
}


pub fn aggregate_target_properties(&self) -> anyhow::Result<LibraryTargetProperties> {
let mut i = LibraryTargetProperties::default();

for tp in &self.loaded_dependencies {
i.merge(&tp.properties)?;
}

return Ok(i);
}
}
18 changes: 11 additions & 7 deletions src/package/management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,13 @@ impl DependencyManager {

let mut manager;
let mut lock: DependencyLock;
let lock_file = Path::new("../Lingo.lock");
let lock_file = target_path.clone().join("../Lingo.lock");

if lock_file.exists() {
lock = serde_json::from_str::<DependencyLock>(&std::fs::read_to_string(lock_file)?)?;
println!("Found Lingo.lock skipping pulling");
lock = serde_json::from_str::<DependencyLock>(&std::fs::read_to_string(lock_file).expect("cannot read lock file")).expect("cannot parse lock file");
println!("reading lfc_include folder ...");
lock.init(&target_path.clone().join("lfc_include")).expect("init failed");
} else {
manager = DependencyManager::new();

Expand All @@ -103,15 +106,18 @@ impl DependencyManager {

let selection = DependencyManager::flatten(root_nodes.clone())?;

lock = DependencyLock::from(selection);
lock = DependencyLock::from(selection, root_nodes);

let mut lock_file = File::create(target_path.join("../Lingo.lock"))?;
let serialized_json = serde_json::to_string_pretty(&lock)?;
lock_file.write(serialized_json.as_ref())?;

let include_folder = target_path.clone().join("lfc_include");
lock.create_library_folder(&library_path, &include_folder).expect("creating lock folder failed");
}

let include_folder = target_path.clone().join("lfc_include");
lock.create_library_folder(&include_folder)?;
let result = lock.aggregate_target_properties().expect("aggregation failed");
println!("cmake: {}", &result.cmake_include);

Ok(DependencyManager::new())
}
Expand Down Expand Up @@ -304,6 +310,4 @@ impl DependencyManager {
Ok(selection)
}

//pub fn forward_target_properties(&self, app_target_properties: &mut AppTargetProperties) -> anyhow::Result<()> {
//}
}
39 changes: 13 additions & 26 deletions src/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ use versions::Versioning;

use crate::package::tree::PackageDetails;
use which::which;
use crate::package::target_properties::{AppTargetProperties, CMakeLoadDeserializer, CMakeLoaderImpl, LibraryTargetProperties};
use crate::package::target_properties::{AppTargetProperties, AppTargetPropertiesFile, LibraryTargetProperties, LibraryTargetPropertiesFile};

/// place where are the build artifacts will be dropped
const OUTPUT_DIRECTORY: &str = "target";
// name of the folder inside the `OUTPUT_DIRECTORY` where libraries will be copied into
const LIBRARY_DIRECTORY: &str = "library";
const LIBRARY_DIRECTORY: &str = "libraries";

fn is_valid_location_for_project(path: &std::path::Path) -> bool {
!path.join("src").exists() && !path.join(".git").exists() && !path.join("application").exists()
Expand Down Expand Up @@ -62,7 +62,7 @@ pub struct ConfigFile {
pub dependencies: HashMap<String, PackageDetails>,
}

#[derive(Clone, Deserialize, Serialize)]
#[derive(Clone)]
pub struct Config {
/// top level package description
pub package: PackageDescription,
Expand All @@ -71,11 +71,9 @@ pub struct Config {
// pub properties: HashMap<String, serde_json::Value>,

/// list of apps defined inside this package
#[serde(rename = "app")]
pub apps: Vec<App>,

/// library exported by this package
#[serde(rename = "lib")]
pub library: Option<Library>,

/// Dependencies for required to build this Lingua-Franca Project
Expand All @@ -98,10 +96,10 @@ pub struct LibraryFile {
pub platform: Option<Platform>,

/// target properties of that lingua-franca app
pub properties: LibraryTargetProperties
pub properties: LibraryTargetPropertiesFile
}

#[derive(Clone, Deserialize, Serialize)]
#[derive(Clone)]
pub struct Library {
/// if not specified will default to value specified in the package description
pub name: String,
Expand Down Expand Up @@ -138,10 +136,10 @@ pub struct AppFile {
pub platform: Option<Platform>,

/// target properties of that lingua-franca app
pub properties: AppTargetProperties
pub properties: AppTargetPropertiesFile
}

#[derive(Clone, Deserialize, Serialize)]
#[derive(Clone)]
pub struct App {
/// Absolute path to the directory where the Lingo.toml file is located.
pub root_path: PathBuf,
Expand Down Expand Up @@ -188,7 +186,7 @@ impl AppFile {
},
target: self.target,
platform: self.platform.unwrap_or(Platform::Native),
properties: self.properties,
properties: self.properties.from(path),
}
}
}
Expand Down Expand Up @@ -221,7 +219,7 @@ impl LibraryFile {
},
target: self.target,
platform: self.platform.unwrap_or(Platform::Native),
properties: self.properties,
properties: self.properties.from(path),
output_root: path.join(OUTPUT_DIRECTORY),
}
}
Expand Down Expand Up @@ -368,21 +366,10 @@ impl ConfigFile {
}

pub fn from(path: &Path) -> io::Result<ConfigFile> {
let content = read_to_string(path)?;

let loader = CMakeLoadDeserializer {
asset_loader: &mut CMakeLoaderImpl {
lib_path: PathBuf::from(path)
}
};


let mut deserializer = toml::Deserializer::new(&content);
let scene = loader.deserialize(&mut deserializer)
.map_err(|e| io::Error::new(ErrorKind::InvalidData, format!("{}", e)))?;

return

read_to_string(path).and_then(|contents| {
toml::from_str(&contents)
.map_err(|e| io::Error::new(ErrorKind::InvalidData, format!("{}", e)))
})
}

// Sets up a standard LF project for "native" development and deployment
Expand Down
Loading

0 comments on commit 1ad74ed

Please sign in to comment.