configure: allow V8 snapshot for cross-compilation
[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 parser.add_option("--debug",
20     action="store_true",
21     dest="debug",
22     help="Also build debug build")
23
24 parser.add_option("--prefix",
25     action="store",
26     dest="prefix",
27     help="Select the install prefix (defaults to /usr/local)")
28
29 parser.add_option("--without-npm",
30     action="store_true",
31     dest="without_npm",
32     help="Don\'t install the bundled npm package manager")
33
34 parser.add_option("--without-ssl",
35     action="store_true",
36     dest="without_ssl",
37     help="Build without SSL")
38
39 parser.add_option("--without-snapshot",
40     action="store_true",
41     dest="without_snapshot",
42     help="Build without snapshotting V8 libraries. You might want to set"
43          " this for cross-compiling. [Default: False]")
44
45 parser.add_option("--shared-v8",
46     action="store_true",
47     dest="shared_v8",
48     help="Link to a shared V8 DLL instead of static linking")
49
50 parser.add_option("--shared-v8-includes",
51     action="store",
52     dest="shared_v8_includes",
53     help="Directory containing V8 header files")
54
55 parser.add_option("--shared-v8-libpath",
56     action="store",
57     dest="shared_v8_libpath",
58     help="A directory to search for the shared V8 DLL")
59
60 parser.add_option("--shared-v8-libname",
61     action="store",
62     dest="shared_v8_libname",
63     help="Alternative lib name to link to (default: 'v8')")
64
65 parser.add_option("--shared-openssl",
66     action="store_true",
67     dest="shared_openssl",
68     help="Link to a shared OpenSSl DLL instead of static linking")
69
70 parser.add_option("--shared-openssl-includes",
71     action="store",
72     dest="shared_openssl_includes",
73     help="Directory containing OpenSSL header files")
74
75 parser.add_option("--shared-openssl-libpath",
76     action="store",
77     dest="shared_openssl_libpath",
78     help="A directory to search for the shared OpenSSL DLLs")
79
80 parser.add_option("--shared-openssl-libname",
81     action="store",
82     dest="shared_openssl_libname",
83     help="Alternative lib name to link to (default: 'crypto,ssl')")
84
85 # deprecated
86 parser.add_option("--openssl-use-sys",
87     action="store_true",
88     dest="shared_openssl",
89     help=optparse.SUPPRESS_HELP)
90
91 # deprecated
92 parser.add_option("--openssl-includes",
93     action="store",
94     dest="shared_openssl_includes",
95     help=optparse.SUPPRESS_HELP)
96
97 # deprecated
98 parser.add_option("--openssl-libpath",
99     action="store",
100     dest="shared_openssl_libpath",
101     help=optparse.SUPPRESS_HELP)
102
103 # TODO document when we've decided on what the tracing API and its options will
104 # look like
105 parser.add_option("--systemtap-includes",
106     action="store",
107     dest="systemtap_includes",
108     help=optparse.SUPPRESS_HELP)
109
110 parser.add_option("--no-ssl2",
111     action="store_true",
112     dest="no_ssl2",
113     help="Disable OpenSSL v2")
114
115 parser.add_option("--shared-zlib",
116     action="store_true",
117     dest="shared_zlib",
118     help="Link to a shared zlib DLL instead of static linking")
119
120 parser.add_option("--shared-zlib-includes",
121     action="store",
122     dest="shared_zlib_includes",
123     help="Directory containing zlib header files")
124
125 parser.add_option("--shared-zlib-libpath",
126     action="store",
127     dest="shared_zlib_libpath",
128     help="A directory to search for the shared zlib DLL")
129
130 parser.add_option("--shared-zlib-libname",
131     action="store",
132     dest="shared_zlib_libname",
133     help="Alternative lib name to link to (default: 'z')")
134
135 parser.add_option("--shared-http-parser",
136     action="store_true",
137     dest="shared_http_parser",
138     help="Link to a shared http_parser DLL instead of static linking")
139
140 parser.add_option("--shared-http-parser-includes",
141     action="store",
142     dest="shared_http_parser_includes",
143     help="Directory containing http_parser header files")
144
145 parser.add_option("--shared-http-parser-libpath",
146     action="store",
147     dest="shared_http_parser_libpath",
148     help="A directory to search for the shared http_parser DLL")
149
150 parser.add_option("--shared-http-parser-libname",
151     action="store",
152     dest="shared_http_parser_libname",
153     help="Alternative lib name to link to (default: 'http_parser')")
154
155 parser.add_option("--shared-cares",
156     action="store_true",
157     dest="shared_cares",
158     help="Link to a shared cares DLL instead of static linking")
159
160 parser.add_option("--shared-cares-includes",
161     action="store",
162     dest="shared_cares_includes",
163     help="Directory containing cares header files")
164
165 parser.add_option("--shared-cares-libpath",
166     action="store",
167     dest="shared_cares_libpath",
168     help="A directory to search for the shared cares DLL")
169
170 parser.add_option("--shared-cares-libname",
171     action="store",
172     dest="shared_cares_libname",
173     help="Alternative lib name to link to (default: 'cares')")
174
175 parser.add_option("--shared-libuv",
176     action="store_true",
177     dest="shared_libuv",
178     help="Link to a shared libuv DLL instead of static linking")
179
180 parser.add_option("--shared-libuv-includes",
181     action="store",
182     dest="shared_libuv_includes",
183     help="Directory containing libuv header files")
184
185 parser.add_option("--shared-libuv-libpath",
186     action="store",
187     dest="shared_libuv_libpath",
188     help="A directory to search for the shared libuv DLL")
189
190 parser.add_option("--shared-libuv-libname",
191     action="store",
192     dest="shared_libuv_libname",
193     help="Alternative lib name to link to (default: 'uv')")
194
195 parser.add_option("--with-dtrace",
196     action="store_true",
197     dest="with_dtrace",
198     help="Build with DTrace (default is true on sunos)")
199
200 parser.add_option("--without-dtrace",
201     action="store_true",
202     dest="without_dtrace",
203     help="Build without DTrace")
204
205 parser.add_option("--with-etw",
206     action="store_true",
207     dest="with_etw",
208     help="Build with ETW (default is true on Windows)")
209
210 parser.add_option("--without-etw",
211     action="store_true",
212     dest="without_etw",
213     help="Build without ETW")
214
215 parser.add_option("--with-perfctr",
216     action="store_true",
217     dest="with_perfctr",
218     help="Build with performance counters (default is true on Windows)")
219
220 parser.add_option("--without-perfctr",
221     action="store_true",
222     dest="without_perfctr",
223     help="Build without performance counters")
224
225 # CHECKME does this still work with recent releases of V8?
226 parser.add_option("--gdb",
227     action="store_true",
228     dest="gdb",
229     help="add gdb support")
230
231 parser.add_option("--dest-cpu",
232     action="store",
233     dest="dest_cpu",
234     help="CPU architecture to build for. Valid values are: arm, ia32, x64")
235
236 parser.add_option("--dest-os",
237     action="store",
238     dest="dest_os",
239     help="Operating system to build for. Valid values are: "
240          "win, mac, solaris, freebsd, openbsd, linux")
241
242 parser.add_option("--no-ifaddrs",
243     action="store_true",
244     dest="no_ifaddrs",
245     help="Use on deprecated SunOS systems that do not support ifaddrs.h")
246
247 parser.add_option("--with-arm-float-abi",
248     action="store",
249     dest="arm_float_abi",
250     help="Specifies which floating-point ABI to use. Valid values are: "
251          "soft, softfp, hard")
252
253 parser.add_option("--with-mips-float-abi",
254     action="store",
255     dest="mips_float_abi",
256     help="Specifies which floating-point ABI to use. Valid values are: "
257          "soft, hard")
258
259 parser.add_option("--ninja",
260     action="store_true",
261     dest="use_ninja",
262     help="Generate files for the ninja build system")
263
264 # Using --unsafe-optimizations voids your warranty.
265 parser.add_option("--unsafe-optimizations",
266     action="store_true",
267     dest="unsafe_optimizations",
268     help=optparse.SUPPRESS_HELP)
269
270 parser.add_option("--xcode",
271     action="store_true",
272     dest="use_xcode",
273     help="Generate build files for use with xcode")
274
275 parser.add_option("--tag",
276     action="store",
277     dest="tag",
278     help="Custom build tag")
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 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'], 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'], stdout=subprocess.PIPE)
427   version = tuple(map(int, proc.communicate()[0].split('.')))
428
429   return (version, is_clang)
430
431
432 def configure_arm(o):
433   # V8 on ARM requires that armv7 is set. CPU Model detected by
434   # the presence of __ARM_ARCH_7__ and the like defines in compiler
435   if options.arm_float_abi:
436     hard_float = options.arm_float_abi == 'hard'
437   else:
438     hard_float = arm_hard_float_abi()
439
440   armv7 = is_arch_armv7()
441   # CHECKME VFPv3 implies ARMv7+ but is the reverse true as well?
442   fpu = 'vfpv3' if armv7 else 'vfpv2'
443
444   o['variables']['armv7'] = int(armv7)
445   o['variables']['arm_fpu'] = fpu
446   o['variables']['arm_neon'] = int(is_arm_neon())
447   o['variables']['v8_use_arm_eabi_hardfloat'] = b(hard_float)
448
449
450 def configure_mips(o):
451   if options.mips_float_abi:
452     if options.mips_float_abi in ('soft', 'hard'):
453       o['variables']['v8_use_mips_abi_hardfloat'] = b(
454           options.mips_float_abi == 'hard')
455     else:
456       raise Exception(
457           'Invalid mips-float-abi value. Valid values are: soft, hard')
458
459
460 def configure_node(o):
461   o['variables']['v8_enable_gdbjit'] = 1 if options.gdb else 0
462   o['variables']['v8_no_strict_aliasing'] = 1 # work around compiler bugs
463   o['variables']['node_prefix'] = os.path.expanduser(options.prefix or '')
464   o['variables']['node_install_npm'] = b(not options.without_npm)
465   o['variables']['node_unsafe_optimizations'] = (
466     1 if options.unsafe_optimizations else 0)
467   o['default_configuration'] = 'Debug' if options.debug else 'Release'
468
469   host_arch = host_arch_win() if os.name == 'nt' else host_arch_cc()
470   target_arch = options.dest_cpu or host_arch
471   o['variables']['host_arch'] = host_arch
472   o['variables']['target_arch'] = target_arch
473
474   if target_arch != host_arch and not options.without_snapshot:
475     o['variables']['want_separate_host_toolset'] = 1
476
477   if target_arch == 'arm':
478     configure_arm(o)
479   elif target_arch in ('mips', 'mipsel'):
480     configure_mips(o)
481
482   cc_version, is_clang = compiler_version()
483   o['variables']['clang'] = 1 if is_clang else 0
484
485   if not is_clang and cc_version != 0:
486     o['variables']['gcc_version'] = 10 * cc_version[0] + cc_version[1]
487
488   # clang has always supported -fvisibility=hidden, right?
489   if not is_clang and cc_version < (4,0,0):
490     o['variables']['visibility'] = ''
491
492   # By default, enable DTrace on SunOS systems. Don't allow it on other
493   # systems, since it won't work.  (The MacOS build process is different than
494   # SunOS, and we haven't implemented it.)
495   if flavor in ('solaris', 'mac'):
496     o['variables']['node_use_dtrace'] = b(not options.without_dtrace)
497   elif flavor == 'freebsd':
498     o['variables']['node_use_dtrace'] = b(options.with_dtrace)
499   elif flavor == 'linux':
500     o['variables']['node_use_dtrace'] = 'false'
501     o['variables']['node_use_systemtap'] = b(options.with_dtrace)
502     if options.systemtap_includes:
503       o['include_dirs'] += [options.systemtap_includes]
504   elif options.with_dtrace:
505     raise Exception(
506        'DTrace is currently only supported on SunOS, MacOS or Linux systems.')
507   else:
508     o['variables']['node_use_dtrace'] = 'false'
509     o['variables']['node_use_systemtap'] = 'false'
510
511   if options.no_ifaddrs:
512     o['defines'] += ['SUNOS_NO_IFADDRS']
513
514   # By default, enable ETW on Windows.
515   if flavor == 'win':
516     o['variables']['node_use_etw'] = b(not options.without_etw);
517   elif options.with_etw:
518     raise Exception('ETW is only supported on Windows.')
519   else:
520     o['variables']['node_use_etw'] = 'false'
521
522   # By default, enable Performance counters on Windows.
523   if flavor == 'win':
524     o['variables']['node_use_perfctr'] = b(not options.without_perfctr);
525   elif options.with_perfctr:
526     raise Exception('Performance counter is only supported on Windows.')
527   else:
528     o['variables']['node_use_perfctr'] = 'false'
529
530   if options.tag:
531     o['variables']['node_tag'] = '-' + options.tag
532   else:
533     o['variables']['node_tag'] = ''
534
535
536 def configure_libz(o):
537   o['variables']['node_shared_zlib'] = b(options.shared_zlib)
538
539   # assume shared_zlib if one of these is set?
540   if options.shared_zlib_libpath:
541     o['libraries'] += ['-L%s' % options.shared_zlib_libpath]
542   if options.shared_zlib_libname:
543     o['libraries'] += ['-l%s' % options.shared_zlib_libname]
544   elif options.shared_zlib:
545     o['libraries'] += ['-lz']
546   if options.shared_zlib_includes:
547     o['include_dirs'] += [options.shared_zlib_includes]
548
549
550 def configure_http_parser(o):
551     o['variables']['node_shared_http_parser'] = b(options.shared_http_parser)
552
553     # assume shared http_parser if one of these is set?
554     if options.shared_http_parser_libpath:
555         o['libraries'] += ['-L%s' % options.shared_http_parser_libpath]
556     if options.shared_http_parser_libname:
557         o['libraries'] += ['-l%s' % options.shared_http_parser_libname]
558     elif options.shared_http_parser:
559         o['libraries'] += ['-lhttp_parser']
560     if options.shared_http_parser_includes:
561         o['include_dirs'] += [options.shared_http_parser_includes]
562
563
564 def configure_cares(o):
565     o['variables']['node_shared_cares'] = b(options.shared_cares)
566
567     # assume shared cares if one of these is set?
568     if options.shared_cares_libpath:
569         o['libraries'] += ['-L%s' % options.shared_cares_libpath]
570     if options.shared_cares_libname:
571         o['libraries'] += ['-l%s' % options.shared_cares_libname]
572     elif options.shared_cares:
573         o['libraries'] += ['-lcares']
574     if options.shared_cares_includes:
575         o['include_dirs'] += [options.shared_cares_includes]
576
577
578 def configure_libuv(o):
579   o['variables']['node_shared_libuv'] = b(options.shared_libuv)
580
581   # assume shared libuv if one of these is set?
582   if options.shared_libuv_libpath:
583     o['libraries'] += ['-L%s' % options.shared_libuv_libpath]
584   if options.shared_libuv_libname:
585     o['libraries'] += ['-l%s' % options.shared_libuv_libname]
586   elif options.shared_libuv:
587     o['libraries'] += ['-luv']
588   if options.shared_libuv_includes:
589     o['include_dirs'] += [options.shared_libuv_includes]
590
591
592 def configure_v8(o):
593   o['variables']['v8_use_snapshot'] = b(not options.without_snapshot)
594   o['variables']['node_shared_v8'] = b(options.shared_v8)
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   if options.no_ssl2:
615     o['defines'] += ['OPENSSL_NO_SSL2=1']
616
617   if options.shared_openssl:
618     (libs, cflags) = pkg_config('openssl') or ('-lssl -lcrypto', '')
619
620     if options.shared_openssl_libpath:
621       o['libraries'] += ['-L%s' % options.shared_openssl_libpath]
622
623     if options.shared_openssl_libname:
624       libnames = options.shared_openssl_libname.split(',')
625       o['libraries'] += ['-l%s' % s for s in libnames]
626     else:
627       o['libraries'] += libs.split()
628
629     if options.shared_openssl_includes:
630       o['include_dirs'] += [options.shared_openssl_includes]
631     else:
632       o['cflags'] += cflags.split()
633
634
635 def configure_winsdk(o):
636   if flavor != 'win':
637     return
638
639   winsdk_dir = os.environ.get("WindowsSdkDir")
640
641   if winsdk_dir and os.path.isfile(winsdk_dir + '\\bin\\ctrpp.exe'):
642     print "Found ctrpp in WinSDK--will build generated files into tools/msvs/genfiles."
643     o['variables']['node_has_winsdk'] = 'true'
644     return
645
646   print "ctrpp not found in WinSDK path--using pre-gen files from tools/msvs/genfiles."
647
648
649 # determine the "flavor" (operating system) we're building for,
650 # leveraging gyp's GetFlavor function
651 flavor_params = {};
652 if (options.dest_os):
653   flavor_params['flavor'] = options.dest_os;
654 flavor = GetFlavor(flavor_params);
655
656 output = {
657   'variables': { 'python': sys.executable },
658   'include_dirs': [],
659   'libraries': [],
660   'defines': [],
661   'cflags': [],
662 }
663
664 configure_node(output)
665 configure_libz(output)
666 configure_http_parser(output)
667 configure_cares(output)
668 configure_libuv(output)
669 configure_v8(output)
670 configure_openssl(output)
671 configure_winsdk(output)
672
673 # variables should be a root level element,
674 # move everything else to target_defaults
675 variables = output['variables']
676 del output['variables']
677 output = {
678   'variables': variables,
679   'target_defaults': output
680 }
681 pprint.pprint(output, indent=2)
682
683 def write(filename, data):
684   filename = os.path.join(root_dir, filename)
685   print "creating ", filename
686   f = open(filename, 'w+')
687   f.write(data)
688
689 write('config.gypi', "# Do not edit. Generated by the configure script.\n" +
690   pprint.pformat(output, indent=2) + "\n")
691
692 config = {
693   'BUILDTYPE': 'Debug' if options.debug else 'Release',
694   'USE_NINJA': str(int(options.use_ninja or 0)),
695   'USE_XCODE': str(int(options.use_xcode or 0)),
696   'PYTHON': sys.executable,
697 }
698 config = '\n'.join(map('='.join, config.iteritems())) + '\n'
699
700 write('config.mk',
701       '# Do not edit. Generated by the configure script.\n' + config)
702
703 gyp_args = [sys.executable, 'tools/gyp_node.py', '--no-parallel']
704
705 if options.use_ninja:
706   gyp_args += ['-f', 'ninja-' + flavor]
707 elif options.use_xcode:
708   gyp_args += ['-f', 'xcode']
709 elif flavor == 'win':
710   gyp_args += ['-f', 'msvs', '-G', 'msvs_version=auto']
711 else:
712   gyp_args += ['-f', 'make-' + flavor]
713
714 gyp_args += args
715
716 subprocess.call(gyp_args)