-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmake.py
executable file
·348 lines (244 loc) · 9.42 KB
/
make.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
345
346
347
348
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Make tool
Usage:
make.py run <task-name>
make.py [options]
make.py -h | --help
Options:
-h --help Show this screen.
-d --debug Enable debug mode.
--version Show version.
Examples:
python make.py -h
Tasks:
- build-pdfium
- install-ios
- build-ios
- build-depot-tools
- build-chromium
"""
import os
import shutil
import stat
import sys
import tarfile
import glob
from docopt import docopt
from slugify import slugify
from tqdm import tqdm
from subprocess import call
from shutil import copyfile, copytree
import urllib.request as urllib2
import urllib.parse as urlparse
def main(options):
make_debug = False
make_task = ''
ios_archs = ['arm64', 'arm', 'x86', 'x64']
#ios_archs = ['arm']
ios_configurations = ['release']
# show all params for debug
if ('--debug' in options and options['--debug']) or ('-d' in options and options['-d']):
make_debug = True
if make_debug:
debug('You have executed with options:')
message(str(options))
message('')
# bind options
if '<task-name>' in options:
make_task = options['<task-name>']
# validate data
debug('Validating data...')
# validate task
if not make_task:
error('Task is invalid')
# build pdfium
if make_task == 'build-pdfium':
run_task_build_pdfium()
# build chromium
elif make_task == 'build-chromium':
run_task_build_chromium()
# build depot tools
elif make_task == 'build-depot-tools':
run_task_build_depot_tools()
# install ios
elif make_task == 'install-ios':
run_task_install_ios(
ios_archs=ios_archs,
ios_configurations=ios_configurations
)
# build ios
elif make_task == 'build-ios':
run_task_build_ios(
ios_archs=ios_archs,
ios_configurations=ios_configurations
)
message('')
debug('FINISHED!')
def run_task_build_pdfium():
debug('Building PDFIUM...')
remove_dir(os.path.join('pdfium'))
dtools_dir = os.path.join('depot_tools')
gclient_tool = os.path.join(dtools_dir, 'gclient')
command = ' '.join([gclient_tool, 'config', '--unmanaged', 'https://pdfium.googlesource.com/pdfium.git'])
call(command, shell=True)
command = ' '.join([gclient_tool, 'sync'])
call(command, shell=True)
remove_dir(os.path.join('pdfium', 'testing', 'iossim'))
remove_dir(os.path.join('pdfium', 'testing', 'gtest_ios'))
copytree(os.path.join('chromium', 'testing', 'iossim'), os.path.join('pdfium', 'testing', 'iossim'))
copytree(os.path.join('chromium', 'testing', 'gtest_ios'), os.path.join('pdfium', 'testing', 'gtest_ios'))
def run_task_build_chromium():
debug('Building Chromium...')
remove_dir(os.path.join('chromium'))
command = ' '.join(['git', 'clone', 'https://github.com/chromium/chromium.git'])
call(command, shell=True)
def run_task_build_depot_tools():
debug('Building Depot Tools...')
remove_dir(os.path.join('depot_tools'))
command = ' '.join(['git', 'clone', 'https://chromium.googlesource.com/chromium/tools/depot_tools.git'])
call(command, shell=True)
def run_task_install_ios(ios_archs, ios_configurations):
debug('Installing iOS libraries...')
# configs
for config in ios_configurations:
remove_dir(os.path.join('build', 'ios', config))
create_dir(os.path.join('build', 'ios', config))
# archs
for arch in ios_archs:
folder = os.path.join('pdfium', 'out', '{0}-{1}'.format(config, arch), 'obj', '**', '*.a')
files = glob.glob(folder, recursive=True)
files_str = ' '.join(files)
lib_file_out = os.path.join('build', 'ios', config, 'libpdfium_{0}.a'.format(arch))
command = ' '.join(['libtool', '-static', files_str, '-o', lib_file_out])
call(command, shell=True)
# universal
folder = os.path.join('build', 'ios', config, '*.a')
files = glob.glob(folder)
files_str = ' '.join(files)
lib_file_out = os.path.join('build', 'ios', config, 'libpdfium.a')
command = ' '.join(['lipo', '-create', files_str, '-o', lib_file_out])
call(command, shell=True)
# only to test in my machine
#copyfile(lib_file_out, '/Users/paulo/Downloads/UXReader-iOS/UXReader/UXReader/PDFium/libpdfium.a')
def run_task_build_ios(ios_archs, ios_configurations):
debug('Building iOS libraries...')
current_dir = os.getcwd()
dtools_dir = os.path.join(current_dir, 'depot_tools')
gn_tool = os.path.join(dtools_dir, 'gn')
# configs
for config in ios_configurations:
# archs
for arch in ios_archs:
main_dir = os.path.join('pdfium', 'out', '{0}-{1}'.format(config, arch))
remove_dir(main_dir)
create_dir(main_dir)
os.chdir(os.path.join('pdfium'))
# generating files...
debug('Generating files to arch "{0}" and configuration "{1}"...'.format(arch, config))
arg_is_debug = ('true' if config == 'debug' else 'false')
args = 'target_os="ios" target_cpu="{0}" use_goma=false is_debug={1} pdf_use_skia=false pdf_use_skia_paths=false pdf_enable_xfa=false pdf_enable_v8=false pdf_is_standalone=true is_component_build=false clang_use_chrome_plugins=false ios_enable_code_signing=false symbol_level=0 pdf_is_complete_lib=true strip_debug_info=true enable_stripping=true ios_deployment_target="8.0"'.format(arch, arg_is_debug)
command = ' '.join([gn_tool, 'gen', 'out/{0}-{1}'.format(config, arch), '--args=\'{0}\''.format(args)])
call(command, shell=True)
# compiling...
debug('Compiling to arch "{0}" and configuration "{1}"...'.format(arch, config))
command = ' '.join(['ninja', '-C', 'out/{0}-{1}'.format(config, arch), 'pdfium'])
call(command, shell=True)
os.chdir(current_dir)
def debug(msg):
print('> {0}'.format(msg))
def message(msg):
print('{0}'.format(msg))
def error(msg):
print('ERROR: {0}'.format(msg))
sys.exit(1)
def download_file(url, dest=None):
"""
Download and save a file specified by url to dest directory,
"""
u = urllib2.urlopen(url)
scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
filename = os.path.basename(path)
if not filename:
filename = 'downloaded.file'
if dest:
filename = os.path.join(dest, filename)
with open(filename, 'wb') as f:
debug('Downloading...')
message('')
meta = u.info()
meta_func = meta.getheaders if hasattr(meta, 'getheaders') else meta.get_all
meta_length = meta_func('Content-Length')
file_size = None
pbar = None
if meta_length:
file_size = int(meta_length[0])
if file_size:
pbar = tqdm(total=file_size)
file_size_dl = 0
block_sz = 8192
while True:
dbuffer = u.read(block_sz)
if not dbuffer:
break
dbuffer_len = len(dbuffer)
file_size_dl += dbuffer_len
f.write(dbuffer)
if pbar:
pbar.update(dbuffer_len)
if pbar:
pbar.close()
message('')
return filename
def get_download_filename(url):
scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
filename = os.path.basename(path)
if not filename:
filename = 'downloaded.file'
return filename
def list_subdirs(from_path):
dirs = filter(lambda x: os.path.isdir(os.path.join(from_path, x)), os.listdir(from_path))
return dirs
def remove_all_files(base_path, files_to_remove):
for file_to_remove in files_to_remove:
try:
file_to_remove = os.path.join(base_path, file_to_remove)
if os.path.isdir(file_to_remove):
shutil.rmtree(file_to_remove)
else:
os.remove(file_to_remove)
except IOError as e:
# we will ignore this message, is not important now
# debug('Error removing file: {0} - {1}'.format(file_to_remove, e.strerror))
pass
except OSError as e:
# we will ignore this message, is not important now
# debug('Error removing file: {0} - {1}'.format(file_to_remove, e.strerror))
pass
def create_dir(dir_path):
if not os.path.isdir(dir_path):
os.makedirs(dir_path)
def remove_dir(dir_path):
if os.path.isdir(dir_path):
shutil.rmtree(dir_path)
def remove_file(filename):
if os.path.isfile(filename):
os.remove(filename)
def make_tarfile(output_filename, source_dir):
with tarfile.open(output_filename, 'w:gz') as tar:
tar.add(source_dir, arcname=os.path.basename(source_dir))
def write_to_file(dirname, filename, content):
full_file_path = os.path.join(dirname, filename)
remove_file(full_file_path)
create_dir(dirname)
with open(full_file_path, 'w') as f:
f.write(content)
f.close()
def find_files(directory, pattern):
files = [f for (dir, subdirs, fs) in os.walk(directory) for f in fs if f.endswith(pattern)]
return files
if __name__ == '__main__':
# main CLI entrypoint
args = docopt(__doc__, version='1.0.0')
main(args)