src/share/classes/com/sun/tools/sjavac/options/SourceLocation.java

Print this page

        

@@ -23,14 +23,20 @@
  * questions.
  */
 
 package com.sun.tools.sjavac.options;
 
+import java.io.File;
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.FileNotFoundException;
 import java.nio.file.Path;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.HashSet;
 
 import com.sun.tools.sjavac.Module;
 import com.sun.tools.sjavac.ProblemException;
 import com.sun.tools.sjavac.Source;
 

@@ -110,6 +116,49 @@
     /** Get the file exclude patterns */
     public List<String> getExcludedFiles() {
         return excludedFiles;
     }
 
+    /** Load a list of source files from a file. */
+    public static Set<String> loadList(File sourceList) {
+        // If we are building on win32 using for example cygwin the paths in the makefile source list
+        // might be /cygdrive/c/.... which does not match c:\....
+        // We need to adjust our calculated sources to be identical, if necessary.
+        boolean mightNeedRewriting = java.io.File.pathSeparatorChar == ';';
+        Set<String> loadedSources = new HashSet<>();
+        // Read in the file and create another set of filenames with full paths.
+        try {
+            BufferedReader in = new BufferedReader(new FileReader(sourceList));
+            for (;;) {
+                String l = in.readLine();
+                if (l==null) break;
+                l = l.trim();
+                if (mightNeedRewriting) {
+                    if (l.indexOf(":") == 1 && l.indexOf("\\") == 2) {
+                        // Everything a-ok, the format is already C:\foo\bar
+                    } else if (l.indexOf(":") == 1 && l.indexOf("/") == 2) {
+                        // The format is C:/foo/bar, rewrite into the above format.
+                        l = l.replaceAll("/","\\\\");
+                    } else if (l.charAt(0) == '/' && l.indexOf("/",1) != -1) {
+                        // The format might be: /cygdrive/c/foo/bar, rewrite into the above format.
+                        // Do not hardcode the name cygdrive here.
+                        int slash = l.indexOf("/",1);
+                        l = l.replaceAll("/","\\\\");
+                        l = ""+l.charAt(slash+1)+":"+l.substring(slash+2);
+                    }
+                    if (Character.isLowerCase(l.charAt(0))) {
+                        l = Character.toUpperCase(l.charAt(0))+l.substring(1);
+                    }
+                }
+                loadedSources.add(l);
+            }
+        } catch (FileNotFoundException e) {
+            System.err.println("Could not open "+sourceList.getPath()+" since it does not exist!");
+            return null;
+        } catch (IOException e) {
+            System.err.println("Could not read "+sourceList.getPath());
+            return null;
+        }
+        return loadedSources;
+    }
+
 }