|
| 1 | +Building Packages from Source |
| 2 | +============================= |
| 3 | + |
| 4 | +TKLDev includes most of the components needed to build Debian packages from |
| 5 | +source code OOTB. This can be useful if you want to test changes you've made |
| 6 | +to an existing package or create a new package - of your code or someone |
| 7 | +else's. |
| 8 | + |
| 9 | +Environment variables |
| 10 | +--------------------- |
| 11 | + |
| 12 | +```bash |
| 13 | +# assuming building for the current TurnKey/Debian version |
| 14 | +CODENAME=$(lsb_release -sc) |
| 15 | +``` |
| 16 | + |
| 17 | +Buildroot |
| 18 | +--------- |
| 19 | + |
| 20 | +A buildroot is a minimalist Debian [chroot][chroot] which provides an |
| 21 | +environment specifically for building packages. TurnKey uses our [bootstrap] as |
| 22 | +a base, then preinstalls some extra build related packages/tools. |
| 23 | + |
| 24 | +It is highly recommended that you [generate a buildroot][gen-buildroot] using |
| 25 | +our tool. Do that like this: |
| 26 | + |
| 27 | +```bash |
| 28 | +cd /turnkey |
| 29 | +git clone https://github.com/turnkeylinux/buildroot.git |
| 30 | +cd buildroot |
| 31 | +make install |
| 32 | +``` |
| 33 | + |
| 34 | +Always run `make clean` if/when you rebuild the buildroot. |
| 35 | + |
| 36 | +Note that using a pre-built buildroot will speed up package building, |
| 37 | +although strictly speaking is not required. A vanilla bootstrap may |
| 38 | +be used, as the dependencies noted in the [Build-Depends][build-deps] section |
| 39 | +of the package control file will be installed into the buildroot prior |
| 40 | +to building the package. |
| 41 | + |
| 42 | +Pool configuration |
| 43 | +------------------ |
| 44 | + |
| 45 | +[Pool][pool] is the custom TurnKey tool for building packages. With the |
| 46 | +exception of a buildroot, TKLDev should have (most of) pool configured out of |
| 47 | +the box. |
| 48 | + |
| 49 | +All you need to do is build the buildroot as linked above and you are ready |
| 50 | +to go. |
| 51 | + |
| 52 | +Build a package with pool |
| 53 | +------------------------- |
| 54 | + |
| 55 | +I'll use Turnkey's "inithooks" package as an example: |
| 56 | + |
| 57 | +First, clone or copy your source code to an appropriate location. We use |
| 58 | +/turnkey/public, but it can be any directory you like: |
| 59 | + |
| 60 | +```bash |
| 61 | +mkdir -p /turnkey/public |
| 62 | +cd /turnkey/public |
| 63 | +git clone https://github.com/turnkeylinux/inithooks.git |
| 64 | +``` |
| 65 | + |
| 66 | +Then register the source code with Pool and build the package: |
| 67 | + |
| 68 | +```bash |
| 69 | +cd pools/${CODENAME} |
| 70 | +pool-register /turnkey/public/inithooks |
| 71 | +pool-get . inithooks |
| 72 | +``` |
| 73 | + |
| 74 | +Assuming that the build process completes successfully, you will find the new |
| 75 | +Debian package in the pool directory: |
| 76 | + |
| 77 | +```bash |
| 78 | +root@tkldev pools/bookworm# ls |
| 79 | +inithooks_2.2.1_all.deb |
| 80 | +``` |
| 81 | +If you wish, the package can be installed on the local system using apt: |
| 82 | + |
| 83 | +```bash |
| 84 | +apt install ./inithooks_2.2.1_all.deb |
| 85 | +``` |
| 86 | + |
| 87 | +Alternatively, the package can be used when building an appliance by adding the |
| 88 | +package name to the appliance plan (if required) and setting `FAB_POOL=y` prior |
| 89 | +to appliance build. E.g.: |
| 90 | + |
| 91 | +```bash |
| 92 | +export FAB_POOL=y |
| 93 | +make |
| 94 | +``` |
| 95 | + |
| 96 | +Package versioning |
| 97 | +------------------ |
| 98 | + |
| 99 | +By default, a Debian package will generate it's version from the heading of |
| 100 | +the debian/changelog. That's fine when building "stable" packages. However, |
| 101 | +when hacking on a package it can be useful to preserve each package iteration |
| 102 | +as you make changes. By default, that requires manual update to the changelog |
| 103 | +version for each rebuild - and remembering to do it. |
| 104 | + |
| 105 | +To help out, our default buildroot includes our custom |
| 106 | +[auto version][autoversion] tool. As the name suggests, it can generate a unique |
| 107 | +version for you; each time you rebuild a package. |
| 108 | + |
| 109 | +The requirements for autoversion to generate a custom version are: |
| 110 | +- package source code must be in a git repo |
| 111 | +- changes must be committed |
| 112 | +- the must not be a changelog in the source debian directory |
| 113 | + |
| 114 | +If your package does already have a changelog, then move/remove it until you |
| 115 | +are ready for a new "stable" release. |
| 116 | + |
| 117 | +The generated versions are nicest if your code has at least one annotated tag. |
| 118 | +See the [tags][tags] section of the [Git Pro][git-pro]online book for details |
| 119 | +about annotated tags. For example if package "foo" has the git HEAD tagged |
| 120 | +'v0.1', autoversion will generate version '0.1'. I.e. 'foo_0.1_all.deb'. |
| 121 | + |
| 122 | +Further commits generate versions noting how many commits since the tag and |
| 123 | +the commit ID of HEAD. The new version string will be |
| 124 | +'<version_tag>+<commits_since_tag>+g<commit_id>'. E.g. if foo has 2 commits |
| 125 | +since the 'v0.1' tag and the HEAD commit ID is "59286ad", the new version will |
| 126 | +be "0.1+1+g59286ad" - i.e. 'foo_0.1+1+g59286ad_all.deb'. |
| 127 | + |
| 128 | +It still works if there are no tags, but the versions are a bit ugly. They are |
| 129 | +in the form of '0+<YYYY>.<MM>.<DD>+<HH>.<MM>.<SS>+<commit_id>'. E.g.: |
| 130 | +'foo_0+2025.2.19+06.45.45+a42fea97_all.deb'. |
| 131 | + |
| 132 | +The versions will sort properly, so it's no issue to push them to an apt |
| 133 | +package repo as is, but ideally you want to have a "nice" version number for a |
| 134 | +stable package. So either give it a new version tag - add/replace the |
| 135 | +changelog. |
| 136 | + |
| 137 | +[chroot]: https://en.wikipedia.org/wiki/Chroot |
| 138 | +[bootstrap]: https://github.com/turnkeylinux/bootstrap |
| 139 | +[gen-buildroot]: https://github.com/turnkeylinux/buildroot?tab=readme-ov-file#build |
| 140 | +[build-deps]: https://github.com/turnkeylinux/inithooks/blob/master/debian/control#L5-L8 |
| 141 | +[pool]: https://github.com/turnkeylinux/pool |
| 142 | +[autoversion]: https://github.com/turnkeylinux/autoversion?tab=readme-ov-file#map-git-commits-to-auto-versions-and-vice-versa |
| 143 | +[tags]: https://git-scm.com/book/en/v2/Git-Basics-Tagging |
| 144 | +[git-pro]: https://git-scm.com/book/en/v2 |
0 commit comments