Skip to content
This repository was archived by the owner on Nov 30, 2023. It is now read-only.

Commit 54ca3a2

Browse files
dnastrainbderusha
andauthored
Adding vcpkg to cpp image definitions (#1310)
Co-authored-by: Bill DeRusha <[email protected]>
1 parent d3f94e5 commit 54ca3a2

File tree

8 files changed

+222
-3
lines changed

8 files changed

+222
-3
lines changed

containers/cpp/.devcontainer/Dockerfile

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22
ARG VARIANT=debian-11
33
FROM mcr.microsoft.com/vscode/devcontainers/cpp:0-${VARIANT}
44

5+
# [Optional] Install CMake version different from what base image has already installed.
6+
# CMake reinstall choices: none, 3.21.5, 3.22.2, or versions from https://cmake.org/download/
7+
ARG REINSTALL_CMAKE_VERSION_FROM_SOURCE="none"
8+
9+
# Optionally install the cmake for vcpkg
10+
COPY ./reinstall-cmake.sh /tmp/
11+
RUN if [ "${REINSTALL_CMAKE_VERSION_FROM_SOURCE}" != "none" ]; then \
12+
/tmp/reinstall-cmake.sh ${REINSTALL_CMAKE_VERSION_FROM_SOURCE}; \
13+
fi \
14+
&& rm -f /tmp/reinstall-cmake.sh
15+
16+
# [Optional] Uncomment this section to install additional vcpkg ports.
17+
# RUN su vscode -c "${VCPKG_ROOT}/vcpkg install <your-port-name-here>"
18+
519
# [Optional] Uncomment this section to install additional packages.
620
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
721
# && apt-get -y install --no-install-recommends <your-package-list-here>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env bash
2+
#-------------------------------------------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
5+
#-------------------------------------------------------------------------------------------------------------
6+
#
7+
set -e
8+
9+
USERNAME=${1:-"vscode"}
10+
11+
# bionic and stretch pkg repos install cmake version < 3.15 which is required to run bootstrap-vcpkg.sh on ARM64
12+
VCPKG_UNSUPPORTED_ARM64_VERSION_CODENAMES="stretch bionic"
13+
14+
. /etc/os-release
15+
16+
# Exit early if ARM64 OS does not have cmake version required to build Vcpkg
17+
if [ "$(dpkg --print-architecture)" = "arm64" ] && [[ "${VCPKG_UNSUPPORTED_ARM64_VERSION_CODENAMES}" = *"${VERSION_CODENAME}"* ]]; then
18+
echo "OS ${VERSION_CODENAME} ARM64 pkg repo installs cmake version < 3.15, which is required to build Vcpkg."
19+
exit 0
20+
fi
21+
22+
# Add to bashrc/zshrc files for all users.
23+
updaterc() {
24+
echo "Updating /etc/bash.bashrc and /etc/zsh/zshrc..."
25+
if [[ "$(cat /etc/bash.bashrc)" != *"$1"* ]]; then
26+
echo -e "$1" >> /etc/bash.bashrc
27+
fi
28+
if [ -f "/etc/zsh/zshrc" ] && [[ "$(cat /etc/zsh/zshrc)" != *"$1"* ]]; then
29+
echo -e "$1" >> /etc/zsh/zshrc
30+
fi
31+
}
32+
33+
# Run apt-get if needed.
34+
apt_get_update_if_needed() {
35+
if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then
36+
echo "Running apt-get update..."
37+
apt-get update
38+
else
39+
echo "Skipping apt-get update."
40+
fi
41+
}
42+
43+
# Check if packages are installed and installs them if not.
44+
check_packages() {
45+
if ! dpkg -s "$@" > /dev/null 2>&1; then
46+
apt_get_update_if_needed
47+
apt-get -y install --no-install-recommends "$@"
48+
fi
49+
}
50+
51+
export DEBIAN_FRONTEND=noninteractive
52+
53+
# Install additional packages needed by vcpkg: https://github.com/microsoft/vcpkg/blob/master/README.md#installing-linux-developer-tools
54+
check_packages build-essential tar curl zip unzip pkg-config bash-completion ninja-build git
55+
56+
# Setup group and add user
57+
umask 0002
58+
if ! cat /etc/group | grep -e "^vcpkg:" > /dev/null 2>&1; then
59+
groupadd -r "vcpkg"
60+
fi
61+
usermod -a -G "vcpkg" "${USERNAME}"
62+
63+
# Start Installation
64+
# Clone repository with ports and installer
65+
mkdir -p "${VCPKG_ROOT}"
66+
mkdir -p "${VCPKG_DOWNLOADS}"
67+
git clone --depth=1 \
68+
-c core.eol=lf \
69+
-c core.autocrlf=false \
70+
-c fsck.zeroPaddedFilemode=ignore \
71+
-c fetch.fsck.zeroPaddedFilemode=ignore \
72+
-c receive.fsck.zeroPaddedFilemode=ignore \
73+
https://github.com/microsoft/vcpkg "${VCPKG_ROOT}"
74+
75+
## Run installer to get latest stable vcpkg binary
76+
## https://github.com/microsoft/vcpkg/blob/7e7dad5fe20cdc085731343e0e197a7ae655555b/scripts/bootstrap.sh#L126-L144
77+
"${VCPKG_ROOT}"/bootstrap-vcpkg.sh
78+
79+
# Add vcpkg to PATH
80+
updaterc "$(cat << EOF
81+
export VCPKG_ROOT="${VCPKG_ROOT}"
82+
if [[ "\${PATH}" != *"\${VCPKG_ROOT}"* ]]; then export PATH="\${PATH}:\${VCPKG_ROOT}"; fi
83+
EOF
84+
)"
85+
86+
# Give read/write permissions to the user group.
87+
chown -R ":vcpkg" "${VCPKG_ROOT}" "${VCPKG_DOWNLOADS}"
88+
chmod g+r+w+s "${VCPKG_ROOT}" "${VCPKG_DOWNLOADS}"
89+
chmod -R g+r+w "${VCPKG_ROOT}" "${VCPKG_DOWNLOADS}"
90+
91+
# Enable tab completion for bash and zsh
92+
VCPKG_FORCE_SYSTEM_BINARIES=1 su "${USERNAME}" -c "${VCPKG_ROOT}/vcpkg integrate bash"
93+
VCPKG_FORCE_SYSTEM_BINARIES=1 su "${USERNAME}" -c "${VCPKG_ROOT}/vcpkg integrate zsh"

containers/cpp/.devcontainer/base.Dockerfile

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
88
&& apt-get -y install build-essential cmake cppcheck valgrind clang lldb llvm gdb \
99
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/*
1010

11+
# Setup ENV vars for vcpkg
12+
ENV VCPKG_ROOT=/usr/local/vcpkg \
13+
VCPKG_DOWNLOADS=/usr/local/vcpkg-downloads
14+
15+
ARG USERNAME=vscode
16+
17+
# Install vcpkg itself: https://github.com/microsoft/vcpkg/blob/master/README.md#quick-start-unix
18+
COPY base-scripts/install-vcpkg.sh /tmp/
19+
RUN /tmp/install-vcpkg.sh ${USERNAME} \
20+
&& rm -f /tmp/install-vcpkg.sh
21+
1122
# [Optional] Uncomment this section to install additional OS packages.
1223
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
1324
# && apt-get -y install --no-install-recommends <your-package-list-here>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env bash
2+
#-------------------------------------------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
5+
#-------------------------------------------------------------------------------------------------------------
6+
#
7+
set -e
8+
9+
CMAKE_VERSION=${1:-"none"}
10+
11+
if [ "${CMAKE_VERSION}" = "none" ]; then
12+
echo "No CMake version specified, skipping CMake reinstallation"
13+
exit 0
14+
fi
15+
16+
# Cleanup temporary directory and associated files when exiting the script.
17+
cleanup() {
18+
EXIT_CODE=$?
19+
set +e
20+
if [[ -n "${TMP_DIR}" ]]; then
21+
echo "Executing cleanup of tmp files"
22+
rm -Rf "${TMP_DIR}"
23+
fi
24+
exit $EXIT_CODE
25+
}
26+
trap cleanup EXIT
27+
28+
29+
echo "Installing CMake..."
30+
apt-get -y purge --auto-remove cmake
31+
mkdir -p /opt/cmake
32+
33+
architecture=$(dpkg --print-architecture)
34+
case "${architecture}" in
35+
arm64)
36+
ARCH=aarch64 ;;
37+
amd64)
38+
ARCH=x86_64 ;;
39+
*)
40+
echo "Unsupported architecture ${architecture}."
41+
exit 1
42+
;;
43+
esac
44+
45+
CMAKE_BINARY_NAME="cmake-${CMAKE_VERSION}-linux-${ARCH}.sh"
46+
CMAKE_CHECKSUM_NAME="cmake-${CMAKE_VERSION}-SHA-256.txt"
47+
TMP_DIR=$(mktemp -d -t cmake-XXXXXXXXXX)
48+
49+
echo "${TMP_DIR}"
50+
cd "${TMP_DIR}"
51+
52+
curl -sSL "https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/${CMAKE_BINARY_NAME}" -O
53+
curl -sSL "https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/${CMAKE_CHECKSUM_NAME}" -O
54+
55+
sha256sum -c --ignore-missing "${CMAKE_CHECKSUM_NAME}"
56+
sh "${TMP_DIR}/${CMAKE_BINARY_NAME}" --prefix=/opt/cmake --skip-license
57+
58+
ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake

containers/cpp/.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ test-project
33
history
44
definition-manifest.json
55
library-scripts
6+
base-scripts
67
.vscode
78
.npmignore

containers/cpp/README.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,30 @@ See [history](history) for information on the contents of each version and [here
5050

5151
Alternatively, you can use the contents of `base.Dockerfile` to fully customize your container's contents or to build it for a container host architecture not supported by the image.
5252

53-
Beyond `git`, this image / `Dockerfile` includes `zsh`, [Oh My Zsh!](https://ohmyz.sh/), a non-root `vscode` user with `sudo` access, and a set of common dependencies for development.
53+
Beyond `git`, this image / `Dockerfile` includes `zsh`, [Oh My Zsh!](https://ohmyz.sh/), a non-root `vscode` user with `sudo` access, a set of common dependencies for development, and [Vcpkg](https://github.com/microsoft/vcpkg) a cross-platform package manager for C++.
54+
55+
### Using Vcpkg
56+
This dev container and its associated image includes a clone of the [`Vcpkg`](https://github.com/microsoft/vcpkg) repo for library packages, and a bootstrapped instance of the [Vcpkg-tool](https://github.com/microsoft/vcpkg-tool) itself.
57+
58+
The minimum version of `cmake` required to install packages is higher than the version available in the main package repositories for Debian (<=11) and Ubuntu (<=21.10). `Vcpkg` will download a compatible version of `cmake` for its own use if that is the case (on x86_64 architectures), however you can opt to reinstall a different version of `cmake` globally by adding `"REINSTALL_CMAKE_VERSION_FROM_SOURCE: "<VERSION>"` to build args in `.devcontainer/devcontainer.json`. This will install `cmake` from its github releases. For example:
59+
60+
```json
61+
"args": {
62+
"VARIANT": "debian-11",
63+
"REINSTALL_CMAKE_VERSION_FROM_SOURCE": "3.21.5"
64+
}
65+
```
66+
67+
Most additional library packages installed using Vcpkg will be downloaded from their [official distribution locations](https://github.com/microsoft/vcpkg#security). To configure Vcpkg in this container to access an alternate registry, more information can be found here: [Registries: Bring your own libraries to vcpkg](https://devblogs.microsoft.com/cppblog/registries-bring-your-own-libraries-to-vcpkg/).
68+
69+
To update the available library packages, pull the latest from the git repository using the following command in the terminal:
70+
71+
```sh
72+
cd "${VCPKG_ROOT}"
73+
git pull --ff-only
74+
```
75+
76+
> Note: Please review the [Vcpkg license details](https://github.com/microsoft/vcpkg#license) to better understand its own license and additional license information pertaining to library packages and supported ports.
5477
5578
### Adding the definition to a project or codespace
5679

containers/cpp/definition-manifest.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,18 @@
6464
"clang",
6565
"lldb",
6666
"llvm",
67-
"gdb"
67+
"gdb",
68+
"tar",
69+
"curl",
70+
"zip",
71+
"unzip",
72+
"pkg-config",
73+
"bash-completion",
74+
"ninja-build"
6875
],
6976
"git": {
70-
"Oh My Zsh!": "/home/vscode/.oh-my-zsh"
77+
"Oh My Zsh!": "/home/vscode/.oh-my-zsh",
78+
"vcpkg": "/usr/local/vcpkg"
7179
},
7280
"languages": {
7381
"GCC": {

containers/cpp/test-project/test.sh

+11
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,20 @@ source test-utils.sh vscode
66
# Run common tests
77
checkCommon
88

9+
# Help determine distro
10+
. /etc/os-release
11+
912
# Run definition specific tests
1013
checkExtension "ms-vscode.cpptools"
1114
checkOSPackages "command-line-tools" build-essential cmake cppcheck valgrind clang lldb llvm gdb
15+
checkOSPackages "tools-for-vcpkg" tar curl zip unzip pkg-config bash-completion
16+
VCPKG_UNSUPPORTED_ARM64_VERSION_CODENAMES="stretch bionic"
17+
if [ "$(dpkg --print-architecture)" = "amd64" ] || [[ ! "${VCPKG_UNSUPPORTED_ARM64_VERSION_CODENAMES}" = *"${VERSION_CODENAME}"* ]]; then
18+
check "VCPKG_ROOT" [ -d "${VCPKG_ROOT}" ]
19+
check "VCPKG_DOWNLOAD" [ -d "${VCPKG_DOWNLOADS}" ]
20+
VCPKG_FORCE_SYSTEM_BINARIES=1 check "vcpkg-from-root" ${VCPKG_ROOT}/vcpkg --version
21+
VCPKG_FORCE_SYSTEM_BINARIES=1 check "vcpkg-from-bin" vcpkg --version
22+
fi
1223
check "g++" g++ -g main.cpp -o main.out
1324
rm main.out
1425
mkdir -p build

0 commit comments

Comments
 (0)