Skip to content

Commit 6e33cfc

Browse files
author
Martin Krulis
committed
Make gcc/gpp compilation box more efficient by keeping only c, cc, and cpp files on the command line.
1 parent 04dad05 commit 6e33cfc

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

app/helpers/ExerciseConfig/Pipeline/Box/Boxes/GccCompilationBox.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class GccCompilationBox extends CompilationBox
2323
public static $GCC_TYPE = "gcc";
2424
public static $GCC_BINARY = "/usr/bin/gcc";
2525
public static $DEFAULT_NAME = "GCC Compilation";
26+
public static $CPP_EXT_FILTER = '/[.](cpp|c|cc)$/i';
2627

2728
private static $initialized = false;
2829
private static $defaultInputPorts;
@@ -105,10 +106,8 @@ public function compile(CompilationParams $params): array {
105106
$task->setCommandArguments(
106107
array_merge(
107108
$args,
108-
$this->getInputPortValue(self::$SOURCE_FILES_PORT_KEY)
109-
->getValue(ConfigParams::$EVAL_DIR),
110-
$this->getInputPortValue(self::$EXTRA_FILES_PORT_KEY)
111-
->getValue(ConfigParams::$EVAL_DIR),
109+
$this->getInputPortFilesFiltered(self::$SOURCE_FILES_PORT_KEY, ConfigParams::$EVAL_DIR, self::$CPP_EXT_FILTER),
110+
$this->getInputPortFilesFiltered(self::$EXTRA_FILES_PORT_KEY, ConfigParams::$EVAL_DIR, self::$CPP_EXT_FILTER),
112111
[
113112
"-o",
114113
$this->getOutputPortValue(self::$BINARY_FILE_PORT_KEY)

app/helpers/ExerciseConfig/Pipeline/Box/Boxes/GppCompilationBox.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class GppCompilationBox extends CompilationBox
2323
public static $GPP_TYPE = "g++";
2424
public static $GPP_BINARY = "/usr/bin/g++";
2525
public static $DEFAULT_NAME = "G++ Compilation";
26+
public static $CPP_EXT_FILTER = '/[.](cpp|c|cc)$/i';
2627

2728
private static $initialized = false;
2829
private static $defaultInputPorts;
@@ -105,10 +106,8 @@ public function compile(CompilationParams $params): array {
105106
$task->setCommandArguments(
106107
array_merge(
107108
$args,
108-
$this->getInputPortValue(self::$SOURCE_FILES_PORT_KEY)
109-
->getValue(ConfigParams::$EVAL_DIR),
110-
$this->getInputPortValue(self::$EXTRA_FILES_PORT_KEY)
111-
->getValue(ConfigParams::$EVAL_DIR),
109+
$this->getInputPortFilesFiltered(self::$SOURCE_FILES_PORT_KEY, ConfigParams::$EVAL_DIR, self::$CPP_EXT_FILTER),
110+
$this->getInputPortFilesFiltered(self::$EXTRA_FILES_PORT_KEY, ConfigParams::$EVAL_DIR, self::$CPP_EXT_FILTER),
112111
[
113112
"-o",
114113
$this->getOutputPortValue(self::$BINARY_FILE_PORT_KEY)

app/helpers/ExerciseConfig/Pipeline/Box/Boxes/base/CompilationBox.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use App\Helpers\JobConfig\SandboxConfig;
1212
use App\Helpers\JobConfig\Tasks\Task;
1313
use Nette\Utils\Random;
14+
use Exception;
1415

1516

1617
/**
@@ -77,4 +78,27 @@ protected function compileExistsTask(array $files): Task {
7778
return $task;
7879
}
7980

81+
82+
/**
83+
* Get value of given port (assuming file[] type) and return its value filtered.
84+
* @param string $portName The port identifier.
85+
* @param string $baseDir Base directory (file prefix).
86+
* @param $filter String (regular expression) or callable (filtering function).
87+
* @return mixed List of files (paths).
88+
*/
89+
protected function getInputPortFilesFiltered(string $portName, string $baseDir, $filter)
90+
{
91+
$files = $this->getInputPortValue($portName)->getValue($baseDir);
92+
if (is_string($filter)) {
93+
$realFilter = function($file) use ($filter) {
94+
return preg_match($filter, $file);
95+
};
96+
} else if (is_callable($filter)) {
97+
$realFilter = $filter;
98+
} else {
99+
throw new Exception("Invalid type of file filter."); // this should never happen
100+
}
101+
return array_filter($files, $realFilter);
102+
}
103+
80104
}

tests/ExerciseConfig/Compilation/BaseCompiler.phpt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ class TestBaseCompiler extends Tester\TestCase
8585
]
8686
];
8787
private static $envVariablesTable = [
88-
[ "name" => "source_files", "type" => "file[]", "value" => "source" ],
88+
[ "name" => "source_files", "type" => "file[]", "value" => "source.cpp" ],
8989
[ "name" => "expected_output", "type" => "file", "value" => '$expected-a-out' ]
9090
];
9191
private static $environment = "envA";
9292
private static $compilationPipeline = [
9393
"variables" => [
94-
["name" => "source_files", "type" => "file[]", "value" => ["source"]],
95-
["name" => "extra_files", "type" => "file[]", "value" => ["extra"]],
94+
["name" => "source_files", "type" => "file[]", "value" => ["source.cpp"]],
95+
["name" => "extra_files", "type" => "file[]", "value" => ["extra.cpp"]],
9696
["name" => "binary_file", "type" => "file", "value" => "a.out"]
9797
],
9898
"boxes" => [
@@ -219,10 +219,10 @@ class TestBaseCompiler extends Tester\TestCase
219219
"expected.B.out" => "expected.B.out.hash",
220220
"expected.B.in" => "expected.B.in.hash"
221221
];
222-
private static $submitFiles = [ "source" ];
222+
private static $submitFiles = [ "source.cpp" ];
223223
private static $solutionParams = [
224224
"variables" => [
225-
["name" => "expected-a-out", "value" => "source"]
225+
["name" => "expected-a-out", "value" => "source.cpp"]
226226
]
227227
];
228228

@@ -348,7 +348,7 @@ class TestBaseCompiler extends Tester\TestCase
348348
Assert::count(1, $testACopyTask->getDependencies());
349349
Assert::equal([$testAMkdir->getId()], $testACopyTask->getDependencies());
350350
Assert::equal("cp", $testACopyTask->getCommandBinary());
351-
Assert::equal([ConfigParams::$SOURCE_DIR . "source", ConfigParams::$SOURCE_DIR . "testA/expected.out"], $testACopyTask->getCommandArguments());
351+
Assert::equal([ConfigParams::$SOURCE_DIR . "source.cpp", ConfigParams::$SOURCE_DIR . "testA/expected.out"], $testACopyTask->getCommandArguments());
352352
Assert::null($testACopyTask->getType());
353353
Assert::equal("testA", $testACopyTask->getTestId());
354354
Assert::null($testACopyTask->getSandboxConfig());
@@ -359,7 +359,7 @@ class TestBaseCompiler extends Tester\TestCase
359359
Assert::count(1, $testAExtraTask->getDependencies());
360360
Assert::equal([$testAMkdir->getId()], $testAExtraTask->getDependencies());
361361
Assert::equal("cp", $testAExtraTask->getCommandBinary());
362-
Assert::equal([ConfigParams::$SOURCE_DIR . "extra", ConfigParams::$SOURCE_DIR . "testA/extra"], $testAExtraTask->getCommandArguments());
362+
Assert::equal([ConfigParams::$SOURCE_DIR . "extra.cpp", ConfigParams::$SOURCE_DIR . "testA/extra.cpp"], $testAExtraTask->getCommandArguments());
363363
Assert::null($testAExtraTask->getType());
364364
Assert::equal("testA", $testAExtraTask->getTestId());
365365
Assert::null($testAExtraTask->getSandboxConfig());
@@ -370,7 +370,7 @@ class TestBaseCompiler extends Tester\TestCase
370370
Assert::count(1, $testASourceTask->getDependencies());
371371
Assert::equal([$testAMkdir->getId()], $testASourceTask->getDependencies());
372372
Assert::equal("cp", $testASourceTask->getCommandBinary());
373-
Assert::equal([ConfigParams::$SOURCE_DIR . "source", ConfigParams::$SOURCE_DIR . "testA/source"], $testASourceTask->getCommandArguments());
373+
Assert::equal([ConfigParams::$SOURCE_DIR . "source.cpp", ConfigParams::$SOURCE_DIR . "testA/source.cpp"], $testASourceTask->getCommandArguments());
374374
Assert::null($testASourceTask->getType());
375375
Assert::equal("testA", $testASourceTask->getTestId());
376376
Assert::null($testASourceTask->getSandboxConfig());
@@ -381,7 +381,7 @@ class TestBaseCompiler extends Tester\TestCase
381381
Assert::count(3, $testACompilationTask->getDependencies());
382382
Assert::equal([$testASourceTask->getId(), $testAExtraTask->getId(), $testAMkdir->getId()], $testACompilationTask->getDependencies());
383383
Assert::equal(GccCompilationBox::$GCC_BINARY, $testACompilationTask->getCommandBinary());
384-
Assert::equal([ConfigParams::$EVAL_DIR . "source", ConfigParams::$EVAL_DIR . "extra", "-o", ConfigParams::$EVAL_DIR . "a.out"], $testACompilationTask->getCommandArguments());
384+
Assert::equal([ConfigParams::$EVAL_DIR . "source.cpp", ConfigParams::$EVAL_DIR . "extra.cpp", "-o", ConfigParams::$EVAL_DIR . "a.out"], $testACompilationTask->getCommandArguments());
385385
Assert::equal(TaskType::$INITIATION, $testACompilationTask->getType());
386386
Assert::equal("testA", $testACompilationTask->getTestId());
387387
Assert::notEqual(null, $testACompilationTask->getSandboxConfig());
@@ -449,7 +449,7 @@ class TestBaseCompiler extends Tester\TestCase
449449
Assert::count(1, $testBExtraTask->getDependencies());
450450
Assert::equal([$testBMkdir->getId()], $testBExtraTask->getDependencies());
451451
Assert::equal(TaskCommands::$COPY, $testBExtraTask->getCommandBinary());
452-
Assert::equal([ConfigParams::$SOURCE_DIR . "extra", ConfigParams::$SOURCE_DIR . "testB/extra"], $testBExtraTask->getCommandArguments());
452+
Assert::equal([ConfigParams::$SOURCE_DIR . "extra.cpp", ConfigParams::$SOURCE_DIR . "testB/extra.cpp"], $testBExtraTask->getCommandArguments());
453453
Assert::null($testBExtraTask->getType());
454454
Assert::equal("testB", $testBExtraTask->getTestId());
455455
Assert::null($testBExtraTask->getSandboxConfig());
@@ -460,7 +460,7 @@ class TestBaseCompiler extends Tester\TestCase
460460
Assert::count(1, $testBSourceTask->getDependencies());
461461
Assert::equal([$testBMkdir->getId()], $testBSourceTask->getDependencies());
462462
Assert::equal(TaskCommands::$COPY, $testBSourceTask->getCommandBinary());
463-
Assert::equal([ConfigParams::$SOURCE_DIR . "source", ConfigParams::$SOURCE_DIR . "testB/source"], $testBSourceTask->getCommandArguments());
463+
Assert::equal([ConfigParams::$SOURCE_DIR . "source.cpp", ConfigParams::$SOURCE_DIR . "testB/source.cpp"], $testBSourceTask->getCommandArguments());
464464
Assert::null($testBSourceTask->getType());
465465
Assert::equal("testB", $testBSourceTask->getTestId());
466466
Assert::null($testBSourceTask->getSandboxConfig());
@@ -471,7 +471,7 @@ class TestBaseCompiler extends Tester\TestCase
471471
Assert::count(3, $testBCompilationTask->getDependencies());
472472
Assert::equal([$testBSourceTask->getId(), $testBExtraTask->getId(), $testBMkdir->getId()], $testBCompilationTask->getDependencies());
473473
Assert::equal(GccCompilationBox::$GCC_BINARY, $testBCompilationTask->getCommandBinary());
474-
Assert::equal([ConfigParams::$EVAL_DIR . "source", ConfigParams::$EVAL_DIR . "extra", "-o", ConfigParams::$EVAL_DIR . "a.out"], $testBCompilationTask->getCommandArguments());
474+
Assert::equal([ConfigParams::$EVAL_DIR . "source.cpp", ConfigParams::$EVAL_DIR . "extra.cpp", "-o", ConfigParams::$EVAL_DIR . "a.out"], $testBCompilationTask->getCommandArguments());
475475
Assert::equal(TaskType::$INITIATION, $testBCompilationTask->getType());
476476
Assert::equal("testB", $testBCompilationTask->getTestId());
477477
Assert::notEqual(null, $testBCompilationTask->getSandboxConfig());

0 commit comments

Comments
 (0)