first commit
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
[package]
|
||||
name = "prometeu-build-pipeline"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
[[bin]]
|
||||
name = "prometeu-build-pipeline"
|
||||
path = "src/main.rs"
|
||||
|
||||
[package.metadata.dist]
|
||||
dist = true
|
||||
include = ["../../VERSION.txt"]
|
||||
|
||||
[dependencies]
|
||||
prometeu-deps = { path = "../prometeu-deps" }
|
||||
prometeu-core = { path = "../prometeu-core" }
|
||||
prometeu-languages-registry = { path = "../languages/prometeu-languages-registry" }
|
||||
clap = { version = "4.5.54", features = ["derive"] }
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = "1.0.149"
|
||||
anyhow = "1.0.100"
|
||||
camino = "1.2.2"
|
||||
@@ -0,0 +1,158 @@
|
||||
use crate::pipeline::run_phases;
|
||||
use crate::{BuildMode, PipelineConfig, PipelineInput, PipelineOutput};
|
||||
use anyhow::{Context, Result};
|
||||
use clap::{Parser, Subcommand};
|
||||
use prometeu_deps::{load_sources, resolve_workspace, DepsConfig};
|
||||
use std::path::{Path, PathBuf};
|
||||
use camino::Utf8Path;
|
||||
use crate::emit_artifacts::{emit_artifacts, EmitOptions};
|
||||
|
||||
/// Command line interface for the Prometeu Compiler.
|
||||
#[derive(Parser)]
|
||||
#[command(name = "prometeu")]
|
||||
#[command(version, about = "PROMETEU toolchain entrypoint", long_about = None)]
|
||||
pub struct Cli {
|
||||
#[command(subcommand)]
|
||||
pub command: Commands,
|
||||
}
|
||||
|
||||
/// Available subcommands for the compiler.
|
||||
#[derive(Subcommand)]
|
||||
pub enum Commands {
|
||||
/// Builds a Prometeu project by compiling source code into an artifact (pbc/program image).
|
||||
Build {
|
||||
/// Path to the project root directory.
|
||||
project_dir: PathBuf,
|
||||
|
||||
/// Path to save the compiled artifact.
|
||||
/// If omitted, deps/pipeline decide a default under target/ or dist/.
|
||||
#[arg(short, long)]
|
||||
out: Option<PathBuf>,
|
||||
|
||||
/// Whether to generate a .json symbols file for source mapping.
|
||||
#[arg(long, default_value_t = true)]
|
||||
emit_symbols: bool,
|
||||
|
||||
/// Whether to generate a .disasm file for debugging.
|
||||
#[arg(long, default_value_t = true)]
|
||||
emit_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 valid without emitting code.
|
||||
Verify {
|
||||
project_dir: PathBuf,
|
||||
|
||||
/// Whether to explain the dependency resolution process.
|
||||
#[arg(long)]
|
||||
explain_deps: bool,
|
||||
},
|
||||
}
|
||||
|
||||
pub fn run() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
match cli.command {
|
||||
Commands::Build {
|
||||
project_dir,
|
||||
out,
|
||||
emit_disasm,
|
||||
emit_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,
|
||||
} => {
|
||||
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) -> Result<PipelineOutput> {
|
||||
let deps_cfg = DepsConfig {
|
||||
explain: explain_deps,
|
||||
cache_dir: Default::default(),
|
||||
registry_dirs: vec![],
|
||||
};
|
||||
|
||||
let project_dir_path_buf = Utf8Path::from_path(project_dir)
|
||||
.with_context(|| format!("deps: failed to convert project_dir to Utf8Path: {:?}", project_dir))?;
|
||||
let resolved = resolve_workspace(&deps_cfg, project_dir_path_buf)
|
||||
.with_context(|| format!("deps: failed to resolve project at {:?}", project_dir))?;
|
||||
|
||||
let sources = load_sources(&deps_cfg, &resolved)
|
||||
.context("deps: failed to load sources")?;
|
||||
|
||||
let input = PipelineInput {
|
||||
graph: resolved.graph,
|
||||
stack: resolved.stack,
|
||||
sources
|
||||
};
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
@@ -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,71 @@
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PipelineCtx {
|
||||
pub source_db: FileDB,
|
||||
pub interner: NameInterner,
|
||||
pub diagnostics: Vec<Diagnostic>,
|
||||
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 project_id in &stack.projects {
|
||||
self.projects.push(ProjectCtx::new(project_id.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
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]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
use std::path::PathBuf;
|
||||
use crate::PipelineOutput;
|
||||
|
||||
pub struct EmitOptions {
|
||||
pub(crate) out: Option<PathBuf>,
|
||||
pub(crate) emit_symbols: bool,
|
||||
pub(crate) emit_disasm: bool,
|
||||
}
|
||||
|
||||
pub fn emit_artifacts(_opts: &EmitOptions, _outp: &PipelineOutput) -> anyhow::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,12 @@
|
||||
pub mod cli;
|
||||
pub mod config;
|
||||
pub mod ctx;
|
||||
pub mod pipeline;
|
||||
pub mod phases;
|
||||
mod emit_artifacts;
|
||||
|
||||
pub use config::*;
|
||||
pub use ctx::*;
|
||||
pub use pipeline::*;
|
||||
pub use cli::run;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
use anyhow::Result;
|
||||
|
||||
/// Main entry point for the Prometeu Compiler binary.
|
||||
/// It delegates execution to the library's `run` function.
|
||||
fn main() -> Result<()> {
|
||||
prometeu_build_pipeline::run()
|
||||
}
|
||||
@@ -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,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,117 @@
|
||||
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 project_id = &input.stack.projects[i];
|
||||
let project_name = input.graph.project(project_id).unwrap().name.clone();
|
||||
|
||||
ctx.push_diagnostic(Diagnostic {
|
||||
severity: Severity::Warning,
|
||||
code: "PIPELINE_NO_SOURCES".into(),
|
||||
message: format!(
|
||||
"Project '{}' has no source files loaded.",
|
||||
project_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 @@
|
||||
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,5 @@
|
||||
pub mod boot;
|
||||
pub mod load_source;
|
||||
pub mod language;
|
||||
pub mod lowering;
|
||||
pub mod emit;
|
||||
@@ -0,0 +1,59 @@
|
||||
use crate::{config::PipelineConfig, ctx::PipelineCtx, phases};
|
||||
use prometeu_core::Diagnostic;
|
||||
use prometeu_deps::{BuildStack, LoadedSources, ResolvedGraph};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PipelineInput {
|
||||
pub graph: ResolvedGraph,
|
||||
pub stack: BuildStack,
|
||||
pub sources: LoadedSources
|
||||
}
|
||||
|
||||
#[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);
|
||||
|
||||
// Frontend phase (stub / optional).
|
||||
phases::language::run(&cfg, &input, &mut ctx);
|
||||
|
||||
// Backend phase (stub).
|
||||
phases::lowering::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