diff mx/mx_graal_makefile.py @ 21718:b5bbf03bc17a

Improve makefile generator, exporting files into shared dir (JBS:GRAAL-52)
author Stefan Anzinger <stefan.anzinger@oracle.com>
date Thu, 04 Jun 2015 03:21:58 +0200
parents a3315bce5192
children 73809f97c281
line wrap: on
line diff
--- a/mx/mx_graal_makefile.py	Wed Jun 03 20:24:04 2015 +0200
+++ b/mx/mx_graal_makefile.py	Thu Jun 04 03:21:58 2015 +0200
@@ -24,7 +24,7 @@
 #
 # ----------------------------------------------------------------------------------------------------
 #
-import mx, os
+import mx, mx_graal, os
 from argparse import ArgumentParser, REMAINDER
 
 
@@ -32,16 +32,16 @@
     def __init__(self):
         self.rules = []
         self.definitions = []
-        
+
     def add_rule(self, s):
         self.rules.append(s)
-        
+
     def add_definition(self, s):
         self.definitions.append(s)
-        
+
     def generate(self):
         return "\n\n".join(self.definitions + self.rules)
-    
+
 
 def build_makefile(args):
     """Creates a Makefile which is able to build distributions without mx"""
@@ -49,7 +49,7 @@
     parser.add_argument('-o', action='store', dest='output', help='Write contents to this file.')
     parser.add_argument('selectedDists', help="Selected distribution names which are going to be built with make.", nargs=REMAINDER)
     args = parser.parse_args(args)
-    
+
     if args.selectedDists == None or len(args.selectedDists) == 0:
         parser.print_help()
         return
@@ -61,7 +61,7 @@
         else:
             with open(args.output, "w") as f:
                 f.write(contents)
-    
+
 
 def filter_projects(deps, t):
     def typeFilter(project): # filters
@@ -70,27 +70,31 @@
         return isinstance(project, t)
     return [d for d in deps if typeFilter(d)]
 
+def get_jdk_deployed_dists():
+    return [d.name for d in mx_graal._jdkDeployedDists]
 
 def make_dist_rule(dist, mf, bootClassPath=None):
     def path_dist_relative(p):
         return os.path.relpath(p, dist.suite.dir)
+    jdkDeployedDists = get_jdk_deployed_dists()
     jarPath = path_dist_relative(dist.path)
     sourcesVariableName = dist.name + "_SRC"
-    depJarVariableName = dist.name + "_DEP_JARS";
+    depJarVariableName = dist.name + "_DEP_JARS"
     sources = []
     resources = []
     sortedDeps = dist.sorted_deps(True, transitive=False, includeAnnotationProcessors=True)
     projects = filter_projects(sortedDeps, mx.Project)
     targetPathPrefix = "$(TARGET)" + os.path.sep
     libraryDeps = [path_dist_relative(l.get_path(False)) for l in filter_projects(sortedDeps, mx.Library)]
-    
+
     annotationProcessorDeps = set()
     distDeps = dist.get_dist_deps(includeSelf=False, transitive=True)
     distDepProjects = set()
-    for d in distDeps: 
+    for d in distDeps:
         distDepProjects.update(d.sorted_deps(includeLibs=False, transitive=True))
-    classPath = [targetPathPrefix + path_dist_relative(d.path) for d in distDeps] + libraryDeps
-    
+
+    classPath = [targetPathPrefix + path_dist_relative(d.path) for d in distDeps] + libraryDeps \
+        + [path_dist_relative(mx.dependency(name).path) for name in dist.excludedDependencies]
     for p in projects:
         if p.definedAnnotationProcessors != None and p.definedAnnotationProcessorsDist != dist:
             annotationProcessorDeps.add(p)
@@ -98,25 +102,25 @@
         projectDir = path_dist_relative(p.dir)
         if p not in distDepProjects and p not in annotationProcessorDeps:
             generatedSource = [path_dist_relative(p.source_gen_dir())] if len(annotationProcessorDeps) > 0 else []
-            
+
             for d in p.srcDirs + generatedSource:
                 src = projectDir + os.path.sep + d
-                sources.append("$(shell find {} -type file -name *.java 2> /dev/null)".format(src))
-                metaInf = src + os.path.sep + "META-INF";
+                sources.append("$(shell find {} -type f -name *.java 2> /dev/null)".format(src))
+                metaInf = src + os.path.sep + "META-INF"
                 if os.path.exists(metaInf):
                     resources.append(metaInf)
-        
-            
+
+
     sourceLines = sourcesVariableName + " = " + ("\n" + sourcesVariableName + " += ").join(sources)
-    annotationProcessorPaths = []
-    annotationProcessorDistNames = []
-    annotationProcessorDistVariableNames = []
+    apPaths = []
+    apDistNames = []
+    apDistVariableNames = []
     for p in annotationProcessorDeps:
-        annotationProcessorPaths.append(path_dist_relative(p.definedAnnotationProcessorsDist.path))
+        apPaths.append(path_dist_relative(p.definedAnnotationProcessorsDist.path))
         name = p.definedAnnotationProcessorsDist.name
-        annotationProcessorDistNames.append(name)
-        annotationProcessorDistVariableNames.append("$(" + name + "_JAR)")
-    
+        apDistNames.append(name)
+        apDistVariableNames.append("$(" + name + "_JAR)")
+    shouldExport = dist.name in jdkDeployedDists
     props = {
            "name": dist.name,
            "jarPath": targetPathPrefix + jarPath,
@@ -125,35 +129,38 @@
            "depJarsVariable": depJarVariableName,
            "sourceLines": sourceLines,
            "sourcesVariableName": sourcesVariableName,
-           "annotationProcessors": " ".join(annotationProcessorDistVariableNames),
-           "cpAnnotationProcessors": "-processorpath " + ":".join(annotationProcessorDistVariableNames) if len(annotationProcessorDistVariableNames) > 0 else "",
+           "annotationProcessors": " ".join(apDistVariableNames),
+           "cpAnnotationProcessors": "-processorpath " + ":".join(apDistVariableNames) if len(apDistVariableNames) > 0 else "",
            "bootCp": ("-bootclasspath " + bootClassPath) if bootClassPath != None else "",
            "cpDeps": ("-cp " + ":".join(classPath)) if len(classPath) > 0 else "",
            "jarDeps": " ".join(classPath),
            "copyResources": "cp -r {} $(TMP)".format(" ".join(resources)) if len(resources) > 0 else "",
-           "targetPathPrefix": targetPathPrefix
+           "targetPathPrefix": targetPathPrefix,
+           "shouldExport": shouldExport,
            }
-    
+
     mf.add_definition(sourceLines)
     mf.add_definition("{name}_JAR = {jarPath}".format(**props))
     if len(classPath) > 0: mf.add_definition("{depJarsVariable} = {jarDeps}".format(**props))
-    mf.add_rule("""$({name}_JAR): $({sourcesVariableName}) {annotationProcessors} {depJarsVariableAccess} $(TARGET)/build
+    if shouldExport: mf.add_definition("EXPORTED_FILES += $({name}_JAR)".format(**props))
+    mf.add_rule("""$({name}_JAR): $({sourcesVariableName}) {annotationProcessors} {depJarsVariableAccess}
 \t$(eval TMP := $(shell mktemp -d))
 \t$(JAVAC) -d $(TMP) {cpAnnotationProcessors} {bootCp} {cpDeps} $({sourcesVariableName})
 \t{copyResources}
-\t$(call process_options,$(TMP))
+\t$(call process_options,$(TMP),{shouldExport})
 \tmkdir -p $$(dirname $({name}_JAR))
 \t$(JAR) cf $({name}_JAR) -C $(TMP) .
 \trm -r $(TMP)""".format(**props))
     return
 
 
+
 def do_build_makefile(mf, selectedDists):
     java = mx.java()
     bootClassPath = java.bootclasspath()
     bootClassPath = bootClassPath.replace(java.jdk, "$(JDK)")
     jdkBootClassPathVariableName = "JDK_BOOTCLASSPATH"
-    
+
     mf.add_definition("""VERBOSE=
 TARGET=.
 JDK=
@@ -162,34 +169,57 @@
 JAVAC=$(JDK)/bin/javac -g -target """ + str(java.javaCompliance) + """
 JAR=$(JDK)/bin/jar
 
+EXPORT_DIR=export
+EXPORTED_FILES_ADDITIONAL=$(TARGET)/options $(TARGET)/services
+HS_COMMON_SRC=.
+# where all other stuff built by mx (graal.jar) resides
+MX_TARGET=.
+PROVIDERS_INF=/META-INF/providers/
+SERVICES_INF=/META-INF/services/
+OPTIONS_INF=/META-INF/options/
 
 ifeq ($(JDK),)
-$(error Variable JDK must be set to a JDK installation.)
-endif
-ifneq ($(VERBOSE),)
-SHELL=sh -x
+    $(error Variable JDK must be set to a JDK installation.)
+    endif
+    ifneq ($(VERBOSE),)
+    SHELL=sh -x
 endif
 
 define process_options =
-$(eval providers=$(1)/META-INF/providers/)
-$(eval services=$(1)/META-INF/services/)
-test -d $(services) || mkdir -p $(services)
-test ! -d $(providers) ||   (cd $(providers) && for i in $$(ls $(providers)); do c=$$(cat $$i); echo $$i >> $(services)$$c; rm $$i; done)
+    $(eval providers=$(1)/$(PROVIDERS_INF))
+    $(eval services=$(1)/$(SERVICES_INF))
+    $(eval options=$(1)/$(OPTIONS_INF))
+    test -d $(services) || mkdir -p $(services)
+    test ! -d $(providers) || (cd $(providers) && for i in $$(ls $(providers)); do c=$$(cat $$i); echo $$i >> $(services)$$c; rm $$i; done)
+
+    # We're building all projects together with one javac call; thus we cannot determine, from which project the generated file is thus we hardcode it for now
+    $(eval vmconfig=$(1)/hotspot/HotSpotVMConfig.inline.hpp)
+    $(eval vmconfigDest=$(HS_COMMON_SRC)/../graal/com.oracle.jvmci.hotspot/src_gen/hotspot)
+    test ! -f $(vmconfig) || (mkdir -p $(vmconfigDest) && cp $(vmconfig) $(vmconfigDest))
 endef
 
+define extract =
+    $(eval TMP := $(shell mktemp -d))
+    mkdir -p $(2);
+    cd $(TMP) && $(JAR) xf $(abspath $(1)) && \
+        ((test ! -d .$(SERVICES_INF) || cp -r .$(SERVICES_INF) $(abspath $(2))) &&  (test ! -d .$(OPTIONS_INF) || cp -r .$(OPTIONS_INF) $(abspath $(2))))
+    rm -r $(TMP)
+    cp $(1) $(2)
+endef
+
+
 all: default
 
-$(TARGET)/build:
-\tmkdir -p $(TARGET)/build
+export: all
+\tmkdir -p $(EXPORT_DIR)
+\t$(foreach export,$(EXPORTED_FILES),$(call extract,$(export),$(EXPORT_DIR)))
+.PHONY: export
 
-$(LIB):
-\tmkdir -p $(LIB)
 """)
     s = mx.suite("graal")
     dists = set()
     ap = set()
     projects = set()
-
     for d in s.dists:
         if d.name in selectedDists:
             dists.update(d.get_dist_deps(True, True))
@@ -200,7 +230,12 @@
             if d.definedAnnotationProcessorsDist != None:
                 apd = d.definedAnnotationProcessorsDist
                 ap.add(apd)
-    
+
+    for d in mx_graal._jdkDeployedDists:
+        dist = mx.distribution(d.name)
+        if dist not in dists: # this sould go away
+            mf.add_definition("EXPORTED_FILES += $(MX_TARGET)/{}".format(os.path.relpath(dist.path, dist.suite.dir)))
+
     if len(dists) > 0:
         mf.add_definition(jdkBootClassPathVariableName + " = " + bootClassPath)
         bootClassPathVarAccess = "$(" + jdkBootClassPathVariableName + ")"
@@ -212,8 +247,3 @@
         for d in dists:
             selectedDists.remove(d.name)
         print "Distribution(s) '" + "', '".join(selectedDists) + "' does not exist."
-            
-    
-            
-    
-