init hard reset, pipeline
This commit is contained in:
@@ -14,14 +14,9 @@ dist = true
|
||||
include = ["../../VERSION.txt"]
|
||||
|
||||
[dependencies]
|
||||
prometeu-bytecode = { path = "../prometeu-bytecode" }
|
||||
prometeu-deps = { path = "../prometeu-deps" }
|
||||
prometeu-core = { path = "../prometeu-core" }
|
||||
prometeu-language-api = { path = "../prometeu-language-api" }
|
||||
clap = { version = "4.5.54", features = ["derive"] }
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = "1.0.149"
|
||||
anyhow = "1.0.100"
|
||||
pathdiff = "0.2.1"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3.10.1"
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
use anyhow::Result;
|
||||
use crate::{BuildMode, PipelineConfig, PipelineInput, PipelineOutput};
|
||||
use anyhow::{Context, Result};
|
||||
use clap::{Parser, Subcommand};
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
use prometeu_deps::{load_sources, prepare_caches, resolve_project, DepsConfig};
|
||||
use crate::pipeline::run_phases;
|
||||
|
||||
/// Command line interface for the Prometeu Compiler.
|
||||
#[derive(Parser)]
|
||||
#[command(name = "prometeu-build-pipeline")]
|
||||
#[command(version, about = "Official compiler for the PROMETEU Virtual Machine", long_about = None)]
|
||||
#[command(name = "prometeu")]
|
||||
#[command(version, about = "PROMETEU toolchain entrypoint", long_about = None)]
|
||||
pub struct Cli {
|
||||
/// The action to perform (build or verify).
|
||||
#[command(subcommand)]
|
||||
pub command: Commands,
|
||||
}
|
||||
@@ -15,16 +17,13 @@ pub struct Cli {
|
||||
/// Available subcommands for the compiler.
|
||||
#[derive(Subcommand)]
|
||||
pub enum Commands {
|
||||
/// Builds a Prometeu project by compiling source code into a PBC file.
|
||||
/// Builds a Prometeu project by compiling source code into an artifact (pbc/program image).
|
||||
Build {
|
||||
/// Path to the project root directory.
|
||||
project_dir: PathBuf,
|
||||
|
||||
/// Explicit path to the entry file (defaults to src/main.pbs).
|
||||
#[arg(short, long)]
|
||||
entry: Option<PathBuf>,
|
||||
|
||||
/// Path to save the compiled .pbc file.
|
||||
/// Path to save the compiled artifact.
|
||||
/// If omitted, deps/pipeline decide a default under target/ or dist/.
|
||||
#[arg(short, long)]
|
||||
out: Option<PathBuf>,
|
||||
|
||||
@@ -32,25 +31,21 @@ pub enum Commands {
|
||||
#[arg(long, default_value_t = true)]
|
||||
emit_symbols: bool,
|
||||
|
||||
/// Disable symbol generation.
|
||||
#[arg(long)]
|
||||
no_symbols: bool,
|
||||
|
||||
/// Whether to generate a .disasm file for debugging.
|
||||
#[arg(long, default_value_t = true)]
|
||||
emit_disasm: bool,
|
||||
|
||||
/// Disable disassembly generation.
|
||||
#[arg(long)]
|
||||
no_disasm: bool,
|
||||
|
||||
/// Whether to explain the dependency resolution process.
|
||||
#[arg(long)]
|
||||
explain_deps: bool,
|
||||
|
||||
/// Build mode (debug/release).
|
||||
#[arg(long, default_value = "debug")]
|
||||
mode: String,
|
||||
},
|
||||
/// Verifies if a Prometeu project is syntactically and semantically valid without emitting code.
|
||||
|
||||
/// Verifies if a Prometeu project is valid without emitting code.
|
||||
Verify {
|
||||
/// Path to the project root directory.
|
||||
project_dir: PathBuf,
|
||||
|
||||
/// Whether to explain the dependency resolution process.
|
||||
@@ -59,8 +54,6 @@ pub enum Commands {
|
||||
},
|
||||
}
|
||||
|
||||
/// Main entry point for the compiler library's execution logic.
|
||||
/// Parses CLI arguments and dispatches to the appropriate compiler functions.
|
||||
pub fn run() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
@@ -69,20 +62,126 @@ pub fn run() -> Result<()> {
|
||||
project_dir,
|
||||
out,
|
||||
emit_disasm,
|
||||
no_disasm,
|
||||
emit_symbols,
|
||||
no_symbols,
|
||||
explain_deps,
|
||||
..
|
||||
mode,
|
||||
} => {
|
||||
let build_mode = parse_mode(&mode)?;
|
||||
let cfg = PipelineConfig {
|
||||
mode: build_mode,
|
||||
enable_cache: true,
|
||||
enable_frontends: false,
|
||||
};
|
||||
|
||||
let pipeline_output = run_pipeline(cfg, &project_dir, explain_deps)
|
||||
.context("pipeline: failed to execute pipeline")?;
|
||||
|
||||
for diagnostics in &pipeline_output.diagnostics {
|
||||
eprintln!("{:?}", diagnostics);
|
||||
}
|
||||
|
||||
let emit_opts = EmitOptions {
|
||||
out,
|
||||
emit_symbols,
|
||||
emit_disasm,
|
||||
};
|
||||
|
||||
emit_artifacts(&emit_opts, &pipeline_output)
|
||||
.context("emit: failed to write artifacts")?;
|
||||
|
||||
if pipeline_output.diagnostics.iter().any(|d| d.severity.is_error()) {
|
||||
anyhow::bail!("build failed due to errors");
|
||||
}
|
||||
}
|
||||
|
||||
Commands::Verify {
|
||||
project_dir,
|
||||
explain_deps
|
||||
explain_deps,
|
||||
} => {
|
||||
let cfg = PipelineConfig {
|
||||
mode: BuildMode::Test,
|
||||
enable_cache: true,
|
||||
enable_frontends: false,
|
||||
};
|
||||
|
||||
let pipeline_output = run_pipeline(cfg, &project_dir, explain_deps)
|
||||
.context("pipeline: failed to execute pipeline")?;
|
||||
|
||||
for diagnostic in &pipeline_output.diagnostics {
|
||||
eprintln!("{:?}", diagnostic);
|
||||
}
|
||||
|
||||
if pipeline_output.diagnostics.iter().any(|d| d.severity.is_error()) {
|
||||
anyhow::bail!("verify failed due to errors");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn run_pipeline(cfg: PipelineConfig, project_dir: &Path, explain_deps: bool) -> anyhow::Result<PipelineOutput> {
|
||||
let deps_cfg = DepsConfig {
|
||||
explain: explain_deps,
|
||||
cache_dir: Default::default(),
|
||||
registry_dirs: vec![],
|
||||
};
|
||||
|
||||
let resolved = resolve_project(&deps_cfg, project_dir)
|
||||
.with_context(|| format!("deps: failed to resolve project at {:?}", project_dir))?;
|
||||
|
||||
// resolved deve te dar pelo menos:
|
||||
// - graph
|
||||
// - stack (deps-first topo order)
|
||||
// - (opcional) caches já validados
|
||||
|
||||
let caches = if cfg.enable_cache {
|
||||
Some(
|
||||
prepare_caches(&deps_cfg, &resolved.cache_plan)
|
||||
.context("deps: failed to prepare caches")?
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let sources = load_sources(&deps_cfg, &resolved)
|
||||
.context("deps: failed to load sources")?;
|
||||
|
||||
let input = PipelineInput {
|
||||
graph: resolved.graph,
|
||||
stack: resolved.stack,
|
||||
sources,
|
||||
caches,
|
||||
};
|
||||
|
||||
Ok(run_phases(cfg, input))
|
||||
}
|
||||
|
||||
/// Parse `--mode` from CLI.
|
||||
fn parse_mode(s: &str) -> Result<BuildMode> {
|
||||
match s.to_ascii_lowercase().as_str() {
|
||||
"debug" => Ok(BuildMode::Debug),
|
||||
"release" => Ok(BuildMode::Release),
|
||||
"test" => Ok(BuildMode::Test),
|
||||
other => anyhow::bail!("invalid --mode '{}': expected debug|release|test", other),
|
||||
}
|
||||
}
|
||||
|
||||
/// Emission options
|
||||
struct EmitOptions {
|
||||
out: Option<PathBuf>,
|
||||
emit_symbols: bool,
|
||||
emit_disasm: bool,
|
||||
}
|
||||
|
||||
/// Placeholder emit function.
|
||||
/// In hard reset, this can be a no-op until backend exists.
|
||||
fn emit_artifacts(_opts: &EmitOptions, _outp: &PipelineOutput) -> Result<()> {
|
||||
// Later:
|
||||
// - decide output dir (opts.out or default)
|
||||
// - write .pbc / program image
|
||||
// - write symbols.json (if exists)
|
||||
// - write disasm (if exists)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum BuildMode {
|
||||
Debug,
|
||||
Release,
|
||||
Test,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PipelineConfig {
|
||||
pub mode: BuildMode,
|
||||
pub enable_cache: bool,
|
||||
pub enable_frontends: bool,
|
||||
}
|
||||
|
||||
impl Default for PipelineConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
mode: BuildMode::Debug,
|
||||
enable_cache: true,
|
||||
enable_frontends: false, // Hard Reset default: pipeline runs with no FE.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
use std::any::Any;
|
||||
use prometeu_core::{Diagnostic, FileDB, FileId, NameInterner, ProjectId};
|
||||
use prometeu_deps::BuildStack;
|
||||
|
||||
/// Per-project arena slot created from the BuildStack order.
|
||||
/// The pipeline owns this vector and indexes it by stack position.
|
||||
#[derive(Debug)]
|
||||
pub struct ProjectCtx {
|
||||
pub project_id: ProjectId,
|
||||
|
||||
/// FileIds inserted into `source_db` for this project.
|
||||
pub files: Vec<FileId>,
|
||||
|
||||
/// Frontend output (TypedHIRBundle or similar) - intentionally opaque.
|
||||
pub frontend_out: Option<Box<dyn Any>>,
|
||||
|
||||
/// Backend output (ProgramImage / BytecodeModule / Artifact).
|
||||
/// Keep as opaque until you finalize your bytecode/image crate.
|
||||
pub backend_out: Option<Box<dyn Any>>,
|
||||
}
|
||||
|
||||
impl ProjectCtx {
|
||||
pub fn new(project_id: ProjectId) -> Self {
|
||||
Self {
|
||||
project_id,
|
||||
files: Vec::new(),
|
||||
frontend_out: None,
|
||||
backend_out: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Pipeline context (in-memory state).
|
||||
/// Arena-friendly: uses Vec + IDs as the main storage.
|
||||
#[derive(Debug)]
|
||||
pub struct PipelineCtx {
|
||||
pub source_db: FileDB,
|
||||
pub interner: NameInterner,
|
||||
pub diagnostics: Vec<Diagnostic>,
|
||||
|
||||
/// Projects in stack order (deps first).
|
||||
pub projects: Vec<ProjectCtx>,
|
||||
}
|
||||
|
||||
impl PipelineCtx {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
source_db: FileDB::new(),
|
||||
interner: NameInterner::new(),
|
||||
diagnostics: Vec::new(),
|
||||
projects: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_diagnostic(&mut self, d: Diagnostic) {
|
||||
self.diagnostics.push(d);
|
||||
}
|
||||
|
||||
/// Initialize per-project contexts from the BuildStack order.
|
||||
pub fn init_projects_from_stack(&mut self, stack: &BuildStack) {
|
||||
self.projects.clear();
|
||||
self.projects.reserve(stack.projects.len());
|
||||
for p in &stack.projects {
|
||||
self.projects.push(ProjectCtx::new(p.project_id));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn project_ctx_mut(&mut self, index_in_stack: usize) -> &mut ProjectCtx {
|
||||
&mut self.projects[index_in_stack]
|
||||
}
|
||||
|
||||
pub fn project_ctx(&self, index_in_stack: usize) -> &ProjectCtx {
|
||||
&self.projects[index_in_stack]
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,11 @@
|
||||
mod cli;
|
||||
pub mod cli;
|
||||
pub mod config;
|
||||
pub mod ctx;
|
||||
pub mod pipeline;
|
||||
pub mod phases;
|
||||
pub use config::*;
|
||||
|
||||
pub use ctx::*;
|
||||
pub use pipeline::*;
|
||||
pub use cli::run;
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
use crate::{config::PipelineConfig, ctx::PipelineCtx, pipeline::PipelineInput};
|
||||
|
||||
pub fn run(_cfg: &PipelineConfig, _input: &PipelineInput, _ctx: &mut PipelineCtx) {
|
||||
// Hard Reset stub:
|
||||
// - later: consume TypedHIRBundle(s) and lower into ProgramImage/BytecodeModule.
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
use crate::{
|
||||
config::PipelineConfig,
|
||||
ctx::PipelineCtx,
|
||||
pipeline::{PipelineInput},
|
||||
};
|
||||
|
||||
pub fn run(_cfg: &PipelineConfig, input: &PipelineInput, ctx: &mut PipelineCtx) {
|
||||
// Arena init: one ProjectCtx per project in stack order.
|
||||
ctx.init_projects_from_stack(&input.stack);
|
||||
|
||||
// NOTE: no filesystem, no FE/BE assumptions here.
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
use crate::{config::PipelineConfig, ctx::PipelineCtx, pipeline::PipelineInput};
|
||||
|
||||
pub fn run(cfg: &PipelineConfig, input: &PipelineInput, _ctx: &mut PipelineCtx) {
|
||||
if !cfg.enable_cache {
|
||||
return;
|
||||
}
|
||||
|
||||
// Hard Reset stub:
|
||||
// - input.caches may carry blobs validated by deps.
|
||||
// - pipeline decides *when* to hydrate/use them.
|
||||
// - actual cache IO/validity remains in deps by your rules.
|
||||
let _ = &input.caches;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
use crate::{config::PipelineConfig, ctx::PipelineCtx, pipeline::{Artifacts, PipelineInput}};
|
||||
|
||||
pub fn run(_cfg: &PipelineConfig, _input: &PipelineInput, _ctx: &mut PipelineCtx) -> Artifacts {
|
||||
// Hard Reset stub:
|
||||
// - later: emit build outputs (to FS via deps if you want strict IO centralization).
|
||||
Artifacts::default()
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
use crate::{config::PipelineConfig, ctx::PipelineCtx, pipeline::PipelineInput};
|
||||
|
||||
pub fn run(cfg: &PipelineConfig, _input: &PipelineInput, _ctx: &mut PipelineCtx) {
|
||||
if !cfg.enable_frontends {
|
||||
return;
|
||||
}
|
||||
|
||||
// Hard Reset:
|
||||
// - no FE wired yet.
|
||||
// - later: iterate projects in stack order and call FE plugin(s).
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
use prometeu_core::{Diagnostic, Severity, Span};
|
||||
use prometeu_deps::LoadedSources;
|
||||
use crate::{
|
||||
config::PipelineConfig,
|
||||
ctx::PipelineCtx,
|
||||
pipeline::PipelineInput,
|
||||
};
|
||||
|
||||
pub fn run(_cfg: &PipelineConfig, input: &PipelineInput, ctx: &mut PipelineCtx) {
|
||||
load_sources(&input.sources, ctx);
|
||||
|
||||
for i in 0..ctx.projects.len() {
|
||||
let is_empty = ctx.projects[i].files.is_empty();
|
||||
|
||||
if is_empty {
|
||||
let proj = &input.stack.projects[i];
|
||||
|
||||
ctx.push_diagnostic(Diagnostic {
|
||||
severity: Severity::Warning,
|
||||
code: "PIPELINE_NO_SOURCES".into(),
|
||||
message: format!(
|
||||
"Project '{}' has no source files loaded.",
|
||||
proj.name
|
||||
),
|
||||
span: Span::none(),
|
||||
related: vec![],
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn load_sources(sources: &LoadedSources, ctx: &mut PipelineCtx) {
|
||||
let stack_len = ctx.projects.len();
|
||||
let src_len = sources.per_project.len();
|
||||
|
||||
// 1) Diagnostic is sizes don't match
|
||||
if src_len != stack_len {
|
||||
ctx.push_diagnostic(Diagnostic {
|
||||
severity: Severity::Error,
|
||||
code: "PIPELINE_SOURCES_STACK_LEN_MISMATCH".into(),
|
||||
message: format!(
|
||||
"LoadedSources.per_project len ({}) does not match BuildStack len ({}).",
|
||||
src_len, stack_len
|
||||
),
|
||||
span: Span::none(),
|
||||
related: vec![],
|
||||
});
|
||||
}
|
||||
|
||||
// 2) Process the bare minimum (don't panic, just keep running with diagnostics)
|
||||
let n = stack_len.min(src_len);
|
||||
|
||||
for i in 0..n {
|
||||
let expected = ctx.projects[i].project_id;
|
||||
let got = sources.per_project[i].project_id;
|
||||
|
||||
if got != expected {
|
||||
ctx.push_diagnostic(Diagnostic {
|
||||
severity: Severity::Error,
|
||||
code: "PIPELINE_SOURCES_STACK_ORDER_MISMATCH".into(),
|
||||
message: format!(
|
||||
"LoadedSources is not aligned with BuildStack at index {}: expected project_id {:?}, got {:?}.",
|
||||
i, expected, got
|
||||
),
|
||||
span: Span::none(),
|
||||
related: vec![],
|
||||
});
|
||||
|
||||
// there is no fix tolerance here, if it is wrong, it is wrong
|
||||
// just catch as much diagnostics as possible before "crashing"
|
||||
continue;
|
||||
}
|
||||
|
||||
for f in &sources.per_project[i].files {
|
||||
let file_id = ctx.source_db.upsert(&f.uri, &f.text);
|
||||
ctx.projects[i].files.push(file_id);
|
||||
}
|
||||
}
|
||||
|
||||
// 3) If any LoadSources remains, it is a deps bug
|
||||
if src_len > stack_len {
|
||||
for extra in &sources.per_project[stack_len..] {
|
||||
ctx.push_diagnostic(Diagnostic {
|
||||
severity: Severity::Error,
|
||||
code: "PIPELINE_SOURCES_EXTRA_PROJECT".into(),
|
||||
message: format!(
|
||||
"LoadedSources contains extra project_id {:?} not present in BuildStack.",
|
||||
extra.project_id
|
||||
),
|
||||
span: Span::none(),
|
||||
related: vec![],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 4) If missing inputs, it is another deps bug...
|
||||
if stack_len > src_len {
|
||||
let mut diagnostics: Vec<Diagnostic> = Vec::new();
|
||||
for missing in &ctx.projects[src_len..] {
|
||||
diagnostics.push(Diagnostic {
|
||||
severity: Severity::Error,
|
||||
code: "PIPELINE_SOURCES_MISSING_PROJECT".into(),
|
||||
message: format!(
|
||||
"LoadedSources missing sources for project_id {:?} present in BuildStack.",
|
||||
missing.project_id
|
||||
),
|
||||
span: Span::none(),
|
||||
related: vec![],
|
||||
});
|
||||
}
|
||||
for diagnostic in diagnostics {
|
||||
ctx.push_diagnostic(diagnostic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
pub mod boot;
|
||||
pub mod load_source;
|
||||
pub mod cache;
|
||||
pub mod frontend;
|
||||
pub mod backend;
|
||||
pub mod emit;
|
||||
@@ -0,0 +1,65 @@
|
||||
use std::path::Path;
|
||||
use anyhow::Context;
|
||||
use prometeu_core::Diagnostic;
|
||||
use prometeu_deps::{load_sources, prepare_caches, resolve_project, BuildStack, CacheBlobs, DepsConfig, LoadedSources, ResolvedGraph};
|
||||
use crate::{config::PipelineConfig, ctx::PipelineCtx, phases};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PipelineInput {
|
||||
pub graph: ResolvedGraph,
|
||||
pub stack: BuildStack,
|
||||
pub sources: LoadedSources,
|
||||
pub caches: Option<CacheBlobs>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct PipelineStats {
|
||||
pub projects_count: usize,
|
||||
pub files_count: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct Artifacts {
|
||||
// placeholder: later include produced ProgramImage(s), debug bundles, logs, etc.
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct PipelineOutput {
|
||||
pub diagnostics: Vec<Diagnostic>,
|
||||
pub artifacts: Artifacts,
|
||||
pub stats: PipelineStats,
|
||||
}
|
||||
|
||||
pub(crate) fn run_phases(cfg: PipelineConfig, input: PipelineInput) -> PipelineOutput {
|
||||
let mut ctx = PipelineCtx::new();
|
||||
|
||||
// Boot: create project slots in arena order.
|
||||
phases::boot::run(&cfg, &input, &mut ctx);
|
||||
|
||||
// Load source: populate FileDB from LoadedSources.
|
||||
phases::load_source::run(&cfg, &input, &mut ctx);
|
||||
|
||||
// Cache hydrate (stub for now).
|
||||
phases::cache::run(&cfg, &input, &mut ctx);
|
||||
|
||||
// Frontend phase (stub / optional).
|
||||
phases::frontend::run(&cfg, &input, &mut ctx);
|
||||
|
||||
// Backend phase (stub).
|
||||
phases::backend::run(&cfg, &input, &mut ctx);
|
||||
|
||||
// Emit phase (stub).
|
||||
let artifacts = phases::emit::run(&cfg, &input, &mut ctx);
|
||||
|
||||
// Stats (basic).
|
||||
let mut stats = PipelineStats::default();
|
||||
stats.projects_count = ctx.projects.len();
|
||||
stats.files_count = ctx.projects.iter().map(|p| p.files.len()).sum();
|
||||
|
||||
PipelineOutput {
|
||||
diagnostics: ctx.diagnostics,
|
||||
artifacts,
|
||||
stats,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user