setup-sdl: Add 'add-to-environment' option to modify the environment

From 6316b695a3b1994b32a28197e1aa3d96d04ecb11 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sun, 18 Jun 2023 21:38:45 +0200
Subject: [PATCH] Add 'add-to-environment' option to modify the environment

---
 action.yml      |  6 +++---
 packed/index.js | 41 +++++++++++++++++++++++++++++++++++++----
 src/main.ts     | 20 ++++++++++++++++----
 src/platform.ts | 27 +++++++++++++++++++++++++++
 4 files changed, 83 insertions(+), 11 deletions(-)

diff --git a/action.yml b/action.yml
index f07d837..2d44136 100644
--- a/action.yml
+++ b/action.yml
@@ -16,13 +16,13 @@ inputs:
   ninja:
     description: "Use Ninja make files"
     default: "true"
-    required: true
   shell:
     description: "Run commands as $shell \"commands\""
-    required: true
   discriminator:
     description: "Unique string to avoid fetching a mismatched SDL from cache"
-    required: true
+  add-to-environment:
+    description: "Add path of the SDL (shared) library to PATH/LD_LIBRARY_PATH/DYLD_LIBRARY_PATH"
+    default: "false"
 outputs:
   prefix:
     description: "Actual root of the SDL package"
diff --git a/packed/index.js b/packed/index.js
index a896861..05cf48b 100644
--- a/packed/index.js
+++ b/packed/index.js
@@ -134,7 +134,8 @@ function execute_child_process(command, shell) {
     child_process.execSync(final_command, { stdio: "inherit" });
 }
 async function cmake_configure_build(args) {
-    const configure_command = `cmake -S "${args.source_dir}" -B "${args.build_dir}" ${args.cmake_args}`;
+    const cmake_args = args.cmake_args.join(" ");
+    const configure_command = `cmake -S "${args.source_dir}" -B "${args.build_dir}" ${cmake_args}`;
     const build_command = `cmake --build "${args.build_dir}" --config ${args.build_type}`;
     const install_command = `cmake --install "${args.build_dir}" --prefix ${args.package_dir} --config ${args.build_type}`;
     await core.group(`Configuring SDL (CMake)`, async () => {
@@ -259,9 +260,14 @@ async function run() {
                 await (0, ninja_1.configure_ninja_build_tool)(SDL_BUILD_PLATFORM);
             });
         }
-        let cmake_args = `-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}`;
+        const cmake_args = [
+            `-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}`,
+            "-DCMAKE_INSTALL_BINDIR=bin",
+            "-DCMAKE_INSTALL_INCLUDEDIR=include",
+            "-DCMAKE_INSTALL_LIBDIR=lib",
+        ];
         if (USE_NINJA) {
-            cmake_args += " -GNinja";
+            cmake_args.push("-GNinja");
         }
         await cmake_configure_build({
             source_dir: SOURCE_DIR,
@@ -277,6 +283,9 @@ async function run() {
     }
     const SDL_VERSION = version_1.SdlVersion.detect_sdl_version_from_install_prefix(PACKAGE_DIR);
     core.info(`SDL version is ${SDL_VERSION.toString()}`);
+    if (core.getBooleanInput("add-to-environment")) {
+        (0, platform_1.export_environent_variables)(SDL_BUILD_PLATFORM, PACKAGE_DIR);
+    }
     core.exportVariable(`SDL${SDL_VERSION.major}_ROOT`, PACKAGE_DIR);
     core.setOutput("prefix", PACKAGE_DIR);
     core.setOutput("version", SDL_VERSION.toString());
@@ -389,7 +398,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
     return result;
 };
 Object.defineProperty(exports, "__esModule", ({ value: true }));
-exports.get_platform_root_directory = exports.get_sdl_build_platform = exports.SdlBuildPlatform = void 0;
+exports.export_environent_variables = exports.get_platform_root_directory = exports.get_sdl_build_platform = exports.SdlBuildPlatform = void 0;
 const os = __importStar(__nccwpck_require__(2037));
 const core = __importStar(__nccwpck_require__(2186));
 const util_1 = __nccwpck_require__(9731);
@@ -425,6 +434,30 @@ function get_platform_root_directory(platform) {
     }
 }
 exports.get_platform_root_directory = get_platform_root_directory;
+function export_environent_variables(platform, prefix) {
+    switch (platform) {
+        case SdlBuildPlatform.Windows:
+            core.addPath(`${prefix}/bin`);
+            break;
+        case SdlBuildPlatform.Macos: {
+            let libpath = process.env.DYLD_LIBRARY_PATH;
+            if (libpath) {
+                libpath = "`${prefix}`/lib:${libpath}";
+            }
+            core.exportVariable("DYLD_LIBRARY_PATH", libpath);
+            break;
+        }
+        case SdlBuildPlatform.Linux: {
+            let libpath = process.env.LD_LIBRARY_PATH;
+            if (libpath) {
+                libpath = "`${prefix}`/lib:${libpath}";
+            }
+            core.exportVariable("LD_LIBRARY_PATH", libpath);
+            break;
+        }
+    }
+}
+exports.export_environent_variables = export_environent_variables;
 
 
 /***/ }),
diff --git a/src/main.ts b/src/main.ts
index 1109e11..dc303ac 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -18,6 +18,7 @@ import {
 } from "./version";
 
 import {
+  export_environent_variables,
   get_sdl_build_platform,
   get_platform_root_directory,
   SdlBuildPlatform,
@@ -92,10 +93,12 @@ async function cmake_configure_build(args: {
   build_dir: string;
   package_dir: string;
   build_type: string;
-  cmake_args: string;
+  cmake_args: string[];
   shell: string;
 }) {
-  const configure_command = `cmake -S "${args.source_dir}" -B "${args.build_dir}" ${args.cmake_args}`;
+  const cmake_args = args.cmake_args.join(" ");
+
+  const configure_command = `cmake -S "${args.source_dir}" -B "${args.build_dir}" ${cmake_args}`;
   const build_command = `cmake --build "${args.build_dir}" --config ${args.build_type}`;
   const install_command = `cmake --install "${args.build_dir}" --prefix ${args.package_dir} --config ${args.build_type}`;
 
@@ -259,9 +262,14 @@ async function run() {
       });
     }
 
-    let cmake_args = `-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}`;
+    const cmake_args = [
+      `-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}`,
+      "-DCMAKE_INSTALL_BINDIR=bin",
+      "-DCMAKE_INSTALL_INCLUDEDIR=include",
+      "-DCMAKE_INSTALL_LIBDIR=lib",
+    ];
     if (USE_NINJA) {
-      cmake_args += " -GNinja";
+      cmake_args.push("-GNinja");
     }
 
     await cmake_configure_build({
@@ -282,6 +290,10 @@ async function run() {
     SdlVersion.detect_sdl_version_from_install_prefix(PACKAGE_DIR);
   core.info(`SDL version is ${SDL_VERSION.toString()}`);
 
+  if (core.getBooleanInput("add-to-environment")) {
+    export_environent_variables(SDL_BUILD_PLATFORM, PACKAGE_DIR);
+  }
+
   core.exportVariable(`SDL${SDL_VERSION.major}_ROOT`, PACKAGE_DIR);
   core.setOutput("prefix", PACKAGE_DIR);
   core.setOutput("version", SDL_VERSION.toString());
diff --git a/src/platform.ts b/src/platform.ts
index 0432a29..93df82a 100644
--- a/src/platform.ts
+++ b/src/platform.ts
@@ -36,3 +36,30 @@ export function get_platform_root_directory(
       return `${os.tmpdir()}/setupsdl`;
   }
 }
+
+export function export_environent_variables(
+  platform: SdlBuildPlatform,
+  prefix: string
+) {
+  switch (platform) {
+    case SdlBuildPlatform.Windows:
+      core.addPath(`${prefix}/bin`);
+      break;
+    case SdlBuildPlatform.Macos: {
+      let libpath = process.env.DYLD_LIBRARY_PATH;
+      if (libpath) {
+        libpath = "`${prefix}`/lib:${libpath}";
+      }
+      core.exportVariable("DYLD_LIBRARY_PATH", libpath);
+      break;
+    }
+    case SdlBuildPlatform.Linux: {
+      let libpath = process.env.LD_LIBRARY_PATH;
+      if (libpath) {
+        libpath = "`${prefix}`/lib:${libpath}";
+      }
+      core.exportVariable("LD_LIBRARY_PATH", libpath);
+      break;
+    }
+  }
+}