dev/pbs (#8)
Co-authored-by: Nilton Constantino <nilton.constantino@visma.com> Reviewed-on: #8
This commit is contained in:
@@ -0,0 +1,429 @@
|
||||
use crate::building::output::{CompiledModule};
|
||||
use crate::building::plan::BuildStep;
|
||||
use prometeu_bytecode::opcode::OpCode;
|
||||
use prometeu_bytecode::{ConstantPoolEntry, DebugInfo};
|
||||
use prometeu_core::virtual_machine::{ProgramImage, Value};
|
||||
use std::collections::{HashMap};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
pub enum LinkError {
|
||||
UnresolvedSymbol(String),
|
||||
DuplicateExport(String),
|
||||
IncompatibleSymbolSignature(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for LinkError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
LinkError::UnresolvedSymbol(s) => write!(f, "Unresolved symbol: {}", s),
|
||||
LinkError::DuplicateExport(s) => write!(f, "Duplicate export: {}", s),
|
||||
LinkError::IncompatibleSymbolSignature(s) => write!(f, "Incompatible symbol signature: {}", s),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for LinkError {}
|
||||
|
||||
pub struct Linker;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
struct ConstantPoolBitKey(Vec<u8>);
|
||||
|
||||
impl ConstantPoolBitKey {
|
||||
fn from_entry(entry: &ConstantPoolEntry) -> Self {
|
||||
match entry {
|
||||
ConstantPoolEntry::Null => Self(vec![0]),
|
||||
ConstantPoolEntry::Int64(v) => {
|
||||
let mut b = vec![1];
|
||||
b.extend_from_slice(&v.to_le_bytes());
|
||||
Self(b)
|
||||
}
|
||||
ConstantPoolEntry::Float64(v) => {
|
||||
let mut b = vec![2];
|
||||
b.extend_from_slice(&v.to_bits().to_le_bytes());
|
||||
Self(b)
|
||||
}
|
||||
ConstantPoolEntry::Boolean(v) => {
|
||||
Self(vec![3, if *v { 1 } else { 0 }])
|
||||
}
|
||||
ConstantPoolEntry::String(v) => {
|
||||
let mut b = vec![4];
|
||||
b.extend_from_slice(v.as_bytes());
|
||||
Self(b)
|
||||
}
|
||||
ConstantPoolEntry::Int32(v) => {
|
||||
let mut b = vec![5];
|
||||
b.extend_from_slice(&v.to_le_bytes());
|
||||
Self(b)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Linker {
|
||||
pub fn link(modules: Vec<CompiledModule>, steps: Vec<BuildStep>) -> Result<ProgramImage, LinkError> {
|
||||
if modules.len() != steps.len() {
|
||||
return Err(LinkError::IncompatibleSymbolSignature(format!("Module count ({}) does not match build steps count ({})", modules.len(), steps.len())));
|
||||
}
|
||||
|
||||
let mut combined_code = Vec::new();
|
||||
let mut combined_functions = Vec::new();
|
||||
let mut combined_constants = Vec::new();
|
||||
let mut constant_map: HashMap<ConstantPoolBitKey, u32> = HashMap::new();
|
||||
|
||||
// Debug info merging
|
||||
let mut combined_pc_to_span = Vec::new();
|
||||
let mut combined_function_names = Vec::new();
|
||||
|
||||
// 1. DebugSymbol resolution map: (ProjectId, module_path, symbol_name) -> func_idx in combined_functions
|
||||
let mut global_symbols = HashMap::new();
|
||||
|
||||
let mut module_code_offsets = Vec::with_capacity(modules.len());
|
||||
let mut module_function_offsets = Vec::with_capacity(modules.len());
|
||||
|
||||
// Map ProjectId to index
|
||||
let _project_to_idx: HashMap<_, _> = modules.iter().enumerate().map(|(i, m)| (m.project_id.clone(), i)).collect();
|
||||
|
||||
// PASS 1: Collect exports and calculate offsets
|
||||
for (_i, module) in modules.iter().enumerate() {
|
||||
let code_offset = combined_code.len() as u32;
|
||||
let function_offset = combined_functions.len() as u32;
|
||||
|
||||
module_code_offsets.push(code_offset);
|
||||
module_function_offsets.push(function_offset);
|
||||
|
||||
for (key, meta) in &module.exports {
|
||||
if let Some(local_func_idx) = meta.func_idx {
|
||||
let global_func_idx = function_offset + local_func_idx;
|
||||
// Note: Use a tuple as key for clarity
|
||||
let symbol_id = (module.project_id.clone(), key.module_path.clone(), key.symbol_name.clone());
|
||||
|
||||
if global_symbols.contains_key(&symbol_id) {
|
||||
return Err(LinkError::DuplicateExport(format!("Project {:?} export {}:{} already defined", symbol_id.0, symbol_id.1, symbol_id.2)));
|
||||
}
|
||||
global_symbols.insert(symbol_id, global_func_idx);
|
||||
}
|
||||
}
|
||||
|
||||
combined_code.extend_from_slice(&module.code);
|
||||
for func in &module.function_metas {
|
||||
let mut relocated = func.clone();
|
||||
relocated.code_offset += code_offset;
|
||||
combined_functions.push(relocated);
|
||||
}
|
||||
|
||||
if let Some(debug) = &module.debug_info {
|
||||
for (pc, span) in &debug.pc_to_span {
|
||||
combined_pc_to_span.push((code_offset + pc, span.clone()));
|
||||
}
|
||||
for (func_idx, name) in &debug.function_names {
|
||||
combined_function_names.push((function_offset + func_idx, name.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PASS 2: Relocate constants and patch CALLs
|
||||
for (i, module) in modules.iter().enumerate() {
|
||||
let step = &steps[i];
|
||||
let code_offset = module_code_offsets[i] as usize;
|
||||
|
||||
// Map local constant indices to global constant indices
|
||||
let mut local_to_global_const = Vec::with_capacity(module.const_pool.len());
|
||||
for entry in &module.const_pool {
|
||||
let bit_key = ConstantPoolBitKey::from_entry(entry);
|
||||
if let Some(&global_idx) = constant_map.get(&bit_key) {
|
||||
local_to_global_const.push(global_idx);
|
||||
} else {
|
||||
let global_idx = combined_constants.len() as u32;
|
||||
combined_constants.push(match entry {
|
||||
ConstantPoolEntry::Null => Value::Null,
|
||||
ConstantPoolEntry::Int64(v) => Value::Int64(*v),
|
||||
ConstantPoolEntry::Float64(v) => Value::Float(*v),
|
||||
ConstantPoolEntry::Boolean(v) => Value::Boolean(*v),
|
||||
ConstantPoolEntry::String(v) => Value::String(v.clone()),
|
||||
ConstantPoolEntry::Int32(v) => Value::Int32(*v),
|
||||
});
|
||||
constant_map.insert(bit_key, global_idx);
|
||||
local_to_global_const.push(global_idx);
|
||||
}
|
||||
}
|
||||
|
||||
// Patch imports
|
||||
for import in &module.imports {
|
||||
let dep_project_id = if import.key.dep_alias == "self" || import.key.dep_alias.is_empty() {
|
||||
&module.project_id
|
||||
} else {
|
||||
step.deps.get(&import.key.dep_alias)
|
||||
.ok_or_else(|| LinkError::UnresolvedSymbol(format!("Dependency alias '{}' not found in project {:?}", import.key.dep_alias, module.project_id)))?
|
||||
};
|
||||
|
||||
let symbol_id = (dep_project_id.clone(), import.key.module_path.clone(), import.key.symbol_name.clone());
|
||||
let &target_func_idx = global_symbols.get(&symbol_id)
|
||||
.ok_or_else(|| LinkError::UnresolvedSymbol(format!("DebugSymbol '{}:{}' not found in project {:?}", symbol_id.1, symbol_id.2, symbol_id.0)))?;
|
||||
|
||||
for &reloc_pc in &import.relocation_pcs {
|
||||
let absolute_pc = code_offset + reloc_pc as usize;
|
||||
let imm_offset = absolute_pc + 2;
|
||||
if imm_offset + 4 <= combined_code.len() {
|
||||
combined_code[imm_offset..imm_offset+4].copy_from_slice(&target_func_idx.to_le_bytes());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Internal call relocation (from module-local func_idx to global func_idx)
|
||||
// And PUSH_CONST relocation.
|
||||
let mut pos = code_offset;
|
||||
let end = code_offset + module.code.len();
|
||||
while pos < end {
|
||||
if pos + 2 > end { break; }
|
||||
let op_val = u16::from_le_bytes([combined_code[pos], combined_code[pos+1]]);
|
||||
let opcode = match OpCode::try_from(op_val) {
|
||||
Ok(op) => op,
|
||||
Err(_) => {
|
||||
pos += 2;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
pos += 2;
|
||||
|
||||
match opcode {
|
||||
OpCode::PushConst => {
|
||||
if pos + 4 <= end {
|
||||
let local_idx = u32::from_le_bytes(combined_code[pos..pos+4].try_into().unwrap()) as usize;
|
||||
if let Some(&global_idx) = local_to_global_const.get(local_idx) {
|
||||
combined_code[pos..pos+4].copy_from_slice(&global_idx.to_le_bytes());
|
||||
}
|
||||
pos += 4;
|
||||
}
|
||||
}
|
||||
OpCode::Call => {
|
||||
if pos + 4 <= end {
|
||||
let local_func_idx = u32::from_le_bytes(combined_code[pos..pos+4].try_into().unwrap());
|
||||
|
||||
// Check if this PC was already patched by an import.
|
||||
// If it wasn't, it's an internal call that needs relocation.
|
||||
let reloc_pc = (pos - 2 - code_offset) as u32;
|
||||
let is_import = module.imports.iter().any(|imp| imp.relocation_pcs.contains(&reloc_pc));
|
||||
|
||||
if !is_import {
|
||||
let global_func_idx = module_function_offsets[i] + local_func_idx;
|
||||
combined_code[pos..pos+4].copy_from_slice(&global_func_idx.to_le_bytes());
|
||||
}
|
||||
pos += 4;
|
||||
}
|
||||
}
|
||||
OpCode::PushI32 | OpCode::PushBounded | OpCode::Jmp | OpCode::JmpIfFalse | OpCode::JmpIfTrue
|
||||
| OpCode::GetGlobal | OpCode::SetGlobal | OpCode::GetLocal | OpCode::SetLocal
|
||||
| OpCode::PopN | OpCode::Syscall | OpCode::GateLoad | OpCode::GateStore => {
|
||||
pos += 4;
|
||||
}
|
||||
OpCode::PushI64 | OpCode::PushF64 | OpCode::Alloc => {
|
||||
pos += 8;
|
||||
}
|
||||
OpCode::PushBool => {
|
||||
pos += 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Final Exports map for ProgramImage (String -> func_idx)
|
||||
// Only including exports from the ROOT project (the last one in build plan usually)
|
||||
// In PBS v0, exports are name -> func_id.
|
||||
let mut final_exports = HashMap::new();
|
||||
if let Some(root_module) = modules.last() {
|
||||
for (key, meta) in &root_module.exports {
|
||||
if let Some(local_func_idx) = meta.func_idx {
|
||||
let global_func_idx = module_function_offsets.last().unwrap() + local_func_idx;
|
||||
final_exports.insert(format!("{}:{}", key.module_path, key.symbol_name), global_func_idx);
|
||||
// Also provide short name for root module exports to facilitate entrypoint resolution
|
||||
if !final_exports.contains_key(&key.symbol_name) {
|
||||
final_exports.insert(key.symbol_name.clone(), global_func_idx);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// v0: Fallback export for entrypoint `frame` (root module)
|
||||
if !final_exports.iter().any(|(name, _)| name.ends_with(":frame") || name == "frame") {
|
||||
if let Some(&root_offset) = module_function_offsets.last() {
|
||||
if let Some((idx, _)) = combined_function_names.iter().find(|(i, name)| *i >= root_offset && name == "frame") {
|
||||
final_exports.insert("frame".to_string(), *idx);
|
||||
final_exports.insert("src/main/modules:frame".to_string(), *idx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let combined_debug_info = if combined_pc_to_span.is_empty() && combined_function_names.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(DebugInfo {
|
||||
pc_to_span: combined_pc_to_span,
|
||||
function_names: combined_function_names,
|
||||
})
|
||||
};
|
||||
|
||||
Ok(ProgramImage::new(
|
||||
combined_code,
|
||||
combined_constants,
|
||||
combined_functions,
|
||||
combined_debug_info,
|
||||
final_exports,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::BTreeMap;
|
||||
use super::*;
|
||||
use crate::building::output::{ExportKey, ExportMetadata, ImportKey, ImportMetadata};
|
||||
use crate::semantics::export_surface::ExportSurfaceKind;
|
||||
use crate::building::plan::BuildTarget;
|
||||
use crate::deps::resolver::ProjectId;
|
||||
use prometeu_bytecode::opcode::OpCode;
|
||||
use prometeu_bytecode::FunctionMeta;
|
||||
|
||||
#[test]
|
||||
fn test_link_root_and_lib() {
|
||||
let lib_id = ProjectId { name: "lib".into(), version: "1.0.0".into() };
|
||||
let root_id = ProjectId { name: "root".into(), version: "1.0.0".into() };
|
||||
|
||||
// Lib module: exports 'add'
|
||||
let mut lib_code = Vec::new();
|
||||
lib_code.extend_from_slice(&(OpCode::Add as u16).to_le_bytes());
|
||||
lib_code.extend_from_slice(&(OpCode::Ret as u16).to_le_bytes());
|
||||
|
||||
let mut lib_exports = BTreeMap::new();
|
||||
lib_exports.insert(ExportKey {
|
||||
module_path: "math".into(),
|
||||
symbol_name: "add".into(),
|
||||
kind: ExportSurfaceKind::Service,
|
||||
}, ExportMetadata { func_idx: Some(0), is_host: false, ty: None });
|
||||
|
||||
let lib_module = CompiledModule {
|
||||
project_id: lib_id.clone(),
|
||||
target: BuildTarget::Main,
|
||||
exports: lib_exports,
|
||||
imports: vec![],
|
||||
const_pool: vec![],
|
||||
code: lib_code,
|
||||
function_metas: vec![FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: 4,
|
||||
..Default::default()
|
||||
}],
|
||||
debug_info: None,
|
||||
symbols: vec![],
|
||||
};
|
||||
|
||||
// Root module: calls 'lib::math:add'
|
||||
let mut root_code = Vec::new();
|
||||
root_code.extend_from_slice(&(OpCode::PushI32 as u16).to_le_bytes());
|
||||
root_code.extend_from_slice(&10i32.to_le_bytes());
|
||||
root_code.extend_from_slice(&(OpCode::PushI32 as u16).to_le_bytes());
|
||||
root_code.extend_from_slice(&20i32.to_le_bytes());
|
||||
// Call lib:math:add
|
||||
let call_pc = root_code.len() as u32;
|
||||
root_code.extend_from_slice(&(OpCode::Call as u16).to_le_bytes());
|
||||
root_code.extend_from_slice(&0u32.to_le_bytes()); // placeholder
|
||||
root_code.extend_from_slice(&(OpCode::Halt as u16).to_le_bytes());
|
||||
|
||||
let root_imports = vec![ImportMetadata {
|
||||
key: ImportKey {
|
||||
dep_alias: "mylib".into(),
|
||||
module_path: "math".into(),
|
||||
symbol_name: "add".into(),
|
||||
},
|
||||
relocation_pcs: vec![call_pc],
|
||||
}];
|
||||
|
||||
let root_module = CompiledModule {
|
||||
project_id: root_id.clone(),
|
||||
target: BuildTarget::Main,
|
||||
exports: BTreeMap::new(),
|
||||
imports: root_imports,
|
||||
const_pool: vec![],
|
||||
code: root_code,
|
||||
function_metas: vec![FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: 20,
|
||||
..Default::default()
|
||||
}],
|
||||
debug_info: None,
|
||||
symbols: vec![],
|
||||
};
|
||||
|
||||
let lib_step = BuildStep {
|
||||
project_id: lib_id.clone(),
|
||||
project_dir: "".into(),
|
||||
target: BuildTarget::Main,
|
||||
sources: vec![],
|
||||
deps: BTreeMap::new(),
|
||||
};
|
||||
|
||||
let mut root_deps = BTreeMap::new();
|
||||
root_deps.insert("mylib".into(), lib_id.clone());
|
||||
|
||||
let root_step = BuildStep {
|
||||
project_id: root_id.clone(),
|
||||
project_dir: "".into(),
|
||||
target: BuildTarget::Main,
|
||||
sources: vec![],
|
||||
deps: root_deps,
|
||||
};
|
||||
|
||||
let result = Linker::link(vec![lib_module, root_module], vec![lib_step, root_step]).unwrap();
|
||||
|
||||
assert_eq!(result.functions.len(), 2);
|
||||
// lib:add is func 0
|
||||
// root:main is func 1
|
||||
|
||||
// lib_code length is 4.
|
||||
// Root code starts at 4.
|
||||
// CALL was at root_code offset 12.
|
||||
// Absolute PC of CALL: 4 + 12 = 16.
|
||||
// Immediate is at 16 + 2 = 18.
|
||||
let patched_func_idx = u32::from_le_bytes(result.rom[18..22].try_into().unwrap());
|
||||
assert_eq!(patched_func_idx, 0); // Points to lib:add
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_link_const_deduplication() {
|
||||
let id = ProjectId { name: "test".into(), version: "1.0.0".into() };
|
||||
let step = BuildStep { project_id: id.clone(), project_dir: "".into(), target: BuildTarget::Main, sources: vec![], deps: BTreeMap::new() };
|
||||
|
||||
let m1 = CompiledModule {
|
||||
project_id: id.clone(),
|
||||
target: BuildTarget::Main,
|
||||
exports: BTreeMap::new(),
|
||||
imports: vec![],
|
||||
const_pool: vec![ConstantPoolEntry::Int32(42), ConstantPoolEntry::String("hello".into())],
|
||||
code: vec![],
|
||||
function_metas: vec![],
|
||||
debug_info: None,
|
||||
symbols: vec![],
|
||||
};
|
||||
|
||||
let m2 = CompiledModule {
|
||||
project_id: id.clone(),
|
||||
target: BuildTarget::Main,
|
||||
exports: BTreeMap::new(),
|
||||
imports: vec![],
|
||||
const_pool: vec![ConstantPoolEntry::String("hello".into()), ConstantPoolEntry::Int32(99)],
|
||||
code: vec![],
|
||||
function_metas: vec![],
|
||||
debug_info: None,
|
||||
symbols: vec![],
|
||||
};
|
||||
|
||||
let result = Linker::link(vec![m1, m2], vec![step.clone(), step]).unwrap();
|
||||
|
||||
// Constants should be: 42, "hello", 99
|
||||
assert_eq!(result.constant_pool.len(), 3);
|
||||
assert_eq!(result.constant_pool[0], Value::Int32(42));
|
||||
assert_eq!(result.constant_pool[1], Value::String("hello".into()));
|
||||
assert_eq!(result.constant_pool[2], Value::Int32(99));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
pub mod plan;
|
||||
pub mod output;
|
||||
pub mod linker;
|
||||
pub mod orchestrator;
|
||||
@@ -0,0 +1,73 @@
|
||||
use crate::building::linker::{LinkError, Linker};
|
||||
use crate::building::output::{compile_project, CompileError};
|
||||
use crate::building::plan::{BuildPlan, BuildTarget};
|
||||
use crate::common::files::FileManager;
|
||||
use crate::deps::resolver::ResolvedGraph;
|
||||
use prometeu_core::virtual_machine::ProgramImage;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum BuildError {
|
||||
Compile(CompileError),
|
||||
Link(LinkError),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BuildResult {
|
||||
pub image: ProgramImage,
|
||||
pub file_manager: FileManager,
|
||||
pub symbols: Vec<crate::common::symbols::ProjectSymbols>,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for BuildError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
BuildError::Compile(e) => write!(f, "Compile error: {}", e),
|
||||
BuildError::Link(e) => write!(f, "Link error: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for BuildError {}
|
||||
|
||||
impl From<CompileError> for BuildError {
|
||||
fn from(e: CompileError) -> Self {
|
||||
BuildError::Compile(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<LinkError> for BuildError {
|
||||
fn from(e: LinkError) -> Self {
|
||||
BuildError::Link(e)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_from_graph(graph: &ResolvedGraph, target: BuildTarget) -> Result<BuildResult, BuildError> {
|
||||
let plan = BuildPlan::from_graph(graph, target);
|
||||
let mut compiled_modules = HashMap::new();
|
||||
let mut modules_in_order = Vec::new();
|
||||
let mut file_manager = FileManager::new();
|
||||
|
||||
for step in &plan.steps {
|
||||
let compiled = compile_project(step.clone(), &compiled_modules, &mut file_manager)?;
|
||||
compiled_modules.insert(step.project_id.clone(), compiled.clone());
|
||||
modules_in_order.push(compiled);
|
||||
}
|
||||
|
||||
let program_image = Linker::link(modules_in_order.clone(), plan.steps.clone())?;
|
||||
|
||||
let mut all_project_symbols = Vec::new();
|
||||
for (i, module) in modules_in_order.into_iter().enumerate() {
|
||||
all_project_symbols.push(crate::common::symbols::ProjectSymbols {
|
||||
project: module.project_id.name.clone(),
|
||||
project_dir: plan.steps[i].project_dir.to_string_lossy().to_string(),
|
||||
symbols: module.symbols,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(BuildResult {
|
||||
image: program_image,
|
||||
file_manager,
|
||||
symbols: all_project_symbols,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,412 @@
|
||||
use crate::backend::emit_fragments;
|
||||
use crate::building::plan::{BuildStep, BuildTarget};
|
||||
use crate::common::diagnostics::DiagnosticBundle;
|
||||
use crate::common::files::FileManager;
|
||||
use crate::common::spans::Span;
|
||||
use crate::deps::resolver::ProjectId;
|
||||
use crate::frontends::pbs::ast::FileNode;
|
||||
use crate::frontends::pbs::collector::SymbolCollector;
|
||||
use crate::frontends::pbs::lowering::Lowerer;
|
||||
use crate::frontends::pbs::parser::Parser;
|
||||
use crate::frontends::pbs::resolver::{ModuleProvider, Resolver};
|
||||
use crate::frontends::pbs::symbols::{ModuleSymbols, Namespace, Symbol, SymbolKind, Visibility};
|
||||
use crate::frontends::pbs::typecheck::TypeChecker;
|
||||
use crate::frontends::pbs::types::PbsType;
|
||||
use crate::semantics::export_surface::ExportSurfaceKind;
|
||||
use crate::lowering::core_to_vm;
|
||||
use prometeu_bytecode::{ConstantPoolEntry, DebugInfo, FunctionMeta};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct ExportKey {
|
||||
pub module_path: String,
|
||||
pub symbol_name: String,
|
||||
pub kind: ExportSurfaceKind,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ExportMetadata {
|
||||
pub func_idx: Option<u32>,
|
||||
pub is_host: bool,
|
||||
pub ty: Option<PbsType>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct ImportKey {
|
||||
pub dep_alias: String,
|
||||
pub module_path: String,
|
||||
pub symbol_name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ImportMetadata {
|
||||
pub key: ImportKey,
|
||||
pub relocation_pcs: Vec<u32>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CompiledModule {
|
||||
pub project_id: ProjectId,
|
||||
pub target: BuildTarget,
|
||||
pub exports: BTreeMap<ExportKey, ExportMetadata>,
|
||||
pub imports: Vec<ImportMetadata>,
|
||||
pub const_pool: Vec<ConstantPoolEntry>,
|
||||
pub code: Vec<u8>,
|
||||
pub function_metas: Vec<FunctionMeta>,
|
||||
pub debug_info: Option<DebugInfo>,
|
||||
pub symbols: Vec<crate::common::symbols::Symbol>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum CompileError {
|
||||
Frontend(crate::common::diagnostics::DiagnosticBundle),
|
||||
DuplicateExport {
|
||||
symbol: String,
|
||||
first_dep: String,
|
||||
second_dep: String,
|
||||
},
|
||||
Io(std::io::Error),
|
||||
Internal(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for CompileError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
CompileError::Frontend(d) => write!(f, "Frontend error: {:?}", d),
|
||||
CompileError::DuplicateExport { symbol, first_dep, second_dep } => {
|
||||
write!(f, "duplicate export: symbol `{}`\n first defined in dependency `{}`\n again defined in dependency `{}`", symbol, first_dep, second_dep)
|
||||
}
|
||||
CompileError::Io(e) => write!(f, "IO error: {}", e),
|
||||
CompileError::Internal(s) => write!(f, "Internal error: {}", s),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for CompileError {}
|
||||
|
||||
impl From<std::io::Error> for CompileError {
|
||||
fn from(e: std::io::Error) -> Self {
|
||||
CompileError::Io(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::common::diagnostics::DiagnosticBundle> for CompileError {
|
||||
fn from(d: crate::common::diagnostics::DiagnosticBundle) -> Self {
|
||||
CompileError::Frontend(d)
|
||||
}
|
||||
}
|
||||
|
||||
struct ProjectModuleProvider {
|
||||
modules: HashMap<String, ModuleSymbols>,
|
||||
}
|
||||
|
||||
impl ModuleProvider for ProjectModuleProvider {
|
||||
fn get_module_symbols(&self, from_path: &str) -> Option<&ModuleSymbols> {
|
||||
self.modules.get(from_path)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compile_project(
|
||||
step: BuildStep,
|
||||
dep_modules: &HashMap<ProjectId, CompiledModule>,
|
||||
file_manager: &mut FileManager,
|
||||
) -> Result<CompiledModule, CompileError> {
|
||||
// 1. Parse all files and group symbols by module
|
||||
let mut module_symbols_map: HashMap<String, ModuleSymbols> = HashMap::new();
|
||||
let mut parsed_files: Vec<(String, FileNode)> = Vec::new(); // (module_path, ast)
|
||||
|
||||
for source_rel in &step.sources {
|
||||
let source_abs = step.project_dir.join(source_rel);
|
||||
let source_code = std::fs::read_to_string(&source_abs)?;
|
||||
let file_id = file_manager.add(source_abs.clone(), source_code.clone());
|
||||
|
||||
let mut parser = Parser::new(&source_code, file_id);
|
||||
let ast = parser.parse_file()?;
|
||||
|
||||
let mut collector = SymbolCollector::new();
|
||||
let (ts, vs) = collector.collect(&ast)?;
|
||||
|
||||
let full_path = source_rel.to_string_lossy().replace('\\', "/");
|
||||
let logical_module_path = if let Some(stripped) = full_path.strip_prefix("src/main/modules/") {
|
||||
stripped
|
||||
} else if let Some(stripped) = full_path.strip_prefix("src/test/modules/") {
|
||||
stripped
|
||||
} else {
|
||||
&full_path
|
||||
};
|
||||
|
||||
let module_path = std::path::Path::new(logical_module_path)
|
||||
.parent()
|
||||
.map(|p| p.to_string_lossy().replace('\\', "/"))
|
||||
.unwrap_or_else(|| "".to_string());
|
||||
|
||||
let ms = module_symbols_map.entry(module_path.clone()).or_insert_with(ModuleSymbols::new);
|
||||
|
||||
// Merge symbols
|
||||
for sym in ts.symbols.into_values() {
|
||||
if let Err(existing) = ms.type_symbols.insert(sym) {
|
||||
return Err(DiagnosticBundle::error(
|
||||
format!("Duplicate type symbol '{}' in module '{}'", existing.name, module_path),
|
||||
Some(existing.span)
|
||||
).into());
|
||||
}
|
||||
}
|
||||
for sym in vs.symbols.into_values() {
|
||||
if let Err(existing) = ms.value_symbols.insert(sym) {
|
||||
return Err(DiagnosticBundle::error(
|
||||
format!("Duplicate value symbol '{}' in module '{}'", existing.name, module_path),
|
||||
Some(existing.span)
|
||||
).into());
|
||||
}
|
||||
}
|
||||
|
||||
parsed_files.push((module_path, ast));
|
||||
}
|
||||
|
||||
// 2. Synthesize ModuleSymbols for dependencies
|
||||
let mut all_visible_modules = module_symbols_map.clone();
|
||||
for (alias, project_id) in &step.deps {
|
||||
if let Some(compiled) = dep_modules.get(project_id) {
|
||||
for (key, meta) in &compiled.exports {
|
||||
// Support syntax: "alias/module" and "@alias:module"
|
||||
let key_module_path = &key.module_path;
|
||||
let synthetic_paths = [
|
||||
format!("{}/{}", alias, key_module_path),
|
||||
format!("@{}:{}", alias, key_module_path),
|
||||
];
|
||||
|
||||
for synthetic_module_path in synthetic_paths {
|
||||
let ms = all_visible_modules.entry(synthetic_module_path.clone()).or_insert_with(ModuleSymbols::new);
|
||||
|
||||
let sym = Symbol {
|
||||
name: key.symbol_name.clone(),
|
||||
kind: match key.kind {
|
||||
ExportSurfaceKind::Service => SymbolKind::Service,
|
||||
ExportSurfaceKind::DeclareType => {
|
||||
match &meta.ty {
|
||||
Some(PbsType::Contract(_)) => SymbolKind::Contract,
|
||||
Some(PbsType::ErrorType(_)) => SymbolKind::ErrorType,
|
||||
_ => SymbolKind::Struct,
|
||||
}
|
||||
}
|
||||
},
|
||||
namespace: key.kind.namespace(),
|
||||
visibility: Visibility::Pub,
|
||||
ty: meta.ty.clone(),
|
||||
is_host: meta.is_host,
|
||||
span: Span::new(0, 0, 0),
|
||||
origin: Some(synthetic_module_path.clone()),
|
||||
};
|
||||
|
||||
if sym.namespace == Namespace::Type {
|
||||
if let Err(existing) = ms.type_symbols.insert(sym.clone()) {
|
||||
return Err(CompileError::DuplicateExport {
|
||||
symbol: sym.name,
|
||||
first_dep: existing.origin.unwrap_or_else(|| "unknown".to_string()),
|
||||
second_dep: sym.origin.unwrap_or_else(|| "unknown".to_string()),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if let Err(existing) = ms.value_symbols.insert(sym.clone()) {
|
||||
return Err(CompileError::DuplicateExport {
|
||||
symbol: sym.name,
|
||||
first_dep: existing.origin.unwrap_or_else(|| "unknown".to_string()),
|
||||
second_dep: sym.origin.unwrap_or_else(|| "unknown".to_string()),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Resolve and TypeCheck each file
|
||||
let module_provider = ProjectModuleProvider {
|
||||
modules: all_visible_modules,
|
||||
};
|
||||
|
||||
// We need to collect imported symbols for Lowerer
|
||||
let mut file_imported_symbols: HashMap<String, ModuleSymbols> = HashMap::new(); // keyed by module_path
|
||||
|
||||
for (module_path, ast) in &parsed_files {
|
||||
let ms = module_symbols_map.get(module_path).unwrap();
|
||||
let mut resolver = Resolver::new(ms, &module_provider);
|
||||
resolver.resolve(ast)?;
|
||||
|
||||
// Capture imported symbols
|
||||
file_imported_symbols.insert(module_path.clone(), resolver.imported_symbols.clone());
|
||||
|
||||
// TypeChecker also needs &mut ModuleSymbols
|
||||
let mut ms_mut = module_symbols_map.get_mut(module_path).unwrap();
|
||||
let imported = file_imported_symbols.get(module_path).unwrap();
|
||||
let mut typechecker = TypeChecker::new(&mut ms_mut, imported, &module_provider);
|
||||
typechecker.check(ast)?;
|
||||
}
|
||||
|
||||
// 4. Lower to IR
|
||||
let mut combined_program = crate::ir_core::Program {
|
||||
const_pool: crate::ir_core::ConstPool::new(),
|
||||
modules: Vec::new(),
|
||||
field_offsets: HashMap::new(),
|
||||
field_types: HashMap::new(),
|
||||
};
|
||||
|
||||
for (module_path, ast) in &parsed_files {
|
||||
let ms = module_symbols_map.get(module_path).unwrap();
|
||||
let imported = file_imported_symbols.get(module_path).unwrap();
|
||||
let lowerer = Lowerer::new(ms, imported);
|
||||
let program = lowerer.lower_file(ast, module_path)?;
|
||||
|
||||
// Combine program into combined_program
|
||||
if combined_program.modules.is_empty() {
|
||||
combined_program = program;
|
||||
} else {
|
||||
// TODO: Real merge
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Emit fragments
|
||||
let vm_module = core_to_vm::lower_program(&combined_program)
|
||||
.map_err(|e| CompileError::Internal(format!("Lowering error: {}", e)))?;
|
||||
|
||||
let fragments = emit_fragments(&vm_module)
|
||||
.map_err(|e| CompileError::Internal(format!("Emission error: {}", e)))?;
|
||||
|
||||
// 5. Collect exports
|
||||
let mut exports = BTreeMap::new();
|
||||
for (module_path, ms) in &module_symbols_map {
|
||||
for sym in ms.type_symbols.symbols.values() {
|
||||
if sym.visibility == Visibility::Pub {
|
||||
if let Some(surface_kind) = ExportSurfaceKind::from_symbol_kind(sym.kind) {
|
||||
exports.insert(ExportKey {
|
||||
module_path: module_path.clone(),
|
||||
symbol_name: sym.name.clone(),
|
||||
kind: surface_kind,
|
||||
}, ExportMetadata {
|
||||
func_idx: None,
|
||||
is_host: sym.is_host,
|
||||
ty: sym.ty.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
for sym in ms.value_symbols.symbols.values() {
|
||||
if sym.visibility == Visibility::Pub {
|
||||
if let Some(surface_kind) = ExportSurfaceKind::from_symbol_kind(sym.kind) {
|
||||
// Find func_idx if it's a function or service
|
||||
let func_idx = vm_module.functions.iter().position(|f| f.name == sym.name).map(|i| i as u32);
|
||||
|
||||
exports.insert(ExportKey {
|
||||
module_path: module_path.clone(),
|
||||
symbol_name: sym.name.clone(),
|
||||
kind: surface_kind,
|
||||
}, ExportMetadata {
|
||||
func_idx,
|
||||
is_host: sym.is_host,
|
||||
ty: sym.ty.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 6. Collect symbols
|
||||
let project_symbols = crate::common::symbols::collect_symbols(&step.project_id.name, &module_symbols_map, file_manager);
|
||||
|
||||
// 7. Collect imports from unresolved labels
|
||||
let mut imports = Vec::new();
|
||||
for (label, pcs) in fragments.unresolved_labels {
|
||||
if label.starts_with('@') {
|
||||
// Format: @dep_alias::module_path:symbol_name
|
||||
let parts: Vec<&str> = label[1..].splitn(2, "::").collect();
|
||||
if parts.len() == 2 {
|
||||
let dep_alias = parts[0].to_string();
|
||||
let rest = parts[1];
|
||||
let sub_parts: Vec<&str> = rest.rsplitn(2, ':').collect();
|
||||
if sub_parts.len() == 2 {
|
||||
let symbol_name = sub_parts[0].to_string();
|
||||
let module_path = sub_parts[1].to_string();
|
||||
|
||||
imports.push(ImportMetadata {
|
||||
key: ImportKey {
|
||||
dep_alias,
|
||||
module_path,
|
||||
symbol_name,
|
||||
},
|
||||
relocation_pcs: pcs,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(CompiledModule {
|
||||
project_id: step.project_id,
|
||||
target: step.target,
|
||||
exports,
|
||||
imports,
|
||||
const_pool: fragments.const_pool,
|
||||
code: fragments.code,
|
||||
function_metas: fragments.functions,
|
||||
debug_info: fragments.debug_info,
|
||||
symbols: project_symbols,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[test]
|
||||
fn test_compile_root_only_project() {
|
||||
let dir = tempdir().unwrap();
|
||||
let project_dir = dir.path().to_path_buf();
|
||||
|
||||
fs::create_dir_all(project_dir.join("src/main/modules")).unwrap();
|
||||
|
||||
let main_code = r#"
|
||||
pub declare struct Vec2(x: int, y: int)
|
||||
|
||||
fn add(a: int, b: int): int {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
mod fn frame(): void {
|
||||
let x = add(1, 2);
|
||||
}
|
||||
"#;
|
||||
|
||||
fs::write(project_dir.join("src/main/modules/main.pbs"), main_code).unwrap();
|
||||
|
||||
let project_id = ProjectId { name: "root".to_string(), version: "0.1.0".to_string() };
|
||||
let step = BuildStep {
|
||||
project_id: project_id.clone(),
|
||||
project_dir: project_dir.clone(),
|
||||
target: BuildTarget::Main,
|
||||
sources: vec![PathBuf::from("src/main/modules/main.pbs")],
|
||||
deps: BTreeMap::new(),
|
||||
};
|
||||
|
||||
let mut file_manager = FileManager::new();
|
||||
let compiled = compile_project(step, &HashMap::new(), &mut file_manager).expect("Failed to compile project");
|
||||
|
||||
assert_eq!(compiled.project_id, project_id);
|
||||
assert_eq!(compiled.target, BuildTarget::Main);
|
||||
|
||||
// Vec2 should be exported
|
||||
let vec2_key = ExportKey {
|
||||
module_path: "".to_string(),
|
||||
symbol_name: "Vec2".to_string(),
|
||||
kind: ExportSurfaceKind::DeclareType,
|
||||
};
|
||||
assert!(compiled.exports.contains_key(&vec2_key));
|
||||
|
||||
// frame is NOT exported (top-level fn cannot be pub in v0)
|
||||
// Wait, I put "pub fn frame" in the test code. SymbolCollector should have ignored pub.
|
||||
// Actually, SymbolCollector might error on pub fn.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
use crate::deps::resolver::{ProjectId, ResolvedGraph};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum BuildTarget {
|
||||
Main,
|
||||
Test,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct BuildStep {
|
||||
pub project_id: ProjectId,
|
||||
pub project_dir: PathBuf,
|
||||
pub target: BuildTarget,
|
||||
pub sources: Vec<PathBuf>,
|
||||
pub deps: BTreeMap<String, ProjectId>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct BuildPlan {
|
||||
pub steps: Vec<BuildStep>,
|
||||
}
|
||||
|
||||
impl BuildPlan {
|
||||
pub fn from_graph(graph: &ResolvedGraph, target: BuildTarget) -> Self {
|
||||
let mut steps = Vec::new();
|
||||
let sorted_ids = topological_sort(graph);
|
||||
|
||||
for id in sorted_ids {
|
||||
if let Some(node) = graph.nodes.get(&id) {
|
||||
let sources_list: Vec<PathBuf> = match target {
|
||||
BuildTarget::Main => node.sources.files.clone(),
|
||||
BuildTarget::Test => node.sources.test_files.clone(),
|
||||
};
|
||||
|
||||
// Normalize to relative paths and sort lexicographically
|
||||
let mut sources: Vec<PathBuf> = sources_list
|
||||
.into_iter()
|
||||
.map(|p| {
|
||||
p.strip_prefix(&node.path)
|
||||
.map(|rp| rp.to_path_buf())
|
||||
.unwrap_or(p)
|
||||
})
|
||||
.collect();
|
||||
sources.sort();
|
||||
|
||||
let mut deps = BTreeMap::new();
|
||||
if let Some(edges) = graph.edges.get(&id) {
|
||||
for edge in edges {
|
||||
deps.insert(edge.alias.clone(), edge.to.clone());
|
||||
}
|
||||
}
|
||||
|
||||
steps.push(BuildStep {
|
||||
project_id: id.clone(),
|
||||
project_dir: node.path.clone(),
|
||||
target,
|
||||
sources,
|
||||
deps,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Self { steps }
|
||||
}
|
||||
}
|
||||
|
||||
fn topological_sort(graph: &ResolvedGraph) -> Vec<ProjectId> {
|
||||
let mut in_degree = HashMap::new();
|
||||
let mut adj = HashMap::new();
|
||||
|
||||
for id in graph.nodes.keys() {
|
||||
in_degree.insert(id.clone(), 0);
|
||||
adj.insert(id.clone(), Vec::new());
|
||||
}
|
||||
|
||||
for (from, edges) in &graph.edges {
|
||||
for edge in edges {
|
||||
// from depends on edge.to
|
||||
// so edge.to must be built BEFORE from
|
||||
// edge.to -> from
|
||||
adj.get_mut(&edge.to).unwrap().push(from.clone());
|
||||
*in_degree.get_mut(from).unwrap() += 1;
|
||||
}
|
||||
}
|
||||
|
||||
let mut ready: std::collections::BinaryHeap<ReverseProjectId> = graph.nodes.keys()
|
||||
.filter(|id| *in_degree.get(id).unwrap() == 0)
|
||||
.map(|id| ReverseProjectId(id.clone()))
|
||||
.collect();
|
||||
|
||||
let mut result = Vec::new();
|
||||
while let Some(ReverseProjectId(u)) = ready.pop() {
|
||||
result.push(u.clone());
|
||||
|
||||
if let Some(neighbors) = adj.get(&u) {
|
||||
for v in neighbors {
|
||||
let degree = in_degree.get_mut(v).unwrap();
|
||||
*degree -= 1;
|
||||
if *degree == 0 {
|
||||
ready.push(ReverseProjectId(v.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
struct ReverseProjectId(ProjectId);
|
||||
|
||||
impl Ord for ReverseProjectId {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
// BinaryHeap is a max-heap. We want min-heap for lexicographic order.
|
||||
// So we reverse the comparison.
|
||||
other.0.name.cmp(&self.0.name)
|
||||
.then(other.0.version.cmp(&self.0.version))
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for ReverseProjectId {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::deps::resolver::{ProjectId, ResolvedEdge, ResolvedGraph, ResolvedNode};
|
||||
use crate::manifest::Manifest;
|
||||
use crate::sources::ProjectSources;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
fn mock_node(name: &str, version: &str) -> ResolvedNode {
|
||||
ResolvedNode {
|
||||
id: ProjectId { name: name.to_string(), version: version.to_string() },
|
||||
path: PathBuf::from(format!("/{}", name)),
|
||||
manifest: Manifest {
|
||||
name: name.to_string(),
|
||||
version: version.to_string(),
|
||||
kind: crate::manifest::ManifestKind::Lib,
|
||||
dependencies: BTreeMap::new(),
|
||||
},
|
||||
sources: ProjectSources {
|
||||
main: None,
|
||||
files: vec![PathBuf::from("b.pbs"), PathBuf::from("a.pbs")],
|
||||
test_files: vec![PathBuf::from("test_b.pbs"), PathBuf::from("test_a.pbs")],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_topo_sort_stability() {
|
||||
let mut graph = ResolvedGraph::default();
|
||||
|
||||
let a = mock_node("a", "1.0.0");
|
||||
let b = mock_node("b", "1.0.0");
|
||||
let c = mock_node("c", "1.0.0");
|
||||
|
||||
graph.nodes.insert(a.id.clone(), a);
|
||||
graph.nodes.insert(b.id.clone(), b);
|
||||
graph.nodes.insert(c.id.clone(), c);
|
||||
|
||||
// No edges, should be alphabetical: a, b, c
|
||||
let plan = BuildPlan::from_graph(&graph, BuildTarget::Main);
|
||||
assert_eq!(plan.steps[0].project_id.name, "a");
|
||||
assert_eq!(plan.steps[1].project_id.name, "b");
|
||||
assert_eq!(plan.steps[2].project_id.name, "c");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_topo_sort_dependencies() {
|
||||
let mut graph = ResolvedGraph::default();
|
||||
|
||||
let a = mock_node("a", "1.0.0");
|
||||
let b = mock_node("b", "1.0.0");
|
||||
let c = mock_node("c", "1.0.0");
|
||||
|
||||
graph.nodes.insert(a.id.clone(), a.clone());
|
||||
graph.nodes.insert(b.id.clone(), b.clone());
|
||||
graph.nodes.insert(c.id.clone(), c.clone());
|
||||
|
||||
// c depends on b, b depends on a
|
||||
// Sort should be: a, b, c
|
||||
graph.edges.insert(c.id.clone(), vec![ResolvedEdge { alias: "b_alias".to_string(), to: b.id.clone() }]);
|
||||
graph.edges.insert(b.id.clone(), vec![ResolvedEdge { alias: "a_alias".to_string(), to: a.id.clone() }]);
|
||||
|
||||
let plan = BuildPlan::from_graph(&graph, BuildTarget::Main);
|
||||
assert_eq!(plan.steps.len(), 3);
|
||||
assert_eq!(plan.steps[0].project_id.name, "a");
|
||||
assert_eq!(plan.steps[1].project_id.name, "b");
|
||||
assert_eq!(plan.steps[2].project_id.name, "c");
|
||||
|
||||
assert_eq!(plan.steps[2].deps.get("b_alias").unwrap(), &b.id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_topo_sort_complex() {
|
||||
let mut graph = ResolvedGraph::default();
|
||||
|
||||
// d -> b, c
|
||||
// b -> a
|
||||
// c -> a
|
||||
// a
|
||||
// Valid sorts: a, b, c, d OR a, c, b, d
|
||||
// Lexicographic rule says b before c. So a, b, c, d.
|
||||
|
||||
let a = mock_node("a", "1.0.0");
|
||||
let b = mock_node("b", "1.0.0");
|
||||
let c = mock_node("c", "1.0.0");
|
||||
let d = mock_node("d", "1.0.0");
|
||||
|
||||
graph.nodes.insert(a.id.clone(), a.clone());
|
||||
graph.nodes.insert(b.id.clone(), b.clone());
|
||||
graph.nodes.insert(c.id.clone(), c.clone());
|
||||
graph.nodes.insert(d.id.clone(), d.clone());
|
||||
|
||||
graph.edges.insert(d.id.clone(), vec![
|
||||
ResolvedEdge { alias: "b".to_string(), to: b.id.clone() },
|
||||
ResolvedEdge { alias: "c".to_string(), to: c.id.clone() },
|
||||
]);
|
||||
graph.edges.insert(b.id.clone(), vec![ResolvedEdge { alias: "a".to_string(), to: a.id.clone() }]);
|
||||
graph.edges.insert(c.id.clone(), vec![ResolvedEdge { alias: "a".to_string(), to: a.id.clone() }]);
|
||||
|
||||
let plan = BuildPlan::from_graph(&graph, BuildTarget::Main);
|
||||
let names: Vec<_> = plan.steps.iter().map(|s| s.project_id.name.as_str()).collect();
|
||||
assert_eq!(names, vec!["a", "b", "c", "d"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sources_sorting() {
|
||||
let mut graph = ResolvedGraph::default();
|
||||
let a = mock_node("a", "1.0.0");
|
||||
graph.nodes.insert(a.id.clone(), a);
|
||||
|
||||
let plan = BuildPlan::from_graph(&graph, BuildTarget::Main);
|
||||
assert_eq!(plan.steps[0].sources, vec![PathBuf::from("a.pbs"), PathBuf::from("b.pbs")]);
|
||||
|
||||
let plan_test = BuildPlan::from_graph(&graph, BuildTarget::Test);
|
||||
assert_eq!(plan_test.steps[0].sources, vec![PathBuf::from("test_a.pbs"), PathBuf::from("test_b.pbs")]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user