Skip to content

Commit 3d44ce6

Browse files
authored
feat: add alternatives for gcc and g++ (#805)
* feat: add alternatives for gcc and g++ Adding gcc and g++ alternatives pointing to the correct version of gcc prevents overwriting gcc and g++ by installers of the test environment. In the process switch to --slave options for g++/c++. * test: add additonal test to check arm gcc version * chore: speed-up test execution * chore: add more checks on tool versions * chore: more speedup! * chore: refactor version tests * chore: add more tests and tags --------- Signed-off-by: Ron <[email protected]>
1 parent 34204af commit 3d44ce6

File tree

5 files changed

+128
-33
lines changed

5 files changed

+128
-33
lines changed

.devcontainer/cpp/Dockerfile

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,15 @@ RUN --mount=type=cache,target=/root/.ccache,sharing=locked \
9595

9696
# Update all tool alternatives to the correct version
9797
# and patch root's bashrc to include bash-completion
98-
RUN update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++-14 20 \
99-
&& update-alternatives --install /usr/bin/cc cc /usr/bin/gcc-14 20 \
100-
&& update-alternatives --install /usr/bin/gcov gcov /usr/bin/gcov-14 10 \
98+
RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 10 \
99+
--slave /usr/bin/g++ g++ /usr/bin/g++-14 \
100+
--slave /usr/bin/gcov gcov /usr/bin/gcov-14 \
101+
&& update-alternatives --install /usr/bin/cc cc /usr/bin/gcc-14 10 \
102+
--slave /usr/bin/c++ c++ /usr/bin/g++-14 \
101103
&& update-alternatives --install /usr/bin/iwyu iwyu /usr/local/bin/include-what-you-use 10 \
102104
&& update-alternatives --install /usr/bin/mull-runner mull-runner /usr/bin/mull-runner-${CLANG_VERSION} 10 \
103-
&& update-alternatives --install /usr/bin/mull-reporter mull-reporter /usr/bin/mull-reporter-${CLANG_VERSION} 10 \
104-
&& update-alternatives --install /usr/lib/mull-ir-frontend mull-ir-frontend /usr/lib/mull-ir-frontend-${CLANG_VERSION} 10 \
105+
--slave /usr/bin/mull-reporter mull-reporter /usr/bin/mull-reporter-${CLANG_VERSION} \
106+
--slave /usr/lib/mull-ir-frontend mull-ir-frontend /usr/lib/mull-ir-frontend-${CLANG_VERSION} \
105107
&& update-alternatives --install /usr/bin/python python /usr/bin/python3 10 \
106108
&& cp /etc/skel/.bashrc /root/.bashrc
107109

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
- name: Run Tests
4444
run: |
4545
set -Eeuo pipefail
46-
docker run --rm --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock --mount type=bind,src="${{ github.workspace }}/test/${{ matrix.flavor }}",dst=/ws -w /ws ${{ github.repository }}-${{ matrix.flavor }}:test bats --formatter junit integration-tests.bats | tee test-report-${{ matrix.flavor }}-${{ matrix.runner }}.xml
46+
docker run --rm --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock --mount type=bind,src="${{ github.workspace }}",dst=/ws -w /ws ${{ github.repository }}-${{ matrix.flavor }}:test bats --formatter junit test/${{ matrix.flavor }}/integration-tests.bats | tee test-report-${{ matrix.flavor }}-${{ matrix.runner }}.xml
4747
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
4848
if: always()
4949
with:

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ include(CPM)
44

55
project(devcontainer-test "NONE")
66

7-
CPMAddPackage("gh:neonsoftware/cmake-bats@0.0.3")
7+
CPMAddPackage("gh:neonsoftware/cmake-bats#53562f5a9be517944fb8a2df3ea80f81f17e070c")
88

99
enable_testing()
1010
bats_discover_tests("${CMAKE_CURRENT_SOURCE_DIR}/test/$ENV{CONTAINER_FLAVOR}/integration-tests.bats")

test/cpp/integration-tests.bats

Lines changed: 118 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,7 @@
33
bats_require_minimum_version 1.5.0
44

55
setup_file() {
6-
# Installing the Windows SDK/CRT takes a long time.
7-
# When still valid, use the installation from cache.
8-
9-
xwin --accept-license --manifest-version 16 --cache-dir ${BATS_TEST_DIRNAME}/.xwin-hash list
10-
HASH_LIST_MANIFEST=$(sha256sum ${BATS_TEST_DIRNAME}/.xwin-hash/dl/manifest*.json | awk '{ print $1 }')
11-
HASH_CACHED_MANIFEST=
12-
13-
if [[ -d ${BATS_TEST_DIRNAME}/.xwin-cache/dl ]]; then
14-
HASH_CACHED_MANIFEST=$(sha256sum ${BATS_TEST_DIRNAME}/.xwin-cache/dl/manifest*.json | awk '{ print $1 }')
15-
fi
16-
17-
if [[ $HASH_LIST_MANIFEST != $HASH_CACHED_MANIFEST ]]; then
18-
xwin --accept-license --manifest-version 16 --cache-dir ${BATS_TEST_DIRNAME}/.xwin-cache splat --preserve-ms-arch-notation
19-
fi
20-
21-
ln -sf ${BATS_TEST_DIRNAME}/.xwin-cache/splat/ /winsdk
6+
install_win_sdk_when_ci_set
227
}
238

249
teardown_file() {
@@ -38,6 +23,55 @@ teardown() {
3823
popd
3924
}
4025

26+
## This section contains tests for version correctness and compatibility of the installed tools.
27+
# Comparing the versions of the installed tools with the expected versions and ensuring
28+
# that the tools are compatible with each other. E.g. that the host and embedded toolchains
29+
# are aligned in terms of major and minor versions.
30+
31+
# bats test_tags=Compatibility,Version,Clang
32+
@test "clang toolchain versions should be aligned with expected versions" {
33+
EXPECTED_VERSION=$(get_expected_semver_for clang)
34+
35+
for TOOL in clang clang++ clang-cl clang-format clang-tidy; do
36+
INSTALLED_VERSION=$($TOOL --version | to_semver)
37+
assert_equal_print $EXPECTED_VERSION $INSTALLED_VERSION "Tool '${TOOL}' version"
38+
done
39+
}
40+
41+
# bats test_tags=Compatibility,Version,GCC
42+
@test "host gcc toolchain versions and alternatives should be aligned with expected versions" {
43+
EXPECTED_VERSION=$(get_expected_semver_for g++)
44+
45+
for TOOL in cc gcc c++ g++ gcov; do
46+
INSTALLED_VERSION=$($TOOL --version | to_semver)
47+
assert_equal_print $EXPECTED_VERSION $INSTALLED_VERSION "Tool '${TOOL}' version"
48+
done
49+
}
50+
51+
# bats test_tags=Compatibility,Version,HostGCCArmGCC
52+
@test "host and embedded gcc toolchain versions should be the same major and minor version" {
53+
EXPECTED_MAJOR_MINOR_VERSION=$(get_expected_semver_for g++ | cut -d. -f1,2)
54+
INSTALLED_MAJOR_MINOR_VERSION=$(arm-none-eabi-gcc -dumpfullversion | cut -d. -f1,2)
55+
assert_equal_print $EXPECTED_MAJOR_MINOR_VERSION $INSTALLED_MAJOR_MINOR_VERSION "Host and ARM GCC major and minor version"
56+
}
57+
58+
# bats test_tags=Compatibility,Version,Tools
59+
@test "supporting tool versions should be aligned with expected versions" {
60+
for TOOL in gdb gdb-multiarch git ninja; do
61+
EXPECTED_VERSION=$(get_expected_semver_for ${TOOL})
62+
INSTALLED_VERSION=$(${TOOL} --version | to_semver)
63+
64+
assert_equal_print $EXPECTED_VERSION $INSTALLED_VERSION "Tool '${TOOL}' version"
65+
done
66+
67+
for TOOL in cmake conan; do
68+
EXPECTED_VERSION=$(cat ${BATS_TEST_DIRNAME}/../../.devcontainer/cpp/requirements.in | grep ${TOOL} | to_semver)
69+
INSTALLED_VERSION=$(${TOOL} --version | to_semver)
70+
71+
assert_equal_print $EXPECTED_VERSION $INSTALLED_VERSION "Tool '${TOOL}' version"
72+
done
73+
}
74+
4175
@test "valid code input should result in working executable using host compiler" {
4276
cmake --preset gcc
4377
cmake --build --preset gcc
@@ -57,6 +91,8 @@ teardown() {
5791
}
5892

5993
@test "valid code input should result in Windows executable using clang-cl compiler" {
94+
install_win_sdk_when_ci_unset
95+
6096
cmake --preset clang-cl
6197
cmake --build --preset clang-cl
6298
}
@@ -79,6 +115,8 @@ teardown() {
79115
}
80116

81117
@test "using ccache as a compiler launcher should result in cached build using clang-cl compiler" {
118+
install_win_sdk_when_ci_unset
119+
82120
configure_and_build_with_ccache clang-cl
83121
}
84122

@@ -134,14 +172,6 @@ teardown() {
134172
assert_output --partial "[info] Mutation score:"
135173
}
136174

137-
@test "host gdb should be able to start" {
138-
gdb --version
139-
}
140-
141-
@test "gdb-multiarch should be able to start" {
142-
gdb-multiarch --version
143-
}
144-
145175
@test "clangd should be able to analyze source files" {
146176
run clangd --check=gcc/main.cpp
147177
assert_success
@@ -229,3 +259,67 @@ function build_and_run_with_sanitizers() {
229259
assert_failure
230260
assert_output --partial "ThreadSanitizer: data race"
231261
}
262+
263+
function to_semver() {
264+
grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+' | head -n1
265+
}
266+
267+
function get_expected_version_for() {
268+
local TOOL=${1:?}
269+
270+
jq -sr ".[0] * .[1] | to_entries[] | select(.key | startswith(\"${TOOL}\")) | .value | sub(\"-.*\"; \"\")" \
271+
${BATS_TEST_DIRNAME}/../../.devcontainer/cpp/apt-requirements-base.json \
272+
${BATS_TEST_DIRNAME}/../../.devcontainer/cpp/apt-requirements-clang.json
273+
}
274+
275+
function get_expected_semver_for() {
276+
local TOOL=${1:?}
277+
278+
get_expected_version_for ${TOOL} | to_semver
279+
}
280+
281+
function install_win_sdk() {
282+
# Installing the Windows SDK/CRT takes a long time.
283+
# When still valid, use the installation from cache.
284+
285+
xwin --accept-license --manifest-version 16 --cache-dir ${BATS_TEST_DIRNAME}/.xwin-hash list
286+
local HASH_LIST_MANIFEST=$(sha256sum ${BATS_TEST_DIRNAME}/.xwin-hash/dl/manifest*.json | awk '{ print $1 }')
287+
local HASH_CACHED_MANIFEST=
288+
289+
if [[ -d ${BATS_TEST_DIRNAME}/.xwin-cache/dl ]]; then
290+
HASH_CACHED_MANIFEST=$(sha256sum ${BATS_TEST_DIRNAME}/.xwin-cache/dl/manifest*.json | awk '{ print $1 }')
291+
fi
292+
293+
if [[ $HASH_LIST_MANIFEST != $HASH_CACHED_MANIFEST ]]; then
294+
xwin --accept-license --manifest-version 16 --cache-dir ${BATS_TEST_DIRNAME}/.xwin-cache splat --preserve-ms-arch-notation
295+
fi
296+
297+
ln -sf ${BATS_TEST_DIRNAME}/.xwin-cache/splat/ /winsdk
298+
}
299+
300+
function install_win_sdk_when_ci_unset() {
301+
# When running tests locally we typically run them one by one,
302+
# and want to install the Win SDK only for each test that requires it.
303+
304+
if [[ -z "${CI}" ]]; then
305+
install_win_sdk
306+
fi
307+
}
308+
309+
function install_win_sdk_when_ci_set() {
310+
# When running on a CI environment we run all tests in a single batch,
311+
# and only want to install the Win SKD once.
312+
313+
if [[ -n "${CI}" ]]; then
314+
install_win_sdk
315+
fi
316+
}
317+
318+
function assert_equal_print() {
319+
local EXPECTED=${1:?}
320+
local ACTUAL=${2:?}
321+
local MESSAGE=${3:-"Expecting values to be equal"}
322+
323+
echo "# ${MESSAGE} expected(${EXPECTED}) actual(${ACTUAL})" >&3
324+
assert_equal ${ACTUAL} ${EXPECTED}
325+
}

test/cpp/workspace/test/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ option(ENABLE_MUTATION_TESTING_TEST Off)
44
include(FetchContent)
55

66
FetchContent_Declare(googletest
7-
GIT_REPOSITORY https://github.com/google/googletest
8-
GIT_TAG v1.15.2
7+
URL https://github.com/google/googletest/releases/download/v1.17.0/googletest-1.17.0.tar.gz
98
)
109

1110
if (ENABLE_COVERAGE_TEST)

0 commit comments

Comments
 (0)