refactor: merge compiler and flags inputs to options input

This commit is contained in:
Alfi Maulana 2024-08-07 20:53:22 +07:00
parent 98f46f3e06
commit c67eb4dd73
No known key found for this signature in database
GPG Key ID: 2242A64C2A8DF5A4
5 changed files with 69 additions and 91 deletions

44
dist/index.mjs generated vendored
View File

@ -19,18 +19,6 @@ function configureProject(context) {
if (context.generator) { if (context.generator) {
configureArgs.push(...["-G", context.generator]); configureArgs.push(...["-G", context.generator]);
} }
if (context.cCompiler) {
configureArgs.push("-DCMAKE_C_COMPILER=" + context.cCompiler);
}
if (context.cxxCompiler) {
configureArgs.push("-DCMAKE_CXX_COMPILER=" + context.cxxCompiler);
}
if (context.cFlags) {
configureArgs.push("-DCMAKE_C_FLAGS=" + context.cFlags);
}
if (context.cxxFlags) {
configureArgs.push("-DCMAKE_CXX_FLAGS=" + context.cxxFlags);
}
configureArgs.push(...context.options.map((opt) => "-D" + opt)); configureArgs.push(...context.options.map((opt) => "-D" + opt));
configureArgs.push(...context.args); configureArgs.push(...context.args);
execFileSync("cmake", configureArgs, { stdio: "inherit" }); execFileSync("cmake", configureArgs, { stdio: "inherit" });
@ -57,17 +45,35 @@ function getInput(key) {
} }
function getContext() { function getContext() {
const sourceDir = getInput("source-dir"); const sourceDir = getInput("source-dir");
const options = [];
let input = getInput("c-compiler");
if (input)
options.push(`CMAKE_C_COMPILER=${input}`);
input = getInput("cxx-compiler");
if (input)
options.push(`CMAKE_CXX_COMPILER=${input}`);
input = getInput("c-flags");
if (input) {
const flags = input.replaceAll(/\s+/g, " ");
options.push(`CMAKE_C_FLAGS=${flags}`);
}
input = getInput("cxx-flags");
if (input) {
const flags = input.replaceAll(/\s+/g, " ");
options.push(`CMAKE_CXX_FLAGS=${flags}`);
}
input = getInput("options");
if (input) {
const opts = input.split(/\s+/).filter((arg) => arg != "");
for (const opt of opts) {
options.push(opt);
}
}
return { return {
sourceDir, sourceDir,
buildDir: getInput("build-dir") || path.join(sourceDir, "build"), buildDir: getInput("build-dir") || path.join(sourceDir, "build"),
generator: getInput("generator"), generator: getInput("generator"),
cCompiler: getInput("c-compiler"), options,
cxxCompiler: getInput("cxx-compiler"),
cFlags: getInput("c-flags").replaceAll(/\s+/g, " "),
cxxFlags: getInput("cxx-flags").replaceAll(/\s+/g, " "),
options: getInput("options")
.split(/\s+/)
.filter((arg) => arg != ""),
args: getInput("args") args: getInput("args")
.split(/\s+/) .split(/\s+/)
.filter((arg) => arg != ""), .filter((arg) => arg != ""),

View File

@ -11,10 +11,6 @@ const defaultContext: Context = {
sourceDir: "", sourceDir: "",
buildDir: "build", buildDir: "build",
generator: "", generator: "",
cCompiler: "",
cxxCompiler: "",
cFlags: "",
cxxFlags: "",
options: [], options: [],
args: [], args: [],
runBuild: true, runBuild: true,
@ -46,26 +42,6 @@ describe("configure a CMake project", () => {
context: { generator: "Ninja" }, context: { generator: "Ninja" },
expectedArgs: ["-B", "build", "-G", "Ninja"], expectedArgs: ["-B", "build", "-G", "Ninja"],
}, },
{
name: "with C compiler specified",
context: { cCompiler: "clang" },
expectedArgs: ["-B", "build", "-DCMAKE_C_COMPILER=clang"],
},
{
name: "with C++ compiler specified",
context: { cxxCompiler: "clang++" },
expectedArgs: ["-B", "build", "-DCMAKE_CXX_COMPILER=clang++"],
},
{
name: "with C flags specified",
context: { cFlags: "-Werror -Wall" },
expectedArgs: ["-B", "build", "-DCMAKE_C_FLAGS=-Werror -Wall"],
},
{
name: "with C++ flags specified",
context: { cxxFlags: "-Werror -Wall -Wextra" },
expectedArgs: ["-B", "build", "-DCMAKE_CXX_FLAGS=-Werror -Wall -Wextra"],
},
{ {
name: "with additional options specified", name: "with additional options specified",
context: { options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"] }, context: { options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"] },
@ -87,10 +63,6 @@ describe("configure a CMake project", () => {
sourceDir: "project", sourceDir: "project",
buildDir: "output", buildDir: "output",
generator: "Ninja", generator: "Ninja",
cCompiler: "clang",
cxxCompiler: "clang++",
cFlags: "-Werror -Wall",
cxxFlags: "-Werror -Wall -Wextra",
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"], options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"],
args: ["-Wdev", "-Wdeprecated"], args: ["-Wdev", "-Wdeprecated"],
}, },
@ -100,10 +72,6 @@ describe("configure a CMake project", () => {
"output", "output",
"-G", "-G",
"Ninja", "Ninja",
"-DCMAKE_C_COMPILER=clang",
"-DCMAKE_CXX_COMPILER=clang++",
"-DCMAKE_C_FLAGS=-Werror -Wall",
"-DCMAKE_CXX_FLAGS=-Werror -Wall -Wextra",
"-DBUILD_TESTING=ON", "-DBUILD_TESTING=ON",
"-DBUILD_EXAMPLES=ON", "-DBUILD_EXAMPLES=ON",
"-Wdev", "-Wdev",

View File

@ -19,22 +19,6 @@ export function configureProject(context: Context): void {
configureArgs.push(...["-G", context.generator]); configureArgs.push(...["-G", context.generator]);
} }
if (context.cCompiler) {
configureArgs.push("-DCMAKE_C_COMPILER=" + context.cCompiler);
}
if (context.cxxCompiler) {
configureArgs.push("-DCMAKE_CXX_COMPILER=" + context.cxxCompiler);
}
if (context.cFlags) {
configureArgs.push("-DCMAKE_C_FLAGS=" + context.cFlags);
}
if (context.cxxFlags) {
configureArgs.push("-DCMAKE_CXX_FLAGS=" + context.cxxFlags);
}
configureArgs.push(...context.options.map((opt) => "-D" + opt)); configureArgs.push(...context.options.map((opt) => "-D" + opt));
configureArgs.push(...context.args); configureArgs.push(...context.args);

View File

@ -44,22 +44,24 @@ describe("get action context", () => {
{ {
name: "with C compiler specified", name: "with C compiler specified",
env: { "INPUT_C-COMPILER": "clang" }, env: { "INPUT_C-COMPILER": "clang" },
expectedContext: { cCompiler: "clang" }, expectedContext: { options: ["CMAKE_C_COMPILER=clang"] },
}, },
{ {
name: "with C++ compiler specified", name: "with C++ compiler specified",
env: { "INPUT_CXX-COMPILER": "clang++" }, env: { "INPUT_CXX-COMPILER": "clang++" },
expectedContext: { cxxCompiler: "clang++" }, expectedContext: { options: ["CMAKE_CXX_COMPILER=clang++"] },
}, },
{ {
name: "with C flags specified", name: "with C flags specified",
env: { "INPUT_C-FLAGS": "-Werror -Wall\n-Wextra" }, env: { "INPUT_C-FLAGS": "-Werror -Wall\n-Wextra" },
expectedContext: { cFlags: "-Werror -Wall -Wextra" }, expectedContext: { options: ["CMAKE_C_FLAGS=-Werror -Wall -Wextra"] },
}, },
{ {
name: "with C++ flags specified", name: "with C++ flags specified",
env: { "INPUT_CXX-FLAGS": "-Werror -Wall\n-Wextra -Wpedantic" }, env: { "INPUT_CXX-FLAGS": "-Werror -Wall\n-Wextra -Wpedantic" },
expectedContext: { cxxFlags: "-Werror -Wall -Wextra -Wpedantic" }, expectedContext: {
options: ["CMAKE_CXX_FLAGS=-Werror -Wall -Wextra -Wpedantic"],
},
}, },
{ {
name: "with additional options specified", name: "with additional options specified",
@ -104,11 +106,15 @@ describe("get action context", () => {
sourceDir: "project", sourceDir: "project",
buildDir: "output", buildDir: "output",
generator: "Ninja", generator: "Ninja",
cCompiler: "clang", options: [
cxxCompiler: "clang++", "CMAKE_C_COMPILER=clang",
cFlags: "-Werror -Wall -Wextra", "CMAKE_CXX_COMPILER=clang++",
cxxFlags: "-Werror -Wall -Wextra -Wpedantic", "CMAKE_C_FLAGS=-Werror -Wall -Wextra",
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"], "CMAKE_CXX_FLAGS=-Werror -Wall -Wextra -Wpedantic",
"BUILD_TESTING=ON",
"BUILD_EXAMPLES=ON",
"BUILD_DOCS=ON",
],
args: ["-Wdev", "-Wdeprecated", "--fresh"], args: ["-Wdev", "-Wdeprecated", "--fresh"],
runBuild: true, runBuild: true,
buildArgs: ["--target", "foo", "--parallel", "8"], buildArgs: ["--target", "foo", "--parallel", "8"],
@ -128,10 +134,6 @@ describe("get action context", () => {
sourceDir: "", sourceDir: "",
buildDir: "build", buildDir: "build",
generator: "", generator: "",
cCompiler: "",
cxxCompiler: "",
cFlags: "",
cxxFlags: "",
options: [], options: [],
args: [], args: [],
runBuild: false, runBuild: false,

View File

@ -4,10 +4,6 @@ export interface Context {
sourceDir: string; sourceDir: string;
buildDir: string; buildDir: string;
generator: string; generator: string;
cCompiler: string;
cxxCompiler: string;
cFlags: string;
cxxFlags: string;
options: string[]; options: string[];
args: string[]; args: string[];
runBuild: boolean; runBuild: boolean;
@ -26,17 +22,39 @@ function getInput(key: string): string {
export function getContext(): Context { export function getContext(): Context {
const sourceDir = getInput("source-dir"); const sourceDir = getInput("source-dir");
const options: string[] = [];
let input = getInput("c-compiler");
if (input) options.push(`CMAKE_C_COMPILER=${input}`);
input = getInput("cxx-compiler");
if (input) options.push(`CMAKE_CXX_COMPILER=${input}`);
input = getInput("c-flags");
if (input) {
const flags = input.replaceAll(/\s+/g, " ");
options.push(`CMAKE_C_FLAGS=${flags}`);
}
input = getInput("cxx-flags");
if (input) {
const flags = input.replaceAll(/\s+/g, " ");
options.push(`CMAKE_CXX_FLAGS=${flags}`);
}
input = getInput("options");
if (input) {
const opts = input.split(/\s+/).filter((arg) => arg != "");
for (const opt of opts) {
options.push(opt);
}
}
return { return {
sourceDir, sourceDir,
buildDir: getInput("build-dir") || path.join(sourceDir, "build"), buildDir: getInput("build-dir") || path.join(sourceDir, "build"),
generator: getInput("generator"), generator: getInput("generator"),
cCompiler: getInput("c-compiler"), options,
cxxCompiler: getInput("cxx-compiler"),
cFlags: getInput("c-flags").replaceAll(/\s+/g, " "),
cxxFlags: getInput("cxx-flags").replaceAll(/\s+/g, " "),
options: getInput("options")
.split(/\s+/)
.filter((arg) => arg != ""),
args: getInput("args") args: getInput("args")
.split(/\s+/) .split(/\s+/)
.filter((arg) => arg != ""), .filter((arg) => arg != ""),