docs: add note about default padding in crypto
[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 import shutil
10 import string
11
12 CC = os.environ.get('CC', 'cc')
13 CXX = os.environ.get('CXX', 'c++')
14
15 root_dir = os.path.dirname(__file__)
16 sys.path.insert(0, os.path.join(root_dir, 'tools', 'gyp', 'pylib'))
17 from gyp.common import GetFlavor
18
19 # imports in tools/configure.d
20 sys.path.insert(0, os.path.join(root_dir, 'tools', 'configure.d'))
21 import nodedownload
22
23 # parse our options
24 parser = optparse.OptionParser()
25
26 # Options should be in alphabetical order but keep --prefix at the top,
27 # that's arguably the one people will be looking for most.
28 parser.add_option('--prefix',
29     action='store',
30     dest='prefix',
31     help='select the install prefix (defaults to /usr/local)')
32
33 parser.add_option('--debug',
34     action='store_true',
35     dest='debug',
36     help='also build debug build')
37
38 parser.add_option('--dest-cpu',
39     action='store',
40     dest='dest_cpu',
41     help='CPU architecture to build for. Valid values are: arm, ia32, x32, x64')
42
43 parser.add_option('--dest-os',
44     action='store',
45     dest='dest_os',
46     help='operating system to build for. Valid values are: '
47          'win, mac, solaris, freebsd, openbsd, linux, android')
48
49 parser.add_option('--gdb',
50     action='store_true',
51     dest='gdb',
52     help='add gdb support')
53
54 parser.add_option('--no-ifaddrs',
55     action='store_true',
56     dest='no_ifaddrs',
57     help='use on deprecated SunOS systems that do not support ifaddrs.h')
58
59 parser.add_option("--fully-static",
60     action="store_true",
61     dest="fully_static",
62     help="Generate an executable without external dynamic libraries. This "
63          "will not work on OSX when using default compilation environment")
64
65 parser.add_option("--openssl-no-asm",
66     action="store_true",
67     dest="openssl_no_asm",
68     help="Do not build optimized assembly for OpenSSL")
69
70 # deprecated
71 parser.add_option('--openssl-includes',
72     action='store',
73     dest='shared_openssl_includes',
74     help=optparse.SUPPRESS_HELP)
75
76 # deprecated
77 parser.add_option('--openssl-libpath',
78     action='store',
79     dest='shared_openssl_libpath',
80     help=optparse.SUPPRESS_HELP)
81
82 # deprecated
83 parser.add_option('--openssl-use-sys',
84     action='store_true',
85     dest='shared_openssl',
86     help=optparse.SUPPRESS_HELP)
87
88 parser.add_option('--shared-http-parser',
89     action='store_true',
90     dest='shared_http_parser',
91     help='link to a shared http_parser DLL instead of static linking')
92
93 parser.add_option('--shared-http-parser-includes',
94     action='store',
95     dest='shared_http_parser_includes',
96     help='directory containing http_parser header files')
97
98 parser.add_option('--shared-http-parser-libname',
99     action='store',
100     dest='shared_http_parser_libname',
101     help='alternative lib name to link to (default: \'http_parser\')')
102
103 parser.add_option('--shared-http-parser-libpath',
104     action='store',
105     dest='shared_http_parser_libpath',
106     help='a directory to search for the shared http_parser DLL')
107
108 parser.add_option('--shared-libuv',
109     action='store_true',
110     dest='shared_libuv',
111     help='link to a shared libuv DLL instead of static linking')
112
113 parser.add_option('--shared-libuv-includes',
114     action='store',
115     dest='shared_libuv_includes',
116     help='directory containing libuv header files')
117
118 parser.add_option('--shared-libuv-libname',
119     action='store',
120     dest='shared_libuv_libname',
121     help='alternative lib name to link to (default: \'uv\')')
122
123 parser.add_option('--shared-libuv-libpath',
124     action='store',
125     dest='shared_libuv_libpath',
126     help='a directory to search for the shared libuv DLL')
127
128 parser.add_option('--shared-openssl',
129     action='store_true',
130     dest='shared_openssl',
131     help='link to a shared OpenSSl DLL instead of static linking')
132
133 parser.add_option('--shared-openssl-includes',
134     action='store',
135     dest='shared_openssl_includes',
136     help='directory containing OpenSSL header files')
137
138 parser.add_option('--shared-openssl-libname',
139     action='store',
140     dest='shared_openssl_libname',
141     help='alternative lib name to link to (default: \'crypto,ssl\')')
142
143 parser.add_option('--shared-openssl-libpath',
144     action='store',
145     dest='shared_openssl_libpath',
146     help='a directory to search for the shared OpenSSL DLLs')
147
148 parser.add_option('--shared-v8',
149     action='store_true',
150     dest='shared_v8',
151     help='link to a shared V8 DLL instead of static linking')
152
153 parser.add_option('--shared-v8-includes',
154     action='store',
155     dest='shared_v8_includes',
156     help='directory containing V8 header files')
157
158 parser.add_option('--shared-v8-libname',
159     action='store',
160     dest='shared_v8_libname',
161     help='alternative lib name to link to (default: \'v8\')')
162
163 parser.add_option('--shared-v8-libpath',
164     action='store',
165     dest='shared_v8_libpath',
166     help='a directory to search for the shared V8 DLL')
167
168 parser.add_option('--shared-zlib',
169     action='store_true',
170     dest='shared_zlib',
171     help='link to a shared zlib DLL instead of static linking')
172
173 parser.add_option('--shared-zlib-includes',
174     action='store',
175     dest='shared_zlib_includes',
176     help='directory containing zlib header files')
177
178 parser.add_option('--shared-zlib-libname',
179     action='store',
180     dest='shared_zlib_libname',
181     help='alternative lib name to link to (default: \'z\')')
182
183 parser.add_option('--shared-zlib-libpath',
184     action='store',
185     dest='shared_zlib_libpath',
186     help='a directory to search for the shared zlib DLL')
187
188 # TODO document when we've decided on what the tracing API and its options will
189 # look like
190 parser.add_option('--systemtap-includes',
191     action='store',
192     dest='systemtap_includes',
193     help=optparse.SUPPRESS_HELP)
194
195 parser.add_option('--tag',
196     action='store',
197     dest='tag',
198     help='custom build tag')
199
200 parser.add_option('--v8-options',
201     action='store',
202     dest='v8_options',
203     help='v8 options to pass, see `node --v8-options` for examples.')
204
205 parser.add_option('--with-arm-float-abi',
206     action='store',
207     dest='arm_float_abi',
208     help='specifies which floating-point ABI to use. Valid values are: '
209          'soft, softfp, hard')
210
211 parser.add_option('--with-dtrace',
212     action='store_true',
213     dest='with_dtrace',
214     help='build with DTrace (default is true on sunos)')
215
216 parser.add_option('--with-etw',
217     action='store_true',
218     dest='with_etw',
219     help='build with ETW (default is true on Windows)')
220
221 parser.add_option('--download',
222     action='store',
223     dest='download_list',
224     help=nodedownload.help())
225
226 parser.add_option('--with-icu-path',
227     action='store',
228     dest='with_icu_path',
229     help='Path to icu.gyp (ICU i18n, Chromium version only.)')
230
231 parser.add_option('--with-icu-locales',
232     action='store',
233     dest='with_icu_locales',
234     help='Comma-separated list of locales for "small-icu". Default: "root,en". "root" is assumed.')
235
236 parser.add_option('--with-intl',
237     action='store',
238     dest='with_intl',
239     help='Intl mode: none, full-icu, small-icu (default is none)')
240
241 parser.add_option('--with-icu-source',
242     action='store',
243     dest='with_icu_source',
244     help='Intl mode: optional local path to icu/ dir, or path/URL of icu source archive.')
245
246 parser.add_option('--with-perfctr',
247     action='store_true',
248     dest='with_perfctr',
249     help='build with performance counters (default is true on Windows)')
250
251 parser.add_option('--without-dtrace',
252     action='store_true',
253     dest='without_dtrace',
254     help='build without DTrace')
255
256 parser.add_option('--without-etw',
257     action='store_true',
258     dest='without_etw',
259     help='build without ETW')
260
261 parser.add_option('--without-npm',
262     action='store_true',
263     dest='without_npm',
264     help='don\'t install the bundled npm package manager')
265
266 parser.add_option('--without-perfctr',
267     action='store_true',
268     dest='without_perfctr',
269     help='build without performance counters')
270
271 parser.add_option('--with-snapshot',
272     action='store_true',
273     dest='with_snapshot',
274     help=optparse.SUPPRESS_HELP)
275
276 # Dummy option for backwards compatibility.
277 parser.add_option('--without-snapshot',
278     action='store_true',
279     dest='unused_without_snapshot',
280     help=optparse.SUPPRESS_HELP)
281
282 parser.add_option('--without-ssl',
283     action='store_true',
284     dest='without_ssl',
285     help='build without SSL')
286
287 parser.add_option('--xcode',
288     action='store_true',
289     dest='use_xcode',
290     help='generate build files for use with xcode')
291
292 (options, args) = parser.parse_args()
293
294 # set up auto-download list
295 auto_downloads = nodedownload.parse(options.download_list)
296
297
298 def warn(msg):
299   warn.warned = True
300   prefix = '\033[1m\033[93mWARNING\033[0m' if os.isatty(1) else 'WARNING'
301   print('%s: %s' % (prefix, msg))
302
303 # track if warnings occured
304 warn.warned = False
305
306 def b(value):
307   """Returns the string 'true' if value is truthy, 'false' otherwise."""
308   if value:
309     return 'true'
310   else:
311     return 'false'
312
313
314 def pkg_config(pkg):
315   cmd = os.popen('pkg-config --libs %s' % pkg, 'r')
316   libs = cmd.readline().strip()
317   ret = cmd.close()
318   if (ret): return None
319
320   cmd = os.popen('pkg-config --cflags %s' % pkg, 'r')
321   cflags = cmd.readline().strip()
322   ret = cmd.close()
323   if (ret): return None
324
325   return (libs, cflags)
326
327
328 def try_check_compiler(cc, lang):
329   try:
330     proc = subprocess.Popen(shlex.split(cc) + ['-E', '-P', '-x', lang, '-'],
331                             stdin=subprocess.PIPE, stdout=subprocess.PIPE)
332   except OSError:
333     return (False, False, '', '')
334
335   proc.stdin.write('__clang__ __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__ '
336                    '__clang_major__ __clang_minor__ __clang_patchlevel__')
337
338   values = (proc.communicate()[0].split() + ['0'] * 7)[0:7]
339   is_clang = values[0] == '1'
340   gcc_version = '%s.%s.%s' % tuple(values[1:1+3])
341   clang_version = '%s.%s.%s' % tuple(values[4:4+3])
342
343   return (True, is_clang, clang_version, gcc_version)
344
345
346 # Note: Apple clang self-reports as clang 4.2.0 and gcc 4.2.1.  It passes
347 # the version check more by accident than anything else but a more rigorous
348 # check involves checking the build number against a whitelist.  I'm not
349 # quite prepared to go that far yet.
350 def check_compiler():
351   if sys.platform == 'win32':
352     return
353
354   ok, is_clang, clang_version, gcc_version = try_check_compiler(CXX, 'c++')
355   if not ok:
356     warn('failed to autodetect C++ compiler version (CXX=%s)' % CXX)
357   elif clang_version < '3.4.0' if is_clang else gcc_version < '4.8.0':
358     warn('C++ compiler too old, need g++ 4.8 or clang++ 3.4 (CXX=%s)' % CXX)
359
360   ok, is_clang, clang_version, gcc_version = try_check_compiler(CC, 'c')
361   if not ok:
362     warn('failed to autodetect C compiler version (CC=%s)' % CC)
363   elif not is_clang and gcc_version < '4.2.0':
364     # clang 3.2 is a little white lie because any clang version will probably
365     # do for the C bits.  However, we might as well encourage people to upgrade
366     # to a version that is not completely ancient.
367     warn('C compiler too old, need gcc 4.2 or clang 3.2 (CC=%s)' % CC)
368
369
370 def cc_macros():
371   """Checks predefined macros using the CC command."""
372
373   try:
374     p = subprocess.Popen(shlex.split(CC) + ['-dM', '-E', '-'],
375                          stdin=subprocess.PIPE,
376                          stdout=subprocess.PIPE,
377                          stderr=subprocess.PIPE)
378   except OSError:
379     print '''Node.js configure error: No acceptable C compiler found!
380
381         Please make sure you have a C compiler installed on your system and/or
382         consider adjusting the CC environment variable if you installed
383         it in a non-standard prefix.
384         '''
385     sys.exit()
386
387   p.stdin.write('\n')
388   out = p.communicate()[0]
389
390   out = str(out).split('\n')
391
392   k = {}
393   for line in out:
394     lst = shlex.split(line)
395     if len(lst) > 2:
396       key = lst[1]
397       val = lst[2]
398       k[key] = val
399   return k
400
401
402 def is_arch_armv7():
403   """Check for ARMv7 instructions"""
404   cc_macros_cache = cc_macros()
405   return ('__ARM_ARCH_7__' in cc_macros_cache or
406           '__ARM_ARCH_7A__' in cc_macros_cache or
407           '__ARM_ARCH_7R__' in cc_macros_cache or
408           '__ARM_ARCH_7M__' in cc_macros_cache)
409
410
411 def is_arch_armv6():
412   """Check for ARMv6 instructions"""
413   cc_macros_cache = cc_macros()
414   return ('__ARM_ARCH_6__' in cc_macros_cache or
415           '__ARM_ARCH_6M__' in cc_macros_cache)
416
417
418 def is_arm_hard_float_abi():
419   """Check for hardfloat or softfloat eabi on ARM"""
420   # GCC versions 4.6 and above define __ARM_PCS or __ARM_PCS_VFP to specify
421   # the Floating Point ABI used (PCS stands for Procedure Call Standard).
422   # We use these as well as a couple of other defines to statically determine
423   # what FP ABI used.
424
425   return '__ARM_PCS_VFP' in cc_macros()
426
427
428 def host_arch_cc():
429   """Host architecture check using the CC command."""
430
431   k = cc_macros()
432
433   matchup = {
434     '__x86_64__'  : 'x64',
435     '__i386__'    : 'ia32',
436     '__arm__'     : 'arm',
437     '__mips__'    : 'mips',
438   }
439
440   rtn = 'ia32' # default
441
442   for i in matchup:
443     if i in k and k[i] != '0':
444       rtn = matchup[i]
445       break
446
447   return rtn
448
449
450 def host_arch_win():
451   """Host architecture check using environ vars (better way to do this?)"""
452
453   observed_arch = os.environ.get('PROCESSOR_ARCHITECTURE', 'x86')
454   arch = os.environ.get('PROCESSOR_ARCHITEW6432', observed_arch)
455
456   matchup = {
457     'AMD64'  : 'x64',
458     'x86'    : 'ia32',
459     'arm'    : 'arm',
460     'mips'   : 'mips',
461   }
462
463   return matchup.get(arch, 'ia32')
464
465
466 def configure_arm(o):
467   if options.arm_float_abi:
468     arm_float_abi = options.arm_float_abi
469   elif is_arm_hard_float_abi():
470     arm_float_abi = 'hard'
471   else:
472     arm_float_abi = 'default'
473
474   if is_arch_armv7():
475     o['variables']['arm_fpu'] = 'vfpv3'
476     o['variables']['arm_version'] = '7'
477   else:
478     o['variables']['arm_fpu'] = 'vfpv2'
479     o['variables']['arm_version'] = '6' if is_arch_armv6() else 'default'
480
481   o['variables']['arm_thumb'] = 0      # -marm
482   o['variables']['arm_float_abi'] = arm_float_abi
483
484   # Print warning when snapshot is enabled and building on armv6
485   if is_arch_armv6() and options.with_snapshot:
486     warn('when building on ARMv6, don\'t use --with-snapshot')
487
488
489 def configure_node(o):
490   if options.dest_os == 'android':
491     o['variables']['OS'] = 'android'
492   o['variables']['node_prefix'] = os.path.expanduser(options.prefix or '')
493   o['variables']['node_install_npm'] = b(not options.without_npm)
494   o['default_configuration'] = 'Debug' if options.debug else 'Release'
495
496   host_arch = host_arch_win() if os.name == 'nt' else host_arch_cc()
497   target_arch = options.dest_cpu or host_arch
498   o['variables']['host_arch'] = host_arch
499   o['variables']['target_arch'] = target_arch
500
501   if target_arch != host_arch and options.with_snapshot:
502     o['variables']['want_separate_host_toolset'] = 1
503   else:
504     o['variables']['want_separate_host_toolset'] = 0
505
506   if target_arch == 'arm':
507     configure_arm(o)
508
509   if flavor in ('solaris', 'mac', 'linux', 'freebsd'):
510     use_dtrace = not options.without_dtrace
511     # Don't enable by default on linux and freebsd
512     if flavor in ('linux', 'freebsd'):
513       use_dtrace = options.with_dtrace
514
515     if flavor == 'linux':
516       if options.systemtap_includes:
517         o['include_dirs'] += [options.systemtap_includes]
518     o['variables']['node_use_dtrace'] = b(use_dtrace)
519     o['variables']['uv_use_dtrace'] = b(use_dtrace)
520     o['variables']['uv_parent_path'] = '/deps/uv/'
521   elif options.with_dtrace:
522     raise Exception(
523        'DTrace is currently only supported on SunOS, MacOS or Linux systems.')
524   else:
525     o['variables']['node_use_dtrace'] = 'false'
526
527   # if we're on illumos based systems wrap the helper library into the
528   # executable
529   if flavor == 'solaris':
530     o['variables']['node_use_mdb'] = 'true'
531   else:
532     o['variables']['node_use_mdb'] = 'false'
533
534   if options.no_ifaddrs:
535     o['defines'] += ['SUNOS_NO_IFADDRS']
536
537   # By default, enable ETW on Windows.
538   if flavor == 'win':
539     o['variables']['node_use_etw'] = b(not options.without_etw)
540   elif options.with_etw:
541     raise Exception('ETW is only supported on Windows.')
542   else:
543     o['variables']['node_use_etw'] = 'false'
544
545   # By default, enable Performance counters on Windows.
546   if flavor == 'win':
547     o['variables']['node_use_perfctr'] = b(not options.without_perfctr)
548   elif options.with_perfctr:
549     raise Exception('Performance counter is only supported on Windows.')
550   else:
551     o['variables']['node_use_perfctr'] = 'false'
552
553   if options.tag:
554     o['variables']['node_tag'] = '-' + options.tag
555   else:
556     o['variables']['node_tag'] = ''
557
558   if options.v8_options:
559     o['variables']['node_v8_options'] = options.v8_options.replace('"', '\\"')
560
561
562 def configure_libz(o):
563   o['variables']['node_shared_zlib'] = b(options.shared_zlib)
564
565   # assume shared_zlib if one of these is set?
566   if options.shared_zlib_libpath:
567     o['libraries'] += ['-L%s' % options.shared_zlib_libpath]
568   if options.shared_zlib_libname:
569     o['libraries'] += ['-l%s' % options.shared_zlib_libname]
570   elif options.shared_zlib:
571     o['libraries'] += ['-lz']
572   if options.shared_zlib_includes:
573     o['include_dirs'] += [options.shared_zlib_includes]
574
575
576 def configure_http_parser(o):
577     o['variables']['node_shared_http_parser'] = b(options.shared_http_parser)
578
579     # assume shared http_parser if one of these is set?
580     if options.shared_http_parser_libpath:
581         o['libraries'] += ['-L%s' % options.shared_http_parser_libpath]
582     if options.shared_http_parser_libname:
583         o['libraries'] += ['-l%s' % options.shared_http_parser_libname]
584     elif options.shared_http_parser:
585         o['libraries'] += ['-lhttp_parser']
586     if options.shared_http_parser_includes:
587         o['include_dirs'] += [options.shared_http_parser_includes]
588
589
590 def configure_libuv(o):
591   o['variables']['node_shared_libuv'] = b(options.shared_libuv)
592
593   # assume shared libuv if one of these is set?
594   if options.shared_libuv_libpath:
595     o['libraries'] += ['-L%s' % options.shared_libuv_libpath]
596   else:
597     o['variables']['uv_library'] = 'static_library'
598
599   if options.shared_libuv_libname:
600     o['libraries'] += ['-l%s' % options.shared_libuv_libname]
601   elif options.shared_libuv:
602     o['libraries'] += ['-luv']
603   if options.shared_libuv_includes:
604     o['include_dirs'] += [options.shared_libuv_includes]
605
606
607 def configure_v8(o):
608   o['variables']['node_shared_v8'] = b(options.shared_v8)
609   o['variables']['v8_enable_gdbjit'] = 1 if options.gdb else 0
610   o['variables']['v8_no_strict_aliasing'] = 1  # Work around compiler bugs.
611   o['variables']['v8_optimized_debug'] = 0  # Compile with -O0 in debug builds.
612   o['variables']['v8_random_seed'] = 0  # Use a random seed for hash tables.
613   o['variables']['v8_use_snapshot'] = b(options.with_snapshot)
614
615   # assume shared_v8 if one of these is set?
616   if options.shared_v8_libpath:
617     o['libraries'] += ['-L%s' % options.shared_v8_libpath]
618   if options.shared_v8_libname:
619     o['libraries'] += ['-l%s' % options.shared_v8_libname]
620   elif options.shared_v8:
621     o['libraries'] += ['-lv8']
622   if options.shared_v8_includes:
623     o['include_dirs'] += [options.shared_v8_includes]
624
625
626 def configure_openssl(o):
627   o['variables']['node_use_openssl'] = b(not options.without_ssl)
628   o['variables']['node_shared_openssl'] = b(options.shared_openssl)
629   o['variables']['openssl_no_asm'] = (
630     1 if options.openssl_no_asm else 0)
631
632   if options.without_ssl:
633     return
634
635   if options.shared_openssl:
636     (libs, cflags) = pkg_config('openssl') or ('-lssl -lcrypto', '')
637
638     if options.shared_openssl_libpath:
639       o['libraries'] += ['-L%s' % options.shared_openssl_libpath]
640
641     if options.shared_openssl_libname:
642       libnames = options.shared_openssl_libname.split(',')
643       o['libraries'] += ['-l%s' % s for s in libnames]
644     else:
645       o['libraries'] += libs.split()
646
647     if options.shared_openssl_includes:
648       o['include_dirs'] += [options.shared_openssl_includes]
649     else:
650       o['cflags'] += cflags.split()
651
652
653 def configure_fullystatic(o):
654   if options.fully_static:
655     o['libraries'] += ['-static']
656     if flavor == 'mac':
657       print("Generation of static executable will not work on OSX "
658             "when using default compilation environment")
659
660
661 def configure_winsdk(o):
662   if flavor != 'win':
663     return
664
665   winsdk_dir = os.environ.get('WindowsSdkDir')
666
667   if winsdk_dir and os.path.isfile(winsdk_dir + '\\bin\\ctrpp.exe'):
668     print('Found ctrpp in WinSDK--will build generated files '
669           'into tools/msvs/genfiles.')
670     o['variables']['node_has_winsdk'] = 'true'
671     return
672
673   print('ctrpp not found in WinSDK path--using pre-gen files '
674         'from tools/msvs/genfiles.')
675
676 def write(filename, data):
677   filename = os.path.join(root_dir, filename)
678   print 'creating ', filename
679   f = open(filename, 'w+')
680   f.write(data)
681
682 do_not_edit = '# Do not edit. Generated by the configure script.\n'
683
684 def glob_to_var(dir_base, dir_sub):
685   list = []
686   dir_all = os.path.join(dir_base, dir_sub)
687   files = os.walk(dir_all)
688   for ent in files:
689     (path, dirs, files) = ent
690     for file in files:
691       if file.endswith('.cpp') or file.endswith('.c') or file.endswith('.h'):
692         list.append('%s/%s' % (dir_sub, file))
693     break
694   return list
695
696 def configure_intl(o):
697   icus = [
698     {
699       'url': 'http://download.icu-project.org/files/icu4c/54.1/icu4c-54_1-src.zip',
700       # from https://ssl.icu-project.org/files/icu4c/54.1/icu4c-src-54_1.md5:
701       'md5': '6b89d60e2f0e140898ae4d7f72323bca',
702     },
703   ]
704   def icu_download(path):
705     # download ICU, if needed
706     for icu in icus:
707       url = icu['url']
708       md5 = icu['md5']
709       local = url.split('/')[-1]
710       targetfile = os.path.join(root_dir, 'deps', local)
711       if not os.path.isfile(targetfile):
712         if nodedownload.candownload(auto_downloads, "icu"):
713           nodedownload.retrievefile(url, targetfile)
714       else:
715         print ' Re-using existing %s' % targetfile
716       if os.path.isfile(targetfile):
717         sys.stdout.write(' Checking file integrity with MD5:\r')
718         gotmd5 = nodedownload.md5sum(targetfile)
719         print ' MD5:      %s  %s' % (gotmd5, targetfile)
720         if (md5 == gotmd5):
721           return targetfile
722         else:
723           print ' Expected: %s      *MISMATCH*' % md5
724           print '\n ** Corrupted ZIP? Delete %s to retry download.\n' % targetfile
725     return None
726   icu_config = {
727     'variables': {}
728   }
729   icu_config_name = 'icu_config.gypi'
730   def write_config(data, name):
731     return
732
733   # write an empty file to start with
734   write(icu_config_name, do_not_edit +
735         pprint.pformat(icu_config, indent=2) + '\n')
736
737   # always set icu_small, node.gyp depends on it being defined.
738   o['variables']['icu_small'] = b(False)
739
740   with_intl = options.with_intl
741   with_icu_source = options.with_icu_source
742   have_icu_path = bool(options.with_icu_path)
743   if have_icu_path and with_intl:
744     print 'Error: Cannot specify both --with-icu-path and --with-intl'
745     sys.exit(1)
746   elif have_icu_path:
747     # Chromium .gyp mode: --with-icu-path
748     o['variables']['v8_enable_i18n_support'] = 1
749     # use the .gyp given
750     o['variables']['icu_gyp_path'] = options.with_icu_path
751     return
752   # --with-intl=<with_intl>
753   # set the default
754   if with_intl is None:
755     with_intl = 'none'  # The default mode of Intl
756   # sanity check localelist
757   if options.with_icu_locales and (with_intl != 'small-icu'):
758     print 'Error: --with-icu-locales only makes sense with --with-intl=small-icu'
759     sys.exit(1)
760   if with_intl == 'none' or with_intl is None:
761     o['variables']['v8_enable_i18n_support'] = 0
762     return  # no Intl
763   elif with_intl == 'small-icu':
764     # small ICU (English only)
765     o['variables']['v8_enable_i18n_support'] = 1
766     o['variables']['icu_small'] = b(True)
767     with_icu_locales = options.with_icu_locales
768     if not with_icu_locales:
769       with_icu_locales = 'root,en'
770     locs = set(with_icu_locales.split(','))
771     locs.add('root')  # must have root
772     o['variables']['icu_locales'] = string.join(locs,',')
773   elif with_intl == 'full-icu':
774     # full ICU
775     o['variables']['v8_enable_i18n_support'] = 1
776   elif with_intl == 'system-icu':
777     # ICU from pkg-config.
778     o['variables']['v8_enable_i18n_support'] = 1
779     pkgicu = pkg_config('icu-i18n')
780     if not pkgicu:
781       print 'Error: could not load pkg-config data for "icu-i18n".'
782       print 'See above errors or the README.md.'
783       sys.exit(1)
784     (libs, cflags) = pkgicu
785     o['libraries'] += libs.split()
786     o['cflags'] += cflags.split()
787     # use the "system" .gyp
788     o['variables']['icu_gyp_path'] = 'tools/icu/icu-system.gyp'
789     return
790   else:
791     print 'Error: unknown value --with-intl=%s' % with_intl
792     sys.exit(1)
793   # Note: non-ICU implementations could use other 'with_intl'
794   # values.
795
796   # this is just the 'deps' dir. Used for unpacking.
797   icu_parent_path = os.path.join(root_dir, 'deps')
798
799   # The full path to the ICU source directory.
800   icu_full_path = os.path.join(icu_parent_path, 'icu')
801
802   # icu-tmp is used to download and unpack the ICU tarball.
803   icu_tmp_path = os.path.join(icu_parent_path, 'icu-tmp')
804
805   # --with-icu-source processing
806   # first, check that they didn't pass --with-icu-source=deps/icu
807   if with_icu_source and os.path.abspath(icu_full_path) == os.path.abspath(with_icu_source):
808     print 'Ignoring redundant --with-icu-source=%s' % (with_icu_source)
809     with_icu_source = None
810   # if with_icu_source is still set, try to use it.
811   if with_icu_source:
812     if os.path.isdir(icu_full_path):
813       print 'Deleting old ICU source: %s' % (icu_full_path)
814       shutil.rmtree(icu_full_path)
815     # now, what path was given?
816     if os.path.isdir(with_icu_source):
817       # it's a path. Copy it.
818       print '%s -> %s' % (with_icu_source, icu_full_path)
819       shutil.copytree(with_icu_source, icu_full_path)
820     else:
821       # could be file or URL.
822       # Set up temporary area
823       if os.path.isdir(icu_tmp_path):
824         shutil.rmtree(icu_tmp_path)
825       os.mkdir(icu_tmp_path)
826       icu_tarball = None
827       if os.path.isfile(with_icu_source):
828         # it's a file. Try to unpack it.
829         icu_tarball = with_icu_source
830       else:
831         # Can we download it?
832         local = os.path.join(icu_tmp_path, with_icu_source.split('/')[-1])  # local part
833         icu_tarball = nodedownload.retrievefile(with_icu_source, local)
834       # continue with "icu_tarball"
835       nodedownload.unpack(icu_tarball, icu_tmp_path)
836       # Did it unpack correctly? Should contain 'icu'
837       tmp_icu = os.path.join(icu_tmp_path, 'icu')
838       if os.path.isdir(tmp_icu):
839         os.rename(tmp_icu, icu_full_path)
840         shutil.rmtree(icu_tmp_path)
841       else:
842         print ' Error: --with-icu-source=%s did not result in an "icu" dir.' % with_icu_source
843         shutil.rmtree(icu_tmp_path)
844         sys.exit(1)
845
846   # ICU mode. (icu-generic.gyp)
847   o['variables']['icu_gyp_path'] = 'tools/icu/icu-generic.gyp'
848   # ICU source dir relative to root
849   o['variables']['icu_path'] = icu_full_path
850   if not os.path.isdir(icu_full_path):
851     print '* ECMA-402 (Intl) support didn\'t find ICU in %s..' % (icu_full_path)
852     # can we download (or find) a zipfile?
853     localzip = icu_download(icu_full_path)
854     if localzip:
855       nodedownload.unpack(localzip, icu_parent_path)
856   if not os.path.isdir(icu_full_path):
857     print ' Cannot build Intl without ICU in %s.' % (icu_full_path)
858     print ' (Fix, or disable with "--with-intl=none" )'
859     sys.exit(1)
860   else:
861     print '* Using ICU in %s' % (icu_full_path)
862   # Now, what version of ICU is it? We just need the "major", such as 54.
863   # uvernum.h contains it as a #define.
864   uvernum_h = os.path.join(icu_full_path, 'source/common/unicode/uvernum.h')
865   if not os.path.isfile(uvernum_h):
866     print ' Error: could not load %s - is ICU installed?' % uvernum_h
867     sys.exit(1)
868   icu_ver_major = None
869   matchVerExp = r'^\s*#define\s+U_ICU_VERSION_SHORT\s+"([^"]*)".*'
870   match_version = re.compile(matchVerExp)
871   for line in open(uvernum_h).readlines():
872     m = match_version.match(line)
873     if m:
874       icu_ver_major = m.group(1)
875   if not icu_ver_major:
876     print ' Could not read U_ICU_VERSION_SHORT version from %s' % uvernum_h
877     sys.exit(1)
878   icu_endianness = sys.byteorder[0];  # TODO(srl295): EBCDIC should be 'e'
879   o['variables']['icu_ver_major'] = icu_ver_major
880   o['variables']['icu_endianness'] = icu_endianness
881   icu_data_file_l = 'icudt%s%s.dat' % (icu_ver_major, 'l')
882   icu_data_file = 'icudt%s%s.dat' % (icu_ver_major, icu_endianness)
883   # relative to configure
884   icu_data_path = os.path.join(icu_full_path,
885                                'source/data/in',
886                                icu_data_file_l)
887   # relative to dep..
888   icu_data_in = os.path.join('../../deps/icu/source/data/in', icu_data_file_l)
889   if not os.path.isfile(icu_data_path) and icu_endianness != 'l':
890     # use host endianness
891     icu_data_path = os.path.join(icu_full_path,
892                                  'source/data/in',
893                                  icu_data_file)
894     # relative to dep..
895     icu_data_in = os.path.join('icu/source/data/in',
896                                icu_data_file)
897   # this is the input '.dat' file to use .. icudt*.dat
898   # may be little-endian if from a icu-project.org tarball
899   o['variables']['icu_data_in'] = icu_data_in
900   # this is the icudt*.dat file which node will be using (platform endianness)
901   o['variables']['icu_data_file'] = icu_data_file
902   if not os.path.isfile(icu_data_path):
903     print ' Error: ICU prebuilt data file %s does not exist.' % icu_data_path
904     print ' See the README.md.'
905     # .. and we're not about to build it from .gyp!
906     sys.exit(1)
907   # map from variable name to subdirs
908   icu_src = {
909     'stubdata': 'stubdata',
910     'common': 'common',
911     'i18n': 'i18n',
912     'io': 'io',
913     'tools': 'tools/toolutil',
914     'genccode': 'tools/genccode',
915     'genrb': 'tools/genrb',
916     'icupkg': 'tools/icupkg',
917   }
918   # this creates a variable icu_src_XXX for each of the subdirs
919   # with a list of the src files to use
920   for i in icu_src:
921     var  = 'icu_src_%s' % i
922     path = '../../deps/icu/source/%s' % icu_src[i]
923     icu_config['variables'][var] = glob_to_var('tools/icu', path)
924   # write updated icu_config.gypi with a bunch of paths
925   write(icu_config_name, do_not_edit +
926         pprint.pformat(icu_config, indent=2) + '\n')
927   return  # end of configure_intl
928
929 # Print a warning when the compiler is too old.
930 check_compiler()
931
932 # determine the "flavor" (operating system) we're building for,
933 # leveraging gyp's GetFlavor function
934 flavor_params = {}
935 if (options.dest_os):
936   flavor_params['flavor'] = options.dest_os
937 flavor = GetFlavor(flavor_params)
938
939 output = {
940   'variables': { 'python': sys.executable },
941   'include_dirs': [],
942   'libraries': [],
943   'defines': [],
944   'cflags': [],
945 }
946
947 configure_node(output)
948 configure_libz(output)
949 configure_http_parser(output)
950 configure_libuv(output)
951 configure_v8(output)
952 configure_openssl(output)
953 configure_winsdk(output)
954 configure_intl(output)
955 configure_fullystatic(output)
956
957 # variables should be a root level element,
958 # move everything else to target_defaults
959 variables = output['variables']
960 del output['variables']
961 output = {
962   'variables': variables,
963   'target_defaults': output
964 }
965 pprint.pprint(output, indent=2)
966
967 write('config.gypi', do_not_edit +
968       pprint.pformat(output, indent=2) + '\n')
969
970 config = {
971   'BUILDTYPE': 'Debug' if options.debug else 'Release',
972   'USE_XCODE': str(int(options.use_xcode or 0)),
973   'PYTHON': sys.executable,
974 }
975
976 if options.prefix:
977   config['PREFIX'] = options.prefix
978
979 config = '\n'.join(map('='.join, config.iteritems())) + '\n'
980
981 write('config.mk',
982       '# Do not edit. Generated by the configure script.\n' + config)
983
984 gyp_args = [sys.executable, 'tools/gyp_node.py', '--no-parallel']
985
986 if options.use_xcode:
987   gyp_args += ['-f', 'xcode']
988 elif flavor == 'win' and sys.platform != 'msys':
989   gyp_args += ['-f', 'msvs', '-G', 'msvs_version=auto']
990 else:
991   gyp_args += ['-f', 'make-' + flavor]
992
993 gyp_args += args
994
995 if warn.warned:
996   warn('warnings were emitted in the configure phase')
997
998 sys.exit(subprocess.call(gyp_args))