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