Skip to content

Conversation

@andy1li
Copy link
Member

@andy1li andy1li commented Feb 20, 2025

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

Summary by CodeRabbit

  • Refactor

    • Switched to a structured CMake build process for improved dependency management.
    • Updated execution commands to run the server using relative paths.
  • Documentation

    • Revised file references and tooling instructions to reflect the new build system.
  • Chore

    • Added configuration and ignore files to keep the repository cleaner.
    • Upgraded the language standard to a more current version for enhanced compatibility.

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
@andy1li andy1li self-assigned this Feb 20, 2025
@linear
Copy link

linear bot commented Feb 20, 2025

@coderabbitai
Copy link

coderabbitai bot commented Feb 20, 2025

Walkthrough

This pull request transitions the build process of multiple C projects from direct gcc commands to a CMake‐based approach. Compilation and execution scripts have been updated to configure and build the project with CMake and to execute the built binary via a relative path. New CMake projects, vcpkg JSON configurations, and comprehensive .gitignore rules have been added across compiled starters, solution examples, and starter templates. Additionally, documentation and configuration settings (e.g. language pack and entry point paths) have been updated, and a Dockerfile for the new C/C++ environment is introduced.

Changes

File(s) Change Summary
compiled_starters/c/…/compile.sh, solutions/c/…/compile.sh, starter_templates/c/…/compile.sh, compiled_starters/c/…/your_program.sh, solutions/c/…/your_program.sh Replaced gcc compilation with a two-step CMake configuration and build process.
compiled_starters/c/…/run.sh, solutions/c/…/run.sh, starter_templates/c/…/run.sh Updated execution commands to use a relative path (e.g. $(dirname $0)/build/http-server) instead of a fixed temporary path.
compiled_starters/c/CMakeLists.txt, solutions/c/…/CMakeLists.txt, starter_templates/c/code/CMakeLists.txt Introduced CMake project files that set the C standard to C23, gather source files, and define the http-server executable target.
compiled_starters/c/.gitignore, solutions/c/…/.gitignore, starter_templates/c/code/.gitignore Added .gitignore files to exclude build artifacts, temporary files, and other generated outputs.
compiled_starters/c/vcpkg-configuration.json, compiled_starters/c/vcpkg.json, solutions/c/…/vcpkg-configuration.json, solutions/c/…/vcpkg.json, starter_templates/c/code/vcpkg-configuration.json, starter_templates/c/code/vcpkg.json Added new JSON configuration files for setting up vcpkg registries and managing project dependencies.
compiled_starters/c/codecrafters.yml, solutions/c/…/codecrafters.yml, starter_templates/c/config.yml, compiled_starters/c/README.md, solutions/c/…/README.md, solutions/c/01-at4/explanation.md Updated configuration and documentation: language pack bumped from c‑9.2 to c‑23, server entry point changed from app/server.c to src/main.c, and tool requirements modified from gcc to cmake.
dockerfiles/c-23.Dockerfile Introduced a new Dockerfile for a C/C++ development environment with gcc base image, vcpkg, and CMake integration.

Sequence Diagram(s)

sequenceDiagram
    participant Dev as Developer
    participant CS as Compile Script
    participant CMake as CMake
    participant Build as Build Directory
    participant RS as Run Script
    participant Server as http-server

    Dev->>CS: Run compile.sh / your_program.sh
    CS->>CMake: Configure project (cmake -B build -S .)
    CMake->>Build: Generate build files
    CS->>CMake: Build project (cmake --build ./build)
    Build-->>CS: Binary created
    Dev->>RS: Run run.sh
    RS->>Server: Execute http-server with arguments
Loading

Poem

I’m a hopping rabbit with code so bright,
Leaving gcc behind for CMake’s light.
Build scripts and run paths now dance in tune,
With vcpkg and Docker making our dreams maroon 🐰.
In every file, new paths are born anew—
Happy hops of progress, for me and you!
CodeRabbit cheers as builds take flight!


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (6)
compiled_starters/c/.codecrafters/run.sh (1)

11-11: Quote variable substitutions in the exec command to prevent word splitting.
The static analysis (Shellcheck SC2046) warns about unquoted command substitutions. This can lead to issues if $(dirname $0) contains spaces. Consider updating the command as follows:

- exec $(dirname $0)/build/http-server "$@"
+ exec "$(dirname "$0")/build/http-server" "$@"

This ensures that the path is treated as a single argument.

🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 11-11: Quote this to prevent word splitting.

(SC2046)

solutions/c/01-at4/code/.codecrafters/run.sh (1)

11-11: Quote Command Substitution to Prevent Word Splitting
The current exec invocation may lead to unintended word splitting if the directory name contains spaces. Consider quoting the command substitution to ensure robust behavior.

-exec $(dirname $0)/build/http-server "$@"
+exec "$(dirname "$0")/build/http-server" "$@"
🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 11-11: Quote this to prevent word splitting.

(SC2046)

starter_templates/c/code/.codecrafters/run.sh (1)

11-11: Quote Command Substitution to Prevent Word Splitting
Similar to the changes in the analogous run script, quoting the command substitution avoids potential issues with spaces in directory names.

-exec $(dirname $0)/build/http-server "$@"
+exec "$(dirname "$0")/build/http-server" "$@"
🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 11-11: Quote this to prevent word splitting.

(SC2046)

compiled_starters/c/.codecrafters/compile.sh (1)

11-12: Validate Build Environment Variable
The new CMake-based build commands rely on the VCPKG_ROOT environment variable. Ensure that VCPKG_ROOT is correctly set in the build environment, as its absence may lead to build failures.

solutions/c/01-at4/code/your_program.sh (1)

25-25: Quote command substitution in exec call to prevent word splitting
To avoid potential issues with word splitting in shell, please update the exec call by quoting the command substitution. For example, change:

- exec $(dirname $0)/build/http-server "$@"
+ exec "$(dirname "$0")"/build/http-server "$@"

This ensures that paths with spaces are handled correctly.

🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 25-25: Quote this to prevent word splitting.

(SC2046)

compiled_starters/c/your_program.sh (1)

25-25: Quote command substitution in exec call to prevent word splitting
As with the similar changes in other scripts, please update the exec call by quoting the directory name to prevent unexpected word splitting. Suggested change:

- exec $(dirname $0)/build/http-server "$@"
+ exec "$(dirname "$0")"/build/http-server "$@"
🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 25-25: Quote this to prevent word splitting.

(SC2046)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 805033a and 865fedb.

📒 Files selected for processing (27)
  • compiled_starters/c/.codecrafters/compile.sh (1 hunks)
  • compiled_starters/c/.codecrafters/run.sh (1 hunks)
  • compiled_starters/c/.gitignore (1 hunks)
  • compiled_starters/c/CMakeLists.txt (1 hunks)
  • compiled_starters/c/README.md (2 hunks)
  • compiled_starters/c/codecrafters.yml (1 hunks)
  • compiled_starters/c/vcpkg-configuration.json (1 hunks)
  • compiled_starters/c/vcpkg.json (1 hunks)
  • compiled_starters/c/your_program.sh (1 hunks)
  • dockerfiles/c-23.Dockerfile (1 hunks)
  • solutions/c/01-at4/code/.codecrafters/compile.sh (1 hunks)
  • solutions/c/01-at4/code/.codecrafters/run.sh (1 hunks)
  • solutions/c/01-at4/code/.gitignore (1 hunks)
  • solutions/c/01-at4/code/CMakeLists.txt (1 hunks)
  • solutions/c/01-at4/code/README.md (2 hunks)
  • solutions/c/01-at4/code/codecrafters.yml (1 hunks)
  • solutions/c/01-at4/code/vcpkg-configuration.json (1 hunks)
  • solutions/c/01-at4/code/vcpkg.json (1 hunks)
  • solutions/c/01-at4/code/your_program.sh (1 hunks)
  • solutions/c/01-at4/explanation.md (1 hunks)
  • starter_templates/c/code/.codecrafters/compile.sh (1 hunks)
  • starter_templates/c/code/.codecrafters/run.sh (1 hunks)
  • starter_templates/c/code/.gitignore (1 hunks)
  • starter_templates/c/code/CMakeLists.txt (1 hunks)
  • starter_templates/c/code/vcpkg-configuration.json (1 hunks)
  • starter_templates/c/code/vcpkg.json (1 hunks)
  • starter_templates/c/config.yml (1 hunks)
✅ Files skipped from review due to trivial changes (12)
  • compiled_starters/c/vcpkg.json
  • solutions/c/01-at4/code/vcpkg.json
  • solutions/c/01-at4/explanation.md
  • solutions/c/01-at4/code/.gitignore
  • solutions/c/01-at4/code/codecrafters.yml
  • starter_templates/c/code/vcpkg.json
  • compiled_starters/c/codecrafters.yml
  • solutions/c/01-at4/code/vcpkg-configuration.json
  • starter_templates/c/code/.gitignore
  • compiled_starters/c/.gitignore
  • compiled_starters/c/vcpkg-configuration.json
  • starter_templates/c/code/vcpkg-configuration.json
🧰 Additional context used
🪛 Shellcheck (0.10.0)
compiled_starters/c/.codecrafters/run.sh

[warning] 11-11: Quote this to prevent word splitting.

(SC2046)

starter_templates/c/code/.codecrafters/run.sh

[warning] 11-11: Quote this to prevent word splitting.

(SC2046)

solutions/c/01-at4/code/.codecrafters/run.sh

[warning] 11-11: Quote this to prevent word splitting.

(SC2046)

compiled_starters/c/your_program.sh

[warning] 25-25: Quote this to prevent word splitting.

(SC2046)

solutions/c/01-at4/code/your_program.sh

[warning] 25-25: Quote this to prevent word splitting.

(SC2046)

🪛 Hadolint (2.12.0)
dockerfiles/c-23.Dockerfile

[error] 31-31: invalid flag: --exclude

(DL1000)

🔇 Additional comments (12)
starter_templates/c/config.yml (1)

2-3: Update configuration attributes to reflect the new build system and project structure.
The attributes have been updated correctly:

  • required_executable is now set to cmake, which aligns with the migration from gcc to CMake.
  • user_editable_file has been updated to src/main.c, matching the new project layout.
    Ensure that all dependent documentation and scripts (e.g., README files and build scripts) are updated accordingly.
compiled_starters/c/CMakeLists.txt (1)

1-9: New CMake configuration for the compiled starter.
This CMakeLists.txt file is well structured and sets up the build process for the project:

  • It specifies a minimum CMake version (3.13) and sets the C standard to C23.
  • The use of file(GLOB_RECURSE ...) correctly gathers all source and header files from the src directory.
  • The executable target http-server is defined appropriately using all source files.

Consideration: For large projects, GLOB_RECURSE may miss new files added during incremental builds, so explicit file listings might be more robust as the project scales.

solutions/c/01-at4/code/CMakeLists.txt (1)

1-10: CMake configuration for the solution example is properly set up.
The file mirrors the configuration of the compiled starter, ensuring consistency:

  • It sets a minimum required CMake version and specifies the project.
  • The C23 standard is enforced, and source files are collected recursively.
  • The executable http-server is built as expected.

This consistent structure across different parts of the repository reduces complexity in maintaining multiple build configurations.

starter_templates/c/code/CMakeLists.txt (1)

1-10: Consistent CMake configuration for the starter template.
This file successfully applies the CMake-based build approach:

  • It correctly defines the minimum required version, project name, and the C23 standard.
  • The use of file(GLOB_RECURSE ...) ensures that all .c and .h files from the src directory are included in the build.
  • The executable target is set up consistently with the other CMakeLists.txt files in the project.

Keep the configuration in sync with the documentation regarding the new project structure.

solutions/c/01-at4/code/.codecrafters/compile.sh (1)

11-12: Transition to CMake Build Process
The updated compile script successfully replaces direct gcc compilation with CMake commands. Please ensure that the VCPKG_ROOT environment variable is available to provide the necessary toolchain file path; otherwise, the CMake configuration may fail.

starter_templates/c/code/.codecrafters/compile.sh (1)

11-12: Adopt Structured Build with CMake
The shift from a direct gcc compilation to using CMake improves maintainability and dependency management. As with the other compile scripts, verify that the VCPKG_ROOT environment variable is properly set. This ensures the toolchain file is located correctly during the configuration step.

solutions/c/01-at4/code/your_program.sh (1)

17-18: Use of CMake commands is correctly implemented
The updated commands correctly configure (cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake) and build (cmake --build ./build) the project using CMake. This structured approach improves maintainability and aligns with the new Debian-based environment.

compiled_starters/c/README.md (2)

19-20: Entry point update accurately reflects new project structure
The updated text clearly states that the HTTP server’s entry point is now in src/main.c, which aligns with the overall restructuring.


33-35: Installation instructions now require CMake
The instructions correctly specify that users must have cmake installed, replacing the previous gcc requirement. This consistency helps prevent build confusion.

solutions/c/01-at4/code/README.md (2)

19-20: Updated entry point and toolchain requirements are clear
The documentation now properly indicates that the server implementation entry point is in src/main.c and that cmake is required for local builds. This improves clarity for users following the setup instructions.


33-35: Instructions for running the program are precise
The steps to run ./your_program.sh and the subsequent guidance match the new build and directory structure, ensuring users can quickly validate their setup.

dockerfiles/c-23.Dockerfile (1)

31-31: 🛠️ Refactor suggestion

⚠️ Potential issue

Invalid flag in COPY command for Dockerfile
The usage of the --exclude flag in the COPY command is not supported by Docker. Remove these flags and instead list .git and README.md in a .dockerignore file to avoid including them in the build context. For example, change:

- COPY --exclude=.git --exclude=README.md . /app
+ COPY . /app   # Ensure .git and README.md are excluded via .dockerignore

This will resolve the error indicated by Hadolint.

⛔ Skipped due to learnings
Learnt from: SughiY
PR: codecrafters-io/build-your-own-http-server#112
File: dockerfiles/clojure-1.12.Dockerfile:10-10
Timestamp: 2025-02-17T19:29:27.993Z
Learning: The `--exclude` flag in COPY commands is supported in Dockerfile syntax 1.7-labs and should be retained for optimizing layer caching, despite static analysis tools like Hadolint flagging it as an error.
🧰 Tools
🪛 Hadolint (2.12.0)

[error] 31-31: invalid flag: --exclude

(DL1000)

@andy1li andy1li merged commit acc6743 into main Feb 20, 2025
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants