| 1 | pub fn addCases(ctx: *LinkContext) void { |
| 2 | if (ctx.includeTest("ctor-dtor")) |case| { |
| 3 | if (!ctx.link_libc) return; |
| 4 | |
| 5 | const obj = case.addObject(.{ |
| 6 | .name = "obj", |
| 7 | .use_llvm = true, |
| 8 | .use_lld = true, |
| 9 | .c_source_bytes = |
| 10 | \\#include <stdlib.h> |
| 11 | \\int foo; |
| 12 | \\__attribute__((constructor)) |
| 13 | \\static void init_foo() { |
| 14 | \\ foo = 42; |
| 15 | \\} |
| 16 | \\__attribute__((destructor)) |
| 17 | \\static void deinit_foo() { |
| 18 | \\ exit(42); |
| 19 | \\} |
| 20 | , |
| 21 | }); |
| 22 | |
| 23 | const lib = case.addLibrary(.static, .{ |
| 24 | .name = "lib", |
| 25 | .name_prefix = false, |
| 26 | .name_target = false, |
| 27 | }); |
| 28 | lib.root_module.addObject(obj); |
| 29 | |
| 30 | const exe = case.addExecutable(.{ |
| 31 | .name = "test", |
| 32 | .zig_source_bytes = |
| 33 | \\extern var foo: u32; |
| 34 | \\pub fn main() !u8 { |
| 35 | \\ if (foo != 42) return 1; |
| 36 | \\ return 2; |
| 37 | \\} |
| 38 | , |
| 39 | }); |
| 40 | exe.root_module.addObject(obj); |
| 41 | |
| 42 | const run = case.addRunArtifact(exe); |
| 43 | run.addCheck(.{ .expect_term = .{ .exited = 42 } }); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | const LinkContext = @import("../tests.zig").LinkContext; |
| 48 | const std = @import("std"); |