Skip to content

Commit a03e303

Browse files
committed
Implement Homebrew toolchain provider for core build
1 parent 9d4243f commit a03e303

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

build/org.eclipse.cdt.build.gcc.core/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Bundle-Activator: org.eclipse.cdt.build.gcc.core.internal.Activator
77
Bundle-Vendor: %providerName
88
Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.33.0,4)",
99
org.eclipse.core.resources;bundle-version="[3.22.200,4)",
10-
org.eclipse.cdt.core;bundle-version="[9.1.0,10)",
10+
org.eclipse.cdt.core;bundle-version="[9.2.0,10)",
1111
com.google.gson;bundle-version="[2.13.1,3)"
1212
Bundle-RequiredExecutionEnvironment: JavaSE-17
1313
Bundle-ActivationPolicy: lazy

build/org.eclipse.cdt.build.gcc.core/plugin.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
class="org.eclipse.cdt.build.gcc.core.internal.GCCPathToolChainProvider"
88
id="org.eclipse.cdt.build.gcc.core.gccPathProvider">
99
</provider>
10+
<provider
11+
class="org.eclipse.cdt.build.gcc.core.internal.HomebrewToolChainProvider"
12+
id="org.eclipse.cdt.build.gcc.core.homebrewProvider">
13+
</provider>
1014
<provider
1115
class="org.eclipse.cdt.build.gcc.core.internal.Msys2ToolChainProvider"
1216
id="org.eclipse.cdt.build.gcc.core.msys2Provider">
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2016, 2025 QNX Software Systems and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
* Contributors:
11+
* QNX Software Systems - MinGW implementation
12+
* John Dallaway - Initial Homebrew implementation (#1175)
13+
*******************************************************************************/
14+
package org.eclipse.cdt.build.gcc.core.internal;
15+
16+
import java.io.File;
17+
import java.nio.file.Path;
18+
import java.util.regex.Pattern;
19+
20+
import org.eclipse.cdt.build.gcc.core.ClangToolChain;
21+
import org.eclipse.cdt.build.gcc.core.GCCToolChain;
22+
import org.eclipse.cdt.core.build.IToolChain;
23+
import org.eclipse.cdt.core.build.IToolChainManager;
24+
import org.eclipse.cdt.core.build.IToolChainProvider;
25+
import org.eclipse.cdt.core.envvar.EnvironmentVariable;
26+
import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
27+
import org.eclipse.cdt.internal.core.Homebrew;
28+
import org.eclipse.core.runtime.Platform;
29+
30+
public class HomebrewToolChainProvider implements IToolChainProvider {
31+
32+
private static final String ID = "org.eclipse.cdt.build.gcc.core.homebrewProvider"; //$NON-NLS-1$
33+
private static final Pattern CLANG_PATTERN = Pattern.compile("clang-\\d+"); //$NON-NLS-1$
34+
private static final Pattern GCC_PATTERN = Pattern.compile("gcc-\\d+"); //$NON-NLS-1$
35+
private static final String ENV_PATH = "PATH"; //$NON-NLS-1$
36+
private static final String HOMEBREW_PACKAGE = "homebrew"; //$NON-NLS-1$
37+
38+
@Override
39+
public String getId() {
40+
return ID;
41+
}
42+
43+
@Override
44+
public void init(IToolChainManager manager) {
45+
final String homebrewHome = Homebrew.getHomebrewHome();
46+
if (null != homebrewHome) {
47+
Path homebrewPath = new File(homebrewHome).toPath();
48+
Path homebrewBinPath = homebrewPath.resolve("bin"); //$NON-NLS-1$
49+
Path homebrewLlvmBinPath = homebrewPath.resolve("opt/llvm/bin"); //$NON-NLS-1$
50+
for (File clangFile : getFiles(homebrewLlvmBinPath, CLANG_PATTERN)) {
51+
IEnvironmentVariable[] vars = createEnvironmentVariables(homebrewLlvmBinPath);
52+
IToolChain toolChain = new ClangToolChain(this, clangFile.toPath(), Platform.getOSArch(), vars);
53+
toolChain.setProperty(IToolChain.ATTR_PACKAGE, HOMEBREW_PACKAGE);
54+
manager.addToolChain(toolChain);
55+
}
56+
for (File gccFile : getFiles(homebrewBinPath, GCC_PATTERN)) {
57+
IEnvironmentVariable[] vars = createEnvironmentVariables(homebrewBinPath);
58+
IToolChain toolChain = new GCCToolChain(this, gccFile.toPath(), Platform.getOSArch(), vars);
59+
toolChain.setProperty(IToolChain.ATTR_PACKAGE, HOMEBREW_PACKAGE);
60+
manager.addToolChain(toolChain);
61+
}
62+
}
63+
}
64+
65+
private File[] getFiles(Path path, Pattern filePattern) {
66+
File dir = path.toFile();
67+
if (dir.isDirectory()) {
68+
return dir.listFiles(file -> file.isFile() && filePattern.matcher(file.getName()).matches());
69+
}
70+
return new File[0];
71+
}
72+
73+
private IEnvironmentVariable[] createEnvironmentVariables(Path path) {
74+
EnvironmentVariable pathVariable = new EnvironmentVariable(ENV_PATH, path.toString(),
75+
IEnvironmentVariable.ENVVAR_PREPEND, File.pathSeparator);
76+
return new IEnvironmentVariable[] { pathVariable };
77+
}
78+
79+
}

0 commit comments

Comments
 (0)