Merge remote-tracking branch 'origin/notification-service'
[platform/upstream/iotivity.git] / build_common / external_libs.scons
1 ##
2 # This script manages external libraries
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 # By default, assume the script for an exteranl library is:
16 #    <src_dir>/extlibs/<libname>/SConscript
17 #
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>
21 #
22 ##
23 import os, subprocess
24 import urllib2, urlparse
25 import SCons.Errors
26
27 Import('env')
28
29 target_os = env.get('TARGET_OS')
30 target_arch = env.get('TARGET_ARCH')
31
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'
36
37 if target_os == 'darwin':
38         env.AppendUnique(CPPPATH = ['/usr/local/include'])
39         env.AppendUnique(LIBPATH = ['/usr/local/lib'])
40
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 if target_os not in ['windows']:
44         env.AppendUnique(CPPPATH = [os.path.join(env.get('SRC_DIR'), 'deps', target_os, 'include')])
45         env.AppendUnique(LIBPATH = [os.path.join(env.get('SRC_DIR'), 'deps', target_os, 'lib', target_arch)])
46
47 # Check whether a library exists, if not, notify user to install it or try to
48 # download the source code and build it
49 # @param libname - the name of the library try to prepare
50 # @param lib - the lib(.so, .a etc) to check (a library may include more then
51 #      one lib, e.g. boost, includes boost_thread, boost_system ...
52 # @param path - the directory of the library building script, if it's not set,
53 #                       by default, it's <src_dir>/extlibs/<libname>/
54 # @param script - the building script, by default, it's 'SConscript'
55 #
56 def __prepare_lib(ienv, libname, lib = None, path = None, script = None):
57         p_env = ienv.Clone()
58         if p_env.GetOption('clean') or p_env.GetOption('help'):
59                 return
60
61         conf = Configure(p_env)
62
63         if not lib:
64                 lib = libname
65         if not conf.CheckLib(lib):
66                 if path:
67                         dir = path
68                 else:
69                         dir = os.path.join(env.get('SRC_DIR'), 'extlibs', libname)
70
71                 # Execute the script to download(if required) and build source code
72                 if script:
73                         st = os.path.join(dir, script)
74                 else:
75                         st = os.path.join(dir, 'SConscript')
76
77                 if os.path.exists(st):
78                         SConscript(st)
79                 else:
80                         if target_os in ['linux', 'darwin', 'tizen']:
81                                 print 'Don\'t find library(%s), please intall it, exit ...' % libname
82                         else:
83                                 print 'Don\'t find library(%s) and don\'t find the process script (%s), exit ...' % (libname, st)
84                         Exit(1)
85
86         conf.Finish()
87
88 # Run configure command (usually it's done before build a library)
89 def __configure(env, cwd, cmd) :
90         print "Configuring using [%s/%s] ..." % (cwd, cmd)
91         # build it now (we need the shell, because some programs need it)
92         devnull = open(os.devnull, "wb")
93         handle  = subprocess.Popen(cmd, shell=True, cwd=cwd, stdout=devnull)
94
95         if handle.wait() <> 0 :
96                 raise SCons.Errors.BuildError( "Run configuring script [%s]" % (cmd) )
97
98 # Download from URL 'url', will save as 'target'
99 def __download(ienv, target, url) :
100         if os.path.exists(target) :
101                 return target
102
103         try :
104                 print "Download %s from %s" % (target, url)
105                 print "Downloading ..."
106                 stream = urllib2.urlopen(url)
107                 file = open(target, 'wb')
108                 file.write(stream.read())
109                 file.close()
110                 print "Download %s from %s complete" % (target, url)
111                 return target
112         except Exception, e :
113                 raise SCons.Errors.StopError( '%s [%s]' % (e, url) )
114
115 # Install header file(s) to <src_dir>/deps/<target_os>/include
116 def __install_head_file(ienv, file):
117                 return ienv.Install(os.path.join(env.get('SRC_DIR'), 'dep', target_os, target_arch, 'usr', 'include'), file)
118
119 # Install library binaries to <src_dir>/deps/<target_os>/lib/<arch>
120 def __install_lib(ienv, lib):
121                 return ienv.Install(os.path.join(env.get('SRC_DIR'), 'dep', target_os, target_arch, 'usr', 'lib'), lib)
122
123 SConscript('tools/UnpackAll.py')
124
125 # tinycbor build/fetch
126 SConscript(os.path.join(env.get('SRC_DIR'), 'extlibs', 'tinycbor', 'SConscript'))
127
128 with_ra = env.get('WITH_RA')
129 if with_ra:
130         SConscript(os.path.join(env.get('SRC_DIR'), 'extlibs', 'raxmpp', 'SConscript'))
131
132 with_ra_ibb = env.get('WITH_RA_IBB')
133 if with_ra_ibb:
134         SConscript(os.path.join(env.get('SRC_DIR'), 'extlibs', 'wksxmppxep', 'SConscript'))
135
136
137 env.AddMethod(__prepare_lib, "PrepareLib")
138 env.AddMethod(__configure, "Configure")
139 env.AddMethod(__download, "Download")
140 env.AddMethod(__install_head_file, "InstallHeadFile")
141 env.AddMethod(__install_lib, "InstallLib")
142
143 if env.get('SECURED') == '1' and target_os != 'tizen':
144         SConscript(os.path.join(env.get('SRC_DIR'), 'extlibs', 'sqlite3', 'SConscript'))