Skip to content

Commit c505cf9

Browse files
committed
Adjust Project Structure
1 parent 1d517e6 commit c505cf9

33 files changed

+400
-222
lines changed

.clangd

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CompileFlags:
22
Add:
33
# [TODO] Maybe add a python script to change cuda path automatically?
44
# You may need to change this to /usr/local/cuda if using Linux:
5-
- --cuda-path=C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.5/
5+
# - --cuda-path=C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.5/
66
- --no-cuda-version-check
77
Remove:
88
- -ccbin
@@ -18,19 +18,21 @@ CompileFlags:
1818
- -arch=*
1919

2020
Diagnostics:
21-
UnusedIncludes: None
21+
UnusedIncludes: Strict
22+
MissingIncludes: Strict
2223

2324
ClangTidy:
2425
Add: [
2526
performance-*,
2627
modernize-*,
2728
clang-analyzer-*,
28-
readability-identifier-naming,
29-
readability-braces-around-statements,
30-
readability-identifier-naming,
31-
readability-implicit-bool-conversion,
32-
readability-qualified-auto,
33-
readability-redundant-member-init
29+
readability-*,
30+
]
31+
32+
Remove: [
33+
readability-isolate-declaration,
34+
readability-identifier-length,
35+
readability-magic-numbers,
3436
]
3537

3638
CheckOptions:
@@ -43,9 +45,4 @@ Diagnostics:
4345
readability-identifier-naming.ClassCase: CamelCase
4446
readability-identifier-naming.StructCase: CamelCase
4547
readability-identifier-naming.FunctionCase: lowerCamelCase
46-
readability-identifier-naming.ClassMethodCase: lowerCamelCase
47-
readability-identifier-naming.MethodCase: lowerCamelCase
48-
49-
Remove: [
50-
# modernize-use-trailing-return-type,
51-
]
48+
readability-identifier-naming.ClassMethodCase: lowerCamelCase
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: "[CI] Auto Format and Commit"
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
format:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout main branch
17+
uses: actions/checkout@v4
18+
with:
19+
ref: main
20+
fetch-depth: 0
21+
# You should add a Personal Access Token (PAT) to the repository secrets
22+
token: ${{ secrets.PAT || github.token }}
23+
24+
- name: Set up Python
25+
uses: actions/setup-python@v5
26+
with:
27+
python-version: "3.12"
28+
29+
- name: Install formatter
30+
shell: bash
31+
run: |
32+
wget https://apt.llvm.org/llvm.sh && sudo bash ./llvm.sh 20 && rm ./llvm.sh
33+
sudo apt-get install clang-format-20
34+
sudo ln -sf $(which clang-format-20) /usr/bin/clang-format
35+
python -m pip install black
36+
37+
- name: Run format script
38+
shell: bash
39+
run: |
40+
bash ./scripts/format.sh -cxx -py --clang-format-config=.clang-format
41+
42+
- name: Check for Uncommitted Changes
43+
shell: bash
44+
id: format_check
45+
run: |
46+
# Check if clang-format introduced any changes
47+
if [[ `git status --porcelain` ]]; then
48+
echo "Files were not properly formatted. The changes will be pushed to branch main."
49+
echo "format_status=fail" >> $GITHUB_OUTPUT
50+
else
51+
echo "All files are properly formatted."
52+
echo "format_status=pass" >> $GITHUB_OUTPUT
53+
fi
54+
55+
- name: Commit and Push to main
56+
shell: bash
57+
if : ${{ steps.format_check.outputs.format_status == 'fail' }}
58+
run: |
59+
git config user.name "github-actions[bot]"
60+
git config user.email "github-actions[bot]@users.noreply.github.com"
61+
git commit -am "[skip ci] Auto-format code with clang-format and black"
62+
git push origin main

.vscode/extensions.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
"llvm-vs-code-extensions.vscode-clangd", // clangd
44
"twxs.cmake", // cmake
55
"ms-vscode.cpptools", // C/C++
6+
"ms-python.python", // Python
7+
"ms-python.black-formatter", // Python formatter
8+
"njpwerner.autodocstring", // Python docstring generator
69
]
710
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ conda activate pytemplate
1313
Install `example_package`:
1414

1515
```bash
16-
pip3 install .
16+
pip3 install --no-build-isolation .
1717
```
1818

1919
`torch.ops.example_package.vector_add` will be available after installation; See [test.py](test/test.py) for usage.

csrc/CMakeLists.txt

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,6 @@ cmake_minimum_required(VERSION 3.30)
44
# Project Name and version
55
project(VSC-CMAKE-CXX-TEMPLATE VERSION 1.0.0)
66

7-
####################################################################################################
8-
# [NOTE] All CMake Options and Variables MAY Affect the Build Process
9-
#
10-
# In "cmake/utils/logging.cmake":
11-
# - `LOG_PREFIX`: The prefix for logging messages
12-
#
13-
# In "cmake/compilers/cxx-compiler-configs.cmake":
14-
# - `ENV{CXX}`: The compiler for C++ code
15-
# - `CMAKE_CXX_STANDARD`: The standard for C++ code
16-
# - `STACK_SIZE`: The stack size for the executable
17-
#
18-
# In "cmake/compilers/cuda-compiler-configs.cmake":
19-
# - `ENV{NVCC_CCBIN}`: The CUDA compiler bindir
20-
# - `CMAKE_CUDA_STANDARD`: The standard for CUDA code
21-
#
22-
# In "cmake/utils/run-python.cmake":
23-
# - `ENV{PYTHON_EXECUTABLE}`: The path to the Python executable
24-
#
25-
# In "cmake/libraries/cuda.cmake":
26-
# - `ENV{CUDA_HOME}`: The path to the CUDA toolkit
27-
#
28-
# Others:
29-
# - `BUILD_CUDA_EXAMPLES`: Whether to build CUDA examples
30-
# - `PROJECT_NAMESPACE`: The namespace for the project
31-
# - `BUILD_SHARED_LIBS`: Whether to build shared libraries
32-
####################################################################################################
33-
347
# Common utility functions
358
include(${PROJECT_SOURCE_DIR}/cmake/utils/common.cmake)
369

@@ -39,18 +12,19 @@ set_default_values(
3912
# CMake project namespace:
4013
# All the libs should have a alias with this namespace
4114
# [NOTE] Change this to your own namespace
42-
PROJECT_NAMESPACE "cmake-project-namespace::"
15+
PROJECT_NAMESPACE "_template_project_name_::"
4316
)
4417

45-
# Compiler configurations for cpp, e.g., g++, clang++, msvc(cl), etc.
46-
include(${PROJECT_SOURCE_DIR}/cmake/compilers/cxx-compiler-configs.cmake)
47-
4818
# Utility functions for installing libraries:
4919
include(${PROJECT_SOURCE_DIR}/cmake/utils/lib-install.cmake)
5020

21+
# Compiler configurations for cpp, e.g., g++, clang++, msvc(cl), etc.
22+
include(${PROJECT_SOURCE_DIR}/cmake/compilers/cxx-compiler-configs.cmake)
23+
5124
# Compiler configurations for Python and PyTorch(with CUDA):
5225
include(${PROJECT_SOURCE_DIR}/cmake/compilers/cuda-compiler-configs.cmake)
5326
include(${PROJECT_SOURCE_DIR}/cmake/libraries/libpython.cmake)
5427
include(${PROJECT_SOURCE_DIR}/cmake/libraries/libtorch.cmake)
5528

56-
add_subdirectory(${PROJECT_SOURCE_DIR}/lib)
29+
add_subdirectory(${PROJECT_SOURCE_DIR}/lib)
30+
add_subdirectory(${PROJECT_SOURCE_DIR}/test)

csrc/cmake/cmake-parameters.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# CMake Parameters
2+
3+
## Required Parameters
4+
5+
- `[ENV]{CUDA_HOME}`/`[ENV]{CUDA_DIR}`: CUDA toolkit installation path.
6+
See: [cmake/libraries/cuda.cmake](../cmake/libraries/libcuda.cmake).
7+
8+
## Optional Parameters
9+
10+
- `CMAKE_CUDA_STANDARD`: CUDA standard.
11+
See: [cmake/compilers/cuda-compiler-configs.cmake](../cmake/compilers/cuda-compiler-configs.cmake).
12+
Default: *Auto-Detect*.
13+
Possible values: `14`, `17`, `20`, `23`.
14+
- `CMAKE_CXX_SCAN_FOR_MODULES`: Whether to use C++ modules.
15+
See: [cmake/compilers/cxx-compiler-configs.cmake](../cmake/compilers/cxx-compiler-configs.cmake).
16+
Default: `OFF`.
17+
Possible values: `ON`, `OFF`.
18+
- `CMAKE_CXX_STANDARD`: C++ standard.
19+
See: [cmake/compilers/cxx-compiler-configs.cmake](../cmake/compilers/cxx-compiler-configs.cmake).
20+
Default: *Auto-Detect*.
21+
Possible values: `14`, `17`, `20`, `23`.
22+
- `CMAKE_EXECUTABLE_SUFFIX`: Executable suffix.
23+
See: [CMakelists.txt](../CMakeLists.txt).
24+
Default: `.exe`.
25+
- `ENV{CC}`: C compiler.
26+
See: [cmake/compilers/cxx-compiler-configs.cmake](../cmake/compilers/cxx-compiler-configs.ckmake).
27+
Default: *Auto-Detect*.
28+
Possible values: `gcc`, `clang`, `cl`, etc.
29+
- `ENV{CXX}`: C++ compiler.
30+
See: [cmake/compilers/cxx-compiler-configs.cmake](../cmake/compilers/cxx-compiler-configs.cmake).
31+
Default: *Auto-Detect*.
32+
Possible values: `gcc++`, `clang++`, `cl`, etc.
33+
- `ENV{NVCC_CCBIN}`: CUDA compiler bindir.
34+
See: [cmake/compilers/cuda-compiler-configs.cmake](../cmake/compilers/cuda-compiler-configs.cmake).
35+
Default: *Auto-Detect*.
36+
- `PROJECT_NAMESPACE`: Project namespace; Library targets should have an alias with project namespace as the prefix.
37+
See: [CMakelists.txt](../CMakeLists.txt).
38+
Default: `_template_project_name_`;
39+
- `STACK_SIZE`: Stack size for the executable targets (if any).
40+
See: [cmake/compilers/cxx-compiler-configs.cmake](../cmake/compilers/cxx-compiler-configs.cmake).
41+
Default: `1048576` (Bytes).

csrc/cmake/compilers/cxx-compiler-configs.cmake

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
# @brief Compiler configurations for the host.
44
#
55
# @note Several parameters SHOULD be set BEFORE including this file:
6-
# - `ENV:CXX`: C++ Compiler. Default: auto-detected.
6+
# - `ENV{CXX}`: C++ Compiler. Default: auto-detected.
77
# - `CMAKE_CXX_STANDARD`: C++ Standard. Default: 20.
8+
# - `CMAKE_CXX_SCAN_FOR_MODULES`: Whether to use modules. Default: OFF.
89
# - `STACK_SIZE`: Stack size for the executable. Default: 1048576 (1MB).
910
# ==================================================================================================
1011

11-
include(${PROJECT_SOURCE_DIR}/cmake/utils/logging.cmake)
12+
include(${PROJECT_SOURCE_DIR}/cmake/utils/common.cmake)
1213

14+
set_default_values(CMAKE_CXX_SCAN_FOR_MODULES OFF)
1315
enable_language(CXX)
1416

1517
# Generate compile_commands.json in build directory
@@ -37,7 +39,7 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
3739
string(APPEND CMAKE_CXX_FLAGS_DEBUG " -g")
3840
# Set stack size
3941
if (WIN32)
40-
string(APPEND CMAKE_EXE_LINKER_FLAGS " -Wl,--stack,${STACK_SIZE}")
42+
string(APPEND CMAKE_EXE_LINKER_FLAGS " -Wl,-stack,${STACK_SIZE}")
4143
else()
4244
string(APPEND CMAKE_EXE_LINKER_FLAGS " -Wl,-zstack-size=${STACK_SIZE}")
4345
endif()
@@ -52,8 +54,8 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
5254
else()
5355
string(APPEND CMAKE_EXE_LINKER_FLAGS " -Wl,-zstack-size=${STACK_SIZE}")
5456
endif()
55-
## [TODO] @jamesnulliu
57+
# @todo @jamesnulliu
5658
# |- Add compiler flags for other compilers
5759
else()
5860
log_fatal("Unsupported compiler")
59-
endif()
61+
endif()

csrc/cmake/libraries/libcuda.cmake

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
# @see "https://cliutils.gitlab.io/modern-cmake/chapters/packages/CUDA.html"
44
#
55
# @note Several parameters should be set before including this file:
6-
# - `ENV{CUDA_HOME}`: The path to the CUDA toolkit. For example, `/usr/local/cuda`.
6+
# - [ENV]{CUDA_HOME}/[ENV]{CUDA_DIR}:
7+
# Path to spdlog libaray installation path.
78
# ==================================================================================================
89

9-
# Include "./Logging.cmake"
10-
include(${PROJECT_SOURCE_DIR}/cmake/utils/logging.cmake)
10+
include(${CMAKE_CURRENT_LIST_DIR}/../utils/common.cmake)
1111

12-
if (NOT DEFINED ENV{CUDA_HOME})
12+
try_get_value(CUDA_HOME HINTS CUDA_HOME CUDA_DIR)
13+
if (NOT CUDA_HOME_FOUND)
1314
log_error("ENV:CUDA_HOME is not set.")
1415
endif()
1516

1617
# Append the path to the CMAKE_PREFIX_PATH
17-
set(CUDA_CMAKE_PREFIX_PATH "$ENV{CUDA_HOME}/lib64/cmake")
18+
set(CUDA_CMAKE_PREFIX_PATH "$CUDA_HOME/lib64/cmake")
1819
list(APPEND CMAKE_PREFIX_PATH ${CUDA_CMAKE_PREFIX_PATH})
1920

2021
# Find the CUDA package
2122
# @see
2223
# |- All imported targets:
2324
# |- "https://cmake.org/cmake/help/git-stage/module/FindCUDAToolkit.html#imported-targets"
24-
find_package(CUDAToolkit REQUIRED)
25+
find_package(CUDAToolkit REQUIRED)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# ==================================================================================================
2+
# @brief Enable SPDLOG support.
3+
#
4+
# @note Several parameters should be set before including this file:
5+
# - [ENV]{SPDLOG_HOME}/[ENV]{SPDLOG_DIR}:
6+
# Path to spdlog libaray installation path.
7+
# ==================================================================================================
8+
9+
include(${CMAKE_CURRENT_LIST_DIR}/../utils/common.cmake)
10+
11+
try_get_value(SPDLOG_HOME HINTS SPDLOG_HOME SPDLOG_DIR)
12+
if (NOT SPDLOG_HOME_FOUND)
13+
log_error("[ENV]{SPDLOG_HOME}/[ENV]{SPDLOG_DIR} not set.")
14+
endif()
15+
16+
set(SPDLOG_CMAKE_PREFIX_PATH "${SPDLOG_HOME}/lib/cmake")
17+
list(APPEND CMAKE_PREFIX_PATH ${SPDLOG_CMAKE_PREFIX_PATH})
18+
19+
find_package(spdlog REQUIRED)

csrc/cmake/libraries/libtorch.cmake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
include(${PROJECT_SOURCE_DIR}/cmake/utils/logging.cmake)
2-
include(${PROJECT_SOURCE_DIR}/cmake/utils/run-python.cmake)
1+
include(${CMAKE_CURRENT_LIST_DIR}/../utils/logging.cmake)
2+
include(${CMAKE_CURRENT_LIST_DIR}/../utils/run-python.cmake)
33

44
set(PY_RESULT)
55
set(PY_OUTPUT)
66
set(PY_ERROR)
77

8-
# @see "./cmake/utils/python.cmake"
8+
# @see "../utils/python.cmake"
99
run_python(
1010
"import torch;print(torch.utils.cmake_prefix_path)"
1111
PY_RESULT PY_OUTPUT PY_ERROR

0 commit comments

Comments
 (0)