842daa94a312d7debaff08aabac343918484a2aa
[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          'The flags should be separated by a comma')
212
213 parser.add_option('--with-arm-float-abi',
214     action='store',
215     dest='arm_float_abi',
216     help='specifies which floating-point ABI to use. Valid values are: '
217          'soft, softfp, hard')
218
219 parser.add_option('--with-dtrace',
220     action='store_true',
221     dest='with_dtrace',
222     help='build with DTrace (default is true on sunos)')
223
224 parser.add_option('--with-etw',
225     action='store_true',
226     dest='with_etw',
227     help='build with ETW (default is true on Windows)')
228
229 parser.add_option('--with-icu-path',
230     action='store',
231     dest='with_icu_path',
232     help='Path to icu.gyp (ICU i18n, Chromium version only.)')
233
234 parser.add_option('--with-perfctr',
235     action='store_true',
236     dest='with_perfctr',
237     help='build with performance counters (default is true on Windows)')
238
239 parser.add_option('--with-sslv2',
240     action='store_true',
241     dest='with_sslv2',
242     help='enable SSL v2')
243
244 parser.add_option('--without-dtrace',
245     action='store_true',
246     dest='without_dtrace',
247     help='build without DTrace')
248
249 parser.add_option('--without-etw',
250     action='store_true',
251     dest='without_etw',
252     help='build without ETW')
253
254 parser.add_option('--without-npm',
255     action='store_true',
256     dest='without_npm',
257     help='don\'t install the bundled npm package manager')
258
259 parser.add_option('--without-perfctr',
260     action='store_true',
261     dest='without_perfctr',
262     help='build without performance counters')
263
264 parser.add_option('--without-snapshot',
265     action='store_true',
266     dest='without_snapshot',
267     help='build without snapshotting V8 libraries. You might want to set'
268          ' this for cross-compiling. [Default: False]')
269
270 parser.add_option('--without-ssl',
271     action='store_true',
272     dest='without_ssl',
273     help='build without SSL')
274
275 parser.add_option('--xcode',
276     action='store_true',
277     dest='use_xcode',
278     help='generate build files for use with xcode')
279
280 (options, args) = parser.parse_args()
281
282
283 def b(value):
284   """Returns the string 'true' if value is truthy, 'false' otherwise."""
285   if value:
286     return 'true'
287   else:
288     return 'false'
289
290
291 def pkg_config(pkg):
292   cmd = os.popen('pkg-config --libs %s' % pkg, 'r')
293   libs = cmd.readline().strip()
294   ret = cmd.close()
295   if (ret): return None
296
297   cmd = os.popen('pkg-config --cflags %s' % pkg, 'r')
298   cflags = cmd.readline().strip()
299   ret = cmd.close()
300   if (ret): return None
301
302   return (libs, cflags)
303
304
305 def cc_macros():
306   """Checks predefined macros using the CC command."""
307
308   try:
309     p = subprocess.Popen(shlex.split(CC) + ['-dM', '-E', '-'],
310                          stdin=subprocess.PIPE,
311                          stdout=subprocess.PIPE,
312                          stderr=subprocess.PIPE)
313   except OSError:
314     print '''Node.js configure error: No acceptable C compiler found!
315
316         Please make sure you have a C compiler installed on your system and/or
317         consider adjusting the CC environment variable if you installed
318         it in a non-standard prefix.
319         '''
320     sys.exit()
321
322   p.stdin.write('\n')
323   out = p.communicate()[0]
324
325   out = str(out).split('\n')
326
327   k = {}
328   for line in out:
329     lst = shlex.split(line)
330     if len(lst) > 2:
331       key = lst[1]
332       val = lst[2]
333       k[key] = val
334   return k
335
336
337 def is_arch_armv7():
338   """Check for ARMv7 instructions"""
339   cc_macros_cache = cc_macros()
340   return ('__ARM_ARCH_7__' in cc_macros_cache or
341           '__ARM_ARCH_7A__' in cc_macros_cache or
342           '__ARM_ARCH_7R__' in cc_macros_cache or
343           '__ARM_ARCH_7M__' in cc_macros_cache)
344
345
346 def is_arm_neon():
347   """Check for ARM NEON support"""
348   return '__ARM_NEON__' in cc_macros()
349
350
351 def is_arm_hard_float_abi():
352   """Check for hardfloat or softfloat eabi on ARM"""
353   # GCC versions 4.6 and above define __ARM_PCS or __ARM_PCS_VFP to specify
354   # the Floating Point ABI used (PCS stands for Procedure Call Standard).
355   # We use these as well as a couple of other defines to statically determine
356   # what FP ABI used.
357   # GCC versions 4.4 and below don't support hard-fp.
358   # GCC versions 4.5 may support hard-fp without defining __ARM_PCS or
359   # __ARM_PCS_VFP.
360
361   if compiler_version() >= (4, 6, 0):
362     return '__ARM_PCS_VFP' in cc_macros()
363   elif compiler_version() < (4, 5, 0):
364     return False
365   elif '__ARM_PCS_VFP' in cc_macros():
366     return True
367   elif ('__ARM_PCS' in cc_macros() or
368         '__SOFTFP' in cc_macros() or
369         not '__VFP_FP__' in cc_macros()):
370     return False
371   else:
372     print '''Node.js configure error: Your version of GCC does not report
373       the Floating-Point ABI to compile for your hardware
374
375       Please manually specify which floating-point ABI to use with the
376       --with-arm-float-abi option.
377       '''
378     sys.exit()
379
380
381 def host_arch_cc():
382   """Host architecture check using the CC command."""
383
384   k = cc_macros()
385
386   matchup = {
387     '__x86_64__'  : 'x64',
388     '__i386__'    : 'ia32',
389     '__arm__'     : 'arm',
390     '__mips__'    : 'mips',
391   }
392
393   rtn = 'ia32' # default
394
395   for i in matchup:
396     if i in k and k[i] != '0':
397       rtn = matchup[i]
398       break
399
400   return rtn
401
402
403 def host_arch_win():
404   """Host architecture check using environ vars (better way to do this?)"""
405
406   arch = os.environ.get('PROCESSOR_ARCHITECTURE', 'x86')
407
408   matchup = {
409     'AMD64'  : 'x64',
410     'x86'    : 'ia32',
411     'arm'    : 'arm',
412     'mips'   : 'mips',
413   }
414
415   return matchup.get(arch, 'ia32')
416
417
418 def compiler_version():
419   try:
420     proc = subprocess.Popen(shlex.split(CC) + ['--version'],
421                             stdout=subprocess.PIPE)
422   except WindowsError:
423     return (0, False)
424
425   is_clang = 'clang' in proc.communicate()[0].split('\n')[0]
426
427   proc = subprocess.Popen(shlex.split(CC) + ['-dumpversion'],
428                           stdout=subprocess.PIPE)
429   version = tuple(map(int, proc.communicate()[0].split('.')))
430
431   return (version, is_clang)
432
433
434 def configure_arm(o):
435   if options.arm_float_abi:
436     arm_float_abi = options.arm_float_abi
437   elif is_arm_hard_float_abi():
438     arm_float_abi = 'hard'
439   else:
440     arm_float_abi = 'default'
441   o['variables']['armv7'] = int(is_arch_armv7())
442   o['variables']['arm_fpu'] = 'vfpv3'  # V8 3.18 no longer supports VFP2.
443   o['variables']['arm_neon'] = int(is_arm_neon())
444   o['variables']['arm_thumb'] = 0      # -marm
445   o['variables']['arm_float_abi'] = arm_float_abi
446
447
448 def configure_node(o):
449   if options.dest_os == 'android':
450     o['variables']['OS'] = 'android'
451   o['variables']['node_prefix'] = os.path.expanduser(options.prefix or '')
452   o['variables']['node_install_npm'] = b(not options.without_npm)
453   o['default_configuration'] = 'Debug' if options.debug else 'Release'
454
455   host_arch = host_arch_win() if os.name == 'nt' else host_arch_cc()
456   target_arch = options.dest_cpu or host_arch
457   o['variables']['host_arch'] = host_arch
458   o['variables']['target_arch'] = target_arch
459
460   if target_arch == 'arm':
461     configure_arm(o)
462
463   cc_version, is_clang = compiler_version()
464   o['variables']['clang'] = 1 if is_clang else 0
465
466   if not is_clang and cc_version != 0:
467     o['variables']['gcc_version'] = 10 * cc_version[0] + cc_version[1]
468
469   # clang has always supported -fvisibility=hidden, right?
470   if not is_clang and cc_version < (4,0,0):
471     o['variables']['visibility'] = ''
472
473   if flavor in ('solaris', 'mac', 'linux', 'freebsd'):
474     use_dtrace = not options.without_dtrace
475     # Don't enable by default on linux and freebsd
476     if flavor in ('linux', 'freebsd'):
477       use_dtrace = options.with_dtrace
478
479     if flavor == 'linux':
480       if options.systemtap_includes:
481         o['include_dirs'] += [options.systemtap_includes]
482     o['variables']['node_use_dtrace'] = b(use_dtrace)
483     o['variables']['uv_use_dtrace'] = b(use_dtrace)
484     o['variables']['uv_parent_path'] = '/deps/uv/'
485   elif options.with_dtrace:
486     raise Exception(
487        'DTrace is currently only supported on SunOS, MacOS or Linux systems.')
488   else:
489     o['variables']['node_use_dtrace'] = 'false'
490
491   # if we're on illumos based systems wrap the helper library into the
492   # executable
493   if flavor == 'solaris':
494     o['variables']['node_use_mdb'] = 'true'
495   else:
496     o['variables']['node_use_mdb'] = 'false'
497
498   if options.no_ifaddrs:
499     o['defines'] += ['SUNOS_NO_IFADDRS']
500
501   # By default, enable ETW on Windows.
502   if flavor == 'win':
503     o['variables']['node_use_etw'] = b(not options.without_etw)
504   elif options.with_etw:
505     raise Exception('ETW is only supported on Windows.')
506   else:
507     o['variables']['node_use_etw'] = 'false'
508
509   # By default, enable Performance counters on Windows.
510   if flavor == 'win':
511     o['variables']['node_use_perfctr'] = b(not options.without_perfctr)
512   elif options.with_perfctr:
513     raise Exception('Performance counter is only supported on Windows.')
514   else:
515     o['variables']['node_use_perfctr'] = 'false'
516
517   if options.tag:
518     o['variables']['node_tag'] = '-' + options.tag
519   else:
520     o['variables']['node_tag'] = ''
521
522   if options.v8_options:
523     opts = options.v8_options.split(',')
524     o['variables']['node_v8_options'] = '"' + '","'.join(opts) + '"'
525   else:
526     o['variables']['node_v8_options'] = ''
527
528
529 def configure_libz(o):
530   o['variables']['node_shared_zlib'] = b(options.shared_zlib)
531
532   # assume shared_zlib if one of these is set?
533   if options.shared_zlib_libpath:
534     o['libraries'] += ['-L%s' % options.shared_zlib_libpath]
535   if options.shared_zlib_libname:
536     o['libraries'] += ['-l%s' % options.shared_zlib_libname]
537   elif options.shared_zlib:
538     o['libraries'] += ['-lz']
539   if options.shared_zlib_includes:
540     o['include_dirs'] += [options.shared_zlib_includes]
541
542
543 def configure_http_parser(o):
544     o['variables']['node_shared_http_parser'] = b(options.shared_http_parser)
545
546     # assume shared http_parser if one of these is set?
547     if options.shared_http_parser_libpath:
548         o['libraries'] += ['-L%s' % options.shared_http_parser_libpath]
549     if options.shared_http_parser_libname:
550         o['libraries'] += ['-l%s' % options.shared_http_parser_libname]
551     elif options.shared_http_parser:
552         o['libraries'] += ['-lhttp_parser']
553     if options.shared_http_parser_includes:
554         o['include_dirs'] += [options.shared_http_parser_includes]
555
556
557 def configure_cares(o):
558     o['variables']['node_shared_cares'] = b(options.shared_cares)
559
560     # assume shared cares if one of these is set?
561     if options.shared_cares_libpath:
562         o['libraries'] += ['-L%s' % options.shared_cares_libpath]
563     if options.shared_cares_libname:
564         o['libraries'] += ['-l%s' % options.shared_cares_libname]
565     elif options.shared_cares:
566         o['libraries'] += ['-lcares']
567     if options.shared_cares_includes:
568         o['include_dirs'] += [options.shared_cares_includes]
569
570
571 def configure_libuv(o):
572   o['variables']['node_shared_libuv'] = b(options.shared_libuv)
573
574   # assume shared libuv if one of these is set?
575   if options.shared_libuv_libpath:
576     o['libraries'] += ['-L%s' % options.shared_libuv_libpath]
577   else:
578     o['variables']['uv_library'] = 'static_library'
579
580   if options.shared_libuv_libname:
581     o['libraries'] += ['-l%s' % options.shared_libuv_libname]
582   elif options.shared_libuv:
583     o['libraries'] += ['-luv']
584   if options.shared_libuv_includes:
585     o['include_dirs'] += [options.shared_libuv_includes]
586
587
588 def configure_v8(o):
589   o['variables']['node_shared_v8'] = b(options.shared_v8)
590   o['variables']['v8_enable_gdbjit'] = 1 if options.gdb else 0
591   o['variables']['v8_no_strict_aliasing'] = 1  # Work around compiler bugs.
592   o['variables']['v8_optimized_debug'] = 0  # Compile with -O0 in debug builds.
593   o['variables']['v8_random_seed'] = 0  # Use a random seed for hash tables.
594   o['variables']['v8_use_snapshot'] = b(not options.without_snapshot)
595
596   # assume shared_v8 if one of these is set?
597   if options.shared_v8_libpath:
598     o['libraries'] += ['-L%s' % options.shared_v8_libpath]
599   if options.shared_v8_libname:
600     o['libraries'] += ['-l%s' % options.shared_v8_libname]
601   elif options.shared_v8:
602     o['libraries'] += ['-lv8']
603   if options.shared_v8_includes:
604     o['include_dirs'] += [options.shared_v8_includes]
605
606
607 def configure_openssl(o):
608   o['variables']['node_use_openssl'] = b(not options.without_ssl)
609   o['variables']['node_shared_openssl'] = b(options.shared_openssl)
610
611   if options.without_ssl:
612     return
613
614   # OpenSSL uses `#ifndef OPENSSL_NO_SSL2` checks so only define the
615   # macro when we want to _disable_ SSL2.
616   if not options.with_sslv2:
617     o['defines'] += ['OPENSSL_NO_SSL2=1']
618
619   if options.shared_openssl:
620     (libs, cflags) = pkg_config('openssl') or ('-lssl -lcrypto', '')
621
622     if options.shared_openssl_libpath:
623       o['libraries'] += ['-L%s' % options.shared_openssl_libpath]
624
625     if options.shared_openssl_libname:
626       libnames = options.shared_openssl_libname.split(',')
627       o['libraries'] += ['-l%s' % s for s in libnames]
628     else:
629       o['libraries'] += libs.split()
630
631     if options.shared_openssl_includes:
632       o['include_dirs'] += [options.shared_openssl_includes]
633     else:
634       o['cflags'] += cflags.split()
635
636
637 def configure_winsdk(o):
638   if flavor != 'win':
639     return
640
641   winsdk_dir = os.environ.get('WindowsSdkDir')
642
643   if winsdk_dir and os.path.isfile(winsdk_dir + '\\bin\\ctrpp.exe'):
644     print('Found ctrpp in WinSDK--will build generated files '
645           'into tools/msvs/genfiles.')
646     o['variables']['node_has_winsdk'] = 'true'
647     return
648
649   print('ctrpp not found in WinSDK path--using pre-gen files '
650         'from tools/msvs/genfiles.')
651
652
653 def configure_icu(o):
654   have_icu_path = bool(options.with_icu_path)
655   o['variables']['v8_enable_i18n_support'] = int(have_icu_path)
656   if have_icu_path:
657     o['variables']['icu_gyp_path'] = options.with_icu_path
658
659
660 # determine the "flavor" (operating system) we're building for,
661 # leveraging gyp's GetFlavor function
662 flavor_params = {}
663 if (options.dest_os):
664   flavor_params['flavor'] = options.dest_os
665 flavor = GetFlavor(flavor_params)
666
667 output = {
668   'variables': { 'python': sys.executable },
669   'include_dirs': [],
670   'libraries': [],
671   'defines': [],
672   'cflags': [],
673 }
674
675 configure_node(output)
676 configure_libz(output)
677 configure_http_parser(output)
678 configure_cares(output)
679 configure_libuv(output)
680 configure_v8(output)
681 configure_openssl(output)
682 configure_winsdk(output)
683 configure_icu(output)
684
685 # variables should be a root level element,
686 # move everything else to target_defaults
687 variables = output['variables']
688 del output['variables']
689 output = {
690   'variables': variables,
691   'target_defaults': output
692 }
693 pprint.pprint(output, indent=2)
694
695 def write(filename, data):
696   filename = os.path.join(root_dir, filename)
697   print 'creating ', filename
698   f = open(filename, 'w+')
699   f.write(data)
700
701 write('config.gypi', '# Do not edit. Generated by the configure script.\n' +
702       pprint.pformat(output, indent=2) + '\n')
703
704 config = {
705   'BUILDTYPE': 'Debug' if options.debug else 'Release',
706   'USE_NINJA': str(int(options.use_ninja or 0)),
707   'USE_XCODE': str(int(options.use_xcode or 0)),
708   'PYTHON': sys.executable,
709 }
710
711 if options.prefix:
712   config['PREFIX'] = options.prefix
713
714 config = '\n'.join(map('='.join, config.iteritems())) + '\n'
715
716 write('config.mk',
717       '# Do not edit. Generated by the configure script.\n' + config)
718
719 gyp_args = [sys.executable, 'tools/gyp_node.py', '--no-parallel']
720
721 if options.use_ninja:
722   gyp_args += ['-f', 'ninja-' + flavor]
723 elif options.use_xcode:
724   gyp_args += ['-f', 'xcode']
725 elif flavor == 'win':
726   gyp_args += ['-f', 'msvs', '-G', 'msvs_version=auto']
727 else:
728   gyp_args += ['-f', 'make-' + flavor]
729
730 gyp_args += args
731
732 subprocess.call(gyp_args)