comparison mx/mx_graal.py @ 16628:d3fec84757ed

improved generated sources up-to-date check by using SHA1 digests
author Doug Simon <doug.simon@oracle.com>
date Thu, 31 Jul 2014 13:42:56 +0200
parents 68deb37eed70
children be59a1d39281
comparison
equal deleted inserted replaced
16627:68deb37eed70 16628:d3fec84757ed
28 28
29 import os, stat, errno, sys, shutil, zipfile, tarfile, tempfile, re, time, datetime, platform, subprocess, multiprocessing, StringIO, socket 29 import os, stat, errno, sys, shutil, zipfile, tarfile, tempfile, re, time, datetime, platform, subprocess, multiprocessing, StringIO, socket
30 from os.path import join, exists, dirname, basename 30 from os.path import join, exists, dirname, basename
31 from argparse import ArgumentParser, RawDescriptionHelpFormatter, REMAINDER 31 from argparse import ArgumentParser, RawDescriptionHelpFormatter, REMAINDER
32 from outputparser import OutputParser, ValuesMatcher 32 from outputparser import OutputParser, ValuesMatcher
33 import hashlib
33 import mx 34 import mx
34 import xml.dom.minidom 35 import xml.dom.minidom
35 import sanitycheck 36 import sanitycheck
36 import itertools 37 import itertools
37 import json, textwrap 38 import json, textwrap
484 toDelete = join(jreLibDir, 'graal.options') 485 toDelete = join(jreLibDir, 'graal.options')
485 if exists(toDelete): 486 if exists(toDelete):
486 os.unlink(toDelete) 487 os.unlink(toDelete)
487 488
488 def _makeHotspotGeneratedSourcesDir(): 489 def _makeHotspotGeneratedSourcesDir():
490 """
491 Gets the directory containing all the HotSpot sources generated from
492 Graal Java sources. This directory will be created if it doesn't yet exist.
493 """
489 hsSrcGenDir = join(mx.project('com.oracle.graal.hotspot').source_gen_dir(), 'hotspot') 494 hsSrcGenDir = join(mx.project('com.oracle.graal.hotspot').source_gen_dir(), 'hotspot')
490 if not exists(hsSrcGenDir): 495 if not exists(hsSrcGenDir):
491 os.makedirs(hsSrcGenDir) 496 os.makedirs(hsSrcGenDir)
492 return hsSrcGenDir 497 return hsSrcGenDir
493 498
494 499 def _update_graalRuntime_inline_hpp(dist):
495 def _update_graalRuntime_inline_hpp(jars): 500 """
501 (Re)generates graalRuntime.inline.hpp based on a given distribution
502 that transitively represents all the input for the generation process.
503
504 A SHA1 digest is computed for all generated content and is written to
505 graalRuntime.inline.hpp as well as stored in a generated class
506 that is appended to the dist.path jar. At runtime, these two digests
507 are checked for consistency.
508 """
509
496 p = mx.project('com.oracle.graal.hotspot.sourcegen') 510 p = mx.project('com.oracle.graal.hotspot.sourcegen')
497 mainClass = 'com.oracle.graal.hotspot.sourcegen.GenGraalRuntimeInlineHpp' 511 mainClass = 'com.oracle.graal.hotspot.sourcegen.GenGraalRuntimeInlineHpp'
498 if exists(join(p.output_dir(), mainClass.replace('.', os.sep) + '.class')): 512 if exists(join(p.output_dir(), mainClass.replace('.', os.sep) + '.class')):
499 graalRuntime_inline_hpp = join(_makeHotspotGeneratedSourcesDir(), 'graalRuntime.inline.hpp') 513 genSrcDir = _makeHotspotGeneratedSourcesDir()
514 graalRuntime_inline_hpp = join(genSrcDir, 'graalRuntime.inline.hpp')
515 cp = os.pathsep.join([mx.distribution(d).path for d in dist.distDependencies] + [dist.path, p.output_dir()])
500 tmp = StringIO.StringIO() 516 tmp = StringIO.StringIO()
501 mx.run_java(['-cp', '{}{}{}'.format(os.pathsep.join(jars), os.pathsep, p.output_dir()), mainClass], out=tmp.write) 517 mx.run_java(['-cp', cp, mainClass], out=tmp.write)
518
519 # Compute SHA1 for currently generated graalRuntime.inline.hpp content
520 # and all other generated sources in genSrcDir
521 d = hashlib.sha1()
522 d.update(tmp.getvalue())
523 for e in os.listdir(genSrcDir):
524 if e != 'graalRuntime.inline.hpp':
525 with open(join(genSrcDir, e)) as fp:
526 d.update(fp.read())
527 sha1 = d.hexdigest()
528
529 # Add SHA1 to end of graalRuntime.inline.hpp
530 print >> tmp, ''
531 print >> tmp, 'const char* GraalRuntime::_generated_sources_sha1 = "' + sha1 + '";'
532
502 mx.update_file(graalRuntime_inline_hpp, tmp.getvalue()) 533 mx.update_file(graalRuntime_inline_hpp, tmp.getvalue())
503 534
504 def _checkVMIsNewerThanGeneratedSources(jdk, vm, bld): 535 # Store SHA1 in generated Java class and append class to specified jar
505 if isGraalEnabled(vm) and (not _installed_jdks or _installed_jdks == _graal_home): 536 javaSource = join(_graal_home, 'GeneratedSourcesSha1.java')
506 vmLib = mx.TimeStampFile(join(_vmLibDirInJdk(jdk), vm, mx.add_lib_prefix(mx.add_lib_suffix('jvm')))) 537 javaClass = join(_graal_home, 'GeneratedSourcesSha1.class')
507 for name in ['graalRuntime.inline.hpp', 'HotSpotVMConfig.inline.hpp']: 538 with open (javaSource, 'w') as fp:
508 genSrc = join(_makeHotspotGeneratedSourcesDir(), name) 539 print >> fp, 'class GeneratedSourcesSha1 { private static final String value = "' + sha1 + '"; }'
509 if vmLib.isOlderThan(genSrc): 540 subprocess.check_call([mx.java().javac, '-d', _graal_home, javaSource], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
510 mx.log('The VM ' + vmLib.path + ' is older than ' + genSrc) 541 zf = zipfile.ZipFile(dist.path, 'a')
511 mx.abort('You need to run "mx --vm ' + vm + ' --vmbuild ' + bld + ' build"') 542 with open(javaClass, 'rb') as fp:
543 zf.writestr(os.path.basename(javaClass), fp.read())
544 zf.close()
545 os.unlink(javaSource)
546 os.unlink(javaClass)
512 547
513 def _installDistInJdks(dist): 548 def _installDistInJdks(dist):
514 """ 549 """
515 Installs the jar(s) for a given Distribution into all existing Graal JDKs 550 Installs the jar(s) for a given Distribution into all existing Graal JDKs
516 """ 551 """
517 552
518 distJar = dist.path
519 if dist.name == 'GRAAL_TRUFFLE': 553 if dist.name == 'GRAAL_TRUFFLE':
520 jars = [mx.distribution(d).path for d in dist.distDependencies] + [distJar] 554 _update_graalRuntime_inline_hpp(dist)
521 _update_graalRuntime_inline_hpp(jars)
522 jdks = _jdksDir() 555 jdks = _jdksDir()
523 556
524 if exists(jdks): 557 if exists(jdks):
525 for e in os.listdir(jdks): 558 for e in os.listdir(jdks):
526 jreLibDir = join(jdks, e, 'jre', 'lib') 559 jreLibDir = join(jdks, e, 'jre', 'lib')
542 shutil.copyfile(srcJar, tmp) 575 shutil.copyfile(srcJar, tmp)
543 os.close(fd) 576 os.close(fd)
544 shutil.move(tmp, dstJar) 577 shutil.move(tmp, dstJar)
545 os.chmod(dstJar, JDK_UNIX_PERMISSIONS_FILE) 578 os.chmod(dstJar, JDK_UNIX_PERMISSIONS_FILE)
546 579
547 install(distJar, jreLibDir) 580 install(dist.path, jreLibDir)
548 if dist.sourcesPath: 581 if dist.sourcesPath:
549 install(dist.sourcesPath, join(jdks, e)) 582 install(dist.sourcesPath, join(jdks, e))
550 583
551 # run a command in the windows SDK Debug Shell 584 # run a command in the windows SDK Debug Shell
552 def _runInDebugShell(cmd, workingDir, logFile=None, findInOutput=None, respondTo=None): 585 def _runInDebugShell(cmd, workingDir, logFile=None, findInOutput=None, respondTo=None):
918 mx.abort("conflicting working directories: do not set --vmcwd for this command") 951 mx.abort("conflicting working directories: do not set --vmcwd for this command")
919 952
920 build = vmbuild if vmbuild is not None else _vmbuild if _vmSourcesAvailable else 'product' 953 build = vmbuild if vmbuild is not None else _vmbuild if _vmSourcesAvailable else 'product'
921 jdk = _jdk(build, vmToCheck=vm, installJars=False) 954 jdk = _jdk(build, vmToCheck=vm, installJars=False)
922 _updateInstalledGraalOptionsFile(jdk) 955 _updateInstalledGraalOptionsFile(jdk)
923 _checkVMIsNewerThanGeneratedSources(jdk, vm, build)
924 mx.expand_project_in_args(args) 956 mx.expand_project_in_args(args)
925 if _make_eclipse_launch: 957 if _make_eclipse_launch:
926 mx.make_eclipse_launch(args, 'graal-' + build, name=None, deps=mx.project('com.oracle.graal.hotspot').all_deps([], True)) 958 mx.make_eclipse_launch(args, 'graal-' + build, name=None, deps=mx.project('com.oracle.graal.hotspot').all_deps([], True))
927 if _jacoco == 'on' or _jacoco == 'append': 959 if _jacoco == 'on' or _jacoco == 'append':
928 jacocoagent = mx.library("JACOCOAGENT", True) 960 jacocoagent = mx.library("JACOCOAGENT", True)