-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathlint_cpp.mjs
More file actions
111 lines (98 loc) · 5 KB
/
lint_cpp.mjs
File metadata and controls
111 lines (98 loc) · 5 KB
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
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ Copyright (c) 2017, the Perspective Authors. ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃ This file is part of the Perspective library, distributed under the terms ┃
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
import sh from "./sh.mjs";
import * as fs from "fs";
import * as url from "url";
import * as os from "os";
import { execSync } from "child_process";
import * as dotenv from "dotenv";
import "zx/globals";
const __dirname = url.fileURLToPath(new URL(".", import.meta.url)).slice(0, -1);
export function tidyLint(flags) {
dotenv.config({ path: "./.perspectiverc", quiet: true });
// if (process.env.PSP_PROJECT === "js") {
const cppPath = sh.path`${__dirname}/../../rust/perspective-server/cpp/perspective`;
const cppDistPath = sh.path`${cppPath}/dist/release`;
tidy(cppDistPath, sh.path`${cppPath}/src`, flags);
// } else if (process.env.PSP_PROJECT === "python") {
// const cppPath = sh.path`${__dirname}/../../python/perspective`;
// const cppDistPath = sh.path`${cppPath}/build/last_build`;
// tidy(cppDistPath, sh.path`${cppPath}/perspective`, flags);
// } else {
// console.error("Unknown project type, skipping lint");
// }
}
/** @typedef {import('./sh.mjs').Command} Command */
/**
* Runs clang tidy on the source directory using the compile_commands.json
* from the build directory.
*
* @param {string} buildDir
* @param {string} sourceDir
*/
function tidy(buildDir, sourceDir, flags) {
const ctpath = CLANG_TIDY;
// if (!fs.existsSync(ctpath)) {
// console.warn("run-clang-tidy not found, skipping lint");
// return;
// }
if (fs.existsSync(buildDir)) {
const jobs = os.cpus().length;
const sources = glob
.sync(`${sourceDir}/**/*.{cpp,h}`)
.filter((x) => -1 === x.indexOf("emscripten"));
// `-extra-arg=-I${buildDir + "/../../../../.emsdk/upstream/emscripten/system/include"}`
sh`${ctpath} -use-color ${flags} -quiet -p${buildDir} -extra-arg=-UPSP_ENABLE_WASM -j${jobs} ${sources}`.runSync();
} else {
console.warn("No C++ build directory found, skipping lint");
}
}
const CLANG_TIDY = `run-clang-tidy`;
const CLANG_FORMAT = fs.existsSync(
`${__dirname}/../../.llvm/llvm-toolchain/bin/clang-format`,
)
? `${__dirname}/../../.llvm/llvm-toolchain/bin/clang-format`
: `clang-format`;
function formatLint(dir) {
try {
execSync(`${CLANG_FORMAT} -style=file --dry-run -Werror ${dir}`, {
stdio: "inherit",
});
} catch (e) {
// `clang-format` has been a complete disaster, this exception prepares
// to remove it from the build chain entirely.
console.error("C++ linting fails");
}
}
function clangFormatFix(dir) {
execSync(`${CLANG_FORMAT} -style=file -i ${dir}`, {
stdio: "inherit",
});
}
export function checkFormatting() {
formatLint(
sh.path`./rust/perspective-server/cpp/perspective/src/cpp/*.cpp`,
);
formatLint(
sh.path`./rust/perspective-server/cpp/perspective/src/include/perspective/*.h`,
);
// tidyLint();
}
export function fixFormatting() {
// tidyLint("-fix");
clangFormatFix(
sh.path`./rust/perspective-server/cpp/perspective/src/cpp/*.cpp`,
);
clangFormatFix(
sh.path`./rust/perspective-server/cpp/perspective/src/include/perspective/*.h`,
);
}