projects/aports·blob v3.21.3 · refs

208 lines · 6.7 KiB

Policy for APKBUILDs

This documents defines a policy for writing APKBUILDs.

Standard selection

APKBUILDs are POSIX shell scripts as defined in POSIX.1-2017 Volume 3 POSIX.1-2017 volume 3. Additionally, the following extensions are supported:

  1. The local keyword for introducing variables local to a function is supported, it is briefly documented in the bash manual.
  2. Non-POSIX parameter extensions are supported. This includes: Substring expansions (e.g. ${var:offset:length}) and Replacement expansions (e.g. ${var/pattern/string}). The bash manual contains further information on these two expansions.

NOTE: busybox ash is currently used to evaluate APKBUILDs - since it supports additional POSIX shell extensions, your APKBUILD might be evaluated correctly, even if it is not confirming to this policy document.

Shell Style Considerations

Formatting

Indention

Indent with tabs, don’t use spaces. Avoid whitespaces.

Line Length

Maximum line length is 80 characters, this should be considered a recommendation and not a strict requirement which must be followed at all costs. Most notably, automatically generated parts (e.g. by abuild checksum) are except from this rule.

Compound Statements

Put ; do and ; then on the same line as the while, for or if.

Function Declarations

  • Always put the function name, the parenthesis and the curly brackets on the same line.
  • Don’t use spacing between function name and parenthesis.
  • Do use spacing between function parenthesis and curly brackets.

Case statement

  • Don’t indent alternatives.
  • A one-line alternative needs a space after the close parenthesis of the pattern and before the ;;.
  • End the last case with ;;.

Variable expansion

  • Use ${var} over $var only when it is strictly necessary. Meaning: Only if the character following the variable name is an underscore or an alphanumeric character.

Quoting

  • Always quote string literals (exceptions are assigning pkgname and pkgver, which must not be quoted).
  • Always quote variables, command substitutions or shell meta characters when used in strings. Prefer "$var"/foo/bar over "$var/foo/bar".
  • Never quote literal integers.
  • Double quotes should be used, unless preventing variable interpolation is necessary.

Features

Command Substitution

  • Always use $() instead of backticks.

Test and [

  • Prefer [ over test(1).

Naming Conventions

Function Names

  • Lower-case, with underscores to separate words. Prefix all helper functions with an underscore character.

Variable Names

  • Lower-case, with underscores to separate words. Prefix all non-metadata variables with an underscore character.

Use Local Variables

  • Declare function-specific variables with the local keyword.

Calling Commands

Top-level Scope

  • External commands should not be called outside of functions; in variables, use parameter expansions instead (e.g. ${pkgver/-/.} instead of $(echo $pkgver | tr '-' '.'))).

Return Values

  • APKBUILDs are executed with set -e, explicit || return 1 statements must not be used.

Comments

Spacing

  • All comments begin with an ASCII space character, e.g. # foo.

TODO Comments

  • Use TODO comments for code that is temporary, a short-term solution, or good-enough but not perfect.

APKBUILD style considerations

Comments

Contributor Comment

  • All APKBUILDs may begin with one or more contributor comments (one per line) containing a valid RFC 5322 address. For example, # Contributor: Max Mustermann <[email protected]>.

Maintainer Comment

  • Deprecated, see Maintainer Variable below.

Metadata Variables

Metadata Variables are variables used directly by abuild itself, e.g. pkgname or depends.

Metadata Values

  • pkgname must only contain lowercase characters.
  • pkgname must match the name of the directory the APKBUILD file is located in.

Maintainer Variable

  • All APKBUILDs should contain exactly one variable named maintainer, containing a valid RFC 5322 address. For example, maintainer="Max Mustermann <[email protected]>".
  • maintainer should immediately follow the Contributor comment(s), above pkgname.
  • In the case of package being abandoned, the variable should still be present, but left empty: maintainer="".

Variable Assignments

  • Empty Metadata Variable assignments, e.g. install="" must be removed.
  • The Metadata Variable builddir defaults to $srcdir/$pkgname-$pkgver and should only be assigned if the default values is not appropriate.

Assignment Order

  • All Metadata Variables (except checksums) must be declared before the first function declaration.
  • Checksum Metadata Variables must be declared after the last function declaration, abuild checksum will automatically take care of this.

Comments

  • For Metadata Variables such as options or arch, comments are encouraged to help other contributors understand the choices; in those cases, they should be put either on the same line after the variable, or above it, on a separate line, with an appropriate prefix (e.g. # check: no test suite or # armhf: blocked by qt6-qtwebengine).

Build Functions

Function Declaration

  • Functions should be declared in the order they are called by abuild.
  • All functions are executed in "$builddir" explicit directory changes to $builddir must be avoided (if possible).
  • Variables local to functions must always be declared with the local keyword.
  • If a function body is empty, contains only the default comment from newapkbuild, or only calls default_*, it should be completely omitted from the file.

Function Overwriting

  • If the prepare function is overwritten it should always call default_prepare.
1# Policy for APKBUILDs
2
3This documents defines a policy for writing APKBUILDs.
4
5# Standard selection
6
7APKBUILDs are POSIX shell scripts as defined in [POSIX.1-2017 Volume 3]
8[POSIX.1-2017 volume 3]. Additionally, the following extensions are
9supported:
10
111. The `local` keyword for introducing variables local to a function is
12 supported, it is briefly documented in the [bash manual][bash functions].
132. Non-POSIX [parameter extensions][POSIX.1-2017 parameter expansion]
14 are supported. This includes: Substring expansions (e.g.
15 `${var:offset:length}`) and Replacement expansions (e.g.
16 `${var/pattern/string}`). The [bash manual][bash expansion]
17 contains further information on these two expansions.
18
19**NOTE:** `busybox ash` is currently used to evaluate APKBUILDs - since it
20supports additional POSIX shell extensions, your APKBUILD might be
21evaluated correctly, even if it is not confirming to this policy
22document.
23
24# Shell Style Considerations
25
26<!--
27This should be in conformance with most existing APKBUILDs
28Structure and content inspired by Google Shell Style Guide.
29-->
30
31## Formatting
32
33### Indention
34
35Indent with tabs, don't use spaces. Avoid whitespaces.
36
37### Line Length
38
39Maximum line length is 80 characters, this should be considered a
40recommendation and not a strict requirement which must be followed at
41all costs. Most notably, automatically generated parts (e.g. by `abuild
42checksum`) are except from this rule.
43
44### Compound Statements
45
46Put `; do` and `; then` on the same line as the `while`, `for` or `if`.
47
48### Function Declarations
49
50* Always put the function name, the parenthesis and the curly brackets
51 on the same line.
52* Don't use spacing between function name and parenthesis.
53* Do use spacing between function parenthesis and curly brackets.
54
55### Case statement
56
57* Don't indent alternatives.
58* A one-line alternative needs a space after the close parenthesis of the pattern and before the `;;`.
59* End the last case with `;;`.
60
61### Variable expansion
62
63* Use `${var}` over `$var` only when it is strictly necessary. Meaning:
64 Only if the character following the [variable name][POSIX.1-2017 definition name]
65 is an underscore or an alphanumeric character.
66
67### Quoting
68
69* Always quote string literals (exceptions are assigning `pkgname` and
70 `pkgver`, which must not be quoted).
71* Always quote variables, command substitutions or shell meta characters
72 when used in strings. Prefer `"$var"/foo/bar` over `"$var/foo/bar"`.
73* Never quote literal integers.
74* Double quotes should be used, unless preventing variable interpolation
75 is necessary.
76
77## Features
78
79### Command Substitution
80
81* Always use `$()` instead of backticks.
82
83### Test and [
84
85* Prefer `[` over `test(1)`.
86
87## Naming Conventions
88
89### Function Names
90
91* Lower-case, with underscores to separate words. Prefix all helper
92 functions with an underscore character.
93
94### Variable Names
95
96* Lower-case, with underscores to separate words. Prefix all
97 non-metadata variables with an underscore character.
98
99### Use Local Variables
100
101* Declare function-specific variables with the `local` keyword.
102
103## Calling Commands
104
105### Top-level Scope
106
107* External commands should not be called outside of functions;
108 in variables, use parameter expansions instead
109 (e.g. `${pkgver/-/.}` instead of `$(echo $pkgver | tr '-' '.')`)).
110
111### Return Values
112
113* APKBUILDs are executed with `set -e`, explicit `|| return 1`
114 statements must not be used.
115
116## Comments
117
118### Spacing
119
120* All comments begin with an ASCII space character, e.g. `# foo`.
121
122### TODO Comments
123
124* Use TODO comments for code that is temporary, a short-term solution,
125 or good-enough but not perfect.
126
127# APKBUILD style considerations
128
129<!--
130This section attempts to document policies enforced by the linter from
131atools-go <https://gitlab.alpinelinux.org/alpine/infra/atools-go>,
132newapkbuild and existing APKBUILDs.
133-->
134
135## Comments
136
137### Contributor Comment
138
139* All APKBUILDs may begin with one or more contributor comments (one per
140 line) containing a valid [RFC 5322][RFC 5322] address. For example,
141 `# Contributor: Max Mustermann <[email protected]>`.
142
143### Maintainer Comment
144
145* Deprecated, see *Maintainer Variable* below.
146
147## Metadata Variables
148
149Metadata Variables are variables used directly by abuild itself, e.g. `pkgname` or `depends`.
150
151### Metadata Values
152
153* `pkgname` must only contain lowercase characters.
154* `pkgname` must match the name of the directory the `APKBUILD` file is located in.
155
156### Maintainer Variable
157
158* All APKBUILDs should contain exactly one variable named `maintainer`,
159 containing a valid RFC 5322 address. For example,
160 `maintainer="Max Mustermann <[email protected]>"`.
161* `maintainer` should immediately follow the Contributor comment(s), above `pkgname`.
162* In the case of package being abandoned, the variable should still be present,
163 but left empty: `maintainer=""`.
164
165### Variable Assignments
166
167* Empty Metadata Variable assignments, e.g. `install=""` must be removed.
168* The Metadata Variable `builddir` defaults to `$srcdir/$pkgname-$pkgver`
169 and should only be assigned if the default values is not appropriate.
170
171### Assignment Order
172
173* All Metadata Variables (except checksums) must be declared before the
174 first function declaration.
175* Checksum Metadata Variables must be declared after the last function
176 declaration, `abuild checksum` will automatically take care of this.
177
178### Comments
179
180* For Metadata Variables such as `options` or `arch`,
181 comments are encouraged to help other contributors understand the choices;
182 in those cases, they should be put either on the same line after the variable,
183 or above it, on a separate line, with an appropriate prefix
184 (e.g. `# check: no test suite` or `# armhf: blocked by qt6-qtwebengine`).
185
186## Build Functions
187
188### Function Declaration
189
190* Functions should be declared in the order they are called by abuild.
191* All functions are executed in `"$builddir"` explicit directory changes
192 to `$builddir` must be avoided (if possible).
193* Variables local to functions must always be declared with the `local`
194 keyword.
195* If a function body is empty, contains only the default comment from `newapkbuild`,
196 or only calls `default_*`, it should be completely omitted from the file.
197
198### Function Overwriting
199
200* If the `prepare` function is overwritten it should always call
201 `default_prepare`.
202
203[POSIX.1-2017 volume 3]: https://pubs.opengroup.org/onlinepubs/9699919799/idx/xcu.html
204[POSIX.1-2017 parameter expansion]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02
205[POSIX.1-2017 definition name]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_235
206[bash functions]: https://www.gnu.org/software/bash/manual/bash.html#Shell-Functions
207[bash expansion]: https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion
208[RFC 5322]: https://tools.ietf.org/html/rfc5322