summaryrefslogtreecommitdiff
path: root/java/ant/SliceTask.java
blob: 67636b01fe2e26e2ebd62cd00f57f97c467cb07d (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// **********************************************************************
//
// Copyright (c) 2001
// ZeroC, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

//package Ice.Ant;

import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.taskdefs.ExecTask;
import org.apache.tools.ant.taskdefs.Execute;
import org.apache.tools.ant.taskdefs.PumpStreamHandler;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.Commandline.Argument;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.StringReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;

/**
 * An abstract ant task for slice translators. The task minimizes
 * regeneration by checking the dependencies between slice files.
 *
 * Attributes:
 *
 *   dependencyfile - The file in which dependencies are stored (default: ".depend").
 *   outputdir - The value for the --output-dir translator option.
 *   casesensitive - The value for the --case-sensitive translator option.
 *
 * Nested elements:
 *
 *   includepath - The directories in which to search for Slice files.
 *   These are converted into -I directives for the translator.
 *   fileset - The set of Slice files which contain relevent types.
 *
 */
public class SliceTask extends org.apache.tools.ant.Task
{
    public
    SliceTask()
    {
        _dependencyFile = null;
        _outputDir = null;
	_caseSensitive = false;
        _includePath = null;
	_fileSets = new java.util.LinkedList();
    }

    public void
    setTagdir(File dir)
    {
    }

    public void
    setDependencyFile(File file)
    {
        _dependencyFile = file;
    }

    public void
    setOutputdir(File dir)
    {
        _outputDir = dir;
    }

    public void
    setCaseSensitive(boolean c)
    {
        _caseSensitive = c;
    }

    public Path
    createIncludePath()
    {
        if(_includePath == null) 
        {
            _includePath = new Path(project);
        }
        return _includePath.createPath();
    }

    public void
    setIncludePathRef(Reference ref)
    {
        createIncludePath().setRefid(ref);
    }

    public void
    setIncludePath(Path includePath)
    {
        if(_includePath == null)
        {
            _includePath = includePath;  
        }
        else
        {
            _includePath.append(includePath);
        }
    }

    public FileSet
    createFileset()
    {
        FileSet fileset = new FileSet();
        _fileSets.add(fileset);

        return fileset;
    }    

    //
    // Read the dependency file.
    //
    protected java.util.HashMap
    readDependencies()
    {
	if(_dependencyFile == null)
	{
	    if(_outputDir != null)
	    {
		_dependencyFile = new File(_outputDir, ".depend");
	    }
	    else
	    {
		_dependencyFile = new File(".depend");
	    }
	}

	try
	{
	    java.io.ObjectInputStream in = new java.io.ObjectInputStream(new java.io.FileInputStream(_dependencyFile));
	    java.util.HashMap dependencies = (java.util.HashMap)in.readObject();
	    in.close();
	    return dependencies;
	}
	catch(java.io.IOException ex)
	{
	}
	catch(java.lang.ClassNotFoundException ex)
	{
	}
	
	return new java.util.HashMap();
    }

    protected void
    writeDependencies(java.util.HashMap dependencies)
    {
	try
	{
	    java.io.ObjectOutputStream out = new java.io.ObjectOutputStream(new FileOutputStream(_dependencyFile));
	    out.writeObject(dependencies);
	    out.close();
	}
	catch(java.io.IOException ex)
	{
	    throw new BuildException("Unable to write dependencies in file " + _dependencyFile.getPath() + ": " + ex);
	}
    }

    //
    // Parse dependencies returned by the slice translator (Makefile
    // dependencies).
    //
    protected java.util.List
    parseDependencies(String allDependencies)
    {
	java.util.List dependencies = new java.util.LinkedList();
	try
	{
	    BufferedReader in = new BufferedReader(new StringReader(allDependencies));
	    StringBuffer depline = new StringBuffer();
	    String line;

	    while((line = in.readLine()) != null)
	    {
		if(line.endsWith("\\"))
		{
		    depline.append(line.substring(0, line.length() - 1));
		}
		else
		{
		    depline.append(line);

		    String[] deps = depline.toString().split("[ \t\n\r]+");
		    assert(deps.length > 1);

		    //
		    // The first element is the target (<slice file>.cpp or .o dependending on the
		    // pre-processor), the second element is the compiled slice file and the other 
		    // elements are the dependency of the compiled slice file.
		    //

		    SliceDependency depend = new SliceDependency();

		    depend._dependencies = new String[deps.length - 1];
		    for(int i = 1; i < deps.length; ++i)
		    {
			depend._dependencies[i - 1] = deps[i];
		    }	
		    depend._timeStamp = new java.util.Date().getTime();

		    dependencies.add(depend);

		    depline = new StringBuffer();
		}
	    }
	}
	catch(java.io.IOException ex)
	{
	    throw new BuildException("Unable to read dependencies from slice translator: " + ex);
	}
	
	return dependencies;

    }

    //
    // A slice dependency.
    //
    // * the _timeStamp attribute contains the last time the slice
    //   file was compiled.
    //
    // * the _dependencies attribute contains an array with all the
    //   files this slice file depends on.
    //
    // This dependency represents the dependencies for the slice file
    // _dependencies[0].
    //
    protected class SliceDependency implements java.io.Serializable
    {
	private void writeObject(java.io.ObjectOutputStream out)
	    throws java.io.IOException
        {
	    out.writeObject(_dependencies);
	    out.writeLong(_timeStamp);
	}

	private void readObject(java.io.ObjectInputStream in) 
	    throws java.io.IOException, java.lang.ClassNotFoundException
        {
	    _dependencies = (String[])in.readObject();
	    _timeStamp = in.readLong();
	}

	public boolean
	isUpToDate()
        {
	    for(int i = 0; i < _dependencies.length; ++i)
	    {
		File dep = new File(_dependencies[i]);
		if(!dep.exists() || _timeStamp < dep.lastModified())
		{
		    return false;
		}
	    }

	    return true;
	}

	public String[] _dependencies;
	public long _timeStamp;
    };

    protected File _dependencyFile;
    protected File _outputDir;
    protected boolean _caseSensitive;
    protected Path _includePath;
    protected java.util.List _fileSets = new java.util.LinkedList();
}