diff --git a/Dockerfile b/Dockerfile index 401a488..8178b65 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 @@ -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 \ + /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 diff --git a/bin/compile-with-pch.sh b/bin/compile-with-pch.sh new file mode 100755 index 0000000..74281b3 --- /dev/null +++ b/bin/compile-with-pch.sh @@ -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 "$@" diff --git a/bin/run.sh b/bin/run.sh index b420940..1fe92cf 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -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 /#include /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" +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 +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 -