IOT-1072 Unfork libcoap: add build flag
[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
30 Import('env')
31
32 libcoap_env = env.Clone()
33 target_os = libcoap_env.get('TARGET_OS')
34 src_dir = libcoap_env.get('SRC_DIR')
35 ca_transport = libcoap_env.get('TARGET_TRANSPORT')
36 with_tcp = libcoap_env.get('WITH_TCP')
37 with_upstream_libcoap = libcoap_env.get('WITH_UPSTREAM_LIBCOAP')
38
39 ######################################################################
40 # Download libCoAP
41 ######################################################################
42 libcoap_branch           = 'develop'
43 libcoap_dir              = os.path.join(src_dir, 'extlibs', 'libcoap', 'libcoap')
44 libcoap_zip_file         = os.path.join(src_dir, 'extlibs', 'libcoap', libcoap_branch + '.zip')
45 libcoap_repo_url         = 'https://github.com/obgm/libcoap'
46 libcoap_url              = libcoap_repo_url + '/archive/' + libcoap_branch + '.zip'
47 libcoap_checkout_command = 'git clone ' + libcoap_repo_url + '.git extlibs/libcoap/libcoap --branch ' + libcoap_branch
48
49 if with_upstream_libcoap == '1':
50     print '*** Checking for installation of libCoAP ***'
51     if not os.path.exists(libcoap_dir):
52         # If the libcoap zip file is not already present, download it
53         if not os.path.exists(libcoap_zip_file):
54             libcoap_zip = libcoap_env.Download(libcoap_zip_file, libcoap_url)
55         else:
56             libcoap_zip = libcoap_zip_file
57         # Unzip libcoap
58         if libcoap_zip and not os.path.exists(os.path.join(libcoap_dir, 'configure')):
59             print 'Unzipping libCoAP'
60             env.UnpackAll(libcoap_dir, libcoap_zip)
61         # Rename libcoap
62         libcoap_unzip_dir = os.path.join(src_dir, 'extlibs', 'libcoap', 'libcoap-' + libcoap_branch)
63         if os.path.exists(os.path.join(libcoap_unzip_dir)):
64             os.rename(libcoap_unzip_dir, libcoap_dir)
65         if not os.path.exists(os.path.join(libcoap_dir, 'README')):
66             print '''
67 *********************************** Error: ****************************************
68 * Unable to download and unpack libcoap!                                          *
69 * Please download libcoap using the following command:                            *
70 *                                                                                 *
71 ''' + libcoap_checkout_command + '''
72 *                                                                                 *
73 ***********************************************************************************'''
74             Exit()
75 else:
76     print '''
77 *********************************** Info: *****************************************
78 * Using FORKED copy of libCoap located in:                                        *
79 * resource/csdk/connectivity/lib/libcoap-4.1.1                                    *
80 ***********************************************************************************'''
81
82 ######################################################################
83 # Build libCoAP
84 ######################################################################
85 # As in the source code(C) includes arduino Time library head file(C++)
86 # It requires compile the .c with g++
87 if target_os == 'arduino':
88     libcoap_env.Replace(CFLAGS = libcoap_env.get('CXXFLAGS'))
89     libcoap_env.PrependUnique(CPPPATH = [
90         './',
91         env.get('ARDUINO_HOME') + '/',
92         env.get('ARDUINO_HOME') + '/hardware/arduino/avr/cores/arduino',
93         env.get('ARDUINO_HOME') + '/hardware/tools/avr/avr/include/',
94         env.get('ARDUINO_HOME') + '/hardware/arduino/avr/variants/mega',
95         env.get('ARDUINO_HOME') + '/libraries/Time/Time',
96         env.get('ARDUINO_HOME') + '/libraries/TimedAction',
97         env.get('ARDUINO_HOME') + '/hardware/arduino/avr/libraries/SPI',
98         env.get('ARDUINO_HOME') + '/libraries/Ethernet/src/utility',
99         ])
100
101 # Build flags
102 if target_os not in ['arduino', 'windows', 'winrt', 'msys_nt']:
103     libcoap_env.AppendUnique(CPPDEFINES = ['WITH_POSIX', '_DEFAULT_SOURCE'])
104     libcoap_env.AppendUnique(CFLAGS = ['-std=gnu99','-fPIC'])
105
106 if target_os not in ['windows', 'winrt']:
107     libcoap_env.AppendUnique(CFLAGS = ['-Wall', '-ffunction-sections',
108             '-fdata-sections', '-fno-exceptions'])
109
110 if target_os == 'msys_nt':
111     libcoap_env.AppendUnique(CPPDEFINES = ['_DEFAULT_SOURCE'])
112     libcoap_env.AppendUnique(CFLAGS = ['-std=c99'])
113
114 if target_os in ['linux', 'tizen', 'android', 'ios', 'arduino']:
115     if with_tcp == True:
116         libcoap_env.AppendUnique(CPPDEFINES = ['WITH_TCP'])
117
118 if target_os in ['linux', 'tizen', 'android', 'arduino']:
119     libcoap_env.AppendUnique(LIBS = ['log'])
120     if (('BLE' in ca_transport) or ('BT' in ca_transport) or ('ALL' in ca_transport)):
121         libcoap_env.AppendUnique(CPPDEFINES = ['WITH_TCP'])
122
123 if target_os == 'arduino':
124     libcoap_env.AppendUnique(CPPDEFINES = ['NDEBUG', 'WITH_ARDUINO'])
125
126 if target_os in ['darwin', 'ios']:
127     libcoap_env.AppendUnique(CPPDEFINES = ['_DARWIN_C_SOURCE'])
128
129 if env.get('LOGGING') == '1':
130     libcoap_env.AppendUnique(CPPDEFINES = ['TB_LOG'])
131
132 ######################################################################
133 # Source files and Target(s)
134 ######################################################################
135 with_upstream_libcoap = libcoap_env.get('WITH_UPSTREAM_LIBCOAP')
136 if with_upstream_libcoap == '1':
137     libcoap_env.AppendUnique(CPPDEFINES = ['WITH_UPSTREAM_LIBCOAP'])
138     libcoap_src_root = os.path.join(libcoap_dir, 'src')
139
140     # We need to generate coap.h from coap.h.in
141     coap_h_pc_file = os.path.join(libcoap_dir, 'include', 'coap', 'coap.h.in')
142     coap_h_output  = os.path.join(libcoap_dir, 'include', 'coap', 'coap.h')
143
144     libcoap_version = libcoap_branch
145     lib_prefix = '' + str(libcoap_env.get('PREFIX'))
146     pc_vars = {
147         '\@LIBCOAP_PACKAGE_NAME\@'          : lib_prefix + 'coap',
148         '\@LIBCOAP_PACKAGE_STRING\@'        : lib_prefix + 'coap-' + libcoap_version,
149         '\@LIBCOAP_PACKAGE_URL\@'           : libcoap_repo_url,
150         '\@LIBCOAP_PACKAGE_BUGREPORT\@'     : libcoap_repo_url + '/issues',
151         '\@LIBCOAP_PACKAGE_VERSION\@'       : libcoap_version
152         }
153     libcoap_env.Substfile(coap_h_pc_file, SUBST_DICT = pc_vars)
154 else:
155     # For bring up purposes only, the forked version will live here.
156     libcoap_src_root = src_dir + '/resource/csdk/connectivity/lib/libcoap-4.1.1'
157
158 libcoap_src = glob.glob(os.path.join(libcoap_src_root, '*.c'))
159 libcoap = libcoap_env.StaticLibrary('coap', libcoap_src, OBJPREFIX='libcoap_')
160
161 libcoap_env.InstallTarget([libcoap], 'coap')
162