Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 28905b02c97a24fb06bbbf391f4d7ba81deca2df (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
/*******************************************************************************
 * 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.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.cdt.build.gcc.core.GCCToolChain;
import org.eclipse.cdt.core.build.IToolChainManager;
import org.eclipse.cdt.core.build.IToolChainProvider;

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

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

	private static final Pattern gccPattern = Pattern.compile("(.*-)?(gcc|g\\+\\+|clang|clang\\+\\+)"); //$NON-NLS-1$
	private static final Pattern versionPattern = Pattern.compile(".*(gcc|LLVM) version .*"); //$NON-NLS-1$
	private static final Pattern targetPattern = Pattern.compile("Target: (.*)"); //$NON-NLS-1$

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

	@Override
	public void init(IToolChainManager manager) {
		Set<String> names = new HashSet<>();

		String path = System.getenv("PATH"); //$NON-NLS-1$
		for (String dirStr : path.split(File.pathSeparator)) {
			File dir = new File(dirStr);
			if (dir.isDirectory()) {
				for (String file : dir.list()) {
					Matcher matcher = gccPattern.matcher(file);
					if (matcher.matches()) {
						String prefix = matcher.group(1);
						String command = dirStr + File.separatorChar + file;
						try {
							Process proc = new ProcessBuilder(new String[] { command, "-v" }).redirectErrorStream(true) //$NON-NLS-1$
									.start();
							String version = null;
							String target = null;
							try (BufferedReader reader = new BufferedReader(
									new InputStreamReader(proc.getInputStream()))) {
								for (String line = reader.readLine(); line != null; line = reader.readLine()) {
									Matcher versionMatcher = versionPattern.matcher(line);
									if (versionMatcher.matches()) {
										version = line.trim();
										continue;
									}
									Matcher targetMatcher = targetPattern.matcher(line);
									if (targetMatcher.matches()) {
										target = targetMatcher.group(1);
										continue;
									}
								}
							}
							if (target != null && version != null) {
								String name = target + " - " + version; //$NON-NLS-1$
								if (!names.contains(name)) {
									names.add(name);
									manager.addToolChain(new GCCToolChain(this, target, version,
											new Path[] { dir.toPath() }, prefix));
								}
							}
						} catch (IOException e) {
							Activator.log(e);
						}
					}
				}
			}
		}
	}

}

Back to the top