|
| 1 | +# Development Guidelines |
| 2 | + |
| 3 | +## File Formatting — CRITICAL (applies to every file you create or edit) |
| 4 | + |
| 5 | +- Files must end with exactly one newline (`\n`). No trailing blank lines, no |
| 6 | + trailing whitespace on any line (including blank lines within the file). |
| 7 | +- The final character must be `\n`; the character before it must not be `\n` |
| 8 | + or a space/tab. |
| 9 | + |
| 10 | +## Language-Specific Formatting |
| 11 | + |
| 12 | +### C++ |
| 13 | + |
| 14 | +- clang-format (`.clang-format`): 100-char line limit, 2-space indentation |
| 15 | +- clang-tidy (`.clang-tidy`): follow all recommendations |
| 16 | +- Header files: `.hpp`; implementation files: `.cpp` |
| 17 | +- Test files: `*_test.cpp` or descriptive names |
| 18 | + |
| 19 | +### Python |
| 20 | + |
| 21 | +- ruff for formatting and linting (`pyproject.toml`) |
| 22 | +- Google-style docstrings; 99-char line limit; double quotes; type hints |
| 23 | +- Test files: `test_*.py` |
| 24 | +- Do not name files after standard library modules (e.g., avoid `types.py`) |
| 25 | + |
| 26 | +### CMake |
| 27 | + |
| 28 | +- gersemi or cmake-format: `dangle_align: "child"`, `dangle_parens: true` |
| 29 | + |
| 30 | +### Jsonnet |
| 31 | + |
| 32 | +- jsonnetfmt for formatting; CI enforces this |
| 33 | +- Used for workflow configs in `test/` and elsewhere |
| 34 | + |
| 35 | +### Markdown |
| 36 | + |
| 37 | +- MD012: no multiple consecutive blank lines |
| 38 | +- MD022: headings surrounded by one blank line |
| 39 | +- MD031: fenced code blocks surrounded by one blank line |
| 40 | +- MD032: lists surrounded by one blank line |
| 41 | +- MD034: no bare URLs — use `[text](url)` |
| 42 | +- MD036: use `#` headings, not `**Bold**` for titles |
| 43 | +- MD040: always specify code block language |
| 44 | + |
| 45 | +## Naming Conventions |
| 46 | + |
| 47 | +### C++ |
| 48 | + |
| 49 | +- `lowercase_snake_case` for types, classes, functions, variables |
| 50 | +- `UPPER_CASE` for macros and constants |
| 51 | +- Namespaces: `phlex::` (core), `phlex::experimental::` (experimental) |
| 52 | + |
| 53 | +### Python |
| 54 | + |
| 55 | +- PEP 8; descriptive test function names |
| 56 | + |
| 57 | +## Comments and Documentation |
| 58 | + |
| 59 | +- Explain *why*, not *what* or *how* — code is self-documenting for those |
| 60 | +- No temporal markers (`NEW:`, `CHANGED:`) — git history tracks changes |
| 61 | +- Remove dead code; do not comment it out |
| 62 | +- If an expected feature is absent, explain why (e.g., "Single-threaded |
| 63 | + context; locks unnecessary") |
| 64 | + |
| 65 | +## Git Workflow |
| 66 | + |
| 67 | +### Branch Creation |
| 68 | + |
| 69 | +Do not set upstream tracking at branch creation time: |
| 70 | + |
| 71 | +```bash |
| 72 | +git checkout -b feature/my-feature # no --track |
| 73 | +git switch --no-track -c feature/my-feature # alternative |
| 74 | +``` |
| 75 | + |
| 76 | +Set tracking only when pushing for the first time: |
| 77 | + |
| 78 | +```bash |
| 79 | +git push -u origin feature/my-feature |
| 80 | +``` |
| 81 | + |
| 82 | +### Pull Requests |
| 83 | + |
| 84 | +- Pass all CI checks; follow coding guidelines |
| 85 | +- Minimize changes — keep PRs focused |
| 86 | +- If changes affect `phlex-examples`, create an issue there (or in `phlex` |
| 87 | + if not possible), and notify the user |
| 88 | +- If changes require documentation updates in `phlex-design`, create an issue |
| 89 | + there (or in `phlex` if not possible) |
| 90 | +- If changes affect Spack recipes in `phlex-spack-recipes`, create an issue |
| 91 | + there (or in `phlex` if not possible) |
| 92 | + |
| 93 | +## Build and Test Workflow |
| 94 | + |
| 95 | +### Environment Setup |
| 96 | + |
| 97 | +Always source `setup-env.sh` before building or testing. This applies in all |
| 98 | +environments (devcontainer, local, HPC, CI containers): |
| 99 | + |
| 100 | +```bash |
| 101 | +# Standalone repository |
| 102 | +. scripts/setup-env.sh |
| 103 | + |
| 104 | +# Multi-project workspace (workspace root contains setup-env.sh) |
| 105 | +. srcs/phlex/scripts/setup-env.sh |
| 106 | +``` |
| 107 | + |
| 108 | +### Build |
| 109 | + |
| 110 | +```bash |
| 111 | +# Preferred: use presets |
| 112 | +cmake --preset default -S . -B build |
| 113 | +ninja -C build |
| 114 | + |
| 115 | +# Or manually |
| 116 | +cmake -B build -S . -DCMAKE_BUILD_TYPE=RelWithDebInfo -G Ninja |
| 117 | +cmake --build build -j $(nproc) |
| 118 | +``` |
| 119 | + |
| 120 | +### Test |
| 121 | + |
| 122 | +```bash |
| 123 | +ctest --test-dir build -j $(nproc) --output-on-failure |
| 124 | +# With timeout guard (recommended): |
| 125 | +ctest --test-dir build -j $(nproc) --test-timeout 90 |
| 126 | +# Run specific tests: |
| 127 | +ctest --test-dir build -R "regex" |
| 128 | +``` |
| 129 | + |
| 130 | +### Coverage |
| 131 | + |
| 132 | +```bash |
| 133 | +# Complete workflow |
| 134 | +./scripts/coverage.sh all |
| 135 | + |
| 136 | +# Step by step |
| 137 | +./scripts/coverage.sh setup test html view |
| 138 | + |
| 139 | +# Via CMake targets (after coverage preset build) |
| 140 | +cmake --build build --target coverage-xml |
| 141 | +cmake --build build --target coverage-html |
| 142 | +``` |
| 143 | + |
| 144 | +### Pre-commit Hooks |
| 145 | + |
| 146 | +```bash |
| 147 | +# Detect available tool |
| 148 | +PREKCOMMAND=$(command -v prek || command -v pre-commit || echo "") |
| 149 | + |
| 150 | +# Install hooks (once per clone; automatic in devcontainer) |
| 151 | +$PREKCOMMAND install |
| 152 | + |
| 153 | +# Run all hooks against all files (recommended before opening a PR) |
| 154 | +$PREKCOMMAND run --all-files |
| 155 | + |
| 156 | +# Run against changed files only |
| 157 | +$PREKCOMMAND run |
| 158 | + |
| 159 | +# Update hook revisions |
| 160 | +$PREKCOMMAND auto-update |
| 161 | +``` |
| 162 | + |
| 163 | +`prek` is installed in `phlex-dev`; not in `phlex-ci`. Fall back to invoking |
| 164 | +formatters directly if neither is available. |
| 165 | + |
| 166 | +## Python Integration Details |
| 167 | + |
| 168 | +- `PYTHONPATH` should include only paths containing user Python modules loaded |
| 169 | + by Phlex (source dir and build output dir). Do not append system/Spack/venv |
| 170 | + `site-packages`. |
| 171 | +- Type conversion in `plugins/python/src/modulewrap.cpp` uses substring |
| 172 | + matching on type names — this is brittle. Ensure converters exist for all |
| 173 | + types used in tests. Exact matches required (`numpy.float32 != float`). |
| 174 | +- Python test structure: C++ driver provides data streams, Jsonnet config |
| 175 | + wires the graph, Python script implements algorithms. |
| 176 | + |
| 177 | +## Memory Management |
| 178 | + |
| 179 | +- `std::shared_ptr` for shared ownership; `std::unique_ptr` for exclusive |
| 180 | +- Raw pointers only for non-owning references |
| 181 | +- Python/C++ interop: manual `Py_INCREF`/`Py_DECREF`; `PyGILRAII` for GIL |
| 182 | +- GC-tracked Python types: `Py_TPFLAGS_HAVE_GC`, implement `tp_traverse` and |
| 183 | + `tp_clear`, call `PyObject_GC_UnTrack` before deallocation |
| 184 | + |
| 185 | +## Error Handling |
| 186 | + |
| 187 | +- C++: `std::runtime_error` for runtime failures; propagate Python exceptions |
| 188 | + as `std::runtime_error` |
| 189 | +- Python C API: set exceptions with `PyErr_SetString`/`PyErr_Format`; return |
| 190 | + `nullptr` on error; clear with `PyErr_Clear()` when recovering |
| 191 | + |
| 192 | +## External Software Versions |
| 193 | + |
| 194 | +When specifying versions or commit hashes for external software (e.g., GitHub |
| 195 | +Actions), always verify against the official release source: |
| 196 | + |
| 197 | +1. Check existing workflows in `.github/workflows/` for the version currently |
| 198 | + in use |
| 199 | +2. Verify whether a newer version exists at the official releases page |
| 200 | +3. The authoritative source always takes precedence over training data |
| 201 | + |
| 202 | +## Platform-Specific Code |
| 203 | + |
| 204 | +```cpp |
| 205 | +#if __linux__ |
| 206 | +constexpr double mem_denominator{1e3}; |
| 207 | +#else // AppleClang |
| 208 | +constexpr double mem_denominator{1e6}; |
| 209 | +#endif |
| 210 | +``` |
| 211 | + |
| 212 | +Use POSIX APIs where available; document platform requirements. |
| 213 | + |
| 214 | +## Searching Source Directories |
| 215 | + |
| 216 | +If the workspace root contains a `srcs/` directory with symbolic links, always |
| 217 | +use `find -L` or `find -follow` to ensure linked directories are traversed. |
0 commit comments