Merge remote-tracking branch 'origin/v0.8'
[platform/upstream/nodejs.git] / configure
1 #!/usr/bin/env python
2 import optparse
3 import os
4 import pprint
5 import re
6 import subprocess
7 import sys
8
9 CC = os.environ.get('CC', 'cc')
10
11 root_dir = os.path.dirname(__file__)
12 sys.path.insert(0, os.path.join(root_dir, 'deps', 'v8', 'tools'))
13
14 # parse our options
15 parser = optparse.OptionParser()
16
17 parser.add_option("--debug",
18     action="store_true",
19     dest="debug",
20     help="Also build debug build")
21
22 parser.add_option("--prefix",
23     action="store",
24     dest="prefix",
25     help="Select the install prefix (defaults to /usr/local)")
26
27 parser.add_option("--without-npm",
28     action="store_true",
29     dest="without_npm",
30     help="Don\'t install the bundled npm package manager")
31
32 parser.add_option("--without-ssl",
33     action="store_true",
34     dest="without_ssl",
35     help="Build without SSL")
36
37 parser.add_option("--without-snapshot",
38     action="store_true",
39     dest="without_snapshot",
40     help="Build without snapshotting V8 libraries. You might want to set"
41          " this for cross-compiling. [Default: False]")
42
43 parser.add_option("--shared-v8",
44     action="store_true",
45     dest="shared_v8",
46     help="Link to a shared V8 DLL instead of static linking")
47
48 parser.add_option("--shared-v8-includes",
49     action="store",
50     dest="shared_v8_includes",
51     help="Directory containing V8 header files")
52
53 parser.add_option("--shared-v8-libpath",
54     action="store",
55     dest="shared_v8_libpath",
56     help="A directory to search for the shared V8 DLL")
57
58 parser.add_option("--shared-v8-libname",
59     action="store",
60     dest="shared_v8_libname",
61     help="Alternative lib name to link to (default: 'v8')")
62
63 parser.add_option("--shared-openssl",
64     action="store_true",
65     dest="shared_openssl",
66     help="Link to a shared OpenSSl DLL instead of static linking")
67
68 parser.add_option("--shared-openssl-includes",
69     action="store",
70     dest="shared_openssl_includes",
71     help="Directory containing OpenSSL header files")
72
73 parser.add_option("--shared-openssl-libpath",
74     action="store",
75     dest="shared_openssl_libpath",
76     help="A directory to search for the shared OpenSSL DLLs")
77
78 parser.add_option("--shared-openssl-libname",
79     action="store",
80     dest="shared_openssl_libname",
81     help="Alternative lib name to link to (default: 'crypto,ssl')")
82
83 # deprecated
84 parser.add_option("--openssl-use-sys",
85     action="store_true",
86     dest="shared_openssl",
87     help=optparse.SUPPRESS_HELP)
88
89 # deprecated
90 parser.add_option("--openssl-includes",
91     action="store",
92     dest="shared_openssl_includes",
93     help=optparse.SUPPRESS_HELP)
94
95 # deprecated
96 parser.add_option("--openssl-libpath",
97     action="store",
98     dest="shared_openssl_libpath",
99     help=optparse.SUPPRESS_HELP)
100
101 parser.add_option("--no-ssl2",
102     action="store_true",
103     dest="no_ssl2",
104     help="Disable OpenSSL v2")
105
106 parser.add_option("--shared-zlib",
107     action="store_true",
108     dest="shared_zlib",
109     help="Link to a shared zlib DLL instead of static linking")
110
111 parser.add_option("--shared-zlib-includes",
112     action="store",
113     dest="shared_zlib_includes",
114     help="Directory containing zlib header files")
115
116 parser.add_option("--shared-zlib-libpath",
117     action="store",
118     dest="shared_zlib_libpath",
119     help="A directory to search for the shared zlib DLL")
120
121 parser.add_option("--shared-zlib-libname",
122     action="store",
123     dest="shared_zlib_libname",
124     help="Alternative lib name to link to (default: 'z')")
125
126 parser.add_option("--with-dtrace",
127     action="store_true",
128     dest="with_dtrace",
129     help="Build with DTrace (default is true on supported systems)")
130
131 parser.add_option("--without-dtrace",
132     action="store_true",
133     dest="without_dtrace",
134     help="Build without DTrace")
135
136 parser.add_option("--with-etw",
137     action="store_true",
138     dest="with_etw",
139     help="Build with ETW (default is true on Windows)")
140
141 parser.add_option("--without-etw",
142     action="store_true",
143     dest="without_etw",
144     help="Build without ETW")
145
146 # CHECKME does this still work with recent releases of V8?
147 parser.add_option("--gdb",
148     action="store_true",
149     dest="gdb",
150     help="add gdb support")
151
152 parser.add_option("--dest-cpu",
153     action="store",
154     dest="dest_cpu",
155     help="CPU architecture to build for. Valid values are: arm, ia32, x64")
156
157 parser.add_option("--no-ifaddrs",
158     action="store_true",
159     dest="no_ifaddrs",
160     help="Use on deprecated SunOS systems that do not support ifaddrs.h")
161
162 parser.add_option("--with-arm-float-abi",
163     action="store",
164     dest="arm_float_abi",
165     help="Specifies which floating-point ABI to use. Valid values are: "
166          "soft, softfp, hard")
167
168 (options, args) = parser.parse_args()
169
170
171 def b(value):
172   """Returns the string 'true' if value is truthy, 'false' otherwise."""
173   if value:
174     return 'true'
175   else:
176     return 'false'
177
178
179 def pkg_config(pkg):
180   cmd = os.popen('pkg-config --libs %s' % pkg, 'r')
181   libs = cmd.readline().strip()
182   ret = cmd.close()
183   if (ret): return None
184
185   cmd = os.popen('pkg-config --cflags %s' % pkg, 'r')
186   cflags = cmd.readline().strip()
187   ret = cmd.close()
188   if (ret): return None
189
190   return (libs, cflags)
191
192
193 def cc_macros():
194   """Checks predefined macros using the CC command."""
195
196   try:
197     p = subprocess.Popen(CC.split() + ['-dM', '-E', '-'],
198                          stdin=subprocess.PIPE,
199                          stdout=subprocess.PIPE,
200                          stderr=subprocess.PIPE)
201   except OSError:
202     print '''Node.js configure error: No acceptable C compiler found!
203
204         Please make sure you have a C compiler installed on your system and/or
205         consider adjusting the CC environment variable if you installed
206         it in a non-standard prefix.
207         '''
208     sys.exit()
209
210   p.stdin.write('\n')
211   out = p.communicate()[0]
212
213   out = str(out).split('\n')
214
215   k = {}
216   for line in out:
217     import shlex
218     lst = shlex.split(line)
219     if len(lst) > 2:
220       key = lst[1]
221       val = lst[2]
222       k[key] = val
223   return k
224
225
226 def is_arch_armv7():
227   """Check for ARMv7 instructions"""
228   cc_macros_cache = cc_macros()
229   return ('__ARM_ARCH_7__' in cc_macros_cache or
230           '__ARM_ARCH_7A__' in cc_macros_cache or
231           '__ARM_ARCH_7R__' in cc_macros_cache or
232           '__ARM_ARCH_7M__' in cc_macros_cache)
233
234
235 def arm_hard_float_abi():
236   """Check for hardfloat or softfloat eabi on ARM"""
237   # GCC versions 4.6 and above define __ARM_PCS or __ARM_PCS_VFP to specify
238   # the Floating Point ABI used (PCS stands for Procedure Call Standard).
239   # We use these as well as a couple of other defines to statically determine
240   # what FP ABI used.
241   # GCC versions 4.4 and below don't support hard-fp.
242   # GCC versions 4.5 may support hard-fp without defining __ARM_PCS or
243   # __ARM_PCS_VFP.
244
245   if compiler_version() >= (4, 6, 0):
246     return '__ARM_PCS_VFP' in cc_macros()
247   elif compiler_version() < (4, 5, 0):
248     return False
249   elif '__ARM_PCS_VFP' in cc_macros():
250     return True
251   elif ('__ARM_PCS' in cc_macros() or
252         '__SOFTFP' in cc_macros() or
253         not '__VFP_FP__' in cc_macros()):
254     return False
255   else:
256     print '''Node.js configure error: Your version of GCC does not report
257       the Floating-Point ABI to compile for your hardware
258
259       Please manually specify which floating-point ABI to use with the
260       --with-arm-float-abi option.
261       '''
262     sys.exit()
263
264
265 def host_arch_cc():
266   """Host architecture check using the CC command."""
267
268   k = cc_macros()
269
270   matchup = {
271     '__x86_64__'  : 'x64',
272     '__i386__'    : 'ia32',
273     '__arm__'     : 'arm',
274   }
275
276   rtn = 'ia32' # default
277
278   for i in matchup:
279     if i in k and k[i] != '0':
280       rtn = matchup[i]
281       break
282
283   return rtn
284
285
286 def host_arch_win():
287   """Host architecture check using environ vars (better way to do this?)"""
288
289   arch = os.environ.get('PROCESSOR_ARCHITECTURE', 'x86')
290
291   matchup = {
292     'AMD64'  : 'x64',
293     'x86'    : 'ia32',
294     'arm'    : 'arm',
295   }
296
297   return matchup.get(arch, 'ia32')
298
299
300 def compiler_version():
301   try:
302     proc = subprocess.Popen(CC.split() + ['--version'], stdout=subprocess.PIPE)
303   except WindowsError:
304     return (0, False)
305
306   is_clang = 'clang' in proc.communicate()[0].split('\n')[0]
307
308   proc = subprocess.Popen(CC.split() + ['-dumpversion'], stdout=subprocess.PIPE)
309   version = tuple(map(int, proc.communicate()[0].split('.')))
310
311   return (version, is_clang)
312
313
314 def configure_node(o):
315   # TODO add gdb
316   o['variables']['v8_no_strict_aliasing'] = 1 # work around compiler bugs
317   o['variables']['node_prefix'] = os.path.expanduser(options.prefix or '')
318   o['variables']['node_install_npm'] = b(not options.without_npm)
319   o['default_configuration'] = 'Debug' if options.debug else 'Release'
320
321   host_arch = host_arch_win() if os.name == 'nt' else host_arch_cc()
322   target_arch = options.dest_cpu or host_arch
323   o['variables']['host_arch'] = host_arch
324   o['variables']['target_arch'] = target_arch
325
326   # V8 on ARM requires that armv7 is set. CPU Model detected by
327   # the presence of __ARM_ARCH_7__ and the like defines in compiler
328   if target_arch == 'arm':
329     if options.arm_float_abi:
330       hard_float = options.arm_float_abi == 'hard'
331     else:
332       hard_float = arm_hard_float_abi()
333     o['variables']['v8_use_arm_eabi_hardfloat'] = b(hard_float)
334     o['variables']['armv7'] = 1 if is_arch_armv7() else 0
335
336   cc_version, is_clang = compiler_version()
337   o['variables']['clang'] = 1 if is_clang else 0
338
339   if not is_clang and cc_version != 0:
340     o['variables']['gcc_version'] = 10 * cc_version[0] + cc_version[1]
341
342   # clang has always supported -fvisibility=hidden, right?
343   if not is_clang and cc_version < (4,0,0):
344     o['variables']['visibility'] = ''
345
346   # By default, enable DTrace on SunOS systems. Don't allow it on other
347   # systems, since it won't work.  (The MacOS build process is different than
348   # SunOS, and we haven't implemented it.)
349   if sys.platform.startswith('sunos'):
350     o['variables']['node_use_dtrace'] = b(not options.without_dtrace)
351   elif b(options.with_dtrace) == 'true':
352     raise Exception('DTrace is currently only supported on SunOS systems.')
353   else:
354     o['variables']['node_use_dtrace'] = 'false'
355
356   if options.no_ifaddrs:
357     o['defines'] += ['SUNOS_NO_IFADDRS']
358
359   # By default, enable ETW on Windows.
360   if sys.platform.startswith('win32'):
361     o['variables']['node_use_etw'] = b(not options.without_etw);
362   elif b(options.with_etw) == 'true':
363     raise Exception('ETW is only supported on Windows.')
364   else:
365     o['variables']['node_use_etw'] = 'false'
366
367
368 def configure_libz(o):
369   o['variables']['node_shared_zlib'] = b(options.shared_zlib)
370
371   # assume shared_zlib if one of these is set?
372   if options.shared_zlib_libpath:
373     o['libraries'] += ['-L%s' % options.shared_zlib_libpath]
374   if options.shared_zlib_libname:
375     o['libraries'] += ['-l%s' % options.shared_zlib_libname]
376   elif options.shared_zlib:
377     o['libraries'] += ['-lz']
378   if options.shared_zlib_includes:
379     o['include_dirs'] += [options.shared_zlib_includes]
380
381
382 def configure_v8(o):
383   o['variables']['v8_use_snapshot'] = b(not options.without_snapshot)
384   o['variables']['node_shared_v8'] = b(options.shared_v8)
385
386   # assume shared_v8 if one of these is set?
387   if options.shared_v8_libpath:
388     o['libraries'] += ['-L%s' % options.shared_v8_libpath]
389   if options.shared_v8_libname:
390     o['libraries'] += ['-l%s' % options.shared_v8_libname]
391   elif options.shared_v8:
392     o['libraries'] += ['-lv8']
393   if options.shared_v8_includes:
394     o['include_dirs'] += [options.shared_v8_includes]
395
396
397 def configure_openssl(o):
398   o['variables']['node_use_openssl'] = b(not options.without_ssl)
399   o['variables']['node_shared_openssl'] = b(options.shared_openssl)
400
401   if options.without_ssl:
402     return
403
404   if options.no_ssl2:
405     o['defines'] += ['OPENSSL_NO_SSL2=1']
406
407   if options.shared_openssl:
408     (libs, cflags) = pkg_config('openssl') or ('-lssl -lcrypto', '')
409
410     if options.shared_openssl_libpath:
411       o['libraries'] += ['-L%s' % options.shared_openssl_libpath]
412
413     if options.shared_openssl_libname:
414       libnames = options.shared_openssl_libname.split(',')
415       o['libraries'] += ['-l%s' % s for s in libnames]
416     else:
417       o['libraries'] += libs.split()
418
419     if options.shared_openssl_includes:
420       o['include_dirs'] += [options.shared_openssl_includes]
421     else:
422       o['cflags'] += cflags.split()
423
424
425 output = {
426   'variables': {},
427   'include_dirs': [],
428   'libraries': [],
429   'defines': [],
430   'cflags': [],
431 }
432
433 configure_node(output)
434 configure_libz(output)
435 configure_v8(output)
436 configure_openssl(output)
437
438 # variables should be a root level element,
439 # move everything else to target_defaults
440 variables = output['variables']
441 del output['variables']
442 output = {
443   'variables': variables,
444   'target_defaults': output
445 }
446 pprint.pprint(output, indent=2)
447
448 def write(filename, data):
449   filename = os.path.join(root_dir, filename)
450   print "creating ", filename
451   f = open(filename, 'w+')
452   f.write(data)
453
454 write('config.gypi', "# Do not edit. Generated by the configure script.\n" +
455   pprint.pformat(output, indent=2) + "\n")
456
457 write('config.mk', "# Do not edit. Generated by the configure script.\n" +
458   ("BUILDTYPE=%s\n" % ('Debug' if options.debug else 'Release')))
459
460 if os.name == 'nt':
461   gyp_args = ['-f', 'msvs', '-G', 'msvs_version=2010']
462 else:
463   gyp_args = ['-f', 'make']
464
465 subprocess.call([sys.executable, 'tools/gyp_node'] + gyp_args)