Skip to content

Commit 720c95d

Browse files
committed
chore: Add enough files to begin with the project
1 parent e98f5bd commit 720c95d

18 files changed

Lines changed: 1261 additions & 185 deletions

.env.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LEETCODE_COOKIE=

CMakeLists.txt

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ include(cmake/defaults.cmake)
1111
include(FindThreads)
1212
include(CTest)
1313

14+
# Ensure tests are built by default so files under src/solution/ are compiled
15+
# unless the user explicitly disables BUILD_TESTING in the cache.
16+
if(NOT DEFINED BUILD_TESTING)
17+
set(BUILD_TESTING
18+
ON
19+
CACHE BOOL "Build tests" FORCE)
20+
endif()
21+
22+
# Find CURL provided by Conan or system
23+
find_package(CURL REQUIRED)
24+
find_package(nlohmann_json CONFIG REQUIRED)
25+
1426
add_library(warnings INTERFACE)
1527
set_target_warnings(warnings)
1628

@@ -20,16 +32,76 @@ enable_sanitizers(options)
2032
enable_hardenings(options)
2133
enable_coverage(options)
2234

23-
# put platform-agnostic code here
24-
add_subdirectory(lib)
35+
# Top-level executable (from CMakeLists-ref.txt)
36+
add_executable(leetcode_cpp src/main.cpp src/fetcher.cpp)
37+
target_include_directories(leetcode_cpp PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
38+
target_link_libraries(
39+
leetcode_cpp PRIVATE CURL::libcurl nlohmann_json::nlohmann_json warnings
40+
options)
2541

26-
# put platform-specific code here and guard it with an option to exclude it from
27-
# build when building unit tests (e.g. you develop an embedded firmware)
28-
if(CMAKE_CROSSCOMPILING OR TRUE)
29-
add_subdirectory(src)
42+
# Add a convenient run target so callers can invoke `cmake --build --target
43+
# run_leetcode_cpp`
44+
if(TARGET leetcode_cpp)
45+
add_custom_target(
46+
run_leetcode_cpp
47+
COMMAND ${CMAKE_COMMAND} -E echo "Running leetcode_cpp..."
48+
COMMAND $<TARGET_FILE:leetcode_cpp>
49+
DEPENDS leetcode_cpp
50+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
3051
endif()
3152

3253
# put unit tests that run on your pc here
3354
if(NOT CMAKE_CROSSCOMPILING AND BUILD_TESTING)
34-
add_subdirectory(test)
55+
# Tests target: compile all src/solution/*.cpp into a test executable and link
56+
# gtest Use CONFIGURE_DEPENDS so CMake will re-run configure when files are
57+
# added/removed
58+
enable_testing()
59+
file(GLOB_RECURSE SOLUTION_SRC CONFIGURE_DEPENDS
60+
"${CMAKE_CURRENT_SOURCE_DIR}/src/solution/*.cpp")
61+
if(SOLUTION_SRC)
62+
# Ensure GTest is available (from Conan or system)
63+
find_package(GTest REQUIRED)
64+
add_executable(leetcode_tests ${SOLUTION_SRC})
65+
target_include_directories(leetcode_tests
66+
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
67+
target_compile_definitions(leetcode_tests PRIVATE ENABLE_GTEST=1)
68+
target_link_libraries(leetcode_tests PRIVATE GTest::gtest_main
69+
CURL::libcurl)
70+
# Register individual GoogleTest cases with CTest so each gtest is reported
71+
# as a separate test (gtest_discover_tests requires the GoogleTest CMake
72+
# module)
73+
include(GoogleTest)
74+
gtest_discover_tests(leetcode_tests)
75+
else()
76+
message(
77+
WARNING
78+
"No solution sources found in src/solution; skipping leetcode_tests target"
79+
)
80+
endif()
81+
82+
# Optional: build and run a single solution via -DSOLUTION=<filename without
83+
# .cpp>
84+
set(SOLUTION
85+
""
86+
CACHE STRING
87+
"Solution file basename in src/solution (e.g. s0001_two_sum)")
88+
if(SOLUTION)
89+
set(SOLUTION_SRC "${CMAKE_CURRENT_SOURCE_DIR}/src/solution/${SOLUTION}.cpp")
90+
if(EXISTS ${SOLUTION_SRC})
91+
add_executable(run_solution_exec
92+
${SOLUTION_SRC} ${CMAKE_CURRENT_SOURCE_DIR}/src/runner.cpp)
93+
target_include_directories(run_solution_exec
94+
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
95+
add_custom_target(
96+
run_solution
97+
COMMAND $<TARGET_FILE:run_solution_exec>
98+
DEPENDS run_solution_exec
99+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
100+
else()
101+
message(
102+
WARNING
103+
"SOLUTION ${SOLUTION_SRC} not found; run_solution target will not be created"
104+
)
105+
endif()
106+
endif()
35107
endif()

conanfile.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ class Example(ConanFile):
77
settings = "os", "arch", "compiler", "build_type"
88
languages = "C", "C++"
99
options = {
10-
"fPIC": [True, False]
10+
"fPIC": [True, False],
11+
}
12+
default_options = {
13+
"fPIC": True,
14+
"libcurl/*:with_brotli": True,
15+
"libcurl/*:with_zstd": True,
16+
"libcurl/*:with_zlib": True,
17+
"libcurl/*:with_openssl": True,
1118
}
12-
default_options = {"fPIC": True}
1319

1420
def layout(self):
1521
cmake_layout(self)
@@ -19,9 +25,9 @@ def config_options(self):
1925
del self.options.fPIC
2026

2127
def requirements(self):
22-
self.requires("fmt/12.0.0")
23-
if not self.conf.get("tools.build:skip_test"):
24-
self.requires("gtest/1.17.0")
28+
self.requires("nlohmann_json/3.12.0")
29+
self.requires("libcurl/8.18.0")
30+
self.requires("gtest/1.17.0")
2531

2632
def generate(self):
2733
deps = CMakeDeps(self)

justfile

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
set shell := ["bash", "-eu", "-o", "pipefail", "-c"]
2+
3+
# Defaults (edit as needed)
4+
build_type := "Debug" # choices: "Debug" "Release" "RelWithDebInfo" "MinSizeRel"
5+
preset := "conan-debug" # choices: "conan-debug" "conan-release"
6+
profile := "conan/clang21" # choices: "conan/gcc15" "conan/clang21" "conan/default"
7+
generator := "Ninja" # choices: "Ninja" "Unix Makefiles"
8+
CC := "clang" # choices: "gcc" "clang"
9+
CXX := "clang++" # choices: "g++" "clang++"
10+
skip_test := "False" # choices: "True" "False"
11+
cache_option := "ccache" # choices: "ccache" "sccache" "none"
12+
enable_cppcheck := "False" # choices: "True" "False"
13+
enable_clang_tidy := "False" # choices: "True" "False"
14+
enable_coverage := "False" # choices: "True" "False"
15+
run_binary := "leetcode_cpp" # executable name produced by the build
16+
17+
# Show available recipes
18+
help:
19+
@just --list
20+
21+
# Setup virtual environment using uv (from README)
22+
venv:
23+
uv venv
24+
25+
sync:
26+
uv sync
27+
28+
pre-commit-install:
29+
uv run pre-commit install
30+
31+
conan-profile-detect:
32+
# idempotent: only run detect if the default profile file does not already exist
33+
if [ -f "$HOME/.conan2/profiles/default" ]; then echo "Conan profile 'default' exists -- skipping"; else uv run conan profile detect; fi
34+
35+
# One-shot setup: run venv, sync, pre-commit install and conan profile detect
36+
setup: venv sync pre-commit-install conan-profile-detect
37+
@echo "Setup finished. To activate the virtualenv run: source .venv/bin/activate"
38+
39+
# Install dependencies via Conan (edit variables above before running)
40+
conan-install:
41+
uv run conan install -pr:a default \
42+
-pr:h {{profile}} \
43+
-c:a tools.cmake.cmaketoolchain:generator={{generator}} \
44+
-c:h tools.build:compiler_executables='{"c": "{{CC}}", "cpp": "{{CXX}}"}' \
45+
-c:h tools.build:skip_test={{skip_test}} \
46+
-s:h build_type={{build_type}} -b missing .
47+
48+
# Activate virtualenv (prints instruction because sourcing won't persist)
49+
activate:
50+
@echo "Run: source .venv/bin/activate"
51+
52+
# Configure project using CMake presets (adjust -D flags as needed)
53+
configure:
54+
uv run cmake --preset {{preset}} \
55+
-D ENABLE_CPPCHECK={{enable_cppcheck}} \
56+
-D ENABLE_CLANG_TIDY={{enable_clang_tidy}} \
57+
-D ENABLE_IPO=OFF \
58+
-D ENABLE_CACHE=OFF \
59+
-D CACHE_OPTION={{cache_option}} \
60+
-D ENABLE_COVERAGE={{enable_coverage}} \
61+
-D ENABLE_HARDENINGS=OFF \
62+
-D ENABLE_FORTIFY_SOURCE=OFF \
63+
-D ENABLE_ASAN=OFF \
64+
-D ENABLE_LSAN=OFF \
65+
-D ENABLE_UBSAN=OFF \
66+
-D ENABLE_TSAN=OFF
67+
68+
# Build and test
69+
build:
70+
uv run cmake --build --preset {{preset}}
71+
72+
test:
73+
uv run ctest --preset {{preset}}
74+
75+
# Run the compiled leetcode_cpp executable. Depends on `build` so the binary
76+
# is up-to-date. Override `run_binary` or `build_type` locally if needed.
77+
run: build
78+
uv run ./build/{{build_type}}/{{run_binary}} ${RUN_ARGS:-}
79+
80+
# Cleaning targets
81+
# - `clean-setup`: remove setup artifacts (virtualenv, pre-commit hooks)
82+
# - `clean-build`: remove build artifacts
83+
# - `clean-all`: remove both setup and build artifacts
84+
# Keep `clean` as an alias to `clean-build` for backwards compatibility.
85+
clean-setup:
86+
# If a virtualenv exists, attempt to uninstall pre-commit (without creating a venv), then remove it
87+
if [ -d .venv ]; then if [ -x .venv/bin/pre-commit ]; then .venv/bin/pre-commit uninstall || true; elif command -v uv >/dev/null 2>&1; then uv run pre-commit uninstall || true; fi; fi; rm -rf .venv/ || true
88+
89+
clean-build:
90+
# Remove common build directories and artifacts
91+
for d in build "{{build_type}}" Debug Release "build/{{build_type}}" build/Debug build/Release build/bin; do if [ -d "$d" ]; then rm -rf "$d" || true; fi; done
92+
# remove CMake-generated files in project root if present
93+
rm -f CMakeCache.txt cmake_install.cmake compile_commands.json CMakeUserPresets.json CMakeSettings.json || true
94+
95+
clean-all: clean-setup clean-build
96+
97+
clean: clean-build
98+
99+
# Collect coverage (use appropriate gcov value for your toolchain)
100+
coverage:
101+
GCOV="gcov" uv run gcovr
102+
103+
# Format C/C++ sources under src/ using clang-format
104+
fmt:
105+
if ! command -v clang-format >/dev/null 2>&1; then echo "clang-format not found; install clang-format to use just fmt"; exit 2; fi
106+
find src -type f \( -name '*.cpp' -o -name '*.cc' -o -name '*.cxx' -o -name '*.h' -o -name '*.hpp' \) -print0 | xargs -0 clang-format -i
107+
@echo "Formatted C/C++ sources under src/"
108+
109+
# Default task
110+
default: help

lib/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/math/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.

lib/math/include/math/math.hpp

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/CMakeLists.txt

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)