configure: make --v8-options switch more robust
[platform/upstream/nodejs.git] / configure
1 #!/usr/bin/env python
2 import optparse
3 import os
4 import pprint
5 import re
6 import shlex
7 import subprocess
8 import sys
9
10 CC = os.environ.get('CC', 'cc')
11
12 root_dir = os.path.dirname(__file__)
13 sys.path.insert(0, os.path.join(root_dir, 'tools', 'gyp', 'pylib'))
14 from gyp.common import GetFlavor
15
16 # parse our options
17 parser = optparse.OptionParser()
18
19 # Options should be in alphabetical order but keep --prefix at the top,
20 # that's arguably the one people will be looking for most.
21 parser.add_option('--prefix',
22     action='store',
23     dest='prefix',
24     help='select the install prefix (defaults to /usr/local)')
25
26 parser.add_option('--debug',
27     action='store_true',
28     dest='debug',
29     help='also build debug build')
30
31 parser.add_option('--dest-cpu',
32     action='store',
33     dest='dest_cpu',
34     help='CPU architecture to build for. Valid values are: arm, ia32, x64')
35
36 parser.add_option('--dest-os',
37     action='store',
38     dest='dest_os',
39     help='operating system to build for. Valid values are: '
40          'win, mac, solaris, freebsd, openbsd, linux, android')
41
42 parser.add_option('--gdb',
43     action='store_true',
44     dest='gdb',
45     help='add gdb support')
46
47 parser.add_option('--ninja',
48     action='store_true',
49     dest='use_ninja',
50     help='generate files for the ninja build system')
51
52 parser.add_option('--no-ifaddrs',
53     action='store_true',
54     dest='no_ifaddrs',
55     help='use on deprecated SunOS systems that do not support ifaddrs.h')
56
57 # deprecated
58 parser.add_option('--openssl-includes',
59     action='store',
60     dest='shared_openssl_includes',
61     help=optparse.SUPPRESS_HELP)
62
63 # deprecated
64 parser.add_option('--openssl-libpath',
65     action='store',
66     dest='shared_openssl_libpath',
67     help=optparse.SUPPRESS_HELP)
68
69 # deprecated
70 parser.add_option('--openssl-use-sys',
71     action='store_true',
72     dest='shared_openssl',
73     help=optparse.SUPPRESS_HELP)
74
75 parser.add_option('--shared-cares',
76     action='store_true',
77     dest='shared_cares',
78     help='link to a shared cares DLL instead of static linking')
79
80 parser.add_option('--shared-cares-includes',
81     action='store',
82     dest='shared_cares_includes',
83     help='directory containing cares header files')
84
85 parser.add_option('--shared-cares-libname',
86     action='store',
87     dest='shared_cares_libname',
88     help='alternative lib name to link to (default: \'cares\')')
89
90 parser.add_option('--shared-cares-libpath',
91     action='store',
92     dest='shared_cares_libpath',
93     help='a directory to search for the shared cares DLL')
94
95 parser.add_option('--shared-http-parser',
96     action='store_true',
97     dest='shared_http_parser',
98     help='link to a shared http_parser DLL instead of static linking')
99
100 parser.add_option('--shared-http-parser-includes',
101     action='store',
102     dest='shared_http_parser_includes',
103     help='directory containing http_parser header files')
104
105 parser.add_option('--shared-http-parser-libname',
106     action='store',
107     dest='shared_http_parser_libname',
108     help='alternative lib name to link to (default: \'http_parser\')')
109
110 parser.add_option('--shared-http-parser-libpath',
111     action='store',
112     dest='shared_http_parser_libpath',
113     help='a directory to search for the shared http_parser DLL')
114
115 parser.add_option('--shared-libuv',
116     action='store_true',
117     dest='shared_libuv',
118     help='link to a shared libuv DLL instead of static linking')
119
120 parser.add_option('--shared-libuv-includes',
121     action='store',
122     dest='shared_libuv_includes',
123     help='directory containing libuv header files')
124
125 parser.add_option('--shared-libuv-libname',
126     action='store',
127     dest='shared_libuv_libname',
128     help='alternative lib name to link to (default: \'uv\')')
129
130 parser.add_option('--shared-libuv-libpath',
131     action='store',
132     dest='shared_libuv_libpath',
133     help='a directory to search for the shared libuv DLL')
134
135 parser.add_option('--shared-openssl',
136     action='store_true',
137     dest='shared_openssl',
138     help='link to a shared OpenSSl DLL instead of static linking')
139
140 parser.add_option('--shared-openssl-includes',
141     action='store',
142     dest='shared_openssl_includes',
143     help='directory containing OpenSSL header files')
144
145 parser.add_option('--shared-openssl-libname',
146     action='store',
147     dest='shared_openssl_libname',
148     help='alternative lib name to link to (default: \'crypto,ssl\')')
149
150 parser.add_option('--shared-openssl-libpath',
151     action='store',
152     dest='shared_openssl_libpath',
153     help='a directory to search for the shared OpenSSL DLLs')
154
155 parser.add_option('--shared-v8',
156     action='store_true',
157     dest='shared_v8',
158     help='link to a shared V8 DLL instead of static linking')
159
160 parser.add_option('--shared-v8-includes',
161     action='store',
162     dest='shared_v8_includes',
163     help='directory containing V8 header files')
164
165 parser.add_option('--shared-v8-libname',
166     action='store',
167     dest='shared_v8_libname',
168     help='alternative lib name to link to (default: \'v8\')')
169
170 parser.add_option('--shared-v8-libpath',
171     action='store',
172     dest='shared_v8_libpath',
173     help='a directory to search for the shared V8 DLL')
174
175 parser.add_option('--shared-zlib',
176     action='store_true',
177     dest='shared_zlib',
178     help='link to a shared zlib DLL instead of static linking')
179
180 parser.add_option('--shared-zlib-includes',
181     action='store',
182     dest='shared_zlib_includes',
183     help='directory containing zlib header files')
184
185 parser.add_option('--shared-zlib-libname',
186     action='store',
187     dest='shared_zlib_libname',
188     help='alternative lib name to link to (default: \'z\')')
189
190 parser.add_option('--shared-zlib-libpath',
191     action='store',
192     dest='shared_zlib_libpath',
193     help='a directory to search for the shared zlib DLL')
194
195 # TODO document when we've decided on what the tracing API and its options will
196 # look like
197 parser.add_option('--systemtap-includes',
198     action='store',
199     dest='systemtap_includes',
200     help=optparse.SUPPRESS_HELP)
201
202 parser.add_option('--tag',
203     action='store',
204     dest='tag',
205     help='custom build tag')
206
207 parser.add_option('--v8-options',
208     action='store',
209     dest='v8_options',
210     help='v8 options to pass, see `node --v8-options` for examples.')
211
212 parser.add_option('--with-arm-float-abi',
213     action='store',
214     dest='arm_float_abi',
215     help='specifies which floating-point ABI to use. Valid values are: '
216          'soft, softfp, hard')
217
218 parser.add_option('--with-dtrace',
219     action='store_true',
220     dest='with_dtrace',
221     help='build with DTrace (default is true on sunos)')
222
223 parser.add_option('--with-etw',
224     action='store_true',
225     dest='with_etw',
226     help='build with ETW (default is true on Windows)')
227
228 parser.add_option('--with-icu-path',
229     action='store',
230     dest='with_icu_path',
231     help='Path to icu.gyp (ICU i18n, Chromium version only.)')
232
233 parser.add_option('--with-perfctr',
234     action='store_true',
235     dest='with_perfctr',
236     help='build with performance counters (default is true on Windows)')
237
238 parser.add_option('--with-sslv2',
239     action='store_true',
240     dest='with_sslv2',
241     help='enable SSL v2')
242
243 parser.add_option('--without-dtrace',
244     action='store_true',
245     dest='without_dtrace',
246     help='build without DTrace')
247
248 parser.add_option('--without-etw',
249     action='store_true',
250     dest='without_etw',
251     help='build without ETW')
252
253 parser.add_option('--without-npm',
254     action='store_true',
255     dest='without_npm',
256     help='don\'t install the bundled npm package manager')
257
258 parser.add_option('--without-perfctr',
259     action='store_true',
260     dest='without_perfctr',
261     help='build without performance counters')
262
263 parser.add_option('--without-snapshot',
264     action='store_true',
265     dest='without_snapshot',
266     help='build without snapshotting V8 libraries. You might want to set'
267          ' this for cross-compiling. [Default: False]')
268
269 parser.add_option('--without-ssl',
270     action='store_true',
271     dest='without_ssl',
272     help='build without SSL')
273
274 parser.add_option('--xcode',
275     action='store_true',
276     dest='use_xcode',
277     help='generate build files for use with xcode')
278
279 (options, args) = parser.parse_args()
280
281
282 def b(value):
283   """Returns the string 'true' if value is truthy, 'false' otherwise."""
284   if value:
285     return 'true'
286   else:
287     return 'false'
288
289
290 def pkg_config(pkg):
291   cmd = os.popen('pkg-config --libs %s' % pkg, 'r')
292   libs = cmd.readline().strip()
293   ret = cmd.close()
294   if (ret): return None
295
296   cmd = os.popen('pkg-config --cflags %s' % pkg, 'r')
297   cflags = cmd.readline().strip()
298   ret = cmd.close()
299   if (ret): return None
300
301   return (libs, cflags)
302
303
304 def cc_macros():
305   """Checks predefined macros using the CC command."""
306
307   try:
308     p = subprocess.Popen(shlex.split(CC) + ['-dM', '-E', '-'],
309                          stdin=subprocess.PIPE,
310                          stdout=subprocess.PIPE,
311                          stderr=subprocess.PIPE)
312   except OSError:
313     print '''Node.js configure error: No acceptable C compiler found!
314
315         Please make sure you have a C compiler installed on your system and/or
316         consider adjusting the CC environment variable if you installed
317         it in a non-standard prefix.
318         '''
319     sys.exit()
320
321   p.stdin.write('\n')
322   out = p.communicate()[0]
323
324   out = str(out).split('\n')
325
326   k = {}
327   for line in out:
328     lst = shlex.split(line)
329     if len(lst) > 2:
330       key = lst[1]
331       val = lst[2]
332       k[key] = val
333   return k
334
335
336 def is_arch_armv7():
337   """Check for ARMv7 instructions"""
338   cc_macros_cache = cc_macros()
339   return ('__ARM_ARCH_7__' in cc_macros_cache or
340           '__ARM_ARCH_7A__' in cc_macros_cache or
341           '__ARM_ARCH_7R__' in cc_macros_cache or
342           '__ARM_ARCH_7M__' in cc_macros_cache)
343
344
345 def is_arm_neon():
346   """Check for ARM NEON support"""
347   return '__ARM_NEON__' in cc_macros()
348
349
350 def is_arm_hard_float_abi():
351   """Check for hardfloat or softfloat eabi on ARM"""
352   # GCC versions 4.6 and above define __ARM_PCS or __ARM_PCS_VFP to specify
353   # the Floating Point ABI used (PCS stands for Procedure Call Standard).
354   # We use these as well as a couple of other defines to statically determine
355   # what FP ABI used.
356   # GCC versions 4.4 and below don't support hard-fp.
357   # GCC versions 4.5 may support hard-fp without defining __ARM_PCS or
358   # __ARM_PCS_VFP.
359
360   if compiler_version() >= (4, 6, 0):
361     return '__ARM_PCS_VFP' in cc_macros()
362   elif compiler_version() < (4, 5, 0):
363     return False
364   elif '__ARM_PCS_VFP' in cc_macros():
365     return True
366   elif ('__ARM_PCS' in cc_macros() or
367         '__SOFTFP' in cc_macros() or
368         not '__VFP_FP__' in cc_macros()):
369     return False
370   else:
371     print '''Node.js configure error: Your version of GCC does not report
372       the Floating-Point ABI to compile for your hardware
373
374       Please manually specify which floating-point ABI to use with the
375       --with-arm-float-abi option.
376       '''
377     sys.exit()
378
379
380 def host_arch_cc():
381   """Host architecture check using the CC command."""
382
383   k = cc_macros()
384
385   matchup = {
386     '__x86_64__'  : 'x64',
387     '__i386__'    : 'ia32',
388     '__arm__'     : 'arm',
389     '__mips__'    : 'mips',
390   }
391
392   rtn = 'ia32' # default
393
394   for i in matchup:
395     if i in k and k[i] != '0':
396       rtn = matchup[i]
397       break
398
399   return rtn
400
401
402 def host_arch_win():
403   """Host architecture check using environ vars (better way to do this?)"""
404
405   arch = os.environ.get('PROCESSOR_ARCHITECTURE', 'x86')
406
407   matchup = {
408     'AMD64'  : 'x64',
409     'x86'    : 'ia32',
410     'arm'    : 'arm',
411     'mips'   : 'mips',
412   }
413
414   return matchup.get(arch, 'ia32')
415
416
417 def compiler_version():
418   try:
419     proc = subprocess.Popen(shlex.split(CC) + ['--version'],
420                             stdout=subprocess.PIPE)
421   except WindowsError:
422     return (0, False)
423
424   is_clang = 'clang' in proc.communicate()[0].split('\n')[0]
425
426   proc = subprocess.Popen(shlex.split(CC) + ['-dumpversion'],
427                           stdout=subprocess.PIPE)
428   version = tuple(map(int, proc.communicate()[0].split('.')))
429
430   return (version, is_clang)
431
432
433 def configure_arm(o):
434   if options.arm_float_abi:
435     arm_float_abi = options.arm_float_abi
436   elif is_arm_hard_float_abi():
437     arm_float_abi = 'hard'
438   else:
439     arm_float_abi = 'default'
440   o['variables']['armv7'] = int(is_arch_armv7())
441   o['variables']['arm_fpu'] = 'vfpv3'  # V8 3.18 no longer supports VFP2.
442   o['variables']['arm_neon'] = int(is_arm_neon())
443   o['variables']['arm_thumb'] = 0      # -marm
444   o['variables']['arm_float_abi'] = arm_float_abi
445
446
447 def configure_node(o):
448   if options.dest_os == 'android':
449     o['variables']['OS'] = 'android'
450   o['variables']['node_prefix'] = os.path.expanduser(options.prefix or '')
451   o['variables']['node_install_npm'] = b(not options.without_npm)
452   o['default_configuration'] = 'Debug' if options.debug else 'Release'
453
454   host_arch = host_arch_win() if os.name == 'nt' else host_arch_cc()
455   target_arch = options.dest_cpu or host_arch
456   o['variables']['host_arch'] = host_arch
457   o['variables']['target_arch'] = target_arch
458
459   if target_arch == 'arm':
460     configure_arm(o)
461
462   cc_version, is_clang = compiler_version()
463   o['variables']['clang'] = 1 if is_clang else 0
464
465   if not is_clang and cc_version != 0:
466     o['variables']['gcc_version'] = 10 * cc_version[0] + cc_version[1]
467
468   # clang has always supported -fvisibility=hidden, right?
469   if not is_clang and cc_version < (4,0,0):
470     o['variables']['visibility'] = ''
471
472   if flavor in ('solaris', 'mac', 'linux', 'freebsd'):
473     use_dtrace = not options.without_dtrace
474     # Don't enable by default on linux and freebsd
475     if flavor in ('linux', 'freebsd'):
476       use_dtrace = options.with_dtrace
477
478     if flavor == 'linux':
479       if options.systemtap_includes:
480         o['include_dirs'] += [options.systemtap_includes]
481     o['variables']['node_use_dtrace'] = b(use_dtrace)
482     o['variables']['uv_use_dtrace'] = b(use_dtrace)
483     o['variables']['uv_parent_path'] = '/deps/uv/'
484   elif options.with_dtrace:
485     raise Exception(
486        'DTrace is currently only supported on SunOS, MacOS or Linux systems.')
487   else:
488     o['variables']['node_use_dtrace'] = 'false'
489
490   # if we're on illumos based systems wrap the helper library into the
491   # executable
492   if flavor == 'solaris':
493     o['variables']['node_use_mdb'] = 'true'
494   else:
495     o['variables']['node_use_mdb'] = 'false'
496
497   if options.no_ifaddrs:
498     o['defines'] += ['SUNOS_NO_IFADDRS']
499
500   # By default, enable ETW on Windows.
501   if flavor == 'win':
502     o['variables']['node_use_etw'] = b(not options.without_etw)
503   elif options.with_etw:
504     raise Exception('ETW is only supported on Windows.')
505   else:
506     o['variables']['node_use_etw'] = 'false'
507
508   # By default, enable Performance counters on Windows.
509   if flavor == 'win':
510     o['variables']['node_use_perfctr'] = b(not options.without_perfctr)
511   elif options.with_perfctr:
512     raise Exception('Performance counter is only supported on Windows.')
513   else:
514     o['variables']['node_use_perfctr'] = 'false'
515
516   if options.tag:
517     o['variables']['node_tag'] = '-' + options.tag
518   else:
519     o['variables']['node_tag'] = ''
520
521   if options.v8_options:
522     o['variables']['node_v8_options'] = options.v8_options.replace('"', '\\"')
523
524
525 def configure_libz(o):
526   o['variables']['node_shared_zlib'] = b(options.shared_zlib)
527
528   # assume shared_zlib if one of these is set?
529   if options.shared_zlib_libpath:
530     o['libraries'] += ['-L%s' % options.shared_zlib_libpath]
531   if options.shared_zlib_libname:
532     o['libraries'] += ['-l%s' % options.shared_zlib_libname]
533   elif options.shared_zlib:
534     o['libraries'] += ['-lz']
535   if options.shared_zlib_includes:
536     o['include_dirs'] += [options.shared_zlib_includes]
537
538
539 def configure_http_parser(o):
540     o['variables']['node_shared_http_parser'] = b(options.shared_http_parser)
541
542     # assume shared http_parser if one of these is set?
543     if options.shared_http_parser_libpath:
544         o['libraries'] += ['-L%s' % options.shared_http_parser_libpath]
545     if options.shared_http_parser_libname:
546         o['libraries'] += ['-l%s' % options.shared_http_parser_libname]
547     elif options.shared_http_parser:
548         o['libraries'] += ['-lhttp_parser']
549     if options.shared_http_parser_includes:
550         o['include_dirs'] += [options.shared_http_parser_includes]
551
552
553 def configure_cares(o):
554     o['variables']['node_shared_cares'] = b(options.shared_cares)
555
556     # assume shared cares if one of these is set?
557     if options.shared_cares_libpath:
558         o['libraries'] += ['-L%s' % options.shared_cares_libpath]
559     if options.shared_cares_libname:
560         o['libraries'] += ['-l%s' % options.shared_cares_libname]
561     elif options.shared_cares:
562         o['libraries'] += ['-lcares']
563     if options.shared_cares_includes:
564         o['include_dirs'] += [options.shared_cares_includes]
565
566
567 def configure_libuv(o):
568   o['variables']['node_shared_libuv'] = b(options.shared_libuv)
569
570   # assume shared libuv if one of these is set?
571   if options.shared_libuv_libpath:
572     o['libraries'] += ['-L%s' % options.shared_libuv_libpath]
573   else:
574     o['variables']['uv_library'] = 'static_library'
575
576   if options.shared_libuv_libname:
577     o['libraries'] += ['-l%s' % options.shared_libuv_libname]
578   elif options.shared_libuv:
579     o['libraries'] += ['-luv']
580   if options.shared_libuv_includes:
581     o['include_dirs'] += [options.shared_libuv_includes]
582
583
584 def configure_v8(o):
585   o['variables']['node_shared_v8'] = b(options.shared_v8)
586   o['variables']['v8_enable_gdbjit'] = 1 if options.gdb else 0
587   o['variables']['v8_no_strict_aliasing'] = 1  # Work around compiler bugs.
588   o['variables']['v8_optimized_debug'] = 0  # Compile with -O0 in debug builds.
589   o['variables']['v8_random_seed'] = 0  # Use a random seed for hash tables.
590   o['variables']['v8_use_snapshot'] = b(not options.without_snapshot)
591
592   # assume shared_v8 if one of these is set?
593   if options.shared_v8_libpath:
594     o['libraries'] += ['-L%s' % options.shared_v8_libpath]
595   if options.shared_v8_libname:
596     o['libraries'] += ['-l%s' % options.shared_v8_libname]
597   elif options.shared_v8:
598     o['libraries'] += ['-lv8']
599   if options.shared_v8_includes:
600     o['include_dirs'] += [options.shared_v8_includes]
601
602
603 def configure_openssl(o):
604   o['variables']['node_use_openssl'] = b(not options.without_ssl)
605   o['variables']['node_shared_openssl'] = b(options.shared_openssl)
606
607   if options.without_ssl:
608     return
609
610   # OpenSSL uses `#ifndef OPENSSL_NO_SSL2` checks so only define the
611   # macro when we want to _disable_ SSL2.
612   if not options.with_sslv2:
613     o['defines'] += ['OPENSSL_NO_SSL2=1']
614
615   if options.shared_openssl:
616     (libs, cflags) = pkg_config('openssl') or ('-lssl -lcrypto', '')
617
618     if options.shared_openssl_libpath:
619       o['libraries'] += ['-L%s' % options.shared_openssl_libpath]
620
621     if options.shared_openssl_libname:
622       libnames = options.shared_openssl_libname.split(',')
623       o['libraries'] += ['-l%s' % s for s in libnames]
624     else:
625       o['libraries'] += libs.split()
626
627     if options.shared_openssl_includes:
628       o['include_dirs'] += [options.shared_openssl_includes]
629     else:
630       o['cflags'] += cflags.split()
631
632
633 def configure_winsdk(o):
634   if flavor != 'win':
635     return
636
637   winsdk_dir = os.environ.get('WindowsSdkDir')
638
639   if winsdk_dir and os.path.isfile(winsdk_dir + '\\bin\\ctrpp.exe'):
640     print('Found ctrpp in WinSDK--will build generated files '
641           'into tools/msvs/genfiles.')
642     o['variables']['node_has_winsdk'] = 'true'
643     return
644
645   print('ctrpp not found in WinSDK path--using pre-gen files '
646         'from tools/msvs/genfiles.')
647
648
649 def configure_icu(o):
650   have_icu_path = bool(options.with_icu_path)
651   o['variables']['v8_enable_i18n_support'] = int(have_icu_path)
652   if have_icu_path:
653     o['variables']['icu_gyp_path'] = options.with_icu_path
654
655
656 # determine the "flavor" (operating system) we're building for,
657 # leveraging gyp's GetFlavor function
658 flavor_params = {}
659 if (options.dest_os):
660   flavor_params['flavor'] = options.dest_os
661 flavor = GetFlavor(flavor_params)
662
663 output = {
664   'variables': { 'python': sys.executable },
665   'include_dirs': [],
666   'libraries': [],
667   'defines': [],
668   'cflags': [],
669 }
670
671 configure_node(output)
672 configure_libz(output)
673 configure_http_parser(output)
674 configure_cares(output)
675 configure_libuv(output)
676 configure_v8(output)
677 configure_openssl(output)
678 configure_winsdk(output)
679 configure_icu(output)
680
681 # variables should be a root level element,
682 # move everything else to target_defaults
683 variables = output['variables']
684 del output['variables']
685 output = {
686   'variables': variables,
687   'target_defaults': output
688 }
689 pprint.pprint(output, indent=2)
690
691 def write(filename, data):
692   filename = os.path.join(root_dir, filename)
693   print 'creating ', filename
694   f = open(filename, 'w+')
695   f.write(data)
696
697 write('config.gypi', '# Do not edit. Generated by the configure script.\n' +
698       pprint.pformat(output, indent=2) + '\n')
699
700 config = {
701   'BUILDTYPE': 'Debug' if options.debug else 'Release',
702   'USE_NINJA': str(int(options.use_ninja or 0)),
703   'USE_XCODE': str(int(options.use_xcode or 0)),
704   'PYTHON': sys.executable,
705 }
706
707 if options.prefix:
708   config['PREFIX'] = options.prefix
709
710 config = '\n'.join(map('='.join, config.iteritems())) + '\n'
711
712 write('config.mk',
713       '# Do not edit. Generated by the configure script.\n' + config)
714
715 gyp_args = [sys.executable, 'tools/gyp_node.py', '--no-parallel']
716
717 if options.use_ninja:
718   gyp_args += ['-f', 'ninja-' + flavor]
719 elif options.use_xcode:
720   gyp_args += ['-f', 'xcode']
721 elif flavor == 'win':
722   gyp_args += ['-f', 'msvs', '-G', 'msvs_version=auto']
723 else:
724   gyp_args += ['-f', 'make-' + flavor]
725
726 gyp_args += args
727
728 subprocess.call(gyp_args)