1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-30 20:35:38 +02:00

applied patch from bug #225777.

This commit is contained in:
Warren Paul 2008-04-08 21:50:33 +00:00
parent 24cc1a7822
commit 1ca857981f
10 changed files with 376 additions and 32 deletions

View file

@ -16,3 +16,4 @@ Require-Bundle: org.eclipse.core.runtime,
Bundle-ActivationPolicy: lazy
Bundle-Vendor: Eclipse.org
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Import-Package: org.eclipse.core.filesystem

View file

@ -39,6 +39,7 @@ public class AutomatedIntegrationSuite extends TestSuite {
suite.addTest(StandardBuildTests.suite());
suite.addTest(ScannerDiscoveryTests.suite());
suite.addTest(MakefileReaderProviderTests.suite());
return suite;
}

View file

@ -0,0 +1,169 @@
/**
* (c) 2008 Nokia
* 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
*
* Contributors:
* Ed Swartz (NOKIA) - initial API and implementation
*/
package org.eclipse.cdt.make.core.tests;
import org.eclipse.cdt.make.core.MakeCorePlugin;
import org.eclipse.cdt.make.core.makefile.*;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.runtime.*;
import java.io.*;
import java.net.*;
import junit.framework.Test;
import junit.framework.TestCase;
public class MakefileReaderProviderTests extends TestCase {
private String[] inclDirs;
/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
@Override
protected void setUp() throws Exception {
super.setUp();
String basePath = "";
File baseDir = getPluginRelativeFile(new Path("data"));
if (baseDir != null)
basePath = baseDir.getAbsolutePath() + File.separator;
inclDirs = new String[] {
basePath + "bogus",
basePath + "incl"
};
}
public void testNoReaderProvider() throws Exception {
IPath path = new Path("data/Makefile.main");
File file = getPluginRelativeFile(path);
// doesn't work in packaged plugin, which is fine
if (file != null) {
IMakefile makefile = MakeCorePlugin.createMakefile(
URIUtil.toURI(file.getAbsolutePath()),
true, inclDirs);
assertMakefileContents(makefile);
}
}
public void testNullReaderProvider() throws Exception {
IPath path = new Path("data/Makefile.main");
File file = getPluginRelativeFile(path);
// doesn't work in packaged plugin, which is fine
if (file != null) {
IMakefile makefile = MakeCorePlugin.createMakefile(
URIUtil.toURI(file.getAbsolutePath()), true, inclDirs, null);
assertMakefileContents(makefile);
}
}
public void testInputStreamReaderProvider() throws Exception {
IPath path = new Path("Makefile.main");
// get base directory for searches
final URL url = getPluginRelativeURL(new Path("data").addTrailingSeparator());
IMakefile makefile = MakeCorePlugin.createMakefile(
URIUtil.toURI(path), true, inclDirs,
new IMakefileReaderProvider() {
public Reader getReader(URI fileURI) throws IOException {
URL fileUrl;
try {
fileUrl = new URL(url, fileURI.getPath());
} catch (MalformedURLException e) {
fileUrl = new URL("file", null, fileURI.getPath());
}
InputStream is = fileUrl.openStream();
return new InputStreamReader(is);
}
});
assertMakefileContents(makefile);
}
public void testInMemoryReaderProvider() throws Exception {
IMakefile makefile = MakeCorePlugin.createMakefile(
URIUtil.toURI("Makefile.main"), true, inclDirs,
new IMakefileReaderProvider() {
public Reader getReader(URI fileURI) throws IOException {
String name = new File(fileURI).getName();
if (name.equals("Makefile.main"))
return new StringReader(
"VAR = foo\r\n" +
"\r\n" +
"include Makefile.incl\r\n" +
"\r\n" +
"main: $(VAR)\r\n" +
" nothing\r\n");
if (name.equals("Makefile.incl"))
return new StringReader(
"INCLVAR = bar\r\n" +
"\r\n" +
"foo.o: .PHONY\r\n"
);
throw new FileNotFoundException(fileURI.getPath());
}
});
assertMakefileContents(makefile);
}
/**
* @param makefile
*/
private void assertMakefileContents(IMakefile makefile) {
assertNotNull(makefile);
IMacroDefinition[] macroDefinitions = makefile.getMacroDefinitions();
assertNotNull(macroDefinitions);
assertEquals(2, macroDefinitions.length);
assertEquals("VAR", macroDefinitions[0].getName());
assertEquals("INCLVAR", macroDefinitions[1].getName());
IRule[] rules = makefile.getRules();
assertEquals(2, rules.length);
assertEquals("main", rules[0].getTarget().toString());
assertEquals("foo.o", rules[1].getTarget().toString());
}
/**
* Try to get a file in the development version of a plugin --
* will return <code>null</code> for a jar-packaged plugin.
* @param path
* @return
* @throws Exception
*/
private File getPluginRelativeFile(IPath path) throws Exception {
URL url = getPluginRelativeURL(path);
assertNotNull(url);
if (url.getProtocol().equals("file"))
return new File(url.getPath());
return null;
}
private URL getPluginRelativeURL(IPath path) throws Exception {
if (MakeTestsPlugin.getDefault() != null)
return FileLocator.find(
MakeTestsPlugin.getDefault().getBundle(),
path, null);
else {
return new URL("file", null, path.toFile().getAbsolutePath());
}
}
/**
* @return
*/
public static Test suite() {
return new MakefileReaderProviderTests();
}
}

View file

@ -14,7 +14,6 @@ package org.eclipse.cdt.make.core;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.ArrayList;
@ -24,6 +23,7 @@ import java.util.Map;
import java.util.StringTokenizer;
import org.eclipse.cdt.make.core.makefile.IMakefile;
import org.eclipse.cdt.make.core.makefile.IMakefileReaderProvider;
import org.eclipse.cdt.make.core.scannerconfig.IDiscoveredPathManager;
import org.eclipse.cdt.make.core.scannerconfig.IExternalScannerInfoProvider;
import org.eclipse.cdt.make.core.scannerconfig.IScannerConfigBuilderInfo;
@ -187,8 +187,22 @@ public class MakeCorePlugin extends Plugin {
}
static public IMakefile createMakefile(IFileStore file, boolean isGnuStyle, String[] makefileDirs) throws CoreException {
return createMakefile(file.toURI(), isGnuStyle, makefileDirs, null);
}
/**
* Create an IMakefile using the given IMakefileReaderProvider to fetch
* contents by name.
* @param name URI of main file
* @param isGnuStyle
* @param makefileDirs
* @param makefileReaderProvider may be <code>null</code> for EFS IFileStore reading
* @return IMakefile
* @throws CoreException
*/
public static IMakefile createMakefile(URI fileURI,
boolean isGnuStyle, String[] makefileDirs, IMakefileReaderProvider makefileReaderProvider) {
IMakefile makefile;
URI fileURI = file.toURI();
if (isGnuStyle) {
GNUMakefile gnu = new GNUMakefile();
ArrayList includeList = new ArrayList();
@ -198,14 +212,14 @@ public class MakeCorePlugin extends Plugin {
String[] includes = (String[]) includeList.toArray(new String[includeList.size()]);
gnu.setIncludeDirectories(includes);
try {
gnu.parse(fileURI, new InputStreamReader(file.openInputStream(EFS.NONE, null)));
gnu.parse(fileURI, makefileReaderProvider);
} catch (IOException e) {
}
makefile = gnu;
} else {
PosixMakefile posix = new PosixMakefile();
try {
posix.parse(fileURI, new InputStreamReader(file.openInputStream(EFS.NONE, null)));
posix.parse(fileURI, makefileReaderProvider);
} catch (IOException e) {
}
makefile = posix;
@ -213,6 +227,19 @@ public class MakeCorePlugin extends Plugin {
return makefile;
}
/**
* Create an IMakefile using EFS to fetch contents.
* @param name URI of main file
* @param isGnuStyle
* @param makefileDirs
* @return IMakefile
* @throws CoreException
*/
public static IMakefile createMakefile(URI fileURI,
boolean isGnuStyle, String[] makefileDirs) {
return createMakefile(fileURI, isGnuStyle, makefileDirs, null);
}
public IMakefile createMakefile(IFile file) throws CoreException {
return createMakefile(EFS.getStore(file.getLocationURI()), isMakefileGNUStyle(), getMakefileDirs());
}

View file

@ -124,6 +124,12 @@ public interface IMakefile extends IParent {
*/
String expandString(String line, boolean recursive);
/**
* Get the makefile Reader provider used to create this makefile.
* @return IMakefileReaderProvider or <code>null</code>
*/
IMakefileReaderProvider getMakefileReaderProvider();
/**
* Clear all statements and (re)parse the Makefile
*
@ -142,6 +148,17 @@ public interface IMakefile extends IParent {
*/
void parse(URI fileURI, Reader makefile) throws IOException;
/**
* Clear the all statements and (re)parse the Makefile
* using the given makefile Reader provider
*
* @param fileURI
* @param makefileReaderProvider provider, or <code>null</code> to use a FileReader
* @throws IOException
*/
void parse(URI fileURI, IMakefileReaderProvider makefileReaderProvider) throws IOException;
/**
* @return the <code>URI</code> of this makefile
*/

View file

@ -0,0 +1,31 @@
/**
* (c) 2008 Nokia
* 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
*
* Contributors:
* Ed Swartz (NOKIA) - Initial API and implementation
*/
package org.eclipse.cdt.make.core.makefile;
import java.io.IOException;
import java.io.Reader;
import java.net.URI;
/**
* Provide an abstraction to loading the contents of a makefile
* @author eswartz
*
*/
public interface IMakefileReaderProvider {
/**
* Get a reader for the contents of the file at filename.
* @param fileURI the file to read. It's up to the implementation how to read
* it, but usually EFS.getFileStore(fileURI).getInputStream(...) is the best bet.
* @return Reader a reader for the contents of the existing file
* @throws IOException if the file cannot be found according to the implementation
*/
Reader getReader(URI fileURI) throws IOException;
}

View file

@ -15,6 +15,7 @@ import java.io.Reader;
import java.net.URI;
import org.eclipse.cdt.make.core.makefile.IDirective;
import org.eclipse.cdt.make.core.makefile.IMakefileReaderProvider;
/**
* Makefile : ( statement ) *
@ -61,6 +62,26 @@ public class NullMakefile extends AbstractMakefile {
public void parse(String name, Reader makefile) throws IOException {
}
/* (non-Javadoc)
* @see org.eclipse.cdt.make.core.makefile.IMakefile#getMakefileReaderProvider()
*/
public IMakefileReaderProvider getMakefileReaderProvider() {
return null;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.make.core.makefile.IMakefile#parse(java.lang.String, org.eclipse.cdt.make.core.makefile.IMakefileReaderProvider)
*/
public void parse(String name,
IMakefileReaderProvider makefileReaderProvider) throws IOException {
}
public void parse(URI fileURI, Reader makefile) throws IOException {
}
/* (non-Javadoc)
* @see org.eclipse.cdt.make.core.makefile.IMakefile#parse(java.net.URI, org.eclipse.cdt.make.core.makefile.IMakefileReaderProvider)
*/
public void parse(URI fileURI,
IMakefileReaderProvider makefileReaderProvider) throws IOException {
}
}

View file

@ -27,6 +27,7 @@ import java.util.StringTokenizer;
import org.eclipse.cdt.make.core.MakeCorePlugin;
import org.eclipse.cdt.make.core.makefile.IDirective;
import org.eclipse.cdt.make.core.makefile.IMakefile;
import org.eclipse.cdt.make.core.makefile.IMakefileReaderProvider;
import org.eclipse.cdt.make.core.makefile.gnu.IGNUMakefile;
import org.eclipse.cdt.make.internal.core.makefile.AbstractMakefile;
import org.eclipse.cdt.make.internal.core.makefile.BadDirective;
@ -51,7 +52,9 @@ import org.eclipse.cdt.make.internal.core.makefile.Target;
import org.eclipse.cdt.make.internal.core.makefile.TargetRule;
import org.eclipse.cdt.make.internal.core.makefile.Util;
import org.eclipse.cdt.make.internal.core.makefile.posix.PosixMakefileUtil;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
@ -78,19 +81,49 @@ public class GNUMakefile extends AbstractMakefile implements IGNUMakefile {
String[] includeDirectories = new String[0];
IDirective[] builtins = null;
private IMakefileReaderProvider makefileReaderProvider;
public GNUMakefile() {
super(null);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.make.core.makefile.IMakefile#getMakefileReaderProvider()
*/
public IMakefileReaderProvider getMakefileReaderProvider() {
return makefileReaderProvider;
}
public void parse(String filePath, Reader reader) throws IOException {
parse(URIUtil.toURI(filePath), new MakefileReader(reader));
}
/* (non-Javadoc)
* @see org.eclipse.cdt.make.core.makefile.IMakefile#parse(java.net.URI, org.eclipse.cdt.make.core.makefile.IMakefileReaderProvider)
*/
public void parse(URI fileURI,
IMakefileReaderProvider makefileReaderProvider) throws IOException {
this.makefileReaderProvider = makefileReaderProvider;
MakefileReader reader;
if (makefileReaderProvider == null) {
try {
reader = new MakefileReader(new InputStreamReader(
EFS.getStore(fileURI).openInputStream(EFS.NONE, null)));
} catch (CoreException e) {
MakeCorePlugin.log(e);
throw new IOException(e.getMessage());
}
} else {
reader = new MakefileReader(makefileReaderProvider.getReader(fileURI));
}
parse(fileURI, reader);
}
public void parse(URI filePath, Reader reader) throws IOException {
parse(filePath, new MakefileReader(reader));
}
protected void parse(URI fileURI, MakefileReader reader) throws IOException {
String line;
Rule[] rules = null;
@ -787,22 +820,24 @@ public class GNUMakefile extends AbstractMakefile implements IGNUMakefile {
public IDirective[] getBuiltins() {
if (builtins == null) {
String location = "builtin" + File.separator + "gnu.mk"; //$NON-NLS-1$ //$NON-NLS-2$
try {
InputStream stream = FileLocator.openStream(MakeCorePlugin.getDefault().getBundle(), new Path(location), false);
GNUMakefile gnu = new GNUMakefile();
URL url = FileLocator.find(MakeCorePlugin.getDefault().getBundle(), new Path(location), null);
gnu.parse(url.toURI(), new InputStreamReader(stream));
builtins = gnu.getDirectives();
for (int i = 0; i < builtins.length; i++) {
if (builtins[i] instanceof MacroDefinition) {
((MacroDefinition) builtins[i]).setFromDefault(true);
if (MakeCorePlugin.getDefault() != null) {
try {
InputStream stream = FileLocator.openStream(MakeCorePlugin.getDefault().getBundle(), new Path(location), false);
GNUMakefile gnu = new GNUMakefile();
URL url = FileLocator.find(MakeCorePlugin.getDefault().getBundle(), new Path(location), null);
gnu.parse(url.toURI(), new InputStreamReader(stream));
builtins = gnu.getDirectives();
for (int i = 0; i < builtins.length; i++) {
if (builtins[i] instanceof MacroDefinition) {
((MacroDefinition) builtins[i]).setFromDefault(true);
}
}
}
} catch (IOException e) {
MakeCorePlugin.log(e);
} catch (URISyntaxException e) {
MakeCorePlugin.log(e);
}
} catch (IOException e) {
MakeCorePlugin.log(e);
} catch (URISyntaxException e) {
MakeCorePlugin.log(e);
}
}
if (builtins == null) {
builtins = new IDirective[0];
}

View file

@ -10,20 +10,17 @@
*******************************************************************************/
package org.eclipse.cdt.make.internal.core.makefile.gnu;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import org.eclipse.cdt.make.core.makefile.IDirective;
import org.eclipse.cdt.make.core.makefile.IMakefile;
import org.eclipse.cdt.make.core.makefile.IMakefileReaderProvider;
import org.eclipse.cdt.make.core.makefile.gnu.IInclude;
import org.eclipse.cdt.make.internal.core.makefile.Directive;
import org.eclipse.cdt.make.internal.core.makefile.Parent;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
@ -50,9 +47,24 @@ public class Include extends Parent implements IInclude {
return filenames;
}
private IMakefileReaderProvider getCurrentMakefileReaderProvider() {
IDirective directive = this;
while (directive != null) {
if (directive instanceof IMakefile) {
IMakefileReaderProvider makefileReaderProvider = ((IMakefile) directive).getMakefileReaderProvider();
if (makefileReaderProvider != null)
return makefileReaderProvider;
}
directive = directive.getParent();
}
return null;
}
public IDirective[] getDirectives() {
clearDirectives();
URI uri = getMakefile().getFileURI();
IMakefileReaderProvider makefileReaderProvider = getCurrentMakefileReaderProvider();
for (int i = 0; i < filenames.length; i++) {
IPath includeFilePath = new Path(filenames[i]);
if (includeFilePath.isAbsolute()) {
@ -65,8 +77,7 @@ public class Include extends Parent implements IInclude {
}
try {
GNUMakefile gnu = new GNUMakefile();
final InputStreamReader reader = new InputStreamReader(new FileInputStream(includeFilePath.toFile()));
gnu.parse(includeFilePath.toOSString(), reader);
gnu.parse(URIUtil.toURI(includeFilePath), makefileReaderProvider);
addDirective(gnu);
continue;
} catch (IOException e) {
@ -75,21 +86,19 @@ public class Include extends Parent implements IInclude {
} else if (dirs != null) {
for (int j = 0; j < dirs.length; j++) {
try {
includeFilePath= new Path(dirs[j]).append(includeFilePath);
String uriPath = includeFilePath.toString();
if (includeFilePath.getDevice() != null) {
IPath testIncludeFilePath= new Path(dirs[j]).append(includeFilePath);
String uriPath = testIncludeFilePath.toString();
if (testIncludeFilePath.getDevice() != null) {
// special case: device prefix is seen as relative path by URI
uriPath = '/' + uriPath;
}
GNUMakefile gnu = new GNUMakefile();
URI includeURI = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uriPath, null, null);
IFileStore store = EFS.getStore(includeURI);
gnu.parse(includeURI, new InputStreamReader(store.openInputStream(0, null)));
gnu.parse(includeURI, makefileReaderProvider);
addDirective(gnu);
break;
} catch (IOException e) {
} catch (URISyntaxException exc) {
} catch (CoreException exc) {
}
}
}

View file

@ -21,6 +21,7 @@ import java.net.URL;
import org.eclipse.cdt.make.core.MakeCorePlugin;
import org.eclipse.cdt.make.core.makefile.IDirective;
import org.eclipse.cdt.make.core.makefile.IMakefileReaderProvider;
import org.eclipse.cdt.make.internal.core.makefile.AbstractMakefile;
import org.eclipse.cdt.make.internal.core.makefile.BadDirective;
import org.eclipse.cdt.make.internal.core.makefile.Command;
@ -43,7 +44,9 @@ import org.eclipse.cdt.make.internal.core.makefile.SuffixesRule;
import org.eclipse.cdt.make.internal.core.makefile.Target;
import org.eclipse.cdt.make.internal.core.makefile.TargetRule;
import org.eclipse.cdt.make.internal.core.makefile.Util;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
@ -67,15 +70,44 @@ import org.eclipse.core.runtime.Path;
public class PosixMakefile extends AbstractMakefile {
IDirective[] builtins = null;
private IMakefileReaderProvider makefileReaderProvider;
public PosixMakefile() {
super(null);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.make.core.makefile.IMakefile#getMakefileReaderProvider()
*/
public IMakefileReaderProvider getMakefileReaderProvider() {
return makefileReaderProvider;
}
public void parse(String name, Reader reader) throws IOException {
parse(URIUtil.toURI(name), new MakefileReader(reader));
}
/* (non-Javadoc)
* @see org.eclipse.cdt.make.core.makefile.IMakefile#parse(java.net.URI, org.eclipse.cdt.make.core.makefile.IMakefileReaderProvider)
*/
public void parse(URI fileURI,
IMakefileReaderProvider makefileReaderProvider) throws IOException {
this.makefileReaderProvider = makefileReaderProvider;
MakefileReader reader;
if (makefileReaderProvider == null) {
try {
reader = new MakefileReader(new InputStreamReader(
EFS.getStore(fileURI).openInputStream(EFS.NONE, null)));
} catch (CoreException e) {
MakeCorePlugin.log(e);
throw new IOException(e.getMessage());
}
} else {
reader = new MakefileReader(makefileReaderProvider.getReader(fileURI));
}
parse(fileURI, reader);
}
public void parse(URI fileURI, Reader reader) throws IOException {
parse(fileURI, new MakefileReader(reader));
}
@ -332,4 +364,5 @@ public class PosixMakefile extends AbstractMakefile {
}
return targetRules;
}
}