Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ jobs:
config:
- {
name: 'Macos Clang',
cc: $(brew --prefix llvm@21)/bin/clang,
cxx: $(brew --prefix llvm@21)/bin/clang++,
cc: $(brew --prefix llvm@22)/bin/clang,
cxx: $(brew --prefix llvm@22)/bin/clang++,
profiles: '-pr:h conan/clang21 -pr:h conan/libc++'
}
steps:
Expand All @@ -156,7 +156,7 @@ jobs:

- name: Install dependencies
run: |
brew install llvm@21
brew install llvm@22
uv sync
uv run conan profile detect

Expand Down Expand Up @@ -196,7 +196,7 @@ jobs:

- name: Collect test coverage
if: matrix.build.type == 'Debug'
run: GCOV="$(brew --prefix llvm@21)/bin/llvm-cov gcov" uv run gcovr
run: GCOV="$(brew --prefix llvm@22)/bin/llvm-cov gcov" uv run gcovr

build_windows:
name: ${{ matrix.config.name }} ${{ matrix.build.type }}
Expand Down
210 changes: 192 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,198 @@
# My Leetcode Solution in C++
# cpp-project-template

NOTE: Insipired by [original repository](https://github.com/tan-wei/leetcode-rust), thanks the original author and contributors of it!
[![CI](https://github.com/b1ackviking/cpp-project-template/actions/workflows/ci.yml/badge.svg)](https://github.com/b1ackviking/cpp-project-template/actions/workflows/ci.yml)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://pre-commit.com)

## How to use
## About cpp-project-template

We use `just` to make it easy to install, run and test:
1. `just setup`: Setup all enviroment with `uv`
2. `just conan-install`: Install all dependencies by `conan`
3. `just configure`: Do configure for `CMake`
4. `just build`: Build the whole project
5. `just run`: Run the main problem get program, fetch and solve them!
6. `just test`: Run tests for whole projects
7. `just cppcheck`: Run cppcheck
8. `just clean-all`: Remove all generated files
This is a template of a cross-platform CMake-based C++ project.

- Run [Cppcheck](http://cppcheck.net/) and [Clang-Tidy](https://clang.llvm.org/extra/clang-tidy/).
- Ensure clean and robust code with a pre-configured set of warnings for Clang, GCC, and MSVC.
- Create sanitizer-instrumented builds by changing a single flag.
- Validate files structure and follow
the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/)
specification with [pre-commit](https://pre-commit.com/) hooks.
- Build and test your project with [GitHub Actions](https://github.com/features/actions).

## Usage
## Required software

* Remove all the solution .cpp
* Clean all files in *src/solution*
* Start your leetcode journey in rust by typing `just run` and then input the question id
* To solve a problem (we should start the problem first), input `solve {question_id}`
* To passby cloudflare, please `cp .env.smaple .env`, and set `LEETCODE_COOKIE` with your cookie (could be find in Firefox or Chrome console)
- Common
- [Git](https://git-scm.com/)
- [CMake](https://cmake.org/)
- [Ninja](https://ninja-build.org/)
- [uv](https://github.com/astral-sh/uv)
- [LLVM](https://releases.llvm.org/)
- Windows
- [PowerShell](https://github.com/PowerShell/PowerShell)
- [Visual Studio Build Tools (Desktop development with C++ workload)](https://visualstudio.microsoft.com/downloads/)
- [OpenCppCoverage](https://community.chocolatey.org/packages/opencppcoverage)
- Install with [Chocolatey](https://chocolatey.org/)
- Optional
- [cppcheck](https://github.com/danmar/cppcheck)
- [Node.js](https://nodejs.dev/download/)
- [Commitlint](https://commitlint.js.org/)
- [Semantic Release](https://github.com/semantic-release/semantic-release)

Run the following commands in the root of the repository after cloning:

```bash
uv venv
uv sync
uv run pre-commit install
uv run conan profile detect
```

## Building the project

<details open>
<summary>On Linux/Mac</summary>

```bash
source .venv/bin/activate

# install libraries
conan install -pr:a default \
-pr:h conan/<profile matching the compiler in use> \
-c:a tools.cmake.cmaketoolchain:generator=<CMake generator> \
-c:h tools.build:compiler_executables='{"c": "<CC>", "cpp": "<CXX>"}' \
-c:h tools.build:skip_test=<True|False> \
-s:h build_type=<Debug|RelWithDebInfo|Release|MinSizeRel> -b missing .

# activate build env
source build/<build_type>/generators/conanbuild.sh

# configure
cmake --preset <preset> \
-D ENABLE_CPPCHECK=<bool> \
-D ENABLE_CLANG_TIDY=<bool> \
-D ENABLE_IPO=<bool> \
-D ENABLE_CACHE=<bool> \
-D CACHE_OPTION=<ccache or sccache> \
-D ENABLE_COVERAGE=<bool> \
-D ENABLE_HARDENINGS=<bool> \
-D ENABLE_FORTIFY_SOURCE=<bool> \
-D ENABLE_ASAN=<bool> \
-D ENABLE_LSAN=<bool> \
-D ENABLE_UBSAN=<bool> \
-D ENABLE_TSAN=<bool>

# build
cmake --build --preset <preset>

# to make sure your library headers are self-contained
cmake --build --preset <preset> --target all_verify_interface_header_sets

# run tests
ctest --preset <preset>

# collect test coverage if ENABLE_COVERAGE == TRUE
GCOV=<"gcov" for GCC, "llvm-cov gcov" for Clang> gcovr
```
</details>

<details>
<summary>On Windows</summary>

```powershell
.venv\Scripts\activate.ps1

# install libraries
conan install -pr:a default \
-pr:h conan/<profile matching the compiler in use> \
-c:a tools.cmake.cmaketoolchain:generator=<CMake generator> \
-c:h tools.env.virtualenv:powershell=pwsh \
-c:h tools.build:compiler_executables='{"c": "<CC>", "cpp": "<CXX>"}' \
-c:h tools.build:skip_test=<True|False> \
-s:h build_type=<Debug|RelWithDebInfo|Release|MinSizeRel> -b missing .

# activate build env
build/<build_type>/generators/conanbuild.ps1

# configure
cmake --preset <preset> \
-D ENABLE_CPPCHECK=<bool> \
-D ENABLE_CLANG_TIDY=<bool> \
-D ENABLE_IPO=<bool> \
-D ENABLE_CACHE=<bool> \
-D CACHE_OPTION=<ccache or sccache> \
-D ENABLE_COVERAGE=<bool> \
-D ENABLE_HARDENINGS=<bool> \
-D ENABLE_FORTIFY_SOURCE=<bool> \
-D ENABLE_ASAN=<bool>

# build
cmake --build --preset <preset>

# to make sure your library headers are self-contained
cmake --build --preset <preset> --target all_verify_interface_header_sets

# run tests and collect test coverage (Windows)
OpenCppCoverage.exe --export_type cobertura:coverage.xml --cover_children -- ctest --preset <preset>
```
</details>

## Customization checklist

- [ ] Change the LICENSE file
- [ ] Write a `README.md`
- [ ] Fill in the community health files
- [ ] Edit `.github/CONTRIBUTING.md` (don't forget to update links)
- [ ] Edit `.github/CODE_OF_CONDUCT.md`
- [ ] Edit `.github/SECURITY.md`
- [ ] Clean the `.git-blame-ignore-revs` file
- [ ] Configure the `pyproject.toml` file
- [ ] Configure CODEOWNERS (`.github/CODEOWNERS`)
- [ ] Configure Dependabot (`.github/dependabot.yml`)

### Select an appropriate LICENSE

The template uses the Unlicense license.
A license with no conditions whatsoever which dedicates works to the public domain.
Unlicensed works, modifications and larger works may be distributed
under different terms and without source code.
You likely want to use another license for your project.
If you are not yet sure which one to use,
[this](https://choosealicense.com/) resource can help you.

### Create a README

Describe your project to the audience.
People interested in your project don't need to read this text :)

And don't forget to update badge URLs if you decide to keep them.

### Fill in the community health files

Apart from README, there are a number of files in the .github directory
that help people to interact with your project:
submit issues and pull requests, security vulnerabilities.
Please update the contents of these files with the links to your repository and contact methods.

### Clean the `.git-blame-ignore-revs` file

The `.git-blame-ignore-revs` file contains commit SHAs that you may want to skip
while viewing the git blame on GitHub as if using `git blame --ignore-revs-file=.git-blame-ignore-revs`.

### Configure the `pyproject.toml` file

The `pyproject.toml` file is a main configuration file for `uv`.
This template is not a python project, however, using `uv` makes it
very convenient to bring tool dependencies,
such as recent versions of `conan`, `cmake`, `pre-commit`, etc.
Also, keeping them contained in a virtual environment does not pollute
your system and provides a simple way to setup them on a CI pipeline.

Besides specifying tool dependencies, you may want to also update
the `name`, `version`, `description`, `authors`, and `license` fields.

### Configure CODEOWNERS

Code owners are automatically requested for review
when someone opens a pull request that modifies code that they own.
Code owners are not automatically requested to review draft pull requests.

### Configure Dependabot

Dependabot is a GitHub bot that helps you keep dependencies up to date.
Configure dependency scanning in the `.github/dependabot.yml` file.
15 changes: 13 additions & 2 deletions cmake/defaults.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ endif()
option(ENABLE_HARDENINGS "Enable hardenings" OFF)

# Useful CMake defaults
set(CMAKE_ERROR_DEPRECATED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)
set(CMAKE_COMPILE_WARNING_AS_ERROR ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_SCAN_FOR_MODULES ON)
set(CMAKE_VERIFY_INTERFACE_HEADER_SETS ON)
set(CMAKE_VERIFY_PRIVATE_HEADER_SETS ON)
set(CMAKE_LINK_WHAT_YOU_USE TRUE)
set(CMAKE_VS_JUST_MY_CODE_DEBUGGING ON)
set(CMAKE_COLOR_DIAGNOSTICS ON)
set(CMAKE_GTEST_DISCOVER_TESTS_DISCOVERY_MODE PRE_TEST)
set(CMAKE_CTEST_ARGUMENTS --progress --output-on-failure)
list(APPEND CMAKE_CTEST_ARGUMENTS --parallel)
set(CMAKE_CTEST_ARGUMENTS --progress --output-on-failure --parallel)

# Set a default build type if none was specified
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
Expand Down Expand Up @@ -66,8 +68,11 @@ if(ENABLE_CPPCHECK)
-v
--enable=all
--check-level=exhaustive
--disable=unusedFunction
--inline-suppr
--error-exitcode=42
--checkers-report=${CMAKE_BINARY_DIR}/cppcheck.report
--suppress=checkersReport # see cppcheck.report for details
--suppressions-list=${CMAKE_SOURCE_DIR}/cppcheck.suppressions)
set(CMAKE_C_CPPCHECK ${CMAKE_CXX_CPPCHECK})
else()
Expand Down Expand Up @@ -122,6 +127,7 @@ endif()
# Apply the default set of warnings to the target
function(set_target_warnings target_name)
set(msvc_warnings
# gersemi: off
/W4 # Baseline reasonable warnings
/w14242 # 'identifier': conversion from 'type1' to 'type1', possible loss
# of data
Expand Down Expand Up @@ -153,8 +159,10 @@ function(set_target_warnings target_name)
/w14928 # illegal copy-initialization; more than one user-defined
# conversion has been implicitly applied
/permissive- # standards conformance mode for MSVC compiler.
# gersemi: on
)
set(clang_warnings
# gersemi: off
-Wall
-Wextra # reasonable and standard
-Wpedantic # warn if non-standard C++ is used
Expand All @@ -174,16 +182,19 @@ function(set_target_warnings target_name)
-Wdouble-promotion # warn if float is implicit promoted to double
-Wformat=2 # warn on security issues around functions that format output
# (ie printf)
# gersemi: on
)
set(gcc_warnings
${clang_warnings}
# gersemi: off
-Wmisleading-indentation # warn if indentation implies blocks where blocks
# do not exist
-Wduplicated-cond # warn if if / else chain has duplicated conditions
-Wduplicated-branches # warn if if / else branches have duplicated code
-Wlogical-op # warn about logical operations being used where bitwise were
# probably wanted
-Wuseless-cast # warn if you perform a cast to the same type
# gersemi: on
)
if(MSVC)
set(warnings ${msvc_warnings})
Expand Down
5 changes: 5 additions & 0 deletions conan/clang22
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include(cppstd)

[settings]
compiler=clang
compiler.version=22
Loading
Loading