5 from Utils import cmd_output
6 from os.path import join, dirname, abspath
7 from logging import fatal
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'
26 , help='Build debug variant [Default: False]'
29 opt.add_option( '--efence'
32 , help='Build with -lefence for debugging [Default: False]'
35 opt.add_option( '--system'
38 , help='Build using system libraries and headers (like a debian build) [Default: False]'
43 if not os.path.exists (dir):
46 # Copied from Python 2.6 because 2.4.4 at least is broken by not using
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)
59 if name in ignored_names:
61 srcname = join(src, name)
62 dstname = join(dst, name)
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)
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
77 errors.extend(err.args[0])
79 shutil.copystat(src, dst)
81 if WindowsError is not None and isinstance(why, WindowsError):
82 # Copying file access times may fail on Windows
85 errors.extend((src, dst, str(why)))
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)
94 default_tgt = join(conf.blddir, "default", subdir)
96 if not os.path.exists(default_tgt):
97 copytree(src, default_tgt, True)
100 if os.system("cd \"%s\" && %s" % (default_tgt, command)) != 0:
101 conf.fatal("Configuring %s failed." % (subdir))
103 debug_tgt = join(conf.blddir, "debug", subdir)
105 if not os.path.exists(debug_tgt):
106 copytree(default_tgt, debug_tgt, True)
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')
114 conf.env["USE_DEBUG"] = Options.options.debug
115 conf.env["USE_SYSTEM"] = Options.options.system
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")
122 if sys.platform.startswith("freebsd"):
123 conf.check(lib='kvm', uselib_store='KVM')
125 #if Options.options.debug:
126 # conf.check(lib='profiler', uselib_store='PROFILER')
128 if Options.options.efence:
129 conf.check(lib='efence', libpath=['/usr/lib', '/usr/local/lib'], uselib_store='EFENCE')
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.")
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")
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")
155 conf.check(lib='rt', uselib_store='RT')
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")
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')
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")
175 conf.define("HAVE_CONFIG_H", 1)
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')
182 threadflags='-pthread'
183 conf.env.append_value ('CCFLAGS', threadflags)
184 conf.env.append_value ('CXXFLAGS', threadflags)
185 conf.env.append_value ('LINKFLAGS', threadflags)
187 conf.env.append_value("CCFLAGS", "-DX_STACKSIZE=%d" % (1024*64))
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')
196 platform_def = '-DPLATFORM=' + sys.platform
197 conf.env.append_value('CCFLAGS', platform_def)
198 conf.env.append_value('CXXFLAGS', platform_def)
200 # Split off debug variant before adding variant specific defines
201 debug_env = conf.env.copy()
202 conf.set_env_name('debug', debug_env)
204 # Configure debug variant
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")
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")
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")
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...
228 # XXX Remove this when v8 defaults x86_64 to native builds
230 if bld.env['DEST_CPU'] == 'x86_64':
233 if variant == "default":
238 cmd_R = 'python "%s" -C "%s" -Y "%s" visibility=default mode=%s %s library=static snapshot=on'
240 cmd = cmd_R % ( scons
241 , bld.srcnode.abspath(bld.env_of_name(variant))
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"),
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)
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)
274 bld.install_files('${PREFIX}/include/node/', 'deps/v8/include/*.h')
277 print "DEST_OS: " + bld.env['DEST_OS']
278 print "DEST_CPU: " + bld.env['DEST_CPU']
280 if not bld.env["USE_SYSTEM"]:
281 bld.add_subdirs('deps/libeio deps/libev deps/c-ares')
284 bld.add_subdirs('deps/libeio')
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")
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")
308 def make_macros(loc, content):
313 macros_loc_debug = join(
314 bld.srcnode.abspath(bld.env_of_name("debug")),
318 macros_loc_default = join(
319 bld.srcnode.abspath(bld.env_of_name("default")),
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")
327 def javascript_in_c(task):
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)
334 def javascript_in_c_debug(task):
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)
341 native_cc = bld.new_task_gen(
342 source='src/node.js ' + bld.path.ant_glob('lib/*.js'),
343 target="src/node_natives.h",
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
352 if bld.env["USE_DEBUG"]:
353 native_cc_debug = native_cc.clone("debug")
354 native_cc_debug.rule = javascript_in_c_debug
356 native_cc.rule = javascript_in_c
359 node = bld.new_task_gen("cxx", "program")
365 src/node_http_parser.cc
367 src/node_io_watcher.cc
368 src/node_child_process.cc
369 src/node_constants.cc
373 src/node_signal_watcher.cc
374 src/node_stat_watcher.cc
379 if bld.env["USE_OPENSSL"]:
380 node.source += "src/node_crypto.cc"
382 if not bld.env["USE_SYSTEM"]:
393 node.includes += ' deps/c-ares/' + bld.env['DEST_OS'] + '-' + bld.env['DEST_CPU']
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'
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'
410 node.install_path = '${PREFIX}/lib'
411 node.install_path = '${PREFIX}/bin'
414 def subflags(program):
415 if os.path.exists(join(cwd, ".git")):
416 actual_version=cmd_output("git describe").strip()
418 actual_version=VERSION
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"]
428 # process file.pc.in -> file.pc
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'
436 if bld.env["USE_DEBUG"]:
437 node_g = node.clone("debug")
438 node_g.target = "node_g"
440 node_version_g = node_version.clone("debug")
441 node_version_g.dict = subflags(node_g)
442 node_version_g.install_path = None
445 bld.install_files('${PREFIX}/include/node/', """
448 src/node_object_wrap.h
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')
457 bld.install_files('${PREFIX}/bin/', 'bin/*', chmod=0755)
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')
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')
473 if os.path.exists('node'): os.unlink('node')
474 if os.path.exists('node_g'): os.unlink('node_g')