-
Notifications
You must be signed in to change notification settings - Fork 9
/
configure.py
executable file
·284 lines (233 loc) · 8.05 KB
/
configure.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
#!/usr/bin/env python
from __future__ import print_function
import ast
import optparse
import os
import pprint
import subprocess
import sys
script_dir = os.path.dirname(__file__)
ROOT_DIR = os.path.abspath(script_dir)
NODE_DIR = os.path.join(ROOT_DIR, 'deps', 'node')
def print_verbose(x, verbose=True):
if not verbose:
return
if type(x) is str:
print(x)
else:
pprint.pprint(x, indent=2)
def info(msg):
prefix = '\033[1m\033[32mINFO\033[0m' if os.isatty(1) else 'INFO'
print('%s: %s' % (prefix, msg))
def b(value):
'''Returns the string 'true' if value is truthy, 'false' otherwise.'''
return 'true' if value else 'false'
def n(value):
'''Returns the string '1' if value is truthy, '0' otherwise.'''
return '1' if value else '0'
def read_node_config_gypi(config_gypi_path):
with open(config_gypi_path, 'r') as f:
content = f.read()
return ast.literal_eval(content)
def get_git_hash():
git_rev = ""
git_diff = ""
try:
subprocess.call(
["git", "--version"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
query_git_rev = "echo $(git log --pretty=format:'%h' -n 1)"
query_git_diff = "echo $(git diff --quiet --exit-code || echo +)"
git_rev = (
subprocess.check_output(query_git_rev, shell=True).decode("utf-8").strip()
)
git_diff = (
subprocess.check_output(query_git_diff, shell=True).decode("utf-8").strip()
)
except Exception:
git_rev = "N/A"
return str(git_rev + git_diff if git_rev != ("" or "N/A") else "N/A")
def lwnode_gyp_opts(opts):
'''Returns GYP options.'''
args = []
# definitions (used: node)
args += ['-Dnode_core_target_name=lwnode']
args += ['-Dnode_lib_target_name=liblwnode']
args += ['-Dnode_obj_dir=obj/deps/node']
args += ['-Dlwnode_jsengine_path=' + ROOT_DIR]
# definitions (used: node && escargot)
args += ['-Dexternal_builtins=' + b(not opts.without_external_builtins)]
args += ['-Denable_reload_script=' + b(not opts.without_reload_script)]
# definitions (used: shim && escargot)
args += ['-Dtarget_os=' + ('tizen' if opts.tizen else 'linux')]
args += ['-Dprofile=' + str(opts.profile)] if opts.tizen else []
args += ['-Drevision=' + opts.revision]
# definitions (used: escargot)
args += ['-Descargot_build_mode=' + ('debug' if opts.debug else 'release')]
args += ['-Descargot_lib_type=' + str(opts.escargot_lib_type)]
args += ['-Descargot_threading=' + n(not opts.without_escargot_threading)]
args += ['-Descargot_debugger=' + n(opts.escargot_debugger)]
return args
def main(opts):
if opts.revision == "":
opts.revision = get_git_hash()
# 1. create `NODE_DIR/config.gypi`
configure_path = os.path.join(NODE_DIR, 'configure.py')
node_opts = "--lwnode --skip-node-gyp --ninja \
--dest-os=linux \
--without-bundled-v8 --without-node-code-cache \
--without-node-snapshot --without-inspector \
--without-npm --with-intl=none --shared-zlib".split()
node_opts += ['--debug', '--debug-node'] if opts.debug else []
node_opts += opts.node_more_opts
print_verbose('* node options', opts.verbose)
print_verbose(node_opts, opts.verbose)
subprocess.check_call([sys.executable, configure_path] + node_opts)
# 2. rewrite `NODE_DIR/config.gypi` to append the lwnode variables,
# which are accessible via `process.config` in Node.js JS side.
# e.g) `console.log(process.config.variables.javascript_engine)`
config_gypi_path = os.path.join(NODE_DIR, 'config.gypi')
config = read_node_config_gypi(config_gypi_path)
o = {}
o['javascript_engine'] = 'escargot'
o['lwnode_external_builtin_script'] = b(not opts.without_external_builtins)
o['lwnode_reload_script'] = b(not opts.without_reload_script)
o['lwnode_revision'] = opts.revision
v = config['variables']
print_verbose('* extends config.gypi', opts.verbose)
print_verbose(o, opts.verbose)
print_verbose(v, opts.verbose)
v.update(o)
# `gyp_args` is enabled when `NODE_DIR/config.gypi` is created with
# `--skip-node-gyp`. we remove it since it's given for the next step.
gyp_args = v['gyp_args']
del v['gyp_args']
do_not_edit = '# Do not edit. Generated by the lwnode configure script.\n'
with open(config_gypi_path, 'w') as f:
f.write(do_not_edit + pprint.pformat(config, indent=2) + '\n')
# 3. prepare gyp arguments
target_os = ('tizen' if opts.tizen else 'linux')
gen_build_dir = os.path.join(ROOT_DIR, 'out', target_os)
if opts.tizen:
gen_build_dir = os.path.join(gen_build_dir, opts.arch)
gyp_args += ['--depth=.']
gyp_args += ['--generator-output=' + gen_build_dir]
gyp_args += ['-Goutput_dir=' + gen_build_dir]
gyp_args += ['-Dcomponent=static_library']
gyp_args += ['-Dlibrary=static_library']
gyp_args += ['-I', os.path.join(NODE_DIR, 'common.gypi')]
gyp_args += ['-I', os.path.join(NODE_DIR, 'config.gypi')]
gyp_args += lwnode_gyp_opts(opts)
gyp_args += opts.gyp_more_opts
# 4. run gyp
gyp = os.path.join(NODE_DIR, 'tools/gyp/gyp')
gyp_build_file = 'lwnode.gyp'
command = [gyp, gyp_build_file] + gyp_args
print_verbose('* gyp command', opts.verbose)
print_verbose(command, opts.verbose)
try:
subprocess.check_call(command)
except subprocess.CalledProcessError:
print('Error running GYP')
print_verbose(' '.join(["%s" % str(x) for x in command]), True)
sys.exit(1)
info('configure completed successfully')
def setupCLIOptions(parser):
lwnode_optgroup = optparse.OptionGroup(
parser,
'Lightweight Node.js',
'Flags that allow you to control LWNode.js build options',
)
lwnode_optgroup.add_option(
'--revision',
dest="revision",
help="Set a revision string",
default="",
)
lwnode_optgroup.add_option(
'--tizen',
action='store_true',
dest='tizen',
default=False,
help='Build for Tizen Platform (%default)',
)
lwnode_optgroup.add_option(
'--profile',
choices=['common', 'tv', 'kiosk'],
default='common',
help='Tizen profile: common | tv | kiosk (%default)',
)
lwnode_optgroup.add_option(
'--without-external-builtins',
action='store_true',
dest='without_external_builtins',
default=False,
help='Disable external builtin scripts (%default)',
)
lwnode_optgroup.add_option(
'--without-reload-script',
action='store_true',
dest='without_reload_script',
default=False,
help='Disable Escargot script reloading (%default)',
)
lwnode_optgroup.add_option(
'--without-escargot-threading',
action='store_true',
dest='without_escargot_threading',
default=False,
help='Disable Escargot threading (%default)',
)
lwnode_optgroup.add_option(
'--escargot-lib-type',
choices=['shared_lib', 'static_lib'],
default='shared_lib',
help='shared_lib | static_lib (%default)',
)
lwnode_optgroup.add_option(
'--escargot-debugger',
action='store_true',
dest='escargot_debugger',
default=False,
help='Enable Escargot debugging (%default)',
)
lwnode_optgroup.add_option(
'--nopt',
action='append',
dest='node_more_opts',
default=[],
help='Append Node.js options. Can be used multiple times')
lwnode_optgroup.add_option(
'--gopt',
action='append',
dest='gyp_more_opts',
default=[],
help='Append GYP options. Can be used multiple times')
lwnode_optgroup.add_option(
'--debug',
action='store_true',
dest='debug',
default=False,
help='Debug build (%default)',
)
lwnode_optgroup.add_option(
'--arch',
dest='arch',
choices=['arm', 'x32', 'x64'],
default='x64',
help='Target architecture (%default)',
)
lwnode_optgroup.add_option(
'-v',
'--verbose',
action='store_true',
dest='verbose',
default=False,
help='Get more output from this script (%default)',
)
parser.add_option_group(lwnode_optgroup)
return parser
if __name__ == '__main__':
parser = setupCLIOptions(optparse.OptionParser())
(options, args) = parser.parse_args()
sys.exit(main(options))