Skip to content

Commit

Permalink
Build archive files for packages (#234)
Browse files Browse the repository at this point in the history
* Build archive files for packages

* Sort packages

* Update dictionary

* Fix linting

* Refactor

* Refactor

* Define FFI archive suffix

* Modify lib/os

* Remove unused argument

* Ignore entire lib/os

* Refactor
  • Loading branch information
raviqqe authored Aug 16, 2021
1 parent b2fe744 commit b007e1f
Show file tree
Hide file tree
Showing 18 changed files with 380 additions and 152 deletions.
3 changes: 3 additions & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"words": [
"aarch",
"algo",
"arity",
"bincode",
"builddir",
Expand All @@ -20,6 +21,7 @@
"itertools",
"malloc",
"mtriple",
"petgraph",
"realloc",
"renamer",
"repr",
Expand All @@ -28,6 +30,7 @@
"tailcallopt",
"tempfile",
"termcolor",
"toposort",
"wasi"
]
}
9 changes: 1 addition & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ members = [
"lib/lang",
"lib/mir",
"lib/mir-fmm",
"lib/os/ffi",
"lib/prelude/ffi",
]
exclude = ["lib/os/.pen", "lib/core/.pen", "tmp"]
exclude = ["lib/os", "lib/core/.pen", "tmp"]
1 change: 1 addition & 0 deletions lib/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ lang = { path = "../lang" }
mir = { path = "../mir" }
mir-fmm = { path = "../mir-fmm" }
once_cell = "1"
petgraph = "0.6"
regex = "1"
serde = { version = "1", features = ["derive", "rc"] }
url = "2"
48 changes: 45 additions & 3 deletions lib/app/src/common/file_path_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use crate::{
},
};

const MAIN_ARCHIVE_BASENAME: &str = "main";
const FFI_ARCHIVE_SUFFIX: &str = "_ffi";

pub fn resolve_object_directory(output_directory: &FilePath) -> FilePath {
output_directory.join(&FilePath::new([OBJECT_DIRECTORY]))
}
Expand Down Expand Up @@ -47,11 +50,38 @@ pub fn resolve_package_directory(output_directory: &FilePath, url: &url::Url) ->
]))
}

pub fn resolve_main_package_archive_file(
output_directory: &FilePath,
file_path_configuration: &FilePathConfiguration,
) -> FilePath {
resolve_package_archive_file(
output_directory,
MAIN_ARCHIVE_BASENAME,
file_path_configuration,
)
}

pub fn resolve_main_package_ffi_archive_file(
output_directory: &FilePath,
file_path_configuration: &FilePathConfiguration,
) -> FilePath {
resolve_package_ffi_archive_file(output_directory, "main", file_path_configuration)
resolve_package_ffi_archive_file(
output_directory,
MAIN_ARCHIVE_BASENAME,
file_path_configuration,
)
}

pub fn resolve_external_package_archive_file(
output_directory: &FilePath,
url: &url::Url,
file_path_configuration: &FilePathConfiguration,
) -> FilePath {
resolve_package_archive_file(
output_directory,
&package_id_calculator::calculate(url),
file_path_configuration,
)
}

pub fn resolve_external_package_ffi_archive_file(
Expand All @@ -68,10 +98,22 @@ pub fn resolve_external_package_ffi_archive_file(

fn resolve_package_ffi_archive_file(
output_directory: &FilePath,
package_id: &str,
basename: &str,
file_path_configuration: &FilePathConfiguration,
) -> FilePath {
resolve_package_archive_file(
output_directory,
&(basename.to_owned() + FFI_ARCHIVE_SUFFIX),
file_path_configuration,
)
}

fn resolve_package_archive_file(
output_directory: &FilePath,
basename: &str,
file_path_configuration: &FilePathConfiguration,
) -> FilePath {
resolve_archive_directory(output_directory)
.join(&FilePath::new([package_id]))
.join(&FilePath::new([format!("lib{}", basename)]))
.with_extension(file_path_configuration.archive_file_extension)
}
44 changes: 44 additions & 0 deletions lib/app/src/external_package_archive_sorter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::{
external_package_configuration_reader,
infra::{FilePath, Infrastructure, PackageConfiguration},
};
use petgraph::{algo::toposort, Graph};
use std::{collections::HashMap, error::Error};

pub fn sort(
infrastructure: &Infrastructure,
package_directory: &FilePath,
output_directory: &FilePath,
) -> Result<Vec<url::Url>, Box<dyn Error>> {
sort_external_packages(&external_package_configuration_reader::read_recursively(
infrastructure,
package_directory,
output_directory,
)?)
}

fn sort_external_packages(
package_configurations: &HashMap<url::Url, PackageConfiguration>,
) -> Result<Vec<url::Url>, Box<dyn std::error::Error>> {
let mut graph = Graph::<url::Url, ()>::new();
let mut indices = HashMap::<url::Url, _>::new();

for external_package in package_configurations.keys() {
indices.insert(
external_package.clone(),
graph.add_node(external_package.clone()),
);
}

for (url, package_configuration) in package_configurations {
for dependency_url in package_configuration.dependencies.values() {
graph.add_edge(indices[dependency_url], indices[url], ());
}
}

Ok(toposort(&graph, None)
.unwrap()
.into_iter()
.map(|index| graph[index].clone())
.collect())
}
47 changes: 47 additions & 0 deletions lib/app/src/external_package_configuration_reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::{
common::file_path_resolver,
infra::{FilePath, Infrastructure, PackageConfiguration},
};
use std::{collections::HashMap, error::Error};

pub fn read_recursively(
infrastructure: &Infrastructure,
package_directory: &FilePath,
output_directory: &FilePath,
) -> Result<HashMap<url::Url, PackageConfiguration>, Box<dyn Error>> {
read_dependencies(
infrastructure,
&infrastructure
.package_configuration_reader
.read(package_directory)?,
output_directory,
)
}

fn read_dependencies(
infrastructure: &Infrastructure,
configuration: &PackageConfiguration,
output_directory: &FilePath,
) -> Result<HashMap<url::Url, PackageConfiguration>, Box<dyn Error>> {
Ok(configuration
.dependencies
.values()
.map(|url| -> Result<_, Box<dyn Error>> {
let configuration = infrastructure.package_configuration_reader.read(
&file_path_resolver::resolve_package_directory(output_directory, url),
)?;

Ok(vec![(url.clone(), configuration.clone())]
.into_iter()
.chain(read_dependencies(
infrastructure,
&configuration,
output_directory,
)?)
.collect::<Vec<_>>())
})
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.flatten()
.collect())
}
1 change: 0 additions & 1 deletion lib/app/src/infra/application_linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pub trait ApplicationLinker {
fn link(
&self,
system_package_directory: &FilePath,
object_files: &[FilePath],
archive_files: &[FilePath],
application_file: &FilePath,
target_triple: Option<&str>,
Expand Down
3 changes: 3 additions & 0 deletions lib/app/src/infra/build_script_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub trait BuildScriptCompiler {
main_module_target: Option<&MainModuleTarget>,
child_build_script_files: &[FilePath],
prelude_interface_files: &[FilePath],
archive_file: &FilePath,
ffi_archive_file: &FilePath,
package_directory: &FilePath,
output_directory: &FilePath,
Expand All @@ -18,13 +19,15 @@ pub trait BuildScriptCompiler {
fn compile_external(
&self,
module_targets: &[ModuleTarget],
archive_file: &FilePath,
ffi_archive_file: &FilePath,
package_directory: &FilePath,
) -> Result<String, Box<dyn Error>>;

fn compile_prelude(
&self,
module_targets: &[ModuleTarget],
archive_file: &FilePath,
ffi_archive_file: &FilePath,
package_directory: &FilePath,
) -> Result<String, Box<dyn Error>>;
Expand Down
2 changes: 2 additions & 0 deletions lib/app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod application_configuration;
mod common;
mod error;
mod external_package_archive_sorter;
mod external_package_configuration_reader;
pub mod infra;
pub mod module_compiler;
pub mod module_dependency_resolver;
Expand Down
14 changes: 14 additions & 0 deletions lib/app/src/package_build_script_compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ pub fn compile_main(
output_directory,
prelude_package_url,
)?,
&file_path_resolver::resolve_main_package_archive_file(
output_directory,
&infrastructure.file_path_configuration,
),
&file_path_resolver::resolve_main_package_ffi_archive_file(
output_directory,
&infrastructure.file_path_configuration,
Expand Down Expand Up @@ -120,6 +124,11 @@ pub fn compile_external(
&package_directory,
output_directory,
)?,
&file_path_resolver::resolve_external_package_archive_file(
output_directory,
package_url,
&infrastructure.file_path_configuration,
),
&file_path_resolver::resolve_external_package_ffi_archive_file(
output_directory,
package_url,
Expand Down Expand Up @@ -152,6 +161,11 @@ pub fn compile_prelude(
&package_directory,
output_directory,
)?,
&file_path_resolver::resolve_external_package_archive_file(
output_directory,
package_url,
&infrastructure.file_path_configuration,
),
&file_path_resolver::resolve_external_package_ffi_archive_file(
output_directory,
package_url,
Expand Down
Loading

0 comments on commit b007e1f

Please sign in to comment.