2 # This script manages external libraries
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 # By default, assume the script for an exteranl library is:
16 # <src_dir>/extlibs/<libname>/SConscript
18 # Note: After the external library is built:
19 # Head files should be installed to <src_dir>/deps/<target_os>/include
20 # lib(e.g .so, .a) should be installed to <src_dir>/deps/<target_os>/lib/<arch>
24 import urllib2, urlparse
29 target_os = env.get('TARGET_OS')
30 target_arch = env.get('TARGET_ARCH')
32 # for android, doesn't distinguish 'armeabi-v7a-hard' and 'armeabi-v7a' library
33 if target_os == 'android':
34 if target_arch == 'armeabi-v7a-hard':
35 target_arch = 'armeabi-v7a'
37 if target_os == 'darwin':
38 env.AppendUnique(CPPPATH = ['/usr/local/include'])
39 env.AppendUnique(LIBPATH = ['/usr/local/lib'])
41 # External library include files are in <src_dir>/deps/<target_os>/include
42 # the library binaries are in <src_dir>/deps/<target_os>/lib/<arch>
43 env.AppendUnique(CPPPATH = [env.get('SRC_DIR') + '/deps/' + target_os + '/include'])
44 env.AppendUnique(LIBPATH = [env.get('SRC_DIR') + '/deps/' + target_os + '/lib/' + target_arch])
46 # Check whether a library exists, if not, notify user to install it or try to
47 # download the source code and build it
48 # @param libname - the name of the library try to prepare
49 # @param lib - the lib(.so, .a etc) to check (a library may include more then
50 # one lib, e.g. boost, includes boost_thread, boost_system ...
51 # @param path - the directory of the library building script, if it's not set,
52 # by default, it's <src_dir>/extlibs/<libname>/
53 # @param script - the building script, by default, it's 'SConscript'
55 def __prepare_lib(ienv, libname, lib = None, path = None, script = None):
57 if p_env.GetOption('clean') or p_env.GetOption('help'):
60 conf = Configure(p_env)
64 if not conf.CheckLib(lib):
68 dir = env.get('SRC_DIR') + '/extlibs/' + libname
70 # Execute the script to download(if required) and build source code
72 st = dir + '/' + script
74 st = dir + '/SConscript'
76 if os.path.exists(st):
79 if target_os in ['linux', 'darwin', 'tizen']:
80 print 'Don\'t find library(%s), please intall it, exit ...' % libname
82 print 'Don\'t find library(%s) and don\'t find the process script (%s), exit ...' % (libname, st)
87 # Run configure command (usually it's done before build a library)
88 def __configure(env, cwd, cmd) :
89 print "Configuring using [%s/%s] ..." % (cwd, cmd)
90 # build it now (we need the shell, because some programs need it)
91 devnull = open(os.devnull, "wb")
92 handle = subprocess.Popen(cmd, shell=True, cwd=cwd, stdout=devnull)
94 if handle.wait() <> 0 :
95 raise SCons.Errors.BuildError( "Run configuring script [%s]" % (cmd) )
97 # Download from URL 'url', will save as 'target'
98 def __download(ienv, target, url) :
99 if os.path.exists(target) :
103 print "Download %s from %s" % (target, url)
104 print "Downloading ..."
105 stream = urllib2.urlopen(url)
106 file = open(target, 'wb')
107 file.write(stream.read())
109 print "Download %s from %s complete" % (target, url)
111 except Exception, e :
112 raise SCons.Errors.StopError( '%s [%s]' % (e, url) )
114 # Install header file(s) to <src_dir>/deps/<target_os>/include
115 def __install_head_file(ienv, file):
116 return ienv.Install(env.get('SRC_DIR') + '/deps/' + target_os + '/include', file)
118 # Install library binaries to <src_dir>/deps/<target_os>/lib/<arch>
119 def __install_lib(ienv, lib):
120 return ienv.Install(env.get('SRC_DIR') + '/deps/' + target_os + '/lib/' + target_arch, lib)
122 SConscript('tools/UnpackAll.py')
124 env.AddMethod(__prepare_lib, "PrepareLib")
125 env.AddMethod(__configure, "Configure")
126 env.AddMethod(__download, "Download")
127 env.AddMethod(__install_head_file, "InstallHeadFile")
128 env.AddMethod(__install_lib, "InstallLib")