| 1 | title: One module, one file per target |
| 2 | part: Standing alone |
| 3 | summary: Portable programs, the code that isn't, and how the compiler keeps them in step. |
| 4 | --- |
| 5 | raz is first made for x86-64 Linux, but the language is written to be implemented elsewhere, and the library is arranged for it. The part of a program that depends on the system goes in a module of its own, one file per target. This is how `Sys` is made, and it is how your own code should keep its system-dependent parts apart. |
| 6 | |
| 7 | ## The files |
| 8 | |
| 9 | A module `Name` lives in `Name.raz`. A module whose code depends on the system comes in one file per target, `Name.linux-x86_64.raz`, which the compiler looks for first. The library has `Sys.linux-x86_64.raz`, which makes system calls, and `Sys.raz`, which calls the C library instead, for every other system. A program that imports `Sys` doesn't know or care which it got. |
| 10 | |
| 11 | ```sh |
| 12 | $ ls $RAZLIB | grep -i "^sys\|^thread" |
| 13 | $ cd $RAZLIB && grep -c SYSCALL Sys.linux-x86_64.raz Sys.raz |
| 14 | ``` |
| 15 | |
| 16 | ## A module of your own |
| 17 | |
| 18 | Here is a module with two files. `Clock.linux-x86_64.raz` is for this target; `Clock.raz` is what any other target would use. Both say what they are in `Name`, and both make `ticks`. |
| 19 | |
| 20 | ```raz Clock.linux-x86_64 |
| 21 | module Clock; |
| 22 | import Sys; |
| 23 | |
| 24 | const Name* = "the linux-x86_64 file"; |
| 25 | |
| 26 | (* a number that only grows *) |
| 27 | proc ticks*: I64; |
| 28 | begin |
| 29 | return Sys.clock(); |
| 30 | end; |
| 31 | |
| 32 | end Clock. |
| 33 | ``` |
| 34 | |
| 35 | ```raz Clock |
| 36 | module Clock; |
| 37 | import Sys; |
| 38 | |
| 39 | const Name* = "the portable file"; |
| 40 | |
| 41 | (* a number that only grows *) |
| 42 | proc ticks*: I64; |
| 43 | begin |
| 44 | return Sys.clock(); |
| 45 | end; |
| 46 | |
| 47 | end Clock. |
| 48 | ``` |
| 49 | |
| 50 | A program that imports `Clock` gets the first of them here: |
| 51 | |
| 52 | ```raz Show run |
| 53 | module Show; |
| 54 | import Out, Clock; |
| 55 | |
| 56 | proc main*; |
| 57 | begin |
| 58 | Out.str("using "); Out.str(Clock.Name); Out.ln; |
| 59 | if Clock.ticks() > 0 then Out.str("and it ticks\n"); end; |
| 60 | end; |
| 61 | |
| 62 | end Show. |
| 63 | ``` |
| 64 | |
| 65 | ## Held to one interface |
| 66 | |
| 67 | Two files can't be told apart by their callers, so the compiler holds them to the same interface. Here is the pair again with `ticks` changed in the portable file to return an `INT`: |
| 68 | |
| 69 | ```sh |
| 70 | $ mkdir drift && cp Clock*.raz Show.raz drift && cd drift |
| 71 | $ sed -i 's/proc ticks\*: I64/proc ticks*: INT/; s/return Sys.clock()/return INT(Sys.clock())/' Clock.raz |
| 72 | $ raz Show.raz |
| 73 | ``` |
| 74 | |
| 75 | The program stops compiling on every target, including the ones that would never have used that file. A mismatch is found where it is made, not where someone first builds for a second target. |
| 76 | |
| 77 | ## What the compiler checks |
| 78 | |
| 79 | When it takes one of `Name.*.raz`, it also reads the declarations of the others beside it, and of `Name.raz` if there is one, and reports an error unless they all export the same names, each the same kind of thing: a constant of the same type (its value may differ), a type of the same kind with the same exported fields, a variable of the same type, a procedure of the same signature. The code, the unexported fields, and so the size and layout, may differ. |
| 80 | |
| 81 | So a program written against the interface works on every target that has the module, and the places where it doesn't are the compiler's to find, not yours. |
| 82 | |
| 83 | ## What stays the same everywhere |
| 84 | |
| 85 | The core of the library is what every target must have: `Rt`, `Out`, `Mem`, `Arena`, `Lock`, `Str` and `Buf`, and of `Sys` what they need. A program that keeps to the core runs on every target. `Thread`, `link` and `extern`, and most of `Sys`, are what a target may leave out when its system can't give them. |