Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d997002

Browse files
fix some bugs in compiling and linking
1 parent a0e2eb5 commit d997002

File tree

9 files changed

+158
-199
lines changed

9 files changed

+158
-199
lines changed

OCamlSources/src/ocaml/compile/BaseOCamlCompiler.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import com.intellij.execution.configurations.GeneralCommandLine;
2222
import com.intellij.openapi.compiler.CompileContext;
2323
import com.intellij.openapi.compiler.CompilerMessageCategory;
24+
import com.intellij.openapi.compiler.FileProcessingCompiler;
25+
import com.intellij.openapi.compiler.ValidityState;
2426
import com.intellij.openapi.module.Module;
2527
import com.intellij.openapi.projectRoots.Sdk;
2628
import com.intellij.openapi.roots.ModuleRootManager;
@@ -33,6 +35,7 @@
3335
import org.jetbrains.annotations.NotNull;
3436
import org.jetbrains.annotations.Nullable;
3537

38+
import java.io.File;
3639
import java.util.List;
3740

3841
import static com.intellij.openapi.compiler.CompilerMessageCategory.ERROR;
@@ -90,4 +93,61 @@ protected void processLines(@NotNull final List<String> lines, @NotNull final Co
9093
context.addMessage(category, line, url, -1, -1);
9194
}
9295
}
96+
97+
@NotNull
98+
protected OCamlLinkerProcessingItem createFakeProcessingItem(@NotNull final VirtualFile file) {
99+
return doCreateProcessingItem(null, file, null);
100+
}
101+
102+
@NotNull
103+
protected OCamlLinkerProcessingItem createProcessingItem(@NotNull final VirtualFile file,
104+
@NotNull final File compiledFile,
105+
final boolean isDebugMode,
106+
final boolean force) {
107+
return createProcessingItem(null, file, compiledFile, isDebugMode, force);
108+
}
109+
110+
@NotNull
111+
protected OCamlLinkerProcessingItem createProcessingItem(@Nullable final OCamlModule ocamlModule,
112+
@NotNull final VirtualFile file,
113+
@NotNull final File compiledFile,
114+
final boolean isDebugMode,
115+
final boolean force) {
116+
final ValidityState state = OCamlValidityState.create(file, compiledFile, isDebugMode, force);
117+
return doCreateProcessingItem(ocamlModule, file, state);
118+
}
119+
120+
@NotNull
121+
private OCamlLinkerProcessingItem doCreateProcessingItem(@Nullable final OCamlModule ocamlModule,
122+
@NotNull final VirtualFile file,
123+
@Nullable final ValidityState state) {
124+
return new OCamlLinkerProcessingItem(ocamlModule, file, state);
125+
}
126+
127+
protected static class OCamlLinkerProcessingItem implements FileProcessingCompiler.ProcessingItem {
128+
@Nullable private final OCamlModule myOCamlModule;
129+
@NotNull private final VirtualFile myFile;
130+
@Nullable private final ValidityState myState;
131+
132+
private OCamlLinkerProcessingItem(@Nullable final OCamlModule OCamlModule, @NotNull final VirtualFile file, @Nullable final ValidityState state) {
133+
myOCamlModule = OCamlModule;
134+
myFile = file;
135+
myState = state;
136+
}
137+
138+
@Nullable
139+
OCamlModule getOCamlModule() {
140+
return myOCamlModule;
141+
}
142+
143+
@NotNull
144+
public VirtualFile getFile() {
145+
return myFile;
146+
}
147+
148+
@Nullable
149+
public ValidityState getValidityState() {
150+
return myState;
151+
}
152+
}
93153
}

OCamlSources/src/ocaml/compile/FlagBasedValidityState.java

Lines changed: 0 additions & 68 deletions
This file was deleted.

OCamlSources/src/ocaml/compile/OCamlCompiler.java

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public ProcessingItem[] getProcessingItems(@NotNull final CompileContext context
7171

7272
try {
7373
if (ocamlContext.isStandaloneCompile()) {
74-
ocamlModules.addAll(collectItemsForStandaloneCompile(context, items, isDebugMode));
74+
ocamlModules.addAll(collectItemsForStandaloneCompile(context, items));
7575
}
7676
else {
7777
final OCamlModule mainOCamlModule = getMainOCamlModule(ocamlContext);
@@ -85,26 +85,32 @@ public ProcessingItem[] getProcessingItems(@NotNull final CompileContext context
8585
return new ProcessingItem[0];
8686
}
8787

88+
final boolean isRebuild = context.isRebuild();
8889
final LocalFileSystem fileSystem = LocalFileSystem.getInstance();
8990
for (final OCamlModule ocamlModule : ocamlModules) {
90-
processFile(fileSystem.findFileByIoFile(ocamlModule.getInterfaceFile()), items, isDebugMode);
91-
processFile(fileSystem.findFileByIoFile(ocamlModule.getImplementationFile()), items, isDebugMode);
91+
processFile(fileSystem.findFileByIoFile(ocamlModule.getInterfaceFile()), ocamlModule.getCompiledInterfaceFile(), items, isDebugMode, isRebuild);
92+
processFile(fileSystem.findFileByIoFile(ocamlModule.getImplementationFile()), ocamlModule.getCompiledImplementationFile(), items, isDebugMode, isRebuild);
9293
}
9394

9495
return items.toArray(new ProcessingItem[items.size()]);
9596
}
9697

97-
private void processFile(@Nullable final VirtualFile file, @NotNull final ArrayList<ProcessingItem> items, final boolean isDebugMode) {
98+
private void processFile(@Nullable final VirtualFile file,
99+
@NotNull final File compiledFile,
100+
@NotNull final ArrayList<ProcessingItem> items,
101+
final boolean isDebugMode,
102+
final boolean isRebuild) {
98103
if (file != null) {
99-
items.add(createProcessingItem(file, isDebugMode));
104+
items.add(createProcessingItem(file, compiledFile, isDebugMode, isRebuild));
100105
}
101106
}
102107

103108
@NotNull
104109
private List<OCamlModule> collectItemsForStandaloneCompile(@NotNull final CompileContext context,
105-
@NotNull final ArrayList<ProcessingItem> items,
106-
final boolean isDebugMode) throws CyclicDependencyException {
110+
@NotNull final ArrayList<ProcessingItem> items
111+
) throws CyclicDependencyException {
107112
final Project project = context.getProject();
113+
final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
108114
final Set<OCamlModule> ocamlModules = new HashSet<OCamlModule>();
109115
final Module[] modules = ModuleManager.getInstance(project).getModules();
110116
for (final Module module : modules) {
@@ -119,7 +125,8 @@ private List<OCamlModule> collectItemsForStandaloneCompile(@NotNull final Compil
119125
ocamlModules.add(ocamlModule);
120126
}
121127
else {
122-
items.add(createProcessingItem(file, isDebugMode));
128+
final File destDir = OCamlFileUtil.getCompiledDir(projectFileIndex, file.getParent());
129+
items.add(createProcessingItem(file, new File(destDir, file.getName()), false, false));
123130
}
124131
}
125132
}
@@ -146,7 +153,6 @@ public ProcessingItem[] process(@NotNull final CompileContext context, @NotNull
146153
final ArrayList<ProcessingItem> processedItems = new ArrayList<ProcessingItem>();
147154
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(context.getProject()).getFileIndex();
148155
final OCamlCompileContext ocamlContext = OCamlCompileContext.createOn(context);
149-
final boolean isDebugMode = ocamlContext.isDebugMode();
150156

151157
for (final ProcessingItem item : items) {
152158
processedCount++;
@@ -196,13 +202,10 @@ private boolean compile(@NotNull final VirtualFile file,
196202
cmd.addParameter("-c");
197203

198204
final Set<String> addedPaths = new HashSet<String>();
205+
addPath(cmd, addedPaths, destDir.getPath());
199206
for (final OCamlModule dependency : ocamlModule.collectExactDependencies()) {
200207
final String path = OCamlFileUtil.getCompiledDir(fileIndex, dependency.getSourcesDir()).getPath();
201-
if (!addedPaths.contains(path)) {
202-
cmd.addParameter("-I");
203-
cmd.addParameter(path);
204-
}
205-
addedPaths.add(path);
208+
addPath(cmd, addedPaths, path);
206209
}
207210

208211
cmd.addParameter("-o");
@@ -232,6 +235,14 @@ private boolean compile(@NotNull final VirtualFile file,
232235
return true;
233236
}
234237

238+
private void addPath(@NotNull final GeneralCommandLine cmd, @NotNull final Set<String> addedPaths, @NotNull final String path) {
239+
if (!addedPaths.contains(path)) {
240+
cmd.addParameter("-I");
241+
cmd.addParameter(path);
242+
}
243+
addedPaths.add(path);
244+
}
245+
235246
@NotNull
236247
public ValidityState createValidityState(@NotNull final DataInput in) throws IOException {
237248
return OCamlValidityState.load(in);
@@ -285,25 +296,4 @@ public VirtualFile compute() {
285296

286297
return destDir[0];
287298
}
288-
289-
@NotNull
290-
private ProcessingItem createProcessingItem(@NotNull final VirtualFile file, final boolean isDebugMode) {
291-
return new ProcessingItem() {
292-
@NotNull
293-
public VirtualFile getFile() {
294-
return file;
295-
}
296-
297-
@NotNull
298-
public ValidityState getValidityState() {
299-
final TimestampValidityState timestampValidityState = new TimestampValidityState(file.getTimeStamp());
300-
if (OCamlFileUtil.isOCamlSourceFile(file)) {
301-
return new OCamlValidityState(timestampValidityState, isDebugMode);
302-
}
303-
else {
304-
return timestampValidityState;
305-
}
306-
}
307-
};
308-
}
309299
}

OCamlSources/src/ocaml/compile/OCamlLinker.java

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@
3636
import ocaml.entity.CyclicDependencyException;
3737
import ocaml.entity.OCamlModule;
3838
import ocaml.run.OCamlRunConfiguration;
39-
import ocaml.settings.OCamlSettings;
4039
import ocaml.util.OCamlSystemUtil;
4140
import org.jetbrains.annotations.NotNull;
42-
import org.jetbrains.annotations.Nullable;
4341

4442
import java.io.DataInput;
4543
import java.io.File;
@@ -64,7 +62,7 @@ public ProcessingItem[] getProcessingItems(@NotNull final CompileContext context
6462

6563
final OCamlCompileContext ocamlContext = OCamlCompileContext.createOn(context);
6664
if (!ocamlContext.isStandaloneCompile()) {
67-
return new ProcessingItem[] { createProcessingItem(context, getMainOCamlModule(ocamlContext)) };
65+
return new ProcessingItem[] { createProcessingItem(context, ocamlContext, getMainOCamlModule(ocamlContext)) };
6866
}
6967

7068
final ArrayList<ProcessingItem> items = new ArrayList<ProcessingItem>();
@@ -73,7 +71,7 @@ public ProcessingItem[] getProcessingItems(@NotNull final CompileContext context
7371
if (!(configuration instanceof OCamlRunConfiguration)) continue;
7472
final OCamlModule ocamlModule = ((OCamlRunConfiguration) configuration).getMainOCamlModule();
7573
if (ocamlModule == null) continue;
76-
items.add(createProcessingItem(context, ocamlModule));
74+
items.add(createProcessingItem(context, ocamlContext, ocamlModule));
7775
}
7876

7977
return items.toArray(new ProcessingItem[items.size()]);
@@ -91,7 +89,6 @@ public ProcessingItem[] process(@NotNull final CompileContext context, @NotNull
9189
final ArrayList<ProcessingItem> processedItems = new ArrayList<ProcessingItem>();
9290
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(context.getProject()).getFileIndex();
9391
final OCamlCompileContext ocamlContext = OCamlCompileContext.createOn(context);
94-
final boolean isDebugMode = ocamlContext.isDebugMode();
9592

9693
for (final ProcessingItem item : items) {
9794
progressIndicator.setText2("");
@@ -113,9 +110,6 @@ public ProcessingItem[] process(@NotNull final CompileContext context, @NotNull
113110
}
114111

115112
processedItems.add(item);
116-
final ValidityState state = item.getValidityState();
117-
assert state != null && state instanceof FlagBasedValidityState;
118-
OCamlSettings.getInstance().saveExeFileState(exeFilePath, (FlagBasedValidityState) state);
119113
}
120114

121115
return processedItems.toArray(new ProcessingItem[processedItems.size()]);
@@ -163,49 +157,25 @@ private void link(@NotNull final OCamlModule ocamlModule,
163157

164158
@NotNull
165159
public ValidityState createValidityState(@NotNull final DataInput in) throws IOException {
166-
return FlagBasedValidityState.load(in);
160+
return OCamlValidityState.load(in);
167161
}
168162

169163
@NotNull
170-
private ProcessingItem createProcessingItem(@NotNull final CompileContext context, @NotNull final OCamlModule mainOCamlModule) {
164+
private ProcessingItem createProcessingItem(@NotNull final CompileContext context,
165+
@NotNull final OCamlCompileContext ocamlContext,
166+
@NotNull final OCamlModule mainOCamlModule) {
171167
final File cmoFile = mainOCamlModule.getCompiledImplementationFile();
172-
VirtualFile file = LocalFileSystem.getInstance().findFileByIoFile(cmoFile);
168+
VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(cmoFile);
173169
if (file == null) {
174170
context.addMessage(ERROR, "File \"" + FileUtil.toSystemDependentName(cmoFile.getAbsolutePath()) + "\" was not created. Rebuild the project.", null, -1, -1);
175-
return doCreateProcessingItem(null, mainOCamlModule.getSourcesDir(), null);
171+
return createFakeProcessingItem(mainOCamlModule.getSourcesDir());
176172
}
177173

178174
final File exeFile = mainOCamlModule.getCompiledExecutableFile();
179-
final FlagBasedValidityState oldState = OCamlSettings.getInstance().getExeFileState(exeFile.getAbsolutePath());
180-
boolean relinkingIsNeeded = false;
181175
final Boolean thereWasRecompilation = context.getUserData(THERE_WAS_RECOMPILATION);
182-
if ((thereWasRecompilation != null && thereWasRecompilation) || !exeFile.exists()) {
183-
relinkingIsNeeded = true;
184-
}
176+
final boolean forceRecompilation = (thereWasRecompilation != null && thereWasRecompilation) || context.isRebuild();
185177

186-
return doCreateProcessingItem(mainOCamlModule, file, relinkingIsNeeded ? oldState.another() : oldState);
187-
}
188-
189-
@NotNull
190-
private ProcessingItem doCreateProcessingItem(@Nullable final OCamlModule ocamlModule,
191-
@NotNull final VirtualFile file,
192-
@Nullable final FlagBasedValidityState state) {
193-
return new OCamlLinkerProcessingItem() {
194-
@Nullable
195-
public OCamlModule getOCamlModule() {
196-
return ocamlModule;
197-
}
198-
199-
@NotNull
200-
public VirtualFile getFile() {
201-
return file;
202-
}
203-
204-
@Nullable
205-
public ValidityState getValidityState() {
206-
return state;
207-
}
208-
};
178+
return createProcessingItem(mainOCamlModule, file, exeFile, ocamlContext.isDebugMode(), forceRecompilation);
209179
}
210180

211181
@NotNull
@@ -216,9 +186,4 @@ public String getDescription() {
216186
public boolean validateConfiguration(@NotNull final CompileScope scope) {
217187
return true;
218188
}
219-
220-
private static interface OCamlLinkerProcessingItem extends ProcessingItem {
221-
@Nullable
222-
OCamlModule getOCamlModule();
223-
}
224189
}

0 commit comments

Comments
 (0)