blob: 065999041a76eb81c6db3ee0545cd348b5d0ec8c [file] [log] [blame]
Ryana3627c02009-05-27 14:29:551# /usr/bin/env python
Ryan8ddf9302009-09-02 18:19:522import platform
3import re
Ryan1a126ed2009-04-04 12:50:154import Options
Ryan41d89f62009-07-28 10:29:185import sys, os, shutil
Ryan Dahld979ac92009-10-09 13:00:126from Utils import cmd_output
Ryan1a126ed2009-04-04 12:50:157from os.path import join, dirname, abspath
Ryana4593e32009-04-23 11:18:388from logging import fatal
9
Ryan Dahld979ac92009-10-09 13:00:1210cwd = os.getcwd()
Ryan Dahld79b6e92009-10-09 16:10:5911VERSION="0.1.14"
Ryan4d921992009-08-26 23:11:1612APPNAME="node.js"
13
Ryan63a9cd32009-04-15 08:08:2814import js2c
15
Ryan1a126ed2009-04-04 12:50:1516srcdir = '.'
17blddir = 'build'
18
19def set_options(opt):
20 # the gcc module provides a --debug-level option
21 opt.tool_options('compiler_cxx')
22 opt.tool_options('compiler_cc')
Ryan4d921992009-08-26 23:11:1623 opt.tool_options('misc')
Ryan29b528c2009-04-23 15:29:3124 opt.add_option( '--debug'
25 , action='store_true'
26 , default=False
27 , help='Build debug variant [Default: False]'
28 , dest='debug'
29 )
Ryan7bad9de2009-06-16 13:47:5730 opt.add_option( '--efence'
31 , action='store_true'
32 , default=False
33 , help='Build with -lefence for debugging [Default: False]'
34 , dest='efence'
35 )
Ryan1a126ed2009-04-04 12:50:1536
Ryan41d89f62009-07-28 10:29:1837def mkdir_p(dir):
38 if not os.path.exists (dir):
39 os.makedirs (dir)
40
Ryan Dahl18da8ff2009-09-28 20:39:0041# Copied from Python 2.6 because 2.4.4 at least is broken by not using
42# mkdirs
43# http://mail.python.org/pipermail/python-bugs-list/2005-January/027118.html
44def copytree(src, dst, symlinks=False, ignore=None):
45 names = os.listdir(src)
46 if ignore is not None:
47 ignored_names = ignore(src, names)
48 else:
49 ignored_names = set()
50
51 os.makedirs(dst)
52 errors = []
53 for name in names:
54 if name in ignored_names:
55 continue
Ryan Dahl53ebe752009-10-08 21:20:1456 srcname = join(src, name)
57 dstname = join(dst, name)
Ryan Dahl18da8ff2009-09-28 20:39:0058 try:
59 if symlinks and os.path.islink(srcname):
60 linkto = os.readlink(srcname)
61 os.symlink(linkto, dstname)
62 elif os.path.isdir(srcname):
63 copytree(srcname, dstname, symlinks, ignore)
64 else:
65 shutil.copy2(srcname, dstname)
66 # XXX What about devices, sockets etc.?
67 except (IOError, os.error), why:
68 errors.append((srcname, dstname, str(why)))
69 # catch the Error from the recursive copytree so that we can
70 # continue with other files
71 except Error, err:
72 errors.extend(err.args[0])
73 try:
74 shutil.copystat(src, dst)
75 except OSError, why:
76 if WindowsError is not None and isinstance(why, WindowsError):
77 # Copying file access times may fail on Windows
78 pass
79 else:
80 errors.extend((src, dst, str(why)))
81 if errors:
82 raise Error, errors
83
Ryan41d89f62009-07-28 10:29:1884def conf_subproject (conf, subdir, command=None):
85 print("---- %s ----" % subdir)
86 src = join(conf.srcdir, subdir)
87 if not os.path.exists (src): fatal("no such subproject " + subdir)
88
89 default_tgt = join(conf.blddir, "default", subdir)
90
91 if not os.path.exists(default_tgt):
Ryan Dahl18da8ff2009-09-28 20:39:0092 copytree(src, default_tgt, True)
Ryan41d89f62009-07-28 10:29:1893
94 if command:
95 if os.system("cd %s && %s" % (default_tgt, command)) != 0:
96 fatal("Configuring %s failed." % (subdir))
97
98 debug_tgt = join(conf.blddir, "debug", subdir)
99
100 if not os.path.exists(debug_tgt):
Ryan Dahl18da8ff2009-09-28 20:39:00101 copytree(default_tgt, debug_tgt, True)
Ryan41d89f62009-07-28 10:29:18102
Ryan1a126ed2009-04-04 12:50:15103def configure(conf):
104 conf.check_tool('compiler_cxx')
105 conf.check_tool('compiler_cc')
Ryana4593e32009-04-23 11:18:38106
Ryan8e7bbf22009-04-23 17:26:56107 conf.env["USE_DEBUG"] = Options.options.debug
Ryan1a126ed2009-04-04 12:50:15108
Ryan2b6d7242009-06-20 13:07:10109 conf.check(lib='dl', uselib_store='DL')
Ryan8152f9c2009-09-01 12:15:29110 conf.env.append_value("CCFLAGS", "-rdynamic")
Ryana97dce72009-08-31 09:14:34111 conf.env.append_value("LINKFLAGS_DL", "-rdynamic")
112
Ryan8152f9c2009-09-01 12:15:29113 #if Options.options.debug:
114 # conf.check(lib='profiler', uselib_store='PROFILER')
Ryan7bad9de2009-06-16 13:47:57115
Ryan8152f9c2009-09-01 12:15:29116 #if Options.options.efence:
117 # conf.check(lib='efence', libpath=['/usr/lib', '/usr/local/lib'], uselib_store='EFENCE')
Ryana3627c02009-05-27 14:29:55118
Ryan Dahl8b62e862009-10-10 09:58:36119 if not conf.check(lib="execinfo", libpath=['/usr/lib', '/usr/local/lib'], uselib_store="EXECINFO"):
120 if sys.platform.startswith("freebsd"):
Ryan7bad9de2009-06-16 13:47:57121 fatal("Install the libexecinfo port from /usr/ports/devel/libexecinfo.")
Ryana3627c02009-05-27 14:29:55122
Ryan1a126ed2009-04-04 12:50:15123 conf.sub_config('deps/libeio')
124 conf.sub_config('deps/libev')
125
Ryan41d89f62009-07-28 10:29:18126 conf_subproject(conf, 'deps/udns', './configure')
Ryan41d89f62009-07-28 10:29:18127
Ryan452d3f12009-06-11 11:40:14128 # Not using TLS yet
129 # if conf.check_cfg(package='gnutls', args='--cflags --libs', uselib_store="GNUTLS"):
130 # conf.define("HAVE_GNUTLS", 1)
Ryan1a126ed2009-04-04 12:50:15131
132 conf.define("HAVE_CONFIG_H", 1)
Ryanc62b1242009-04-22 17:55:08133
Ryan1df6d612009-09-03 13:59:48134 conf.env.append_value("CCFLAGS", "-DX_STACKSIZE=%d" % (1024*64))
Ryan427e3f52009-05-14 11:16:45135
Ryan67af9582009-04-18 13:35:42136 # Split off debug variant before adding variant specific defines
Ryan7e1350f2009-04-16 09:37:44137 debug_env = conf.env.copy()
138 conf.set_env_name('debug', debug_env)
Ryan7e1350f2009-04-16 09:37:44139
Ryan67af9582009-04-18 13:35:42140 # Configure debug variant
141 conf.setenv('debug')
142 debug_env.set_variant('debug')
Ryan8ddf9302009-09-02 18:19:52143 debug_env.append_value('CCFLAGS', ['-DDEBUG', '-g', '-O0', '-Wall', '-Wextra'])
144 debug_env.append_value('CXXFLAGS', ['-DDEBUG', '-g', '-O0', '-Wall', '-Wextra'])
Ryan67af9582009-04-18 13:35:42145 conf.write_config_header("config.h")
146
147 # Configure default variant
148 conf.setenv('default')
Ryan8ddf9302009-09-02 18:19:52149 conf.env.append_value('CCFLAGS', ['-DNDEBUG', '-O3'])
150 conf.env.append_value('CXXFLAGS', ['-DNDEBUG', '-O3'])
Ryan67af9582009-04-18 13:35:42151 conf.write_config_header("config.h")
Ryan63a9cd32009-04-15 08:08:28152
Ryan41d89f62009-07-28 10:29:18153def build_udns(bld):
154 default_build_dir = bld.srcnode.abspath(bld.env_of_name("default"))
Ryan1a126ed2009-04-04 12:50:15155
Ryan41d89f62009-07-28 10:29:18156 default_dir = join(default_build_dir, "deps/udns")
157
158 static_lib = bld.env["staticlib_PATTERN"] % "udns"
159
160 rule = 'cd %s && make'
161
162 default = bld.new_task_gen(
163 target= join("deps/udns", static_lib),
164 rule= rule % default_dir,
165 before= "cxx",
166 install_path= None
167 )
168
169 bld.env["CPPPATH_UDNS"] = "deps/udns"
170 bld.env["STATICLIB_UDNS"] = "udns"
171
172 bld.env_of_name('default')["STATICLIB_UDNS"] = "udns"
173 bld.env_of_name('default')["LIBPATH_UDNS"] = default_dir
174
175 if bld.env["USE_DEBUG"]:
176 debug_build_dir = bld.srcnode.abspath(bld.env_of_name("debug"))
177 debug_dir = join(debug_build_dir, "deps/udns")
178 debug = default.clone("debug")
179 debug.rule = rule % debug_dir
180 #debug.target = join(debug_dir, static_lib)
181 bld.env_of_name('debug')["STATICLIB_UDNS"] = "udns"
182 bld.env_of_name('debug')["LIBPATH_UDNS"] = debug_dir
Ryan Dahlbf0d2782009-10-03 20:42:03183 bld.install_files('${PREFIX}/include/node/', 'deps/udns/udns.h')
Ryan41d89f62009-07-28 10:29:18184
Ryan8ddf9302009-09-02 18:19:52185# XXX Remove this when v8 defaults x86_64 to native builds
186def GuessArchitecture():
187 id = platform.machine()
Jeff Smickbc6f3812009-09-12 10:40:27188 arch = platform.architecture()[0]
Ryan8ddf9302009-09-02 18:19:52189 if id.startswith('arm'):
190 return 'arm'
Jeff Smickbc6f3812009-09-12 10:40:27191 elif ('64' in id) or ('64' in arch):
Ryan8ddf9302009-09-02 18:19:52192 return 'x64'
193 elif (not id) or (not re.match('(x|i[3-6])86', id) is None):
194 return 'ia32'
195 else:
196 return None
197
Ryan Dahl53ebe752009-10-08 21:20:14198def v8_cmd(bld, variant):
199 scons = join(cwd, 'tools/scons/scons.py')
Ryan1a126ed2009-04-04 12:50:15200 deps_src = join(bld.path.abspath(),"deps")
Ryan1a126ed2009-04-04 12:50:15201 v8dir_src = join(deps_src,"v8")
Ryan Dahl53ebe752009-10-08 21:20:14202
Ryan Dahlbc9b3432009-10-02 12:10:40203 # NOTE: We want to compile V8 to export its symbols. I.E. Do not want
204 # -fvisibility=hidden. When using dlopen() it seems that the loaded DSO
205 # cannot see symbols in the executable which are hidden, even if the
206 # executable is statically linked together...
Ryan8ddf9302009-09-02 18:19:52207
Ryan Dahld85724d2009-10-08 22:34:39208 arch = ""
209 if GuessArchitecture() == "x64":
210 arch = "arch=x64"
211
212 if variant == "default":
213 mode = "release"
214 else:
215 mode = "debug"
Ryana4593e32009-04-23 11:18:38216
Ryan Dahl53ebe752009-10-08 21:20:14217 cmd_R = 'python %s -C %s -Y %s visibility=default mode=%s %s library=static snapshot=on'
218
219 cmd = cmd_R % ( scons
220 , bld.srcnode.abspath(bld.env_of_name(variant))
221 , v8dir_src
222 , mode
223 , arch
224 )
225 return cmd
226
227
228def build_v8(bld):
Ryan1a126ed2009-04-04 12:50:15229 v8 = bld.new_task_gen(
Ryan Dahl59b7a1b2009-10-09 10:49:48230 source = 'deps/v8/SConstruct '
231 + bld.path.ant_glob('v8/include/*')
232 + bld.path.ant_glob('v8/src/*'),
Ryan Dahl53ebe752009-10-08 21:20:14233 target = bld.env["staticlib_PATTERN"] % "v8",
234 rule = v8_cmd(bld, "default"),
235 before = "cxx",
236 install_path = None
Ryan1a126ed2009-04-04 12:50:15237 )
Ryan Dahl8b62e862009-10-10 09:58:36238 v8.uselib = "EXECINFO"
Ryan1a126ed2009-04-04 12:50:15239 bld.env["CPPPATH_V8"] = "deps/v8/include"
Ryana4593e32009-04-23 11:18:38240 bld.env_of_name('default')["STATICLIB_V8"] = "v8"
Ryan8ddf9302009-09-02 18:19:52241 bld.env_of_name('default')["LINKFLAGS_V8"] = ["-pthread"]
Ryana4593e32009-04-23 11:18:38242
243 ### v8 debug
Ryan29b528c2009-04-23 15:29:31244 if bld.env["USE_DEBUG"]:
Ryan29b528c2009-04-23 15:29:31245 v8_debug = v8.clone("debug")
Ryan Dahl53ebe752009-10-08 21:20:14246 v8_debug.rule = v8_cmd(bld, "debug")
247 v8_debug.target = bld.env["staticlib_PATTERN"] % "v8_g"
Ryan Dahl8b62e862009-10-10 09:58:36248 v8_debug.uselib = "EXECINFO"
Ryan29b528c2009-04-23 15:29:31249 bld.env_of_name('debug')["STATICLIB_V8"] = "v8_g"
Ryan8ddf9302009-09-02 18:19:52250 bld.env_of_name('debug')["LINKFLAGS_V8"] = ["-pthread"]
Ryan1a126ed2009-04-04 12:50:15251
Ryan Dahl53ebe752009-10-08 21:20:14252 bld.install_files('${PREFIX}/include/node/', 'deps/v8/include/*.h')
Ryan2b6d7242009-06-20 13:07:10253
Ryan41d89f62009-07-28 10:29:18254def build(bld):
255 bld.add_subdirs('deps/libeio deps/libev')
256
257 build_udns(bld)
258 build_v8(bld)
259
Ryan0fb0af32009-07-25 15:52:21260 ### evcom
261 evcom = bld.new_task_gen("cc", "staticlib")
262 evcom.source = "deps/evcom/evcom.c"
263 evcom.includes = "deps/evcom/ deps/libev/"
264 evcom.name = "evcom"
265 evcom.target = "evcom"
266 # evcom.uselib = "GNUTLS"
267 evcom.install_path = None
Ryan29b528c2009-04-23 15:29:31268 if bld.env["USE_DEBUG"]:
Ryan0fb0af32009-07-25 15:52:21269 evcom.clone("debug")
Ryan Dahlbf0d2782009-10-03 20:42:03270 bld.install_files('${PREFIX}/include/node/', 'deps/evcom/evcom.h')
Ryan1a126ed2009-04-04 12:50:15271
Ryan5a071ad2009-05-03 12:09:16272 ### http_parser
273 http_parser = bld.new_task_gen("cc", "staticlib")
274 http_parser.source = "deps/http_parser/http_parser.c"
275 http_parser.includes = "deps/http_parser/"
276 http_parser.name = "http_parser"
277 http_parser.target = "http_parser"
278 http_parser.install_path = None
Ryan29b528c2009-04-23 15:29:31279 if bld.env["USE_DEBUG"]:
Ryan5a071ad2009-05-03 12:09:16280 http_parser.clone("debug")
Ryan1a126ed2009-04-04 12:50:15281
Ryan17c6a672009-08-24 18:25:24282 ### coupling
283 coupling = bld.new_task_gen("cc", "staticlib")
284 coupling.source = "deps/coupling/coupling.c"
285 coupling.includes = "deps/coupling/"
286 coupling.name = "coupling"
287 coupling.target = "coupling"
288 coupling.install_path = None
289 if bld.env["USE_DEBUG"]:
290 coupling.clone("debug")
291
Ryan63a9cd32009-04-15 08:08:28292 ### src/native.cc
293 def javascript_in_c(task):
294 env = task.env
295 source = map(lambda x: x.srcpath(env), task.inputs)
296 targets = map(lambda x: x.srcpath(env), task.outputs)
297 js2c.JS2C(source, targets)
298
299 native_cc = bld.new_task_gen(
Ryan2ecd7ff2009-06-25 17:13:20300 source = """
Ryaneb105532009-07-16 15:19:02301 src/util.js
Ryan2ecd7ff2009-06-25 17:13:20302 src/events.js
Ryan2ecd7ff2009-06-25 17:13:20303 src/file.js
304 src/node.js
305 """,
Ryan Dahl53ebe752009-10-08 21:20:14306 target="src/node_natives.h",
Ryan63a9cd32009-04-15 08:08:28307 before="cxx"
308 )
Ryan8e7bbf22009-04-23 17:26:56309 native_cc.install_path = None
Ryan Dahl4bcb01c2009-10-16 20:53:44310
311 # Add the rule /after/ cloning the debug
312 # This is a work around for an error had in python 2.4.3 (I'll paste the
313 # error that was had into the git commit meessage. git-blame to find out
314 # where.)
Ryan29b528c2009-04-23 15:29:31315 if bld.env["USE_DEBUG"]:
Ryan Dahl4bcb01c2009-10-16 20:53:44316 native_cc_debug = native_cc.clone("debug")
317 native_cc_debug.rule = javascript_in_c
318 native_cc.rule = javascript_in_c
Ryan63a9cd32009-04-15 08:08:28319
Ryan2b6d7242009-06-20 13:07:10320 ### node lib
Ryan8152f9c2009-09-01 12:15:29321 node = bld.new_task_gen("cxx", "program")
322 node.name = "node"
323 node.target = "node"
324 node.source = """
Ryan1a126ed2009-04-04 12:50:15325 src/node.cc
Ryan2ecd7ff2009-06-25 17:13:20326 src/events.cc
Ryan67af9582009-04-18 13:35:42327 src/http.cc
Ryan707f2442009-04-21 17:56:30328 src/net.cc
Ryan17c6a672009-08-24 18:25:24329 src/node_stdio.cc
Ryan41d89f62009-07-28 10:29:18330 src/dns.cc
Ryan63a9cd32009-04-15 08:08:28331 src/file.cc
Brandon Beacherf0682512009-10-06 00:56:33332 src/signal_handler.cc
Ryanf213a272009-04-29 09:00:46333 src/timer.cc
Ryanad9d6832009-08-26 20:11:51334 src/child_process.cc
Ryanb260a912009-05-26 17:48:49335 src/constants.cc
Ryan1a126ed2009-04-04 12:50:15336 """
Ryan8152f9c2009-09-01 12:15:29337 node.includes = """
Ryan1a126ed2009-04-04 12:50:15338 src/
339 deps/v8/include
340 deps/libev
Ryan41d89f62009-07-28 10:29:18341 deps/udns
Ryan1a126ed2009-04-04 12:50:15342 deps/libeio
Ryan0fb0af32009-07-25 15:52:21343 deps/evcom
Ryan5a071ad2009-05-03 12:09:16344 deps/http_parser
Ryan17c6a672009-08-24 18:25:24345 deps/coupling
Ryan1a126ed2009-04-04 12:50:15346 """
Ryan8152f9c2009-09-01 12:15:29347 node.uselib_local = "evcom ev eio http_parser coupling"
348 node.uselib = "UDNS V8 EXECINFO DL"
349 node.install_path = '${PREFIX}/lib'
Ryan8e7bbf22009-04-23 17:26:56350 node.install_path = '${PREFIX}/bin'
351 node.chmod = 0755
352
Ryan4d921992009-08-26 23:11:16353 def subflags(program):
Ryan Dahld979ac92009-10-09 13:00:12354 if os.path.exists(join(cwd, ".git")):
Ryan Dahl962e9292009-10-09 14:16:27355 actual_version=cmd_output("git describe").strip()
Ryan Dahld979ac92009-10-09 13:00:12356 else:
357 actual_version=VERSION
358
Ryanb73264d2009-08-27 00:15:11359 x = { 'CCFLAGS' : " ".join(program.env["CCFLAGS"])
360 , 'CPPFLAGS' : " ".join(program.env["CPPFLAGS"])
361 , 'LIBFLAGS' : " ".join(program.env["LIBFLAGS"])
Ryan Dahld979ac92009-10-09 13:00:12362 , 'VERSION' : actual_version
Ryanb73264d2009-08-27 00:15:11363 , 'PREFIX' : program.env["PREFIX"]
Ryan4d921992009-08-26 23:11:16364 }
Ryan Dahlbf0d2782009-10-03 20:42:03365 return x
Ryan4d921992009-08-26 23:11:16366
Ryan4d921992009-08-26 23:11:16367 # process file.pc.in -> file.pc
Ryan Dahld979ac92009-10-09 13:00:12368
Ryanb73264d2009-08-27 00:15:11369 node_version = bld.new_task_gen('subst', before="cxx")
370 node_version.source = 'src/node_version.h.in'
371 node_version.target = 'src/node_version.h'
372 node_version.dict = subflags(node)
Ryana97dce72009-08-31 09:14:34373 node_version.install_path = '${PREFIX}/include/node'
Ryan4d921992009-08-26 23:11:16374
Ryan29b528c2009-04-23 15:29:31375 if bld.env["USE_DEBUG"]:
Ryan2b6d7242009-06-20 13:07:10376 node_g = node.clone("debug")
377 node_g.target = "node_g"
Ryan4d921992009-08-26 23:11:16378
Ryanb73264d2009-08-27 00:15:11379 node_version_g = node_version.clone("debug")
380 node_version_g.dict = subflags(node_g)
Ryana97dce72009-08-31 09:14:34381 node_version_g.install_path = None
Ryan4d921992009-08-26 23:11:16382
Ryana97dce72009-08-31 09:14:34383
384 bld.install_files('${PREFIX}/include/node/', """
385 config.h
386 src/node.h
387 src/object_wrap.h
388 src/events.h
389 src/net.h
Ryan Dahlbf0d2782009-10-03 20:42:03390 """)
391
392 # Only install the man page if it exists.
393 # Do 'make doc install' to build and install it.
394 if os.path.exists('doc/node.1'):
395 bld.install_files('${PREFIX}/share/man/man1/', 'doc/node.1')
396
397 bld.install_files('${PREFIX}/bin/', 'bin/*', chmod=0755)
Ryan Dahl6f17ca52009-10-03 17:08:05398
399 # Why am I using two lines? Because WAF SUCKS.
Ryan Dahlbf0d2782009-10-03 20:42:03400 bld.install_files('${PREFIX}/lib/node/wafadmin', 'tools/wafadmin/*.py')
401 bld.install_files('${PREFIX}/lib/node/wafadmin/Tools', 'tools/wafadmin/Tools/*.py')
Ryan Dahl6f17ca52009-10-03 17:08:05402
Ryan Dahlbf0d2782009-10-03 20:42:03403 bld.install_files('${PREFIX}/lib/node/libraries/', 'lib/*.js')