summaryrefslogtreecommitdiff
path: root/chapter29/src/test/java/com/seaofnodes/simple/IncrementalBuildTest.java
blob: d8f7ef3422da3c911e775ef1ecf69113f4e867c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.seaofnodes.simple;

import com.seaofnodes.simple.codegen.*;
import com.seaofnodes.simple.type.TypeInteger;
import com.seaofnodes.simple.util.Ary;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import org.junit.Test;
import static org.junit.Assert.*;

public class IncrementalBuildTest {
    @Test public void testRequiredTypesBelongToTheirSource() throws Exception {
        Path base = Path.of("build/objs/incremental");
        Files.createDirectories(base);
        Path root = Files.createTempDirectory(base,"required-types-");
        Path src = Files.createDirectory(root.resolve("src")), obj = Files.createDirectory(root.resolve("obj"));
        write(src,"App","val marker = 0;",1000);
        write(src,"App/Main","return A.x+C.y;",1000);
        write(src,"App/A","int x = 0; val readB = { B b -> b.x; };",1000);
        write(src,"App/B","int x = 0;",1000);
        write(src,"App/C","int y = 0;",1000);
        CodeGen code = new CodeGen(src.toString(),obj.toString(),null,"App/Main",null,123,true,TypeInteger.BOT)
            .parse();
        ParseAllTestSupport.checkRequiredTypes(code);
    }

    @Test public void testTransitiveSourceChange() throws Exception {
        Path base=Path.of("build/objs/incremental");
        Files.createDirectories(base);
        Path root=Files.createTempDirectory(base,"chain-");
        Path src=Files.createDirectory(root.resolve("src"));
        Path obj=Files.createDirectory(root.resolve("obj"));
        write(src,"App","val marker=0;",1000);
        write(src,"App/B","val value={ -> 5; };",1000);
        write(src,"App/A","val value={ -> B.value()+1; };",1000);
        write(src,"App/C","val value={ -> A.value()+1; };",1000);
        write(src,"App/Main","val run={ -> C.value(); }; return run();",1000);
        compile(src,obj,"App");
        stamp(obj.resolve("App.o"),1500);
        compile(src,obj,"App/B");
        stamp(obj.resolve("App/B.o"),2000);
        compile(src,obj,"App/A");
        stamp(obj.resolve("App/A.o"),3000);
        compile(src,obj,"App/C");
        stamp(obj.resolve("App/C.o"),3500);
        CodeGen code=client(src,obj);
        assertNull("Reuse unchanged A",code._compunits.get("App/A")._src);
        assertNull("Reuse unchanged B",code._compunits.get("App/B")._src);
        assertNull("Reuse unchanged C",code._compunits.get("App/C")._src);
        assertEquals("7",Eval2.eval(code,0));

        write(src,"App/B","val value={ -> 7; };",4000);
        code=client(src,obj);
        assertEquals("9",Eval2.eval(code,0));
        assertNotNull(code._compunits.get("App/C")._src);
        assertNotNull("Reparse A before loading its stale inlined body",code._compunits.get("App/A")._src);
        assertNotNull(code._compunits.get("App/B")._src);
        compile(src,obj,"App/Main");
        checkNative(obj);

        Path clean=Files.createDirectory(root.resolve("clean"));
        assertEquals("9",Eval2.eval(client(src,clean),0));
        compile(src,clean,"App/Main");
        checkNative(clean);
    }

    @Test public void testSourceChangeAndCycle() throws Exception {
        Path root=fixture("bundle-");
        Path src=root.resolve("src"), obj=root.resolve("obj");
        write(src,"App/A","val value={int n -> n ? B.value(n-1) : 1; };",1000);
        write(src,"App/B","val value={int n -> n ? A.value(n-1) : 5; };",1000);
        write(src,"App/Main","return A.value(1);",1000);
        compile(src,obj,"App/A");
        assertEquals("One build timestamp for the cycle",Files.getLastModifiedTime(obj.resolve("App/A.o")),
                     Files.getLastModifiedTime(obj.resolve("App/B.o")));
        stamp(obj.resolve("App/A.o"),3000);
        assertTrue("Each side of the cycle owns an object",Files.exists(obj.resolve("App/B.o")));
        stamp(obj.resolve("App/B.o"),3000);
        CodeGen code=client(src,obj);
        assertEquals("5",Eval2.eval(code,0));
        assertNull(code._compunits.get("App/A")._src);
        assertNull(code._compunits.get("App/B")._src);

        write(src,"App/B","val value={int n -> n ? A.value(n-1) : 7; };",4000);
        code=client(src,obj);
        assertEquals("7",Eval2.eval(code,0));
        assertNotNull(code._compunits.get("App/A")._src);
        assertNotNull(code._compunits.get("App/B")._src);
    }

    @Test public void testMissingDependency() throws Exception {
        Path root=fixture("missing-");
        Path src=root.resolve("src"), obj=root.resolve("obj");
        write(src,"App/A","val value={ -> B.value()+1; };",1000);
        write(src,"App/B","val value={ -> 5; };",1000);
        write(src,"App/Main","return A.value();",1000);
        compile(src,obj,"App/B");
        stamp(obj.resolve("App/B.o"),2000);
        compile(src,obj,"App/A");
        stamp(obj.resolve("App/A.o"),3000);
        Files.delete(obj.resolve("App/B.o"));
        CodeGen code=client(src,obj);
        assertEquals("6",Eval2.eval(code,0));
        assertNotNull(code._compunits.get("App/A")._src);
        assertNotNull(code._compunits.get("App/B")._src);
        Files.delete(src.resolve("App/B.smp"));
        try {
            client(src,obj);
            fail("Missing source and object must not reuse A's inlined body");
        } catch( RuntimeException e ) {
            assertTrue(e.getMessage(),e.getMessage().contains("Undefined name 'B'"));
        }
    }

    @Test public void testLibraryDependency() throws Exception {
        Path root=fixture("library-");
        Path src=root.resolve("src"), obj=root.resolve("obj");
        Path lib=Files.createDirectory(root.resolve("lib"));
        var paths=new Ary<>(new String[]{lib.toString(),obj.toString()});
        library(lib,5);
        stamp(lib.resolve("LibB.o"),2000);
        write(src,"App/A","val value={ -> LibB.value()+1; };",1000);
        write(src,"App/Main","return A.value();",1000);
        new CodeGen(src.toString(),obj.toString(),paths,"App/A",null,123,true,TypeInteger.BOT)
            .driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false);
        stamp(obj.resolve("App/A.o"),3000);
        CodeGen code=new CodeGen(src.toString(),obj.toString(),paths,"App/Main",null,123,true,TypeInteger.BOT)
            .driver(CodeGen.Phase.TypeCheck);
        assertEquals("6",Eval2.eval(code,0));
        assertNull("Unchanged library inputs permit reuse",code._compunits.get("App/A")._src);

        library(lib,7);
        stamp(lib.resolve("LibB.o"),4000);
        code=new CodeGen(src.toString(),obj.toString(),paths,"App/Main",null,123,true,TypeInteger.BOT)
            .driver(CodeGen.Phase.TypeCheck);
        assertEquals("8",Eval2.eval(code,0));
        assertNotNull(code._compunits.get("App/A")._src);
    }

    private static void library(Path lib, int value) {
        new CodeGen(null,lib.toString(),null,"LibB","val value={ -> "+value+"; };",123,true,TypeInteger.BOT)
            .driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false);
    }

    private static void checkNative(Path obj) throws Exception {
        Path driver=obj.resolve("driver.c");
        Files.writeString(driver,"""
            __attribute__((CALL_CONV)) extern long long run(void) __asm__("App.Main.run");
            int main(void) { return run()==9 ? 0 : 1; }
            """);
        String exe=obj.resolve(TestC.OS.startsWith("Windows")?"test.exe":"test").toString();
        Ary<String> libs = new Ary<>(new String[]{obj.resolve("App.o").toString(),
            obj.resolve("App/A.o").toString(),obj.resolve("App/B.o").toString(),obj.resolve("App/C.o").toString()});
        TestC.linkExe(obj.resolve("App/Main.o").toString(),
            TestC.CALL_CONVENTION.equals("win64")?"ms_abi":"sysv_abi",driver.toString(),libs,exe);
        assertEquals("",TestC.exec(TestC.TEST_TIMEOUT_SECONDS,exe));
    }

    private static Path fixture(String prefix) throws Exception {
        Path base=Path.of("build/objs/incremental");
        Files.createDirectories(base);
        Path root=Files.createTempDirectory(base,prefix);
        Path src=Files.createDirectory(root.resolve("src"));
        Path obj=Files.createDirectory(root.resolve("obj"));
        write(src,"App","val marker=0;",1000);
        compile(src,obj,"App");
        stamp(obj.resolve("App.o"),1500);
        return root;
    }

    private static void write(Path src, String name, String text, long time) throws Exception {
        Path path=src.resolve(name+".smp");
        Files.createDirectories(path.getParent());
        Files.writeString(path,text);
        stamp(path,time);
    }

    private static void stamp(Path path, long time) throws Exception {
        Files.setLastModifiedTime(path,FileTime.fromMillis(time));
    }

    private static void compile(Path src, Path obj, String name) {
        new CodeGen(src.toString(),obj.toString(),null,name,null,123,true,TypeInteger.BOT)
            .driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false);
    }

    private static CodeGen client(Path src, Path obj) {
        return new CodeGen(src.toString(),obj.toString(),null,"App/Main",null,123,true,TypeInteger.BOT)
            .driver(CodeGen.Phase.TypeCheck);
    }
}