forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKDepFrontEnd.java
More file actions
112 lines (103 loc) · 4.09 KB
/
KDepFrontEnd.java
File metadata and controls
112 lines (103 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2015-2016 K Team. All Rights Reserved.
package org.kframework.kdep;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.google.inject.Module;
import org.apache.commons.collections15.ListUtils;
import org.kframework.attributes.Source;
import org.kframework.kompile.Kompile;
import org.kframework.main.FrontEnd;
import org.kframework.main.GlobalOptions;
import org.kframework.parser.concrete2kore.ParserUtils;
import org.kframework.utils.Stopwatch;
import org.kframework.utils.errorsystem.KExceptionManager;
import org.kframework.utils.file.FileUtil;
import org.kframework.utils.file.JarInfo;
import org.kframework.utils.inject.CommonModule;
import org.kframework.utils.inject.JCommanderModule;
import org.kframework.utils.inject.JCommanderModule.ExperimentalUsage;
import org.kframework.utils.inject.JCommanderModule.Usage;
import org.kframework.utils.options.OuterParsingOptions;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Frontend for kdep tool.
* <p>
* kdep is designed to generate a Makefile that contains the dependencies
* that kompile has on files when you run it. This can be used in order to ensure that if any
* of the files required by a k definition are changed, the makefile will rerun kompile.
* <p>
* Example Makefile snippet:
* <p>
* <pre>
* .depend:
* kdep definition.k -d "directory" -I includes > .depend
*
* include .depend
* </pre>
*/
public class KDepFrontEnd extends FrontEnd {
private final OuterParsingOptions options;
private final KExceptionManager kem;
private final Stopwatch sw;
private final FileUtil files;
private final ParserUtils parser;
@Inject
public KDepFrontEnd(
OuterParsingOptions options,
KExceptionManager kem,
GlobalOptions globalOptions,
@Usage String usage,
@ExperimentalUsage String experimentalUsage,
Stopwatch sw,
JarInfo jarInfo,
FileUtil files) {
super(kem, globalOptions, usage, experimentalUsage, jarInfo, files);
this.options = options;
this.kem = kem;
this.sw = sw;
this.files = files;
this.parser = new ParserUtils(files::resolveWorkingDirectory, kem, globalOptions);
}
public static List<Module> getModules() {
List<Module> modules = new ArrayList<>();
modules.add(new KDepModule());
modules.add(new JCommanderModule());
modules.add(new CommonModule());
return modules;
}
@Override
protected int run() {
List<org.kframework.kil.Module> modules = new ArrayList<>();
Source source = Source.apply(options.mainDefinitionFile(files).getAbsolutePath());
File currentDirectory = options.mainDefinitionFile(files).getParentFile();
List<File> lookupDirectories = ListUtils.union(options.includes.stream()
.map(files::resolveWorkingDirectory).collect(Collectors.toList()),
Lists.newArrayList(Kompile.BUILTIN_DIRECTORY));
Set<File> requiredFiles = new HashSet<>();
// load builtin files if needed first
if (!options.noPrelude) {
modules.addAll(parser.slurp(Kompile.REQUIRE_PRELUDE_K,
source,
currentDirectory,
lookupDirectories,
requiredFiles));
}
modules.addAll(parser.slurp(FileUtil.load(options.mainDefinitionFile(files)),
source,
currentDirectory,
lookupDirectories,
requiredFiles));;
Set<File> allFiles = modules.stream().map(m -> new File(m.getSource().source())).collect(Collectors.toSet());
System.out.println(files.resolveWorkingDirectory(".").toURI().relativize(files.resolveKompiled("timestamp").toURI()).getPath() + " : \\");
for (File file : allFiles) {
System.out.println(" " + file.getAbsolutePath() + " \\");
}
System.out.println();
return 0;
}
}