summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/common/tools/gen_cache.ts
blob: ce0854aa20463be994b074f58638e6cc2b2d1635 (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
193
194
195
196
197
198
import * as fs from 'fs';
import * as path from 'path';
import * as process from 'process';

import { Cacheable, dataCache, setIsBuildingDataCache } from '../framework/data_cache.js';

function usage(rc: number): void {
  console.error(`Usage: tools/gen_cache [options] [OUT_DIR] [SUITE_DIRS...]

For each suite in SUITE_DIRS, pre-compute data that is expensive to generate
at runtime and store it under OUT_DIR. If the data file is found then the
DataCache will load this instead of building the expensive data at CTS runtime.

Options:
  --help          Print this message and exit.
  --list          Print the list of output files without writing them.
  --nth i/n       Only process every file where (file_index % n == i)
  --validate      Check that cache should build (Tests for collisions).
  --verbose       Print each action taken.
`);
  process.exit(rc);
}

let mode: 'emit' | 'list' | 'validate' = 'emit';
let nth = { i: 0, n: 1 };
let verbose = false;

const nonFlagsArgs: string[] = [];

for (let i = 0; i < process.argv.length; i++) {
  const arg = process.argv[i];
  if (arg.startsWith('-')) {
    switch (arg) {
      case '--list': {
        mode = 'list';
        break;
      }
      case '--help': {
        usage(0);
        break;
      }
      case '--verbose': {
        verbose = true;
        break;
      }
      case '--validate': {
        mode = 'validate';
        break;
      }
      case '--nth': {
        const err = () => {
          console.error(
            `--nth requires a value of the form 'i/n', where i and n are positive integers and i < n`
          );
          process.exit(1);
        };
        i++;
        if (i >= process.argv.length) {
          err();
        }
        const value = process.argv[i];
        const parts = value.split('/');
        if (parts.length !== 2) {
          err();
        }
        nth = { i: parseInt(parts[0]), n: parseInt(parts[1]) };
        if (nth.i < 0 || nth.n < 1 || nth.i > nth.n) {
          err();
        }
        break;
      }
      default: {
        console.log('unrecognized flag: ', arg);
        usage(1);
      }
    }
  } else {
    nonFlagsArgs.push(arg);
  }
}

if (nonFlagsArgs.length < 4) {
  usage(0);
}

const outRootDir = nonFlagsArgs[2];

dataCache.setStore({
  load: (path: string) => {
    return new Promise<Uint8Array>((resolve, reject) => {
      fs.readFile(`data/${path}`, (err, data) => {
        if (err !== null) {
          reject(err.message);
        } else {
          resolve(data);
        }
      });
    });
  },
});
setIsBuildingDataCache();

void (async () => {
  for (const suiteDir of nonFlagsArgs.slice(3)) {
    await build(suiteDir);
  }
})();

const specFileSuffix = __filename.endsWith('.ts') ? '.spec.ts' : '.spec.js';

async function crawlFilesRecursively(dir: string): Promise<string[]> {
  const subpathInfo = await Promise.all(
    (await fs.promises.readdir(dir)).map(async d => {
      const p = path.join(dir, d);
      const stats = await fs.promises.stat(p);
      return {
        path: p,
        isDirectory: stats.isDirectory(),
        isFile: stats.isFile(),
      };
    })
  );

  const files = subpathInfo
    .filter(i => i.isFile && i.path.endsWith(specFileSuffix))
    .map(i => i.path);

  return files.concat(
    await subpathInfo
      .filter(i => i.isDirectory)
      .map(i => crawlFilesRecursively(i.path))
      .reduce(async (a, b) => (await a).concat(await b), Promise.resolve([]))
  );
}

async function build(suiteDir: string) {
  if (!fs.existsSync(suiteDir)) {
    console.error(`Could not find ${suiteDir}`);
    process.exit(1);
  }

  // Crawl files and convert paths to be POSIX-style, relative to suiteDir.
  let filesToEnumerate = (await crawlFilesRecursively(suiteDir)).sort();

  // Filter out non-spec files
  filesToEnumerate = filesToEnumerate.filter(f => f.endsWith(specFileSuffix));

  const cacheablePathToTS = new Map<string, string>();

  let fileIndex = 0;
  for (const file of filesToEnumerate) {
    const pathWithoutExtension = file.substring(0, file.length - specFileSuffix.length);
    const mod = await import(`../../../${pathWithoutExtension}.spec.js`);
    if (mod.d?.serialize !== undefined) {
      const cacheable = mod.d as Cacheable<unknown>;

      {
        // Check for collisions
        const existing = cacheablePathToTS.get(cacheable.path);
        if (existing !== undefined) {
          console.error(
            `error: Cacheable '${cacheable.path}' is emitted by both:
    '${existing}'
and
    '${file}'`
          );
          process.exit(1);
        }
        cacheablePathToTS.set(cacheable.path, file);
      }

      const outPath = `${outRootDir}/data/${cacheable.path}`;

      if (fileIndex++ % nth.n === nth.i) {
        switch (mode) {
          case 'emit': {
            if (verbose) {
              console.log(`building '${outPath}'`);
            }
            const data = await cacheable.build();
            const serialized = cacheable.serialize(data);
            fs.mkdirSync(path.dirname(outPath), { recursive: true });
            fs.writeFileSync(outPath, serialized, 'binary');
            break;
          }
          case 'list': {
            console.log(outPath);
            break;
          }
          case 'validate': {
            // Only check currently performed is the collision detection above
            break;
          }
        }
      }
    }
  }
}