Skip to content
Open
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
11 changes: 10 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM alpine:3.24.3@sha256:be2656061b536c1883c4034790e21d0a09c5aaaf51968b89368a3c0371ea2c9a

RUN apk add --no-cache coreutils g++ libc-dev cmake make git boost-dev
RUN apk add --no-cache coreutils g++ libc-dev cmake make git boost-dev mold

# Build Catch as a library directly inside the Docker image
# since it takes a bit of time to compile it,this way we speed
Expand All @@ -12,6 +12,15 @@ RUN git clone https://github.com/catchorg/Catch2.git --depth 1 --branch v3.4.0 &
cd .. && \
rm -rf Catch2

# Precompile Catch2's catch_all.hpp, which every test file includes. Parsing it
# takes about a second per run, and loading the precompiled copy takes about
# 0.1 s. GCC only uses the precompiled copy when the compiler options match, so
# these are the options used by the exercises' CMakeLists.txt in Debug mode.
# -Wpedantic matters because it changes which macros GCC defines.
# bin/compile-with-pch.sh makes the test file include it first.
RUN c++ -std=c++17 -g -Wall -Wextra -Wpedantic -Werror -x c++-header \

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds very fragile. I guess there's a reason why pre-compiled headers are not exactly popular ...

/usr/local/include/catch2/catch_all.hpp -o /usr/local/include/catch2/catch_all.hpp.gch

WORKDIR /opt/test-runner
COPY . .
RUN g++ -I ./include ./src/*.cpp -o ./bin/exercism_parser -lboost_system -lboost_json
Expand Down
25 changes: 25 additions & 0 deletions bin/compile-with-pch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env sh

# Synopsis:
# Compile one file with the C++ compiler. CMake calls this script in place of
# the compiler (see CMAKE_CXX_COMPILER_LAUNCHER in run.sh) and passes the full
# compiler command as arguments.

# The test file includes Catch2's catch_all.hpp, which takes about a second to
# parse. The Docker image holds a precompiled copy of that header, built in
# the Dockerfile. GCC only uses a precompiled header if it is
# the first thing included, and the test file includes the solution first.
# So for the test file, this script includes Catch2 before anything else.
# The test file's own include of Catch2 then does nothing.
#
# The solution's files are compiled unchanged. If a precompiled header does not
# match the compiler options, GCC ignores it and parses the header as before.

# run.sh sets EXERCISM_TEST_FILE to the test file's path. CMake passes the
# source file as the last argument.
for last_arg; do :; done

if [ "${last_arg}" = "${EXERCISM_TEST_FILE}" ]; then
exec "$@" -include catch2/catch_all.hpp
fi
exec "$@"
50 changes: 47 additions & 3 deletions bin/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,56 @@ cp -R "${input_dir}/" "${build_dir}" && cd "${build_dir}"
# Replace the old Catch2 v2 include line with Catch2 v3
sed -i -e 's/#include <catch2\/catch.hpp>/#include <catch2\/catch_all.hpp>/g' "${test_file_path}"

cmake -DEXERCISM_TEST_SUITE=1 -DEXERCISM_RUN_ALL_TESTS=1 .
make 2> "${compilation_errors_file_name}"
parse_results() {
/opt/test-runner/bin/exercism_parser "${build_dir}/${test_output_file_name}" "${results_file}" "${build_dir}/${compilation_errors_file_name}" "${test_file_path}"
}

# The Docker image holds a precompiled copy of Catch2's header, which saves
# about a second on every compile of the test file. compile-with-pch.sh makes
# the test file use it. The browser runner has no precompiled header, so it
# compiles as before.
cmake_options="-DEXERCISM_TEST_SUITE=1 -DEXERCISM_RUN_ALL_TESTS=1"
if [ -e /usr/local/include/catch2/catch_all.hpp.gch ]; then
export EXERCISM_TEST_FILE="${test_file_path}"
cmake_options="${cmake_options} -DCMAKE_CXX_COMPILER_LAUNCHER=/opt/test-runner/bin/compile-with-pch.sh"
fi

# mold links the test executable in about 0.06 s, against about 0.3 s for
# GNU ld, because it uses all cores and does less work per symbol.
if command -v mold > /dev/null; then
cmake_options="${cmake_options} -DCMAKE_EXE_LINKER_FLAGS=-fuse-ld=mold"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be cleaner:

Suggested change
cmake_options="${cmake_options} -DCMAKE_EXE_LINKER_FLAGS=-fuse-ld=mold"
cmake_options="${cmake_options} -DCMAKE_LINKER_TYPE=MOLD"

In general, doing things in the CMakeLists.txt I would find preferable, since then everyone benefits from improvements, not just the Docker runner.

fi

cmake ${cmake_options} .

# Build only the test executable. The default target also runs the tests,
# so building it ran every test twice: once from make and once below to
# write the XML. Slow solutions took twice as long and timed out sooner.
# An exercise with no target named after the slug builds everything as before.
# The browser runner has no grep, so this uses sed.
built_target_only=false

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is known and something I found surprising when starting working on exercises and track maintenance. I think we could discuss if we'd want to change that. Normally, you build a project with a plain make. To run anything, you'd have to explicitly say so. Or, maybe even better, we could register the test as an actual CMake test and then use ctest. But again, that would require changing all the CMakeLists.txt files.

Tests are typically fast, so I never considered this an issue. But I do see that for slow solutions we pay too much. However, I don't see why it needs to be so complicated. Always doing

make "${slug}" 2> "${compilation_errors_file_name}"

followed by the line that produces the XML should work? All CMakeLists.txt are identical, so assuming that there is a target named ${slug} is save.

if [ -n "$(sed -n "/^${slug}:/p" Makefile)" ]; then
built_target_only=true
make "${slug}" 2> "${compilation_errors_file_name}"
else
make 2> "${compilation_errors_file_name}"
fi

# In case of compilation errors the executable will not be created
[ -f "./${slug}" ] && chmod +x "./${slug}" && "./${slug}" -r xml -o "${test_output_file_name}"
/opt/test-runner/bin/exercism_parser "${build_dir}/${test_output_file_name}" "${results_file}" "${build_dir}/${compilation_errors_file_name}" "${test_file_path}"
parse_results

# If the tests crashed or threw, the parser reports an error and shows the
# compilation errors file as the message. Run the default target as before,
# so that the message holds the same output from make as it did before.
if ${built_target_only} && [ -f "./${slug}" ]; then
case "$(cat "${results_file}")" in
*'"status": "error"'*)
make 2>> "${compilation_errors_file_name}"
parse_results
;;
esac
fi

cd -

Expand Down