Update c-cpp.yml #53
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build | |
on: | |
push: | |
jobs: | |
build-project: | |
name: Build Project | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Project | |
uses: actions/[email protected] | |
- name: Fetch master | |
run: | | |
git fetch --no-tags --prune --depth=1 origin master | |
- name: Setup Ninja | |
uses: seanmiddleditch/gha-setup-ninja@v5 | |
- name: Build Project | |
uses: threeal/[email protected] | |
with: | |
generator: Ninja | |
options: | | |
CMAKE_EXPORT_COMPILE_COMMANDS=ON | |
ENABLE_COVERAGE=ON | |
run-build: false | |
- name: Get affected targets | |
id: find-targets | |
uses: actions/github-script@v7 | |
with: | |
result-encoding: string | |
script: | | |
const fs = require('fs'); | |
const { execSync } = require('child_process'); | |
let sources = execSync(`git diff --name-only origin/master`).toString().split('\n') | |
let outputs = []; | |
let commands = JSON.parse(fs.readFileSync('build/compile_commands.json', 'utf8')); | |
for (let 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(' '); | |
- name: Build affected targets | |
if: ${{steps.find-targets.outputs.result != ''}} | |
run: ninja -C build ${{steps.find-targets.outputs.result}} | |
- name: Run ctest | |
if: ${{steps.find-targets.outputs.result != ''}} | |
run: ctest --test-dir build | |
- name: Generate a code coverage report | |
if: ${{steps.find-targets.outputs.result != ''}} | |
uses: threeal/gcovr-action@xml-out | |
with: | |
xml-out: coverage.xml | |
- run: npm install xml2js | |
- name: Check coverage report | |
id: check-coverage | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const fs = require('fs'); | |
const parseString = require('xml2js').parseString; | |
const result = await new Promise((resolve, reject) => parseString(fs.readFileSync('coverage.xml', 'utf8'), (err, result) => { | |
if (err) reject(err); | |
else resolve(result); | |
})); | |
console.log('${{ github.base_ref }}'); | |
console.log('${{ github.head_ref }}'); | |
console.log('${{ github.ref }}'); | |
console.log('${{ github.ref_name }}'); | |
console.log('${{ github.path }}'); | |
# "packages": [ | |
# { | |
# "package": [ | |
# { | |
# "$": { | |
# "name": "src", | |
# "line-rate": "0.5", | |
# "branch-rate": "0.5", | |
# "complexity": "0.0" | |
# }, | |
# "classes": [ | |
# { | |
# "class": [ | |
# { | |
# "$": { | |
# "name": "main_cpp", | |
# "filename": "src/main.cpp", | |
# "line-rate": "0.0", | |
# "branch-rate": "1.0", | |
# "complexity": "0.0" | |
# }, | |
# "methods": [ | |
# "" | |
# ], | |
# "lines": [ | |
# { | |
# "line": [ | |
# { | |
# "$": { | |
# "number": "6", | |
# "hits": "0", | |
# "branch": "false" | |
# } | |
# }, | |
# { | |
# "$": { | |
# "number": "7", | |
# "hits": "0", | |
# "branch": "false" | |
# } | |
# }, |