Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / tizen / SConscript
1 ##
2 # This script includes generic build options:
3 #    release/debug, target os, target arch, cross toolchain, build environment etc
4 ##
5 import os
6 import platform
7
8 # Map of host os and allowed target os (host: allowed target os)
9 host_target_map = {
10                 'linux': ['linux', 'android', 'arduino', 'yocto', 'tizen'],
11                 'windows': ['windows', 'winrt', 'android', 'arduino', 'tizen'],
12                 'darwin': ['darwin', 'ios', 'android', 'arduino'],
13                 }
14
15 # Map of os and allowed archs (os: allowed archs)
16 os_arch_map = {
17                 'linux': ['x86', 'x86_64', 'arm', 'arm64'],
18                 'android': ['x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'armeabi-v7a-hard', 'arm64-v8a'],
19                 'windows': ['x86', 'amd64', 'arm'],
20                 'winrt': ['arm'],
21                 'darwin': ['i386', 'x86_64'],
22                 'ios': ['i386', 'x86_64', 'armv7', 'armv7s', 'arm64'],
23                 'arduino': ['avr', 'arm'],
24                 'yocto': ['x86', 'x86_64'],
25                 'tizen': ['armv7'],
26                 }
27
28 host = platform.system().lower()
29
30 if not host_target_map.has_key(host):
31         print "\nError: Current system (%s) isn't supported\n" % host
32         Exit(1)
33
34 ######################################################################
35 # Get build options (the optins from command line)
36 ######################################################################
37 target_os = ARGUMENTS.get('TARGET_OS', host).lower() # target os
38
39 if target_os not in host_target_map[host]:
40         print "\nError: Unknown target os: %s (Allow values: %s)\n" % (target_os, host_target_map[host])
41         Exit(1)
42
43 default_arch = platform.machine()
44 if default_arch not in os_arch_map[target_os]:
45         default_arch = os_arch_map[target_os][0].lower()
46
47 target_arch = ARGUMENTS.get('TARGET_ARCH', default_arch) # target arch
48
49 ######################################################################
50 # Common build options (release, target os, target arch)
51 ######################################################################
52 help_vars = Variables()
53 help_vars.Add(BoolVariable('RELEASE', 'Build for release?', True)) # set to 'no', 'false' or 0 for debug
54 help_vars.Add(EnumVariable('TARGET_OS', 'Target platform', host, host_target_map[host]))
55 help_vars.Add(ListVariable('TARGET_TRANSPORT', 'Target transport', 'ALL', ['ALL', 'IP', 'BT', 'BLE']))
56 help_vars.Add(EnumVariable('TARGET_ARCH', 'Target architecture', default_arch, os_arch_map[target_os]))
57 help_vars.Add(EnumVariable('SECURED', 'Build with DTLS', '0', allowed_values=('0', '1')))
58
59
60 AddOption('--prefix',
61                   dest='prefix',
62                   type='string',
63                   nargs=1,
64                   action='store',
65                   metavar='DIR',
66                   help='installation prefix')
67
68 ######################################################################
69 # Platform(build target) specific options: SDK/NDK & toolchain
70 ######################################################################
71 targets_support_cc = ['linux', 'arduino', 'tizen']
72
73 if target_os in targets_support_cc:
74         # Set cross compile toolchain
75         help_vars.Add('TC_PREFIX', "Toolchain prefix (Generally only be required for cross-compiling)", os.environ.get('TC_PREFIX'))
76         help_vars.Add(PathVariable('TC_PATH',
77                         'Toolchain path (Generally only be required for cross-compiling)',
78                         os.environ.get('TC_PATH')))
79
80 if target_os in ['android', 'arduino']: # Android/Arduino always uses GNU compiler regardless of the host
81         env = Environment(variables = help_vars,
82                         tools = ['gnulink', 'gcc', 'g++', 'ar', 'as']
83                         )
84 else:
85         env = Environment(variables = help_vars, TARGET_ARCH = target_arch, TARGET_OS = target_os, PREFIX = GetOption('prefix'))
86
87 Help(help_vars.GenerateHelpText(env))
88
89 tc_set_msg = '''
90 ************************************ Warning **********************************
91 *   Enviornment variable TC_PREFIX/TC_PATH is set. It will change the default *
92 * toolchain, if it isn't what you expect you should unset it, otherwise it may*
93 * cause inexplicable errors.                                                  *
94 *******************************************************************************
95 '''
96
97 if target_os in targets_support_cc:
98         prefix = env.get('TC_PREFIX')
99         tc_path = env.get('TC_PATH')
100         if prefix:
101                 env.Replace(CC = prefix + 'gcc')
102                 env.Replace(CXX = prefix + 'g++')
103                 env.Replace(AR = prefix + 'ar')
104                 env.Replace(AS = prefix + 'as')
105                 env.Replace(LINK = prefix + 'ld')
106                 env.Replace(RANLIB = prefix + 'ranlib')
107
108         if tc_path:
109                 env.PrependENVPath('PATH', tc_path)
110                 sys_root = os.path.abspath(tc_path + '/../')
111                 env.AppendUnique(CCFLAGS = ['--sysroot=' + sys_root])
112                 env.AppendUnique(LINKFLAGS = ['--sysroot=' + sys_root])
113
114         if prefix or tc_path:
115                 print tc_set_msg
116
117 # Ensure scons be able to change its working directory
118 env.SConscriptChdir(1)
119
120 # Set the source directory and build directory
121 #   Source directory: 'dir'
122 #   Build directory: 'dir'/out/<target_os>/<target_arch>/<release or debug>/
123 #
124 # You can get the directory as following:
125 #   env.get('SRC_DIR')
126 #   env.get('BUILD_DIR')
127
128 def __set_dir(env, dir):
129         if not os.path.exists(dir + '/SConstruct'):
130                 print '''
131 *************************************** Error *********************************
132 * The directory(%s) seems isn't a source code directory, no SConstruct file is
133 * found. *
134 *******************************************************************************
135 ''' % dir
136                 Exit(1)
137
138         if env.get('RELEASE'):
139                 build_dir = dir + '/out/' + target_os + '/' + target_arch + '/release/'
140         else:
141                 build_dir = dir + '/out/' + target_os + '/' + target_arch + '/debug/'
142         env.VariantDir(build_dir, dir, duplicate=0)
143
144         env.Replace(BUILD_DIR = build_dir)
145         env.Replace(SRC_DIR = dir)
146
147 def __src_to_obj(env, src, home = ''):
148         obj = env.get('BUILD_DIR') + src.replace(home, '')
149         if env.get('OBJSUFFIX'):
150                 obj += env.get('OBJSUFFIX')
151         return env.Object(obj, src)
152
153 def __install(ienv, targets, name):
154         i_n = ienv.Install(env.get('BUILD_DIR'), targets)
155         Alias(name, i_n)
156         env.AppendUnique(TS = [name])
157
158 def __append_target(ienv, target):
159         env.AppendUnique(TS = [target])
160
161 def __installlib(ienv, targets, name):
162         user_prefix = env.get('PREFIX')
163         if user_prefix:
164                 i_n = ienv.Install(user_prefix + '/lib', targets)
165         else:
166                 i_n = ienv.Install(env.get('BUILD_DIR'), targets)
167         ienv.Alias("install", i_n)
168
169 def __installbin(ienv, targets, name):
170         user_prefix = env.get('PREFIX')
171         if user_prefix:
172                 i_n = ienv.Install(user_prefix + '/bin', targets)
173         else:
174                 i_n = ienv.Install(env.get('BUILD_DIR'), targets)
175         ienv.Alias("install", i_n)
176
177 def __print_targets(env):
178         Help('''
179 ===============================================================================
180 Targets:\n    ''')
181         for t in env.get('TS'):
182                 Help(t + ' ')
183         Help('''
184 \nDefault all targets will be built. You can specify the target to build:
185
186     $ scons [options] [target]
187 ===============================================================================
188 ''')
189
190 env.AddMethod(__set_dir, 'SetDir')
191 env.AddMethod(__print_targets, 'PrintTargets')
192 env.AddMethod(__src_to_obj, 'SrcToObj')
193 env.AddMethod(__append_target, 'AppendTarget')
194 env.AddMethod(__install, 'InstallTarget')
195 env.AddMethod(__installlib, 'UserInstallTargetLib')
196 env.AddMethod(__installbin, 'UserInstallTargetBin')
197 env.SetDir(env.GetLaunchDir())
198 env['ROOT_DIR']=env.GetLaunchDir()
199
200 Export('env')
201
202 ######################################################################
203 # Link scons to Yocto cross-toolchain ONLY when target_os is yocto
204 ######################################################################
205 if target_os == "yocto":
206     '''
207     This code injects Yocto cross-compilation tools+flags into scons'
208     build environment in order to invoke the relevant tools while
209     performing a build.
210     '''
211     import os.path
212     try:
213         CC = os.environ['CC']
214         target_prefix = CC.split()[0]
215         target_prefix = target_prefix[:len(target_prefix)-3]
216         tools = {"CC" : target_prefix+"gcc",
217                 "CXX" : target_prefix+"g++",
218                 "AS" : target_prefix+"as",
219                 "LD" : target_prefix+"ld",
220                 "GDB" : target_prefix+"gdb",
221                 "STRIP" : target_prefix+"strip",
222                 "RANLIB" : target_prefix+"ranlib",
223                 "OBJCOPY" : target_prefix+"objcopy",
224                 "OBJDUMP" : target_prefix+"objdump",
225                 "AR" : target_prefix+"ar",
226                 "NM" : target_prefix+"nm",
227                 "M4" : "m4",
228                 "STRINGS": target_prefix+"strings"}
229         PATH = os.environ['PATH'].split(os.pathsep)
230         for tool in tools:
231             if tool in os.environ:
232                 for path in PATH:
233                     if os.path.isfile(os.path.join(path, tools[tool])):
234                         env[tool] = os.path.join(path, os.environ[tool])
235                         break
236     except:
237         print "ERROR in Yocto cross-toolchain environment"
238         Exit(1)
239     '''
240     Now reset TARGET_OS to linux so that all linux specific build configurations
241     hereupon apply for the entirety of the build process.
242     '''
243     env['TARGET_OS'] = 'linux'
244     '''
245     We want to preserve debug symbols to allow BitBake to generate both DEBUG and
246     RELEASE packages for OIC.
247     '''
248     env['CCFLAGS'].append('-g')
249     Export('env')
250 else:
251     '''
252     If target_os is not Yocto, continue with the regular build process
253     '''
254     # Load config of target os
255     env.SConscript(target_os + '/SConscript')
256
257 # Delete the temp files of configuration
258 if env.GetOption('clean'):
259         dir = env.get('SRC_DIR')
260
261         if os.path.exists(dir + '/config.log'):
262                 Execute(Delete(dir + '/config.log'))
263                 Execute(Delete(dir + '/.sconsign.dblite'))
264                 Execute(Delete(dir + '/.sconf_temp'))
265
266 Return('env')
267