changeset 12631:2303d9e199e3

rebuild projects if their annotation processor dependencies change; clean output directories before (re)compiling a Java project
author Doug Simon <doug.simon@oracle.com>
date Wed, 30 Oct 2013 09:49:53 +0100
parents aadb188e4258
children 8bb706d2b365
files mxtool/mx.py
diffstat 1 files changed, 49 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/mxtool/mx.py	Tue Oct 29 21:05:15 2013 -0700
+++ b/mxtool/mx.py	Wed Oct 30 09:49:53 2013 +0100
@@ -326,6 +326,24 @@
             self._annotationProcessors = list(ap)
         return self._annotationProcessors
 
+    def update_current_annotation_processors_file(self):
+        aps = self.annotation_processors()
+        outOfDate = False
+        currentApsFile = join(self.dir, '.currentAnnotationProcessors')
+        if exists(currentApsFile):
+            with open(currentApsFile) as fp:
+                currentAps = [l.strip() for l in fp.readlines()]
+                if currentAps != aps:
+                    outOfDate = True
+        else:
+            outOfDate = True
+        if outOfDate:
+            with open(currentApsFile, 'w') as fp:
+                for ap in aps:
+                    print >> fp, ap
+        return outOfDate
+
+
 class Library(Dependency):
     def __init__(self, suite, name, path, mustExist, urls, sourcePath, sourceUrls):
         Dependency.__init__(self, suite, name)
@@ -1853,6 +1871,22 @@
     if args.java:
         ideinit([], refreshOnly=True, buildProcessorJars=False)
 
+    def prepareOutputDirs(p, clean):
+        outputDir = p.output_dir()
+        if exists(outputDir):
+            if clean:
+                log('Cleaning {0}...'.format(outputDir))
+                shutil.rmtree(outputDir)
+                os.mkdir(outputDir)
+        else:
+            os.mkdir(outputDir)
+        genDir = p.source_gen_dir()
+        if genDir != '' and exists(genDir) and clean:
+            log('Cleaning {0}...'.format(genDir))
+            for f in os.listdir(genDir):
+                shutil.rmtree(join(genDir, f))
+        return outputDir
+
     for p in sortedProjects:
         if p.native:
             if args.native:
@@ -1875,14 +1909,7 @@
             log('Excluding {0} from build (Java compliance level {1} required)'.format(p.name, p.javaCompliance))
             continue
 
-        outputDir = p.output_dir()
-        if exists(outputDir):
-            if args.clean:
-                log('Cleaning {0}...'.format(outputDir))
-                shutil.rmtree(outputDir)
-                os.mkdir(outputDir)
-        else:
-            os.mkdir(outputDir)
+        outputDir = prepareOutputDirs(p, args.clean)
 
         cp = classpath(p.name, includeSelf=True)
         sourceDirs = p.source_dirs()
@@ -1892,6 +1919,7 @@
                 if dep.name in built:
                     mustBuild = True
 
+
         jasminAvailable = None
         javafilelist = []
         for sourceDir in sourceDirs:
@@ -1941,11 +1969,17 @@
 
                 if not mustBuild:
                     for javafile in javafiles:
-                        classfile = outputDir + javafile[len(sourceDir):-len('java')] + 'class'
-                        if not exists(classfile) or os.path.getmtime(javafile) > os.path.getmtime(classfile):
+                        classfile = TimeStampFile(outputDir + javafile[len(sourceDir):-len('java')] + 'class')
+                        if not classfile.exists() or classfile.isOlderThan(javafile):
                             mustBuild = True
                             break
 
+        aps = p.annotation_processors()
+        apsOutOfDate = p.update_current_annotation_processors_file()
+        if apsOutOfDate:
+            logv('[annotation processors for {0} changed]'.format(p.name))
+            mustBuild = True
+
         if not mustBuild:
             logv('[all class files for {0} are up to date - skipping]'.format(p.name))
             continue
@@ -1954,6 +1988,9 @@
             logv('[no Java sources for {0} - skipping]'.format(p.name))
             continue
 
+        # Ensure that the output directories are clean
+        prepareOutputDirs(p, True)
+
         built.add(p.name)
 
         argfileName = join(p.dir, 'javafilelist.txt')
@@ -1963,9 +2000,8 @@
 
         processorArgs = []
 
-        ap = p.annotation_processors()
-        if len(ap) > 0:
-            processorPath = classpath(ap, resolve=True)
+        if len(aps) > 0:
+            processorPath = classpath(aps, resolve=True)
             genDir = p.source_gen_dir()
             if exists(genDir):
                 shutil.rmtree(genDir)