diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000000000000000000000000000000000..f60835847928161e60b0ff98afcddba12e5c5bb6 --- /dev/null +++ b/.clang-format @@ -0,0 +1,17 @@ +--- +Language: Cpp +BasedOnStyle: Google +ColumnLimit: 120 +NamespaceIndentation: All +SortIncludes: true +IndentWidth: 2 +AccessModifierOffset: -2 +PenaltyBreakComment: 30 +PenaltyExcessCharacter: 100 +AlignAfterOpenBracket: Align +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakTemplateDeclarations: Yes +ReflowComments: false +BinPackArguments: false +BinPackParameters: false diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000000000000000000000000000000000000..901f98a4211e7e6d818a829f6ccec71b98fcff57 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,62 @@ +Checks: >- + bugprone-*, + clang-analyzer-*, + concurrency-*, + cppcoreguidelines-*, + google-*, + hicpp-*, + llvm-*, + misc-*, + modernize-*, + performance-*, + portability-*, + readability-*, + -modernize-use-trailing-return-type, + -readability-identifier-length, + -readability-magic-numbers, + -cppcoreguidelines-avoid-magic-numbers, + -bugprone-easily-swappable-parameters, + -misc-include-cleaner, + -llvm-header-guard, + -modernize-macro-to-enum, + -bugprone-use-after-move, + -hicpp-invalid-access-moved, + -misc-const-correctness + +CheckOptions: + - { key: readability-identifier-naming.NamespaceCase, value: lower_case } + - { key: readability-identifier-naming.ClassCase, value: CamelCase } + - { key: readability-identifier-naming.StructCase, value: CamelCase } + - { + key: readability-identifier-naming.TemplateParameterCase, + value: aNy_CasE, + } + - { key: readability-identifier-naming.FunctionCase, value: aNy_CasE } + - { key: readability-identifier-naming.VariableCase, value: lower_case } + - { key: readability-identifier-naming.ClassMemberCase, value: lower_case } + - { key: readability-identifier-naming.ClassMemberPrefix, value: f_ } + - { key: readability-identifier-naming.MethodCase, value: CamelCase } + - { key: readability-identifier-naming.PrivateMemberPrefix, value: m_ } + - { key: readability-identifier-naming.ProtectedMemberPrefix, value: m_ } + - { key: readability-identifier-naming.EnumConstantCase, value: camelBack } + - { + key: readability-identifier-naming.ConstexprVariableCase, + value: CamelCase, + } + - { key: readability-identifier-naming.ConstexprVariablePrefix, value: k } + - { key: readability-identifier-naming.GlobalConstantCase, value: CamelCase } + - { key: readability-identifier-naming.GlobalConstantPrefix, value: f } + - { key: readability-identifier-naming.MemberConstantCase, value: CamelCase } + - { key: readability-identifier-naming.MemberConstantPrefix, value: k } + - { key: readability-identifier-naming.StaticConstantCase, value: CamelCase } + - { key: readability-identifier-naming.StaticConstantPrefix, value: k } + - { + key: misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic, + value: 1, + } + - { key: readability-function-cognitive-complexity.Threshold, value: 21 } + +WarningsAsErrors: "*" +HeaderFilterRegex: "" +FormatStyle: file +User: "" diff --git a/.vscode/settings.json b/.vscode/settings.json index 97453c13456ff049eabc31b05b49c3ffea754004..0ce852d849fdf7346be9aa95fb3585fb0b4d7971 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,5 +10,20 @@ }, "editor.formatOnSave": true, "isort.args": ["--profile", "black"], - "notebook.lineNumbers": "on" + "notebook.lineNumbers": "on", + "clangd.arguments": [ + "--log=info", + "--pretty", + "--all-scopes-completion", + "--completion-style=bundled", + "--header-insertion=iwyu", + "--header-insertion-decorators", + "--background-index", + "--clang-tidy", + "-j=20", + "--pch-storage=disk", + "--function-arg-placeholders=false", + "--compile-commands-dir=build" + ], + "clangd.path": "clangd-17" } diff --git a/README.md b/README.md index ad6f472695a53aa14da49e75b17f98a69421f94c..976a320822a81c601eb7ee5048d11deb816d4558 100644 --- a/README.md +++ b/README.md @@ -139,10 +139,74 @@ repos: ``` ### c++ lint - nox + vscode extensions +- setup nox +```python +@nox.session(venv_backend="none") +def dev(session: nox.Session) -> None: + """ + Prepare a .venv folder. + """ + + session.run(sys.executable, "-m", "venv", ".venv") + session.run( + ".venv/bin/pip", + "install", + "scikit-build-core[pyproject]", + "pybind11", + "pip>=23", + ) + session.run( + ".venv/bin/pip", + "install", + "--no-build-isolation", + "--check-build-dependencies", + "-ve.", + "-C", + "cmake.define.CMAKE_EXPORT_COMPILE_COMMANDS=1", + "-C", + "build-dir=build", + ) + +@nox.session(reuse_venv=True) +def cpp_lint(session: nox.Session) -> None: + cpp_files = glob.glob("**/*.cc", recursive=True) + session.install("clangd-tidy") + session.run("clangd-tidy", "-p", "build", *cpp_files) +``` +- vscode extensions. use `clangd` +``` +Name: clangd +Id: llvm-vs-code-extensions.vscode-clangd +Description: C/C++ completion, navigation, and insights +Version: 0.1.30 +Publisher: LLVM +VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd +``` +and set +```json +"clangd.arguments": [ + "--log=info", + "--pretty", + "--all-scopes-completion", + "--completion-style=bundled", + "--header-insertion=iwyu", + "--header-insertion-decorators", + "--background-index", + "--clang-tidy", + "-j=20", + "--pch-storage=disk", + "--function-arg-placeholders=false", + "--compile-commands-dir=build" + ], +"clangd.path": "clangd-17" +``` ### python lint -- ruff: pre-commit + vscode extensions -- pylint: nox + vscode extensions +#### ruff: pre-commit + vscode extensions +- install pre-commit + +- install extensions +#### pylint: nox + vscode extensions ## vscode extensions to install - "isort", which sort python import diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 2dedfcf75cddbecc50147ec50baa0da3c914de44..18e508b66cc1e1c17bdb48119002b8382206c162 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -4,13 +4,13 @@ target_include_directories(hello PUBLIC $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>) install(TARGETS hello LIBRARY DESTINATION .) -add_library(complex SHARED src/complex.cc) -target_include_directories(complex PUBLIC +add_library(complexf SHARED src/complexf.cc) +target_include_directories(complexf PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>) -target_link_libraries(complex PRIVATE hello) -install(TARGETS complex LIBRARY DESTINATION .) -set_target_properties(complex PROPERTIES +target_link_libraries(complexf PRIVATE hello) +install(TARGETS complexf LIBRARY DESTINATION .) +set_target_properties(complexf PROPERTIES INSTALL_RPATH "$ORIGIN" ) diff --git a/cpp/include/complex.h b/cpp/include/complex.h deleted file mode 100644 index 0ebcf62ea2790d3b720bf6e7fc668799607d0cc5..0000000000000000000000000000000000000000 --- a/cpp/include/complex.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -double complex(double a, double b); diff --git a/cpp/include/complexf.h b/cpp/include/complexf.h new file mode 100644 index 0000000000000000000000000000000000000000..4a6f20f5e838e22ed6568a0157b6237d977fa764 --- /dev/null +++ b/cpp/include/complexf.h @@ -0,0 +1,3 @@ +#pragma once + +double complexf(double a, double b); diff --git a/cpp/src/complex.cc b/cpp/src/complex.cc deleted file mode 100644 index 3114e15ef1e89e7ecc158bf197e50e46d54b4692..0000000000000000000000000000000000000000 --- a/cpp/src/complex.cc +++ /dev/null @@ -1,4 +0,0 @@ -#include "complex.h" -#include "hello.h" - -double complex(double a, double b) { return add(a, b) + 2; } diff --git a/cpp/src/complexf.cc b/cpp/src/complexf.cc new file mode 100644 index 0000000000000000000000000000000000000000..0232427f79b9c9cfd3fe6bf8eadcc8051dc9f14c --- /dev/null +++ b/cpp/src/complexf.cc @@ -0,0 +1,5 @@ +#include "complexf.h" + +#include "hello.h" + +double complexf(double a, double b) { return add(a, b) + 2; } diff --git a/noxfile.py b/noxfile.py index a611171b1034f8f8260e00c73e3cf37c24661052..f877b03da3b25ed314fb749eb04d1d78f79629a7 100644 --- a/noxfile.py +++ b/noxfile.py @@ -2,6 +2,7 @@ nox configuration file. """ +import glob import sys from pathlib import Path @@ -46,6 +47,20 @@ def dev(session: nox.Session) -> None: ) +@nox.session(reuse_venv=True) +def cpp_lint_slow(session: nox.Session) -> None: + if "cpp_lint_slow" in sys.argv: + cpp_files = glob.glob("**/*.cc", recursive=True) + session.run("clang-tidy", "-p", "build", *cpp_files) + + +@nox.session(reuse_venv=True) +def cpp_lint(session: nox.Session) -> None: + cpp_files = glob.glob("**/*.cc", recursive=True) + session.install("clangd-tidy") + session.run("clangd-tidy", "-p", "build", *cpp_files) + + @nox.session(reuse_venv=True) def rr_run(session: nox.Session) -> None: """ diff --git a/python/myproj/simulation/_csrc/CMakeLists.txt b/python/myproj/simulation/_csrc/CMakeLists.txt index 3103c4af7c3ca884a74c9b7d7b77d420dede3042..796e35d880c8556e446b36de918693a205a1d7f7 100644 --- a/python/myproj/simulation/_csrc/CMakeLists.txt +++ b/python/myproj/simulation/_csrc/CMakeLists.txt @@ -2,7 +2,7 @@ find_package(Python COMPONENTS Interpreter Development.Module REQUIRED) find_package(pybind11 CONFIG REQUIRED) pybind11_add_module(_hello MODULE _hello_python.cc) -target_link_libraries(_hello PRIVATE hello complex) +target_link_libraries(_hello PRIVATE complexf) install(TARGETS _hello DESTINATION _csrc) set_target_properties(_hello PROPERTIES INSTALL_RPATH "$ORIGIN/.." diff --git a/python/myproj/simulation/_csrc/_hello_python.cc b/python/myproj/simulation/_csrc/_hello_python.cc index 21078d9b3628fbf7fbb03fdc184a098b925648d2..594169f84a7157fc17ecf3ab22b761d28b1d4b41 100644 --- a/python/myproj/simulation/_csrc/_hello_python.cc +++ b/python/myproj/simulation/_csrc/_hello_python.cc @@ -1,12 +1,11 @@ -#include "complex.h" -#include "hello.h" #include <pybind11/pybind11.h> -namespace py = pybind11; +#include "complexf.h" +#include "hello.h" PYBIND11_MODULE(_hello, m) { - m.doc() = "pybind11 example plugin"; // optional module docstring + m.doc() = "pybind11 example plugin"; // optional module docstring m.def("add", &add, "A function which adds two numbers"); - m.def("complex", &complex, "complex(a,b) = add(a,b) + 2"); + m.def("complex", &complexf, "complex(a,b) = add(a,b) + 2"); }