-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBuilderCompilerHandler.php
327 lines (262 loc) · 10.5 KB
/
BuilderCompilerHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
namespace Codebender\CompilerBundle\Handler;
// This file uses mktemp() to create a temporary directory where all the files
// needed to process the compile request are stored.
require_once "System.php";
use System;
use Codebender\CompilerBundle\Handler\MCUHandler;
use Codebender\CompilerBundle\Handler\CompilerHandler;
class BuilderCompilerHandler
{
private $preproc;
private $postproc;
private $utility;
function __construct()
{
$this->preproc = new PreprocessingHandler();
$this->postproc = new PostprocessingHandler();
$this->utility = new UtilityHandler();
}
/**
\brief Processes a compile request.
\param string $request The body of the POST request.
\return A message to be JSON-encoded and sent back to the requestor.
*/
function main($request, $compiler_config)
{
error_reporting(E_ALL & ~E_STRICT);
$start_time = microtime(true);
// Step 0: Reject the request if the input data is not valid.
//TODO: Replace $tmp variable name
$tmp = $this->requestValid($request);
if($tmp["success"] == false)
return $tmp;
$this->set_variables($request, $format, $libraries, $version, $mcu, $f_cpu, $core, $variant, $vid, $pid);
// Step 1: Extract the files and filenames included in the request.
$filenames = array();
foreach ($request->files as $file)
array_push($filenames, $file->filename);
$tmp = $this->extractFiles($request, $dir, $files);
if ($tmp["success"] == false)
return $tmp;
$tmp = $this->doCompile($files, $dir, $format, $version, $mcu, $f_cpu, $core, $variant, $pid, $vid);
if ($tmp["success"] == false)
return $tmp;
if ($format == "syntax")
return array(
"success" => true,
"time" => microtime(true) - $start_time);
$build_path = '/tmp/codebender_object_files';
$tmp = $this->convertOutput($build_path, $format, $SIZE, $filenames , $start_time);
return $tmp;
}
private function requestValid(&$request)
{
$request = $this->preproc->validate_input($request);
if (!$request)
return array(
"success" => false,
"step" => 0,
"message" => "Invalid input.");
else return array("success" => true);
}
protected function extractFiles($request, &$dir, &$files)
{
// Create a temporary directory to place all the files needed to process
// the compile request. This directory is created in $TMPDIR or /tmp by
// default and is automatically removed upon execution completion.
$dir = "/tmp/compiler";
mkdir($dir);
if (!$dir)
return array(
"success" => false,
"step" => 1,
"message" => "Failed to create temporary directory.");
$response = $this->utility->extract_files($dir, $request->files);
if ($response["success"] === false)
return $response;
$files = $response["files"];
if (!file_exists($dir."/libraries"))
mkdir($dir."/libraries/", 0777, true);
//TODO: check if it succeeded
$files["libs"] = array();
foreach($request->libraries as $library_name => $library_files)
{
//TODO: check if it succeeded
if (!file_exists($dir."/libraries".$library_name))
mkdir($dir."/libraries/".$library_name, 0777, true);
$tmp = $this->utility->extract_files($dir."/libraries/".$library_name, $library_files);
$files["libs"][] = $tmp["files"];
}
return array("success" => true);
}
protected function doCompile(&$files, $dir, $format, $version, $mcu, $f_cpu, $core, $variant, $pid, $vid)
{
$hardware = "-hardware /home/vagrant/work/src/arduino.cc/builder/hardware -hardware /opt/codebender/codebender-arduino-core-files/v106/hardware -hardware /opt/codebender/codebender-arduino-core-files/v106/hardware/arduino/ -hardware /opt/codebender/codebender-arduino-core-files/v106/hardware/tools/";
$tools = "-tools /usr/bin -tools /opt/codebender/codebender-arduino-core-files/v106/hardware/tools/avr -tools /opt/codebender/codebender-arduino-core-files/v106/tools-builder";
$libraries = "-libraries /opt/codebender/codebender-arduino-core-files/v106/libraries -libraries /opt/codebender/codebender-arduino-core-files/v106/hardware/arduino/avr/libraries -libraries /tmp/compiler/libraries ";
$build_path = "-build-path /tmp/codebender_object_files";
$prefs = "-prefs mcu=$mcu -prefs f_cpu=$f_cpu -prefs core=$core -prefs variant=$variant ";
// If pid and vid build preferences were specified
if($pid != "")
$prefs = $prefs."-prefs pid=$pid ";
if($vid != "")
$prefs = $prefs."-prefs vid=$vid ";
$hardware = stripslashes($hardware);
$tools = stripslashes($tools);
$libraries = stripslashes($libraries);
$build_path = stripslashes($build_path);
$prefs = stripslashes($prefs);
foreach (array("c", "cpp", "S","ino") as $ext)
{
foreach ($files[$ext] as $file)
{
// From hereon, $file is shell escaped and thus should only be used in calls
// to exec().
$file = escapeshellarg($file);
exec("/home/vagrant/work/bin/builder -fqbn arduino:avr:uno $hardware $tools $libraries $prefs $build_path $file.$ext 2>&1", $output, $ret_compile);
if (isset($ret_compile) && $ret_compile)
{
return array(
"success" => false,
"step" => 4,
"message" => $output,
"debug" => $avr_output);
}
unset($output);
$files["o"][] = array_shift($files[$ext]);
}
}
return array("success" => true);
}
protected function convertOutput($dir, $format, $SIZE, $filenames, $start_time)
{
$OUTPUT = $filenames[0];
// To Do, Handle multiple files than just one.
if ($format == "elf")
{
$ret_objcopy = false;
$content = base64_encode(file_get_contents("$dir/$OUTPUT.elf"));
$size = filesize("$dir/$OUTPUT.elf");
}
elseif ($format == "binary")
{
$content = base64_encode(file_get_contents("$dir/$OUTPUT.hex"));
$size = filesize("$dir/$OUTPUT.hex");
}
elseif ($format == "hex")
{
$content = file_get_contents("$dir/$OUTPUT.hex");
$size = filesize("$dir/$OUTPUT.hex");
}
elseif ($format == "object")
{
$content = base64_encode(file_get_contents("$dir/sketch/$OUTPUT.cpp.o"));
$size = filesize("$dir/sketch/$OUTPUT.cpp.o");
}
// If everything went well, return the reply to the caller.
if ($ret_objcopy || $content === false)
return array(
"success" => false,
"step" => 8,
"message" => "There was a problem while generating the your binary file for $dir/sketch/$OUTPUT.cpp.o");
else
return array(
"success" => true,
"time" => microtime(true) - $start_time,
"size" => $size,
"output" => $content);
}
private function preprocessIno(&$files, $ARDUINO_CORES_DIR, $ARDUINO_SKEL, $version, $core)
{
foreach ($files["ino"] as $file)
{
//TODO: make it compatible with non-default hardware (variants & cores)
if (!isset($skel) && ($skel = file_get_contents("$ARDUINO_CORES_DIR/v$version/hardware/arduino/cores/$core/$ARDUINO_SKEL")) === false)
return array(
"success" => false,
"step" => 2,
"message" => "Failed to open Arduino skeleton file.");
$code = file_get_contents("$file.ino");
$new_code = $this->preproc->ino_to_cpp($skel, $code, "$file.ino");
$ret = file_put_contents("$file.cpp", $new_code);
if ($code === false || !$new_code || !$ret)
return array(
"success" => false,
"step" => 2,
"message" => "Failed to preprocess file '$file.ino'.");
$files["cpp"][] = array_shift($files["ino"]);
}
return array("success" => true);
}
public function preprocessHeaders(&$files, &$libraries, &$include_directories, $dir, $ARDUINO_CORES_DIR, $version, $core, $variant)
{
try
{
// Create command-line arguments for header search paths. Note that the
// current directory is added to eliminate the difference between <>
// and "" in include preprocessor directives.
//TODO: make it compatible with non-default hardware (variants & cores)
$include_directories = "-I$dir -I$ARDUINO_CORES_DIR/v$version/hardware/arduino/cores/$core -I$ARDUINO_CORES_DIR/v$version/hardware/arduino/variants/$variant";
//TODO: The code that rests on the main website looks for headers in all files, not just c, cpp and h. Might raise a security issue
$files["dir"] = array();
foreach($libraries as $library_name => $library_files)
{
$files["dir"][] = $dir."/libraries/".$library_name;
}
// Add the libraries' paths in the include paths in the command-line arguments
if (file_exists("$dir/utility"))
$include_directories .= " -I$dir/utility";
foreach ($files["dir"] as $directory)
$include_directories .= " -I$directory";
}
catch(\Exception $e)
{
return array("success" => false, "step" => 3, "message" => "Unknown Error:\n".$e->getMessage());
}
return array("success" => true);
}
/*private function set_values($compiler_config,
&$CC, &$CPP, &$AS, &$LD, &$CLANG, &$OBJCOPY, &$SIZE, &$CFLAGS, &$CPPFLAGS,
&$ASFLAGS, &$LDFLAGS, &$LDFLAGS_TAIL, &$CLANG_FLAGS, &$OBJCOPY_FLAGS, &$SIZE_FLAGS,
&$OUTPUT, &$ARDUINO_CORES_DIR, &$ARDUINO_SKEL)
{
// External binaries.
$CC = $compiler_config["cc"];
$CPP = $compiler_config["cpp"];
$AS = $compiler_config["as"];
$LD = $compiler_config["ld"];
$CLANG = $compiler_config["clang"];
$OBJCOPY = $compiler_config["objcopy"];
$SIZE = $compiler_config["size"];
// Standard command-line arguments used by the binaries.
$CFLAGS = $compiler_config["cflags"];
$CPPFLAGS = $compiler_config["cppflags"];
$ASFLAGS = $compiler_config["asflags"];
$LDFLAGS = $compiler_config["ldflags"];
$LDFLAGS_TAIL = $compiler_config["ldflags_tail"];
$CLANG_FLAGS = $compiler_config["clang_flags"];
$OBJCOPY_FLAGS = $compiler_config["objcopy_flags"];
$SIZE_FLAGS = $compiler_config["size_flags"];
// The default name of the output file.
$OUTPUT = $compiler_config["output"];
// Path to arduino-core-files repository.
$ARDUINO_CORES_DIR = $compiler_config["arduino_cores_dir"];
// The name of the Arduino skeleton file.
$ARDUINO_SKEL = $compiler_config["arduino_skel"];
}*/
private function set_variables($request, &$format, &$libraries, &$version, &$mcu, &$f_cpu, &$core, &$variant, &$vid, &$pid)
{
// Extract the request options for easier access.
$format = $request->format;
$libraries = $request->libraries;
$version = $request->version;
$mcu = $request->build->mcu;
$f_cpu = $request->build->f_cpu;
$core = $request->build->core;
$variant = $request->build->variant;
// Set the appropriate variables for vid and pid (Leonardo).
$vid = ($variant == "leonardo") ? $request->build->vid : "";
$pid = ($variant == "leonardo") ? $request->build->pid : "";
}
}