Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,26 @@ if(BUILD_TESTING)
--llvm "${NIL_ENABLE_LLVM}"
)
set_tests_properties(nilbuild.project-contract PROPERTIES LABELS "tools;project;analysis;rc" TIMEOUT 180)
add_test(NAME nilbuild.incremental-contract
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/incremental_build_contract.py"
--builder $<TARGET_FILE:nilbuild> --compiler $<TARGET_FILE:nilcompiler>
--interpreter $<TARGET_FILE:nilinterpreter> --stdlib "${CMAKE_CURRENT_SOURCE_DIR}/nilstd/src"
--llvm "${NIL_ENABLE_LLVM}")
set_tests_properties(nilbuild.incremental-contract PROPERTIES LABELS "tools;project;rc" TIMEOUT 180)
add_test(NAME nilbuild.parallel-process-contract
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/parallel_process_contract.py"
--root "${CMAKE_CURRENT_SOURCE_DIR}" --cxx "${CMAKE_CXX_COMPILER}")
set_tests_properties(nilbuild.parallel-process-contract PROPERTIES LABELS "tools;project;rc" TIMEOUT 60)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
add_test(NAME nilbuild.toolchain-inputs
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/toolchain_inputs_contract.py"
--root "${CMAKE_CURRENT_SOURCE_DIR}" --cxx "${CMAKE_CXX_COMPILER}" --driver "${CMAKE_CXX_COMPILER}")
set_tests_properties(nilbuild.toolchain-inputs PROPERTIES LABELS "tools;project;rc" TIMEOUT 90)
endif()
add_test(NAME nilbuild.analysis-dependencies
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/analysis_dependencies.py"
--compiler $<TARGET_FILE:nilcompiler>)
set_tests_properties(nilbuild.analysis-dependencies PROPERTIES LABELS "tools;analysis;project;rc" TIMEOUT 60)
add_test(
NAME ny.inline-assembly
COMMAND "${Python3_EXECUTABLE}"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ checked bindings across both languages; references and rename are available for
locals whose complete occurrence sets the compiler can certify.

For projects with multiple source modules, add a
[`nil.project.json`](docs/tools/projects.md) manifest. Project check, build and
[`nil.project.nildata`](docs/tools/projects.md) manifest. Project check, build and
run commands use the same import paths and analyzer policy as live checks.

## Examples
Expand Down
71 changes: 71 additions & 0 deletions common/content_digest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

// SHA-256 content identities for build inputs. Timestamps are not cache keys.
#include <array>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include <string>

namespace nil::detail {
class sha256 {
std::array<std::uint32_t, 8> state{0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,
0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19};
std::array<unsigned char, 64> block{};
std::uint64_t bytes = 0;
std::size_t used = 0;
static std::uint32_t rotate(std::uint32_t x, unsigned n) { return (x >> n) | (x << (32 - n)); }
void transform() {
static constexpr std::uint32_t k[] = {
0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2};
std::array<std::uint32_t,64> w{};
for (unsigned i=0; i<16; ++i) w[i]=(std::uint32_t(block[4*i])<<24)|(std::uint32_t(block[4*i+1])<<16)|(std::uint32_t(block[4*i+2])<<8)|block[4*i+3];
for (unsigned i=16; i<64; ++i) {
const auto a=w[i-15], b=w[i-2];
w[i]=w[i-16]+(rotate(a,7)^rotate(a,18)^(a>>3))+w[i-7]+(rotate(b,17)^rotate(b,19)^(b>>10));
}
auto a=state[0],b=state[1],c=state[2],d=state[3],e=state[4],f=state[5],g=state[6],h=state[7];
for (unsigned i=0; i<64; ++i) {
const auto t1=h+(rotate(e,6)^rotate(e,11)^rotate(e,25))+((e&f)^(~e&g))+k[i]+w[i];
const auto t2=(rotate(a,2)^rotate(a,13)^rotate(a,22))+((a&b)^(a&c)^(b&c));
h=g;g=f;f=e;e=d+t1;d=c;c=b;b=a;a=t1+t2;
}
state[0]+=a;state[1]+=b;state[2]+=c;state[3]+=d;state[4]+=e;state[5]+=f;state[6]+=g;state[7]+=h;
}
public:
void update(const char *data, std::size_t length) {
bytes+=length;
for (std::size_t i=0;i<length;++i) { block[used++]=static_cast<unsigned char>(data[i]); if(used==64){transform();used=0;} }
}
std::string finish() {
const auto bits=bytes*8;
const char padding=static_cast<char>(0x80), zero=0;
update(&padding,1);
while(used!=56) update(&zero,1);
for(int i=7;i>=0;--i) { const char byte=static_cast<char>(bits>>(i*8));update(&byte,1); }
std::ostringstream out; out<<std::hex<<std::setfill('0');
for(auto word:state) out<<std::setw(8)<<word;
return out.str();
}
};
inline std::string content_digest(const std::string &text) { sha256 hash; hash.update(text.data(),text.size());return hash.finish(); }
inline std::string file_digest(const std::filesystem::path &path) {
if(!std::filesystem::is_regular_file(path)) throw std::runtime_error("expected a regular build input: "+path.string());
std::ifstream input(path,std::ios::binary);
if(!input) throw std::runtime_error("cannot read build input: "+path.string());
sha256 hash; char buffer[65536];
while(input.read(buffer,sizeof(buffer))||input.gcount()) hash.update(buffer,static_cast<std::size_t>(input.gcount()));
if(input.bad()) throw std::runtime_error("cannot read build input: "+path.string());
return hash.finish();
}
} // namespace nil::detail
46 changes: 43 additions & 3 deletions docs/analysis-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ source: `NIL2001` reports unreachable statements after unconditional exits, and
`NIL2002` reports standalone calls whose standard `Option`/`Result` outcomes
are discarded. These are warnings without automatic fixes. Imported modules
receive compiler checks but are not recursively linted by these rules.
These rules traverse named function/method and test bodies; expression-embedded
lambda bodies remain outside this initial traversal.
These rules traverse named function/method and test bodies, nested lambdas,
and switches in expression position.

`nilcompiler --list-analysis-rules` returns a `nil.analyzers.v1` catalog with
the `nil.default` analyzer and each rule's code, title, description, default
Expand All @@ -169,6 +169,46 @@ declarations/annotations and 1 MiB of metadata, and marks truncation explicitly.
The full JSON response is limited to 8 MiB. These are representation limits,
not a compiler execution sandbox or time budget.

## Build input dependencies

Analysis reports also expose metadata for incremental build clients:

| Field | Meaning |
| --- | --- |
| `dependencies` | Sorted, unique `{path, digest}` entries for consumed native files. `path` is canonical and absolute; `digest` is lowercase hexadecimal SHA-256 of the exact consumed bytes. |
| `dependenciesComplete` | True only after successful disk-based analysis with a complete, consistent dependency list containing the primary source. |
| `compilerFiles` | Sorted paths of regular files mapped into the compiler on Linux, including its executable and loaded shared libraries. Empty when unavailable on another host. |
| `nativeRuntime` | Resolved native runtime archive path, or null when unavailable. |
| `nativeLinker` | Resolved configured native linker driver path, or null when unavailable. |

Dependencies include the primary file, imported source modules, textual
preprocessor includes regardless of filename extension, and binary library
containers. Embedded `nillib://` source identities do not appear as filesystem
dependencies; the containing library supplies their content identity. This
list is independent of `sources`, which remains the bounded diagnostic snapshot
array and is not a complete build input inventory.

The list is bounded to 4,096 distinct files. Failed checks, inconsistent reads,
or exceeding that count set `dependenciesComplete` to false. `--source-stdin`
and `--workspace-stdin` always leave it false because their checked bytes need
not match disk. Clients must also treat missing completeness metadata from an
older compiler as unavailable, not as an empty complete graph.

Digests describe what analysis read; clients must compare them against the
current filesystem before reusing an artifact. They do not establish an
immutable snapshot. Native runtime/linker fields identify tools without hashing
them; a build client accounts for their current contents and its effective
settings separately. On Linux, `compilerFiles` lets a client include the loaded
compiler libraries, such as a dynamically linked LLVM, in its toolchain
identity. Those paths also require content hashing; they are not dependency
digests or a complete inventory of the native linker's own inputs. Fresh
analysis is required to discover
changed import resolution before considering an incremental cache entry.

`nilcompiler --analyze` and editor request/response protocols remain JSON. The
project driver's optional NILData/YAML presentation does not change that IPC
contract.

## Semantic source index

`--analyze --semantic-index` adds a `semantic` member to the analysis report.
Expand Down Expand Up @@ -601,7 +641,7 @@ and applies fixes only to the same document version that produced them.
Compiler `extraArgs` are deliberately
not forwarded to analysis. Existing structural LSP diagnostics remain separate.

Within a `nil.project.json` project, live checks inherit project import roots,
Within a `nil.project.nildata` project, live checks inherit project import roots,
SDK location and rule policy. Project build/run tasks use `nilbuild` and saved
files. The current-file command keeps the active file as its primary source;
other open source buffers supply dependencies. Live analysis requests the
Expand Down
4 changes: 4 additions & 0 deletions docs/design/NIL-0008-project-tooling.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
- **Scope:** Compiler, project driver, SDK and editor integration
- **Date:** 2026-10-18

The initial behavior below is extended by
[NIL-0022](NIL-0022-incremental-project-builds.md), which adds project references,
incremental and parallel builds, and NILData as the preferred manifest format.

## Context

Compiler analysis already exists, but single-file editor tasks and framework
Expand Down
97 changes: 97 additions & 0 deletions docs/design/NIL-0022-incremental-project-builds.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# NIL-0022: Incremental project builds and NILData configuration

**Status:** Implemented initial local project graph and target scheduling profile.

## Context

The initial project driver checks and recompiles one executable on every build.
It has no dependency ordering between projects and no safe parallel scheduling
boundary. Its project manifest also predates NILData, leaving the framework's
own configuration outside the use cases that exercise the new notation.

The compiler combines an entrypoint and all imported units into one checked
program. It does not expose a separate-object contract for compiling individual
source modules independently. Parallelism must respect that compilation model.

## Decision

Retain one entrypoint per `nil.project.v1` manifest and add optional
`projectReferences`, an array of local manifest paths. References establish
dependency ordering, with canonical project identities, cycle rejection and
output conflict checks. They do not change source import lookup or imply a
package/library linker. Existing `includePaths` remain authoritative for
shared source modules.

The driver checks and builds projects in dependency waves. Independent checks
and compiler processes can run concurrently within a wave, bounded by
`--jobs` (1–64, default up to four hardware threads). `build --target all` adds
independent VM and native output tasks. Each compiler still handles a complete
program; this is not concurrent parsing or code generation within one program.

Every build runs fresh compiler analysis. Its additive dependency report lists
canonical native file paths and SHA-256 digests of the exact bytes consumed:
primary and imported sources, arbitrary-extension textual includes and binary
library containers. `dependenciesComplete` certifies successful disk analysis
with a complete, consistent list. Editor overlays and failed/incomplete checks
cannot certify a disk build. The native runtime archive and configured linker
driver are identified separately for native toolchain fingerprints.

The driver uses those inputs, effective project settings, tool identity and
artifact content to decide whether compilation and linking can be skipped.
Timestamp equality alone never justifies reuse. Fresh analysis also discovers
changed import resolution, including newly created higher-priority candidates.
Missing, invalid or incompatible cache state causes a rebuild. Older compilers
without complete dependency reports remain usable without cache reuse.
`--rebuild` bypasses the optimization explicitly.

Artifacts remain staged and are published only after a successful compiler
process produces a regular artifact of the required kind. Output locks prevent
two drivers from building the same outputs simultaneously; contention fails
with a diagnostic rather than waiting. Process-group cancellation, bounded
child output and timeouts apply to all active jobs. The complete graph is not
one transaction: successful independent projects may publish before another
project fails. A failed build never launches a stale program.

## Configuration format

`nil.project.nildata` is the preferred manifest, and NILData is the default for
new manifests, `describe` output and incremental state under
`<outputDirectory>/.nilbuild/`. `init` creates only a manifest; it does not
generate application source. `convert` validates the schema, keeps the manifest
in the same project directory to preserve import lookup and working directory,
and never overwrites an existing file.

JSON and a bounded declarative YAML subset remain supported for loading and
explicit creation/conversion. Deprecation applies to their role as the default
`nilbuild` configuration format, not to the standard library. The YAML subset
supports block mappings/sequences and JSON-compatible scalar/flow values,
without tags, anchors, aliases, merge keys or block strings. The
[project guide](../tools/projects.md) defines the complete surface.

Discovery selects the nearest ancestor directory with a supported manifest,
preferring NILData before JSON, YAML and YML in that directory. Explicit paths
remain available. The compiler/editor JSON protocols, SDK/release artifacts
and VS Code's own host configuration keep their established formats. JSON
machine consumers can continue to request `--format json`.

## Limits and follow-up

Analysis is intentionally not cached, so warm builds still pay for parsing and
checking. Further acceleration needs a compiler-owned persistent checked graph
or separate compilation contract, rather than inferring module boundaries in
the build driver. Project references do not yet produce reusable library
targets or resolve packages. Scheduling uses dependency waves rather than
starting a dependent immediately when its last prerequisite finishes.

Build inputs and host tools must remain stable during a build. Content checks
detect observed changes, but the driver does not create an immutable filesystem
snapshot or certify every host system library used by the linker. Lock
contention does not queue or recover another driver's work. Comment-preserving
manifest editing and richer NILData schema completion remain future tooling.

Qualification covers first/warm/forced builds, changed source and tool inputs,
arbitrary include filenames, import shadowing, binary libraries, damaged state
and artifacts, graph cycles and output collisions, bounded process overlap,
failure/cancellation, and cross-format conversion/discovery. The earlier
[NIL-0008](NIL-0008-project-tooling.md) remains the record of the initial
single-project behavior; this decision extends it.
1 change: 1 addition & 0 deletions docs/design/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ not assigned architecture identifiers.
| NIL-0019 | [Explicit binding-reference parameters](NIL-0019-binding-reference-parameters.md) | Accepted; initial scalar/text implementation |
| NIL-0020 | [Declaration annotations as shared metadata](NIL-0020-declaration-annotations.md) | Accepted; initial scalar metadata profile |
| NIL-0021 | [Switch statements and expressions with automatic fallthrough](NIL-0021-fallthrough-switch.md) | Accepted; initial literal and payload-free enum selector profile |
| NIL-0022 | [Incremental project builds and NILData configuration](NIL-0022-incremental-project-builds.md) | Implemented initial local project graph and target scheduling profile |
| NIL-0093 | [Executable context is artifact self-description](NIL-0093-executable-context.md) | Implemented within the documented inspection profile |

## Retained Ny feature references
Expand Down
Loading
Loading