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