Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / extlibs / arduino / SConscript
1 ##
2 # Script to install (if they do not exist) the Arduino library, Time library,
3 # Red Bear Library, and Nordic library.
4 ##
5
6 import os, subprocess, struct
7 import urllib2, urlparse
8 import SCons.Errors
9 import shutil
10
11 Import('env')
12
13 target_os = env.get('TARGET_OS')
14 src_dir = env.get('SRC_DIR')
15
16 SConscript(src_dir + '/build_common/tools/UnpackAll.py')
17 SConscript(src_dir + '/build_common/external_libs.scons')
18
19 # Download
20 if target_os == 'arduino':
21         arduino_home = env.get('ARDUINO_HOME')
22         if not arduino_home:
23                 print 'Creating ARDUINO_HOME for Arduino lib'
24                 print '''
25         *******************************************************************************
26         *   Arduino root directory isn't set, you can set enviornment variable        *
27         *   ARDUINO_HOME or add it in command line as follows (Only set if your       *
28         *   version has fixes applied as depicted below. Press ctrl+c now if you      *
29         *   wish to manually set ARDUINO_HOME.):                                      *
30         *      # scons ARDUINO_HOME=<path to arduino root directory> ...              *
31         *******************************************************************************
32         '''
33                 arduinolib_dir      = src_dir + '/extlibs/arduino/arduino-1.5.8'
34
35                 if not os.path.exists(arduinolib_dir):
36                         from sys import platform as _platform
37                         if _platform == "linux" or _platform == "linux2":
38                                 archType = 8 * struct.calcsize("P")
39                                 print 'On %s-bit machine.' % (archType)
40                                 if archType == 32:
41                                         arduinolib_zip_file = src_dir + '/extlibs/arduino/arduino-1.5.8-linux32.tgz'
42                                         arduinolib_url      = 'http://arduino.cc/download.php?f=/arduino-1.5.8-linux32.tgz'
43                                 else:
44                                         arduinolib_zip_file = src_dir + '/extlibs/arduino/arduino-1.5.8-linux64.tgz'
45                                         arduinolib_url      = 'http://arduino.cc/download.php?f=/arduino-1.5.8-linux64.tgz'
46                         elif _platform == "darwin":
47                                 arduinolib_zip_file = src_dir + '/extlibs/arduino/arduino-1.5.8-macosx.zip'
48                                 arduinolib_url      = 'http://arduino.cc/download.php?f=/arduino-1.5.8-macosx.zip'
49                         elif _platform == "win32":
50                                 arduinolib_zip_file = src_dir + '/extlibs/arduino/arduino-1.5.8-windows.zip'
51                                 arduinolib_url      = src_dir + 'http://arduino.cc/download.php?f=/arduino-1.5.8-windows.zip'
52
53                         # If the zip file is not already present, download it
54                         if not os.path.exists(arduinolib_zip_file):
55                                 arduinolib_zip = env.Download(arduinolib_zip_file, arduinolib_url)
56                         else:
57                                 arduinolib_zip = arduinolib_zip_file
58
59                         # Unzip the lib
60                         print 'Unzipping arduino lib...'
61                         env.UnpackAll(arduinolib_dir, arduinolib_zip)
62                         print 'Unzipping arduino lib complete'
63
64                         # Remove downloaded file
65                         os.remove(arduinolib_zip_file)
66         else:
67                 arduinolib_dir = env.get('ARDUINO_HOME')
68
69         timelib_dir         = arduinolib_dir + '/libraries/Time'
70
71         if not os.path.exists(timelib_dir):
72                 if WhereIs('dos2unix') is not None:
73                         timelib_zip_file    = src_dir + '/extlibs/arduino/Time.zip'
74                         timelib_url         = 'http://playground.arduino.cc/uploads/Code/Time.zip'
75                         # Install Arduino Time library
76                         # If the zip file is not already present, download it
77                         os.mkdir(timelib_dir)
78                         os.chdir(timelib_dir)
79                         if not os.path.exists(timelib_zip_file):
80                                 timelib_zip = env.Download(timelib_zip_file, timelib_url)
81                         else:
82                                 timelib_zip = timelib_zip_file
83
84                         # Unzip the lib
85                         print 'Unzipping Arduino Time lib...'
86                         env.UnpackAll(timelib_dir + '/Time', timelib_zip)
87
88                         # Apply patches to ARDUINO_HOME directory.
89                         os.chdir(arduinolib_dir)
90                         print 'Patching Arduino libraries...'
91
92                         os.system("find ./libraries/Time/Time/DateStrings.cpp -type f -exec dos2unix {} \;")
93                         os.system("patch -p1 < " + src_dir + "/resource/csdk/connectivity/lib/arduino/arduino_libraries.patch --directory=" + arduinolib_dir)
94
95                         # Remove downloaded file
96                         os.remove(timelib_zip_file)
97                 else:
98                         print 'Please manually install package dos2unix. The build process will end now. Your Action Required: Install package manually, then restart build process.'
99                         print 'You may likely retrieve this package as follows:'
100                         print '      sudo apt-get install dos2unix'
101                         print '               or'
102                         print '      sudo yum install dos2unix'
103                         exit(1)
104
105         redbearlib_dir         = arduinolib_dir + '/libraries/RBL_nRF8001'
106
107         if not os.path.exists(redbearlib_dir):
108                 if WhereIs('dos2unix') is not None:
109                         redbearlib_zip_file    = src_dir + '/extlibs/arduino/25643e7b1b7da3740406325a471e3faa4b948747.zip'
110                         redbearlib_url         = 'https://github.com/RedBearLab/nRF8001/archive/25643e7b1b7da3740406325a471e3faa4b948747.zip'
111                         if not os.path.exists(redbearlib_zip_file):
112                                 redbearlib_zip = env.Download(redbearlib_zip_file, redbearlib_url)
113                         else:
114                                 redbearlib_zip = redbearlib_zip_file
115
116                         # Unzip the lib
117                         print 'Unzipping Red Bear lib...'
118                         os.chdir(arduinolib_dir + '/libraries')
119                         env.UnpackAll(redbearlib_dir, redbearlib_zip)
120
121                         # Because the way Red Bear lib is distributed... All Red Bear source files must be moved up a few directories.
122                         shutil.move('nRF8001-25643e7b1b7da3740406325a471e3faa4b948747/Arduino/libraries/RBL_nRF8001/', '.')
123                         shutil.rmtree('nRF8001-25643e7b1b7da3740406325a471e3faa4b948747')
124
125                         # Apply Red Bear patches
126                         print 'Patching Red Bear library...'
127                         os.chdir(arduinolib_dir + '/libraries/RBL_nRF8001/')
128
129                         os.system("find . -type f -exec dos2unix {} \;")
130                         os.system("patch -p1 < " + src_dir + "/resource/csdk/connectivity/lib/arduino/RBL_nRF8001.patch")
131
132                         # Remove downloaded file
133                         os.remove(redbearlib_zip_file)
134                 else:
135                         print 'Please manually install package dos2unix. The build process will end now. Your Action Required: Install package manually, then restart build process.'
136                         print 'You may likely retrieve this package as follows:'
137                         print '      sudo apt-get install dos2unix'
138                         print '               or'
139                         print '      sudo yum install dos2unix'
140                         exit(1)
141
142         nordiclib_dir           = arduinolib_dir + '/libraries/BLE'
143
144         if not os.path.exists(nordiclib_dir):
145                 if WhereIs('dos2unix') is not None:
146                         nordiclib_zip_file    = src_dir + '/extlibs/arduino/ble-sdk-arduino-0.9.5.beta.zip'
147                         nordiclib_url         = 'https://github.com/NordicSemiconductor/ble-sdk-arduino/archive/0.9.5.beta.zip'
148                         if not os.path.exists(nordiclib_zip_file):
149                                 nordiclib_zip = env.Download(nordiclib_zip_file, nordiclib_url)
150                         else:
151                                 nordiclib_zip = nordiclib_zip_file
152
153                         # Unzip the lib
154                         print 'Unzipping Nordic lib...'
155                         os.chdir(arduinolib_dir + '/libraries')
156                         env.UnpackAll(nordiclib_dir, nordiclib_zip)
157
158                         # Because the way Nordic lib is distributed... All Nordic source files must be moved up a few directories.
159                         shutil.move('ble-sdk-arduino-0.9.5.beta/libraries/BLE/', '.')
160                         shutil.rmtree('ble-sdk-arduino-0.9.5.beta')
161
162                         # Apply Nordic lib patches
163                         print 'Patching Nordic library...'
164                         os.chdir(arduinolib_dir + '/libraries/BLE/')
165
166                         os.system("find . -type f -exec dos2unix {} \;")
167                         os.system("patch -p1 < " + src_dir + "/resource/csdk/connectivity/lib/arduino/arduino_due_ble.patch")
168                 
169                         # Remove downloaded file
170                         os.remove(nordiclib_zip_file)
171                 else:
172                         print 'Please manually install package dos2unix. The build process will end now. Your Action Required: Install package manually, then restart build process.'
173                         print 'You may likely retrieve this package as follows:'
174                         print '      sudo apt-get install dos2unix'
175                         print '               or'
176                         print '      sudo yum install dos2unix'
177                         exit(1)
178
179 # Set the ARDUINO_HOME
180 env.Replace(ARDUINO_HOME = arduinolib_dir)
181 print 'ARDUINO_HOME = ' + env.get('ARDUINO_HOME')