Merge "[IOT-1785] Merge branch 'iot-1785'"
[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 rd_mode = env.get('RD_MODE')
32
33 # for android, doesn't distinguish 'armeabi-v7a-hard' and 'armeabi-v7a' library
34 if target_os == 'android':
35         if target_arch == 'armeabi-v7a-hard':
36                 target_arch = 'armeabi-v7a'
37         env.AppendUnique(CCFLAGS = ['-D__JAVA__'])
38
39 if target_os == 'darwin':
40         env.AppendUnique(CPPPATH = ['/usr/local/include'])
41         env.AppendUnique(LIBPATH = ['/usr/local/lib'])
42
43 if env.get('BUILD_JAVA') == True and target_os != 'android':
44         if env.get('JAVA_HOME') != None:
45                         env.AppendUnique(CCFLAGS = ['-D__JAVA__'])
46                         env.AppendUnique(CPPPATH = [
47                         env.get('JAVA_HOME') + '/include',
48                         env.get('JAVA_HOME') + '/include/' + target_os
49                 ])
50         else:
51                 raise SCons.Errors.StopError( 'BUILD_JAVA is specified, but JAVA_HOME is not set.')
52
53
54 # External library include files are in <src_dir>/deps/<target_os>/include
55 # the library binaries are in <src_dir>/deps/<target_os>/lib/<arch>
56 if target_os not in ['windows']:
57         env.AppendUnique(CPPPATH = [os.path.join(env.get('SRC_DIR'), 'deps', target_os, 'include')])
58         env.AppendUnique(LIBPATH = [os.path.join(env.get('SRC_DIR'), 'deps', target_os, 'lib', target_arch)])
59
60 # Check whether a library exists, if not, notify user to install it or try to
61 # download the source code and build it
62 # @param libname - the name of the library try to prepare
63 # @param lib - the lib(.so, .a etc) to check (a library may include more then
64 #         one lib, e.g. boost, includes boost_thread, boost_system ...
65 # @param path - the directory of the library building script, if it's not set,
66 #                       by default, it's <src_dir>/extlibs/<libname>/
67 # @param script - the building script, by default, it's 'SConscript'
68 #
69 def __prepare_lib(ienv, libname, lib = None, path = None, script = None):
70         p_env = ienv.Clone(LIBS = [])
71         if p_env.GetOption('clean') or p_env.GetOption('help'):
72                 return
73
74         conf = Configure(p_env)
75
76         if not lib:
77                 lib = libname
78         if not conf.CheckLib(lib):
79                 if path:
80                         dir = path
81                 else:
82                         dir = os.path.join(env.get('SRC_DIR'), 'extlibs', libname)
83
84                 # Execute the script to download(if required) and build source code
85                 if script:
86                         st = os.path.join(dir, script)
87                 else:
88                         st = os.path.join(dir, 'SConscript')
89
90                 if os.path.exists(st):
91                         SConscript(st)
92                 else:
93                         if target_os in ['linux', 'darwin', 'tizen']:
94                                 print 'Don\'t find library(%s), please intall it, exit ...' % libname
95                         else:
96                                 print 'Don\'t find library(%s) and don\'t find the process script (%s), exit ...' % (libname, st)
97                         Exit(1)
98
99         conf.Finish()
100
101 # Run configure command (usually it's done before build a library)
102 def __configure(env, cwd, cmd) :
103         print "Configuring using [%s/%s] ..." % (cwd, cmd)
104         # build it now (we need the shell, because some programs need it)
105         devnull = open(os.devnull, "wb")
106         handle  = subprocess.Popen(cmd, shell=True, cwd=cwd, stdout=devnull)
107
108         if handle.wait() <> 0 :
109                 raise SCons.Errors.BuildError( "Run configuring script [%s]" % (cmd) )
110
111 # Download from URL 'url', will save as 'target'
112 def __download(ienv, target, url) :
113         if os.path.exists(target) :
114                 return target
115
116         try :
117                 print "Download %s from %s" % (target, url)
118                 print "Downloading ..."
119                 stream = urllib2.urlopen(url)
120                 file = open(target, 'wb')
121                 file.write(stream.read())
122                 file.close()
123                 print "Download %s from %s complete" % (target, url)
124                 return target
125         except Exception, e :
126                 raise SCons.Errors.StopError( '%s [%s]' % (e, url) )
127
128 # Install header file(s) to <src_dir>/deps/<target_os>/include
129 def __install_head_file(ienv, file):
130                 return ienv.Install(os.path.join(env.get('SRC_DIR'), 'dep', target_os, target_arch, 'usr', 'include'), file)
131
132 # Install library binaries to <src_dir>/deps/<target_os>/lib/<arch>
133 def __install_lib(ienv, lib):
134                 return ienv.Install(os.path.join(env.get('SRC_DIR'), 'dep', target_os, target_arch, 'usr', 'lib'), lib)
135
136 SConscript('tools/UnpackAll.py')
137
138 # tinycbor build/fetch
139 SConscript(os.path.join(env.get('SRC_DIR'), 'extlibs', 'tinycbor', 'SConscript'))
140
141 with_ra = env.get('WITH_RA')
142 if with_ra:
143         SConscript(os.path.join(env.get('SRC_DIR'), 'extlibs', 'raxmpp', 'SConscript'))
144
145 with_ra_ibb = env.get('WITH_RA_IBB')
146 if with_ra_ibb:
147         SConscript(os.path.join(env.get('SRC_DIR'), 'extlibs', 'wksxmppxep', 'SConscript'))
148
149
150 env.AddMethod(__prepare_lib, "PrepareLib")
151 env.AddMethod(__configure, "Configure")
152 env.AddMethod(__download, "Download")
153 env.AddMethod(__install_head_file, "InstallHeadFile")
154 env.AddMethod(__install_lib, "InstallLib")
155
156 if env.get('SECURED') == '1' or 'SERVER' in rd_mode:
157         if target_os not in ['linux', 'tizen']:
158                 SConscript('#extlibs/sqlite3/SConscript')