Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.nio.file.Files;
  6. import java.nio.file.Paths;
  7. import java.util.ArrayList;
  8. import java.util.Arrays;
  9. import java.util.Collections;
  10. import java.util.HashSet;
  11. import java.util.concurrent.ConcurrentHashMap;
  12. import java.util.concurrent.ConcurrentLinkedQueue;
  13. import java.util.stream.Collectors;
  14.  
  15. public class DependencyDiscoverer {
  16. static ArrayList<String> dirs = new ArrayList<>();
  17. static ArrayList<String> files = new ArrayList<>();
  18. static ArrayList<String> parsedFiles = new ArrayList<>();
  19.  
  20. static ConcurrentHashMap<String, ArrayList<String>> masterMap = new ConcurrentHashMap<>();
  21.  
  22. public static void main(String[] args) {
  23. ConcurrentLinkedQueue<String> workQueue = new ConcurrentLinkedQueue<>();
  24.  
  25.  
  26. String cHome = System.getenv("CPATH");
  27. if (cHome != null) {
  28. Collections.addAll(dirs, cHome.split(":"));
  29. if (dirs.size() == 0) dirs.add(cHome);
  30. }
  31. dirs.add(".");
  32. Arrays.stream(args).forEach(arg -> {
  33. if (arg.startsWith("-I")) {
  34. dirs.add(arg.substring(2));
  35. } else {
  36. workQueue.add(arg);
  37. }
  38. });
  39.  
  40. dirs.stream().filter(d -> Files.exists(Paths.get(d)))
  41. .forEach((String dir) -> files.addAll(Arrays.stream(new File(dir).listFiles())
  42. .filter(f -> f.isFile() && f.getName().endsWith(".h")).map(File::getAbsolutePath).collect(Collectors.toList())));
  43.  
  44. String i;
  45. while ((i = workQueue.poll()) != null) {
  46. ArrayList<String> dependencies = new ArrayList<>();
  47. String fileName = i.substring(i.lastIndexOf("/") + 1);
  48. String currentDirName = i.replace(fileName, "");
  49. dependencies.add(fileName);
  50.  
  51. BufferedReader reader = null;
  52.  
  53. try {
  54. File file = new File(i);
  55. reader = new BufferedReader(new FileReader(file));
  56. String line;
  57. while ((line = reader.readLine()) != null) {
  58. line = line.trim();
  59. if (line.startsWith("#include") && line.contains("\"")) {
  60. String depFileName = line.substring(line.indexOf("\"") + 1, line.lastIndexOf("\""));
  61. dependencies.add(depFileName);
  62. String depPath = currentDirName + depFileName;
  63. if (!parsedFiles.contains(depPath)) {
  64. workQueue.add(depPath);
  65. parsedFiles.add(depPath);
  66. }
  67. }
  68. }
  69.  
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. } finally {
  73. try {
  74. reader.close();
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. }
  78. }
  79. masterMap.put(fileName.endsWith(".h") ? fileName : fileName.substring(0, fileName.lastIndexOf(".")) + ".o", dependencies);
  80. }
  81. // masterMap.keySet().forEach(k -> {
  82. // System.out.println(k + ":" + String.join(" ", masterMap.get(k)));
  83. // });
  84.  
  85. masterMap.keySet().parallelStream().filter(k -> !k.endsWith(".h")).forEach(j -> {
  86. HashSet<String> output = new HashSet<>();
  87. output.addAll(getDependencies(new HashSet<>(), j));
  88. output.addAll(masterMap.get(j));
  89. System.out.println(j + ": " + String.join(" ", output));
  90. });
  91.  
  92.  
  93. }
  94.  
  95.  
  96. private static HashSet<String> getDependencies(HashSet<String> currentDeps, String initial) {
  97. if (masterMap.containsKey(initial)) {
  98. for (String k : masterMap.get(initial)) {
  99. if (!currentDeps.contains(k)) {
  100. currentDeps.add(k);
  101. if (masterMap.containsKey(k)) {
  102. currentDeps.addAll(getDependencies(currentDeps, k));
  103. }
  104. }
  105. }
  106. }
  107. return currentDeps;
  108. }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement