Correct cherry picking of emulator-base's build configuration; Allow blank string...
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / general / cmake / CMakeBuildTask.java
blob8bb6249fb48e73eb3bf2201df7da9773da4635dd
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 java.io.IOException;
13 import java.nio.file.Files;
14 import java.nio.file.Path;
15 import java.nio.file.Paths;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.Collections;
19 import java.util.List;
20 import javax.inject.Inject;
21 import org.gradle.api.DefaultTask;
22 import org.gradle.util.internal.VersionNumber;
24 /**
25 * Task for building CMake projects.
27 * @since 2024/03/15
29 public class CMakeBuildTask
30 extends DefaultTask
32 /** The CMake project root. */
33 public final Path cmakeSource;
35 /** CMake build path, where output goes. */
36 public final Path cmakeBuild;
38 /** The output file we want. */
39 public final Path cmakeOutFile;
41 /** The rule to use. */
42 public final List<String> cmakeRules;
44 /**
45 * Initializes the build task.
47 * @param __source The project source directory.
48 * @param __outputFile The output file we want, this is optional.
49 * @param __rules The rules to execute.
50 * @since 2024/03/15
52 @Inject
53 public CMakeBuildTask(Path __source, String __outputFile,
54 List<String> __rules)
55 throws NullPointerException
57 if (__source == null || __rules == null || __rules.isEmpty())
58 throw new NullPointerException("NARG");
60 __rules = new ArrayList<>(__rules);
61 for (String rule : __rules)
62 if (rule == null)
63 throw new NullPointerException("NARG");
65 // Blank?
66 if (__outputFile.isEmpty())
67 __outputFile = null;
69 // Set source for later
70 this.cmakeSource = __source;
71 this.cmakeRules = Collections.<String>unmodifiableList(__rules);
73 // The build root is based on the task
74 Path cmakeBuild = this.getProject().getBuildDir().toPath()
75 .resolve("cmake-" + this.getName());
76 this.cmakeBuild = cmakeBuild;
78 // The desired output we want
79 if (__outputFile == null)
80 this.cmakeOutFile = null;
81 else
82 this.cmakeOutFile = this.cmakeBuild.resolve(
83 Paths.get(__outputFile));
85 // Description
86 this.setGroup("squirreljme");
87 this.setDescription("Performs a CMake build.");
89 // Must be run _after_ clean, otherwise bad stuff happens
90 this.mustRunAfter(
91 this.getProject().getTasks().named("clean"));
93 // Build action
94 CMakeBuildTaskAction action = new CMakeBuildTaskAction();
96 // Need to get some details about the CMake build
97 try
99 // Configure CMake first before we continue with anything
100 CMakeUtils.configure(this);
102 catch (IOException __e)
104 throw new RuntimeException(String.format(
105 "Could not configure CMake for task %s", this.getName()), __e);
108 // Check if out of date
109 this.getOutputs().upToDateWhen(new CMakeUpToDateWhen());
111 // At the minimum we know the base input and outputs
112 this.getInputs().dir(this.cmakeSource);
113 this.getInputs().files(
114 this.cmakeSource.resolve("CMakeLists.txt"));
115 this.getOutputs().dirs(this.cmakeBuild);
116 this.getOutputs().files(
117 this.cmakeBuild.resolve("CMakeCache.txt"));
118 if (__outputFile != null)
119 this.getOutputs().files(this.cmakeOutFile);
121 // What to do for this
122 this.doFirst(action);