Skip to content

Commit 5b6aca5

Browse files
authored
Remove GCC and Clang toolchains from being found by default on windows (#175)
1 parent c779be8 commit 5b6aca5

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

ToolchainPlugin/src/main/java/edu/wpi/first/toolchain/ToolchainExtension.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ public void withCrossLinuxArm64() {
107107
}
108108
}
109109

110+
private boolean removeInvalidWindowsToolchains = true;
111+
112+
public void setRemoveInvalidWindowsToolchains(boolean remove) {
113+
this.removeInvalidWindowsToolchains = remove;
114+
}
115+
116+
public boolean isRemoveInvalidWindowsToolchains() {
117+
return this.removeInvalidWindowsToolchains;
118+
}
119+
110120
public NamedDomainObjectContainer<ToolchainDescriptorBase> getToolchainDescriptors() {
111121
return toolchainDescriptors;
112122
}

ToolchainPlugin/src/main/java/edu/wpi/first/toolchain/ToolchainRules.java

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package edu.wpi.first.toolchain;
22

3+
import java.util.ArrayList;
34
import java.util.HashMap;
45
import java.util.List;
56
import java.util.Map;
@@ -28,6 +29,8 @@
2829
import org.gradle.nativeplatform.toolchain.NativeToolChain;
2930
import org.gradle.nativeplatform.toolchain.NativeToolChainRegistry;
3031
import org.gradle.nativeplatform.toolchain.internal.NativeToolChainRegistryInternal;
32+
import org.gradle.nativeplatform.toolchain.internal.clang.ClangToolChain;
33+
import org.gradle.nativeplatform.toolchain.internal.gcc.GccToolChain;
3134
import org.gradle.platform.base.BinaryContainer;
3235
import org.gradle.platform.base.BinarySpec;
3336
import org.gradle.platform.base.BinaryTasks;
@@ -42,17 +45,31 @@ public class ToolchainRules extends RuleSource {
4245
private static final ETLogger logger = ETLoggerFactory.INSTANCE.create("ToolchainRules");
4346

4447
@Finalize
45-
void addClangArm(NativeToolChainRegistryInternal toolChainRegistry) {
48+
void addClangArm(NativeToolChainRegistryInternal toolChainRegistry, ExtensionContainer extContainer) {
49+
// To work around sometimes GCC and Clang getting picked up on Windows, remove
50+
// them completely
51+
List<Object> toRemove = new ArrayList<>();
4652
toolChainRegistry.all(n -> {
53+
if (OperatingSystem.current().equals(OperatingSystem.WINDOWS)) {
54+
if (n instanceof Gcc && n.getName().equals(GccToolChain.DEFAULT_NAME)) {
55+
toRemove.add(n);
56+
}
57+
if (n instanceof Clang && n.getName().equals(ClangToolChain.DEFAULT_NAME)) {
58+
toRemove.add(n);
59+
}
60+
}
61+
4762
if (n instanceof Gcc && OperatingSystem.current().equals(OperatingSystem.LINUX)) {
48-
Gcc gcc = (Gcc)n;
49-
if ((NativePlatforms.desktop.equals(NativePlatforms.linuxarm32) || NativePlatforms.desktop.equals(NativePlatforms.linuxarm64)) && gcc.getName().equals("gcc")) {
63+
Gcc gcc = (Gcc) n;
64+
if ((NativePlatforms.desktop.equals(NativePlatforms.linuxarm32)
65+
|| NativePlatforms.desktop.equals(NativePlatforms.linuxarm64))
66+
&& gcc.getName().equals(GccToolChain.DEFAULT_NAME)) {
5067
gcc.setTargets();
5168
gcc.target(NativePlatforms.desktop);
5269
}
5370
}
5471
if (n instanceof Clang && OperatingSystem.current().equals(OperatingSystem.MAC_OS)) {
55-
Clang gcc = (Clang)n;
72+
Clang gcc = (Clang) n;
5673
gcc.setTargets();
5774
gcc.target("osxuniversal", gccToolChain -> {
5875
Action<List<String>> m64args = new Action<List<String>>() {
@@ -73,6 +90,13 @@ public void execute(List<String> args) {
7390
});
7491
}
7592
});
93+
94+
final ToolchainExtension ext = extContainer.getByType(ToolchainExtension.class);
95+
if (ext.isRemoveInvalidWindowsToolchains()) {
96+
for (var t : toRemove) {
97+
toolChainRegistry.remove(t);
98+
}
99+
}
76100
}
77101

78102
@Defaults
@@ -113,7 +137,6 @@ void addDefaultToolchains(NativeToolChainRegistryInternal toolChainRegistry,
113137
});
114138
});
115139

116-
117140
}
118141

119142
@Mutate
@@ -181,7 +204,8 @@ void addDefaultPlatforms(final ExtensionContainer extContainer, final PlatformCo
181204
}
182205

183206
@Validate
184-
void checkEnabledToolchains(final BinaryContainer binaries, final NativeToolChainRegistry toolChains, final ExtensionContainer extContainer) {
207+
void checkEnabledToolchains(final BinaryContainer binaries, final NativeToolChainRegistry toolChains,
208+
final ExtensionContainer extContainer) {
185209
final ToolchainExtension ext = extContainer.getByType(ToolchainExtension.class);
186210
// Map of platform to toolchains
187211
Map<String, GccExtension> gccToolChains = new HashMap<>();
@@ -205,8 +229,9 @@ void checkEnabledToolchains(final BinaryContainer binaries, final NativeToolChai
205229
}
206230
}
207231

208-
public static OrderedStripTask configureOrderedStrip(AbstractLinkTask link, GccExtension gcc, NativeBinarySpec binary) {
209-
ExtensionAware linkExt = (ExtensionAware)link;
232+
public static OrderedStripTask configureOrderedStrip(AbstractLinkTask link, GccExtension gcc,
233+
NativeBinarySpec binary) {
234+
ExtensionAware linkExt = (ExtensionAware) link;
210235
OrderedStripTask strip = linkExt.getExtensions().findByType(OrderedStripTask.class);
211236
if (strip == null) {
212237
Project project = link.getProject();
@@ -216,14 +241,16 @@ public static OrderedStripTask configureOrderedStrip(AbstractLinkTask link, GccE
216241
return null;
217242
}
218243

219-
strip = linkExt.getExtensions().create("orderedStrip", OrderedStripTask.class, tcExt, binary, link, gcc, project);
244+
strip = linkExt.getExtensions().create("orderedStrip", OrderedStripTask.class, tcExt, binary, link, gcc,
245+
project);
220246
link.doLast(strip);
221247
}
222248
return strip;
223249
}
224250

225251
@BinaryTasks
226-
void createNativeStripTasks(final ModelMap<Task> tasks, NativeBinarySpec binary, final ExtensionContainer extContainer) {
252+
void createNativeStripTasks(final ModelMap<Task> tasks, NativeBinarySpec binary,
253+
final ExtensionContainer extContainer) {
227254
final ToolchainExtension ext = extContainer.getByType(ToolchainExtension.class);
228255
NativeToolChain tc = binary.getToolChain();
229256
GccExtension gccExt = ext.getGccExtensionMap().getOrDefault(tc, null);

src/main/java/edu/wpi/first/nativeutils/NativeUtilsExtension.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,12 @@ public boolean isSkipInstallPdb() {
388388
public void setSkipInstallPdb(boolean skip) {
389389
skipInstallPdb = skip;
390390
}
391+
392+
public void setRemoveInvalidWindowsToolchains(boolean remove) {
393+
tcExt.setRemoveInvalidWindowsToolchains(remove);
394+
}
395+
396+
public boolean isRemoveInvalidWindowsToolchains() {
397+
return tcExt.isRemoveInvalidWindowsToolchains();
398+
}
391399
}

0 commit comments

Comments
 (0)