setup-sdl: Also check for CMAKE_TOOLCHAIN_FILE environment variable

From c3bf7ade3d7f3d4c15e76c6820d75a9acb4452b5 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sun, 18 Jun 2023 23:03:25 +0200
Subject: [PATCH] Also check for CMAKE_TOOLCHAIN_FILE environment variable

---
 packed/index.js | 37 ++++++++++++++++++++++++---------
 src/main.ts     | 54 ++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 68 insertions(+), 23 deletions(-)

diff --git a/packed/index.js b/packed/index.js
index 553f7d9..78e11d3 100644
--- a/packed/index.js
+++ b/packed/index.js
@@ -162,6 +162,7 @@ function calculate_state_hash(args) {
         "LIB",
         "LIBPATH",
         "CMAKE_PREFIX_PATH",
+        "CMAKE_TOOLCHAIN_FILE",
         "PKG_CONFIG_PATH",
     ];
     const env_state = [];
@@ -206,19 +207,37 @@ function calculate_state_hash(args) {
     core.debug(`state_string=${state_string}`);
     return crypto.createHash("sha256").update(state_string).digest("hex");
 }
+function resolve_workspace_path(in_path) {
+    if (!in_path) {
+        return undefined;
+    }
+    if (fs.existsSync(in_path)) {
+        return path.resolve(in_path);
+    }
+    const workspace_path = path.resolve(`${process.env.GITHUB_WORKSPACE}`, in_path);
+    if (fs.existsSync(workspace_path)) {
+        return workspace_path;
+    }
+    return undefined;
+}
 function get_cmake_toolchain_path() {
     const in_cmake_toolchain_file = core.getInput("cmake-toolchain-file");
-    if (!in_cmake_toolchain_file) {
-        return in_cmake_toolchain_file;
-    }
-    if (fs.existsSync(in_cmake_toolchain_file)) {
-        return path.resolve(in_cmake_toolchain_file);
+    if (in_cmake_toolchain_file) {
+        const resolved_cmake_toolchain_file = resolve_workspace_path(in_cmake_toolchain_file);
+        if (!resolved_cmake_toolchain_file) {
+            throw new util_1.SetupSdlError(`Cannot find CMake toolchain file: ${in_cmake_toolchain_file}`);
+        }
+        return resolved_cmake_toolchain_file;
     }
-    const workspace_cmake_toolchain_file = path.resolve(`${process.env.GITHUB_WORKSPACE}`, in_cmake_toolchain_file);
-    if (fs.existsSync(workspace_cmake_toolchain_file)) {
-        return workspace_cmake_toolchain_file;
+    const env_cmake_toolchain_file = process.env.CMAKE_TOOLCHAIN_FILE;
+    if (env_cmake_toolchain_file) {
+        const resolved_cmake_toolchain_file = resolve_workspace_path(env_cmake_toolchain_file);
+        if (!resolved_cmake_toolchain_file) {
+            throw new util_1.SetupSdlError(`Cannot find CMake toolchain file: ${env_cmake_toolchain_file}`);
+        }
+        return resolved_cmake_toolchain_file;
     }
-    throw new util_1.SetupSdlError(`Cannot find CMake toolchain file: ${in_cmake_toolchain_file}`);
+    return undefined;
 }
 async function run() {
     const SDL_BUILD_PLATFORM = (0, platform_1.get_sdl_build_platform)();
diff --git a/src/main.ts b/src/main.ts
index 98ba44c..7eb8841 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -118,7 +118,7 @@ function calculate_state_hash(args: {
   git_hash: string;
   build_platform: SdlBuildPlatform;
   shell: string;
-  cmake_toolchain_file: string;
+  cmake_toolchain_file: string | undefined;
 }) {
   const ENV_KEYS = [
     "AR",
@@ -132,6 +132,7 @@ function calculate_state_hash(args: {
     "LIB",
     "LIBPATH",
     "CMAKE_PREFIX_PATH",
+    "CMAKE_TOOLCHAIN_FILE",
     "PKG_CONFIG_PATH",
   ];
   const env_state: string[] = [];
@@ -184,24 +185,49 @@ function calculate_state_hash(args: {
   return crypto.createHash("sha256").update(state_string).digest("hex");
 }
 
-function get_cmake_toolchain_path(): string {
-  const in_cmake_toolchain_file = core.getInput("cmake-toolchain-file");
-  if (!in_cmake_toolchain_file) {
-    return in_cmake_toolchain_file;
+function resolve_workspace_path(in_path: string): string | undefined {
+  if (!in_path) {
+    return undefined;
   }
-  if (fs.existsSync(in_cmake_toolchain_file)) {
-    return path.resolve(in_cmake_toolchain_file);
+  if (fs.existsSync(in_path)) {
+    return path.resolve(in_path);
   }
-  const workspace_cmake_toolchain_file = path.resolve(
+  const workspace_path = path.resolve(
     `${process.env.GITHUB_WORKSPACE}`,
-    in_cmake_toolchain_file
+    in_path
   );
-  if (fs.existsSync(workspace_cmake_toolchain_file)) {
-    return workspace_cmake_toolchain_file;
+  if (fs.existsSync(workspace_path)) {
+    return workspace_path;
   }
-  throw new SetupSdlError(
-    `Cannot find CMake toolchain file: ${in_cmake_toolchain_file}`
-  );
+  return undefined;
+}
+
+function get_cmake_toolchain_path(): string | undefined {
+  const in_cmake_toolchain_file = core.getInput("cmake-toolchain-file");
+  if (in_cmake_toolchain_file) {
+    const resolved_cmake_toolchain_file = resolve_workspace_path(
+      in_cmake_toolchain_file
+    );
+    if (!resolved_cmake_toolchain_file) {
+      throw new SetupSdlError(
+        `Cannot find CMake toolchain file: ${in_cmake_toolchain_file}`
+      );
+    }
+    return resolved_cmake_toolchain_file;
+  }
+  const env_cmake_toolchain_file = process.env.CMAKE_TOOLCHAIN_FILE;
+  if (env_cmake_toolchain_file) {
+    const resolved_cmake_toolchain_file = resolve_workspace_path(
+      env_cmake_toolchain_file
+    );
+    if (!resolved_cmake_toolchain_file) {
+      throw new SetupSdlError(
+        `Cannot find CMake toolchain file: ${env_cmake_toolchain_file}`
+      );
+    }
+    return resolved_cmake_toolchain_file;
+  }
+  return undefined;
 }
 
 async function run() {