Make CMake's output executable optional.
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / general / cmake / CMakeUtils.java
blob50299ec45c2953980509110a88a9628ab72b5c51
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // Multi-Phasic Applications: SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.plugin.general.cmake;
12 import cc.squirreljme.plugin.util.PathUtils;
13 import java.io.IOException;
14 import java.nio.file.Files;
15 import java.nio.file.Path;
16 import java.nio.file.Paths;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21 import org.gradle.api.logging.LogLevel;
22 import org.gradle.api.logging.Logger;
23 import org.gradle.internal.os.OperatingSystem;
25 /**
26 * Utilities for CMake.
28 * @since 2024/03/15
30 public final class CMakeUtils
32 /**
33 * Not used.
35 * @since 2024/03/15
37 private CMakeUtils()
41 /**
42 * Returns the path where CMake is available.
44 * @return The CMake path.
45 * @since 2024/03/15
47 public static Path cmakeExePath()
49 // Standard executable?
50 Path cmakePath = PathUtils.findPath("cmake");
52 // Windows executable?
53 if (cmakePath == null)
54 cmakePath = PathUtils.findPath("cmake.exe");
56 // Standard installation on Windows?
57 if (OperatingSystem.current() == OperatingSystem.WINDOWS)
59 String programFiles = System.getenv("PROGRAMFILES");
60 if (programFiles != null)
62 Path maybe = Paths.get(programFiles).resolve("CMake")
63 .resolve("bin").resolve("cmake.exe");
64 if (Files.exists(maybe))
65 return maybe;
69 return cmakePath;
72 /**
73 * Executes a CMake task.
75 * @param __logger The logger to use.
76 * @param __logName The name of the log.
77 * @param __cmakeBuild The CMake build directory.
78 * @param __args CMake arguments.
79 * @throws IOException On read/write or execution errors.
80 * @since 2024/03/15
82 public static void cmakeExecute(Logger __logger, String __logName,
83 Path __cmakeBuild, String... __args)
84 throws IOException
86 // Need CMake
87 Path cmakePath = CMakeUtils.cmakeExePath();
88 if (cmakePath == null)
89 throw new RuntimeException("CMake not found.");
91 // Determine run arguments
92 List<String> args = new ArrayList<>();
93 args.add(cmakePath.toAbsolutePath().toString());
94 args.addAll(Arrays.asList(__args));
96 // Set executable process
97 ProcessBuilder procBuilder = new ProcessBuilder();
98 procBuilder.command(args);
100 // Output log files
101 Path outLog = __cmakeBuild.resolve(__logName + ".out");
102 Path errLog = __cmakeBuild.resolve(__logName + ".err");
104 // Log the output somewhere
105 procBuilder.redirectOutput(outLog.toFile());
106 procBuilder.redirectError(errLog.toFile());
108 // Start the process
109 Process proc = procBuilder.start();
111 // Wait for it to complete
114 if (!proc.waitFor(5, TimeUnit.MINUTES) ||
115 proc.exitValue() != 0)
116 throw new RuntimeException(String.format(
117 "CMake failed to %s...", __logName));
119 catch (InterruptedException __e)
121 throw new RuntimeException("CMake timed out!", __e);
123 finally
125 CMakeUtils.dumpLog(__logger,
126 LogLevel.LIFECYCLE, outLog);
127 CMakeUtils.dumpLog(__logger,
128 LogLevel.ERROR, errLog);
133 * Dumps log to the output.
135 * @param __logger The logger to output to.
136 * @param __level The log level.
137 * @param __pathy The output path.
138 * @since 2024/03/15
140 public static void dumpLog(Logger __logger, LogLevel __level, Path __pathy)
144 for (String ln : Files.readAllLines(__pathy))
145 __logger.log(__level, ln);
147 catch (Throwable e)
149 e.printStackTrace();