From c67eb4dd738813a6cd229bfebaa3d5eb7e109626 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 7 Aug 2024 20:53:22 +0700 Subject: [PATCH] refactor: merge compiler and flags inputs to options input --- dist/index.mjs | 44 +++++++++++++++++++++++++------------------- src/cmake.test.ts | 32 -------------------------------- src/cmake.ts | 16 ---------------- src/context.test.ts | 28 +++++++++++++++------------- src/context.ts | 40 +++++++++++++++++++++++++++++----------- 5 files changed, 69 insertions(+), 91 deletions(-) diff --git a/dist/index.mjs b/dist/index.mjs index fadcea4..39bb779 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -19,18 +19,6 @@ function configureProject(context) { if (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.args); execFileSync("cmake", configureArgs, { stdio: "inherit" }); @@ -57,17 +45,35 @@ function getInput(key) { } function getContext() { 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 { sourceDir, buildDir: getInput("build-dir") || path.join(sourceDir, "build"), generator: getInput("generator"), - cCompiler: getInput("c-compiler"), - 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 != ""), + options, args: getInput("args") .split(/\s+/) .filter((arg) => arg != ""), diff --git a/src/cmake.test.ts b/src/cmake.test.ts index 6718f2a..c18ef96 100644 --- a/src/cmake.test.ts +++ b/src/cmake.test.ts @@ -11,10 +11,6 @@ const defaultContext: Context = { sourceDir: "", buildDir: "build", generator: "", - cCompiler: "", - cxxCompiler: "", - cFlags: "", - cxxFlags: "", options: [], args: [], runBuild: true, @@ -46,26 +42,6 @@ describe("configure a CMake project", () => { context: { generator: "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", context: { options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"] }, @@ -87,10 +63,6 @@ describe("configure a CMake project", () => { sourceDir: "project", buildDir: "output", generator: "Ninja", - cCompiler: "clang", - cxxCompiler: "clang++", - cFlags: "-Werror -Wall", - cxxFlags: "-Werror -Wall -Wextra", options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"], args: ["-Wdev", "-Wdeprecated"], }, @@ -100,10 +72,6 @@ describe("configure a CMake project", () => { "output", "-G", "Ninja", - "-DCMAKE_C_COMPILER=clang", - "-DCMAKE_CXX_COMPILER=clang++", - "-DCMAKE_C_FLAGS=-Werror -Wall", - "-DCMAKE_CXX_FLAGS=-Werror -Wall -Wextra", "-DBUILD_TESTING=ON", "-DBUILD_EXAMPLES=ON", "-Wdev", diff --git a/src/cmake.ts b/src/cmake.ts index 24b8462..f9a27da 100644 --- a/src/cmake.ts +++ b/src/cmake.ts @@ -19,22 +19,6 @@ export function configureProject(context: Context): void { 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.args); diff --git a/src/context.test.ts b/src/context.test.ts index fd20839..43e9386 100644 --- a/src/context.test.ts +++ b/src/context.test.ts @@ -44,22 +44,24 @@ describe("get action context", () => { { name: "with C compiler specified", env: { "INPUT_C-COMPILER": "clang" }, - expectedContext: { cCompiler: "clang" }, + expectedContext: { options: ["CMAKE_C_COMPILER=clang"] }, }, { name: "with C++ compiler specified", env: { "INPUT_CXX-COMPILER": "clang++" }, - expectedContext: { cxxCompiler: "clang++" }, + expectedContext: { options: ["CMAKE_CXX_COMPILER=clang++"] }, }, { name: "with C flags specified", 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", 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", @@ -104,11 +106,15 @@ describe("get action context", () => { sourceDir: "project", buildDir: "output", generator: "Ninja", - cCompiler: "clang", - cxxCompiler: "clang++", - cFlags: "-Werror -Wall -Wextra", - cxxFlags: "-Werror -Wall -Wextra -Wpedantic", - options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"], + options: [ + "CMAKE_C_COMPILER=clang", + "CMAKE_CXX_COMPILER=clang++", + "CMAKE_C_FLAGS=-Werror -Wall -Wextra", + "CMAKE_CXX_FLAGS=-Werror -Wall -Wextra -Wpedantic", + "BUILD_TESTING=ON", + "BUILD_EXAMPLES=ON", + "BUILD_DOCS=ON", + ], args: ["-Wdev", "-Wdeprecated", "--fresh"], runBuild: true, buildArgs: ["--target", "foo", "--parallel", "8"], @@ -128,10 +134,6 @@ describe("get action context", () => { sourceDir: "", buildDir: "build", generator: "", - cCompiler: "", - cxxCompiler: "", - cFlags: "", - cxxFlags: "", options: [], args: [], runBuild: false, diff --git a/src/context.ts b/src/context.ts index 712a13d..058f11f 100644 --- a/src/context.ts +++ b/src/context.ts @@ -4,10 +4,6 @@ export interface Context { sourceDir: string; buildDir: string; generator: string; - cCompiler: string; - cxxCompiler: string; - cFlags: string; - cxxFlags: string; options: string[]; args: string[]; runBuild: boolean; @@ -26,17 +22,39 @@ function getInput(key: string): string { export function getContext(): Context { 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 { sourceDir, buildDir: getInput("build-dir") || path.join(sourceDir, "build"), generator: getInput("generator"), - cCompiler: getInput("c-compiler"), - 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 != ""), + options, args: getInput("args") .split(/\s+/) .filter((arg) => arg != ""),