aboutsummaryrefslogtreecommitdiffstats
path: root/anttasks/src/com/android/ant/DependencyGraph.java
blob: 7f9c65a5d2875558de0420c52656c7f95df93fd0 (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ant;

import org.apache.tools.ant.BuildException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *  This class takes care of dependency tracking for all targets and prerequisites listed in
 *  a single dependency file. A dependency graph always has a dependency file associated with it
 *  for the duration of its lifetime
 */
public class DependencyGraph {

    private static enum DependencyStatus {
        NONE, NEW_FILE, UPDATED_FILE, MISSING_FILE, ERROR;
    }

    // Files that we know about from the dependency file
    private Set<File> mTargets = Collections.emptySet();
    private Set<File> mPrereqs = mTargets;
    private File mFirstPrereq = null;
    private boolean mMissingDepFile = false;
    private long mDepFileLastModified;
    private final List<File> mNewInputs;

    public DependencyGraph(String dependencyFilePath, List<File> newInputPaths) {
        mNewInputs = newInputPaths;
        parseDependencyFile(dependencyFilePath);
    }

    /**
     * Check all the dependencies to see if anything has changed.
     * @param extensionsToCheck a set of extensions. Only files with an extension in this set will
     *        be considered for a modification check. All deleted/created files will still be
     *        checked. If this is null, all files will be checked for modification date
     * @param printStatus will print to {@link System#out} the dependencies status.
     * @return true if new prerequisites have appeared, target files are missing or if
     *         prerequisite files have been modified since the last target generation.
     */
    public boolean dependenciesHaveChanged(Set<String> extensionsToCheck, boolean printStatus) {
        // If no dependency file has been set up, then we'll just return true
        // if we have a dependency file, we'll check to see what's been changed
        if (mMissingDepFile) {
            System.out.println("No Dependency File Found");
            return true;
        }

        // check for missing output first
        if (missingTargetFile()) {
            if (printStatus) {
                System.out.println("Found Deleted Target File");
            }
            return true;
        }

        // get the timestamp of the oldest target.
        long oldestTarget = getOutputLastModified();

        // first look through the input folders and look for new files or modified files.
        DependencyStatus status = checkInputs(extensionsToCheck, oldestTarget);

        // this can't find missing files. This is done later.
        switch (status) {
            case ERROR:
                throw new BuildException();
            case NEW_FILE:
                if (printStatus) {
                    System.out.println("Found new input file");
                }
                return true;
            case UPDATED_FILE:
                if (printStatus) {
                    System.out.println("Found modified input file");
                }
                return true;
        }

        // now do a full check on the remaining files.
        status = checkPrereqFiles(extensionsToCheck, oldestTarget);
        // this can't find new input files. This is done above.
        switch (status) {
            case ERROR:
                throw new BuildException();
            case MISSING_FILE:
                if (printStatus) {
                    System.out.println("Found deleted input file");
                }
                return true;
            case UPDATED_FILE:
                if (printStatus) {
                    System.out.println("Found modified input file");
                }
                return true;
        }

        return false;
    }

    public Set<File> getTargets() {
        return Collections.unmodifiableSet(mTargets);
    }

    public File getFirstPrereq() {
        return mFirstPrereq;
    }

    /**
     * Parses the given dependency file and stores the file paths
     *
     * @param dependencyFilePath the dependency file
     */
    private void parseDependencyFile(String dependencyFilePath) {
        // first check if the dependency file is here.
        File depFile = new File(dependencyFilePath);
        if (depFile.isFile() == false) {
            mMissingDepFile = true;
            return;
        }

        // get the modification time of the dep file as we may need it later
        mDepFileLastModified = depFile.lastModified();

        // Read in our dependency file
        String content = readFile(dependencyFilePath);
        if (content == null) {
            System.err.println("ERROR: Couldn't read " + dependencyFilePath);
            return;
        }

        // The format is something like:
        // output1 output2 [...]: dep1 dep2 [...]
        // expect it's likely split on several lines. So let's move it back on a single line
        // first
        String[] lines = content.toString().split("\n");
        StringBuilder sb = new StringBuilder(content.length());
        for (String line : lines) {
            line = line.trim();
            if (line.endsWith("\\")) {
                line = line.substring(0, line.length() - 1);
            }
            sb.append(line);
        }

        // split the left and right part
        String[] files = sb.toString().split(":");

        // get the target files:
        String[] targets = files[0].trim().split(" ");

        String[] prereqs = {};
        // Check to make sure our dependency file is okay
        if (files.length < 1) {
            System.err.println(
                    "Warning! Dependency file does not list any prerequisites after ':' ");
        } else {
            // and the prerequisite files:
            prereqs = files[1].trim().split(" ");
        }

        mTargets = new HashSet<File>(targets.length);
        for (String path : targets) {
            if (path.length() > 0) {
                mTargets.add(new File(path));
            }
        }

        mPrereqs = new HashSet<File>(prereqs.length);
        for (String path : prereqs) {
            if (path.length() > 0) {
                File f = new File(path);
                if (mFirstPrereq == null) {
                    mFirstPrereq = f;
                }
                mPrereqs.add(f);
            }
        }
    }

    /**
     * Check all the input files and folders to see if there have been new
     * files added to them or if any of the existing files have been modified.
     *
     * This looks at the input paths, not at the list of known prereq. Therefore this
     * will not find missing files. It will however remove processed files from the
     * prereq file list so that we can process those in a 2nd step.
     *
     * This should be followed by a call to {@link #checkPrereqFiles(long)} which
     * will process the remaining files in the prereq list.
     *
     * If a change is found, this will return immediatly with either
     * {@link DependencyStatus#NEW_FILE} or {@link DependencyStatus#UPDATED_FILE}.
     *
     * @param extensionsToCheck a set of extensions. Only files with an extension in this set will
     *        be considered for a modification check. All deleted/created files will still be
     *        checked. If this is null, all files will be checked for modification date
     * @param oldestTarget the timestamp of the oldest output file to compare against.
     *
     * @return the status of the file in the watched folders.
     *
     */
    private DependencyStatus checkInputs(Set<String> extensionsToCheck, long oldestTarget) {
        if (mNewInputs != null) {
            for (File input : mNewInputs) {
                if (input.isDirectory()) {
                    DependencyStatus status = checkInputFolder(input, extensionsToCheck,
                            oldestTarget);
                    if (status != DependencyStatus.NONE) {
                        return status;
                    }
                } else if (input.isFile()) {
                    DependencyStatus status = checkInputFile(input, extensionsToCheck,
                            oldestTarget);
                    if (status != DependencyStatus.NONE) {
                        return status;
                    }
                }
            }
        }

        // If we make it all the way through our directories we're good.
        return DependencyStatus.NONE;
    }

    /**
     * Check all the files in the tree under root and check to see if the files are
     * listed under the dependencies, or if they have been modified. Recurses into subdirs.
     *
     * @param rootFolder the folder to search through.
     * @param extensionsToCheck a set of extensions. Only files with an extension in this set will
     *        be considered for a modification check. All deleted/created files will still be
     *        checked. If this is null, all files will be checked for modification date
     * @param oldestTarget the timestamp of the oldest output file to compare against.
     *
     * @return the status of the file in the folder.
     */
    private DependencyStatus checkInputFolder(File rootFolder, Set<String> extensionsToCheck,
            long oldestTarget) {
        File[] files = rootFolder.listFiles();
        if (files == null) {
            System.err.println("ERROR " + rootFolder.toString() + " is not a dir or can't be read");
            return DependencyStatus.ERROR;
        }
        // Loop through files in this folder
        for (File file : files) {
            // If this is a directory, recurse into it
            if (file.isDirectory()) {
                DependencyStatus status = checkInputFolder(file, extensionsToCheck, oldestTarget);
                if (status != DependencyStatus.NONE) {
                    return status;
                }
            } else if (file.isFile()) {
                DependencyStatus status = checkInputFile(file, extensionsToCheck, oldestTarget);
                if (status != DependencyStatus.NONE) {
                    return status;
                }
            }
        }
        // If we got to here then we didn't find anything interesting
        return DependencyStatus.NONE;
    }

    private DependencyStatus checkInputFile(File file, Set<String> extensionsToCheck,
            long oldestTarget) {
        // if it's a file, remove it from the list of prereqs.
        // This way if files in this folder don't trigger a build we'll have less
        // files to go through manually
        if (mPrereqs.remove(file) == false) {
            // turns out this is a new file!
            return DependencyStatus.NEW_FILE;
        } else {
            // check the time stamp on this file if it's a file we care about based on the
            // list of extensions to check.
            if (extensionsToCheck == null || extensionsToCheck.contains(getExtension(file))) {
                if (file.lastModified() > oldestTarget) {
                    return DependencyStatus.UPDATED_FILE;
                }
            }
        }

        return DependencyStatus.NONE;
    }

    /**
     * Check all the prereq files we know about to make sure they're still there, or that they
     * haven't been modified since the last build.
     *
     * @param extensionsToCheck a set of extensions. Only files with an extension in this set will
     *        be considered for a modification check. All deleted/created files will still be
     *        checked. If this is null, all files will be checked for modification date
     *
     * @return the status of the files
     */
    private DependencyStatus checkPrereqFiles(Set<String> extensionsToCheck, long oldestTarget) {
        // Loop through our prereq files and make sure they still exist
        for (File prereq : mPrereqs) {
            if (prereq.exists() == false) {
                return DependencyStatus.MISSING_FILE;
            }

            // check the time stamp on this file if it's a file we care about based on the
            // list of extensions to check.
            if (extensionsToCheck == null || extensionsToCheck.contains(getExtension(prereq))) {
                if (prereq.lastModified() > oldestTarget) {
                    return DependencyStatus.UPDATED_FILE;
                }
            }
        }

        // If we get this far, then all our prereq are okay
        return DependencyStatus.NONE;
    }

    /**
     * Check all the target files we know about to make sure they're still there
     * @return true if any of the target files are missing.
     */
    private boolean missingTargetFile() {
        // Loop through our target files and make sure they still exist
        for (File target : mTargets) {
            if (target.exists() == false) {
                return true;
            }
        }
        // If we get this far, then all our targets are okay
        return false;
    }

    /**
     * Returns the earliest modification time stamp from all the output targets. If there
     * are no known target, the dependency file time stamp is returned.
     */
    private long getOutputLastModified() {
        // Find the oldest target
        long oldestTarget = Long.MAX_VALUE;
        // if there's no output, then compare to the time of the dependency file.
        if (mTargets.size() == 0) {
            oldestTarget = mDepFileLastModified;
        } else {
            for (File target : mTargets) {
                if (target.lastModified() < oldestTarget) {
                    oldestTarget = target.lastModified();
                }
            }
        }

        return oldestTarget;
    }

    /**
     * Reads and returns the content of a text file.
     * @param filepath the file path to the text file
     * @return null if the file could not be read
     */
    private static String readFile(String filepath) {
        try {
            FileInputStream fStream = new FileInputStream(filepath);
            if (fStream != null) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(fStream));

                String line;
                StringBuilder total = new StringBuilder(reader.readLine());
                while ((line = reader.readLine()) != null) {
                    total.append('\n');
                    total.append(line);
                }
                return total.toString();
            }
        } catch (IOException e) {
            // we'll just return null
        }
        return null;
    }

    /**
      *  Gets the extension (if present) on a file by looking at the filename
      *  @param file the file to get the extension of
      *  @return the extension if present, or the empty string if the filename doesn't have
      *          and extension.
      */
    private static String getExtension(File file) {
        String filename = file.getName();
        if (filename.lastIndexOf('.') == -1) {
            return "";
        }
        // Don't include the leading '.' in the extension
        return filename.substring(filename.lastIndexOf('.') + 1);
    }
}