comparison mx/mx_graal_makefile.py @ 21488:6420ac0cbe3c

Add Makefile generator for building graal without mx
author Stefan Anzinger <stefan.anzinger@oracle.com>
date Tue, 26 May 2015 14:46:32 +0200
parents
children 93d486d51ab4
comparison
equal deleted inserted replaced
21487:6b59a0656841 21488:6420ac0cbe3c
1 import mx, os, sys
2 #
3 # ----------------------------------------------------------------------------------------------------
4 #
5 # Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
6 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 #
8 # This code is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License version 2 only, as
10 # published by the Free Software Foundation.
11 #
12 # This code is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 # version 2 for more details (a copy is included in the LICENSE file that
16 # accompanied this code).
17 #
18 # You should have received a copy of the GNU General Public License version
19 # 2 along with this work; if not, write to the Free Software Foundation,
20 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 #
22 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23 # or visit www.oracle.com if you need additional information or have any
24 # questions.
25 #
26 # ----------------------------------------------------------------------------------------------------
27 #
28
29
30 def build_makefile(args):
31 """Build a Makefile from the suitte.py to build graa.jar without python"""
32 if len(args) == 0 or args[0] == "-":
33 do_build_makefile(lambda l: sys.stdout.write(l + os.linesep))
34 elif args[0] == "-o":
35 with open(args[1], "w") as f:
36 do_build_makefile(lambda l: f.write(l + os.linesep))
37
38 def relative_dep_path(d):
39 if isinstance(d, str): d = mx.dependency(d)
40 return os.path.basename(d.get_path(False))
41
42 def createMakeRule(p, bootClasspath):
43 def filterDeps(deps, t):
44 def typeFilter(project): # filters
45 if isinstance(project, str):
46 project = mx.dependency(project, True)
47 return isinstance(project, t)
48 return [d for d in deps if typeFilter(d)]
49
50
51 canonicalDeps = p.canonical_deps()
52 canonicalProjectDep = filterDeps(canonicalDeps, mx.Project)
53 canonicalProjectDepDirs = ['$(TARGET)/' +i for i in canonicalProjectDep]
54 canonicalLibDep = filterDeps(canonicalDeps, mx.Library)
55 canonicalLibDepJars = ["$(LIB)/" + relative_dep_path(d) for d in canonicalLibDep]
56
57 allDep = p.all_deps([], True, False, includeAnnotationProcessors=True)
58 allProcessorDistNames = [x.definedAnnotationProcessorsDist.name for x in filterDeps(allDep, mx.Project) if x.definedAnnotationProcessors != None]
59 allProjectDep = filterDeps(allDep, mx.Project)
60 allProjectDepDir = ['$(TARGET)/' +i.name for i in allProjectDep]
61 allLibDep = filterDeps(allDep, mx.Library)
62 allLibDepJar = ["$(LIB)/" + relative_dep_path(d) for d in allLibDep]
63
64 processor = p.annotation_processors_path()
65 if processor != None: processor = processor.replace(p.suite.dir, "$(TARGET)")
66
67 cp = allLibDepJar +allProjectDepDir
68 props = {
69 'name': p.name,
70 'project_deps': ' '.join(canonicalProjectDepDirs + canonicalLibDepJars + allProcessorDistNames),
71 'cp_deps': ('-cp ' + ':'.join(cp)) if len(cp) > 0 else '',
72 'cp_boot': ('-bootclasspath ' + bootClasspath) if len(bootClasspath) > 0 else '',
73 'processor': ('-processorpath ' + processor) if processor != None else ''
74 }
75 return """$(TARGET)/{name}: $(shell find graal/{name}/src/ -type f -name *.java) {project_deps}
76 \t$(eval TMP := $(shell mktemp -d))
77 \ttest ! -d $(TARGET)/{name} || cp -Rp $(TARGET)/{name} $(TMP)
78 \t$(JAVAC) -d $(TMP) {cp_boot} {processor} {cp_deps} $(shell find graal/{name}/src/ -type f -name *.java)
79 \ttest ! -d graal/{name}/src/META-INF || (mkdir -p $(TARGET)/{name}/META-INF/ && cp -r graal/{name}/src/META-INF/ $(TARGET)/{name}/)
80 \tmkdir -p $(TARGET)/{name}
81 \tcp -r $(TMP)/* $(TARGET)/{name}
82 \ttouch $(TARGET)/{name}
83 \trm -r $(TMP)
84 """.format(**props)
85
86 def createDistributionRule(dist):
87 depDirs = ' '.join(['$(TARGET)/' + i.name for i in dist.sorted_deps(False, True)])
88 depDirsStar = ' '.join(['$(TARGET)/' + i.name + '/*' for i in dist.sorted_deps(False, True)])
89 jarPath = os.path.relpath(dist.path, dist.suite.dir)
90 jarDir = os.path.dirname(jarPath)
91 props = {
92 'dist_name': dist.name,
93 'depDirs': depDirs,
94 'depDirsStar': depDirsStar,
95 'jar_path': jarPath,
96 'jar_dir': jarDir,
97 'providers_dir': '$(TMP)/META-INF/providers/ ',
98 'services_dir': '$(TMP)/META-INF/services/'
99 }
100 return """{dist_name}: {depDirs}
101 \t$(eval TMP := $(shell mktemp -d))
102 \tmkdir -p $(TARGET){jar_dir}
103 \ttouch $(TARGET)/{jar_path}
104 \tcp -r {depDirsStar} $(TMP)
105 \ttest -d {services_dir} || mkdir -p {services_dir}
106 \ttest ! -d {providers_dir} || (cd {providers_dir} && for i in $$(ls); do c=$$(cat $$i); echo $$i >> {services_dir}$$c; done)
107 \ttest ! -d {providers_dir} || rm -r {providers_dir}
108 \t$(JAR) cvf $(TARGET){jar_path} -C $(TMP) .
109 \trm -r $(TMP)
110 """.format(**props)
111
112 def createDownloadRule(lib):
113 http_urls = [u for u in lib.urls if u.startswith("http")]
114 if len(http_urls) == 0: http_urls = [u for u in lib.urls if u.startswith("jar")]
115 if len(http_urls) == 0: raise BaseException("No http url specified for downloading library %s: available urls: %s" % (lib.name, lib.urls))
116 url = http_urls[0]
117 tofile = '$(LIB)/' + relative_dep_path(lib)
118 if url.startswith("jar"):
119 props = {
120 'url': url[url.find(":")+1:url.rfind("!")],
121 'archive_file': url[url.rfind("!")+1:],
122 'dest': tofile
123 }
124 dl = """\t$(eval TMP := $(shell mktemp -d))
125 \tcd $(TMP) && $(WGET) -O dl.zip {url} && $(JAR) xf dl.zip
126 \tmv $(TMP)/{archive_file} {dest}
127 \trm -rf $(TMP)""".format(**props)
128 else:
129 dl = "\t$(WGET) -O {} {}".format(tofile, url)
130 return """{}:\n{}""".format(tofile, dl)
131
132
133 def create_suite_build(suite, out):
134 for p in suite.projects:
135 java = mx.java(p.javaCompliance)
136 bootClassPath = java.bootclasspath()
137 bootClassPath = bootClassPath.replace(java.jdk, "$(JDK)")
138 out(createMakeRule(p, bootClassPath))
139 for l in suite.libs:
140 out(createDownloadRule(l))
141
142 distributionNames = []
143 for d in suite.dists:
144 distributionNames.append(d.name)
145 out(createDistributionRule(d))
146 out("{0}: {1}\n.PHONY: {1}".format(suite.name, " ".join(distributionNames)))
147
148
149 def do_build_makefile(out):
150 out("""VERBOSE=
151 TARGET=build/
152 LIB=$(TARGET)/lib
153 JDK=
154
155 WGET=wget
156 JAVAC=$(JDK)/bin/javac
157 JAR=$(JDK)/bin/jar
158
159
160 ifeq ($(JDK),)
161 $(error Variable JDK must be set to a JDK installation.)
162 endif
163 ifneq ($(VERBOSE),)
164 SHELL=sh -x
165 endif
166
167 all: default
168
169 $(TARGET):
170 \tmkdir -p $(TARGET)
171
172 $(LIB):
173 \tmkdir -p $(LIB)
174 """)
175 suiteNames = []
176 for s in mx.suites():
177 suiteNames.append(s.name)
178 create_suite_build(s, out)
179
180 out("""default: $(TARGET) $(LIB) {0}
181 .PHONY: {0}
182 """.format(" ".join(suiteNames)))
183