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 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
|
# Obuild Design Documentation
This document explains the architecture and design decisions of obuild, a parallel, incremental, and declarative build system for OCaml.
## Table of Contents
1. [Genesis and Philosophy](#genesis-and-philosophy)
2. [Architectural Layers](#architectural-layers)
3. [Core Data Flow](#core-data-flow)
4. [The Two-DAG Architecture](#the-two-dag-architecture)
5. [Dependency Resolution System](#dependency-resolution-system)
6. [Build Execution Pipeline](#build-execution-pipeline)
7. [Type System Design](#type-system-design)
8. [Ctypes.cstubs Integration](#ctypescstubs-integration)
9. [Custom Code Generators](#custom-code-generators)
10. [PPX and Preprocessor Resolution](#ppx-and-preprocessor-resolution)
11. [Configuration Files](#configuration-files)
12. [CLI Reference](#cli-reference)
13. [Design Decisions and Trade-offs](#design-decisions-and-trade-offs)
---
## Genesis and Philosophy
Obuild started on a bank holiday after xmas, as an experiment to make the
simplest OCaml build system. The main goals are to:
* provide a good user experience.
* provide a building black box, no mocking around generic rules.
* provide features in the highest level possible.
* the cleanest build possible, with by-products hidden from the user.
* provide good defaults, and standardize names as much as possible.
* expose everything that the user of the build system needs in one place.
* be simple to build to prevent any bootstrapping problem.
One of the main influences was Haskell Cabal, which provides to all Haskellers
a simple way to provide a build system to a project with a single file. This
applies well for the myriad of OCaml options too.
### Simple to Build
Obuild is buildable with just the compiler and the compiler standard library.
This make bootstrapping very easy: all you need is the OCaml compiler installed.
This creates some pain for developers of obuild, as lots of basic functions
available in others libraries need to written again as part of obuild. As the
initial development was done really quickly, some functions are not as
performant (CPU or memory-wise) as they could be. This can be fixed as problem
becomes apparent in scaling.
### Simple to Use
Each project is described really simply in a one place, in a user friendly format.
A central .obuild file is used, and provides high level description of your project.
Along with some meta data (name, authors, description, etc), it defines the library,
and/or executable that the project wants to have, from which inputs (source
files, modules).
All dependencies are fully autogenerated internally and used to recompile only
the necessary bits.
Along with library and executable, test and benchmark can be defined, so as to
provide an easy way to test or bench some part of your project. It also provides
a standard on how to build and execute tests and benchmarks.
### Design Principles
```
┌─────────────────────────────────────────────────────────────────┐
│ OBUILD DESIGN GOALS │
├─────────────────────────────────────────────────────────────────┤
│ Declarative │ Users describe targets, not build steps │
│ Parallel │ Independent tasks run concurrently │
│ Incremental │ Only rebuild what changed │
│ Self-contained │ No external build tool dependencies │
│ Discoverable │ Auto-detect modules, dependencies │
└─────────────────────────────────────────────────────────────────┘
```
---
## Architectural Layers
Obuild is organized in four distinct layers, each with clear responsibilities:
```
┌─────────────────────────────────────────────────────────────────┐
│ LAYER 4: CLI │
│ src/main.ml │
│ Command dispatcher, user interface │
├─────────────────────────────────────────────────────────────────┤
│ LAYER 3: COMMANDS │
│ lib/help.ml, init.ml, install.ml, etc. │
│ High-level operations (init, install, doc) │
├─────────────────────────────────────────────────────────────────┤
│ LAYER 2: CORE │
│ lib/core/*.ml │
│ Parsing, analysis, dependency resolution, build execution │
├─────────────────────────────────────────────────────────────────┤
│ LAYER 1: BASE │
│ lib/base/*.ml │
│ Filepath, Filesystem, Fugue (utilities), CLI │
└─────────────────────────────────────────────────────────────────┘
```
### Layer 1: Base Utilities (`lib/base/`)
The foundation layer has **no dependencies** on OCaml build tools. This is critical for bootstrapping - obuild must compile itself before it can use itself.
| Module | Purpose |
|--------|---------|
| `compat.ml` | OCaml version compatibility shims |
| `filepath.ml` | Type-safe path abstractions |
| `filesystem.ml` | File I/O operations |
| `fugue.ml` | Functional utilities (string, list, option) |
| `cli.ml` | Command-line parsing framework |
### Layer 2: Core Build System (`lib/core/`)
This layer contains ~60 modules implementing the build system logic:
```
lib/core/
├── Parsing ──────────── obuild_lexer.ml, obuild_parser.ml,
│ obuild_ast.ml, obuild_validate.ml,
│ project.ml, project_read.ml, meta.ml
│
├── Types ────────────── types.ml, target.ml, libname.ml,
│ modname.ml, hier.ml, filetype.ml
│
├── Analysis ─────────── analyze.ml, prepare.ml, prepare_types.ml,
│ dependencies.ml, metacache.ml
│
├── Execution ────────── build.ml, scheduler.ml, taskdep.ml,
│ process.ml, prog.ml, buildprogs.ml
│
├── Configuration ────── configure.ml, gconf.ml, findlibConf.ml
│
└── Utilities ────────── dag.ml, dagutils.ml, utils.ml, helper.ml
```
### Layer 3: Command Modules (`lib/`)
High-level operations that users invoke:
| Module | Command | Purpose |
|--------|---------|---------|
| `help.ml` | `obuild help` | Documentation system |
| `init.ml` | `obuild init` | Create new projects |
| `install.ml` | `obuild install` | Install artifacts |
| `doc.ml` | `obuild doc` | Generate documentation |
| `sdist.ml` | `obuild sdist` | Create source distributions |
### Layer 4: Main Entry Point (`src/main.ml`)
The CLI dispatcher that:
- Parses command-line arguments
- Dispatches to appropriate command handler
- Manages global configuration
- Handles top-level error reporting
---
## Core Data Flow
The build process follows a clear pipeline from configuration to compiled artifacts:
```
┌──────────────────────────────────────────────────────────────────────────┐
│ BUILD PIPELINE │
└──────────────────────────────────────────────────────────────────────────┘
.obuild file META files Source files
│ │ │
▼ ▼ ▼
┌─────────────┐ ┌─────────────────┐ ┌───────────────┐
│ PARSING │ │ DEPENDENCY │ │ MODULE │
│ │ │ RESOLUTION │ │ ANALYSIS │
│ Lexer │ │ │ │ │
│ Parser │ │ FindlibConf │ │ ocamldep │
│ Validator │ │ Meta parser │ │ Hier mapping │
│ │ │ Metacache │ │ │
└──────┬──────┘ └────────┬────────┘ └───────┬───────┘
│ │ │
▼ ▼ ▼
┌──────────────────────────────────────────────────────────────────────────┐
│ ANALYSIS │
│ (analyze.ml) │
│ │
│ Combines all inputs into project_config with resolved dependencies │
└──────────────────────────────────────────────────────────────────────────┘
│
▼
┌──────────────────────────────────────────────────────────────────────────┐
│ PREPARATION │
│ (prepare.ml) │
│ │
│ For each target: scan modules, build compilation DAG, compute paths │
└──────────────────────────────────────────────────────────────────────────┘
│
▼
┌──────────────────────────────────────────────────────────────────────────┐
│ EXECUTION │
│ (build.ml) │
│ │
│ Scheduler runs compile_steps in parallel, respecting dependencies │
└──────────────────────────────────────────────────────────────────────────┘
│
▼
Compiled Artifacts
(dist/build/<target>/...)
```
### Sequence Diagram: Full Build
```
User main.ml Project_read Analyze Prepare Build Scheduler
│ │ │ │ │ │ │
│──build───────>│ │ │ │ │ │
│ │──read()─────>│ │ │ │ │
│ │ │──parse──────│ │ │ │
│ │ │──validate───│ │ │ │
│ │<──Project.t──│ │ │ │ │
│ │ │ │ │ │ │
│ │──prepare()───────────────>│ │ │ │
│ │ │ │──resolve───│ │ │
│ │ │ │ META │ │ │
│ │ │ │ files │ │ │
│ │<──project_config───────────│ │ │ │
│ │ │ │ │ │ │
│ │──build_dag()────────────────────────────────────────>│ │
│ │ │ │ │ │ │
│ │ │ │──prepare_target()──────>│ │
│ │ │ │<──compilation_state─────│ │
│ │ │ │ │ │ │
│ │ │ │ │──schedule()────────────>│
│ │ │ │ │ │ ┌────────┤
│ │ │ │ │ │ │parallel│
│ │ │ │ │ │ │compile │
│ │ │ │ │ │ │steps │
│ │ │ │ │ │ └────────┤
│ │ │ │ │<───────────────done──────│
│<──success─────│ │ │ │ │ │
```
---
## The Two-DAG Architecture
A distinctive feature of obuild is its use of **two separate DAGs** (Directed Acyclic Graphs) for dependency tracking. Understanding why requires understanding two different questions:
### Question 1: "What changed?"
The **Files DAG** answers this question. It tracks file-level dependencies:
```
Files DAG Example (for module Bar that uses Foo):
bar.cmo
/ | \
/ | \
bar.ml bar.cmi foo.cmi
|
bar.mli
```
- **Nodes**: Individual files (.ml, .mli, .cmi, .cmo, .cmx, .c, .h, .o)
- **Edges**: "file A depends on file B" (based on content)
- **Purpose**: Check modification times to determine what needs rebuilding
When `foo.ml` changes but `foo.mli` doesn't:
- `bar.cmo` doesn't need rebuilding (it only depends on `foo.cmi`)
- Only `foo.cmo` needs recompilation
### Question 2: "What order?"
The **Steps DAG** answers this question. It tracks task execution order:
```
Steps DAG Example:
LinkTarget(mylib)
/ | \
/ | \
CompileModule CompileModule CompileC
(Bar) (Foo) (stubs.c)
| |
CompileInterface CompileInterface
(Bar) (Foo)
```
- **Nodes**: Compilation tasks (CompileModule, CompileInterface, CompileC, LinkTarget)
- **Edges**: "task A must complete before task B"
- **Purpose**: Enable parallel scheduling while respecting dependencies
### Why Two DAGs?
| Aspect | Files DAG | Steps DAG |
|--------|-----------|-----------|
| Purpose | Incremental builds | Parallel execution |
| Answers | "What changed?" | "What order?" |
| Granularity | Individual files | Compilation tasks |
| Usage | mtime comparison | Topological sort |
Separating these concerns allows:
1. **Fine-grained incrementality**: Only recompile files whose dependencies changed
2. **Maximum parallelism**: Independent tasks run concurrently
3. **Correct ordering**: Dependent tasks wait for prerequisites
---
## Dependency Resolution System
Obuild resolves dependencies through OCamlfind's META file system. This is one of the more complex subsystems.
### Internal META Parsing
ocamlfind is the current de-facto standard for installed package querying.
ocamlfind is usually injected on the command line to ocamlopt, ocamldep, ocamlc
with special flags (-syntax, -package), that ocamlfind will re-write to call
the program with something that the program can understand. All the information
for this transformation is stored in META files.
Unfortunately this design prevents META caching, and each time ocamlc/ocamlopt
is used it will reparse the META files. This also causes problems if ocamlfind
does not exist when used as a program, or if the library is not installed when
used as a library.
Because of those 2 reasons, obuild has its own implementation of META parsing
with caching support.
### META File Discovery
```
Search Order for library "base":
1. Check each path in OCAMLPATH:
├── /home/user/.opam/default/lib/base/META ← Found!
├── /usr/lib/ocaml/base/META
└── ...
2. Alternative: META.<name> format:
└── /usr/lib/ocaml/META.base
```
### META Parsing Flow
```
META file (text)
│
▼
┌─────────────┐
│ LEXER │
│ (meta.ml) │
└──────┬──────┘
│ tokens
▼
┌─────────────┐
│ PARSER │
│ (meta.ml) │
└──────┬──────┘
│
▼
┌─────────────┐
│ Pkg.t │
│ (cached) │
└─────────────┘
```
### Metacache Architecture
A critical design decision is how META files are cached:
```
┌─────────────────────────────────────────────────────────────────┐
│ METACACHE │
│ │
│ Hashtbl: main_name → (filepath, root_pkg) │
│ │
│ "base" → ("/path/to/base/META", <Pkg.t for base>) │
│ "str" → ("/path/to/str/META", <Pkg.t for str>) │
│ │
└─────────────────────────────────────────────────────────────────┘
│
│ get_from_cache("base.shadow_stdlib")
▼
Returns: (filepath, ROOT package for "base")
│
│ Caller must resolve subpackages:
│ Meta.Pkg.find(["shadow_stdlib"], root)
▼
Resolved subpackage Pkg.t
```
**Key design**: Metacache always returns the **root package**. Callers are responsible for navigating to subpackages. This prevents:
- Double resolution (cache pre-resolving, then caller resolving again)
- Incorrect path generation for subpackages
### Include Path Resolution
META files specify directories using special syntax:
```
Directory Field Resolves To
─────────────────────────────────────────────────
"" or "." basePath (where META lives)
"^" parent directory
"^subdir" parent/subdir
"+stdlib" $OCAMLLIB/stdlib
"/absolute/path" /absolute/path
"relative/path" basePath/relative/path
```
### Dependency Graph Construction
```
Analyze.prepare() builds multiple graphs:
┌─────────────────────────────────────────────────────────────────┐
│ project_pkgdeps_dag │
│ (Complete dependency picture) │
│ │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │Exe:main │────────>│Lib:mylib│────────>│Dep:base │ │
│ └─────────┘ └────┬────┘ └────┬────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌─────────┐ ┌─────────┐ │
│ │Dep:unix │ │Dep:sexp │ │
│ └─────────┘ └─────────┘ │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ project_targets_dag │
│ (Internal targets only) │
│ │
│ ┌─────────┐ ┌─────────┐ │
│ │Exe:main │──────────────────>│Lib:mylib│ │
│ └─────────┘ └─────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ project_dep_data │
│ (Dependency classification) │
│ │
│ mylib → Internal (defined in project) │
│ base → System (from OCamlfind) │
│ unix → System (from OCamlfind) │
│ │
└─────────────────────────────────────────────────────────────────┘
```
---
## Build Execution Pipeline
### Scheduler Design
The scheduler manages parallel job execution with dependency tracking:
```
┌─────────────────────────────────────────────────────────────────┐
│ SCHEDULER │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Ready │ │ Running │ │ Completed │ │
│ │ Queue │───>│ Jobs │───>│ Tasks │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ ▲ │ │
│ │ │ │
│ └──────────────────────────────────────┘ │
│ When dependencies are satisfied │
│ │
└─────────────────────────────────────────────────────────────────┘
```
### Compile Steps
Each step represents an atomic compilation task:
```ocaml
type compile_step =
| CompileModule of Hier.t (* .ml → .cmo/.cmx *)
| CompileInterface of Hier.t (* .mli → .cmi *)
| CompileDirectory of Hier.t (* Pack directory modules *)
| CompileC of filename (* .c → .o *)
| GenerateCstubsTypes of Libname.t (* ctypes type discovery *)
| GenerateCstubsFunctions of Libname.t (* ctypes stub generation *)
| CompileCstubsC of Libname.t (* Compile generated C *)
| LinkTarget of target (* Link library/executable *)
| CheckTarget of target (* Verify outputs exist *)
```
### Build Execution Sequence
```
For library "mylib" with modules Foo, Bar (Bar depends on Foo):
Time ──────────────────────────────────────────────────────────>
Thread 1: ┌──────────────────┐
│CompileInterface │
│ (Foo) │
└────────┬─────────┘
│
┌────────▼─────────┐
│ CompileModule │
│ (Foo) │
└────────┬─────────┘
│
▼ (Bar can now start)
┌──────────────────┐
│ CompileModule │
│ (Bar) │
└────────┬─────────┘
│
▼
┌──────────────────┐
│ LinkTarget │
│ (mylib) │
└──────────────────┘
Thread 2: ┌──────────────────┐
│CompileInterface │ (parallel with Foo interface)
│ (Bar) │
└──────────────────┘
│
│ (waits for CompileModule(Foo))
▼
┌──────────────────┐
│ CompileC │ (parallel with Bar module)
│ (stubs.c) │
└──────────────────┘
```
---
## Type System Design
Obuild uses domain-specific types to prevent common errors:
### Path Types
```ocaml
(* lib/base/filepath.ml *)
type filepath (* Directory or file path *)
type filename (* Just a filename, no directory *)
(* These are abstract types - you cannot mix them up *)
val (</>) : filepath -> filename -> filepath (* Combine path + name *)
val (<//>) : filepath -> filepath -> filepath (* Combine paths *)
```
### Name Types
```
┌─────────────────────────────────────────────────────────────────┐
│ NAME TYPES │
│ │
│ Libname.t │
│ ├── main_name: "base" │
│ └── subnames: ["shadow_stdlib"] │
│ → "base.shadow_stdlib" │
│ │
│ Modname.t │
│ └── Validated OCaml module name │
│ → "Base" (capitalized, valid chars) │
│ │
│ Hier.t │
│ └── Module hierarchy: [Base; Utils; String_extra] │
│ → "Base.Utils.String_extra" (module path) │
│ → "base/utils/string_extra" (file path) │
│ │
│ Target.Name.t │
│ ├── Lib of Libname.t │
│ ├── Exe of string │
│ ├── Test of string │
│ ├── Bench of string │
│ └── Example of string │
│ │
└─────────────────────────────────────────────────────────────────┘
```
### Why This Matters
```ocaml
(* WRONG: Easy to confuse in stringly-typed code *)
let path = dir ^ "/" ^ name ^ ".ml" (* What if name has a slash? *)
(* RIGHT: Types prevent mistakes *)
let path = dir </> (fn (name ^ ".ml")) (* Type-checked! *)
```
---
## Ctypes.cstubs Integration
Obuild supports ctypes.cstubs for generating C bindings declaratively.
### Configuration Syntax
```
library mylib
modules: Bindings, C, Types_generated
build-deps: ctypes, ctypes.stubs
cstubs
external-library-name: mylib_stubs
type-description: Bindings.Types -> Types_gen
function-description: Bindings.Functions -> Funcs_gen
generated-types: Types_generated
generated-entry-point: C
headers: string.h
```
### Cstubs Build Flow
```
.obuild configuration
│
│ cstubs block parsed
▼
┌─────────────────────────────────────────────────────────────────┐
│ PHASE 1: Type Discovery │
│ │
│ Generate discover.ml ───> Compile ───> Run ───> discover.c │
│ │ │
│ Compile discover.c ───> Run ───> types_generated.ml │
│ │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ PHASE 2: Compile Bindings │
│ │
│ bindings.ml (user's functor definitions) │
│ │ │
│ ▼ │
│ Compile bindings.cmo │
│ │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ PHASE 3: Stub Generation │
│ │
│ Generate stubgen.ml ───> Compile with bindings.cmo ───> Run │
│ │ │
│ ├───> mylib_stubs_generated.ml (FOREIGN implementation) │
│ ├───> c.ml (entry point) │
│ └───> mylib_stubs.c (C stubs) │
│ │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ PHASE 4: Compile Everything │
│ │
│ Compile generated .ml files │
│ Compile mylib_stubs.c ───> mylib_stubs.o │
│ Archive ───> libmylib_stubs.a │
│ Link everything into final library │
│ │
└─────────────────────────────────────────────────────────────────┘
```
### Cstubs DAG Integration
```
LinkTarget(mylib)
/ | \
/ | \
CompileModule CompileCstubsC CompileModule
(C - entry) (stubs.c) (Mylib_stubs_generated)
| | |
└───────────────┼───────────────────┘
│
GenerateCstubsFunctions
│
┌──────────┴──────────┐
│ │
CompileModule(Bindings) GenerateCstubsTypes
│
CompileModule(Types_generated)
```
---
## Custom Code Generators
Obuild supports custom code generators defined in the `.obuild` file. This allows any code generation tool (menhir, ocamllex, atdgen, protobuf, etc.) to be integrated without hardcoding support in obuild itself.
### Generator Architecture
Generators are registered during project parsing and invoked during module resolution. When obuild looks for a module and cannot find a `.ml` file, it checks for source files matching registered generator suffixes.
```
Module resolution for "Parser":
1. Look for parser.ml in src-dir → Not found
2. Check registered generators:
├── suffix "mly" → look for parser.mly → Found!
└── suffix "mll" → look for parser.mll → Not found
3. Run menhir generator on parser.mly
4. Produces parser.ml (and parser.mli)
5. Continue compilation with generated files
```
### Generator Types
Generators are defined in `lib/core/generators.ml` with two key types:
```ocaml
(* Built-in generator *)
type t = {
suffix : string; (* File extension to match *)
modname : Modname.t -> Modname.t; (* Module name transformation *)
commands : filepath -> filepath -> string -> string list list;
generated_files : filename -> string -> filename;
}
(* Custom generator from .obuild file *)
type custom = {
custom_name : string; (* Unique identifier *)
custom_suffix : string option; (* Auto-detection suffix *)
custom_command : string; (* Command template *)
custom_outputs : string list; (* Output file patterns *)
custom_module_name : string option; (* Optional module name pattern *)
}
```
### Variable Substitution
Generator commands support template variables:
| Variable | Description |
|----------|-------------|
| `${src}` | Full path to source file |
| `${dest}` | Destination path without extension |
| `${base}` | Base filename without extension |
| `${srcdir}` | Source directory |
| `${destdir}` | Destination directory |
| `${sources}` | Space-separated list of all inputs (multi-input only) |
### Generator Execution Flow
```
.obuild file Module resolution Build execution
│ │ │
│ generator menhir │ Parser not found │
│ suffix: mly │ parser.mly exists │
│ command: menhir ... │ │
│ outputs: ${base}.ml │ │
│ │ │
▼ ▼ ▼
register_custom() get_generators("mly") run(dest, src, modName)
│ │ │
│ Stored in global │ Returns matching │ substitute_variables()
│ generator registry │ generators │ Execute via sh -c
│ │ │ Produce .ml/.mli
```
Custom generators defined in `.obuild` take precedence over built-in ones, allowing users to override default behavior (e.g., using menhir instead of ocamlyacc for `.mly` files).
---
## PPX and Preprocessor Resolution
The `ppx_resolver.ml` module handles resolution of PPX preprocessors and legacy camlp4 syntax extensions.
### Resolution Strategy
PPX and syntax packages are resolved through the META file system using special predicates:
```
For a target with syntax dependencies:
1. Collect syntax deps from project DAG
2. For each syntax dep:
├── Internal (project library)?
│ └── Use compiled bytecode archive path
└── External (findlib package)?
└── Query META with predicates:
[Syntax, Preprocessor, Camlp4o/Camlp4r]
3. Generate -I include paths
4. Generate archive file arguments
5. Return complete preprocessor command
```
### Key Functions
- `get_syntax_pp`: Generates preprocessor flags (`-I` paths and archive files) for syntax packages
- `get_target_pp`: Resolves all syntax/preprocessor dependencies for a target and returns a complete preprocessor configuration
The resolver distinguishes between internal syntax packages (compiled as part of the project) and external ones (resolved via findlib META files).
---
## Configuration Files
Obuild supports configuration files (`.obuildrc`) for setting default values that apply before command-line argument parsing.
### File Locations
Configuration is loaded from two locations, in order:
1. `~/.obuildrc` — User-level defaults
2. `./.obuildrc` — Project-level defaults
Project-level values override user-level values. Command-line arguments override both.
### File Format
```
# Comments start with #
verbose = true
jobs = 4
ocamlopt = /usr/local/bin/ocamlopt
```
Each line is a `key = value` pair. Keys correspond to CLI option names (without the `--` prefix).
### Integration
The CLI framework (`lib/base/cli.ml`) loads configuration files via `load_config()` and applies them as argument defaults through `run_with_config`. This happens before command-line parsing, so CLI flags always take precedence.
```
Priority (highest to lowest):
1. Command-line arguments --jobs 8
2. Project .obuildrc jobs = 4
3. User ~/.obuildrc jobs = 2
4. Built-in defaults jobs = 2
```
---
## CLI Reference
### Commands
| Command | Description |
|---------|-------------|
| `configure` | Detect dependencies and prepare build configuration |
| `build` | Compile targets (all or specified) |
| `clean` | Remove build artifacts |
| `install` | Install compiled artifacts |
| `test` | Build and run test targets |
| `init` | Create a new project skeleton |
| `sdist` | Create a source distribution tarball |
| `get` | Query configuration values |
| `generate` | Generate helper files (see subcommands below) |
| `doc` | Generate documentation (not yet implemented) |
| `infer` | Infer project structure (not yet implemented) |
### Generate Subcommands
| Subcommand | Description |
|------------|-------------|
| `generate merlin` | Create `.merlin` file for IDE support (source dirs, build dirs, packages) |
| `generate opam` | Create `.opam` file (OPAM 2.0 format) from project metadata |
| `generate completion` | Generate shell completion scripts (bash, zsh, fish) |
### Global Options
These options apply to all commands:
| Option | Short | Description |
|--------|-------|-------------|
| `--help` | `-h` | Show help |
| `--version` | `-V` | Show version |
| `--verbose` | `-v` | Enable verbose output |
| `--quiet` | `-q` | Suppress output |
| `--debug` | `-d` | Enable debug output |
| `--debug+` | | Enable debug output with commands |
| `--color` | | Enable colored output |
| `--findlib-conf` | | Path to findlib configuration |
| `--ocamlopt` | | Path to ocamlopt compiler |
| `--ocamldep` | | Path to ocamldep tool |
| `--ocamlc` | | Path to ocamlc compiler |
| `--cc` | | Path to C compiler |
| `--ar` | | Path to ar archiver |
| `--pkg-config` | | Path to pkg-config tool |
| `--ranlib` | | Path to ranlib tool |
| `--ocamldoc` | | Path to ocamldoc tool |
| `--ld` | | Path to linker |
### Configure Options
| Option | Default | Description |
|--------|---------|-------------|
| `--executable-native` | true | Build native executables |
| `--executable-bytecode` | false | Build bytecode executables |
| `--executable-profiling` | false | Build profiling executables |
| `--executable-debugging` | false | Build debugging executables |
| `--executable-as-obj` | false | Build executables as objects |
| `--library-native` | true | Build native libraries |
| `--library-bytecode` | true | Build bytecode libraries |
| `--library-profiling` | false | Build profiling libraries |
| `--library-debugging` | false | Build debugging libraries |
| `--library-plugin` | true (Unix) | Build library plugins |
| `--build-examples` | false | Build example targets |
| `--build-benchs` | false | Build benchmark targets |
| `--build-tests` | false | Build test targets |
| `--annot` | false | Generate annotation files |
### Build Options
| Option | Short | Description |
|--------|-------|-------------|
| `--jobs` | `-j` | Number of parallel jobs (default: 2) |
| `--dot` | | Dump dependency graph in DOT format |
| `--noocamlmklib` | | Disable ocamlmklib usage |
| `-g` | | Shorthand for `--library-debugging --executable-debugging` |
---
## Design Decisions and Trade-offs
### Decision 1: Two Separate DAGs
**Trade-off**: More complex implementation vs. better incrementality and parallelism.
**Rationale**: A single DAG cannot efficiently answer both "what changed?" (file-level) and "what order?" (task-level). Separating them allows:
- File DAG: Fine-grained mtime checks
- Steps DAG: Coarse-grained parallel scheduling
### Decision 2: Metacache Returns Root Packages
**Trade-off**: Callers must resolve subpackages vs. no double-resolution bugs.
**Rationale**: When the cache pre-resolved subpackages, callers that also resolved (common pattern) caused `SubpackageNotFound` errors. Returning root packages consistently prevents this class of bugs.
### Decision 3: Type-Safe Path Abstractions
**Trade-off**: More verbose code vs. compile-time path safety.
**Rationale**: Path manipulation bugs are common and hard to debug. Type-safe `filepath`/`filename` types catch errors at compile time rather than runtime.
### Decision 4: Exception-Based Error Handling
**Trade-off**: Less explicit control flow vs. simpler code structure.
**Rationale**: OCaml's exception model allows errors to propagate naturally without Result types threading through every function. The main entry point catches and formats all exceptions.
### Decision 5: Lazy META Parsing with Caching
**Trade-off**: First build is slower vs. subsequent builds are faster.
**Rationale**: Parsing META files is I/O bound. Caching parsed results in memory dramatically speeds up dependency resolution for large projects with many dependencies.
### Decision 6: New Parser Architecture
**Trade-off**: More code, separate modules vs. cleaner separation of concerns.
**Rationale**: The new parser architecture (Lexer → Parser → AST → Validator) separates:
- Tokenization (obuild_lexer.ml)
- Syntax analysis (obuild_parser.ml)
- Data representation (obuild_ast.ml)
- Semantic validation (obuild_validate.ml)
This makes each component easier to test, modify, and understand.
---
## Summary
Obuild's architecture reflects its goals of being declarative, parallel, and incremental:
```
┌─────────────────────────────────────────────────────────────────┐
│ ARCHITECTURE SUMMARY │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Declarative Input Parallel Execution │
│ │ │ │
│ .obuild file Scheduler + DAGs │
│ │ │ │
│ ▼ ▼ │
│ ┌─────────┐ ┌─────────┐ │
│ │ Parsing │──────────────│ Build │ │
│ │ Layer │ Analysis │ Layer │ │
│ └─────────┘ └─────────┘ │
│ │ │ │
│ Project.t Compiled Artifacts │
│ │ │ │
│ Type-Safe Incremental Rebuilds │
│ Representation │
│ │
└─────────────────────────────────────────────────────────────────┘
```
The key insight is that build systems solve two distinct problems:
1. **What to build**: Declarative configuration, dependency resolution
2. **How to build**: Scheduling, parallelism, incrementality
Obuild addresses both through its layered architecture and dual-DAG design.
---
## Librification
Obuild has been designed to be used as a library eventually.
The code is shifting towards using pure structures and functions, so
that things can be reused. There is some global state that will be
eventually reduced to provide better control of each part.
One possible development would be to provide an optional daemon
that monitors file changes and automatically rebuilds on demand without having
to re-analyze the whole project.
Some other possible scenarios include having other programs use the project file
format, either to provide tools to write them or tools that read them.
|