From a89ce59df232d5ffc3daf74ad9b2c14d28f08516 Mon Sep 17 00:00:00 2001 From: Erwin Waterlander Date: Tue, 7 Mar 2023 12:12:28 +0000 Subject: [PATCH] Improved getting resources from the gcc commandline. The old method assumed all resources were at the end of the line, optionally followed by option -o. Now the complete line is scanned for valid resource file extensions. --- .../META-INF/MANIFEST.MF | 2 +- .../cdt/build/gcc/core/GCCToolChain.java | 75 ++++++++++++++----- 2 files changed, 56 insertions(+), 21 deletions(-) diff --git a/build/org.eclipse.cdt.build.gcc.core/META-INF/MANIFEST.MF b/build/org.eclipse.cdt.build.gcc.core/META-INF/MANIFEST.MF index bbd122df157..49dd7fa2965 100644 --- a/build/org.eclipse.cdt.build.gcc.core/META-INF/MANIFEST.MF +++ b/build/org.eclipse.cdt.build.gcc.core/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.cdt.build.gcc.core;singleton:=true -Bundle-Version: 2.1.0.qualifier +Bundle-Version: 2.1.100.qualifier Bundle-Activator: org.eclipse.cdt.build.gcc.core.internal.Activator Bundle-Vendor: %providerName Require-Bundle: org.eclipse.core.runtime, diff --git a/build/org.eclipse.cdt.build.gcc.core/src/org/eclipse/cdt/build/gcc/core/GCCToolChain.java b/build/org.eclipse.cdt.build.gcc.core/src/org/eclipse/cdt/build/gcc/core/GCCToolChain.java index 0bdcbd7cf16..4c7bec4b39c 100644 --- a/build/org.eclipse.cdt.build.gcc.core/src/org/eclipse/cdt/build/gcc/core/GCCToolChain.java +++ b/build/org.eclipse.cdt.build.gcc.core/src/org/eclipse/cdt/build/gcc/core/GCCToolChain.java @@ -20,10 +20,13 @@ import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; +import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -46,6 +49,8 @@ import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.PlatformObject; +import org.eclipse.core.runtime.content.IContentType; +import org.eclipse.core.runtime.content.IContentTypeManager; /** * The GCC toolchain. This is the base class for all GCC toolchains. It @@ -68,6 +73,7 @@ public class GCCToolChain extends PlatformObject implements IToolChain { private String cCommand; private String cppCommand; private String[] commands; + private Set resourcesFileExtensions = getResourcesFileExtensions(); @Deprecated public GCCToolChain(IToolChainProvider provider, String id, String version) { @@ -640,42 +646,71 @@ public class GCCToolChain extends PlatformObject implements IToolChain { } } + private Set getResourcesFileExtensions() { + IContentTypeManager manager = Platform.getContentTypeManager(); + + Set fileExts = new TreeSet(String.CASE_INSENSITIVE_ORDER); + + IContentType contentTypeCpp = manager.getContentType(CCorePlugin.CONTENT_TYPE_CXXSOURCE); + fileExts.addAll(Arrays.asList(contentTypeCpp.getFileSpecs(IContentType.FILE_EXTENSION_SPEC))); + + IContentType contentTypeC = manager.getContentType(CCorePlugin.CONTENT_TYPE_CSOURCE); + fileExts.addAll(Arrays.asList(contentTypeC.getFileSpecs(IContentType.FILE_EXTENSION_SPEC))); + + return fileExts; + } + + private static String getFileExtension(String fileName) { + String ext = ""; //$NON-NLS-1$ + + int i = fileName.lastIndexOf('.'); + if (i > 0) { + ext = fileName.substring(i + 1); + } + return ext; + } + @Override public IResource[] getResourcesFromCommand(List cmd, URI buildDirectoryURI) { // Start at the back looking for arguments List resources = new ArrayList<>(); IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); + for (int i = cmd.size() - 1; i >= 0; --i) { String arg = cmd.get(i); if (arg.startsWith("-")) { //$NON-NLS-1$ - // ran into an option, we're done. - break; + // ran into an option, skip. + continue; } if (i > 1 && cmd.get(i - 1).equals("-o")) { //$NON-NLS-1$ // this is an output file --i; continue; } - try { - Path srcPath = Paths.get(arg); - URI uri; - if (srcPath.isAbsolute()) { - uri = srcPath.toUri(); - } else { - String mingwPath = fixMingwPath(arg); - if (mingwPath != arg) { - uri = Paths.get(mingwPath).toUri(); - } else { - uri = Paths.get(buildDirectoryURI).resolve(srcPath).toUri().normalize(); - } - } - for (IFile resource : root.findFilesForLocationURI(uri)) { - resources.add(resource); + String ext = getFileExtension(arg); + if (resourcesFileExtensions.contains(ext)) { + try { + Path srcPath = Paths.get(arg); + URI uri; + if (srcPath.isAbsolute()) { + uri = srcPath.toUri(); + } else { + String mingwPath = fixMingwPath(arg); + if (mingwPath != arg) { + uri = Paths.get(mingwPath).toUri(); + } else { + uri = Paths.get(buildDirectoryURI).resolve(srcPath).toUri().normalize(); + } + } + + for (IFile resource : root.findFilesForLocationURI(uri)) { + resources.add(resource); + } + } catch (IllegalArgumentException e) { + // Bad URI + continue; } - } catch (IllegalArgumentException e) { - // Bad URI - continue; } }