From 42ff5b59d0ebd83a4a92d603885b72f2c3e12cdd Mon Sep 17 00:00:00 2001 From: Fraxy V Date: Sun, 1 Sep 2024 11:15:36 +0300 Subject: [PATCH] target script --- scripts/diff.js | 60 ++++++++++++++++++++++------------------------ scripts/targets.js | 45 ++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 31 deletions(-) create mode 100644 scripts/targets.js diff --git a/scripts/diff.js b/scripts/diff.js index 806b975..5658d79 100644 --- a/scripts/diff.js +++ b/scripts/diff.js @@ -1,38 +1,36 @@ const { execSync } = require('child_process'); -const { parse } = require('path'); -const output = execSync(`git diff origin/master`).toString().split('\n') - -let count = 0; -let start = 0; -let current = 0; -let sources = []; -for (let i = 0; i < output.length; i++) { - if (output[i].startsWith('diff --git')) { - // skip 'diff --git a/' prefix - const file = output[i].split(' ')[2].substring(2); - sources.push({'file': file, 'lines': []}); - // go to header line, e.g. '@@ -1,2 +1,3 @@' - do { - ++i; +module.exports = () => { + const output = execSync(`git diff origin/master`).toString().split('\n') + let count = 0; + let start = 0; + let current = 0; + let sources = []; + for (let i = 0; i < output.length; i++) { + if (output[i].startsWith('diff --git')) { + // skip 'diff --git a/' prefix + const file = output[i].split(' ')[2].substring(2); + sources.push({'file': file, 'lines': []}); + // go to header line, e.g. '@@ -1,2 +1,3 @@' + do { + ++i; + } + while (!output[i].startsWith('@@')); + output[i].split(' ').forEach((c) => { + if (c.startsWith('+')) { + [start, count] = c.split(','); + start = parseInt(start); + count = parseInt(count); + current = 0; + } + }); } - while (!output[i].startsWith('@@')); - output[i].split(' ').forEach((c) => { - if (c.startsWith('+')) { - [start, count] = c.split(','); - start = parseInt(start); - count = parseInt(count); - current = 0; - } - }); - } - else if (current < count && !output[i].startsWith('-')) { - if (output[i].startsWith('+')) { - sources[sources.length - 1].lines.push(start + current); + else if (current < count && !output[i].startsWith('-')) { + if (output[i].startsWith('+')) { + sources[sources.length - 1].lines.push(start + current); + } + ++current; } - ++current; } -} -module.exports = () => { return sources; }; diff --git a/scripts/targets.js b/scripts/targets.js new file mode 100644 index 0000000..69fe7d9 --- /dev/null +++ b/scripts/targets.js @@ -0,0 +1,45 @@ +const fs = require('fs'); +const { execSync } = require('child_process'); + +module.exports = (sources) => { + let outputs = []; + let commands = JSON.parse(fs.readFileSync('build/compile_commands.json', 'utf8')); + for (let {'file': src, ..._} of sources) { + if (src.endsWith('.cpp') || src.endsWith('.h') || src.endsWith('.c') + || src.endsWith('.hpp')) { + const idx = commands.findIndex(entry => entry.file.endsWith(src)); + if (idx >= 0) { + let entry = commands[idx]; + if (entry.output !== 'undefined') { + entry.output = entry.command.split(' ').find(token => token.endsWith('.o')); + } + outputs.push(entry.output); + commands.splice(idx, 1); + } + } + } + + targets = new Set(); + while (outputs.length > 0) { + level_targets = new Set(); + for (const output of outputs) { + let lines = execSync(`ninja -C build -t query ${output}`).toString().split('\n'); + + let insert = false; + for (const line of lines) { + if (line.endsWith('outputs:')) { + insert = true; + continue; + } + const value = line.trim(); + if (insert && value.length > 0) { + level_targets.add(value); + } + } + } + + outputs = [...level_targets]; + targets = new Set([...targets, ...level_targets]); + } + return [...targets].join(' '); +} \ No newline at end of file