Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.HashSet;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.concurrent.ConcurrentLinkedQueue;
- import java.util.stream.Collectors;
- public class DependencyDiscoverer {
- static ArrayList<String> dirs = new ArrayList<>();
- static ArrayList<String> files = new ArrayList<>();
- static ArrayList<String> parsedFiles = new ArrayList<>();
- static ConcurrentHashMap<String, ArrayList<String>> masterMap = new ConcurrentHashMap<>();
- public static void main(String[] args) {
- ConcurrentLinkedQueue<String> workQueue = new ConcurrentLinkedQueue<>();
- String cHome = System.getenv("CPATH");
- if (cHome != null) {
- Collections.addAll(dirs, cHome.split(":"));
- if (dirs.size() == 0) dirs.add(cHome);
- }
- dirs.add(".");
- Arrays.stream(args).forEach(arg -> {
- if (arg.startsWith("-I")) {
- dirs.add(arg.substring(2));
- } else {
- workQueue.add(arg);
- }
- });
- dirs.stream().filter(d -> Files.exists(Paths.get(d)))
- .forEach((String dir) -> files.addAll(Arrays.stream(new File(dir).listFiles())
- .filter(f -> f.isFile() && f.getName().endsWith(".h")).map(File::getAbsolutePath).collect(Collectors.toList())));
- String i;
- while ((i = workQueue.poll()) != null) {
- ArrayList<String> dependencies = new ArrayList<>();
- String fileName = i.substring(i.lastIndexOf("/") + 1);
- String currentDirName = i.replace(fileName, "");
- dependencies.add(fileName);
- BufferedReader reader = null;
- try {
- File file = new File(i);
- reader = new BufferedReader(new FileReader(file));
- String line;
- while ((line = reader.readLine()) != null) {
- line = line.trim();
- if (line.startsWith("#include") && line.contains("\"")) {
- String depFileName = line.substring(line.indexOf("\"") + 1, line.lastIndexOf("\""));
- dependencies.add(depFileName);
- String depPath = currentDirName + depFileName;
- if (!parsedFiles.contains(depPath)) {
- workQueue.add(depPath);
- parsedFiles.add(depPath);
- }
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- masterMap.put(fileName.endsWith(".h") ? fileName : fileName.substring(0, fileName.lastIndexOf(".")) + ".o", dependencies);
- }
- // masterMap.keySet().forEach(k -> {
- // System.out.println(k + ":" + String.join(" ", masterMap.get(k)));
- // });
- masterMap.keySet().parallelStream().filter(k -> !k.endsWith(".h")).forEach(j -> {
- HashSet<String> output = new HashSet<>();
- output.addAll(getDependencies(new HashSet<>(), j));
- output.addAll(masterMap.get(j));
- System.out.println(j + ": " + String.join(" ", output));
- });
- }
- private static HashSet<String> getDependencies(HashSet<String> currentDeps, String initial) {
- if (masterMap.containsKey(initial)) {
- for (String k : masterMap.get(initial)) {
- if (!currentDeps.contains(k)) {
- currentDeps.add(k);
- if (masterMap.containsKey(k)) {
- currentDeps.addAll(getDependencies(currentDeps, k));
- }
- }
- }
- }
- return currentDeps;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement