From 865fedb943a73e26954eb0379ee007c56b53b91a Mon Sep 17 00:00:00 2001 From: Andy Li <1450947+andy1li@users.noreply.github.com> Date: Thu, 20 Feb 2025 21:55:33 +0800 Subject: [PATCH] feat: Migrate C starter template to CMake and C23 This commit updates the C starter template to: - Switch from gcc to CMake for build system - Update to C23 language standard - Restructure project layout from `app/` to `src/` - Add vcpkg configuration for potential future dependencies - Update compilation and run scripts to use CMake --- compiled_starters/c/.codecrafters/compile.sh | 3 +- compiled_starters/c/.codecrafters/run.sh | 2 +- compiled_starters/c/.gitignore | 55 +++++++++++++++++++ compiled_starters/c/CMakeLists.txt | 9 +++ compiled_starters/c/README.md | 6 +- compiled_starters/c/codecrafters.yml | 4 +- .../c/{app/server.c => src/main.c} | 0 compiled_starters/c/vcpkg-configuration.json | 14 +++++ compiled_starters/c/vcpkg.json | 3 + compiled_starters/c/your_program.sh | 5 +- dockerfiles/c-23.Dockerfile | 38 +++++++++++++ .../c/01-at4/code/.codecrafters/compile.sh | 3 +- solutions/c/01-at4/code/.codecrafters/run.sh | 2 +- solutions/c/01-at4/code/.gitignore | 55 +++++++++++++++++++ solutions/c/01-at4/code/CMakeLists.txt | 9 +++ solutions/c/01-at4/code/README.md | 6 +- solutions/c/01-at4/code/codecrafters.yml | 4 +- .../01-at4/code/{app/server.c => src/main.c} | 0 .../c/01-at4/code/vcpkg-configuration.json | 14 +++++ solutions/c/01-at4/code/vcpkg.json | 3 + solutions/c/01-at4/code/your_program.sh | 5 +- .../{app/server.c.diff => src/main.c.diff} | 0 solutions/c/01-at4/explanation.md | 2 +- .../c/code/.codecrafters/compile.sh | 3 +- starter_templates/c/code/.codecrafters/run.sh | 2 +- starter_templates/c/code/.gitignore | 55 +++++++++++++++++++ starter_templates/c/code/CMakeLists.txt | 9 +++ .../c/code/{app/server.c => src/main.c} | 0 .../c/code/vcpkg-configuration.json | 14 +++++ starter_templates/c/code/vcpkg.json | 3 + starter_templates/c/config.yml | 4 +- 31 files changed, 309 insertions(+), 23 deletions(-) create mode 100644 compiled_starters/c/.gitignore create mode 100644 compiled_starters/c/CMakeLists.txt rename compiled_starters/c/{app/server.c => src/main.c} (100%) create mode 100644 compiled_starters/c/vcpkg-configuration.json create mode 100644 compiled_starters/c/vcpkg.json create mode 100644 dockerfiles/c-23.Dockerfile create mode 100644 solutions/c/01-at4/code/.gitignore create mode 100644 solutions/c/01-at4/code/CMakeLists.txt rename solutions/c/01-at4/code/{app/server.c => src/main.c} (100%) create mode 100644 solutions/c/01-at4/code/vcpkg-configuration.json create mode 100644 solutions/c/01-at4/code/vcpkg.json rename solutions/c/01-at4/diff/{app/server.c.diff => src/main.c.diff} (100%) create mode 100644 starter_templates/c/code/.gitignore create mode 100644 starter_templates/c/code/CMakeLists.txt rename starter_templates/c/code/{app/server.c => src/main.c} (100%) create mode 100644 starter_templates/c/code/vcpkg-configuration.json create mode 100644 starter_templates/c/code/vcpkg.json diff --git a/compiled_starters/c/.codecrafters/compile.sh b/compiled_starters/c/.codecrafters/compile.sh index ce0e69c..9da226b 100755 --- a/compiled_starters/c/.codecrafters/compile.sh +++ b/compiled_starters/c/.codecrafters/compile.sh @@ -8,4 +8,5 @@ set -e # Exit on failure -gcc -lcurl -lz -o /tmp/codecrafters-build-http-server-c app/*.c +cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake +cmake --build ./build diff --git a/compiled_starters/c/.codecrafters/run.sh b/compiled_starters/c/.codecrafters/run.sh index 5bed6d0..d433e95 100755 --- a/compiled_starters/c/.codecrafters/run.sh +++ b/compiled_starters/c/.codecrafters/run.sh @@ -8,4 +8,4 @@ set -e # Exit on failure -exec /tmp/codecrafters-build-http-server-c "$@" +exec $(dirname $0)/build/http-server "$@" diff --git a/compiled_starters/c/.gitignore b/compiled_starters/c/.gitignore new file mode 100644 index 0000000..6e9506c --- /dev/null +++ b/compiled_starters/c/.gitignore @@ -0,0 +1,55 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +build +vcpkg_installed \ No newline at end of file diff --git a/compiled_starters/c/CMakeLists.txt b/compiled_starters/c/CMakeLists.txt new file mode 100644 index 0000000..e4837f8 --- /dev/null +++ b/compiled_starters/c/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.13) + +project(codecrafters-http-server) + +file(GLOB_RECURSE SOURCE_FILES src/*.c src/*.h) + +set(CMAKE_C_STANDARD 23) # Enable the C23 standard + +add_executable(http-server ${SOURCE_FILES}) diff --git a/compiled_starters/c/README.md b/compiled_starters/c/README.md index 96793c7..9ffb007 100644 --- a/compiled_starters/c/README.md +++ b/compiled_starters/c/README.md @@ -16,7 +16,7 @@ and more. # Passing the first stage -The entry point for your HTTP server implementation is in `app/server.c`. Study +The entry point for your HTTP server implementation is in `src/main.c`. Study and uncomment the relevant code, and push your changes to pass the first stage: ```sh @@ -30,8 +30,8 @@ Time to move on to the next stage! Note: This section is for stages 2 and beyond. -1. Ensure you have `gcc` installed locally +1. Ensure you have `cmake` installed locally 1. Run `./your_program.sh` to run your program, which is implemented in - `app/server.c`. + `src/main.c`. 1. Commit your changes and run `git push origin master` to submit your solution to CodeCrafters. Test output will be streamed to your terminal. diff --git a/compiled_starters/c/codecrafters.yml b/compiled_starters/c/codecrafters.yml index 4de7cd9..51a9b70 100644 --- a/compiled_starters/c/codecrafters.yml +++ b/compiled_starters/c/codecrafters.yml @@ -7,5 +7,5 @@ debug: false # Use this to change the C version used to run your code # on Codecrafters. # -# Available versions: c-9.2 -language_pack: c-9.2 +# Available versions: c-23 +language_pack: c-23 diff --git a/compiled_starters/c/app/server.c b/compiled_starters/c/src/main.c similarity index 100% rename from compiled_starters/c/app/server.c rename to compiled_starters/c/src/main.c diff --git a/compiled_starters/c/vcpkg-configuration.json b/compiled_starters/c/vcpkg-configuration.json new file mode 100644 index 0000000..16b40d6 --- /dev/null +++ b/compiled_starters/c/vcpkg-configuration.json @@ -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" + } + ] +} diff --git a/compiled_starters/c/vcpkg.json b/compiled_starters/c/vcpkg.json new file mode 100644 index 0000000..0b8fa67 --- /dev/null +++ b/compiled_starters/c/vcpkg.json @@ -0,0 +1,3 @@ +{ + "dependencies": [] +} \ No newline at end of file diff --git a/compiled_starters/c/your_program.sh b/compiled_starters/c/your_program.sh index 5718e9d..d934ebe 100755 --- a/compiled_starters/c/your_program.sh +++ b/compiled_starters/c/your_program.sh @@ -14,11 +14,12 @@ set -e # Exit early if any commands fail # - Edit .codecrafters/compile.sh to change how your program compiles remotely ( cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory - gcc -lcurl -lz -o /tmp/codecrafters-build-http-server-c app/*.c + cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake + cmake --build ./build ) # Copied from .codecrafters/run.sh # # - Edit this to change how your program runs locally # - Edit .codecrafters/run.sh to change how your program runs remotely -exec /tmp/codecrafters-build-http-server-c "$@" +exec $(dirname $0)/build/http-server "$@" diff --git a/dockerfiles/c-23.Dockerfile b/dockerfiles/c-23.Dockerfile new file mode 100644 index 0000000..a4ca356 --- /dev/null +++ b/dockerfiles/c-23.Dockerfile @@ -0,0 +1,38 @@ +# syntax=docker/dockerfile:1.7-labs +FROM gcc:14.2.0-bookworm + +# Ensures the container is re-built if dependency files change +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 is required by vcpkg +RUN wget --progress=dot:giga https://github.com/Kitware/CMake/releases/download/v3.30.5/cmake-3.30.5-Linux-x86_64.tar.gz && \ + tar -xzvf cmake-3.30.5-Linux-x86_64.tar.gz && \ + mv cmake-3.30.5-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 +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; fi diff --git a/solutions/c/01-at4/code/.codecrafters/compile.sh b/solutions/c/01-at4/code/.codecrafters/compile.sh index ce0e69c..9da226b 100755 --- a/solutions/c/01-at4/code/.codecrafters/compile.sh +++ b/solutions/c/01-at4/code/.codecrafters/compile.sh @@ -8,4 +8,5 @@ set -e # Exit on failure -gcc -lcurl -lz -o /tmp/codecrafters-build-http-server-c app/*.c +cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake +cmake --build ./build diff --git a/solutions/c/01-at4/code/.codecrafters/run.sh b/solutions/c/01-at4/code/.codecrafters/run.sh index 5bed6d0..d433e95 100755 --- a/solutions/c/01-at4/code/.codecrafters/run.sh +++ b/solutions/c/01-at4/code/.codecrafters/run.sh @@ -8,4 +8,4 @@ set -e # Exit on failure -exec /tmp/codecrafters-build-http-server-c "$@" +exec $(dirname $0)/build/http-server "$@" diff --git a/solutions/c/01-at4/code/.gitignore b/solutions/c/01-at4/code/.gitignore new file mode 100644 index 0000000..6e9506c --- /dev/null +++ b/solutions/c/01-at4/code/.gitignore @@ -0,0 +1,55 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +build +vcpkg_installed \ No newline at end of file diff --git a/solutions/c/01-at4/code/CMakeLists.txt b/solutions/c/01-at4/code/CMakeLists.txt new file mode 100644 index 0000000..e4837f8 --- /dev/null +++ b/solutions/c/01-at4/code/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.13) + +project(codecrafters-http-server) + +file(GLOB_RECURSE SOURCE_FILES src/*.c src/*.h) + +set(CMAKE_C_STANDARD 23) # Enable the C23 standard + +add_executable(http-server ${SOURCE_FILES}) diff --git a/solutions/c/01-at4/code/README.md b/solutions/c/01-at4/code/README.md index 96793c7..9ffb007 100644 --- a/solutions/c/01-at4/code/README.md +++ b/solutions/c/01-at4/code/README.md @@ -16,7 +16,7 @@ and more. # Passing the first stage -The entry point for your HTTP server implementation is in `app/server.c`. Study +The entry point for your HTTP server implementation is in `src/main.c`. Study and uncomment the relevant code, and push your changes to pass the first stage: ```sh @@ -30,8 +30,8 @@ Time to move on to the next stage! Note: This section is for stages 2 and beyond. -1. Ensure you have `gcc` installed locally +1. Ensure you have `cmake` installed locally 1. Run `./your_program.sh` to run your program, which is implemented in - `app/server.c`. + `src/main.c`. 1. Commit your changes and run `git push origin master` to submit your solution to CodeCrafters. Test output will be streamed to your terminal. diff --git a/solutions/c/01-at4/code/codecrafters.yml b/solutions/c/01-at4/code/codecrafters.yml index 4de7cd9..51a9b70 100644 --- a/solutions/c/01-at4/code/codecrafters.yml +++ b/solutions/c/01-at4/code/codecrafters.yml @@ -7,5 +7,5 @@ debug: false # Use this to change the C version used to run your code # on Codecrafters. # -# Available versions: c-9.2 -language_pack: c-9.2 +# Available versions: c-23 +language_pack: c-23 diff --git a/solutions/c/01-at4/code/app/server.c b/solutions/c/01-at4/code/src/main.c similarity index 100% rename from solutions/c/01-at4/code/app/server.c rename to solutions/c/01-at4/code/src/main.c diff --git a/solutions/c/01-at4/code/vcpkg-configuration.json b/solutions/c/01-at4/code/vcpkg-configuration.json new file mode 100644 index 0000000..16b40d6 --- /dev/null +++ b/solutions/c/01-at4/code/vcpkg-configuration.json @@ -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" + } + ] +} diff --git a/solutions/c/01-at4/code/vcpkg.json b/solutions/c/01-at4/code/vcpkg.json new file mode 100644 index 0000000..0b8fa67 --- /dev/null +++ b/solutions/c/01-at4/code/vcpkg.json @@ -0,0 +1,3 @@ +{ + "dependencies": [] +} \ No newline at end of file diff --git a/solutions/c/01-at4/code/your_program.sh b/solutions/c/01-at4/code/your_program.sh index 5718e9d..d934ebe 100755 --- a/solutions/c/01-at4/code/your_program.sh +++ b/solutions/c/01-at4/code/your_program.sh @@ -14,11 +14,12 @@ set -e # Exit early if any commands fail # - Edit .codecrafters/compile.sh to change how your program compiles remotely ( cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory - gcc -lcurl -lz -o /tmp/codecrafters-build-http-server-c app/*.c + cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake + cmake --build ./build ) # Copied from .codecrafters/run.sh # # - Edit this to change how your program runs locally # - Edit .codecrafters/run.sh to change how your program runs remotely -exec /tmp/codecrafters-build-http-server-c "$@" +exec $(dirname $0)/build/http-server "$@" diff --git a/solutions/c/01-at4/diff/app/server.c.diff b/solutions/c/01-at4/diff/src/main.c.diff similarity index 100% rename from solutions/c/01-at4/diff/app/server.c.diff rename to solutions/c/01-at4/diff/src/main.c.diff diff --git a/solutions/c/01-at4/explanation.md b/solutions/c/01-at4/explanation.md index aa00d7e..535312b 100644 --- a/solutions/c/01-at4/explanation.md +++ b/solutions/c/01-at4/explanation.md @@ -1,4 +1,4 @@ -The entry point for your HTTP server implementation is in `app/server.c`. +The entry point for your HTTP server implementation is in `src/main.c`. Study and uncomment the relevant code: diff --git a/starter_templates/c/code/.codecrafters/compile.sh b/starter_templates/c/code/.codecrafters/compile.sh index ce0e69c..9da226b 100755 --- a/starter_templates/c/code/.codecrafters/compile.sh +++ b/starter_templates/c/code/.codecrafters/compile.sh @@ -8,4 +8,5 @@ set -e # Exit on failure -gcc -lcurl -lz -o /tmp/codecrafters-build-http-server-c app/*.c +cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake +cmake --build ./build diff --git a/starter_templates/c/code/.codecrafters/run.sh b/starter_templates/c/code/.codecrafters/run.sh index 5bed6d0..d433e95 100755 --- a/starter_templates/c/code/.codecrafters/run.sh +++ b/starter_templates/c/code/.codecrafters/run.sh @@ -8,4 +8,4 @@ set -e # Exit on failure -exec /tmp/codecrafters-build-http-server-c "$@" +exec $(dirname $0)/build/http-server "$@" diff --git a/starter_templates/c/code/.gitignore b/starter_templates/c/code/.gitignore new file mode 100644 index 0000000..6e9506c --- /dev/null +++ b/starter_templates/c/code/.gitignore @@ -0,0 +1,55 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +build +vcpkg_installed \ No newline at end of file diff --git a/starter_templates/c/code/CMakeLists.txt b/starter_templates/c/code/CMakeLists.txt new file mode 100644 index 0000000..e4837f8 --- /dev/null +++ b/starter_templates/c/code/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.13) + +project(codecrafters-http-server) + +file(GLOB_RECURSE SOURCE_FILES src/*.c src/*.h) + +set(CMAKE_C_STANDARD 23) # Enable the C23 standard + +add_executable(http-server ${SOURCE_FILES}) diff --git a/starter_templates/c/code/app/server.c b/starter_templates/c/code/src/main.c similarity index 100% rename from starter_templates/c/code/app/server.c rename to starter_templates/c/code/src/main.c diff --git a/starter_templates/c/code/vcpkg-configuration.json b/starter_templates/c/code/vcpkg-configuration.json new file mode 100644 index 0000000..16b40d6 --- /dev/null +++ b/starter_templates/c/code/vcpkg-configuration.json @@ -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" + } + ] +} diff --git a/starter_templates/c/code/vcpkg.json b/starter_templates/c/code/vcpkg.json new file mode 100644 index 0000000..0b8fa67 --- /dev/null +++ b/starter_templates/c/code/vcpkg.json @@ -0,0 +1,3 @@ +{ + "dependencies": [] +} \ No newline at end of file diff --git a/starter_templates/c/config.yml b/starter_templates/c/config.yml index 6f4f96a..d36874a 100644 --- a/starter_templates/c/config.yml +++ b/starter_templates/c/config.yml @@ -1,3 +1,3 @@ attributes: - required_executable: gcc - user_editable_file: app/server.c + required_executable: cmake + user_editable_file: src/main.c