IOT-1583: Disabling rebuild of config headers with every scons run.
[platform/upstream/iotivity.git] / extlibs / libcoap / SConscript
1 ################################################################################
2 #
3 # Copyright 2016 Intel Corporation
4 #
5 #
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #      http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 ################################################################################
20
21 ##
22 # 'libcoap' script to check if LibCoAP library is installed.  If not,
23 # get it and install it
24 #
25 ##
26
27 import os
28 import glob
29 import datetime
30 import subprocess
31
32 Import('env')
33
34 libcoap_env = env.Clone()
35 target_os = libcoap_env.get('TARGET_OS')
36 src_dir = libcoap_env.get('SRC_DIR')
37 ca_transport = libcoap_env.get('TARGET_TRANSPORT')
38 with_tcp = libcoap_env.get('WITH_TCP')
39 with_upstream_libcoap = libcoap_env.get('WITH_UPSTREAM_LIBCOAP')
40
41 # Temporary LibCoAP URL is a fork of the original.
42 # Once a pull request is merged, change this back to the obgm original below.
43 libcoap_repo_url    = 'https://github.com/dthaler/libcoap'
44 #libcoap_repo_url    = 'https://github.com/obgm/libcoap'
45
46 ######################################################################
47 # Download libCoAP
48 ######################################################################
49 # Right now this script assumes the revision is a tag, and not a branch or an arbitrary
50 # commit. If this changes, update the check below, or else the script will always conclude
51 # the repo is not up to date because a tag with that name doesn't exist.
52 libcoap_version          = 'IoTivity-1.2.1'
53 libcoap_dir              = os.path.join(src_dir, 'extlibs', 'libcoap', 'libcoap')
54 libcoap_checkout_command = 'git clone ' + libcoap_repo_url + '.git extlibs/libcoap/libcoap -b ' + libcoap_version
55
56 if with_upstream_libcoap == '1':
57     print '*** Checking for installation of libCoAP ***'
58     if not os.path.exists(libcoap_dir):
59         print '''
60 *********************************** Error: ****************************************
61 * Please download libcoap using the following command:                            *
62 *     $ %s                              
63 ***********************************************************************************
64  ''' % libcoap_checkout_command
65         Exit(1)
66
67     os.chdir(libcoap_dir)
68     out = subprocess.check_output('git tag -l ' + libcoap_version, shell = True)
69     if libcoap_version not in out:
70         print out
71         print '''
72 *********************************** Error: ****************************************
73 * Your libCoAP repo is not up to date with the latest version we require. Please  *
74 * update with the following commands:                                             *
75 *     $ cd extlibs/libcoap/libcoap                                                *
76 *     $ git fetch                                                                 *
77 ***********************************************************************************
78  '''
79         Exit(1)
80
81 else:
82     print '''
83 *********************************** Info: *****************************************
84 * Using FORKED copy of libCoap located in:                                        *
85 * resource/csdk/connectivity/lib/libcoap-4.1.1                                    *
86 ***********************************************************************************'''
87
88 ######################################################################
89 # Build libCoAP
90 ######################################################################
91 # As in the source code(C) includes arduino Time library head file(C++)
92 # It requires compile the .c with g++
93 if target_os == 'arduino':
94     libcoap_env.Replace(CFLAGS = libcoap_env.get('CXXFLAGS'))
95     libcoap_env.PrependUnique(CPPPATH = [
96         './',
97         env.get('ARDUINO_HOME') + '/',
98         env.get('ARDUINO_HOME') + '/hardware/arduino/avr/cores/arduino',
99         env.get('ARDUINO_HOME') + '/hardware/tools/avr/avr/include/',
100         env.get('ARDUINO_HOME') + '/hardware/arduino/avr/variants/mega',
101         env.get('ARDUINO_HOME') + '/libraries/Time/Time',
102         env.get('ARDUINO_HOME') + '/libraries/TimedAction',
103         env.get('ARDUINO_HOME') + '/hardware/arduino/avr/libraries/SPI',
104         env.get('ARDUINO_HOME') + '/libraries/Ethernet/src/utility',
105         ])
106
107 # Build flags
108 if target_os not in ['arduino', 'windows', 'winrt', 'msys_nt']:
109     libcoap_env.AppendUnique(CPPDEFINES = ['WITH_POSIX', '_DEFAULT_SOURCE'])
110     libcoap_env.AppendUnique(CFLAGS = ['-std=gnu99','-fPIC'])
111
112 if target_os not in ['windows', 'winrt']:
113     libcoap_env.AppendUnique(CFLAGS = ['-Wall', '-ffunction-sections',
114             '-fdata-sections', '-fno-exceptions'])
115
116 if target_os == 'msys_nt':
117     libcoap_env.AppendUnique(CPPDEFINES = ['_DEFAULT_SOURCE'])
118     libcoap_env.AppendUnique(CFLAGS = ['-std=c99'])
119
120 if target_os in ['linux', 'tizen', 'android', 'ios', 'arduino', 'windows']:
121     if with_tcp == True:
122         libcoap_env.AppendUnique(CPPDEFINES = ['WITH_TCP'])
123
124 if target_os in ['linux', 'tizen', 'android', 'arduino']:
125     libcoap_env.AppendUnique(LIBS = ['log'])
126     if (('BLE' in ca_transport) or ('BT' in ca_transport) or ('ALL' in ca_transport)):
127         libcoap_env.AppendUnique(CPPDEFINES = ['WITH_TCP'])
128
129 if target_os == 'arduino':
130     libcoap_env.AppendUnique(CPPDEFINES = ['NDEBUG', 'WITH_ARDUINO'])
131
132 if target_os in ['darwin', 'ios']:
133     libcoap_env.AppendUnique(CPPDEFINES = ['_DARWIN_C_SOURCE'])
134
135 if env.get('LOGGING') == '1':
136     libcoap_env.AppendUnique(CPPDEFINES = ['TB_LOG'])
137
138 ######################################################################
139 # Source files and Target(s)
140 ######################################################################
141 with_upstream_libcoap = libcoap_env.get('WITH_UPSTREAM_LIBCOAP')
142 config_h_file_path = os.path.join(src_dir, 'extlibs', 'libcoap', 'libcoap', 'include', 'coap', 'coap_config.h')
143 if with_upstream_libcoap == '1':
144     libcoap_env.PrependUnique(CPPPATH = ['libcoap/include/coap'])
145     libcoap_env.AppendUnique(CPPDEFINES = ['WITH_UPSTREAM_LIBCOAP'])
146     libcoap_src_root = os.path.join(libcoap_dir, 'src')
147
148     # We need to generate coap.h from coap.h.in
149     coap_h_pc_file = os.path.join(libcoap_dir, 'include', 'coap', 'coap.h.in')
150     coap_h_output  = os.path.join(libcoap_dir, 'include', 'coap', 'coap.h')
151
152     # Generate coap_config.h
153     target_arch = env.get('TARGET_ARCH')
154     lib_prefix = '' + str(libcoap_env.get('PREFIX'))
155
156     if (not os.path.isfile(config_h_file_path)) and (not env.GetOption('clean')):
157         conf = Configure(libcoap_env)
158
159         config_h_header = '''
160 /* ****************************************************************************
161  * coap_config.h - libcoap platform-specific configuration header.
162  *
163  * Auto-generated code for the %s %s platform.
164  *
165  *************************************************************************** */
166
167 #ifndef _COAP_CONFIG_H_
168 #define _COAP_CONFIG_H_
169
170 ''' % (str(target_os), str(target_arch))
171
172         config_h_body = ''
173
174         config_h_footer = '''
175
176 /* Define to the full name of this package. */
177 #define PACKAGE_NAME "%s"
178
179 /* Define to the full name and version of this package. */
180 #define PACKAGE_STRING "%s"
181
182 #ifndef COAP_STATIC_INLINE
183 #  if defined(__cplusplus)
184 #    define COAP_STATIC_INLINE inline
185 #  else
186 #    ifdef _MSC_VER
187 #      define COAP_STATIC_INLINE static __inline
188 #    else
189 #      define COAP_STATIC_INLINE static inline
190 #    endif
191 #  endif
192 #endif
193
194 #endif // _COAP_CONFIG_H_
195
196     ''' % (str(lib_prefix + 'coap'), str(lib_prefix + 'coap ' + libcoap_version))
197
198         cxx_headers = ['arpa/inet.h',
199                        'assert.h',
200                        'limits.h',
201                        'netinet/in.h',
202                        'stdio.h',
203                        'strings.h',
204                        'sys/select.h',
205                        'sys/socket.h',
206                        'sys/time.h',
207                        'sys/types.h',
208                        'sys/uio.h',
209                        'sys/unistd.h',
210                        'syslog.h',
211                        'time.h',
212                        'unistd.h',
213                        'winsock2.h',
214                        'ws2tcpip.h']
215
216         cxx_functions = ['malloc',
217                          'snprintf',
218                          'strnlen',
219                          'vprintf']
220
221         if target_os == 'arduino':
222             # Detection of headers on the Arduino platform is currently broken.
223             cxx_headers = []
224
225         def get_define_from_string(string):
226             string_converted = string.replace("/","_").replace(".","_").upper()
227             return "HAVE_" + string_converted
228
229         for header_file_name in cxx_headers:
230             if conf.CheckCXXHeader(header_file_name):
231                 config_h_body += "#define %s 1\n\n" % get_define_from_string(header_file_name)
232
233         for function_name in cxx_functions:
234             if conf.CheckFunc(function_name):
235                 config_h_body += "#define %s 1\n\n" % get_define_from_string(function_name)
236
237         if conf.CheckCXXHeader('windows.h'):
238             config_h_body += "#define ssize_t SSIZE_T\n\n"
239             config_h_body += "#define in_port_t uint16_t\n\n"
240
241         conf.Finish()
242
243         # Autoconf feature doesn't work with Jenkins' arduino toolchain, so hardcode it here.
244         if target_os == 'arduino':
245             config_h_body += "#define HAVE_ARDUINO_TIME_H\n\n"
246
247         # Generate the file
248         if os.path.exists(config_h_file_path):
249             os.remove(config_h_file_path)
250         config_h_file = open(config_h_file_path, "w")
251         config_h_file.write(config_h_header + config_h_body + config_h_footer)
252         config_h_file.close()
253
254     # Sanity check to ensure that the above block created the file.
255     if (not os.path.exists(config_h_file_path)) and (not env.GetOption('clean')):
256         print "Error: coap_config.h file not created!"
257         Exit(1)
258
259     pc_vars = {
260         '\@LIBCOAP_PACKAGE_NAME\@'          : lib_prefix + 'coap',
261         '\@LIBCOAP_PACKAGE_STRING\@'        : lib_prefix + 'coap-' + libcoap_version,
262         '\@LIBCOAP_PACKAGE_URL\@'           : libcoap_repo_url,
263         '\@LIBCOAP_PACKAGE_BUGREPORT\@'     : libcoap_repo_url + '/issues',
264         '\@LIBCOAP_PACKAGE_VERSION\@'       : libcoap_version
265         }
266     libcoap_env.Substfile(coap_h_pc_file, SUBST_DICT = pc_vars)
267
268     libcoap_src = glob.glob(os.path.join(libcoap_src_root, '*.c'))
269     libcoap_src.remove(os.path.join(libcoap_src_root, 'coap_io_lwip.c'))
270 else:
271     # For bring up purposes only, the forked version will live here.
272     libcoap_src_root = src_dir + '/resource/csdk/connectivity/lib/libcoap-4.1.1'
273     libcoap_src = glob.glob(os.path.join(libcoap_src_root, '*.c'))
274
275 libcoap = libcoap_env.StaticLibrary('coap', libcoap_src, OBJPREFIX='libcoap_')
276
277 Clean(libcoap, config_h_file_path)
278 libcoap_env.InstallTarget([libcoap], 'coap')
279