|
| 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