Skip to content

Commit

Permalink
Add C23
Browse files Browse the repository at this point in the history
  • Loading branch information
andy1li committed Oct 24, 2024
1 parent f393495 commit 7d5fc4d
Show file tree
Hide file tree
Showing 33 changed files with 778 additions and 0 deletions.
12 changes: 12 additions & 0 deletions compiled_starters/c/.codecrafters/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
#
# This script is used to compile your program on CodeCrafters
#
# This runs before .codecrafters/run.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
cmake --build ./build
11 changes: 11 additions & 0 deletions compiled_starters/c/.codecrafters/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
#
# This script is used to run your program on CodeCrafters
#
# This runs after .codecrafters/compile.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

exec ./build/codecrafters-build-git-c "$@"
1 change: 1 addition & 0 deletions compiled_starters/c/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
55 changes: 55 additions & 0 deletions compiled_starters/c/.gitignore
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions compiled_starters/c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.13)

project(codecrafters-git)

file(GLOB_RECURSE SOURCE_FILES app/*.c app/*.h)

set(CMAKE_C_STANDARD 23) # Enable the C23 standard

add_executable(codecrafters-build-git-c ${SOURCE_FILES})
59 changes: 59 additions & 0 deletions compiled_starters/c/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/git.png)

This is a starting point for C solutions to the
["Build Your Own Git" Challenge](https://codecrafters.io/challenges/git).

In this challenge, you'll build a small Git implementation that's capable of
initializing a repository, creating commits and cloning a public repository.
Along the way we'll learn about the `.git` directory, Git objects (blobs,
commits, trees etc.), Git's transfer protocols and more.

**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to try the challenge.

# Passing the first stage

The entry point for your Git implementation is in `app/main.c`. Study and
uncomment the relevant code, and push your changes to pass the first stage:

```sh
git commit -am "pass 1st stage" # any msg
git push origin master
```

That's all!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `gcc` installed locally
1. Run `./your_program.sh` to run your Git implementation, which is implemented
in `app/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.

# Testing locally

The `your_program.sh` script is expected to operate on the `.git` folder inside
the current working directory. If you're running this inside the root of this
repository, you might end up accidentally damaging your repository's `.git`
folder.

We suggest executing `your_program.sh` in a different folder when testing
locally. For example:

```sh
mkdir -p /tmp/testing && cd /tmp/testing
/path/to/your/repo/your_program.sh init
```

To make this easier to type out, you could add a
[shell alias](https://shapeshed.com/unix-alias/):

```sh
alias mygit=/path/to/your/repo/your_program.sh

mkdir -p /tmp/testing && cd /tmp/testing
mygit init
```
47 changes: 47 additions & 0 deletions compiled_starters/c/app/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>

int main(int argc, char *argv[]) {
// Disable output buffering
setbuf(stdout, NULL);
setbuf(stderr, NULL);

if (argc < 2) {
fprintf(stderr, "Usage: ./your_program.sh <command> [<args>]\n");
return 1;
}

const char *command = argv[1];

if (strcmp(command, "init") == 0) {
// You can use print statements as follows for debugging, they'll be visible when running tests.
fprintf(stderr, "Logs from your program will appear here!\n");

// Uncomment this block to pass the first stage
//
// if (mkdir(".git", 0755) == -1 ||
// mkdir(".git/objects", 0755) == -1 ||
// mkdir(".git/refs", 0755) == -1) {
// fprintf(stderr, "Failed to create directories: %s\n", strerror(errno));
// return 1;
// }
//
// FILE *headFile = fopen(".git/HEAD", "w");
// if (headFile == NULL) {
// fprintf(stderr, "Failed to create .git/HEAD file: %s\n", strerror(errno));
// return 1;
// }
// fprintf(headFile, "ref: refs/heads/main\n");
// fclose(headFile);
//
// printf("Initialized git directory\n");
} else {
fprintf(stderr, "Unknown command %s\n", command);
return 1;
}

return 0;
}
11 changes: 11 additions & 0 deletions compiled_starters/c/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the C version used to run your code
# on Codecrafters.
#
# Available versions: c-23
language_pack: c-23
14 changes: 14 additions & 0 deletions compiled_starters/c/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/c/vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dependencies": []
}
25 changes: 25 additions & 0 deletions compiled_starters/c/your_program.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh
#
# Use this script to run your program LOCALLY.
#
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program.
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit early if any commands fail

# Copied from .codecrafters/compile.sh
#
# - Edit this to change how your program compiles locally
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
(
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
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 ./build/codecrafters-build-git-c "$@"
40 changes: 40 additions & 0 deletions dockerfiles/c-23.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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

# Install language-specific dependencies
RUN .codecrafters/compile.sh

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; fi
12 changes: 12 additions & 0 deletions solutions/c/01-gg4/code/.codecrafters/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
#
# This script is used to compile your program on CodeCrafters
#
# This runs before .codecrafters/run.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
cmake --build ./build
11 changes: 11 additions & 0 deletions solutions/c/01-gg4/code/.codecrafters/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
#
# This script is used to run your program on CodeCrafters
#
# This runs after .codecrafters/compile.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

exec ./build/codecrafters-build-git-c "$@"
1 change: 1 addition & 0 deletions solutions/c/01-gg4/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
55 changes: 55 additions & 0 deletions solutions/c/01-gg4/code/.gitignore
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions solutions/c/01-gg4/code/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.13)

project(codecrafters-git)

file(GLOB_RECURSE SOURCE_FILES app/*.c app/*.h)

set(CMAKE_C_STANDARD 23) # Enable the C23 standard

add_executable(codecrafters-build-git-c ${SOURCE_FILES})
Loading

0 comments on commit 7d5fc4d

Please sign in to comment.