Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare script language defaults #2135

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions judge/judgedaemon.main.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,26 +405,38 @@ function fetch_executable_internal(
if ($execlang === false) {
return [null, "executable must either provide an executable file named 'build' or a C/C++/Java or Python file.", null];
}
// Gather compiler & arguments
$language_config = dj_json_decode(request('languages/' . $execlang, 'GET'));
switch ($execlang) {
case 'c':
$buildscript .= "gcc -Wall -O2 -std=gnu11 $source -o run -lm\n";
$compiler = $language_config['compiler']['command'] ?? 'gcc';
$flags = $language_config['compiler']['args'] ?? '-Wall -O2 -std=gnu11';
$buildscript .= "$compiler $flags $source -o run -lm\n";
break;
case 'cpp':
$buildscript .= "g++ -Wall -O2 -std=gnu++17 $source -o run\n";
$compiler = $language_config['compiler']['command'] ?? 'g++';
$flags = $language_config['compiler']['args'] ?? '-Wall -O2 -std=gnu++17';
$buildscript .= "$compiler $flags $source -o run\n";
break;
case 'java':
$buildscript .= "javac -cp . -d . $source\n";
$compiler = $language_config['compiler']['command'] ?? 'javac';
$flags = $language_config['compiler']['args'] ?? '';
$runner = $language_config['runner']['command'] ?? 'java';
$runner_flags = $language_config['runner']['args'] ?? '';
$buildscript .= "$compiler $flags -cp . -d . $source\n";
$buildscript .= "echo '#!/bin/sh' > run\n";
// no main class detection here
$buildscript .= "echo 'COMPARE_DIR=\$(dirname \"\$0\")' >> run\n";
$mainClass = basename($unescapedSource, '.java');
$buildscript .= "echo 'java -cp \"\$COMPARE_DIR\" $mainClass \"\\\$@\"' >> run\n";
$buildscript .= "echo '$runner $runner_flags -cp \"\$COMPARE_DIR\" $mainClass \"\\\$@\"' >> run\n";
$buildscript .= "chmod +x run\n";
break;
case 'py':
$runner = $language_config['runner']['command'] ?? 'pypy3';
$flags = $language_config['runner']['args'] ?? '';
$buildscript .= "echo '#!/bin/sh' > run\n";
$buildscript .= "echo 'COMPARE_DIR=\$(dirname \"\$0\")' >> run\n";
$buildscript .= "echo 'python3 \$COMPARE_DIR/$source' \"\\\$@\" >> run\n";
$buildscript .= "echo '$runner $flags \$COMPARE_DIR/$source' \"\\\$@\" >> run\n";
$buildscript .= "chmod +x run\n";
break;
}
Expand Down
31 changes: 31 additions & 0 deletions webapp/migrations/Version20230813111642.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20230813111642 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add the {compiler,runner} run/build command (+ arguments) to the database.';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE language ADD compiler_command VARCHAR(255) DEFAULT NULL COMMENT \'Compiler command\', ADD runner_command VARCHAR(255) DEFAULT NULL COMMENT \'Runner command\', ADD compiler_command_args VARCHAR(255) DEFAULT NULL COMMENT \'Compiler command arguments\', ADD runner_command_args VARCHAR(255) DEFAULT NULL COMMENT \'Runner command arguments\'');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE language DROP compiler_command, DROP runner_command, DROP compiler_command_args, DROP runner_command_args');
}

public function isTransactional(): bool
{
return false;
}
}
76 changes: 74 additions & 2 deletions webapp/src/Entity/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ class Language extends BaseApiEntity
#[Serializer\Exclude]
private Collection $versions;

#[ORM\Column(type: 'string', length: 255, nullable: true, options: ['comment' => 'Compiler command'])]
#[Serializer\Exclude]
private ?string $compilerCommand = null;

#[ORM\Column(type: 'string', length: 255, nullable: true, options: ['comment' => 'Runner command'])]
#[Serializer\Exclude]
private ?string $runnerCommand = null;

#[ORM\Column(type: 'string', length: 255, nullable: true, options: ['comment' => 'Compiler command arguments'])]
#[Serializer\Exclude]
private ?string $compilerCommandArgs = null;

#[ORM\Column(type: 'string', length: 255, nullable: true, options: ['comment' => 'Runner command arguments'])]
#[Serializer\Exclude]
private ?string $runnerCommandArgs = null;

#[ORM\Column(type: 'blobtext', nullable: true, options: ['comment' => 'Compiler version'])]
#[Serializer\Exclude]
private ?string $compilerVersion = null;
Expand Down Expand Up @@ -157,6 +173,50 @@ public function getVersions(): Collection
return $this->versions;
}

public function getCompilerCommand(): ?string
{
return $this->compilerCommand;
}

public function setCompilerCommand(?string $compilerCommand): Language
{
$this->compilerCommand = $compilerCommand;
return $this;
}

public function getRunnerCommand(): ?string
{
return $this->runnerCommand;
}

public function setRunnerCommand(?string $runnerCommand): Language
{
$this->runnerCommand = $runnerCommand;
return $this;
}

public function getCompilerCommandArgs(): ?string
{
return $this->compilerCommandArgs;
}

public function setCompilerCommandArgs(?string $compilerCommandArgs): Language
{
$this->compilerCommandArgs = $compilerCommandArgs;
return $this;
}

public function getRunnerCommandArgs(): ?string
{
return $this->runnerCommandArgs;
}

public function setRunnerCommandArgs(?string $runnerCommandArgs): Language
{
$this->runnerCommandArgs = $runnerCommandArgs;
return $this;
}

public function getCompilerVersion(): ?string
{
return $this->compilerVersion;
Expand Down Expand Up @@ -216,10 +276,16 @@ public function getCompileExecutableHash(): ?string
*/
#[Serializer\VirtualProperty]
#[Serializer\SerializedName('compiler')]
#[Serializer\Exclude(if:'object.getCompilerVersionCommand() == ""')]
#[Serializer\Exclude(if:'object.getCompilerVersionCommand() == "" && object.getCompilerCommand() == ""')]
public function getCompilerData(): array
{
$ret = [];
if (!empty($this->getCompilerCommand())) {
$ret['command'] = $this->getCompilerCommand();
if (!empty($this->getCompilerCommandArgs())) {
$ret['args'] = $this->getCompilerCommandArgs();
}
}
if (!empty($this->getCompilerVersionCommand())) {
$ret['version_command'] = $this->getCompilerVersionCommand();
if (!empty($this->getCompilerVersion())) {
Expand All @@ -234,10 +300,16 @@ public function getCompilerData(): array
*/
#[Serializer\VirtualProperty]
#[Serializer\SerializedName('runner')]
#[Serializer\Exclude(if:'object.getRunnerVersionCommand() == ""')]
#[Serializer\Exclude(if:'object.getRunnerVersionCommand() == "" && object.getRunnerCommand() == ""')]
public function getRunnerData(): array
{
$ret = [];
if (!empty($this->getRunnerCommand())) {
$ret['command'] = $this->getRunnerCommand();
if (!empty($this->getRunnerCommandArgs())) {
$ret['args'] = $this->getRunnerCommandArgs();
}
}
if (!empty($this->getRunnerVersionCommand())) {
$ret['version_command'] = $this->getRunnerVersionCommand();
if (!empty($this->getRunnerVersion())) {
Expand Down
16 changes: 16 additions & 0 deletions webapp/src/Form/Type/LanguageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'No' => false,
],
]);
$builder->add('compilerCommand', TextType::class, [
'label' => 'Compiler command',
'required' => false,
]);
$builder->add('compilerCommandArgs', TextType::class, [
'label' => 'Compiler command arguments',
'required' => false,
]);
$builder->add('runnerCommand', TextType::class, [
'label' => 'Runner command',
'required' => false,
]);
$builder->add('runnerCommandArgs', TextType::class, [
'label' => 'Runner command arguments',
'required' => false,
]);
$builder->add('compilerVersionCommand', TextType::class, [
'label' => 'Compiler version command',
'required' => false,
Expand Down
34 changes: 34 additions & 0 deletions webapp/templates/jury/language.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,40 @@
{{ language.filterCompilerFiles | printYesNo }}
</td>
</tr>
<tr>
<th>Compiler command</th>
<td>
{% if language.compilerCommand %}
<pre class="output_text" style="white-space:normal">
$ {{ language.compilerCommand }}
{% if language.compilerCommandArgs %}
{{ language.compilerCommandArgs }}
{% else %}
<p class="nodata">(No command arguments specified.)</p>
{% endif %}
</pre>
{% else %}
<p class="nodata">No command specified.</p>
{% endif %}
</td>
</tr>
<tr>
<th>Runner command</th>
<td>
{%- if language.runnerCommand -%}
<pre class="output_text" style="white-space:normal">
$ {{ language.runnerCommand }}
{% if language.runnerCommandArgs %}
{{ language.runnerCommandArgs }}
{% else %}
<p class="nodata">(No command arguments specified.)</p>
{% endif %}
</pre>
{% else %}
<p class="nodata">No command specified.</p>
{% endif %}
</td>
</tr>
<tr>
<th>Compiler version</th>
<td>
Expand Down
3 changes: 3 additions & 0 deletions webapp/tests/Unit/Controller/API/LanguageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ class LanguageControllerTest extends BaseTestCase
'entry_point_required' => false,
'entry_point_name' => null,
'extensions' => ['cpp', 'cc', 'cxx', 'c++'],
'compiler' => ['version_command' => 'g++ --version']
],
'java' => [
'name' => 'Java',
'entry_point_required' => true,
'entry_point_name' => 'Main class',
'extensions' => ['java'],
'compiler' => ['version_command' => 'javac -version'],
'runner' => ['version_command' => 'java -version'],
],
];

Expand Down
5 changes: 5 additions & 0 deletions webapp/tests/Unit/Controller/Jury/LanguagesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ class LanguagesControllerTest extends JuryControllerTestCase
'filterCompilerFiles' => '0'],
['langid' => 'compVers',
'compilerVersionCommand' => 'unit -V'],
['langid' => 'runCompileCommand',
'compilerCommand' => 'runc',
'compilerCommandArgs' => '-a -b -c {files}',
'runnerCommand' => 'run',
'runnerCommandArgs' => '-g "\$MAIN" {files}'],
['langid' => 'runVers',
'runnerVersionCommand' => 'run -x |yes|tr "\n" "\`true\`"']];
protected static array $addEntitiesFailure = ['Only alphanumeric characters and ._- are allowed' => [['langid' => '§$#`"'],
Expand Down
Loading