mirror of
https://github.com/threeal/cmake-action.git
synced 2025-04-20 10:41:21 +00:00
refactor: merge compiler and flags inputs to options input
This commit is contained in:
parent
98f46f3e06
commit
c67eb4dd73
44
dist/index.mjs
generated
vendored
44
dist/index.mjs
generated
vendored
@ -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 != ""),
|
||||
|
@ -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",
|
||||
|
16
src/cmake.ts
16
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);
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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 != ""),
|
||||
|
Loading…
Reference in New Issue
Block a user