Fix the Windows build
[platform/upstream/nodejs.git] / configure
1 #!/usr/bin/env python
2 import optparse
3 import os
4 import pprint
5 import re
6 import subprocess
7 import sys
8
9 CC = os.environ.get('CC', 'cc')
10
11 root_dir = os.path.dirname(__file__)
12 sys.path.insert(0, os.path.join(root_dir, 'deps', 'v8', 'tools'))
13
14 # parse our options
15 parser = optparse.OptionParser()
16
17 parser.add_option("--debug",
18     action="store_true",
19     dest="debug",
20     help="Also build debug build")
21
22 parser.add_option("--prefix",
23     action="store",
24     dest="prefix",
25     help="Select the install prefix (defaults to /usr/local)")
26
27 parser.add_option("--without-npm",
28     action="store_true",
29     dest="without_npm",
30     help="Don\'t install the bundled npm package manager")
31
32 parser.add_option("--without-waf",
33     action="store_true",
34     dest="without_waf",
35     help="Don\'t install node-waf")
36
37 parser.add_option("--without-ssl",
38     action="store_true",
39     dest="without_ssl",
40     help="Build without SSL")
41
42 parser.add_option("--without-snapshot",
43     action="store_true",
44     dest="without_snapshot",
45     help="Build without snapshotting V8 libraries. You might want to set"
46          " this for cross-compiling. [Default: False]")
47
48 parser.add_option("--shared-v8",
49     action="store_true",
50     dest="shared_v8",
51     help="Link to a shared V8 DLL instead of static linking")
52
53 parser.add_option("--shared-v8-includes",
54     action="store",
55     dest="shared_v8_includes",
56     help="Directory containing V8 header files")
57
58 parser.add_option("--shared-v8-libpath",
59     action="store",
60     dest="shared_v8_libpath",
61     help="A directory to search for the shared V8 DLL")
62
63 parser.add_option("--shared-v8-libname",
64     action="store",
65     dest="shared_v8_libname",
66     help="Alternative lib name to link to (default: 'v8')")
67
68 parser.add_option("--shared-openssl",
69     action="store_true",
70     dest="shared_openssl",
71     help="Link to a shared OpenSSl DLL instead of static linking")
72
73 parser.add_option("--shared-openssl-includes",
74     action="store",
75     dest="shared_openssl_includes",
76     help="Directory containing OpenSSL header files")
77
78 parser.add_option("--shared-openssl-libpath",
79     action="store",
80     dest="shared_openssl_libpath",
81     help="A directory to search for the shared OpenSSL DLLs")
82
83 parser.add_option("--shared-openssl-libname",
84     action="store",
85     dest="shared_openssl_libname",
86     help="Alternative lib name to link to (default: 'crypto,ssl')")
87
88 # deprecated
89 parser.add_option("--openssl-use-sys",
90     action="store_true",
91     dest="shared_openssl",
92     help=optparse.SUPPRESS_HELP)
93
94 # deprecated
95 parser.add_option("--openssl-includes",
96     action="store",
97     dest="shared_openssl_includes",
98     help=optparse.SUPPRESS_HELP)
99
100 # deprecated
101 parser.add_option("--openssl-libpath",
102     action="store",
103     dest="shared_openssl_libpath",
104     help=optparse.SUPPRESS_HELP)
105
106 parser.add_option("--no-ssl2",
107     action="store_true",
108     dest="no_ssl2",
109     help="Disable OpenSSL v2")
110
111 parser.add_option("--shared-zlib",
112     action="store_true",
113     dest="shared_zlib",
114     help="Link to a shared zlib DLL instead of static linking")
115
116 parser.add_option("--shared-zlib-includes",
117     action="store",
118     dest="shared_zlib_includes",
119     help="Directory containing zlib header files")
120
121 parser.add_option("--shared-zlib-libpath",
122     action="store",
123     dest="shared_zlib_libpath",
124     help="A directory to search for the shared zlib DLL")
125
126 parser.add_option("--shared-zlib-libname",
127     action="store",
128     dest="shared_zlib_libname",
129     help="Alternative lib name to link to (default: 'z')")
130
131 parser.add_option("--with-dtrace",
132     action="store_true",
133     dest="with_dtrace",
134     help="Build with DTrace (default is true on supported systems)")
135
136 parser.add_option("--without-dtrace",
137     action="store_true",
138     dest="without_dtrace",
139     help="Build without DTrace")
140
141 parser.add_option("--with-etw",
142     action="store_true",
143     dest="with_etw",
144     help="Build with ETW (default is true on Windows)")
145
146 parser.add_option("--without-etw",
147     action="store_true",
148     dest="without_etw",
149     help="Build without ETW")
150
151 # CHECKME does this still work with recent releases of V8?
152 parser.add_option("--gdb",
153     action="store_true",
154     dest="gdb",
155     help="add gdb support")
156
157 parser.add_option("--dest-cpu",
158     action="store",
159     dest="dest_cpu",
160     help="CPU architecture to build for. Valid values are: arm, ia32, x64")
161
162 parser.add_option("--no-ifaddrs",
163     action="store_true",
164     dest="no_ifaddrs",
165     help="Use on deprecated SunOS systems that do not support ifaddrs.h")
166
167 (options, args) = parser.parse_args()
168
169
170 def b(value):
171   """Returns the string 'true' if value is truthy, 'false' otherwise."""
172   if value:
173     return 'true'
174   else:
175     return 'false'
176
177
178 def pkg_config(pkg):
179   cmd = os.popen('pkg-config --libs %s' % pkg, 'r')
180   libs = cmd.readline().strip()
181   ret = cmd.close()
182   if (ret): return None
183
184   cmd = os.popen('pkg-config --cflags %s' % pkg, 'r')
185   cflags = cmd.readline().strip()
186   ret = cmd.close()
187   if (ret): return None
188
189   return (libs, cflags)
190
191
192 def host_arch_cc():
193   """Host architecture check using the CC command."""
194
195   try:
196     p = subprocess.Popen(CC.split() + ['-dM', '-E', '-'],
197                          stdin=subprocess.PIPE,
198                          stdout=subprocess.PIPE,
199                          stderr=subprocess.PIPE)
200   except OSError:
201     print '''Node.js configure error: No acceptable C compiler found!
202
203         Please make sure you have a C compiler installed on your system and/or
204         consider adjusting the CC environment variable if you installed
205         it in a non-standard prefix.
206         '''
207     sys.exit()
208
209   p.stdin.write('\n')
210   out = p.communicate()[0]
211
212   out = str(out).split('\n')
213
214   k = {}
215   for line in out:
216     import shlex
217     lst = shlex.split(line)
218     if len(lst) > 2:
219       key = lst[1]
220       val = lst[2]
221       k[key] = val
222
223   matchup = {
224     '__x86_64__'  : 'x64',
225     '__i386__'    : 'ia32',
226     '__arm__'     : 'arm',
227   }
228
229   rtn = 'ia32' # default
230
231   for i in matchup:
232     if i in k and k[i] != '0':
233       rtn = matchup[i]
234       break
235
236   return rtn
237
238
239 def host_arch_win():
240   """Host architecture check using environ vars (better way to do this?)"""
241
242   arch = os.environ.get('PROCESSOR_ARCHITECTURE', 'x86')
243
244   matchup = {
245     'AMD64'  : 'x64',
246     'x86'    : 'ia32',
247     'arm'    : 'arm',
248   }
249
250   return matchup.get(arch, 'ia32')
251
252
253 def host_arch():
254   """Host architecture. One of arm, ia32 or x64."""
255   if os.name == 'nt':
256     arch = host_arch_win()
257   else:
258     arch = host_arch_cc()
259   return arch
260
261
262 def target_arch():
263   return host_arch()
264
265
266 def compiler_version():
267   try:
268     proc = subprocess.Popen(CC.split() + ['--version'], stdout=subprocess.PIPE)
269   except WindowsError:
270     return (0, False)
271
272   is_clang = 'clang' in proc.communicate()[0].split('\n')[0]
273
274   proc = subprocess.Popen(CC.split() + ['-dumpversion'], stdout=subprocess.PIPE)
275   version = tuple(map(int, proc.communicate()[0].split('.')))
276
277   return (version, is_clang)
278
279
280 def configure_node(o):
281   # TODO add gdb
282   o['variables']['node_prefix'] = os.path.expanduser(options.prefix or '')
283   o['variables']['node_install_npm'] = b(not options.without_npm)
284   o['variables']['node_install_waf'] = b(not options.without_waf)
285   o['variables']['host_arch'] = host_arch()
286   o['variables']['target_arch'] = options.dest_cpu or target_arch()
287   o['default_configuration'] = 'Debug' if options.debug else 'Release'
288
289   cc_version, is_clang = compiler_version()
290
291   # turn off strict aliasing if gcc < 4.6.0 unless it's llvm-gcc
292   # see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45883
293   # see http://code.google.com/p/v8/issues/detail?id=884
294   no_strict_aliasing = int(not(is_clang or cc_version >= (4,6,0)))
295   o['variables']['v8_no_strict_aliasing'] = no_strict_aliasing
296   o['variables']['node_no_strict_aliasing'] = no_strict_aliasing
297
298   # clang has always supported -fvisibility=hidden, right?
299   if not is_clang and cc_version < (4,0,0):
300     o['variables']['visibility'] = ''
301
302   # By default, enable DTrace on SunOS systems. Don't allow it on other
303   # systems, since it won't work.  (The MacOS build process is different than
304   # SunOS, and we haven't implemented it.)
305   if sys.platform.startswith('sunos'):
306     o['variables']['node_use_dtrace'] = b(not options.without_dtrace);
307     # Strict aliasing causes problems with the V8 snapshots on SunOS
308     o['variables']['strict_aliasing'] = b(False);
309   elif b(options.with_dtrace) == 'true':
310     raise Exception('DTrace is currently only supported on SunOS systems.')
311   else:
312     o['variables']['node_use_dtrace'] = 'false'
313
314   if options.no_ifaddrs:
315     o['defines'] += ['SUNOS_NO_IFADDRS']
316
317   # By default, enable ETW on Windows.
318   if sys.platform.startswith('win32'):
319     o['variables']['node_use_etw'] = b(not options.without_etw);
320   elif b(options.with_etw) == 'true':
321     raise Exception('ETW is only supported on Windows.')
322   else:
323     o['variables']['node_use_etw'] = 'false'
324
325
326 def configure_libz(o):
327   o['variables']['node_shared_zlib'] = b(options.shared_zlib)
328
329   # assume shared_zlib if one of these is set?
330   if options.shared_zlib_libpath:
331     o['libraries'] += ['-L%s' % options.shared_zlib_libpath]
332   if options.shared_zlib_libname:
333     o['libraries'] += ['-l%s' % options.shared_zlib_libname]
334   elif options.shared_zlib:
335     o['libraries'] += ['-lz']
336   if options.shared_zlib_includes:
337     o['include_dirs'] += [options.shared_zlib_includes]
338
339
340 def configure_v8(o):
341   o['variables']['v8_use_snapshot'] = b(not options.without_snapshot)
342   o['variables']['node_shared_v8'] = b(options.shared_v8)
343
344   # assume shared_v8 if one of these is set?
345   if options.shared_v8_libpath:
346     o['libraries'] += ['-L%s' % options.shared_v8_libpath]
347   if options.shared_v8_libname:
348     o['libraries'] += ['-l%s' % options.shared_v8_libname]
349   elif options.shared_v8:
350     o['libraries'] += ['-lv8']
351   if options.shared_v8_includes:
352     o['include_dirs'] += [options.shared_v8_includes]
353
354
355 def configure_openssl(o):
356   o['variables']['node_use_openssl'] = b(not options.without_ssl)
357   o['variables']['node_shared_openssl'] = b(options.shared_openssl)
358
359   if options.without_ssl:
360     return
361
362   if options.no_ssl2:
363     o['defines'] += ['OPENSSL_NO_SSL2=1']
364
365   if options.shared_openssl:
366     (libs, cflags) = pkg_config('openssl') or ('-lssl -lcrypto', '')
367
368     if options.shared_openssl_libpath:
369       o['libraries'] += ['-L%s' % options.shared_openssl_libpath]
370
371     if options.shared_openssl_libname:
372       libnames = options.shared_openssl_libname.split(',')
373       o['libraries'] += ['-l%s' % s for s in libnames]
374     else:
375       o['libraries'] += libs.split()
376
377     if options.shared_openssl_includes:
378       o['include_dirs'] += [options.shared_openssl_includes]
379     else:
380       o['cflags'] += cflags.split()
381
382
383 output = {
384   'variables': {},
385   'include_dirs': [],
386   'libraries': [],
387   'defines': [],
388   'cflags': [],
389 }
390
391 configure_node(output)
392 configure_libz(output)
393 configure_v8(output)
394 configure_openssl(output)
395
396 # variables should be a root level element,
397 # move everything else to target_defaults
398 variables = output['variables']
399 del output['variables']
400 output = {
401   'variables': variables,
402   'target_defaults': output
403 }
404 pprint.pprint(output, indent=2)
405
406 def write(filename, data):
407   filename = os.path.join(root_dir, filename)
408   print "creating ", filename
409   f = open(filename, 'w+')
410   f.write(data)
411
412 write('config.gypi', "# Do not edit. Generated by the configure script.\n" +
413   pprint.pformat(output, indent=2) + "\n")
414
415 write('config.mk', "# Do not edit. Generated by the configure script.\n" +
416   ("BUILDTYPE=%s\n" % ('Debug' if options.debug else 'Release')))
417
418 if os.name == 'nt':
419   gyp_args = ['-f', 'msvs', '-G', 'msvs_version=2010']
420 else:
421   gyp_args = ['-f', 'make']
422
423 subprocess.call([sys.executable, 'tools/gyp_node'] + gyp_args)