Imported Upstream version 0.9.1
[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                 timelib_zip_file    = src_dir + '/extlibs/arduino/Time.zip'
73                 timelib_url         = 'http://playground.arduino.cc/uploads/Code/Time.zip'
74                 # Install Arduino Time library
75                 # If the zip file is not already present, download it
76                 os.mkdir(timelib_dir)
77                 os.chdir(timelib_dir)
78                 if not os.path.exists(timelib_zip_file):
79                         timelib_zip = env.Download(timelib_zip_file, timelib_url)
80                 else:
81                         timelib_zip = timelib_zip_file
82
83                 # Unzip the lib
84                 print 'Unzipping Arduino Time lib...'
85                 env.UnpackAll(timelib_dir + '/Time', timelib_zip)
86
87                 # Apply patches to ARDUINO_HOME directory.
88                 os.chdir(arduinolib_dir)
89                 print 'Patching Arduino libraries...'
90                 os.system("find ./libraries/Time/Time/DateStrings.cpp -type f -exec dos2unix {} \;")
91                 os.system("patch -p1 < " + src_dir + "/resource/csdk/connectivity/lib/arduino/arduino_libraries.patch --directory=" + arduinolib_dir)
92
93                 # Remove downloaded file
94                 os.remove(timelib_zip_file)
95
96         redbearlib_dir         = arduinolib_dir + '/libraries/RBL_nRF8001'
97
98         if not os.path.exists(redbearlib_dir):
99                 redbearlib_zip_file    = src_dir + '/extlibs/arduino/25643e7b1b7da3740406325a471e3faa4b948747.zip'
100                 redbearlib_url         = 'https://github.com/RedBearLab/nRF8001/archive/25643e7b1b7da3740406325a471e3faa4b948747.zip'
101                 if not os.path.exists(redbearlib_zip_file):
102                         redbearlib_zip = env.Download(redbearlib_zip_file, redbearlib_url)
103                 else:
104                         redbearlib_zip = redbearlib_zip_file
105
106                 # Unzip the lib
107                 print 'Unzipping Red Bear lib...'
108                 os.chdir(arduinolib_dir + '/libraries')
109                 env.UnpackAll(redbearlib_dir, redbearlib_zip)
110
111                 # Because the way Red Bear lib is distributed... All Red Bear source files must be moved up a few directories.
112                 shutil.move('nRF8001-25643e7b1b7da3740406325a471e3faa4b948747/Arduino/libraries/RBL_nRF8001/', '.')
113                 shutil.rmtree('nRF8001-25643e7b1b7da3740406325a471e3faa4b948747')
114
115                 # Apply Red Bear patches
116                 print 'Patching Red Bear library...'
117                 os.chdir(arduinolib_dir + '/libraries/RBL_nRF8001/')
118                 os.system("find . -type f -exec dos2unix {} \;")
119                 os.system("patch -p1 < " + src_dir + "/resource/csdk/connectivity/lib/arduino/RBL_nRF8001.patch")
120
121                 # Remove downloaded file
122                 os.remove(redbearlib_zip_file)
123
124         nordiclib_dir           = arduinolib_dir + '/libraries/BLE'
125
126         if not os.path.exists(nordiclib_dir):
127                 nordiclib_zip_file    = src_dir + '/extlibs/arduino/ble-sdk-arduino-0.9.5.beta.zip'
128                 nordiclib_url         = 'https://github.com/NordicSemiconductor/ble-sdk-arduino/archive/0.9.5.beta.zip'
129                 if not os.path.exists(nordiclib_zip_file):
130                         nordiclib_zip = env.Download(nordiclib_zip_file, nordiclib_url)
131                 else:
132                         nordiclib_zip = nordiclib_zip_file
133
134                 # Unzip the lib
135                 print 'Unzipping Nordic lib...'
136                 os.chdir(arduinolib_dir + '/libraries')
137                 env.UnpackAll(nordiclib_dir, nordiclib_zip)
138
139                 # Because the way Nordic lib is distributed... All Nordic source files must be moved up a few directories.
140                 shutil.move('ble-sdk-arduino-0.9.5.beta/libraries/BLE/', '.')
141                 shutil.rmtree('ble-sdk-arduino-0.9.5.beta')
142
143                 # Apply Nordic lib patches
144                 print 'Patching Nordic library...'
145                 os.chdir(arduinolib_dir + '/libraries/BLE/')
146                 os.system("find . -type f -exec dos2unix {} \;")
147                 os.system("patch -p1 < " + src_dir + "/resource/csdk/connectivity/lib/arduino/arduino_due_ble.patch")
148                 
149                 # Remove downloaded file
150                 os.remove(nordiclib_zip_file)
151
152 # Set the ARDUINO_HOME
153 env.Replace(ARDUINO_HOME = arduinolib_dir)
154 print 'ARDUINO_HOME = ' + env.get('ARDUINO_HOME')