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