|
| 1 | +# Build System |
| 2 | + |
| 3 | +## 1. You Run |
| 4 | + |
| 5 | + pip install -e . |
| 6 | + |
| 7 | +This tells `pip` to: |
| 8 | + |
| 9 | +- Perform an **editable install** of the current project. |
| 10 | +- Use the `pyproject.toml` as the **single source of truth** for building and packaging. |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## 2. `pip` Reads `pyproject.toml` |
| 15 | + |
| 16 | +It sees: |
| 17 | + |
| 18 | + [build-system] |
| 19 | + build-backend = "scikit_build_core.build" |
| 20 | + |
| 21 | +So `pip` uses **`scikit-build-core`** as the **build backend** (instead of legacy `setuptools`). |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## 3. `scikit-build-core` Invokes CMake |
| 26 | + |
| 27 | +It starts a CMake build, just like if you had run: |
| 28 | + |
| 29 | + cmake -S . -B build |
| 30 | + cmake --build build |
| 31 | + |
| 32 | +It uses the `CMakeLists.txt` at the project root, which: |
| 33 | + |
| 34 | +- Declares the project: |
| 35 | + |
| 36 | + project(rcs LANGUAGES C CXX VERSION 0.4.0) |
| 37 | + |
| 38 | +- Sets modern C++20 and compiler policies. |
| 39 | +- Locates external dependencies: |
| 40 | + - `Eigen3`, `Python3`, `MuJoCo`, `pinocchio` |
| 41 | +- Downloads with `FetchContent`: `rl`, `pybind11` |
| 42 | +- Includes: |
| 43 | + |
| 44 | + add_subdirectory(src) |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +## 4. CMake Enters `src/CMakeLists.txt` |
| 49 | + |
| 50 | + add_subdirectory(rcs) |
| 51 | + target_include_directories(rcs INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) |
| 52 | + add_subdirectory(sim) |
| 53 | + add_subdirectory(pybind) |
| 54 | + |
| 55 | +This loads and builds 3 subcomponents. |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +## 5. Subcomponent Builds |
| 60 | + |
| 61 | +### `src/rcs/CMakeLists.txt` |
| 62 | + |
| 63 | + add_library(rcs SHARED) |
| 64 | + target_sources(rcs PRIVATE Pose.cpp Robot.cpp IK.cpp utils.cpp) |
| 65 | + target_link_libraries(rcs PUBLIC Eigen3::Eigen mdl pinocchio::all) |
| 66 | + |
| 67 | +- Builds a **shared C++ library**: `rcs` |
| 68 | +- Contains your **robot control logic** |
| 69 | +- Exposes `include/` headers |
| 70 | +- Linked against external libraries |
| 71 | + |
| 72 | +### `src/sim/CMakeLists.txt` |
| 73 | + |
| 74 | + add_library(sim) |
| 75 | + target_sources(sim PRIVATE sim.cpp SimRobot.cpp ...) |
| 76 | + target_link_libraries(sim PUBLIC rcs MuJoCo::MuJoCo) |
| 77 | + |
| 78 | +- Builds a **C++ simulation library** |
| 79 | +- Depends on: |
| 80 | + - Your own `rcs` library |
| 81 | + - MuJoCo physics engine |
| 82 | + |
| 83 | +### `src/pybind/CMakeLists.txt` |
| 84 | + |
| 85 | + pybind11_add_module(_core MODULE rcs.cpp) |
| 86 | + target_link_libraries(_core PRIVATE sim rcs) |
| 87 | + |
| 88 | +- Compiles a Python extension module: `_core.so` |
| 89 | +- Uses `pybind11` to bind C++ classes/functions |
| 90 | +- Links to both `sim` and `rcs` native libraries |
| 91 | +- Adds install instructions: |
| 92 | + |
| 93 | + install(TARGETS _core rcs DESTINATION rcs COMPONENT python_package) |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## 6. Packaging into Python |
| 98 | + |
| 99 | +From `pyproject.toml`: |
| 100 | + |
| 101 | + [tool.scikit-build] |
| 102 | + build.targets = ["_core", "scenes", "rcs"] |
| 103 | + wheel.packages = ["python/rcs"] |
| 104 | + install.components = ["python_package"] |
| 105 | + |
| 106 | +- `scikit-build-core` installs `_core.so` into: |
| 107 | + |
| 108 | + python/rcs/_core.so |
| 109 | + |
| 110 | +- That directory becomes a valid **Python package** |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## 7. Result: Python Import Works |
| 115 | + |
| 116 | +After install: |
| 117 | + |
| 118 | + from rcs import _core |
| 119 | + |
| 120 | +- You now access the C++ functionality exposed in `rcs.cpp` through Python. |
| 121 | +- `_core` contains Python-wrapped C++ objects/functions via `pybind11`. |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +## Summary Diagram |
| 126 | + |
| 127 | + pip install -e . |
| 128 | + │ |
| 129 | + ▼ |
| 130 | + Reads pyproject.toml (scikit_build_core used) |
| 131 | + │ |
| 132 | + ▼ |
| 133 | + CMakeLists.txt (root) → add_subdirectory(src) |
| 134 | + │ |
| 135 | + ▼ |
| 136 | + src/rcs → builds C++ library `rcs` |
| 137 | + src/sim → builds C++ library `sim` |
| 138 | + src/pybind → builds Python extension `_core` |
| 139 | + │ |
| 140 | + ▼ |
| 141 | + All .so libraries installed in `python/rcs/` |
| 142 | + `_core` accessible from Python as import |
| 143 | + |
| 144 | +--- |
| 145 | + |
| 146 | +## Recap of Build Layers |
| 147 | + |
| 148 | +| Layer | Role | Tool | |
| 149 | +| --- | --- | --- | |
| 150 | +| `pyproject.toml` | Project metadata, build backend | `pip`, `scikit-build-core` | |
| 151 | +| `CMakeLists.txt` (root) | Configure the full C++ build | `CMake` | |
| 152 | +| `src/rcs` | Core robot logic in C++ | `C++`, `Eigen`, `pinocchio` | |
| 153 | +| `src/sim` | Simulation logic, MuJoCo | `C++`, `MuJoCo` | |
| 154 | +| `src/pybind` | Bindings to Python | `pybind11` | |
| 155 | +| `python/rcs/` | Python package with compiled `.so` | `scikit-build`, `pip` | |
| 156 | + |
| 157 | + |
| 158 | + |
0 commit comments