aboutsummaryrefslogtreecommitdiff
blob: 97e599539e4fcbd611b4b08acbd79709005b9f07 (plain)
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
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
<?xml version="1.0" encoding="UTF-8"?>
<guide self="ebuild-writing/variables/">
<chapter>
<title>Variables</title>

<body>
<p>
There are a number of special variables which must be set in ebuilds, and many
more which can optionally be specified. There are also some predefined variables
which are of use throughout the ebuild.
</p>
</body>

<section>
<title>Predefined read-only variables</title>
<body>

<p>
The following variables are defined for you. You must not attempt to
set them. Developers must not rely on package manager specific values
for these variables when writing ebuilds.
</p>

<table>
  <tr>
    <th>Variable</th>
    <th>Purpose</th>
  </tr>
  <tr>
    <ti><c>P</c></ti>
    <ti>Package name and version (excluding revision, if any), for example <c>vim-6.3</c>.</ti>
  </tr>
  <tr>
    <ti><c>PN</c></ti>
    <ti>Package name, for example <c>vim</c>.</ti>
  </tr>
  <tr>
    <ti><c>PV</c></ti>
    <ti>Package version (excluding revision, if any), for example <c>6.3</c>. It should reflect the upstream versioning scheme.</ti>
  </tr>
  <tr>
    <ti><c>PR</c></ti>
    <ti>Package revision, or <c>r0</c> if no revision exists.</ti>
  </tr>
  <tr>
    <ti><c>PVR</c></ti>
    <ti>Package version and revision (if any), for example <c>6.3</c>, <c>6.3-r1</c>.</ti>
  </tr>
  <tr>
    <ti><c>PF</c></ti>
    <ti>Full package name, <c>${PN}-${PVR}</c>, for example <c>vim-6.3-r1</c>.</ti>
  </tr>
  <tr>
    <ti><c>A</c></ti>
    <ti>
      All the source files for the package (excluding those
      which are not available because of <c>USE</c> flags).
    </ti>
  </tr>
  <tr>
    <ti><c>CATEGORY</c></ti>
    <ti>Package's category, for example <c>app-editors</c>.</ti>
  </tr>
  <tr>
    <ti><c>FILESDIR</c></ti>
    <ti>
      Path to the package's <c>files/</c> directory, commonly used for small
      patches and other files.
    </ti>
  </tr>
  <tr>
    <ti><c>WORKDIR</c></ti>
    <ti>
      Path to the ebuild's root build directory. For example:
      <c>"${PORTAGE_BUILDDIR}/work"</c>.
    </ti>
  </tr>
  <tr>
    <ti><c>T</c></ti>
    <ti>
       Path to a temporary directory which may be used by the
       ebuild. For example: <c>"${PORTAGE_BUILDDIR}/temp"</c>.
    </ti>
  </tr>
  <tr>
    <ti><c>D</c></ti>
    <ti>
      Path to the temporary install directory. For example:
      <c>"${PORTAGE_BUILDDIR}/image"</c>.
    </ti>
  </tr>
  <tr>
    <ti><c>HOME</c></ti>
    <ti>
      Path to a temporary directory for use by any programs invoked by
      an ebuild that may read or modify the home directory. For example:
      <c>"${PORTAGE_BUILDDIR}/homedir"</c>.
    </ti>
  </tr>
  <tr>
    <ti><c>ROOT</c></ti>
    <ti>
      The absolute path to the root directory into which the package is to be
      merged. Only allowed in pkg_* phases.
      See <uri link="::ebuild-writing/variables/#ROOT"/>
    </ti>
  </tr>
  <tr>
    <ti><c>DISTDIR</c></ti>
    <ti>
      Contains the path to the directory where all the files
      fetched for the package are stored.
    </ti>
  </tr>
  <tr>
    <ti><c>EPREFIX</c></ti>
    <ti>
      The normalised offset-prefix path of an offset installation.
      See <uri link="https://wiki.gentoo.org/wiki/Project:Prefix/Technical_Documentation">
      Gentoo Prefix Technical Documentation</uri> for more information.
    </ti>
  </tr>
  <tr>
    <ti><c>ED</c></ti>
    <ti>
      Shorthand for <c>${D%/}${EPREFIX}/</c>.
    </ti>
  </tr>
  <tr>
    <ti><c>EROOT</c></ti>
    <ti>
      Shorthand for <c>${ROOT%/}${EPREFIX}/</c>.
    </ti>
  </tr>
  <tr>
    <ti><c>SYSROOT</c></ti>
    <ti>
      (EAPI=7) The absolute path to the root directory containing build dependencies
		satisfied by <c>DEPEND</c>
    </ti>
  </tr>
  <tr>
    <ti><c>ESYSROOT</c></ti>
    <ti>
      (EAPI=7) Shorthand for <c>${SYSROOT%/}${EPREFIX}/</c>.
    </ti>
  </tr>
  <tr>
    <ti><c>BROOT</c></ti>
    <ti>
      (EAPI=7) The absolute path to the root directory containing build dependencies
		satisfied by <c>BDEPEND</c>, typically executable build tools.
    </ti>
  </tr>
  <tr>
    <ti><c>MERGE_TYPE</c></ti>
    <ti>
      The type of package that is being merged. Possible values are:
      <c>source</c> if building and installing a package from source,
      <c>binary</c> if installing a binary package previously built from
      the ebuild, <c>buildonly</c> if building a binary package without
      installing it.
    </ti>
  </tr>
  <tr>
    <ti><c>REPLACING_VERSIONS</c></ti>
    <ti>
      A whitespace-separated list of all versions (<c>PVR</c>) of this package
      that are being replaced (uninstalled or overwritten) as a result of this
      install. It is a list, not a single optional value, to handle pathological
      cases such as installing <c>foo-2:2</c> to replace <c>foo-2:1</c> and
      <c>foo-3:2</c>. Available in <c>pkg_preinst</c> and <c>pkg_postinst</c>.
    </ti>
  </tr>
  <tr>
    <ti><c>REPLACED_BY_VERSION</c></ti>
    <ti>
      The single version (<c>PVR</c>) of this package that is replacing the
      version provided by this ebuild, if it is being uninstalled as part of
      an install. An empty string otherwise, i.e., if it is being uninstalled
      without replacement. Available in <c>pkg_prerm</c> and <c>pkg_postrm</c>.
    </ti>
  </tr>
</table>

</body>

<subsection>
<title>ROOT</title>
<body>

<p>
The idea behind <c>ROOT</c> is that one can build a system with
<c>ROOT=/somewhere</c> and then chroot into it or tar up
<c>/somewhere</c> as a system image. It is not designed to allow the
user to run <c>/somewhere/usr/bin/foo</c>.
</p>

<p>
Ebuilds may reference <c>ROOT</c> only during <c>pkg_*</c> phases. It
can't be used correctly in <c>src_*</c> phases, since <c>ROOT</c> may
be different when merging a binary package. For example, a binary
package may be built with <c>ROOT=/</c> and then installed onto a
system using <c>ROOT=/somewhere</c>.
</p>

<p>
When building a package, <c>ROOT</c> should not be used to satisfy the
required dependencies on libraries, headers files etc. Instead, the
files on the build system should be specified using <c>/</c>.
</p>

<p>
In a cross compiling environment, ebuilds must not call any of the
binaries inside <c>ROOT</c> since they may not be executable on the
build system.
</p>

<p>
Below is an example of an ebuild that uses <c>ROOT</c> in
<c>pkg_postinst()</c> to conditionally print an error message if an
old and obsolete configuration file still exists:
</p>

<codesample lang="ebuild">
pkg_postinst() {
	if [[ -e ${ROOT}/etc/oldconfig ]]; then
		ewarn "You still have the obsolete config file"
		ewarn "    ${ROOT}/etc/oldconfig."
		ewarn "Please migrate your settings to ${ROOT}/etc/newconfig"
		ewarn "and remove ${ROOT}/etc/oldconfig."
	fi
}
</codesample>

</body>
</subsection>
</section>

<section>
<title>Ebuild-defined variables</title>
<body>

<p>
The following variables may or must be defined by every ebuild.
</p>

<table>
  <tr>
    <th>Variable</th>
    <th>Purpose</th>
  </tr>
  <tr>
    <ti><c>EAPI</c></ti>
    <ti>
    The EAPI. See <uri link="::ebuild-writing/eapi/"/>.
    </ti>
  </tr>
  <tr>
    <ti><c>DESCRIPTION</c></ti>
    <ti>
    A short (not more than 80 characters) description of the package's
    purpose. Mandatory.
    </ti>
  </tr>
  <tr>
    <ti><c>HOMEPAGE</c></ti>
    <ti>
    Package's homepage(s). Mandatory (except for virtuals), accepts
    multiple values. In some contexts, it is customary to provide
    package tracker and/or code hosting links besides the main homepage
    (e.g. PyPI link for Python packages, GitHub link when code can't
    easily be found on homepage) for convenience. If no homepage for
    the package is available, please set it to
    <c>https://wiki.gentoo.org/wiki/No_homepage</c>.
    Never refer to a variable name in the string; include only raw text.
    </ti>
  </tr>
  <tr>
    <ti><c>SRC_URI</c></ti>
    <ti>
    A list of source URIs for the package. Can contain
    <c>USE</c>-conditional parts, see
    <uri link="::ebuild-writing/variables/#SRC_URI"/>.
    </ti>
  </tr>
  <tr>
    <ti><c>LICENSE</c></ti>
    <ti>
    The package's license, corresponding exactly (including case)
    to a file in <c>licenses/</c>. Mandatory (except for virtuals).
    See <uri link="::ebuild-writing/variables/#LICENSE"/>.
    </ti>
  </tr>
  <tr>
    <ti><c>SLOT</c></ti>
    <ti>
    The package's <c>SLOT</c>. Mandatory.
    See <uri link="::ebuild-writing/variables/#SLOT"/>.
    </ti>
  </tr>
  <tr>
    <ti><c>KEYWORDS</c></ti>
    <ti>
    See <uri link="::ebuild-writing/variables/#KEYWORDS"/> and
    <uri link="::keywording/"/>.
    </ti>
  </tr>
  <tr>
    <ti><c>IUSE</c></ti>
    <ti>
    A list of all <c>USE</c> flags (excluding arch flags, but
    including <c>USE_EXPAND</c> flags) used within the ebuild.
    See <uri link="::ebuild-writing/variables/#IUSE"/>.
    </ti>
  </tr>
  <tr>
    <ti><c>REQUIRED_USE</c></ti>
    <ti>
      A list of assertions that must be met by the configuration of <c>USE</c>
      flags to be valid for this ebuild.
      See <uri link="::ebuild-writing/variables/#REQUIRED_USE"/>.
    </ti>
  </tr>
  <tr>
    <ti><c>PROPERTIES</c></ti>
    <ti>
    A space-delimited list of properties, with conditional syntax support.
    <c>interactive</c>, <c>live</c>, and <c>test_network</c> are valid values.
    </ti>
  </tr>
  <tr>
    <ti><c>RESTRICT</c></ti>
    <ti>
    A space-delimited list of Portage features to restrict.
    Valid values are <c>fetch</c>, <c>mirror</c>, <c>strip</c>, <c>test</c> and
    <c>userpriv</c>. See <c>man 5 ebuild</c> for details.
    </ti>
  </tr>
  <tr>
    <ti><c>DEPEND</c></ti>
    <ti>
    A list of the package's build dependencies.
    See <uri link="::general-concepts/dependencies/"/>.
    Starting with EAPI-7, applies to CHOST only.
    </ti>
  </tr>
  <tr>
    <ti><c>BDEPEND</c></ti>
    <ti>
    (EAPI=7) A list of the package's CBUILD build dependencies.
    See <uri link="::general-concepts/dependencies/"/>.
    </ti>
  </tr>
  <tr>
    <ti><c>RDEPEND</c></ti>
    <ti>
    A list of the package's runtime dependencies.
    See <uri link="::general-concepts/dependencies/"/>.
    </ti>
  </tr>
  <tr>
    <ti><c>PDEPEND</c></ti>
    <ti>
    A list of packages to be installed (if possible) after the package
    is merged. Use this <e>only</e> when <c>RDEPEND</c> would cause cyclic
    dependencies. See <uri link="::general-concepts/dependencies/"/>.
    </ti>
  </tr>
  <tr>
    <ti><c>S</c></ti>
    <ti>
    Path to the temporary build directory, used by
    <c>src_compile</c> and <c>src_install</c>. Default:
    <c>"${WORKDIR}/${P}"</c>. Ebuilds should <b>not</b> provide a
    value for this variable if it is the same as the default value.
    </ti>
  </tr>
  <tr>
    <ti><c>DOCS</c></ti>
    <ti>
      An array or whitespace-separated list of documentation files for
      the default <c>src_install</c> function to install using <c>dodoc</c>.
      If undefined, a reasonable default list is used. See the
      <uri link="::ebuild-writing/functions/src_install/#Default src_install">
      default <c>src_install</c> function</uri>.
    </ti>
  </tr>
  <tr>
    <ti><c>HTML_DOCS</c></ti>
    <ti>
    An array or space-delimited list of documentation files or
    directories for the <c>einstalldocs</c> function to install
    recursively. (Requires
    <uri link="::ebuild-writing/eapi/#EAPI 6">EAPI&gt;=6</uri>.)
    </ti>
  </tr>
</table>

</body>

<subsection>
<title>SRC_URI</title>

<subsubsection>
<title>Conditional sources</title>
<body>

<p>
Conditional source files based upon USE flags are allowed using the usual
<c>flag? ( )</c> syntax. This is often useful for arch-specific code or for binary
packages <d/> downloading sparc binaries on ppc would be a waste of bandwidth.
</p>

<codesample lang="ebuild">
SRC_URI="https://example.com/files/${P}-core.tar.bz2
	x86?   ( https://example.com/files/${P}/${P}-sse-asm.tar.bz2 )
	ppc?   ( https://example.com/files/${P}/${P}-vmx-asm.tar.bz2 )
	sparc? ( https://example.com/files/${P}/${P}-vis-asm.tar.bz2 )
	doc?   ( https://example.com/files/${P}/${P}-docs.tar.bz2 )"
</codesample>

</body>
</subsubsection>

<subsubsection>
<title>Renaming sources</title>
<body>

<p>
Sometimes the upstream URI uses generic names that can easily clash with other
packages when creating a single mirror. Using the <c>-&gt;</c> syntax allows
you to rename the file when it's fetched from upstream for storing on mirrors
and in the local distdir.
</p>

<p>
Here we download a file from upstream which has a plain name like
<c>1.0.tar.gz</c> and save/mirror it with a better name like
<c>thepackage-1.0.tar.gz</c>. As usual, all tokens, including the operator
and the output file name, must be separated by whitespace.
</p>

<codesample lang="ebuild">
SRC_URI="https://example.com/files/${PV}.tar.gz -&gt; ${P}.tar.gz"
</codesample>

</body>
</subsubsection>

<subsubsection>
<title>Third-party mirrors</title>
<body>

<p>
If the items in <c>SRC_URI</c> are available on multiple third-party mirrors,
and the same set of mirrors is shared across multiple ebuilds, then you
don't have to repeatedly list each of them in every ebuild.
The <c>profiles/thirdpartymirrors</c> file in the <c>::gentoo</c> repository
contains named groups of mirrors, in the format specified by the
Package Manager Specification (PMS), accessible through the
<c>mirror://</c>
pseudo-protocol.
</p>

<p>
One might define a set of "example" mirrors,
</p>

<pre>
example https://download.example.com https://mirror1.example.org/example
</pre>

<p>
that can afterwards be referenced through a <c>mirror://</c> URI:
</p>

<codesample lang="ebuild">
SRC_URI="mirror://example/${PN}/${P}.tar.gz"
</codesample>

<p>
There are two valid cases for using <c>thirdpartymirrors</c>:
</p>

<ol>
  <li>
    providing multiple download locations for mirror- or fetch-restricted
    packages,
  </li>

  <li>
    dealing with upstreams that distribute their distfiles via a network
    of mirrors without a primary download location or a bouncer service.
  </li>
</ol>

<p>
In any other case, the primary location must be used instead. The distfiles
will be <uri link="::general-concepts/mirrors/">mirrored onto Gentoo
infrastructure</uri>; in that case, the benefit to using third-party mirror
list does not outweigh the burden of maintaining it.
</p>

</body>
</subsubsection>

<subsubsection>
<title>Lifting restrictions</title>
<body>

<p>
In EAPI 8, individual items in <c>SRC_URI</c> can be exempted from automatic
mirroring and fetching restrictions (imposed by <c>RESTRICT="mirror"</c> and
<c>RESTRICT="fetch"</c>) by prefixing the addresses with <c>mirror+</c> or
<c>fetch+</c>.  For example, in the following ebuild,
</p>

<codesample lang="ebuild">
EAPI="8"

SRC_URI="${P}.tar.gz
	mirror+https://dev.gentoo.org/~larry/distfiles/${P}-addons.tar.gz"

RESTRICT="fetch"
</codesample>

<p>
Portage will be prevented from trying to fetch <c>${P}.tar.gz</c> as expected,
but the <c>${P}-patches.tar.gz</c> file will be mirrored and fetched by Portage
without restriction.
</p>

<p>
The following table shows the effects of the prefixes when <c>RESTRICT="mirror"</c> and <c>RESTRICT="fetch"</c> are set:
</p>

<table>
<tr>
  <th></th>
  <th><e>(no prefix)</e></th>
  <th><c>fetch+</c></th>
  <th><c>mirror+</c></th>
</tr>
<tr>
  <th><e>(no RESTRICT)</e></th>
  <ti>fetch &amp; mirror</ti>
  <ti>fetch &amp; mirror</ti>
  <ti>fetch &amp; mirror</ti>
</tr>
<tr>
  <th><c>RESTRICT="mirror"</c></th>
  <ti>fetch only</ti>
  <ti>fetch only</ti>
  <ti>fetch &amp; mirror</ti>
</tr>
<tr>
  <th><c>RESTRICT="fetch"</c></th>
  <ti>unfetchable</ti>
  <ti>fetch only</ti>
  <ti>fetch &amp; mirror</ti>
</tr>
</table>

</body>
</subsubsection>
</subsection>

<subsection>
<title>LICENSE</title>
<body>

<p>
It is possible to specify multiple <c>LICENSE</c> entries, and entries which only
apply if a particular <c>USE</c> flag is set. The format is the same as for
<c>DEPEND</c>. See <uri link="https://www.gentoo.org/glep/glep-0023.html">
GLEP 23</uri> for details.
</p>

</body>
</subsection>

<subsection>
<title>SLOT</title>
<body>

<p>
When slots are not needed, use <c>SLOT="0"</c>. Do <b>not</b> use
<c>SLOT=""</c>, because the variable must not be empty.
</p>

<p>
See <uri link="::general-concepts/slotting/" /> for more information on this
variable and see <uri link="::ebuild-maintenance/package-moves/" />.
</p>

</body>
</subsection>

<subsection>
<title>KEYWORDS</title>
<body>

<p>
The only valid construct involving a <c>*</c> inside <c>KEYWORDS</c> is a <c>-*</c>. Do
<b>not</b> use <c>KEYWORDS="*"</c> or <c>KEYWORDS="~*"</c>.
</p>

<p>
See <uri link="::keywording/"/> for how to handle this variable.
</p>

</body>
</subsection>

<subsection>
<title>IUSE</title>
<body>

<p>
Note that the <c>IUSE</c> variable is cumulative, so there is no need to specify
USE flags used only within inherited eclasses within the ebuild's IUSE.
</p>
<note>
You need not assign the IUSE variable in your ebuild if it is empty.
</note>

<p>
Arch USE flags (<c>sparc</c>, <c>mips</c>, <c>x64-macos</c> and so on) should
not be listed.
</p>

</body>
</subsection>

<subsection>
<title>REQUIRED_USE</title>
<body>
<p>
The <c>REQUIRED_USE</c> variable contains a list of assertions that must be
met by the configuration of USE flags to be valid for this ebuild. In order
to be matched, a USE flag in a terminal element must be enabled
(or disabled if it has an exclamation mark prefix).
</p>

<p>
Essentially, <c>REQUIRED_USE</c> is an analogue of <c>DEPEND</c> style syntax.
For example, to state that some combination is forbidden, i.e. "if foo is set,
bar must be unset":
</p>

<codesample lang="ebuild">
REQUIRED_USE="foo? ( !bar )"
</codesample>
<p>
To state "if foo is set, then at least one of bar, baz, and quux must be activated":
</p>
<codesample lang="ebuild">
REQUIRED_USE="foo? ( || ( bar baz quux ) )"
</codesample>
<p>
To state "exactly one of foo, bar, or baz must be set, but not several":
</p>
<codesample lang="ebuild">
REQUIRED_USE="^^ ( foo bar baz )"
</codesample>
<p>
Note that the last relationship is that of an Exclusive OR (XOR).
While an XOR could be formed from usual DEPEND syntax,
a specific ^^ operator has been added for this case.
</p>
<p>
Finally, to state "at least one of foo, bar, or baz must be set":
</p>
<codesample lang="ebuild">
REQUIRED_USE="|| ( foo bar baz )"
</codesample>
<important>
See section <uri link="::general-concepts/use-flags/#Conflicting USE flags"/>
for when (and when not) to use <c>REQUIRED_USE</c>.
</important>
</body>

<subsubsection>
<title>EAPI 5</title>
<body>
<p>
EAPI 5 added an additional case to simplify conflicting USE flags.
</p>
<p>
To state "zero or one of foo, bar, or baz must be set, but not several":
</p>
<codesample lang="ebuild">
REQUIRED_USE="?? ( foo bar baz )"
</codesample>
<p>In the previous EAPI, this would be the same as:</p>
<codesample lang="ebuild">
REQUIRED_USE="foo? ( !bar !baz ) bar? ( !foo !baz ) baz? ( !foo !bar )"
</codesample>
</body>
</subsubsection>
</subsection>

<subsection>
<title>RESTRICT</title>
<body>

<p>
While Portage may recognise several different <c>RESTRICT</c> tokens, it is
important that you do not rely on them existing. That is, you should ensure
your ebuild does not fail if those tokens are not exposed or given a different
name by another package manager. You can make use of Portage-provided
<c>RESTRICT</c> tokens, but do not fail hard without them. See
<uri link="https://projects.gentoo.org/pms/7/pms.html#x1-810008.2.8">
PMS</uri> for the list of standardised <c>RESTRICT</c> tokens, or the above
table.
</p>

</body>
</subsection>
</section>

<section>
<title>Variables reserved for the package manager</title>
<body>

<p>
Variables and functions that begin with any of the following strings (ignoring
case) are reserved for package manager use. Ebuilds must neither use them nor
rely upon them:
</p>

<ul>
  <li><c>__</c> (two underscores)</li>
  <li><c>abort</c></li>
  <li><c>dyn</c></li>
  <li><c>prep</c></li>
</ul>

<p>
The same applies to functions and variables that contain any of the following
strings (ignoring case):
</p>

<ul>
  <li>
    <c>ebuild</c> (unless immediately preceded by another letter, and except
    for the <c>EBUILD_PHASE</c> and <c>EBUILD_PHASE_FUNC</c> variables)
  </li>
  <li><c>hook</c></li>
  <li><c>paludis</c></li>
  <li><c>portage</c></li>
</ul>

</body>
</section>

<section>
<title>Version and name formatting issues</title>
<body>

<p>
Often upstream's tarball versioning or naming format doesn't quite follow Gentoo
conventions. Using capital letters in names or differences in how underscores and hyphens
are used in versions are particularly common. For example, what Gentoo calls <c>foo-1.2.3b</c> may be
expecting a tarball named <c>Foo-1.2-3b.tar.bz2</c>. Rather than hard coding the package string,
it is preferable to make <c>MY_PN</c>, <c>MY_PV</c> and <c>MY_P</c> variables and use those to define the
upstream naming.
EAPI=7 debuted a new set of functions, ver_cut, ver_rs and ver_test.
These were backported into older EAPIs with the <c>eapi7-ver</c> eclass.
The easy way of redefining the version, which should be used unless you are sure
you know what you are doing, is to use these functions:
</p>

<codesample lang="ebuild">
inherit eapi7-ver

MY_PN="Foo"
# Replace the second period separator in PV with -
MY_PV=$(ver_rs 2 '-')
MY_P="${MY_PN}-${MY_PV}"
</codesample>

<p>
This code has the advantage that it will carry on working even if upstream
switches to a format like <c>Foo-1.3-4.5.tar.bz2</c> (yes, this really happens).
</p>

<p>
It is also possible to use bash substitution to achieve the same effect (this is
how eapi7-ver works internally), but this is complicated, error prone and hard
to read.
</p>

<p>
Some ebuilds use calls to <c>sed</c>, <c>awk</c> and / or <c>cut</c> to do this.
This must <e>not</e> be done for any new code and should be fixed to use
built-in version manipulation commands (for EAPI 7 or later), Bash substitution,
or in older EAPIs before 7, <c>eapi7-ver</c>. Global scope non-Bash code is
<e>strongly</e> discouraged.
</p>

<p>
The ver_ functions are used extract particular components
from a version string. See <c>man eapi7-ver.eclass</c> and the eclass source code
for further documentation and examples. A brief summary of the functions
follows.
</p>

<table>
  <tr>
    <th>Function</th>
    <th>Purpose</th>
  </tr>
  <tr>
    <ti><c>ver_rs [range] ' '</c></ti>
    <ti>Get important version components, excluding '.', '-' and '_'.</ti>
  </tr>
  <tr>
    <ti><c>ver_cut 1</c></ti>
    <ti>Get the major version.</ti>
  </tr>
  <tr>
    <ti><c>ver_cut [range]</c></ti>
    <ti>Extract a range of subparts from a version string.</ti>
  </tr>
  <tr>
    <ti><c>ver_cut 2-</c></ti>
    <ti>Get everything after the major version.</ti>
  </tr>
  <tr>
    <ti><c>ver_rs [range] [char]</c></ti>
    <ti>Replace a particular version separator.</ti>
  </tr>
  <tr>
    <ti><c>ver_rs 1- [char]</c></ti>
    <ti>Replace all version separators.</ti>
  </tr>
  <tr>
    <ti><c>ver_rs [range] ''</c></ti>
    <ti>Delete a version separator.</ti>
  </tr>
  <tr>
    <ti><c>ver_rs 1- ''</c></ti>
    <ti>Delete all version separators.</ti>
  </tr>
</table>

<note>
A helpful guide to the newer complements of the old <c>versionator.eclass</c>
functions can be found
<uri link="https://mgorny.pl/articles/the-ultimate-guide-to-eapi-7.html#replacing-versionator-eclass-uses">
here</uri>, courtesy of Gentoo developer mgorny.
</note>

</body>
</section>

<section>
<title>Trailing slashes in variables</title>
<body>

<p>
The following variables never end with a trailing slash in EAPI 7:
<c>D</c>, <c>ED</c>, <c>ROOT</c>, <c>EROOT</c>. Conversely, in EAPIs
preceeding EAPI 7, these variables are guaranteed to end with a
trailing slash. When working with EAPIs prior to EAPI 7, developers
are encouraged to use the bash suffix removal for the trailing slash
and add an explicit <c>/</c> when joining paths. For example:
<c>${D%/}/</c>, <c>${ED%/}/</c>, <c>${ROOT%/}/</c>,
<c>${EROOT%/}/</c>.
</p>

</body>
</section>

<section>
<title>Use of constant-value variables in ebuilds</title>
<body>

<p>
Variables have significant value in ebuilds, making it possible to avoid
unnecessary repetitions and make maintenance easier.  However,
references to constant-value variables should be used with care as their
excessive use can harm readability and increase maintenance burden (e.g.
when renaming a package). In particular, using variables whose values
have no direct correlation with the expected string should be avoided.
</p>

<p>The examples of beneficial constant-value variable references are:</p>
<ul>
  <li>
    using <c>${PV}</c> and the variables derived from it (<c>${P}</c>,
    <c>${MY_P}</c>…) in <c>SRC_URI</c> and <c>S</c> to avoid having
    to update those variables for every version bump (assuming that
    <c>${PV}</c> is used to indicate the upstream version);
  </li>

  <li>
    using <c>${PF}</c> in <c>--docdir</c> path  this is a canonical
    Gentoo path that is always required to match <c>${PF}</c>.
  </li>
</ul>

<p>The examples of bad constant-value variable references are:</p>
<ul>
  <li>
    using <c>${PN}</c> in <c>HOMEPAGE</c>, <c>EGIT_REPO_URI</c>
    or the domain of <c>SRC_URI</c>  it breaks URL parsing in editors
    and terminals, and makes it hard to copy the link for external use
    (e.g. while reviewing via gitweb),
  </li>

  <li>
    using <c>${HOMEPAGE}</c> in <c>SRC_URI</c>  it breaks when homepage
    changes or additional entry is added,
  </li>

  <li>
    using <c>${PN}</c> (or other unnecessary helper variables)
    excessively in <c>src_install()</c>  it impairs readability for
    little benefit and causes a lot of trouble when the package needs to
    be renamed.
  </li>
</ul>

</body>
</section>

<section>
<title>User environment</title>
<body>

<p>
The following variables may be set in the user's environment and should be
respected by all ebuilds. The purpose of each variable within Gentoo is listed
alongside an example of a valid value. Upstream usage may diverge, but ebuilds
should ensure that these variables are interpreted consistently within Gentoo.
The chosen meanings are inspired by a few real and de-facto standards:
</p>

<ul>
  <li>
    <uri link="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/make.html">
    The POSIX (2018) make specification</uri>
  </li>
  <li>
    <uri link="https://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html">
    The GNU make (v4.3) implementation</uri>
  </li>
  <li>
    <uri link="https://www.gnu.org/software/libtool/manual/libtool.html#LT_005fINIT">
    The GNU libtool (v2.4.6) package</uri>
  </li>
</ul>

<p>
Many of these variables only have an effect if they are invoked directly.
For example, your compiler driver is usually responsible for assembling object
files rather than a direct call to <c>${AS}</c>. In that case, setting
<c>ASFLAGS</c> will have no effect on the build process; instead, you would set
something like <c>CFLAGS="-Wa,-alh,-L"</c> to tell the C compiler to pass those
flags to the assembler. The <c>LDFLAGS</c> variable is the exception to this
rule, as it is intended to be passed to the compiler driver rather than
<c>${LD}</c>.
</p>

<table>
  <tr>
    <th>Variable</th>
    <th>Purpose</th>
    <th>Origin</th>
    <th>Example</th>
  </tr>
  <tr>
    <ti><c>AR</c></ti>
    <ti>
      <uri link="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ar.html">
      ar</uri>-compatible library archiver
    </ti>
    <ti>POSIX make</ti>
    <ti><c>x86_64-pc-linux-gnu-ar</c></ti>
  </tr>
  <tr>
    <ti><c>ARFLAGS</c></ti>
    <ti>flags for <c>${AR}</c></ti>
    <ti>POSIX make</ti>
    <ti><c>-v</c></ti>
  </tr>
  <tr>
    <ti><c>AS</c></ti>
    <ti>as-compatible assembler</ti>
    <ti>GNU make</ti>
    <ti><c>x86_64-pc-linux-gnu-as</c></ti>
  </tr>
  <tr>
    <ti><c>ASFLAGS</c></ti>
    <ti>flags for <c>${AS}</c></ti>
    <ti>GNU make</ti>
    <ti><c>--reduce-memory-overheads</c></ti>
  </tr>
  <tr>
    <ti><c>CC</c></ti>
    <ti>C compiler driver (also usually used for linking)</ti>
    <ti>POSIX make</ti>
    <ti><c>clang-9</c></ti>
  </tr>
  <tr>
    <ti><c>CFLAGS</c></ti>
    <ti>flags for <c>${CC}</c></ti>
    <ti>POSIX make</ti>
    <ti><c>-march=native</c></ti>
  </tr>
  <tr>
    <ti><c>CPPFLAGS</c></ti>
    <ti>flags for the C preprocessor</ti>
    <ti>GNU make</ti>
    <ti><c>-D_GNU_SOURCE</c></ti>
  </tr>
  <tr>
    <ti><c>CXX</c></ti>
    <ti>C++ compiler driver (also usually used for linking)</ti>
    <ti>GNU make</ti>
    <ti><c>clang++</c></ti>
  </tr>
  <tr>
    <ti><c>CXXFLAGS</c></ti>
    <ti>flags for <c>${CXX}</c></ti>
    <ti>GNU make</ti>
    <ti><c>-fvisibility=hidden</c></ti>
  </tr>
  <tr>
    <ti><c>LD</c></ti>
    <ti>dynamic linker</ti>
    <ti>GNU libtool</ti>
    <ti><c>x86_64-pc-linux-gnu-ld</c></ti>
  </tr>
  <tr>
    <ti><c>LDFLAGS</c></ti>
    <ti>flags for the <e>compiler driver</e> to pass to its linker</ti>
    <ti>POSIX make</ti>
    <ti><c>-Wl,-O1 -Wl,--as-needed</c></ti>
  </tr>
  <tr>
    <ti><c>LEX</c></ti>
    <ti>
      <uri link="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/lex.html">
      lex</uri>-compatible lexer
    </ti>
    <ti>POSIX make</ti>
    <ti><c>/usr/bin/flex</c></ti>
  </tr>
  <tr>
    <ti><c>LFLAGS</c></ti>
    <ti>flags for <c>${LEX}</c></ti>
    <ti>POSIX make</ti>
    <ti><c>--8bit --posix-compat</c></ti>
  </tr>
  <tr>
    <ti><c>NM</c></ti>
    <ti>
      <uri link="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/nm.html">
      nm</uri>-compatible symbol extractor
    </ti>
    <ti>GNU libtool</ti>
    <ti><c>x86_64-pc-linux-gnu-nm</c></ti>
  </tr>
  <tr>
    <ti><c>RANLIB</c></ti>
    <ti>archive index generator</ti>
    <ti>GNU libtool</ti>
    <ti><c>x86_64-pc-linux-gnu-ranlib</c></ti>
  </tr>
  <tr>
    <ti><c>YACC</c></ti>
    <ti>
      <uri link="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/yacc.html">
      yacc</uri>-compatible compiler-compiler
    </ti>
    <ti>POSIX make</ti>
    <ti><c>/usr/bin/bison</c></ti>
  </tr>
  <tr>
    <ti><c>YFLAGS</c></ti>
    <ti>flags for <c>${YACC}</c></ti>
    <ti>POSIX make</ti>
    <ti><c>-d</c></ti>
  </tr>
</table>

</body>
</section>
</chapter>
</guide>