Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dae4f2df0d5c6e55d3c50c0a9e5abee03eba0857 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*******************************************************************************
 * Copyright (c) 2015, 2016 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.cdt.build.gcc.core.internal;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.cdt.build.gcc.core.ClangToolChain;
import org.eclipse.cdt.build.gcc.core.GCCToolChain;
import org.eclipse.cdt.build.gcc.core.GCCToolChain.GCCInfo;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.build.IToolChain;
import org.eclipse.cdt.core.build.IToolChainManager;
import org.eclipse.cdt.core.build.IToolChainProvider;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;

/**
 * Finds gcc and clang on the path.
 */
public class GCCPathToolChainProvider implements IToolChainProvider {

	public static final String ID = "org.eclipse.cdt.build.gcc.core.gccPathProvider"; //$NON-NLS-1$

	private static final Pattern gccPattern = Pattern.compile("(.*-)?gcc(\\.exe)?"); //$NON-NLS-1$
	private static final Pattern clangPattern = Pattern.compile("clang(\\.exe)?"); //$NON-NLS-1$

	@Override
	public String getId() {
		return ID;
	}

	@Override
	public void init(IToolChainManager manager) {
		String path = System.getenv("PATH"); //$NON-NLS-1$
		for (String dirStr : path.split(File.pathSeparator)) {
			File dir = new File(dirStr);
			if (dir.isDirectory()) {
				for (File file : dir.listFiles()) {
					if (file.isDirectory()) {
						continue;
					}
					Matcher matcher = gccPattern.matcher(file.getName());
					if (matcher.matches()) {
						try {
							GCCInfo info = new GCCInfo(file.toString());
							if (info.target != null && info.version != null) {
								String[] tuple = info.target.split("-"); //$NON-NLS-1$
								if (tuple.length > 2) {
									GCCToolChain gcc = new GCCToolChain(this, file.toPath(), tuple[0], null);

									// OS
									switch (tuple[1]) {
									case "w64": //$NON-NLS-1$
										gcc.setProperty(IToolChain.ATTR_OS, Platform.OS_WIN32);
										break;
									case "linux": //$NON-NLS-1$
										gcc.setProperty(IToolChain.ATTR_OS, Platform.OS_LINUX);
										break;
									case "apple": //$NON-NLS-1$
										gcc.setProperty(IToolChain.ATTR_OS, Platform.OS_MACOSX);
										break;
									}
									try {
										if (manager.getToolChain(gcc.getTypeId(), gcc.getId()) == null) {
											// Only add if another provider hasn't already added it
											manager.addToolChain(gcc);
										}
									} catch (CoreException e) {
										CCorePlugin.log(e.getStatus());
									}
								}
							}
						} catch (IOException e) {
							Activator.log(e);
						}
					} else {
						matcher = clangPattern.matcher(file.getName());
						if (matcher.matches()) {
							// TODO only support host clang for now, need to figure out multi
							ClangToolChain clang = new ClangToolChain(this, file.toPath(), Platform.getOSArch(), null);
							clang.setProperty(IToolChain.ATTR_OS, Platform.getOS());
							manager.addToolChain(clang);
						}
					}
				}
			}
		}
	}

}

Back to the top