2 # Add external Pseudo-Builders
4 # Some methods are added to manage external packages:
5 # 'PrepareLib': Checks the existence of an external library, if it
6 # doesn't exist, calls the script user provided to download(if required)
7 # and build the source code of the external library or notify user to
9 # 'Download': Download package from specify URL
10 # 'UnpackAll': Unpack the package
11 # 'Configure': Execute specify script(configure, bootstrap etc)
12 # 'InstallHeadFile': Install head files
13 # 'InstallLib': Install library binaries(.so, .a etc)
15 # for more on Pseudo-Builders see scons offical documentation
16 # 'Pseudo-Builders: the AddMethod function'
19 import urllib2, urlparse
24 # Check whether a library exists, if not, notify user to install it or try to
25 # download the source code and build it
26 # @param libname - the name of the library try to prepare
27 # @param lib - the lib(.so, .a etc) to check (a library may include more then
28 # one lib, e.g. boost, includes boost_thread, boost_system ...
29 # @param path - the directory of the library building script, if it's not set,
30 # by default, it's <src_dir>/extlibs/<libname>/
31 # @param script - the building script, by default, it's 'SConscript'
33 def __prepare_lib(ienv, libname, lib = None, path = None, script = None):
34 p_env = ienv.Clone(LIBS = [])
35 if p_env.GetOption('clean') or p_env.GetOption('help'):
38 conf = Configure(p_env)
42 if not conf.CheckLib(lib):
46 dir = os.path.join(env.get('SRC_DIR'), 'extlibs', libname)
48 # Execute the script to download(if required) and build source code
50 st = os.path.join(dir, script)
52 st = os.path.join(dir, 'SConscript')
54 if os.path.exists(st):
57 if target_os in ['linux', 'darwin', 'tizen']:
58 print 'Don\'t find library(%s), please intall it, exit ...' % libname
60 print 'Don\'t find library(%s) and don\'t find the process script (%s), exit ...' % (libname, st)
65 # Run configure command (usually it's done before build a library)
66 def __configure(env, cwd, cmd) :
67 print "Configuring using [%s/%s] ..." % (cwd, cmd)
68 # build it now (we need the shell, because some programs need it)
69 devnull = open(os.devnull, "wb")
70 handle = subprocess.Popen(cmd, shell=True, cwd=cwd, stdout=devnull)
72 if handle.wait() <> 0 :
73 raise SCons.Errors.BuildError( "Run configuring script [%s]" % (cmd) )
75 # Download from URL 'url', will save as 'target'
76 def __download(ienv, target, url) :
77 if os.path.exists(target) :
81 print "Download %s from %s" % (target, url)
82 print "Downloading ..."
83 stream = urllib2.urlopen(url)
84 file = open(target, 'wb')
85 file.write(stream.read())
87 print "Download %s from %s complete" % (target, url)
90 raise SCons.Errors.StopError( '%s [%s]' % (e, url) )
92 # Install header file(s) to <src_dir>/deps/<target_os>/include
93 def __install_head_file(ienv, file):
94 return ienv.Install(os.path.join(env.get('SRC_DIR'), 'dep', target_os, target_arch, 'usr', 'include'), file)
96 # Install library binaries to <src_dir>/deps/<target_os>/lib/<arch>
97 def __install_lib(ienv, lib):
98 return ienv.Install(os.path.join(env.get('SRC_DIR'), 'dep', target_os, target_arch, 'usr', 'lib'), lib)
100 SConscript('tools/UnpackAll.py')
102 env.AddMethod(__prepare_lib, "PrepareLib")
103 env.AddMethod(__configure, "Configure")
104 env.AddMethod(__download, "Download")
105 env.AddMethod(__install_head_file, "InstallHeadFile")
106 env.AddMethod(__install_lib, "InstallLib")