compiler: Make package artifacts portable

4130eff6afff05af3efd7bcf207271a31902933edbb2149f401a974540960e8b
Record radiance-1.0.0 as the artifact producer and expose it with
-version. Keep producer provenance separate from numeric format and ABI
compatibility.

Use logical module paths and relative file origins in cache keys and
stored artifacts. Retain only embedded files referenced by generated RID
declarations so private embed edits preserve client metadata pins. Add
clean [-cache <directory>] to remove completed and temporary cache
entries.

Keep private embed and relocation programs in source fixture files.

Assisted-by: Codex:gpt-6
Change-Id: Ibf3a7b5c3f97cb44c5a1f00f091c4e5b44eb9014
Alexis Sellier committed ago 1 parent 2ccf2a07
compiler/radiance.rad +22 -1
75 75
/// it.
76 76
constant FORMAT_WIDTH: u32 = 100;
77 77
/// Spaces per indentation level of formatted source.
78 78
constant FORMAT_INDENT: u32 = 4;
79 79
80 +
/// Human-readable compiler provenance written into package artifacts.
81 +
export constant IDENTITY: *[u8] = "radiance-1.0.0";
82 +
80 83
/// Maximum rodata size (4MB).
81 84
constant MAX_RO_DATA_SIZE: u32 = 4194304;
82 85
/// Maximum rwdata size (4MB).
83 86
constant MAX_RW_DATA_SIZE: u32 = 4194304;
84 87
/// Maximum path length.
92 95
/// Default directory for package cache entries.
93 96
constant DEFAULT_CACHE_DIR: *[u8] = "cache";
94 97
95 98
/// Usage string.
96 99
constant USAGE: *[u8] =
97 -
    "usage: radiance -pkg <name> [-start <input.ras>] -mod <input>.. [-pkg <name> -mod <input>..] -entry <pkg> [-o <output> | -ril <directory>]\n-artifact <directory> selects a compiled package tree.\n-manifest <path> selects dependency names; the default is <package>.manifest beside the root module.\n-cache <directory> selects cache storage; -no-cache bypasses cache reads and writes.\n-mod accepts a source path or logical/path=source.rad relative to the package root.\n-embed <path> declares a file under the package root for @embed.\n       radiance -fmt [-fmt.wrap <columns>] <input.rad>\n       radiance -fmt.check [-fmt.wrap <columns>] <input.rad>\n-fmt prints the source of a module in the canonical style, wrapped at 100 columns unless -fmt.wrap sets another width.\n-fmt.check prints nothing and fails if the source of a module is not in the canonical style.\n";
100 +
    "usage: radiance -pkg <name> [-start <input.ras>] -mod <input>.. [-pkg <name> -mod <input>..] -entry <pkg> [-o <output> | -ril <directory>]\n-artifact <directory> selects a compiled package tree.\n-manifest <path> selects dependency names; the default is <package>.manifest beside the root module.\n-cache <directory> selects cache storage; -no-cache bypasses cache reads and writes.\n-mod accepts a source path or logical/path=source.rad relative to the package root.\n-embed <path> declares a file under the package root for @embed.\n       radiance -fmt [-fmt.wrap <columns>] <input.rad>\n       radiance -fmt.check [-fmt.wrap <columns>] <input.rad>\n-fmt prints the source of a module in the canonical style, wrapped at 100 columns unless -fmt.wrap sets another width.\n-fmt.check prints nothing and fails if the source of a module is not in the canonical style.\n       radiance clean [-cache <directory>]\nclean removes completed and temporary package cache entries.\n       radiance -version\n-version prints the compiler identity.\n";
98 101
99 102
/// Compiler error.
100 103
export union Error: Copy {
101 104
    Other,
102 105
}
1346 1349
/// `storage` is the memory of the process. `run` splits it by field, and each
1347 1350
/// buffer has one owner: the arenas and tables move to the parser, the module
1348 1351
/// graph, the resolver, and the lowering and code generation passes, and the
1349 1352
/// output buffers are lent to the one pass that runs.
1350 1353
fn run(env: *sys::Env, storage: *mut Storage) -> i32 {
1354 +
    if env.args.len > 0 and mem::eq(env.args[0], "clean") {
1355 +
        let mut root: *[u8] = DEFAULT_CACHE_DIR;
1356 +
        if env.args.len == 3 and mem::eq(env.args[1], "-cache") {
1357 +
            set root = env.args[2];
1358 +
        } else if env.args.len <> 1 {
1359 +
            io::printError(USAGE);
1360 +
            return 1;
1361 +
        }
1362 +
        try cache::clean(root, heap::over(&mut storage.frontend[..])) catch {
1363 +
            error(&["cannot clean package cache", root]);
1364 +
            return 1;
1365 +
        };
1366 +
        return 0;
1367 +
    }
1368 +
    if env.args.len == 1 and mem::eq(env.args[0], "-version") {
1369 +
        io::printLn(IDENTITY);
1370 +
        return 0;
1371 +
    }
1351 1372
    if env.args.len > 0 and (mem::eq(env.args[0], "-fmt") or mem::eq(env.args[0], "-fmt.check")) {
1352 1373
        let mut width = FORMAT_WIDTH;
1353 1374
        let mut mode = FormatMode::Print;
1354 1375
        if mem::eq(env.args[0], "-fmt.check") {
1355 1376
            set mode = FormatMode::Check;
compiler/radiance/artifact.rad +16 -5
74 74
75 75
/// An immutable non-Radiance artifact input.
76 76
export record File: Copy {
77 77
    /// Relative storage path inside the interface tree.
78 78
    path: *[u8],
79 -
    /// Original diagnostic source path.
79 +
    /// Relative diagnostic source path in a stored artifact.
80 80
    origin: *[u8],
81 81
    /// Complete input contents.
82 82
    bytes: *[u8],
83 83
    /// Use of these bytes during native linking.
84 84
    role: FileRole,
98 98
99 99
/// Package artifact retained in immutable compiler storage.
100 100
export record Package: Copy {
101 101
    /// Package name and root module identity.
102 102
    name: *[u8],
103 +
    /// Identity of the compiler that produced this artifact.
104 +
    compiler: *[u8],
103 105
    /// Text modules in parent-before-child order.
104 106
    modules: *[Module],
105 -
    /// Assembly and source locations used by native output.
107 +
    /// Assembly, interface embeds, and source locations retained with the package.
106 108
    files: *[File],
107 109
    /// Dependency names and required compiler metadata.
108 110
    dependencies: *[Dependency],
109 111
    /// Canonical hash of exported runtime contracts.
110 112
    exports: binary::Id,
133 135
    mem::store32Le(&mut size[..], 0, bytes.len);
134 136
    blake3::update(state, &size[..]);
135 137
    blake3::update(state, bytes);
136 138
}
137 139
138 -
/// Hash exported contracts, RID declarations, embedded files, and dependency metadata.
139 -
/// RID declarations can read embedded files. Build provenance, source locations, and
140 -
/// native-link files are separate inputs.
140 +
/// Hash exported contracts, RID declarations, interface embeds, and dependency
141 +
/// metadata. RID declarations can read embedded files. Build provenance, source
142 +
/// locations, and native-link files are separate inputs.
141 143
export fn declarations(package: &Package) -> binary::Id {
142 144
    let mut state = blake3::new();
143 145
    field(&mut state, package.name);
144 146
    field(&mut state, &package.exports.bytes[..]);
145 147
    let mut embeds: u32 = 0;
317 319
        &mut nodes,
318 320
        "Package",
319 321
        &[
320 322
            ast::field("version", try ast::numberValue(VERSION as u64, arena)),
321 323
            ast::field("name", ast::stringValue(package.name)),
324 +
            ast::field("compiler", ast::stringValue(package.compiler)),
322 325
            ast::field("exports",      try ast::hexStringValue(&package.exports.bytes[..], arena)),
323 326
            ast::field("inputs",       try ast::hexStringValue(&inputs.bytes[..], arena)),
324 327
            ast::field("image",        try ast::hexStringValue(&identity.bytes[..], arena)),
325 328
            ast::field("modules",      moduleValues),
326 329
            ast::field("files",        fileValues),
387 390
        or not mem::eq(try data.bytes(rootValue, "name"), name)
388 391
    {
389 392
        throw binary::Error::Invalid;
390 393
    }
391 394
    let exports = try readId(&data, rootValue, "exports");
395 +
    let compiler = try join(arena, &[try data.bytes(rootValue, "compiler")]);
396 +
    if compiler.len == 0 {
397 +
        throw binary::Error::Invalid;
398 +
    }
392 399
    let inputs = try readId(&data, rootValue, "inputs");
393 400
    let identity = try readId(&data, rootValue, "image");
394 401
    let image = try readFile(try join(arena, &[root, "/", name, ".ril.bin"]), arena);
395 402
    if image.len < 12
396 403
        or mem::load32Le(image, 0) <> binary::MAGIC
431 438
    let mut files: *mut [File] = &mut [];
432 439
    for i in 0 .. fileValues.len {
433 440
        let descriptor = try data.at(&fileValues, i);
434 441
        let path = try join(arena, &[try data.bytes(descriptor, "path")]);
435 442
        let origin = try join(arena, &[try data.bytes(descriptor, "origin")]);
443 +
        if origin.len > 0 and origin[0] == '/' {
444 +
            throw binary::Error::Invalid;
445 +
        }
436 446
        let kind = try data.variant(descriptor, "role");
437 447
        if not validPath(path) or kind >= ROLES.len {
438 448
            throw binary::Error::Invalid;
439 449
        }
440 450
        let hash = try readId(&data, descriptor, "hash");
469 479
            throw binary::Error::Storage;
470 480
        };
471 481
    }
472 482
    let mut package = Package {
473 483
        name,
484 +
        compiler,
474 485
        modules: &modules[..],
475 486
        files: &files[..],
476 487
        dependencies: &dependencies[..],
477 488
        exports,
478 489
        declarations: binary::Id { bytes: [0; 32] },
compiler/radiance/artifact/schema.rad +3 -1
25 25
26 26
/// One file stored with an artifact.
27 27
export record File: Copy {
28 28
    /// Relative storage path.
29 29
    path: *[u8],
30 -
    /// Diagnostic source path.
30 +
    /// Relative diagnostic source path.
31 31
    origin: *[u8],
32 32
    /// Use of the contents.
33 33
    role: FileRole,
34 34
    /// Content identity in hexadecimal.
35 35
    hash: *[u8],
47 47
export record Package: Copy {
48 48
    /// Descriptor format version.
49 49
    version: u32,
50 50
    /// Root package name.
51 51
    name: *[u8],
52 +
    /// Identity of the compiler that produced the artifact.
53 +
    compiler: *[u8],
52 54
    /// Exported contract identity in hexadecimal.
53 55
    exports: *[u8],
54 56
    /// Compilation request identity in hexadecimal.
55 57
    inputs: *[u8],
56 58
    /// Executable image identity in hexadecimal.
compiler/radiance/build.rad +144 -31
67 67
68 68
/// Immutable module input before parsing or fingerprinting.
69 69
record Snapshot: Copy {
70 70
    /// Source path, with an optional logical module mapping.
71 71
    path: *[u8],
72 +
    /// Package-qualified logical module path.
73 +
    logical: *[u8],
72 74
    /// Complete file contents read for this invocation.
73 75
    text: *[u8],
74 76
}
75 77
76 78
/// Immutable implementation inputs captured before parsing.
77 79
record Source: Copy {
78 80
    /// Source paths and immutable text, before implementation parsing.
79 81
    modules: *[Snapshot],
80 -
    /// Immutable assembly inputs, with their native-link role.
82 +
    /// Immutable non-Radiance inputs, with their artifact role.
81 83
    files: *[artifact::File],
82 84
    /// Exact source manifest bytes.
83 85
    manifest: *[u8],
84 86
}
85 87
107 109
    declared: *[u16],
108 110
    /// Completed provider contracts used to select imports.
109 111
    providers: &'providers [link::Provider],
110 112
    /// Content key when cache or artifact publication is required.
111 113
    key: ?binary::Id,
112 -
    /// Captured assembly inputs.
114 +
    /// Captured assembly and embedded file inputs.
113 115
    files: *[artifact::File],
114 116
}
115 117
116 118
/// Artifacts of one source compilation.
117 119
record Compilation {
235 237
        let path = options.radPaths[i];
236 238
        if not mem::endsWith(sourcePath(path), ".rad") {
237 239
            throw sys::Error::Invalid;
238 240
        }
239 241
        let text = try artifact::readFile(sourcePath(path), arena);
242 +
        let mut logical = options.name;
243 +
        if i == 0 {
244 +
            if mem::find(path, "=") <> nil {
245 +
                throw sys::Error::Invalid;
246 +
            }
247 +
        } else {
248 +
            let mut relative: *[u8] = "";
249 +
            if let at = mem::find(path, "=") {
250 +
                set relative = &path[.. at];
251 +
            } else {
252 +
                let root = module::trimExtension(sourcePath(options.radPaths[0]))
253 +
                    else throw sys::Error::Invalid;
254 +
                let stem = module::trimExtension(path) else throw sys::Error::Invalid;
255 +
                if root.len <= rootDirectory(options).len
256 +
                    or stem.len <= root.len + 1
257 +
                    or not mem::startsWith(stem, root)
258 +
                    or stem[root.len] <> '/'
259 +
                {
260 +
                    throw sys::Error::Invalid;
261 +
                }
262 +
                set relative = &stem[root.len + 1 ..];
263 +
            }
264 +
            set logical = try artifact::join(arena, &[options.name, "/", relative]);
265 +
        }
240 266
        try modules.append(
241 -
            Snapshot { path, text }, arena
267 +
            Snapshot { path, logical, text }, arena
242 268
        ) catch {
243 269
            throw sys::Error::TooLarge;
244 270
        };
245 271
    }
246 272
    let path = options.manifestPath else try artifact::join(arena, &[
291 317
    let first = sourcePath(options.radPaths[0]);
292 318
    let slash = mem::rfind(first, "/") else return "";
293 319
    return &first[.. slash + 1];
294 320
}
295 321
322 +
/// Return a relative diagnostic origin for a captured artifact file.
323 +
fn fileOrigin(root: *[u8], file: &artifact::File) -> *[u8] {
324 +
    if file.origin.len == 0 {
325 +
        return "";
326 +
    }
327 +
    if mem::startsWith(file.origin, root) {
328 +
        let relative = &file.origin[root.len ..];
329 +
        if relative.len > 0 and relative[0] <> '/' {
330 +
            return relative;
331 +
        }
332 +
    }
333 +
    return file.path;
334 +
}
335 +
296 336
/// Read the files that a source package declares for `@embed`. Each one must lie
297 337
/// under the package root, and is stored at its path from the root in the interface
298 338
/// tree.
299 339
export fn embeds(options: super::PackageInput, arena: &heap::Heap) -> *[artifact::File]
300 340
    throws (sys::Error)
530 570
}
531 571
532 572
/// Hash source inputs, compiler options, and selected dependency metadata.
533 573
fn key(
534 574
    ctx: &super::CompileContext,
535 -
    name: *[u8],
575 +
    options: &super::PackageInput,
536 576
    input: &Source,
537 577
    providers: &[artifact::Package],
538 578
    declared: &[u16],
539 579
    testEntry: bool,
540 580
    compiler: binary::Id
541 581
) -> binary::Id {
542 582
    let mut state = blake3::new();
543 583
    artifact::field(&mut state, "Radiance package cache 1");
544 584
    artifact::field(&mut state, &compiler.bytes[..]);
545 -
    artifact::field(&mut state, name);
585 +
    artifact::field(&mut state, options.name);
546 586
    artifact::field(&mut state, input.manifest);
547 -
    let options: [u8; 4] = [
587 +
    let flags: [u8; 4] = [
548 588
        1 if ctx.debug else 0, 1 if ctx.config.buildTest else 0,
549 589
        1 if ctx.stackCheck else 0, 1 if testEntry else 0,
550 590
    ];
551 -
    artifact::field(&mut state, &options[..]);
591 +
    artifact::field(&mut state, &flags[..]);
552 592
    let mut counts: [u8; 12] = [0; 12];
553 593
    mem::store32Le(&mut counts[..], 0, input.modules.len);
554 594
    mem::store32Le(&mut counts[..], 4, input.files.len);
555 595
    mem::store32Le(&mut counts[..], 8, declared.len);
556 596
    artifact::field(&mut state, &counts[..]);
557 597
    for module in input.modules {
558 -
        artifact::field(&mut state, module.path);
598 +
        // Logical mappings select the module registration rules.
599 +
        let mapped: [u8; 1] = [1 if mem::find(module.path, "=") <> nil else 0];
600 +
        artifact::field(&mut state, &mapped[..]);
601 +
        artifact::field(&mut state, module.logical);
559 602
        let hash = artifact::digest(module.text);
560 603
        artifact::field(&mut state, &hash.bytes[..]);
561 604
    }
605 +
    let root = rootDirectory(*options);
562 606
    for file in input.files {
563 -
        artifact::field(&mut state, file.origin);
607 +
        artifact::field(&mut state, file.path);
608 +
        artifact::field(&mut state, fileOrigin(root, &file));
564 609
        let role: [u8; 1] = [file.role as u8];
565 610
        artifact::field(&mut state, &role[..]);
566 611
        artifact::field(&mut state, file.bytes);
567 612
    }
568 613
    for id in declared {
669 714
            case Contents::Compiled(package) => set built = package,
670 715
            case Contents::Source(source) => {
671 716
                resetFrontend(ctx, graph, work.main);
672 717
                let request: ?binary::Id = key(
673 718
                    ctx,
674 -
                    input.options.name,
719 +
                    &input.options,
675 720
                    &source,
676 721
                    &compiled[..],
677 722
                    providerIds,
678 723
                    ctx.config.buildTest and index == entry,
679 724
                    compiler
777 822
        try validate(&built, &compiled[..]);
778 823
        if not fresh {
779 824
            if let case locations::Native::Program(target) = &mut native {
780 825
                try decode(
781 826
                    ctx,
827 +
                    &input,
782 828
                    &built,
783 829
                    entryName,
784 830
                    &mut target.generator,
785 831
                    &mut allData,
786 832
                    &mut sourcePaths,
845 891
            throw super::Error::Other;
846 892
        };
847 893
        let mut modules: *mut [Snapshot] = &mut [];
848 894
        for item in compiled[i].modules {
849 895
            try modules.append(
850 -
                Snapshot { path: item.path, text: item.text }, driverArena
896 +
                Snapshot {
897 +
                    path: item.path, logical: item.path, text: item.text
898 +
                },
899 +
                driverArena
851 900
            ) catch {
852 901
                throw super::Error::Other;
853 902
            };
854 903
        }
855 904
        let embeds = try embedded(compiled[i].files, driverArena);
951 1000
        or ast::attributesContains(&list, ast::Attribute::Test)
952 1001
        or ast::attributesContains(&list, ast::Attribute::Intrinsic)
953 1002
        or ast::attributesContains(&list, ast::Attribute::Language);
954 1003
}
955 1004
956 -
/// Mark the symbols of the nodes that a statement layout presents. Every alternative
957 -
/// of a choice presents the same nodes, so only the first is visited.
958 -
fn markReferences(out: *layout::Layout, res: &resolver::Resolver, marks: &mut bitset::Bitset) {
1005 +
/// Mark symbols and embedded files that a statement layout presents. Every
1006 +
/// alternative of a choice presents the same nodes, so only the first is visited.
1007 +
fn markReferences(
1008 +
    out: *layout::Layout,
1009 +
    res: &resolver::Resolver,
1010 +
    marks: &mut bitset::Bitset,
1011 +
    files: &[artifact::File],
1012 +
    usedEmbeds: &mut bitset::Bitset
1013 +
) {
959 1014
    if let source = out.source {
960 1015
        if let symbol = resolver::symbolFor(res, source) {
961 1016
            bitset::put(marks, *symbol);
962 1017
        }
1018 +
        if let case ast::NodeValue::BuiltinCall { kind, args } =
1019 +
            source.value;
1020 +
            kind == ast::Builtin::Embed
1021 +
        {
1022 +
            let case ast::NodeValue::String(path) = args[0].value
1023 +
                else panic "markReferences: expected an embedded path";
1024 +
            for file, i in files {
1025 +
                if file.role == artifact::FileRole::Embed and mem::eq(file.path, path) {
1026 +
                    bitset::put(usedEmbeds, i);
1027 +
                    break;
1028 +
                }
1029 +
            }
1030 +
        }
963 1031
    }
964 1032
    match out.item {
965 1033
        case layout::Item::Box(children) => {
966 1034
            for child in children {
967 -
                markReferences(child.layout, res, marks);
1035 +
                markReferences(child.layout, res, marks, files, usedEmbeds);
968 1036
            }
969 1037
        }
970 -
        case layout::Item::Choice(alternatives) => markReferences(alternatives[0], res, marks),
971 -
        case layout::Item::Broken(inner)        => markReferences(inner, res, marks),
972 -
        else                                    => {}
1038 +
        case layout::Item::Choice(alternatives) =>
1039 +
            markReferences(alternatives[0], res, marks, files, usedEmbeds),
1040 +
        case layout::Item::Broken(inner) => markReferences(inner, res, marks, files, usedEmbeds),
1041 +
        else                             => {}
973 1042
    }
974 1043
}
975 1044
976 1045
/// Format the interface of a module: its roots and the private declarations that
977 1046
/// retained declarations refer to, transitively. Private symbols are visible only in
978 1047
/// their module, so the closure stays within it. A private declaration that nothing
979 1048
/// retained refers to is omitted, so editing it leaves the interface unchanged.
980 1049
fn interface(
981 -
    root: *ast::Node, res: &resolver::Resolver, arena: *heap::Heap
1050 +
    root: *ast::Node,
1051 +
    res: &resolver::Resolver,
1052 +
    files: &[artifact::File],
1053 +
    usedEmbeds: &mut bitset::Bitset,
1054 +
    arena: *heap::Heap
982 1055
) -> *layout::Layout throws (alloc::AllocError) {
983 1056
    let case ast::NodeValue::Module(block) = root.value
984 1057
        else panic "interface: expected module root";
985 1058
    let mut retained: *mut [bool] = &mut [];
986 1059
    let mut formatted: *mut [?*layout::Layout] = &mut [];
999 1072
    while added.len > 0 {
1000 1073
        for i in added {
1001 1074
            if let statement = try pretty::interfaceStatement(
1002 1075
                block.statements[i], arena
1003 1076
            ) {
1004 -
                markReferences(statement, res, &mut marks);
1077 +
                markReferences(statement, res, &mut marks, files, usedEmbeds);
1005 1078
                set formatted[i] = statement;
1006 1079
            }
1007 1080
        }
1008 1081
        set added = &mut [];
1009 1082
        for node, i in block.statements {
1038 1111
    selection: Request['providers],
1039 1112
    native: locations::Native
1040 1113
) -> Compilation throws (super::Error) {
1041 1114
    let pkg = ctx.packages[index];
1042 1115
    let mut declarations: *mut [artifact::Module] = &mut [];
1116 +
    let mut usedEmbeds = try bitset::allocate(work.main, selection.files.len) catch {
1117 +
        throw super::Error::Other;
1118 +
    };
1043 1119
    {
1044 1120
        let mut text = try buffer(work.functions, INTERFACE_BYTES);
1045 1121
        for entry in modules.entries {
1046 1122
            if entry.packageId as u32 <> index {
1047 1123
                continue;
1048 1124
            }
1049 1125
            let root = entry.ast else throw super::Error::Other;
1050 1126
            let layoutStart = heap::mark(work.functions);
1051 -
            let tree = try interface(root, &res, work.functions) catch {
1127 +
            let tree = try interface(
1128 +
                root, &res, selection.files, &mut usedEmbeds, work.functions
1129 +
            ) catch {
1052 1130
                throw super::error(&["cannot render interface", entry.filePath]);
1053 1131
            };
1054 1132
            let mut output = sexpr::Buffer { buf: text.take(), pos: 0 };
1055 1133
            layout::text::renderFixed(tree, 4, &mut output);
1056 1134
            if output.pos == output.buf.len {
1084 1162
        }
1085 1163
    }
1086 1164
    unsafe {
1087 1165
        heap::reset(work.functions);
1088 1166
    }
1167 +
    let mut files: *mut [artifact::File] = &mut [];
1168 +
    for file, i in selection.files {
1169 +
        if file.role <> artifact::FileRole::Embed or bitset::contains(&usedEmbeds, i) {
1170 +
            try files.append(file, work.artifacts) catch {
1171 +
                throw super::Error::Other;
1172 +
            };
1173 +
        }
1174 +
    }
1175 +
    let request = Request {
1176 +
        declared: selection.declared,
1177 +
        providers: selection.providers,
1178 +
        key: selection.key,
1179 +
        files: &files[..],
1180 +
    };
1089 1181
    let bytes: ?*mut [u8] = work.spool.take() if selection.key <> nil else nil;
1090 1182
    let initial = try codegen::ril(work.emitter, bytes, pkg.name) catch {
1091 1183
        throw super::error(&["package encoding storage exhausted", pkg.name]);
1092 1184
    };
1093 1185
    let mut low = lower::lowerer(res, modules, pkg.name, work.functions, lower::LowerOptions {
1098 1190
        set debug = try buffer(work.main, DEBUG_BYTES);
1099 1191
    }
1100 1192
    let mut locationWriter = writer::new(debug);
1101 1193
    let root = pkg.rootModuleId else panic "compile: missing root";
1102 1194
    if locationWriter.bytes.len > 0 {
1103 -
        let count = modules.entries.len - root as u32;
1104 -
        try writer::word(&mut locationWriter, count as u64) catch {
1195 +
        let listed: &[artifact::Module] = &declarations[..];
1196 +
        try writer::word(&mut locationWriter, listed.len as u64) catch {
1105 1197
            throw super::Error::Other;
1106 1198
        };
1107 -
        for i in root as u32 .. modules.entries.len {
1108 -
            try writer::bytes(&mut locationWriter, modules.entries[i].filePath) catch {
1199 +
        for entry in listed {
1200 +
            let path = try artifact::join(work.artifacts, &[
1201 +
                &entry.path[.. entry.path.len - 4], ".rad"
1202 +
            ]) catch {
1203 +
                throw super::Error::Other;
1204 +
            };
1205 +
            try writer::bytes(&mut locationWriter, path) catch {
1109 1206
                throw super::Error::Other;
1110 1207
            };
1111 1208
        }
1112 1209
    }
1113 1210
    let entryIndex = ctx.entryPkgIdx else panic "compile: missing entry package";
1118 1215
        native,
1119 1216
        entry: index == entryIndex,
1120 1217
    };
1121 1218
    try super::lowerPackage(ctx, modules, &mut low, &pkg, true, &mut sink);
1122 1219
    return try finishPackage(
1123 -
        ctx, index, low, sink, &declarations[..], providers, work, selection
1220 +
        ctx, index, low, sink, &declarations[..], providers, work, request
1124 1221
    );
1125 1222
}
1126 1223
1127 1224
/// Encode the tables, metadata pins, and artifact descriptors after the
1128 1225
/// functions of package slot `index` are consumed.
1139 1236
    let mut lowering = low;
1140 1237
    let pkg = ctx.packages[index];
1141 1238
    let case Request { declared, key: request, files, .. } = selection
1142 1239
        else panic "finishPackage: expected source request";
1143 1240
    let mut packageFiles: *mut [artifact::File] = &mut [];
1144 -
    try packageFiles.extend(files, work.artifacts) catch {
1145 -
        throw super::Error::Other;
1146 -
    };
1241 +
    let root = rootDirectory(ctx.inputs[index]);
1242 +
    for file in files {
1243 +
        let mut stored = file;
1244 +
        set stored.origin = fileOrigin(root, &file);
1245 +
        try packageFiles.append(stored, work.artifacts) catch {
1246 +
            throw super::Error::Other;
1247 +
        };
1248 +
    }
1147 1249
    if sink.locations.bytes.len > 0 {
1148 1250
        let bytes = try keep(
1149 1251
            work.artifacts, &sink.locations.bytes[.. sink.locations.offset]
1150 1252
        ) catch {
1151 1253
            throw super::Error::Other;
1248 1350
        }
1249 1351
    }
1250 1352
    super::pkgLog(&pkg, &["encoding interface metadata"]);
1251 1353
    let mut result = artifact::Package {
1252 1354
        name: pkg.name,
1355 +
        compiler: super::IDENTITY,
1253 1356
        modules: declarations,
1254 1357
        files: &packageFiles[..],
1255 1358
        dependencies: &dependencies[..],
1256 1359
        exports,
1257 1360
        declarations: binary::Id { bytes: [0; linkage::ID_BYTES] },
1378 1481
}
1379 1482
1380 1483
/// Decode one cached or explicit package for native generation.
1381 1484
fn decode(
1382 1485
    ctx: &super::CompileContext,
1486 +
    selected: &Input,
1383 1487
    package: &artifact::Package,
1384 1488
    entry: *[u8],
1385 1489
    generator: &mut rv64::Generator,
1386 1490
    allData: &mut *mut [il::Data],
1387 1491
    sourcePaths: &mut *mut [*[u8]],
1403 1507
            throw super::Error::Other;
1404 1508
        };
1405 1509
        if sourceCount <> package.modules.len {
1406 1510
            throw super::error(&["package debug module count mismatch", package.name]);
1407 1511
        }
1408 -
        for _ in 0 .. sourceCount {
1409 -
            let path = try debug.bytes(work.emitter) catch {
1512 +
        for i in 0 .. sourceCount {
1513 +
            let mut path = try debug.bytes(work.emitter) catch {
1410 1514
                throw super::Error::Other;
1411 1515
            };
1516 +
            if let case Contents::Source(source) = selected.contents {
1517 +
                if i >= source.modules.len
1518 +
                    or not mem::endsWith(path, ".rad")
1519 +
                    or not mem::eq(&path[.. path.len - 4], source.modules[i].logical)
1520 +
                {
1521 +
                    throw super::error(&["invalid cached debug module", path]);
1522 +
                }
1523 +
                set path = sourcePath(source.modules[i].path);
1524 +
            }
1412 1525
            try sourcePaths.append(path, work.emitter) catch {
1413 1526
                throw super::Error::Other;
1414 1527
            };
1415 1528
        }
1416 1529
    }
compiler/radiance/cache.rad +69 -2
1 1
//! Content-keyed package reuse and atomic artifact publication.
2 2
3 +
use std::char;
3 4
use std::fmt;
4 5
use std::fs;
5 6
use std::lang::alloc;
6 7
use std::lang::il::binary;
7 8
use std::mem;
9 10
use std::sys;
10 11
use super::artifact;
11 12
12 13
/// Bound on temporary directory reservations left by concurrent or interrupted writers.
13 14
constant TEMPORARY_SLOTS: u32 = 4096;
15 +
/// Initial storage for the cache root directory listing.
16 +
constant DIRECTORY_BYTES: u32 = 65536;
17 +
/// Hexadecimal characters in a 256-bit cache key.
18 +
constant KEY_LENGTH: u32 = 64;
19 +
/// Suffix that identifies a private cache publication directory.
20 +
constant TEMPORARY_MARKER: *[u8] = ".tmp.";
14 21
15 22
/// Build the lowercase hexadecimal directory name of a request key.
16 23
fn entry(root: *[u8], key: binary::Id, arena: &heap::Heap) -> *[u8] throws (sys::Error) {
17 -
    let mut text: [u8; 64] = [0; 64];
24 +
    let mut text: [u8; KEY_LENGTH] = [0; KEY_LENGTH];
18 25
    fmt::formatHex(&key.bytes[..], &mut text[..]);
19 26
    let name = try alloc::copyBytes(arena, &text[..]) catch {
20 27
        throw sys::Error::TooLarge;
21 28
    };
22 29
    return try artifact::join(arena, &[root, "/", &name[..]]);
63 70
            let mut digits: [u8; fmt::U32_STR_LEN] = undefined;
64 71
            let text = fmt::formatU32(i, &mut digits[..]);
65 72
            let owned = try alloc::copyBytes(arena, text) catch {
66 73
                throw sys::Error::TooLarge;
67 74
            };
68 -
            set path = try artifact::join(arena, &[base, ".tmp.", &owned[..]]);
75 +
            set path = try artifact::join(arena, &[base, TEMPORARY_MARKER, &owned[..]]);
69 76
        }
70 77
        try fs::makeDir(path) catch error {
71 78
            if error == sys::Error::Exists {
72 79
                continue;
73 80
            }
76 83
        return path;
77 84
    }
78 85
    throw sys::Error::Limit;
79 86
}
80 87
88 +
/// Remove completed and temporary cache entries below `root`.
89 +
/// Missing caches are empty. Other files in the cache root are retained.
90 +
export fn clean(root: *[u8], arena: &heap::Heap) throws (sys::Error) {
91 +
    let info = try fs::stat(root) else return;
92 +
    if info.kind <> fs::Kind::Directory {
93 +
        throw sys::Error::Invalid;
94 +
    }
95 +
    let mut capacity = DIRECTORY_BYTES;
96 +
    let mut listing: *[u8] = "";
97 +
    loop {
98 +
        let saved = heap::mark(arena);
99 +
        let mut bytes: *mut [u8] = &mut [];
100 +
        try bytes.resize(capacity, 0, arena) catch {
101 +
            throw sys::Error::TooLarge;
102 +
        };
103 +
        set listing = try fs::listDir(root, bytes.take()) catch error {
104 +
            if error <> sys::Error::TooLarge or capacity > mem::U32_MAX / 2 {
105 +
                throw error;
106 +
            }
107 +
            // A failed listing retains no pointers into the temporary buffer.
108 +
            unsafe {
109 +
                heap::rewind(arena, saved);
110 +
            }
111 +
            set capacity *= 2;
112 +
            continue;
113 +
        };
114 +
        break;
115 +
    }
116 +
    let mut names = mem::split(listing, 0);
117 +
    while let name = names.next() {
118 +
        if name.len < KEY_LENGTH {
119 +
            continue;
120 +
        }
121 +
        let mut owned = true;
122 +
        for byte in &name[.. KEY_LENGTH] {
123 +
            set owned = owned and char::isHexDigit(byte);
124 +
        }
125 +
        if name.len > KEY_LENGTH {
126 +
            let suffix = &name[KEY_LENGTH ..];
127 +
            if not mem::startsWith(suffix, TEMPORARY_MARKER)
128 +
                or suffix.len == TEMPORARY_MARKER.len
129 +
            {
130 +
                continue;
131 +
            }
132 +
            for byte in &suffix[TEMPORARY_MARKER.len ..] {
133 +
                set owned = owned and char::isDigit(byte);
134 +
            }
135 +
        }
136 +
        if not owned {
137 +
            continue;
138 +
        }
139 +
        let saved = heap::mark(arena);
140 +
        try fs::removeTree(try artifact::join(arena, &[root, "/", name]), arena);
141 +
        // Entry paths and recursive listings have no users after removal.
142 +
        unsafe {
143 +
            heap::rewind(arena, saved);
144 +
        }
145 +
    }
146 +
}
147 +
81 148
/// Write a private complete set, then make it visible with one directory rename.
82 149
export fn publish(
83 150
    root: *[u8], package: &artifact::Package, scratch: *heap::Heap, arena: &heap::Heap
84 151
)
85 152
    throws (sys::Error)
test/integration/packages.rad +276 -0
1 1
//! Source-free package inputs and content-keyed compilation cache checks.
2 2
3 3
use std::fs;
4 4
use std::lang::il::binary;
5 5
use std::lang::il::binary::program;
6 +
use std::lang::il::binary::reader;
6 7
use std::lang::il::verify;
7 8
use std::lang::il::verify::linking;
8 9
use std::mem;
9 10
use std::mem::heap;
10 11
use std::process;
184 185
        harness::Expect::Contains("embedded file is declared twice")
185 186
    ]);
186 187
    // An embedded file shares the interface tree with the generated module interfaces.
187 188
    let interface = h.join(&[root, "/text.rid"]);
188 189
    try fs::writeFile(interface, "");
190 +
    try fs::writeFileParts(provider, &[
191 +
        code, "export constant INTERFACE: *[u8] = @embed(\"text.rid\");\n"
192 +
    ]);
189 193
    h.add(
190 194
        &[
191 195
            "-no-cache", "-pkg", "text", "-mod", provider, "-embed", data, "-embed", interface,
192 196
            "-ril", rejected,
193 197
        ]
194 198
    );
195 199
    let clash = h.cc(h.take(), h.out(&["embed-clash.log"]));
196 200
    h.run("reject embedded files at interface paths", clash, process::Outcome::Exited(1), &[
197 201
        harness::Expect::Contains("artifact path conflicts with another stored path: text.rid")
198 202
    ]);
203 +
    try fs::writeFile(provider, code);
199 204
    let outside = h.path(&["outside.txt"]);
200 205
    try fs::writeFile(outside, "");
201 206
    h.add(&["-no-cache", "-pkg", "text", "-mod", provider, "-embed", outside, "-ril", rejected]);
202 207
    let escaped = h.cc(h.take(), h.out(&["embed-outside.log"]));
203 208
    h.run("reject embedded files outside the package", escaped, process::Outcome::Exited(1), &[
204 209
        harness::Expect::Contains("not a clean path under the package root")
205 210
    ]);
206 211
}
207 212
213 +
/// Private embedded inputs rebuild their provider and preserve its client contract.
214 +
fn privateEmbeds(h: &mut Harness) throws (sys::Error) {
215 +
    let root = h.path(&["private-embeds"]);
216 +
    try harness::makeDirs(h.join(&[root, "/data"]));
217 +
    try harness::makeDirs(h.join(&[root, "/text"]));
218 +
    let provider = h.join(&[root, "/text.rad"]);
219 +
    let child = h.join(&[root, "/text/labels.rad"]);
220 +
    let private = h.join(&[root, "/data/private.txt"]);
221 +
    let unused = h.join(&[root, "/data/unused.txt"]);
222 +
    let public = h.join(&[root, "/data/public.txt"]);
223 +
    let client = h.join(&[root, "/app.rad"]);
224 +
    let first = h.join(&[root, "/0"]);
225 +
    let mut original: ?binary::Id = nil;
226 +
    try fs::writeFile(provider, try h.load("test/packages/private-embeds/text.rad"));
227 +
    try fs::writeFile(
228 +
        child,
229 +
        "constant TEXT: *[u8] = @embed(\"data/public.txt\");\nexport constant VALUE: *[u8] = TEXT;\n"
230 +
    );
231 +
    try fs::writeFile(private, "p");
232 +
    try fs::writeFile(unused, "u");
233 +
    try fs::writeFile(public, "hello\n");
234 +
    try fs::writeFile(client, try h.load("test/packages/private-embeds/app.rad"));
235 +
    try fs::writeFile(h.join(&[root, "/app.manifest"]), "text\n");
236 +
    for version in 0 .. 3 {
237 +
        if version == 1 {
238 +
            try fs::writeFile(private, "q");
239 +
        } else if version == 2 {
240 +
            try fs::writeFile(unused, "v");
241 +
        }
242 +
        let output = h.join(&[root, "/", h.num(version)]);
243 +
        let report = h.join(&[root, "/", h.num(version), ".log"]);
244 +
        h.add(
245 +
            &[
246 +
                "-cache", h.join(&[root, "/cache"]),
247 +
                "-pkg", "text", "-mod", provider, "-mod", child,
248 +
                "-embed", private, "-embed", unused, "-embed", public,
249 +
                "-pkg", "app", "-mod", client, "-entry", "app", "-ril", output,
250 +
            ]
251 +
        );
252 +
        if not h.compile("compile private embedded inputs", process::Output::File(report)) {
253 +
            return;
254 +
        }
255 +
        let wire = try h.load(h.join(&[output, "/text.ril.bin"]));
256 +
        let mut input = reader::new(wire);
257 +
        let header = try! program::header(&mut input, h.arena);
258 +
        if let expected = original {
259 +
            assert header.links.metadata == expected;
260 +
            h.expect("private embed edits retain clients", report, harness::Expect::Contains(
261 +
                "cache hit app"
262 +
            ));
263 +
            h.expect("private embeds remain build inputs", report, harness::Expect::Matching {
264 +
                text: "cache hit text", count: 0
265 +
            });
266 +
            h.expect(
267 +
                "private embed edits preserve client images",
268 +
                h.join(&[output, "/app.ril.bin"]),
269 +
                harness::Expect::Same(h.join(&[first, "/app.ril.bin"]))
270 +
            );
271 +
        } else {
272 +
            set original = header.links.metadata;
273 +
        }
274 +
        if version == 1 {
275 +
            assert not mem::eq(wire, try h.load(h.join(&[first, "/text.ril.bin"])));
276 +
        }
277 +
        h.expect(
278 +
            "retain embeds reached through private interface declarations",
279 +
            h.join(&[output, "/text.interface/data/public.txt"]),
280 +
            harness::Expect::Present
281 +
        );
282 +
        for name in &["private.txt", "unused.txt"] {
283 +
            h.expect(
284 +
                "store only interface embeds",
285 +
                h.join(&[output, "/text.interface/data/", name]),
286 +
                harness::Expect::Absent
287 +
            );
288 +
        }
289 +
    }
290 +
    for path in &[provider, child, private, unused, public] {
291 +
        try fs::remove(path);
292 +
    }
293 +
    let image = h.join(&[root, "/app.rv64"]);
294 +
    h.add(
295 +
        &[
296 +
            "-no-cache", "-pkg", "app", "-artifact", h.join(&[root, "/2"]),
297 +
            "-entry", "app", "-o", image,
298 +
        ]
299 +
    );
300 +
    if h.compile("link private embeds without source files", h.out(&["private-embed-link.log"])) {
301 +
        execute(h, image);
302 +
    }
303 +
}
304 +
305 +
/// Clean package cache entries and retain files outside the cache.
306 +
fn cleanCache(h: &mut Harness) throws (sys::Error) {
307 +
    let root = h.path(&["clean-cache"]);
308 +
    let cache = h.join(&[root, "/cache"]);
309 +
    let exported = h.join(&[root, "/exported"]);
310 +
    let code = h.join(&[root, "/cleancheck.rad"]);
311 +
    try harness::makeDirs(root);
312 +
    try fs::writeFile(code, "@default fn main() -> u32 { return 0; }\n");
313 +
    h.add(
314 +
        &[
315 +
            "-cache", cache, "-pkg", "cleancheck", "-mod", code,
316 +
            "-entry", "cleancheck", "-ril", exported,
317 +
        ]
318 +
    );
319 +
    if not h.compile("populate the cache to clean", h.out(&["clean-populate.log"])) {
320 +
        return;
321 +
    }
322 +
    let entries = try h.children(cache);
323 +
    assert entries.len == 1;
324 +
    let partial = h.join(&[
325 +
        cache, "/0000000000000000000000000000000000000000000000000000000000000000.tmp.0"
326 +
    ]);
327 +
    try harness::makeDirs(partial);
328 +
    try fs::writeFile(h.join(&[partial, "/partial"]), "partial cache entry");
329 +
    let unrelated = h.join(&[cache, "/keep.txt"]);
330 +
    try fs::writeFile(unrelated, "unrelated file");
331 +
    h.add(&["clean", "-cache", cache]);
332 +
    if not h.compile("clean the selected package cache", h.out(&["clean.log"])) {
333 +
        return;
334 +
    }
335 +
    h.expect("remove completed cache entries", entries[0], harness::Expect::Absent);
336 +
    h.expect("remove interrupted cache entries", partial, harness::Expect::Absent);
337 +
    h.expect("retain unrelated cache-root files", unrelated, harness::Expect::Present);
338 +
    h.expect(
339 +
        "retain exported packages",
340 +
        h.join(&[exported, "/cleancheck.ril.bin"]),
341 +
        harness::Expect::Present
342 +
    );
343 +
    h.add(&["clean", "-cache", cache]);
344 +
    h.compile("clean an empty cache", h.out(&["clean-empty.log"]));
345 +
    h.add(&["clean", "-cache", h.join(&[root, "/missing"])]);
346 +
    h.compile("clean a missing cache", h.out(&["clean-missing.log"]));
347 +
    h.add(&["clean", "-cache"]);
348 +
    let invalid = h.cc(h.take(), h.out(&["clean-invalid.log"]));
349 +
    h.run("reject a missing cache path", invalid, process::Outcome::Exited(1), &[]);
350 +
}
351 +
352 +
/// Relocated source packages share cache entries and portable debug information.
353 +
fn relocation(h: &mut Harness) throws (sys::Error) {
354 +
    let root = h.path(&["relocation"]);
355 +
    let first = h.join(&[root, "/0/exported"]);
356 +
    let second = h.join(&[root, "/1/exported"]);
357 +
358 +
    let entry = try h.load("test/packages/relocation/entry.rad");
359 +
360 +
    for i in 0 .. 3 {
361 +
        let checkout = h.join(&[root, "/", h.num(i)]);
362 +
        try harness::makeDirs(checkout);
363 +
        let source = h.join(&[checkout, "/entry.rad" if i == 0 else "/renamed.rad"]);
364 +
        let child = h.join(&[checkout, "/implementation.rad"]);
365 +
        let assembly = h.join(&[checkout, "/renamed.ras" if i == 2 else "/native.ras"]);
366 +
        let data = h.join(&[checkout, "/asset.bin"]);
367 +
        try fs::writeFile(source, entry);
368 +
        try fs::writeFile(child, "export fn answer() -> u32 { return 42; }\n");
369 +
        try fs::writeFile(assembly, ".text;\n");
370 +
        try fs::writeFile(data, "x");
371 +
        let output = h.join(&[checkout, "/exported"]);
372 +
        let report = h.join(&[checkout, "/build.log"]);
373 +
        h.add(
374 +
            &[
375 +
                "-cache", h.join(&[root, "/cache"]), "-debug",
376 +
                "-pkg", "portable", "-mod", source,
377 +
                "-mod", h.join(&["detail=", child]), "-mod", assembly, "-embed", data,
378 +
                "-entry", "portable", "-ril", output,
379 +
            ]
380 +
        );
381 +
        if not h.compile("compile a relocated package", process::Output::File(report)) {
382 +
            return;
383 +
        }
384 +
        let native = h.join(&[checkout, "/portable.rv64"]);
385 +
        let nativeLog = h.out(&["relocated-native.log"]);
386 +
        h.add(
387 +
            &[
388 +
                "-cache", h.join(&[root, "/cache"]), "-debug",
389 +
                "-pkg", "portable", "-mod", source, "-mod", h.join(&["detail=", child]),
390 +
                "-mod", assembly, "-embed", data, "-entry", "portable", "-o", native,
391 +
            ]
392 +
        );
393 +
        if not h.compile("link from the relocated cache", nativeLog) {
394 +
            return;
395 +
        }
396 +
        h.expect(
397 +
            "link reuses the source cache",
398 +
            h.path(&["relocated-native.log"]),
399 +
            harness::Expect::Contains("cache hit portable")
400 +
        );
401 +
        for path in &[source, child] {
402 +
            h.expect(
403 +
                "debug locations use current source paths",
404 +
                h.join(&[native, ".debug"]),
405 +
                harness::Expect::Contains(path)
406 +
            );
407 +
        }
408 +
        for path in &["portable.interface/package.metadata", "portable.interface/locations"] {
409 +
            h.expect(
410 +
                "artifacts omit checkout paths",
411 +
                h.join(&[output, "/", path]),
412 +
                harness::Expect::Matching { text: root, count: 0 }
413 +
            );
414 +
        }
415 +
        if i == 1 {
416 +
            h.expect("relocation reuses compiled packages", report, harness::Expect::Contains(
417 +
                "cache hit portable"
418 +
            ));
419 +
            for path in &[
420 +
                "portable.ril.bin", "portable.interface/package.metadata",
421 +
                "portable.interface/locations",
422 +
            ] {
423 +
                h.expect(
424 +
                    "relocation preserves package artifacts",
425 +
                    h.join(&[second, "/", path]),
426 +
                    harness::Expect::Same(h.join(&[first, "/", path]))
427 +
                );
428 +
            }
429 +
        } else if i == 2 {
430 +
            h.expect(
431 +
                "relative origin edits rebuild artifact metadata",
432 +
                report,
433 +
                harness::Expect::Matching { text: "cache hit portable", count: 0 }
434 +
            );
435 +
            h.expect(
436 +
                "retain relative source origins",
437 +
                h.join(&[output, "/portable.interface/package.metadata"]),
438 +
                harness::Expect::Contains("origin: \"renamed.ras\"")
439 +
            );
440 +
        }
441 +
    }
442 +
    let image = h.join(&[root, "/portable.rv64"]);
443 +
    h.add(&["-no-cache", "-debug", "-pkg", "portable", "-artifact", second, "-o", image]);
444 +
    if h.compile("link a relocated artifact", h.out(&["relocation-link.log"])) {
445 +
        execute(h, image);
446 +
    }
447 +
}
448 +
449 +
/// Record compiler provenance without making it an artifact compatibility check.
450 +
fn producer(h: &mut Harness, exported: *[u8]) throws (sys::Error) {
451 +
    let path = h.join(&[exported, "/dep.interface/package.metadata"]);
452 +
    let original = try h.load(path);
453 +
    let field = "compiler: \"radiance-1.0.0\"";
454 +
    if not h.expect("record the artifact compiler", path, harness::Expect::Contains(field)) {
455 +
        return;
456 +
    }
457 +
    let start = mem::find(original, field) else panic "producer: missing compiler field";
458 +
    try fs::writeFileParts(path, &[
459 +
        &original[.. start], "compiler: \"another-compiler-2.0.0\"", &original[start + field.len ..]
460 +
    ]);
461 +
    let output = h.path(&["producer-exported"]);
462 +
    h.add(&["-no-cache", "-pkg", "dep", "-artifact", exported, "-ril", output]);
463 +
    if h.compile("read compatible artifacts from another producer", h.out(&["producer.log"])) {
464 +
        h.expect(
465 +
            "preserve original artifact provenance",
466 +
            h.join(&[output, "/dep.interface/package.metadata"]),
467 +
            harness::Expect::Contains("compiler: \"another-compiler-2.0.0\"")
468 +
        );
469 +
    }
470 +
    try fs::writeFile(path, original);
471 +
    h.add(&["-version"]);
472 +
    h.compile("print the compiler identity", h.out(&["compiler-version.log"]));
473 +
    h.expect(
474 +
        "compiler identity is readable",
475 +
        h.path(&["compiler-version.log"]),
476 +
        harness::Expect::Contains("radiance-1.0.0")
477 +
    );
478 +
}
479 +
208 480
/// Check public package re-exports and private aliases through generated interfaces.
209 481
fn reexports(h: &mut Harness) throws (sys::Error) {
210 482
    let root = h.path(&["reexports"]);
211 483
    try harness::makeDirs(root);
212 484
    let base = h.join(&[root, "/base.rad"]);
502 774
    source(h, exported, "-ril");
503 775
    if not h.compile("export complete package closure", h.out(&["export.log"])) {
504 776
        return;
505 777
    }
506 778
    try compilerImage(h, exported);
779 +
    try producer(h, exported);
507 780
    try metadata(h, exported);
508 781
    let metadataPath = h.join(&[exported, "/dep.interface/package.metadata"]);
509 782
    h.expect("artifact metadata is text", metadataPath, harness::Expect::Contains(
510 783
        "version: 1,"
511 784
    ));
624 897
        &[]
625 898
    );
626 899
    try metadataReuse(h);
627 900
    try reexports(h);
628 901
    try embeds(h);
902 +
    try privateEmbeds(h);
903 +
    try cleanCache(h);
904 +
    try relocation(h);
629 905
    try paths(h);
630 906
    try large(h);
631 907
    h.done(mark, "packages: source-free linking, cache reuse, and integrity checks passed");
632 908
}
test/packages/private-embeds/app.rad added +10 -0
1 +
use text;
2 +
use text::labels;
3 +
4 +
@default fn main() -> u32 {
5 +
    assert text::privateText()[0] == 'q';
6 +
    assert labels::VALUE.len == 6;
7 +
    assert labels::VALUE[0] == 'h';
8 +
9 +
    return 0;
10 +
}
test/packages/private-embeds/text.rad added +7 -0
1 +
export mod labels;
2 +
3 +
constant UNUSED: *[u8] = @embed("data/unused.txt");
4 +
5 +
export fn privateText() -> *[u8] {
6 +
    return @embed("data/private.txt");
7 +
}
test/packages/relocation/entry.rad added +10 -0
1 +
export mod detail;
2 +
3 +
export constant DATA: *[u8] = @embed("asset.bin");
4 +
5 +
@default fn main() -> u32 {
6 +
    assert detail::answer() == 42;
7 +
    assert DATA[0] == 'x';
8 +
9 +
    return 0;
10 +
}