Revert "Development of CoAP-HTTP Proxy"
[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', '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                 '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 help_vars.Add(EnumVariable('ROUTING', 'Enable routing', 'EP', allowed_values=('GW', 'EP')))
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 env.AppendUnique(CPPDEFINES = ['TB_LOG'])
175 if env.get('ROUTING') == 'GW':
176         env.AppendUnique(CPPDEFINES = ['ROUTING_GATEWAY'])
177 elif env.get('ROUTING') == 'EP':
178         env.AppendUnique(CPPDEFINES = ['ROUTING_EP'])
179 env.AppendUnique(CPPDEFINES = ['__TIZEN__'])
180
181 Export('env')
182
183 ######################################################################
184 # Link scons to Yocto cross-toolchain ONLY when target_os is yocto
185 ######################################################################
186 if target_os == "yocto":
187     '''
188     This code injects Yocto cross-compilation tools+flags into scons'
189     build environment in order to invoke the relevant tools while
190     performing a build.
191     '''
192     import os.path
193     try:
194         CC = os.environ['CC']
195         target_prefix = CC.split()[0]
196         target_prefix = target_prefix[:len(target_prefix)-3]
197         tools = {"CC" : target_prefix+"gcc",
198                 "CXX" : target_prefix+"g++",
199                 "AS" : target_prefix+"as",
200                 "LD" : target_prefix+"ld",
201                 "GDB" : target_prefix+"gdb",
202                 "STRIP" : target_prefix+"strip",
203                 "RANLIB" : target_prefix+"ranlib",
204                 "OBJCOPY" : target_prefix+"objcopy",
205                 "OBJDUMP" : target_prefix+"objdump",
206                 "AR" : target_prefix+"ar",
207                 "NM" : target_prefix+"nm",
208                 "M4" : "m4",
209                 "STRINGS": target_prefix+"strings"}
210         PATH = os.environ['PATH'].split(os.pathsep)
211         for tool in tools:
212             if tool in os.environ:
213                 for path in PATH:
214                     if os.path.isfile(os.path.join(path, tools[tool])):
215                         env[tool] = os.path.join(path, os.environ[tool])
216                         break
217     except:
218         print "ERROR in Yocto cross-toolchain environment"
219         Exit(1)
220     '''
221     Now reset TARGET_OS to linux so that all linux specific build configurations
222     hereupon apply for the entirety of the build process.
223     '''
224     env['TARGET_OS'] = 'linux'
225     '''
226     We want to preserve debug symbols to allow BitBake to generate both DEBUG and
227     RELEASE packages for OIC.
228     '''
229     env['CCFLAGS'].append('-g')
230     Export('env')
231 else:
232     '''
233     If target_os is not Yocto, continue with the regular build process
234     '''
235     # Load config of target os
236     env.SConscript(target_os + '/SConscript')
237
238 # Delete the temp files of configuration
239 if env.GetOption('clean'):
240         dir = env.get('SRC_DIR')
241
242         if os.path.exists(dir + '/config.log'):
243                 Execute(Delete(dir + '/config.log'))
244                 Execute(Delete(dir + '/.sconsign.dblite'))
245                 Execute(Delete(dir + '/.sconf_temp'))
246
247 Return('env')
248