/
GhcCompiler.java
159 lines (142 loc) · 5.73 KB
/
GhcCompiler.java
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) 2003-2005 by Leif Frenzel - see http://leiffrenzel.de
package net.sf.eclipsefp.haskell.ghccompiler.core;
import java.io.File;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import net.sf.eclipsefp.haskell.core.HaskellCorePlugin;
import net.sf.eclipsefp.haskell.core.compiler.DefaultHaskellCompiler;
import net.sf.eclipsefp.haskell.core.internal.util.MultiplexedWriter;
import net.sf.eclipsefp.haskell.core.project.HaskellProjectManager;
import net.sf.eclipsefp.haskell.core.project.IBuildTarget;
import net.sf.eclipsefp.haskell.core.project.IHaskellProject;
import net.sf.eclipsefp.haskell.core.util.IProcessRunner;
import net.sf.eclipsefp.haskell.core.util.ProcessRunner;
import net.sf.eclipsefp.haskell.core.util.ResourceUtil;
import net.sf.eclipsefp.haskell.ghccompiler.GhcCompilerPlugin;
import net.sf.eclipsefp.haskell.ghccompiler.ui.internal.util.UITexts;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
/**
* <p>
* Implements the compiler specific parts for the Glasgow Haskell compiler
* (ghc).
* </p>
*
* @author Leif Frenzel
*/
public class GhcCompiler extends DefaultHaskellCompiler {
private static boolean trace = GhcCompilerPlugin.isTracing();
private final IProcessRunner fProcessRunner;
private final IGhcOutputProcessor fOutputProcessor;
private final CompilerParams compilerParams = new CompilerParams();
public GhcCompiler() {
fProcessRunner = new ProcessRunner();
fOutputProcessor = new GhcOutputProcessor( );
}
public GhcCompiler(final IProcessRunner processRunner, final IGhcOutputProcessor outputProcessor) {
fProcessRunner = processRunner;
fOutputProcessor = outputProcessor;
}
@Override
public void compile( final IFile file, final Writer outputWriter ) {
final IProject project = file.getProject();
IHaskellProject hsProject = HaskellProjectManager.get( project );
String[] cmdLine = buildCommandLine( file, hsProject );
File workDir = null;
Set<IPath> sourcePaths = hsProject.getSourcePaths();
for( IPath sourcePath: sourcePaths ) {
IPath src = project.getFullPath().append( sourcePath );
if( src.isPrefixOf( file.getFullPath() ) ) {
IPath loc = project.getFolder( sourcePath ).getLocation();
workDir = new File( loc.toOSString() );
}
}
if( workDir == null ) {
throw new IllegalStateException();
}
PipedWriter pipedWriter = new PipedWriter();
PipedReader pipedReader;
try {
pipedReader = new PipedReader(pipedWriter);
} catch( IOException ex ) {
GhcCompilerPlugin.log( UITexts.error_processOutput, ex );
return;
}
try {
Writer out = new MultiplexedWriter( outputWriter, pipedWriter );
fProcessRunner.executeNonblocking( workDir, out, out, cmdLine );
} catch( IOException ex ) {
GhcCompilerPlugin.log( UITexts.error_launchGhc, ex );
return;
}
fOutputProcessor.setWorkingDir( workDir );
GhcOutputParser outputParser = new GhcOutputParser( pipedReader, fOutputProcessor );
try {
outputParser.parse();
} catch( IOException ex ) {
GhcCompilerPlugin.log( UITexts.error_processOutput, ex );
}
}
private String[] buildCommandLine( final IFile file,
final IHaskellProject haskellProject ) {
if( trace ) {
System.out.println( "Constructing command line for file " + file ); //$NON-NLS-1$
}
IProject project = haskellProject.getResource();
String buildDir = getAbsPath( project, haskellProject.getBuildPath() );
List<String> cmdLine = new ArrayList<String>();
// command and special options
cmdLine.add( Util.getCompilerExecutable() );
String libPath = Util.constructLibPath( haskellProject );
if( !libPath.equals( "" ) ) { //$NON-NLS-1$
cmdLine.add( libPath );
}
cmdLine.add( "--make" ); //$NON-NLS-1$
cmdLine.add( "-odir" ); //$NON-NLS-1$
cmdLine.add( buildDir );
cmdLine.add( "-hidir" ); //$NON-NLS-1$
cmdLine.add( buildDir );
cmdLine.add( "-ferror-spans" ); //$NON-NLS-1$
cmdLine.add( "-o" ); //$NON-NLS-1$
IPath targetName = getTargetName( haskellProject );
if( targetName.segmentCount() > 1 ) {
try {
ResourceUtil.mkdirs( targetName.removeLastSegments( 1 ), project );
} catch( final CoreException cex ) {
String msg = "Could not create folders for target executable"; //$NON-NLS-1$
GhcCompilerPlugin.log( msg, cex );
}
}
cmdLine.add( project.getLocation().append( targetName ).toOSString() );
cmdLine.addAll( compilerParams.construct() );
cmdLine.add( ResourceUtil.getSourceDirRelativeName( file, haskellProject ) );
if( trace ) {
HaskellCorePlugin.dump( cmdLine );
}
return cmdLine.toArray( new String[ cmdLine.size() ] );
}
// helping methods
// ////////////////
private String getAbsPath( final IProject project, final IPath path ) {
return project.getLocation().toOSString() + File.separator
+ path.toOSString();
}
private IPath getTargetName( final IHaskellProject haskellProject ) {
IPath targetPath;
Set<IBuildTarget> targetNames = haskellProject.getTargets();
if( targetNames.size() > 0 ) {
targetPath = targetNames.iterator().next().getPath();
} else {
targetPath = Path.fromPortableString( "theResult" ); //$NON-NLS-1$
}
return haskellProject.getOutputPath().append( targetPath );
}
}