|
| 1 | +# Socket CLI Build System |
| 2 | + |
| 3 | +Complete guide to building Socket CLI from source. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +```bash |
| 8 | +# Build everything with smart caching (recommended) |
| 9 | +pnpm build |
| 10 | + |
| 11 | +# Force rebuild all packages |
| 12 | +pnpm build --force |
| 13 | + |
| 14 | +# Build CLI package only |
| 15 | +pnpm build:cli |
| 16 | + |
| 17 | +# Watch mode for development |
| 18 | +pnpm build:watch |
| 19 | +# or |
| 20 | +pnpm dev |
| 21 | +``` |
| 22 | + |
| 23 | +## What Gets Built |
| 24 | + |
| 25 | +The Socket CLI build system builds packages in this order: |
| 26 | + |
| 27 | +1. **ONNX Runtime WASM** (`@socketsecurity/onnxruntime`) |
| 28 | + - AI model execution runtime |
| 29 | + - Output: `packages/onnxruntime/dist/ort-wasm-simd.wasm` |
| 30 | + |
| 31 | +2. **Yoga WASM** (`@socketsecurity/yoga`) |
| 32 | + - Terminal layout engine |
| 33 | + - Output: `packages/yoga/dist/yoga.wasm` |
| 34 | + |
| 35 | +3. **CLI Package** (`@socketsecurity/cli`) |
| 36 | + - Main CLI application |
| 37 | + - Output: `packages/cli/dist/index.js` |
| 38 | + |
| 39 | +4. **SEA Binary** (`@socketbin/node-sea-builder-builder`) |
| 40 | + - Single Executable Application (Node.js + CLI bundled) |
| 41 | + - Output: `packages/socketbin-node-sea-builder-builder/bin/socket` |
| 42 | + |
| 43 | +## Build Commands |
| 44 | + |
| 45 | +### Root Level Commands |
| 46 | + |
| 47 | +| Command | Description | |
| 48 | +|---------|-------------| |
| 49 | +| `pnpm build` | Smart build with caching (skips unchanged packages) | |
| 50 | +| `pnpm build --force` | Force rebuild everything | |
| 51 | +| `pnpm build --target <name>` | Build specific target (see targets below) | |
| 52 | +| `pnpm build --targets <t1,t2,...>` | Build multiple targets | |
| 53 | +| `pnpm build --platforms` | Build all platform binaries (8 platforms) | |
| 54 | +| `pnpm build --platforms --parallel` | Build platforms in parallel (faster) | |
| 55 | +| `pnpm build:cli` | Build just the CLI package | |
| 56 | +| `pnpm build:watch` | Watch mode for development | |
| 57 | + |
| 58 | +### CLI Package Commands |
| 59 | + |
| 60 | +```bash |
| 61 | +cd packages/cli |
| 62 | + |
| 63 | +# Build CLI |
| 64 | +pnpm build |
| 65 | + |
| 66 | +# Force rebuild with clean |
| 67 | +pnpm build --force |
| 68 | + |
| 69 | +# Watch mode |
| 70 | +pnpm build --watch |
| 71 | +``` |
| 72 | + |
| 73 | +## Build Targets |
| 74 | + |
| 75 | +Available targets for `pnpm build --target <name>`: |
| 76 | + |
| 77 | +### Primary Targets |
| 78 | +- `cli` - CLI package only |
| 79 | +- `sea` - SEA binary builder |
| 80 | +- `node` - Node.js smol builder |
| 81 | +- `socket` - Socket package (bootstrap wrapper) |
| 82 | +- `bootstrap` - Bootstrap package |
| 83 | + |
| 84 | +### Platform Binaries |
| 85 | +- `darwin-arm64` - macOS Apple Silicon |
| 86 | +- `darwin-x64` - macOS Intel |
| 87 | +- `linux-arm64` - Linux ARM64 |
| 88 | +- `linux-x64` - Linux x64 |
| 89 | +- `alpine-arm64` - Alpine Linux ARM64 |
| 90 | +- `alpine-x64` - Alpine Linux x64 |
| 91 | +- `win32-arm64` - Windows ARM64 |
| 92 | +- `win32-x64` - Windows x64 |
| 93 | + |
| 94 | +### Examples |
| 95 | + |
| 96 | +```bash |
| 97 | +# Build just the CLI |
| 98 | +pnpm build --target cli |
| 99 | + |
| 100 | +# Build multiple targets |
| 101 | +pnpm build --targets cli,sea |
| 102 | + |
| 103 | +# Build all platform binaries sequentially |
| 104 | +pnpm build --platforms |
| 105 | + |
| 106 | +# Build all platform binaries in parallel (faster) |
| 107 | +pnpm build --platforms --parallel |
| 108 | +``` |
| 109 | + |
| 110 | +## Build Features |
| 111 | + |
| 112 | +### Intelligent Caching |
| 113 | + |
| 114 | +The build system automatically skips packages that are already built and haven't changed: |
| 115 | + |
| 116 | +```bash |
| 117 | +pnpm build |
| 118 | +# First run: Builds all 4 packages (~2-5 minutes) |
| 119 | + |
| 120 | +pnpm build |
| 121 | +# Second run: Skips all unchanged packages (< 1 second) |
| 122 | +``` |
| 123 | + |
| 124 | +To force rebuild: |
| 125 | + |
| 126 | +```bash |
| 127 | +pnpm build --force |
| 128 | +``` |
| 129 | + |
| 130 | +### Watch Mode |
| 131 | + |
| 132 | +For active development, use watch mode to automatically rebuild on changes: |
| 133 | + |
| 134 | +```bash |
| 135 | +pnpm build:watch |
| 136 | +# or |
| 137 | +pnpm dev |
| 138 | +``` |
| 139 | + |
| 140 | +This watches for changes in the CLI package and automatically rebuilds. |
| 141 | + |
| 142 | +## Build Output |
| 143 | + |
| 144 | +### Directory Structure |
| 145 | + |
| 146 | +``` |
| 147 | +packages/ |
| 148 | +├── cli/ |
| 149 | +│ ├── dist/ |
| 150 | +│ │ ├── index.js # Main CLI bundle |
| 151 | +│ │ ├── cli.js # CLI core (compressed) |
| 152 | +│ │ └── cli.js.bz # Brotli compressed CLI |
| 153 | +│ └── build/ |
| 154 | +│ ├── cli.js # Pre-compression CLI bundle |
| 155 | +│ ├── yoga-sync.mjs # Yoga WASM loader |
| 156 | +│ └── onnx-sync.mjs # ONNX WASM loader |
| 157 | +│ |
| 158 | +├── onnxruntime/ |
| 159 | +│ └── dist/ |
| 160 | +│ └── ort-wasm-simd.wasm |
| 161 | +│ |
| 162 | +├── yoga/ |
| 163 | +│ └── dist/ |
| 164 | +│ └── yoga.wasm |
| 165 | +│ |
| 166 | +└── socketbin-node-sea-builder-builder/ |
| 167 | + └── bin/ |
| 168 | + └── socket # SEA binary |
| 169 | +``` |
| 170 | + |
| 171 | +### Build Artifacts |
| 172 | + |
| 173 | +The CLI build process creates these artifacts: |
| 174 | + |
| 175 | +1. **TypeScript Compilation** - `.mts` → `.js` |
| 176 | +2. **Bundling** - All code bundled into single file with esbuild |
| 177 | +3. **WASM Extraction** - Yoga and ONNX WASM files extracted |
| 178 | +4. **Compression** - Brotli compression for distribution |
| 179 | +5. **Checksums** - SHA256 checksums for verification |
| 180 | + |
| 181 | +## Build Time Estimates |
| 182 | + |
| 183 | +| Build Type | Time | Disk Space | |
| 184 | +|------------|------|------------| |
| 185 | +| CLI only (cached) | < 1s | N/A | |
| 186 | +| CLI only (fresh) | 30-60s | ~50 MB | |
| 187 | +| Full build (cached) | < 1s | N/A | |
| 188 | +| Full build (fresh) | 2-5 min | ~200 MB | |
| 189 | +| Platform binaries (sequential) | 30-60 min | ~1 GB | |
| 190 | +| Platform binaries (parallel) | 10-20 min | ~1 GB | |
| 191 | + |
| 192 | +## Setup Requirements |
| 193 | + |
| 194 | +### Development Dependencies |
| 195 | + |
| 196 | +Install dependencies: |
| 197 | + |
| 198 | +```bash |
| 199 | +pnpm install |
| 200 | +``` |
| 201 | + |
| 202 | +### Platform-Specific Tools |
| 203 | + |
| 204 | +See [Build Toolchain Setup](build-toolchain-setup.md) for platform-specific installation guides. |
| 205 | + |
| 206 | +**Quick check:** |
| 207 | + |
| 208 | +```bash |
| 209 | +# Verify you have required tools |
| 210 | +node --version # >=18 |
| 211 | +pnpm --version # >=10.16.0 |
| 212 | +``` |
| 213 | + |
| 214 | +## Build Configuration |
| 215 | + |
| 216 | +### Environment Variables |
| 217 | + |
| 218 | +Configure builds with environment variables: |
| 219 | + |
| 220 | +```bash |
| 221 | +# Published build (production optimizations) |
| 222 | +INLINED_SOCKET_CLI_PUBLISHED_BUILD=1 pnpm build |
| 223 | + |
| 224 | +# Legacy build (compatibility mode) |
| 225 | +INLINED_SOCKET_CLI_LEGACY_BUILD=1 pnpm build |
| 226 | + |
| 227 | +# Sentry build (with error tracking) |
| 228 | +INLINED_SOCKET_CLI_SENTRY_BUILD=1 pnpm build |
| 229 | + |
| 230 | +# No minification (for debugging) |
| 231 | +SOCKET_CLI_NO_MINIFY=1 pnpm build |
| 232 | + |
| 233 | +# Force build (skip cache) |
| 234 | +SOCKET_CLI_FORCE_BUILD=1 pnpm build |
| 235 | +``` |
| 236 | + |
| 237 | +### Build Scripts |
| 238 | + |
| 239 | +The build system consists of: |
| 240 | + |
| 241 | +- **Root**: `scripts/build.mjs` - Orchestrates full build with caching |
| 242 | +- **CLI**: `packages/cli/scripts/build.mjs` - Builds CLI package |
| 243 | +- **esbuild**: `packages/cli/.config/esbuild.cli.build.mjs` - Bundle configuration |
| 244 | + |
| 245 | +## Troubleshooting |
| 246 | + |
| 247 | +### Build Fails with "Module not found" |
| 248 | + |
| 249 | +**Solution**: Ensure dependencies are installed: |
| 250 | + |
| 251 | +```bash |
| 252 | +pnpm install |
| 253 | +``` |
| 254 | + |
| 255 | +### Build is Slow |
| 256 | + |
| 257 | +**Solution**: Use caching and parallel builds: |
| 258 | + |
| 259 | +```bash |
| 260 | +# Smart caching (only rebuilds changed packages) |
| 261 | +pnpm build |
| 262 | + |
| 263 | +# Parallel platform builds |
| 264 | +pnpm build --platforms --parallel |
| 265 | +``` |
| 266 | + |
| 267 | +### "Command not found: pnpm" |
| 268 | + |
| 269 | +**Solution**: Install pnpm: |
| 270 | + |
| 271 | +```bash |
| 272 | +npm install -g pnpm@latest |
| 273 | +``` |
| 274 | + |
| 275 | +### Clean Build After git pull |
| 276 | + |
| 277 | +**Solution**: Force rebuild: |
| 278 | + |
| 279 | +```bash |
| 280 | +pnpm build --force |
| 281 | +``` |
| 282 | + |
| 283 | +### WASM Files Missing |
| 284 | + |
| 285 | +**Solution**: Build will automatically extract WASM files, but you can manually run: |
| 286 | + |
| 287 | +```bash |
| 288 | +cd packages/cli |
| 289 | +node scripts/extract-yoga-wasm.mjs |
| 290 | +``` |
| 291 | + |
| 292 | +## Related Documentation |
| 293 | + |
| 294 | +- [Build/Dist Structure](build-dist-structure.md) - Output directory structure |
| 295 | +- [Caching Strategy](caching-strategy.md) - How caching works |
| 296 | +- [WASM Build Guide](wasm-build-guide.md) - Building WASM packages |
| 297 | +- [Node.js Build Quick Reference](node-build-quick-reference.md) - Building custom Node.js |
| 298 | +- [Node.js Patch Creation Guide](node-patch-creation-guide.md) - Creating Node.js patches |
| 299 | + |
| 300 | +## Advanced Topics |
| 301 | + |
| 302 | +### Building Custom Node.js Binaries |
| 303 | + |
| 304 | +See the comprehensive [yao-pkg Build Guide](../guides/yao-pkg-build.md) for building custom Node.js binaries with Socket patches. |
| 305 | + |
| 306 | +### Platform-Specific Builds |
| 307 | + |
| 308 | +Build for specific platforms: |
| 309 | + |
| 310 | +```bash |
| 311 | +# macOS Apple Silicon |
| 312 | +pnpm build --target darwin-arm64 |
| 313 | + |
| 314 | +# Linux x64 |
| 315 | +pnpm build --target linux-x64 |
| 316 | + |
| 317 | +# Windows x64 |
| 318 | +pnpm build --target win32-x64 |
| 319 | +``` |
| 320 | + |
| 321 | +### SEA Binary Build |
| 322 | + |
| 323 | +Build the Single Executable Application: |
| 324 | + |
| 325 | +```bash |
| 326 | +# Via target |
| 327 | +pnpm build --target sea |
| 328 | + |
| 329 | +# Via CLI package |
| 330 | +cd packages/cli |
| 331 | +pnpm build --sea |
| 332 | +``` |
| 333 | + |
| 334 | +### CI/CD Integration |
| 335 | + |
| 336 | +For CI/CD pipelines: |
| 337 | + |
| 338 | +```bash |
| 339 | +# Install dependencies |
| 340 | +pnpm install --frozen-lockfile |
| 341 | + |
| 342 | +# Build everything |
| 343 | +pnpm build |
| 344 | + |
| 345 | +# Verify build |
| 346 | +pnpm check |
| 347 | +pnpm test |
| 348 | +``` |
| 349 | + |
| 350 | +## Help |
| 351 | + |
| 352 | +For more help: |
| 353 | + |
| 354 | +```bash |
| 355 | +# Show build system help |
| 356 | +pnpm build --help |
| 357 | + |
| 358 | +# Show available targets |
| 359 | +pnpm build --help | grep -A20 "Available Targets" |
| 360 | +``` |
0 commit comments