Skip to content
Snippets Groups Projects
README.md 5.9 KiB
Newer Older
Xuefeng Ding's avatar
Xuefeng Ding committed
# scikit-build-cpp-python



## Why and What
Xuefeng Ding's avatar
Xuefeng Ding committed

use scikit-build-core to setup a python-cmake project that can be installed with `pip install -e .`
Xuefeng Ding's avatar
Xuefeng Ding committed

## Conclusion

Xuefeng Ding's avatar
Xuefeng Ding committed
- package name has nothing to do with the import path. pacakge named my_package is installed with `pip install .`, uninstalled with `pip uninstall my_package`, but can be used as `import kk.ss as ks`.
Xuefeng Ding's avatar
Xuefeng Ding committed
- installation of cpp files:
Xuefeng Ding's avatar
Xuefeng Ding committed
```cmake
Xuefeng Ding's avatar
Xuefeng Ding committed
install(TARGET mylib LIBRARY DESTINATION .)
Xuefeng Ding's avatar
Xuefeng Ding committed
```
and set
```
[tool.scikit-build]
wheel.install-dir = "kk/ss"
```
Xuefeng Ding's avatar
Xuefeng Ding committed
- installation of python files:
```
[tool.scikit-build.wheel.packages]
"mm/tt" = "python/myproj/simulation"

[tool.scikit-build]
wheel.exclude = ["CMakeLists.txt","*.cc","*.h"]
```
Xuefeng Ding's avatar
Xuefeng Ding committed


## special variable


- `${SKBUILD_PLATLIB_DIR}`: The original platlib directory. Anything here goes
  directly to site-packages when a wheel is installed.
- `${SKBUILD_DATA_DIR}`: The data directory. Anything here goes to the root of
  the environment when a wheel is installed (use with care).
- `${SKBUILD_HEADERS_DIR}`: The header directory. Anything in here gets
  installed to Python's header directory.
- `${SKBUILD_SCRIPTS_DIR}`: The scripts directory. Anything placed in here will
  go to `bin` (Unix) or `Scripts` (Windows).
- `${SKBUILD_METADATA_DIR}`: The dist-info directory. Licenses go in the
  `licenses` subdirectory. _Note that CMake is not run in the
  `prepare_metadata_\*` hooks, so anything written to this directory will only
  be present when writing wheels.\_
- `${SKBUILD_NULL_DIR}`: Anything installed here will not be placed in the
  wheel.

Xuefeng Ding's avatar
Xuefeng Ding committed
## style and lint check
Xuefeng Ding's avatar
Xuefeng Ding committed
### notebook output stripping
Xuefeng Ding's avatar
Xuefeng Ding committed
- use pre-commit
1. install
```bash
pip install pre-commit nbstripout
```
2. set
```yaml
repos:
Xuefeng Ding's avatar
Xuefeng Ding committed
  - repo: https://gitlab.cern.ch/pre-commit-hook-mirrors/kynan/nbstripout
    rev: 0.8.0
Xuefeng Ding's avatar
Xuefeng Ding committed
    hooks:
      - id: nbstripout
        files: \.ipynb$
```
Xuefeng Ding's avatar
Xuefeng Ding committed
3. install
Xuefeng Ding's avatar
Xuefeng Ding committed
```bash
pre-commit install
```
Xuefeng Ding's avatar
Xuefeng Ding committed
4. run and verify
```bash
pre-commit run --all-files
```
or
```bash
nox -s pre_commit
```

### c++ formating
- pre-commit + vscode extensions
- install extensions
```
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 make sure this is in `settings.json`:
```json
  "[cpp]": {
    "editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd"
  }
```
- install pre-commit hooks
Name: Black Formatter
Id: ms-python.black-formatter
Description: Formatting support for Python files using the Black formatter.
Version: 2024.4.0
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
```yaml
- repo: https://gitlab.cern.ch/pre-commit-hook-mirrors/pre-commit/mirrors-clang-format
  rev: v19.1.3
  hooks:
    - id: clang-format
      files: \.(cpp|hpp|c|h)$
```

### python formatting
- pre-commit + vscode extensions
- install vscode extensions
- install extensions
```
Name: Black Formatter
Id: ms-python.black-formatter
Description: Formatting support for Python files using the Black formatter.
Version: 2024.4.0
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
```
and update `settings.json`
```json
  "[python]": {
    "editor.codeActionsOnSave": {
      "source.organizeImports": "always"
    },
    "editor.defaultFormatter": "ms-python.black-formatter"
  },
  "isort.args": ["--profile", "black"],
```
- install pre-commit hooks
```
repos:
  - repo: https://gitlab.cern.ch/pre-commit-hook-mirrors/psf/black
    rev: 24.10.0  # Use the latest stable version
    hooks:
      - id: black
```
### c++ lint
- nox + vscode extensions
Xuefeng Ding's avatar
Xuefeng Ding committed
- 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"
```
Xuefeng Ding's avatar
Xuefeng Ding committed

### python lint
Xuefeng Ding's avatar
Xuefeng Ding committed
#### ruff: pre-commit + vscode extensions
- install pre-commit

- install extensions
#### pylint: nox + vscode extensions
Xuefeng Ding's avatar
Xuefeng Ding committed

## vscode extensions to install
- "isort", which sort python import
```
Name: isort
Id: ms-python.isort
Description: Import organization support for Python files using isort.
Version: 2023.10.1
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.isort
```
- "pylint", for complrehensive code analysis
```
Name: Pylint
Id: ms-python.pylint
Description: Linting support for Python files using Pylint.
Version: 2024.0.0
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.pylint
```