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