-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildlib.py
344 lines (287 loc) · 10.6 KB
/
buildlib.py
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
'''
Copyright 2018, Infor Inc.
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.
'''
import os
import sys
import types
import argparse
g_args = {}
g_libpath = {}
def parse_arguments(prefix, deps = {}):
global g_args
parser = argparse.ArgumentParser(description='configure build of package')
parser.add_argument('--prefix', help='installation prefix', default=prefix)
for k, v in deps.items():
if isinstance(v, str):
default_path = v
else:
if os.getenv(v[0]) == None:
default_path = v[1]
else:
default_path = '$(' + v[0] + ')'
parser.add_argument('--with-' + k,
help='Installation prefix of ' + k,
metavar='DIR',
default=default_path)
g_args = parser.parse_args()
def open_makefile():
global g_makefile
g_makefile = open('Makefile', 'w')
emit('topdir = $(shell pwd)')
emit('prefix = ' + g_args.prefix)
emit('build = $(topdir)/build')
for k ,v in g_args.__dict__.items():
if k.startswith('with_'):
dep = k[len('with_'):]
emit(dep + ' = ' + v)
emit('rt_version = 3.9')
emit('bloxcompiler_flags = -runtimeVersion $(rt_version) -progress -explain')
def close_makefile():
'''Close the makefile'''
dist_files(['configure', 'buildlib.py'])
dist_target()
check_target()
install_target()
g_makefile.close()
def phony(target, makefile=None):
"""Declare a target to be a phony target, so that a file with that
name will not prevent make from executing that rule.
The target parameter can contain multiple targets as a single
space-delimited string."""
emit('.PHONY: ' + target, makefile=makefile)
def emit(line, makefile=None):
'''Add a line to the makefile'''
if makefile is None: makefile = g_makefile
makefile.write(line + '\n')
def flatten(seq):
"""Flattens a nested list or tuple.
Example:
>>> list(flatten([[1, 2], [3, 4]]))
[1, 2, 3, 4]
"""
for v0 in seq:
# Don't recurse again unless it's a collection
if isinstance(v0, (list, tuple)):
for v1 in flatten(v0):
yield v1
else:
yield v0
def rule(output, input, commands=None, separator=':', makefile=None):
if not isinstance(output, list):
output = [ output ]
if not isinstance(input, list):
input = [ input ]
if commands is None:
commands = []
if not isinstance(commands, list):
commands = [ commands ]
commands = list(flatten(commands))
last_output = len(output) - 1
for i, trg in enumerate(output):
tab = '\t' if i > 0 else ''
sep = (' %s' % separator) if i == last_output else ''
cont = ' \\' #if len(output) > 1 and i != last_output else ''
emit(tab + trg + sep + cont, makefile=makefile)
last_input = len(input) - 1
for dep in input:
tab = '\t'
cont = ' \\'
emit(tab + dep + cont, makefile=makefile)
emit('', makefile=makefile)
if len(commands):
for cmd in commands:
emit('\t%s' % cmd, makefile=makefile)
# Always add a blank line at the end for readability.
emit('', makefile=makefile)
def find_files(dirname, extension):
'''utility to find all files with the given extension (e.g. .java) in a directory.'''
result = []
for dirpath, dirnames, filenames in os.walk(dirname):
for filename in filenames:
f = os.path.join(dirpath, filename)
if f.endswith(extension):
result.append(f)
return result
def check_jar(name, main, srcdir = None, srcdirs = [], classpath = [], srcgen = [], javadoc = None):
jar(name, srcdir, srcdirs, classpath, srcgen, javadoc, install = False)
jar_file = '$(build)/jars/' + name + '.jar'
rule(
output = 'check-' + name,
input = [jar_file],
commands = [
'java -cp ' + ':'.join(classpath) + ':' + jar_file + ' ' + main],
separator = '::')
rule(
output = 'check',
input = ['check-' + name],
separator = '::')
def jar(name,
srcdir = None,
srcdirs = [],
classpath = [],
srcgen = [],
javadoc = None,
resources = [],
install = True,
javac_flags = "",
java_version = "11",
manifest=None):
'''Build a jar by compiling Java files with javac'''
java_files = []
if srcdir != None:
srcdirs = [srcdir] + srcdirs
for d in srcdirs:
java_files.extend(find_files(d, '.java'))
dist_files(java_files)
dist_files(resources)
java_files.extend(srcgen)
jar_file = '$(build)/jars/' + name + '.jar'
classes_dir = '$(build)/jars/' + name + '.classes'
emit(jar_file + ': ' + ' '.join(java_files) + ' ' + ' '.join(resources))
emit('\tmkdir -p ' + classes_dir)
emit('\tjavac -Xlint:deprecation ' + javac_flags + ' -d ' + classes_dir +
' -source ' + java_version +
' -target ' + java_version +
' -cp ' + ':'.join(classpath) + ':' + classes_dir +
' ' +
' '.join(java_files))
for f in resources:
emit('\tcp ' + f + ' ' + classes_dir)
if manifest is not None:
manifest_file = '%s/Manifest.txt' % classes_dir
# clear out previous file
emit("\tcat /dev/null > %s" % manifest_file)
if manifest.get('add_classpath'):
manifest_classpath = [os.path.split(p)[1] for p in classpath] # gets filename only
# need to write the classpath this way to avoid a "line to long" error in the jar command
if len(manifest_classpath) >= 1:
emit("\techo 'Class-Path: %s' >> %s" % (manifest_classpath.pop(), manifest_file))
for cp in manifest_classpath:
emit("\techo ' %s' >> %s" % (cp, manifest_file))
if manifest.get('main_class'):
emit("\techo 'Main-Class: %s' >> %s" % (manifest.get('main_class'), manifest_file))
if manifest.get('package_version'):
for header, definition in manifest.get('package_version'):
emit("\techo '%s: %s' >> %s" % (header, definition, manifest_file))
# manifest files need empty line at end
emit("\techo ' ' >> %s" % manifest_file)
emit('\t(cd ' + classes_dir + '; ' +
'jar cfm ../' + name + '.jar Manifest.txt .)')
else:
emit('\t(cd ' + classes_dir + '; ' +
'jar cf ../' + name + '.jar' + ' .)')
emit_clean_file(jar_file)
emit_clean_dir(classes_dir)
rule(
output = 'jars',
input = jar_file,
separator = '::')
if install:
install_file(jar_file, '$(prefix)/lib/java')
if javadoc != None:
javadoc_dir = '$(build)/javadoc/' + name
emit(javadoc_dir + ' : ' + ' '.join(java_files))
emit('\tjavadoc ' +
' -classpath ' + ':'.join(classpath) + ':' + classes_dir +
' -windowtitle "' + javadoc['title'] + '"' +
' -doctitle "' + javadoc['title'] + '"' +
' -public ' +
# 2020.06.11 linkoffline causes a hard error now that the package-list files were removed in commit c18736e
#' -linkoffline "http://docs.oracle.com/javase/7/docs/api/" "./package-list/java"' +
#' -linkoffline "http://docs.guava-libraries.googlecode.com/git/javadoc/" "./package-list/guava"' +
#' -linkoffline "http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/" "./package-list/aws"' +
' -d ' + javadoc_dir +
' ' + ' '.join(java_files))
if install:
install_dir(javadoc_dir, '$(prefix)/docs/api/' + name)
def bin_program(name):
install_file(name, '$(prefix)/bin')
dist_files([name])
def config_file(name):
install_file(name, '$(prefix)/config')
dist_files([name])
def install_files(filenames, destdir):
for f in filenames:
install_file(f, destdir)
def install_file(filename, destdir):
rule(
output = 'install',
input = filename,
commands = 'mkdir -p $(DESTDIR)' + destdir + ' && cp ' + filename + ' $(DESTDIR)' + destdir,
separator = '::')
def install_dir(dirname, destdir):
rule(
output = 'install',
input = dirname,
commands = 'mkdir -p $(DESTDIR)' + destdir + ' && cp -R ' + dirname + '/* $(DESTDIR)' + destdir,
separator = '::')
def dist_files(files):
'''make a list of files part of the source distribution'''
emit('dist_files :: dist_dir')
for f in files:
emit('\tcp --parents --target-directory $(package_name) ' + f)
def dist_target():
rule(
output='dist_dir',
input=[],
commands=[
'rm -rf $(package_name)',
'mkdir -p $(package_name)'
],
separator=':')
rule(
output='dist',
input=['dist_files'],
commands=[
'tar zcvf $(package_name).tar.gz $(package_name)',
'rm -rf $(package_name)'
],
separator=':')
rule(
output='distcheck',
input=['dist'],
commands=[
'rm -rf $(build)/distcheck',
'rm -rf $(build)/prefix',
'mkdir -p $(build)/distcheck',
'cd $(build)/distcheck && ' + \
'tar zxf ../../$(package_name).tar.gz',
'cd $(build)/distcheck/$(package_name) &&' + \
'./configure --prefix=$(build)/prefix',
'cd $(build)/distcheck/$(package_name) && ' \
'$(MAKE)',
'cd $(build)/distcheck/$(package_name) && ' \
'$(MAKE) install',
],
separator=':')
def check_target():
rule(
output = 'check',
input = [],
separator='::')
def install_target():
rule(
output = 'install',
input = [],
separator='::')
def emit_clean_dir(dir):
rule(
output='clean',
input=[],
commands='rm -rf ' + dir,
separator='::')
def emit_clean_file(file):
rule(
output='clean',
input=[],
commands='rm -f ' + file,
separator='::')