[IOT-2324]warning fix 1
[platform/upstream/iotivity.git] / build_common / external_builders.scons
1 ##
2 # Add external Pseudo-Builders
3 #
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
8 # install the library.
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)
14 #
15 # for more on Pseudo-Builders see scons offical documentation
16 # 'Pseudo-Builders: the AddMethod function'
17 ##
18 import os, subprocess
19 import urllib2, urlparse
20 import SCons.Errors
21
22 Import('env')
23
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'
32 #
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'):
36         return
37
38     conf = Configure(p_env)
39
40     if not lib:
41         lib = libname
42     if not conf.CheckLib(lib):
43         if path:
44             dir = path
45         else:
46             dir = os.path.join(env.get('SRC_DIR'), 'extlibs', libname)
47
48         # Execute the script to download(if required) and build source code
49         if script:
50             st = os.path.join(dir, script)
51         else:
52             st = os.path.join(dir, 'SConscript')
53
54         if os.path.exists(st):
55             SConscript(st)
56         else:
57             if target_os in ['linux', 'darwin', 'tizen']:
58                 print 'Don\'t find library(%s), please intall it, exit ...' % libname
59             else:
60                 print 'Don\'t find library(%s) and don\'t find the process script (%s), exit ...' % (libname, st)
61             Exit(1)
62
63     conf.Finish()
64
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)
71
72     if handle.wait() <> 0 :
73         raise SCons.Errors.BuildError( "Run configuring script [%s]" % (cmd) )
74
75 # Download from URL 'url', will save as 'target'
76 def __download(ienv, target, url) :
77     if os.path.exists(target) :
78         return target
79
80     try :
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())
86         file.close()
87         print "Download %s from %s complete" % (target, url)
88         return target
89     except Exception, e :
90         raise SCons.Errors.StopError( '%s [%s]' % (e, url) )
91
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)
95
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)
99
100 SConscript('tools/UnpackAll.py')
101
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")