1 ################################################################################
3 # Copyright 2016 Intel Corporation
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 ################################################################################
22 # 'libcoap' script to check if LibCoAP library is installed. If not,
23 # get it and install it
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')
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'
46 ######################################################################
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
56 if with_upstream_libcoap == '1':
57 print '*** Checking for installation of libCoAP ***'
58 if not os.path.exists(libcoap_dir):
60 *********************************** Error: ****************************************
61 * Please download libcoap using the following command: *
63 ***********************************************************************************
64 ''' % libcoap_checkout_command
68 out = subprocess.check_output('git tag -l ' + libcoap_version, shell = True)
69 if libcoap_version not in out:
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 *
77 ***********************************************************************************
83 *********************************** Info: *****************************************
84 * Using FORKED copy of libCoap located in: *
85 * resource/csdk/connectivity/lib/libcoap-4.1.1 *
86 ***********************************************************************************'''
88 ######################################################################
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 = [
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',
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'])
112 if target_os not in ['windows', 'winrt']:
113 libcoap_env.AppendUnique(CFLAGS = ['-Wall', '-ffunction-sections',
114 '-fdata-sections', '-fno-exceptions'])
116 if target_os == 'msys_nt':
117 libcoap_env.AppendUnique(CPPDEFINES = ['_DEFAULT_SOURCE'])
118 libcoap_env.AppendUnique(CFLAGS = ['-std=c99'])
120 if target_os in ['linux', 'tizen', 'android', 'ios', 'arduino', 'windows']:
122 libcoap_env.AppendUnique(CPPDEFINES = ['WITH_TCP'])
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'])
129 if target_os == 'arduino':
130 libcoap_env.AppendUnique(CPPDEFINES = ['NDEBUG', 'WITH_ARDUINO'])
132 if target_os in ['darwin', 'ios']:
133 libcoap_env.AppendUnique(CPPDEFINES = ['_DARWIN_C_SOURCE'])
135 if env.get('LOGGING') == '1':
136 libcoap_env.AppendUnique(CPPDEFINES = ['TB_LOG'])
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')
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')
152 # Generate coap_config.h
153 target_arch = env.get('TARGET_ARCH')
154 lib_prefix = '' + str(libcoap_env.get('PREFIX'))
156 if (not os.path.isfile(config_h_file_path)) and (not env.GetOption('clean')):
157 conf = Configure(libcoap_env)
159 config_h_header = '''
160 /* ****************************************************************************
161 * coap_config.h - libcoap platform-specific configuration header.
163 * Auto-generated code for the %s %s platform.
165 *************************************************************************** */
167 #ifndef _COAP_CONFIG_H_
168 #define _COAP_CONFIG_H_
170 ''' % (str(target_os), str(target_arch))
174 config_h_footer = '''
176 /* Define to the full name of this package. */
177 #define PACKAGE_NAME "%s"
179 /* Define to the full name and version of this package. */
180 #define PACKAGE_STRING "%s"
182 #ifndef COAP_STATIC_INLINE
183 # if defined(__cplusplus)
184 # define COAP_STATIC_INLINE inline
187 # define COAP_STATIC_INLINE static __inline
189 # define COAP_STATIC_INLINE static inline
194 #endif // _COAP_CONFIG_H_
196 ''' % (str(lib_prefix + 'coap'), str(lib_prefix + 'coap ' + libcoap_version))
198 cxx_headers = ['arpa/inet.h',
216 cxx_functions = ['malloc',
221 if target_os == 'arduino':
222 # Detection of headers on the Arduino platform is currently broken.
225 def get_define_from_string(string):
226 string_converted = string.replace("/","_").replace(".","_").upper()
227 return "HAVE_" + string_converted
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)
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)
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"
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"
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()
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!"
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
266 libcoap_env.Substfile(coap_h_pc_file, SUBST_DICT = pc_vars)
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'))
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'))
275 libcoap = libcoap_env.StaticLibrary('coap', libcoap_src, OBJPREFIX='libcoap_')
277 Clean(libcoap, config_h_file_path)
278 libcoap_env.InstallTarget([libcoap], 'coap')