Skip to content

Commit dfcdfd6

Browse files
authored
Compilation improvements (#38)
* Print gcc output with colors * Add option to compile with weaker flags * Fix tests * Remove hasattr check * Bump version * Small refactor * Add test for --weak-compilation-flags * Fix fixtures * tmp fix, will be fixed in pr with checker/inwer
1 parent 31aef36 commit dfcdfd6

File tree

11 files changed

+81
-13
lines changed

11 files changed

+81
-13
lines changed

src/sinol_make/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from sinol_make import util
55

6-
__version__ = "1.1.1"
6+
__version__ = "1.1.2"
77

88
def configure_parsers():
99
parser = argparse.ArgumentParser(

src/sinol_make/commands/run/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ def configure_subparser(self, subparser):
5757
help='Python interpreter to use (default: python3)')
5858
parser.add_argument('--java_compiler_path', type=str, default=compiler.get_java_compiler_path(),
5959
help='Java compiler to use (default: javac)')
60+
parser.add_argument('--weak_compilation_flags', dest='weak_compilation_flags', action='store_true',
61+
help='use weaker compilation flags')
6062
parser.add_argument('--apply_suggestions', dest='apply_suggestions', action='store_true',
6163
help='apply suggestions from expected scores report')
6264

@@ -191,8 +193,10 @@ def compile(self, solution):
191193
self.COMPILATION_DIR, "%s.compile_log" % self.extract_file_name(solution))
192194
source_file = os.path.join(os.getcwd(), "prog", self.get_solution_from_exe(solution))
193195
output = os.path.join(self.EXECUTABLES_DIR, self.get_executable(solution))
196+
194197
try:
195-
compile.compile(source_file, output, self.compilers, open(compile_log_file, "w"))
198+
compile.compile(source_file, output, self.compilers,
199+
open(compile_log_file, "w"), self.args.weak_compilation_flags)
196200
print(util.info("Compilation of file %s was successful."
197201
% self.extract_file_name(solution)))
198202
return True

src/sinol_make/helpers/compile.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22
from sinol_make.interfaces.Errors import CompilationError
33
import os, subprocess, sys
44

5-
def compile(program, output, compilers = None, compile_log = None):
5+
def compile(program, output, compilers = None, compile_log = None, weak_compilation_flags = False):
66
"""
77
Compile a program
88
compilers - A dictionary of compilers to use. If not set, the default compilers will be used
99
"""
10+
gcc_compilation_flags = '-Werror -Wall -Wextra -Wshadow -Wconversion -Wno-unused-result -Wfloat-equal'
11+
if weak_compilation_flags:
12+
gcc_compilation_flags = '-w' # Disable all warnings
13+
1014
ext = os.path.splitext(program)[1]
1115
arguments = []
1216
if ext == '.cpp':
1317
arguments = [compilers['cpp_compiler_path'] or compiler.get_cpp_compiler_path(), program, '-o', output] + \
14-
'--std=c++17 -O3 -lm -Werror -Wall -Wextra -Wshadow -Wconversion -Wno-unused-result -Wfloat-equal'.split(' ')
18+
f'--std=c++17 -O3 -lm {gcc_compilation_flags} -fdiagnostics-color'.split(' ')
1519
elif ext == '.c':
1620
arguments = [compilers['c_compiler_path'] or compiler.get_c_compiler_path(), program, '-o', output] + \
17-
'--std=c17 -O3 -lm -Werror -Wall -Wextra -Wshadow -Wconversion -Wno-unused-result -Wfloat-equal'.split(' ')
21+
f'--std=c17 -O3 -lm {gcc_compilation_flags} -fdiagnostics-color'.split(' ')
1822
elif ext == '.py':
1923
if sys.platform == 'win32' or sys.platform == 'cygwin':
2024
# TODO: Make this work on Windows

tests/commands/run/test_integration.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from sinol_make import configure_parsers
66

77

8-
@pytest.mark.parametrize("create_package", [get_simple_package_path(), get_verify_status_package_path()], indirect=True)
8+
@pytest.mark.parametrize("create_package", [get_simple_package_path()], indirect=True)
99
def test_simple(create_package, time_tool):
1010
"""
1111
Test a simple run.
@@ -21,7 +21,7 @@ def test_simple(create_package, time_tool):
2121
command.run(args)
2222

2323

24-
@pytest.mark.parametrize("create_package", [get_simple_package_path(), get_verify_status_package_path()], indirect=True)
24+
@pytest.mark.parametrize("create_package", [get_simple_package_path()], indirect=True)
2525
def test_no_expected_scores(capsys, create_package, time_tool):
2626
"""
2727
Test with no sinol_expected_scores in config.yml.
@@ -51,7 +51,7 @@ def test_no_expected_scores(capsys, create_package, time_tool):
5151
assert "abc.cpp" in out
5252

5353

54-
@pytest.mark.parametrize("create_package", [get_simple_package_path(), get_verify_status_package_path()], indirect=True)
54+
@pytest.mark.parametrize("create_package", [get_simple_package_path()], indirect=True)
5555
def test_apply_suggestions(create_package, time_tool):
5656
"""
5757
Test with no sinol_expected_scores in config.yml.
@@ -143,3 +143,23 @@ def test_flag_solutions(capsys, create_package, time_tool):
143143
assert "abc1.cpp" in out
144144
assert "abc2.cpp" in out
145145
assert "abc3.cpp" not in out
146+
147+
148+
@pytest.mark.parametrize("create_package", [get_weak_compilation_flags_package_path()], indirect=True)
149+
def test_weak_compilation_flags(create_package):
150+
"""
151+
Test flag --weak-compilation-flags.
152+
"""
153+
parser = configure_parsers()
154+
args = parser.parse_args(["run", "--time_tool", "time"])
155+
command = Command()
156+
157+
with pytest.raises(SystemExit) as e:
158+
command.run(args)
159+
160+
assert e.type == SystemExit
161+
assert e.value.code == 1
162+
163+
args = parser.parse_args(["run", "--weak_compilation_flags", "--time_tool", "time"])
164+
command = Command()
165+
command.run(args)

tests/commands/run/test_unit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def test_get_tests(create_package):
7272
def test_execution(create_package, time_tool):
7373
package_path = create_package
7474
command = get_command(package_path)
75-
command.args = argparse.Namespace(time_tool = time_tool)
75+
command.args = argparse.Namespace(time_tool=time_tool, weak_compilation_flags=False)
7676
solution = "abc.cpp"
7777
executable = command.get_executable(solution)
7878
result = command.compile_solutions([solution])
@@ -105,7 +105,7 @@ def test_calculate_points():
105105
def test_run_solutions(create_package, time_tool):
106106
package_path = create_package
107107
command = get_command(package_path)
108-
command.args = argparse.Namespace(solutions_report=False, time_tool=time_tool)
108+
command.args = argparse.Namespace(solutions_report=False, time_tool=time_tool, weak_compilation_flags=False)
109109
create_ins_outs(package_path, command)
110110
command.tests = command.get_tests(None)
111111
command.groups = list(sorted(set([command.get_group(test) for test in command.tests])))

tests/commands/run/util.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import multiprocessing as mp
22
import os, glob
33

4+
import argparse
45
import yaml
56
from ...util import *
67
from sinol_make.commands.run import Command
@@ -23,6 +24,7 @@ def get_command(path = None):
2324
'java_compiler_path': compiler.get_java_compiler_path()
2425
}
2526
command.config = yaml.load(open(os.path.join(path, "config.yml"), "r"), Loader=yaml.FullLoader)
27+
set_default_args(command)
2628
return command
2729

2830
def create_ins(package_path, command):
@@ -47,3 +49,9 @@ def create_outs(package_path, command):
4749
def create_ins_outs(package_path, command):
4850
create_ins(package_path, command)
4951
create_outs(package_path, command)
52+
53+
54+
def set_default_args(command):
55+
command.args = argparse.Namespace(
56+
weak_compilation_flags=False
57+
)

tests/fixtures.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33

44

55
@pytest.fixture
6-
def create_package(path=None):
7-
if path is None:
6+
def create_package(request):
7+
"""
8+
Fixture to create a temporary directory with specified package (by default simple package).
9+
Changes the current working directory to the package directory.
10+
"""
11+
if hasattr(request, 'param') and request.param is not None:
12+
path = request.param
13+
else:
814
path = get_simple_package_path()
15+
task_id = os.path.basename(path)
16+
917
tmpdir = tempfile.TemporaryDirectory()
10-
package_path = os.path.join(tmpdir.name, "abc")
18+
package_path = os.path.join(tmpdir.name, task_id)
1119
shutil.copytree(path, package_path)
1220
os.chdir(package_path)
1321

tests/packages/wcf/config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
title: Package for testing --weak-compilation-flags
2+
memory_limit: 16000
3+
time_limit: 1000
4+
5+
scores:
6+
1: 100

tests/packages/wcf/in/.gitkeep

Whitespace-only changes.

tests/packages/wcf/prog/wcf.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int main() {
6+
int a, b;
7+
int c; // Unused variable
8+
9+
cin >> a >> b;
10+
cout << a + b << endl;
11+
}

0 commit comments

Comments
 (0)