Skip to content

Commit

Permalink
Merge pull request #79 from codecrafters-io/CC-1222-cpp-upgrade
Browse files Browse the repository at this point in the history
CC-1222: update cpp version, and add vcpkg support for dependency resolution
  • Loading branch information
ryan-gang authored Jun 6, 2024
2 parents b002802 + e6185e7 commit d9d1fb7
Show file tree
Hide file tree
Showing 20 changed files with 140 additions and 24 deletions.
5 changes: 4 additions & 1 deletion compiled_starters/cpp/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
_deps

build/
vcpkg_installed
6 changes: 4 additions & 2 deletions compiled_starters/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
cmake_minimum_required(VERSION 3.13)

project(grep-starter-cpp)
set(CMAKE_CXX_STANDARD 20) # Enable the C++20 standard

file(GLOB_RECURSE SOURCE_FILES src/*.cpp)
set(CMAKE_CXX_STANDARD 23) # Enable the C++23 standard

file(GLOB_RECURSE SOURCE_FILES src/*.cpp src/*.hpp)

add_executable(server ${SOURCE_FILES})
4 changes: 2 additions & 2 deletions compiled_starters/cpp/codecrafters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ debug: false
# Use this to change the C++ version used to run your code
# on Codecrafters.
#
# Available versions: cpp-20
language_pack: cpp-20
# Available versions: cpp-23
language_pack: cpp-23
14 changes: 14 additions & 0 deletions compiled_starters/cpp/vcpkg-configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"default-registry": {
"kind": "git",
"baseline": "c4af3593e1f1aa9e14a560a09e45ea2cb0dfd74d",
"repository": "https://github.com/microsoft/vcpkg"
},
"registries": [
{
"kind": "artifact",
"location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip",
"name": "microsoft"
}
]
}
3 changes: 3 additions & 0 deletions compiled_starters/cpp/vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dependencies": []
}
7 changes: 4 additions & 3 deletions compiled_starters/cpp/your_grep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#
# DON'T EDIT THIS!
set -e
cmake $(dirname $0) > /dev/null
make > /dev/null
exec ./server "$@"
# vcpkg & cmake are required.
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
cmake --build ./build
exec ./build/server "$@"
43 changes: 43 additions & 0 deletions dockerfiles/cpp-23.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# syntax=docker/dockerfile:1.7-labs
FROM gcc:13.2.0-bookworm

ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="vcpkg.json,vcpkg-configuration.json"

RUN apt-get update && \
apt-get install --no-install-recommends -y zip=3.* && \
apt-get install --no-install-recommends -y g++=4:* && \
apt-get install --no-install-recommends -y build-essential=12.* && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# cmake 3.29.2 is required by vcpkg
RUN wget --progress=dot:giga https://github.com/Kitware/CMake/releases/download/v3.29.2/cmake-3.29.2-Linux-x86_64.tar.gz && \
tar -xzvf cmake-3.29.2-Linux-x86_64.tar.gz && \
mv cmake-3.29.2-linux-x86_64/ /cmake

ENV CMAKE_BIN="/cmake/bin"
ENV PATH="${CMAKE_BIN}:$PATH"

RUN git clone https://github.com/microsoft/vcpkg.git && \
./vcpkg/bootstrap-vcpkg.sh -disableMetrics

ENV VCPKG_ROOT="/vcpkg"
ENV PATH="${VCPKG_ROOT}:$PATH"

WORKDIR /app

# .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses
COPY --exclude=.git --exclude=README.md . /app

RUN vcpkg install --no-print-usage
RUN sed -i '1s/^/set(VCPKG_INSTALL_OPTIONS --no-print-usage)\n/' ${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake

RUN mkdir -p /app-cached/build
RUN if [ -d "/app/build" ]; then mv /app/build /app-cached; fi
RUN if [ -d "/app/vcpkg_installed" ]; then mv /app/vcpkg_installed /app-cached/build; fi

RUN echo "cd \${CODECRAFTERS_SUBMISSION_DIR} && cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake && cmake --build ./build && sed -i '/^cmake/ s/^/# /' ./your_grep.sh" > /codecrafters-precompile.sh
RUN chmod +x /codecrafters-precompile.sh

# Once the heavy steps are done, we can copy all files back
COPY . /app
5 changes: 4 additions & 1 deletion solutions/cpp/01-cq2/code/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
_deps

build/
vcpkg_installed
6 changes: 4 additions & 2 deletions solutions/cpp/01-cq2/code/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
cmake_minimum_required(VERSION 3.13)

project(grep-starter-cpp)
set(CMAKE_CXX_STANDARD 20) # Enable the C++20 standard

file(GLOB_RECURSE SOURCE_FILES src/*.cpp)
set(CMAKE_CXX_STANDARD 23) # Enable the C++23 standard

file(GLOB_RECURSE SOURCE_FILES src/*.cpp src/*.hpp)

add_executable(server ${SOURCE_FILES})
4 changes: 2 additions & 2 deletions solutions/cpp/01-cq2/code/codecrafters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ debug: false
# Use this to change the C++ version used to run your code
# on Codecrafters.
#
# Available versions: cpp-20
language_pack: cpp-20
# Available versions: cpp-23
language_pack: cpp-23
14 changes: 14 additions & 0 deletions solutions/cpp/01-cq2/code/vcpkg-configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"default-registry": {
"kind": "git",
"baseline": "c4af3593e1f1aa9e14a560a09e45ea2cb0dfd74d",
"repository": "https://github.com/microsoft/vcpkg"
},
"registries": [
{
"kind": "artifact",
"location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip",
"name": "microsoft"
}
]
}
3 changes: 3 additions & 0 deletions solutions/cpp/01-cq2/code/vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dependencies": []
}
7 changes: 4 additions & 3 deletions solutions/cpp/01-cq2/code/your_grep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#
# DON'T EDIT THIS!
set -e
cmake $(dirname $0) > /dev/null
make > /dev/null
exec ./server "$@"
# vcpkg & cmake are required.
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
cmake --build ./build
exec ./build/server "$@"
4 changes: 4 additions & 0 deletions starter-repository-definitions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@
target: src/Server.cpp
- source: starter_templates/cpp/CMakeLists.txt
target: CMakeLists.txt
- source: starter_templates/cpp/vcpkg.json
target: vcpkg.json
- source: starter_templates/cpp/vcpkg-configuration.json
target: vcpkg-configuration.json
- source: starter_templates/cpp/your_grep.sh
target: your_grep.sh
- source: starter_templates/.gitattributes
Expand Down
4 changes: 2 additions & 2 deletions starter_templates/codecrafters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ language_pack: clojure-1.10.3
language_pack: dotnet-8.0
{{/language_is_csharp}}
{{#language_is_cpp}}
# Available versions: cpp-20
language_pack: cpp-20
# Available versions: cpp-23
language_pack: cpp-23
{{/language_is_cpp}}
{{#language_is_typescript}}
# Available versions: bun-1.1
Expand Down
5 changes: 4 additions & 1 deletion starter_templates/cpp/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
_deps

build/
vcpkg_installed
6 changes: 4 additions & 2 deletions starter_templates/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
cmake_minimum_required(VERSION 3.13)

project(grep-starter-cpp)
set(CMAKE_CXX_STANDARD 20) # Enable the C++20 standard

file(GLOB_RECURSE SOURCE_FILES src/*.cpp)
set(CMAKE_CXX_STANDARD 23) # Enable the C++23 standard

file(GLOB_RECURSE SOURCE_FILES src/*.cpp src/*.hpp)

add_executable(server ${SOURCE_FILES})
14 changes: 14 additions & 0 deletions starter_templates/cpp/vcpkg-configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"default-registry": {
"kind": "git",
"baseline": "c4af3593e1f1aa9e14a560a09e45ea2cb0dfd74d",
"repository": "https://github.com/microsoft/vcpkg"
},
"registries": [
{
"kind": "artifact",
"location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip",
"name": "microsoft"
}
]
}
3 changes: 3 additions & 0 deletions starter_templates/cpp/vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dependencies": []
}
7 changes: 4 additions & 3 deletions starter_templates/cpp/your_grep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#
# DON'T EDIT THIS!
set -e
cmake $(dirname $0) > /dev/null
make > /dev/null
exec ./server "$@"
# vcpkg & cmake are required.
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
cmake --build ./build
exec ./build/server "$@"

0 comments on commit d9d1fb7

Please sign in to comment.