test-dns so that NXDOMAIN does not rise errors
[platform/upstream/nodejs.git] / wscript
1 #!/usr/bin/env python
2 import re
3 import Options
4 import sys, os, shutil
5 from Utils import cmd_output
6 from os.path import join, dirname, abspath
7 from logging import fatal
8
9 cwd = os.getcwd()
10 VERSION="0.1.92"
11 APPNAME="node.js"
12
13 import js2c
14
15 srcdir = '.'
16 blddir = 'build'
17
18 def set_options(opt):
19   # the gcc module provides a --debug-level option
20   opt.tool_options('compiler_cxx')
21   opt.tool_options('compiler_cc')
22   opt.tool_options('misc')
23   opt.add_option( '--debug'
24                 , action='store_true'
25                 , default=False
26                 , help='Build debug variant [Default: False]'
27                 , dest='debug'
28                 )
29   opt.add_option( '--efence'
30                 , action='store_true'
31                 , default=False
32                 , help='Build with -lefence for debugging [Default: False]'
33                 , dest='efence'
34                 )
35   opt.add_option( '--system'
36                 , action='store_true'
37                 , default=False
38                 , help='Build using system libraries and headers (like a debian build) [Default: False]'
39                 , dest='system'
40                 )
41
42 def mkdir_p(dir):
43   if not os.path.exists (dir):
44     os.makedirs (dir)
45
46 # Copied from Python 2.6 because 2.4.4 at least is broken by not using
47 # mkdirs
48 # http://mail.python.org/pipermail/python-bugs-list/2005-January/027118.html
49 def copytree(src, dst, symlinks=False, ignore=None):
50     names = os.listdir(src)
51     if ignore is not None:
52         ignored_names = ignore(src, names)
53     else:
54         ignored_names = set()
55
56     os.makedirs(dst)
57     errors = []
58     for name in names:
59         if name in ignored_names:
60             continue
61         srcname = join(src, name)
62         dstname = join(dst, name)
63         try:
64             if symlinks and os.path.islink(srcname):
65                 linkto = os.readlink(srcname)
66                 os.symlink(linkto, dstname)
67             elif os.path.isdir(srcname):
68                 copytree(srcname, dstname, symlinks, ignore)
69             else:
70                 shutil.copy2(srcname, dstname)
71             # XXX What about devices, sockets etc.?
72         except (IOError, os.error), why:
73             errors.append((srcname, dstname, str(why)))
74         # catch the Error from the recursive copytree so that we can
75         # continue with other files
76         except Error, err:
77             errors.extend(err.args[0])
78     try:
79         shutil.copystat(src, dst)
80     except OSError, why:
81         if WindowsError is not None and isinstance(why, WindowsError):
82             # Copying file access times may fail on Windows
83             pass
84         else:
85             errors.extend((src, dst, str(why)))
86     if errors:
87         raise Error, errors
88
89 def conf_subproject (conf, subdir, command=None):
90   print("---- %s ----" % subdir)
91   src = join(conf.srcdir, subdir)
92   if not os.path.exists (src): conf.fatal("no such subproject " + subdir)
93
94   default_tgt = join(conf.blddir, "default", subdir)
95
96   if not os.path.exists(default_tgt):
97     copytree(src, default_tgt, True)
98
99   if command:
100     if os.system("cd \"%s\" && %s" % (default_tgt, command)) != 0:
101       conf.fatal("Configuring %s failed." % (subdir))
102
103   debug_tgt = join(conf.blddir, "debug", subdir)
104
105   if not os.path.exists(debug_tgt):
106     copytree(default_tgt, debug_tgt, True)
107
108 def configure(conf):
109   conf.check_tool('compiler_cxx')
110   if not conf.env.CXX: conf.fatal('c++ compiler not found')
111   conf.check_tool('compiler_cc')
112   if not conf.env.CC: conf.fatal('c compiler not found')
113
114   conf.env["USE_DEBUG"] = Options.options.debug
115   conf.env["USE_SYSTEM"] = Options.options.system
116
117   conf.check(lib='dl', uselib_store='DL')
118   if not sys.platform.startswith("sunos"):
119     conf.env.append_value("CCFLAGS", "-rdynamic")
120     conf.env.append_value("LINKFLAGS_DL", "-rdynamic")
121
122   if sys.platform.startswith("freebsd"):
123     conf.check(lib='kvm', uselib_store='KVM')
124
125   #if Options.options.debug:
126   #  conf.check(lib='profiler', uselib_store='PROFILER')
127
128   if Options.options.efence:
129     conf.check(lib='efence', libpath=['/usr/lib', '/usr/local/lib'], uselib_store='EFENCE')
130
131   if not conf.check(lib="execinfo", includes=['/usr/include', '/usr/local/include'], libpath=['/usr/lib', '/usr/local/lib'], uselib_store="EXECINFO"):
132     # Note on Darwin/OS X: This will fail, but will still be used as the
133     # execinfo stuff are part of the standard library.
134     if sys.platform.startswith("freebsd"):
135       conf.fatal("Install the libexecinfo port from /usr/ports/devel/libexecinfo.")
136
137   if conf.check_cfg(package='openssl',
138                     args='--cflags --libs',
139                     uselib_store='OPENSSL'):
140     conf.env["USE_OPENSSL"] = True
141     conf.env.append_value("CXXFLAGS", "-DHAVE_OPENSSL=1")
142   else:
143     libssl = conf.check_cc(lib='ssl',
144                            header_name='openssl/ssl.h',
145                            function_name='SSL_library_init',
146                            libpath=['/usr/lib', '/usr/local/lib', '/opt/local/lib', '/usr/sfw/lib'],
147                            uselib_store='OPENSSL')
148     libcrypto = conf.check_cc(lib='crypto',
149                               header_name='openssl/crypto.h',
150                               uselib_store='OPENSSL')
151     if libcrypto and libssl:
152       conf.env["USE_OPENSSL"] = True
153       conf.env.append_value("CXXFLAGS", "-DHAVE_OPENSSL=1")
154
155   conf.check(lib='rt', uselib_store='RT')
156
157   if sys.platform.startswith("sunos"):
158     if not conf.check(lib='socket', uselib_store="SOCKET"):
159       conf.fatal("Cannot find socket library")
160     if not conf.check(lib='nsl', uselib_store="NSL"):
161       conf.fatal("Cannot find nsl library")
162
163   conf.sub_config('deps/libeio')
164   if not Options.options.system:
165     conf.sub_config('deps/libev')
166     conf.sub_config('deps/c-ares')
167   else:
168     if not conf.check(lib='v8', uselib_store='V8'):
169       conf.fatal("Cannot find V8")
170     if not conf.check(lib='ev', uselib_store='EV'):
171       conf.fatal("Cannot find libev")
172     if not conf.check(lib='cares', uselib_store='CARES'):
173       conf.fatal("Cannot find c-ares")
174
175   conf.define("HAVE_CONFIG_H", 1)
176
177   if sys.platform.startswith("sunos"):
178     conf.env.append_value ('CCFLAGS', '-threads')
179     conf.env.append_value ('CXXFLAGS', '-threads')
180     #conf.env.append_value ('LINKFLAGS', ' -threads')
181   else:
182     threadflags='-pthread'
183     conf.env.append_value ('CCFLAGS', threadflags)
184     conf.env.append_value ('CXXFLAGS', threadflags)
185     conf.env.append_value ('LINKFLAGS', threadflags)
186
187   conf.env.append_value("CCFLAGS", "-DX_STACKSIZE=%d" % (1024*64))
188
189   # LFS
190   conf.env.append_value('CCFLAGS',  '-D_LARGEFILE_SOURCE')
191   conf.env.append_value('CXXFLAGS', '-D_LARGEFILE_SOURCE')
192   conf.env.append_value('CCFLAGS',  '-D_FILE_OFFSET_BITS=64')
193   conf.env.append_value('CXXFLAGS', '-D_FILE_OFFSET_BITS=64')
194
195   # platform
196   platform_def = '-DPLATFORM=' + sys.platform
197   conf.env.append_value('CCFLAGS', platform_def)
198   conf.env.append_value('CXXFLAGS', platform_def)
199
200   # Split off debug variant before adding variant specific defines
201   debug_env = conf.env.copy()
202   conf.set_env_name('debug', debug_env)
203
204   # Configure debug variant
205   conf.setenv('debug')
206   debug_env.set_variant('debug')
207   debug_env.append_value('CCFLAGS', ['-DDEBUG', '-g', '-O0', '-Wall', '-Wextra'])
208   debug_env.append_value('CXXFLAGS', ['-DDEBUG', '-g', '-O0', '-Wall', '-Wextra'])
209   conf.write_config_header("config.h")
210
211   # Configure default variant
212   conf.setenv('default')
213   conf.env.append_value('CCFLAGS', ['-DNDEBUG', '-O3'])
214   conf.env.append_value('CXXFLAGS', ['-DNDEBUG', '-O3'])
215   conf.write_config_header("config.h")
216
217
218 def v8_cmd(bld, variant):
219   scons = join(cwd, 'tools/scons/scons.py')
220   deps_src = join(bld.path.abspath(),"deps")
221   v8dir_src = join(deps_src,"v8")
222
223   # NOTE: We want to compile V8 to export its symbols. I.E. Do not want
224   # -fvisibility=hidden. When using dlopen() it seems that the loaded DSO
225   # cannot see symbols in the executable which are hidden, even if the
226   # executable is statically linked together...
227
228   # XXX Remove this when v8 defaults x86_64 to native builds
229   arch = ""
230   if bld.env['DEST_CPU'] == 'x86_64':
231     arch = "arch=x64"
232
233   if variant == "default":
234     mode = "release"
235   else:
236     mode = "debug"
237
238   cmd_R = 'python "%s" -C "%s" -Y "%s" visibility=default mode=%s %s library=static snapshot=on'
239
240   cmd = cmd_R % ( scons
241                 , bld.srcnode.abspath(bld.env_of_name(variant))
242                 , v8dir_src
243                 , mode
244                 , arch
245                 )
246   return cmd
247
248
249 def build_v8(bld):
250   v8 = bld.new_task_gen(
251     source        = 'deps/v8/SConstruct ' 
252                   + bld.path.ant_glob('v8/include/*') 
253                   + bld.path.ant_glob('v8/src/*'),
254     target        = bld.env["staticlib_PATTERN"] % "v8",
255     rule          = v8_cmd(bld, "default"),
256     before        = "cxx",
257     install_path  = None
258   )
259   v8.uselib = "EXECINFO"
260   bld.env["CPPPATH_V8"] = "deps/v8/include"
261   t = join(bld.srcnode.abspath(bld.env_of_name("default")), v8.target)
262   bld.env_of_name('default').append_value("LINKFLAGS_V8", t)
263
264
265   ### v8 debug
266   if bld.env["USE_DEBUG"]:
267     v8_debug = v8.clone("debug")
268     v8_debug.rule   = v8_cmd(bld, "debug")
269     v8_debug.target = bld.env["staticlib_PATTERN"] % "v8_g"
270     v8_debug.uselib = "EXECINFO"
271     t = join(bld.srcnode.abspath(bld.env_of_name("debug")), v8_debug.target)
272     bld.env_of_name('debug').append_value("LINKFLAGS_V8", t)
273
274   bld.install_files('${PREFIX}/include/node/', 'deps/v8/include/*.h')
275
276 def build(bld):
277   print "DEST_OS: " + bld.env['DEST_OS']
278   print "DEST_CPU: " + bld.env['DEST_CPU']
279
280   if not bld.env["USE_SYSTEM"]:
281     bld.add_subdirs('deps/libeio deps/libev deps/c-ares')
282     build_v8(bld)
283   else:
284     bld.add_subdirs('deps/libeio')
285
286
287   ### http_parser
288   http_parser = bld.new_task_gen("cc")
289   http_parser.source = "deps/http_parser/http_parser.c"
290   http_parser.includes = "deps/http_parser/"
291   http_parser.name = "http_parser"
292   http_parser.target = "http_parser"
293   http_parser.install_path = None
294   if bld.env["USE_DEBUG"]:
295     http_parser.clone("debug")
296
297   ### coupling
298   coupling = bld.new_task_gen("cc")
299   coupling.source = "deps/coupling/coupling.c"
300   coupling.includes = "deps/coupling/"
301   coupling.name = "coupling"
302   coupling.target = "coupling"
303   coupling.install_path = None
304   if bld.env["USE_DEBUG"]:
305     coupling.clone("debug")
306
307   ### src/native.cc
308   def make_macros(loc, content):
309     f = open(loc, 'w')
310     f.write(content)
311     f.close
312
313   macros_loc_debug   = join(
314      bld.srcnode.abspath(bld.env_of_name("debug")),
315      "macros.py"
316   )
317
318   macros_loc_default = join(
319     bld.srcnode.abspath(bld.env_of_name("default")),
320     "macros.py"
321   )
322
323   make_macros(macros_loc_debug, "")  # leave debug(x) as is in debug build
324   # replace debug(x) with nothing in release build
325   make_macros(macros_loc_default, "macro debug(x) = ;\n")
326
327   def javascript_in_c(task):
328     env = task.env
329     source = map(lambda x: x.srcpath(env), task.inputs)
330     targets = map(lambda x: x.srcpath(env), task.outputs)
331     source.append(macros_loc_default)
332     js2c.JS2C(source, targets)
333
334   def javascript_in_c_debug(task):
335     env = task.env
336     source = map(lambda x: x.srcpath(env), task.inputs)
337     targets = map(lambda x: x.srcpath(env), task.outputs)
338     source.append(macros_loc_debug)
339     js2c.JS2C(source, targets)
340
341   native_cc = bld.new_task_gen(
342     source='src/node.js ' + bld.path.ant_glob('lib/*.js'),
343     target="src/node_natives.h",
344     before="cxx",
345     install_path=None
346   )
347
348   # Add the rule /after/ cloning the debug
349   # This is a work around for an error had in python 2.4.3 (I'll paste the
350   # error that was had into the git commit meessage. git-blame to find out
351   # where.)
352   if bld.env["USE_DEBUG"]:
353     native_cc_debug = native_cc.clone("debug")
354     native_cc_debug.rule = javascript_in_c_debug
355
356   native_cc.rule = javascript_in_c
357
358   ### node lib
359   node = bld.new_task_gen("cxx", "program")
360   node.name         = "node"
361   node.target       = "node"
362   node.source = """
363     src/node.cc
364     src/node_buffer.cc
365     src/node_http_parser.cc
366     src/node_net2.cc
367     src/node_io_watcher.cc
368     src/node_child_process.cc
369     src/node_constants.cc
370     src/node_cares.cc
371     src/node_events.cc
372     src/node_file.cc
373     src/node_signal_watcher.cc
374     src/node_stat_watcher.cc
375     src/node_stdio.cc
376     src/node_timer.cc
377     src/node_script.cc
378   """
379   if bld.env["USE_OPENSSL"]:
380     node.source += "src/node_crypto.cc"
381
382   if not bld.env["USE_SYSTEM"]:
383     node.includes = """
384       src/ 
385       deps/v8/include
386       deps/libev
387       deps/c-ares
388       deps/libeio
389       deps/http_parser
390       deps/coupling
391     """
392
393     node.includes += ' deps/c-ares/' + bld.env['DEST_OS'] + '-' + bld.env['DEST_CPU']
394
395
396     node.add_objects = 'cares ev eio http_parser coupling'
397     node.uselib_local = ''
398     node.uselib = 'RT OPENSSL V8 EXECINFO DL KVM SOCKET NSL'
399   else:
400     node.includes = """
401       src/
402       deps/libeio
403       deps/http_parser
404       deps/coupling
405     """
406     node.add_objects = 'eio http_parser coupling'
407     node.uselib_local = 'eio'
408     node.uselib = 'RT EV OPENSSL CARES V8 EXECINFO DL KVM SOCKET NSL'
409
410   node.install_path = '${PREFIX}/lib'
411   node.install_path = '${PREFIX}/bin'
412   node.chmod = 0755
413
414   def subflags(program):
415     if os.path.exists(join(cwd, ".git")):
416       actual_version=cmd_output("git describe").strip()
417     else:
418       actual_version=VERSION
419
420     x = { 'CCFLAGS'   : " ".join(program.env["CCFLAGS"])
421         , 'CPPFLAGS'  : " ".join(program.env["CPPFLAGS"])
422         , 'LIBFLAGS'  : " ".join(program.env["LIBFLAGS"])
423         , 'VERSION'   : actual_version
424         , 'PREFIX'    : program.env["PREFIX"]
425         }
426     return x
427
428   # process file.pc.in -> file.pc
429
430   node_version = bld.new_task_gen('subst', before="cxx")
431   node_version.source = 'src/node_version.h.in'
432   node_version.target = 'src/node_version.h'
433   node_version.dict = subflags(node)
434   node_version.install_path = '${PREFIX}/include/node'
435
436   if bld.env["USE_DEBUG"]:
437     node_g = node.clone("debug")
438     node_g.target = "node_g"
439     
440     node_version_g = node_version.clone("debug")
441     node_version_g.dict = subflags(node_g)
442     node_version_g.install_path = None
443
444
445   bld.install_files('${PREFIX}/include/node/', """
446     config.h
447     src/node.h
448     src/node_object_wrap.h
449     src/node_events.h
450   """)
451
452   # Only install the man page if it exists. 
453   # Do 'make doc install' to build and install it.
454   if os.path.exists('doc/node.1'):
455     bld.install_files('${PREFIX}/share/man/man1/', 'doc/node.1')
456
457   bld.install_files('${PREFIX}/bin/', 'bin/*', chmod=0755)
458
459   # Why am I using two lines? Because WAF SUCKS.
460   bld.install_files('${PREFIX}/lib/node/wafadmin', 'tools/wafadmin/*.py')
461   bld.install_files('${PREFIX}/lib/node/wafadmin/Tools', 'tools/wafadmin/Tools/*.py')
462
463 def shutdown():
464   Options.options.debug
465   # HACK to get binding.node out of build directory.
466   # better way to do this?
467   if not Options.commands['clean']:
468     if os.path.exists('build/default/node') and not os.path.exists('node'):
469       os.symlink('build/default/node', 'node')
470     if os.path.exists('build/debug/node_g') and not os.path.exists('node_g'):
471       os.symlink('build/debug/node_g', 'node_g')
472   else:
473     if os.path.exists('node'): os.unlink('node')
474     if os.path.exists('node_g'): os.unlink('node_g')