daea8cad1217d6f5d842feb685752a4c243e1fad
[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 != host_arch and not options.without_snapshot:
460     o['variables']['want_separate_host_toolset'] = 1
461
462   if target_arch == 'arm':
463     configure_arm(o)
464
465   cc_version, is_clang = compiler_version()
466   o['variables']['clang'] = 1 if is_clang else 0
467
468   if not is_clang and cc_version != 0:
469     o['variables']['gcc_version'] = 10 * cc_version[0] + cc_version[1]
470
471   # clang has always supported -fvisibility=hidden, right?
472   if not is_clang and cc_version < (4,0,0):
473     o['variables']['visibility'] = ''
474
475   if flavor in ('solaris', 'mac', 'linux', 'freebsd'):
476     use_dtrace = not options.without_dtrace
477     # Don't enable by default on linux and freebsd
478     if flavor in ('linux', 'freebsd'):
479       use_dtrace = options.with_dtrace
480
481     if flavor == 'linux':
482       if options.systemtap_includes:
483         o['include_dirs'] += [options.systemtap_includes]
484     o['variables']['node_use_dtrace'] = b(use_dtrace)
485     o['variables']['uv_use_dtrace'] = b(use_dtrace)
486     o['variables']['uv_parent_path'] = '/deps/uv/'
487   elif options.with_dtrace:
488     raise Exception(
489        'DTrace is currently only supported on SunOS, MacOS or Linux systems.')
490   else:
491     o['variables']['node_use_dtrace'] = 'false'
492
493   # if we're on illumos based systems wrap the helper library into the
494   # executable
495   if flavor == 'solaris':
496     o['variables']['node_use_mdb'] = 'true'
497   else:
498     o['variables']['node_use_mdb'] = 'false'
499
500   if options.no_ifaddrs:
501     o['defines'] += ['SUNOS_NO_IFADDRS']
502
503   # By default, enable ETW on Windows.
504   if flavor == 'win':
505     o['variables']['node_use_etw'] = b(not options.without_etw)
506   elif options.with_etw:
507     raise Exception('ETW is only supported on Windows.')
508   else:
509     o['variables']['node_use_etw'] = 'false'
510
511   # By default, enable Performance counters on Windows.
512   if flavor == 'win':
513     o['variables']['node_use_perfctr'] = b(not options.without_perfctr)
514   elif options.with_perfctr:
515     raise Exception('Performance counter is only supported on Windows.')
516   else:
517     o['variables']['node_use_perfctr'] = 'false'
518
519   if options.tag:
520     o['variables']['node_tag'] = '-' + options.tag
521   else:
522     o['variables']['node_tag'] = ''
523
524   if options.v8_options:
525     o['variables']['node_v8_options'] = options.v8_options.replace('"', '\\"')
526
527
528 def configure_libz(o):
529   o['variables']['node_shared_zlib'] = b(options.shared_zlib)
530
531   # assume shared_zlib if one of these is set?
532   if options.shared_zlib_libpath:
533     o['libraries'] += ['-L%s' % options.shared_zlib_libpath]
534   if options.shared_zlib_libname:
535     o['libraries'] += ['-l%s' % options.shared_zlib_libname]
536   elif options.shared_zlib:
537     o['libraries'] += ['-lz']
538   if options.shared_zlib_includes:
539     o['include_dirs'] += [options.shared_zlib_includes]
540
541
542 def configure_http_parser(o):
543     o['variables']['node_shared_http_parser'] = b(options.shared_http_parser)
544
545     # assume shared http_parser if one of these is set?
546     if options.shared_http_parser_libpath:
547         o['libraries'] += ['-L%s' % options.shared_http_parser_libpath]
548     if options.shared_http_parser_libname:
549         o['libraries'] += ['-l%s' % options.shared_http_parser_libname]
550     elif options.shared_http_parser:
551         o['libraries'] += ['-lhttp_parser']
552     if options.shared_http_parser_includes:
553         o['include_dirs'] += [options.shared_http_parser_includes]
554
555
556 def configure_cares(o):
557     o['variables']['node_shared_cares'] = b(options.shared_cares)
558
559     # assume shared cares if one of these is set?
560     if options.shared_cares_libpath:
561         o['libraries'] += ['-L%s' % options.shared_cares_libpath]
562     if options.shared_cares_libname:
563         o['libraries'] += ['-l%s' % options.shared_cares_libname]
564     elif options.shared_cares:
565         o['libraries'] += ['-lcares']
566     if options.shared_cares_includes:
567         o['include_dirs'] += [options.shared_cares_includes]
568
569
570 def configure_libuv(o):
571   o['variables']['node_shared_libuv'] = b(options.shared_libuv)
572
573   # assume shared libuv if one of these is set?
574   if options.shared_libuv_libpath:
575     o['libraries'] += ['-L%s' % options.shared_libuv_libpath]
576   else:
577     o['variables']['uv_library'] = 'static_library'
578
579   if options.shared_libuv_libname:
580     o['libraries'] += ['-l%s' % options.shared_libuv_libname]
581   elif options.shared_libuv:
582     o['libraries'] += ['-luv']
583   if options.shared_libuv_includes:
584     o['include_dirs'] += [options.shared_libuv_includes]
585
586
587 def configure_v8(o):
588   o['variables']['node_shared_v8'] = b(options.shared_v8)
589   o['variables']['v8_enable_gdbjit'] = 1 if options.gdb else 0
590   o['variables']['v8_no_strict_aliasing'] = 1  # Work around compiler bugs.
591   o['variables']['v8_optimized_debug'] = 0  # Compile with -O0 in debug builds.
592   o['variables']['v8_random_seed'] = 0  # Use a random seed for hash tables.
593   o['variables']['v8_use_snapshot'] = b(not options.without_snapshot)
594
595   # assume shared_v8 if one of these is set?
596   if options.shared_v8_libpath:
597     o['libraries'] += ['-L%s' % options.shared_v8_libpath]
598   if options.shared_v8_libname:
599     o['libraries'] += ['-l%s' % options.shared_v8_libname]
600   elif options.shared_v8:
601     o['libraries'] += ['-lv8']
602   if options.shared_v8_includes:
603     o['include_dirs'] += [options.shared_v8_includes]
604
605
606 def configure_openssl(o):
607   o['variables']['node_use_openssl'] = b(not options.without_ssl)
608   o['variables']['node_shared_openssl'] = b(options.shared_openssl)
609
610   if options.without_ssl:
611     return
612
613   # OpenSSL uses `#ifndef OPENSSL_NO_SSL2` checks so only define the
614   # macro when we want to _disable_ SSL2.
615   if not options.with_sslv2:
616     o['defines'] += ['OPENSSL_NO_SSL2=1']
617
618   if options.shared_openssl:
619     (libs, cflags) = pkg_config('openssl') or ('-lssl -lcrypto', '')
620
621     if options.shared_openssl_libpath:
622       o['libraries'] += ['-L%s' % options.shared_openssl_libpath]
623
624     if options.shared_openssl_libname:
625       libnames = options.shared_openssl_libname.split(',')
626       o['libraries'] += ['-l%s' % s for s in libnames]
627     else:
628       o['libraries'] += libs.split()
629
630     if options.shared_openssl_includes:
631       o['include_dirs'] += [options.shared_openssl_includes]
632     else:
633       o['cflags'] += cflags.split()
634
635
636 def configure_winsdk(o):
637   if flavor != 'win':
638     return
639
640   winsdk_dir = os.environ.get('WindowsSdkDir')
641
642   if winsdk_dir and os.path.isfile(winsdk_dir + '\\bin\\ctrpp.exe'):
643     print('Found ctrpp in WinSDK--will build generated files '
644           'into tools/msvs/genfiles.')
645     o['variables']['node_has_winsdk'] = 'true'
646     return
647
648   print('ctrpp not found in WinSDK path--using pre-gen files '
649         'from tools/msvs/genfiles.')
650
651
652 def configure_icu(o):
653   have_icu_path = bool(options.with_icu_path)
654   o['variables']['v8_enable_i18n_support'] = int(have_icu_path)
655   if have_icu_path:
656     o['variables']['icu_gyp_path'] = options.with_icu_path
657
658
659 # determine the "flavor" (operating system) we're building for,
660 # leveraging gyp's GetFlavor function
661 flavor_params = {}
662 if (options.dest_os):
663   flavor_params['flavor'] = options.dest_os
664 flavor = GetFlavor(flavor_params)
665
666 output = {
667   'variables': { 'python': sys.executable },
668   'include_dirs': [],
669   'libraries': [],
670   'defines': [],
671   'cflags': [],
672 }
673
674 configure_node(output)
675 configure_libz(output)
676 configure_http_parser(output)
677 configure_cares(output)
678 configure_libuv(output)
679 configure_v8(output)
680 configure_openssl(output)
681 configure_winsdk(output)
682 configure_icu(output)
683
684 # variables should be a root level element,
685 # move everything else to target_defaults
686 variables = output['variables']
687 del output['variables']
688 output = {
689   'variables': variables,
690   'target_defaults': output
691 }
692 pprint.pprint(output, indent=2)
693
694 def write(filename, data):
695   filename = os.path.join(root_dir, filename)
696   print 'creating ', filename
697   f = open(filename, 'w+')
698   f.write(data)
699
700 write('config.gypi', '# Do not edit. Generated by the configure script.\n' +
701       pprint.pformat(output, indent=2) + '\n')
702
703 config = {
704   'BUILDTYPE': 'Debug' if options.debug else 'Release',
705   'USE_NINJA': str(int(options.use_ninja or 0)),
706   'USE_XCODE': str(int(options.use_xcode or 0)),
707   'PYTHON': sys.executable,
708 }
709
710 if options.prefix:
711   config['PREFIX'] = options.prefix
712
713 config = '\n'.join(map('='.join, config.iteritems())) + '\n'
714
715 write('config.mk',
716       '# Do not edit. Generated by the configure script.\n' + config)
717
718 gyp_args = [sys.executable, 'tools/gyp_node.py', '--no-parallel']
719
720 if options.use_ninja:
721   gyp_args += ['-f', 'ninja-' + flavor]
722 elif options.use_xcode:
723   gyp_args += ['-f', 'xcode']
724 elif flavor == 'win':
725   gyp_args += ['-f', 'msvs', '-G', 'msvs_version=auto']
726 else:
727   gyp_args += ['-f', 'make-' + flavor]
728
729 gyp_args += args
730
731 subprocess.call(gyp_args)