1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
|
# Tickborg — Build Executor
## Overview
The **build executor** is the component responsible for actually running builds
of sub-projects in the Project Tick monorepo. Unlike the original ofborg which
used `nix-build` exclusively, tickborg's build executor supports multiple build
systems: CMake, Meson, Autotools, Cargo, Gradle, Make, and custom commands.
The build executor is invoked by the **builder** binary
(`tickborg/src/bin/builder.rs`) which consumes `BuildJob` messages from
architecture-specific queues.
---
## Key Source Files
| File | Purpose |
|------|---------|
| `tickborg/src/buildtool.rs` | Build system abstraction, `BuildExecutor`, `ProjectBuildConfig` |
| `tickborg/src/tasks/build.rs` | `BuildWorker`, `JobActions` — the task implementation |
| `tickborg/src/bin/builder.rs` | Binary entry point |
| `tickborg/src/asynccmd.rs` | Async subprocess execution |
---
## Build System Abstraction
### `BuildSystem` Enum
```rust
// tickborg/src/buildtool.rs
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum BuildSystem {
CMake,
Meson,
Autotools,
Cargo,
Gradle,
Make,
Custom { command: String },
}
```
Each variant corresponds to a well-known build system with a standard
invocation pattern.
### `ProjectBuildConfig`
```rust
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ProjectBuildConfig {
pub name: String,
pub path: String,
pub build_system: BuildSystem,
pub build_timeout_seconds: u16,
pub configure_args: Vec<String>,
pub build_args: Vec<String>,
pub test_command: Option<Vec<String>>,
}
```
Each sub-project in the monorepo has a `ProjectBuildConfig` that specifies:
- **name**: Human-readable project name (e.g., `"meshmc"`, `"mnv"`)
- **path**: Relative path within the repository
- **build_system**: Which build system to use
- **build_timeout_seconds**: Maximum time allowed for the build
- **configure_args**: Arguments passed to the configure step
- **build_args**: Arguments passed to the build step
- **test_command**: Custom test command (overrides the default for the build system)
### `BuildExecutor`
```rust
#[derive(Clone, Debug)]
pub struct BuildExecutor {
pub build_timeout: u16,
}
impl BuildExecutor {
pub fn new(build_timeout: u16) -> Self {
Self { build_timeout }
}
}
```
The `BuildExecutor` is created from the configuration with a minimum timeout
of 300 seconds:
```rust
// config.rs
impl Config {
pub fn build_executor(&self) -> BuildExecutor {
if self.build.build_timeout_seconds < 300 {
error!(?self.build.build_timeout_seconds,
"Please set build_timeout_seconds to at least 300");
panic!();
}
BuildExecutor::new(self.build.build_timeout_seconds)
}
}
```
---
## Build Commands Per System
### CMake
```rust
fn build_command(&self, project_dir: &Path, config: &ProjectBuildConfig) -> Command {
let build_dir = project_dir.join("build");
let mut cmd = Command::new("cmake");
cmd.arg("--build").arg(&build_dir);
cmd.args(["--config", "Release"]);
for arg in &config.build_args { cmd.arg(arg); }
cmd.current_dir(project_dir);
cmd
}
```
Test command (default):
```rust
let mut cmd = Command::new("ctest");
cmd.arg("--test-dir").arg("build");
cmd.args(["--output-on-failure"]);
```
### Meson
```rust
let mut cmd = Command::new("meson");
cmd.arg("compile");
cmd.args(["-C", "build"]);
```
Test:
```rust
let mut cmd = Command::new("meson");
cmd.arg("test").args(["-C", "build"]);
```
### Autotools / Make
```rust
let mut cmd = Command::new("make");
cmd.args(["-j", &num_cpus().to_string()]);
```
Test:
```rust
let mut cmd = Command::new("make");
cmd.arg("check");
```
### Cargo
```rust
let mut cmd = Command::new("cargo");
cmd.arg("build").arg("--release");
```
Test:
```rust
let mut cmd = Command::new("cargo");
cmd.arg("test");
```
### Gradle
```rust
let gradlew = project_dir.join("gradlew");
let prog = if gradlew.exists() {
gradlew.to_string_lossy().to_string()
} else {
"gradle".to_string()
};
let mut cmd = Command::new(prog);
cmd.arg("build");
```
Gradle prefers the wrapper (`gradlew`) if present.
### Custom
```rust
let mut cmd = Command::new("sh");
cmd.args(["-c", command]);
```
---
## Build Execution Methods
### Synchronous Build
```rust
impl BuildExecutor {
pub fn build_project(
&self, project_root: &Path, config: &ProjectBuildConfig,
) -> Result<fs::File, fs::File> {
let project_dir = project_root.join(&config.path);
let cmd = self.build_command(&project_dir, config);
self.run(cmd, true)
}
}
```
Returns `Ok(File)` with stdout/stderr on success, `Err(File)` on failure.
The `File` contains the captured output.
### Asynchronous Build
```rust
impl BuildExecutor {
pub fn build_project_async(
&self, project_root: &Path, config: &ProjectBuildConfig,
) -> SpawnedAsyncCmd {
let project_dir = project_root.join(&config.path);
let cmd = self.build_command(&project_dir, config);
AsyncCmd::new(cmd).spawn()
}
}
```
Returns a `SpawnedAsyncCmd` that allows streaming output line-by-line.
### Test Execution
```rust
impl BuildExecutor {
pub fn test_project(
&self, project_root: &Path, config: &ProjectBuildConfig,
) -> Result<fs::File, fs::File> {
let project_dir = project_root.join(&config.path);
let cmd = self.test_command(&project_dir, config);
self.run(cmd, true)
}
}
```
If `config.test_command` is set, it is used directly. Otherwise, the default
test command for the build system is used.
---
## Async Command Execution (`asynccmd.rs`)
The `AsyncCmd` abstraction wraps `std::process::Command` to provide:
- Non-blocking output streaming via channels
- Separate stderr/stdout capture
- Exit status monitoring
```rust
pub struct AsyncCmd {
command: Command,
}
pub struct SpawnedAsyncCmd {
waiter: JoinHandle<Option<Result<ExitStatus, io::Error>>>,
rx: Receiver<String>,
}
```
### Spawning
```rust
impl AsyncCmd {
pub fn new(cmd: Command) -> AsyncCmd {
AsyncCmd { command: cmd }
}
pub fn spawn(mut self) -> SpawnedAsyncCmd {
let mut child = self.command
.stdin(Stdio::null())
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap();
// Sets up channels and monitoring threads
// ...
}
}
```
The spawn implementation:
1. Creates a `sync_channel` for output lines (buffer size: 30).
2. Spawns a reader thread for stdout.
3. Spawns a reader thread for stderr.
4. Spawns a waiter thread for each (stdout thread, stderr thread, child process).
5. Returns a `SpawnedAsyncCmd` whose `rx` receiver yields lines as they arrive.
```rust
fn reader_tx<R: 'static + Read + Send>(
read: R, tx: SyncSender<String>,
) -> thread::JoinHandle<()> {
let read = BufReader::new(read);
thread::spawn(move || {
for line in read.lines() {
let to_send = match line {
Ok(line) => line,
Err(e) => {
error!("Error reading data in reader_tx: {:?}", e);
"Non-UTF8 data omitted from the log.".to_owned()
}
};
if let Err(e) = tx.send(to_send) {
error!("Failed to send log line: {:?}", e);
}
}
})
}
```
The channel buffer size is intentionally small (30) to apply backpressure:
```rust
const OUT_CHANNEL_BUFFER_SIZE: usize = 30;
```
---
## The Builder Binary
### Entry Point
```rust
// src/bin/builder.rs
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
tickborg::setup_log();
let arg = env::args().nth(1).unwrap_or_else(|| panic!("usage: ..."));
let cfg = config::load(arg.as_ref());
let conn = easylapin::from_config(&builder_cfg.rabbitmq).await?;
let mut handles: Vec<Pin<Box<dyn Future<Output = ()> + Send>>> = Vec::new();
for system in &cfg.build.system {
handles.push(create_handle(&conn, &cfg, system.to_string()).await?);
}
future::join_all(handles).await;
}
```
The builder creates one consumer handle per configured system. This allows a
single builder process to serve multiple architectures (e.g., `x86_64-linux`
and `aarch64-linux`).
### Channel Setup
```rust
async fn create_handle(
conn: &lapin::Connection, cfg: &config::Config, system: String,
) -> Result<Pin<Box<dyn Future<Output = ()> + Send>>, Box<dyn Error>> {
let mut chan = conn.create_channel().await?;
let cloner = checkout::cached_cloner(Path::new(&cfg.checkout.root));
let build_executor = cfg.build_executor();
// Declare build-jobs exchange (Fanout)
chan.declare_exchange(/* build-jobs, Fanout */);
// Declare and bind the system-specific queue
let queue_name = format!("build-inputs-{system}");
chan.declare_queue(/* queue_name, durable */);
chan.bind_queue(/* queue_name ← build-jobs */);
// Start consuming
let handle = easylapin::NotifyChannel(chan).consume(
tasks::build::BuildWorker::new(
cloner, build_executor, system, cfg.runner.identity.clone()
),
easyamqp::ConsumeConfig {
queue: queue_name,
consumer_tag: format!("{}-builder", cfg.whoami()),
no_local: false, no_ack: false, no_wait: false, exclusive: false,
},
).await?;
Ok(handle)
}
```
### Development Mode (`build_all_jobs`)
When `runner.build_all_jobs` is set to `true`, the builder creates an
exclusive, auto-delete queue instead of the named durable one:
```rust
if cfg.runner.build_all_jobs != Some(true) {
// Normal: named durable queue
let queue_name = format!("build-inputs-{system}");
chan.declare_queue(QueueConfig { durable: true, exclusive: false, ... });
} else {
// Dev mode: ephemeral queue (receives ALL jobs)
warn!("Building all jobs, please don't use this unless ...");
chan.declare_queue(QueueConfig { durable: false, exclusive: true, auto_delete: true, ... });
}
```
---
## The `BuildWorker`
```rust
// tasks/build.rs
pub struct BuildWorker {
cloner: checkout::CachedCloner,
build_executor: buildtool::BuildExecutor,
system: String,
identity: String,
}
impl BuildWorker {
pub fn new(
cloner: checkout::CachedCloner,
build_executor: buildtool::BuildExecutor,
system: String,
identity: String,
) -> BuildWorker { ... }
}
```
The `BuildWorker` implements `SimpleNotifyWorker`, meaning it receives a
`NotificationReceiver` that allows it to stream log lines back during
processing.
---
## `JobActions` — The Streaming Helper
`JobActions` wraps the build job context and provides methods for logging and
reporting:
```rust
pub struct JobActions {
system: String,
identity: String,
receiver: Arc<dyn NotificationReceiver + Send + Sync>,
job: buildjob::BuildJob,
line_counter: AtomicU64,
snippet_log: parking_lot::RwLock<VecDeque<String>>,
attempt_id: String,
log_exchange: Option<String>,
log_routing_key: Option<String>,
result_exchange: Option<String>,
result_routing_key: Option<String>,
}
```
### Attempt ID
Each build execution gets a unique UUID v4 `attempt_id`:
```rust
attempt_id: Uuid::new_v4().to_string(),
```
### Snippet Log
The last 10 lines of output are kept in a ring buffer for inclusion in the
build result:
```rust
snippet_log: parking_lot::RwLock::new(VecDeque::with_capacity(10)),
```
### Log Streaming
```rust
impl JobActions {
pub async fn log_line(&self, line: String) {
self.line_counter.fetch_add(1, Ordering::SeqCst);
// Update snippet ring buffer
{
let mut snippet_log = self.snippet_log.write();
if snippet_log.len() >= 10 {
snippet_log.pop_front();
}
snippet_log.push_back(line.clone());
}
let msg = buildlogmsg::BuildLogMsg {
identity: self.identity.clone(),
system: self.system.clone(),
attempt_id: self.attempt_id.clone(),
line_number: self.line_counter.load(Ordering::SeqCst),
output: line,
};
self.tell(worker::publish_serde_action(
self.log_exchange.clone(),
self.log_routing_key.clone(),
&msg,
)).await;
}
}
```
Each log line is published as a `BuildLogMsg` to the `logs` exchange in
real-time. The `line_counter` uses `AtomicU64` for thread-safe incrementing.
### Build Start Notification
```rust
pub async fn log_started(&self, can_build: Vec<String>, cannot_build: Vec<String>) {
let msg = buildlogmsg::BuildLogStart {
identity: self.identity.clone(),
system: self.system.clone(),
attempt_id: self.attempt_id.clone(),
attempted_attrs: Some(can_build),
skipped_attrs: Some(cannot_build),
};
self.tell(worker::publish_serde_action(
self.log_exchange.clone(), self.log_routing_key.clone(), &msg,
)).await;
}
```
### Build Result Reporting
```rust
pub async fn merge_failed(&self) {
let msg = BuildResult::V1 {
tag: V1Tag::V1,
repo: self.job.repo.clone(),
pr: self.job.pr.clone(),
system: self.system.clone(),
output: vec![String::from("Merge failed")],
attempt_id: self.attempt_id.clone(),
request_id: self.job.request_id.clone(),
attempted_attrs: None,
skipped_attrs: None,
status: BuildStatus::Failure,
push: self.job.push.clone(),
};
self.tell(worker::publish_serde_action(
self.result_exchange.clone(),
self.result_routing_key.clone(),
&msg,
)).await;
self.tell(worker::Action::Ack).await;
}
```
### Other Status Methods
```rust
impl JobActions {
pub async fn pr_head_missing(&self) { self.tell(Action::Ack).await; }
pub async fn commit_missing(&self) { self.tell(Action::Ack).await; }
pub async fn nothing_to_do(&self) { self.tell(Action::Ack).await; }
pub async fn merge_failed(&self) { /* publish Failure + Ack */ }
pub async fn log_started(&self, ...) { /* publish BuildLogStart */ }
pub async fn log_line(&self, line) { /* publish BuildLogMsg */ }
pub async fn log_instantiation_errors(&self, ...) { /* log each error */ }
pub fn log_snippet(&self) -> Vec<String> { /* return last 10 lines */ }
}
```
---
## Build Flow
1. **Receive** `BuildJob` from queue
2. **Clone** repository (using `CachedCloner`)
3. **Checkout** target branch
4. **Fetch** PR (if PR-triggered)
5. **Merge** PR into target branch
6. **Determine** which attrs can build on this system
7. **Log start** (`BuildLogStart` message)
8. **For each attr**:
a. Execute build command
b. Stream output lines (`BuildLogMsg` messages)
c. Check exit status
9. **Publish result** (`BuildResult` with `BuildStatus`)
10. **Ack** the original message
---
## Project Detection
The `detect_changed_projects` function in `buildtool.rs` maps changed files
to project names:
```rust
pub fn detect_changed_projects(changed_files: &[String]) -> Vec<String>;
```
It examines the first path component of each changed file and matches it
against known project directories in the monorepo.
The `find_project` function looks up a project by name:
```rust
pub fn find_project(name: &str) -> Option<ProjectBuildConfig>;
```
---
## Build Timeout
The build timeout is enforced at the configuration level:
```rust
pub struct BuildConfig {
pub system: Vec<String>,
pub build_timeout_seconds: u16,
pub extra_env: Option<HashMap<String, String>>,
}
```
The minimum is 300 seconds (5 minutes). This is validated at startup:
```rust
if self.build.build_timeout_seconds < 300 {
error!("Please set build_timeout_seconds to at least 300");
panic!();
}
```
When a build times out, the result status is set to `BuildStatus::TimedOut`.
---
## NixOS Service Configuration
The builder has special systemd resource limits:
```nix
# service.nix
"tickborg-builder" = mkTickborgService "Builder" {
binary = "builder";
serviceConfig = {
MemoryMax = "8G";
CPUQuota = "400%";
};
};
```
The `CPUQuota = "400%"` allows the builder to use up to 4 CPU cores.
The service PATH includes build tools:
```nix
path = with pkgs; [
git bash cmake gnumake gcc pkg-config
meson ninja
autoconf automake libtool
jdk17
rustc cargo
];
```
|