dpkg-buildflags.1 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. .\" dpkg manual page - dpkg-buildflags(1)
  2. .\"
  3. .\" Copyright © 2010-2011 Raphaël Hertzog <hertzog@debian.org>
  4. .\" Copyright © 2011 Kees Cook <kees@debian.org>
  5. .\" Copyright © 2011-2015 Guillem Jover <guillem@debian.org>
  6. .\"
  7. .\" This is free software; you can redistribute it and/or modify
  8. .\" it under the terms of the GNU General Public License as published by
  9. .\" the Free Software Foundation; either version 2 of the License, or
  10. .\" (at your option) any later version.
  11. .\"
  12. .\" This is distributed in the hope that it will be useful,
  13. .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. .\" GNU General Public License for more details.
  16. .\"
  17. .\" You should have received a copy of the GNU General Public License
  18. .\" along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. .
  20. .TH dpkg\-buildflags 1 "2014-09-04" "Debian Project" "dpkg suite"
  21. .SH NAME
  22. dpkg\-buildflags \- returns build flags to use during package build
  23. .
  24. .SH SYNOPSIS
  25. .B dpkg\-buildflags
  26. .RI [ option "...] [" command ]
  27. .
  28. .SH DESCRIPTION
  29. \fBdpkg\-buildflags\fP is a tool to retrieve compilation flags to use during
  30. build of Debian packages.
  31. .
  32. The default flags are defined by the vendor but they can be
  33. extended/overriden in several ways:
  34. .IP 1.
  35. system-wide with \fB/etc/dpkg/buildflags.conf\fP;
  36. .IP 2.
  37. for the current user with \fB$XDG_CONFIG_HOME/dpkg/buildflags.conf\fP
  38. where \fB$XDG_CONFIG_HOME\fP defaults to \fB$HOME/.config\fP;
  39. .IP 3.
  40. temporarily by the user with environment variables (see section
  41. \fBENVIRONMENT\fP);
  42. .IP 4.
  43. dynamically by the package maintainer with environment variables set via
  44. \fBdebian/rules\fP (see section \fBENVIRONMENT\fP).
  45. .P
  46. The configuration files can contain two types of directives:
  47. .TP
  48. .BI SET " flag value"
  49. Override the flag named \fIflag\fP to have the value \fIvalue\fP.
  50. .TP
  51. .BI STRIP " flag value"
  52. Strip from the flag named \fIflag\fP all the build flags listed in \fIvalue\fP.
  53. .TP
  54. .BI APPEND " flag value"
  55. Extend the flag named \fIflag\fP by appending the options given in \fIvalue\fP.
  56. A space is prepended to the appended value if the flag's current value is non-empty.
  57. .TP
  58. .BI PREPEND " flag value"
  59. Extend the flag named \fIflag\fP by prepending the options given in \fIvalue\fP.
  60. A space is appended to the prepended value if the flag's current value is non-empty.
  61. .P
  62. The configuration files can contain comments on lines starting with a hash
  63. (#). Empty lines are also ignored.
  64. .SH COMMANDS
  65. .TP
  66. .BI \-\-dump
  67. Print to standard output all compilation flags and their values. It prints
  68. one flag per line separated from its value by an equal sign
  69. (\(lq\fIflag\fP=\fIvalue\fP\(rq). This is the default action.
  70. .TP
  71. .BI \-\-list
  72. Print the list of flags supported by the current vendor
  73. (one per line). See the \fBSUPPORTED FLAGS\fP section for more
  74. information about them.
  75. .TP
  76. .BI \-\-status
  77. Display any information that can be useful to explain the behaviour of
  78. \fBdpkg\-buildflags\fP: relevant environment variables, current vendor,
  79. state of all feature flags (since dpkg 1.16.5).
  80. Also print the resulting compiler flags with their origin.
  81. This is intended to be run from \fBdebian/rules\fP, so that the build log
  82. keeps a clear trace of the build flags used. This can be useful to diagnose
  83. problems related to them.
  84. .TP
  85. .BI \-\-export= format
  86. Print to standard output commands that can be used to export all the
  87. compilation flags for some particular tool. If the \fIformat\fP value is not
  88. given, \fBsh\fP is assumed. Only compilation flags starting with an
  89. upper case character are included, others are assumed to not be suitable
  90. for the environment. Supported formats:
  91. .RS
  92. .TP
  93. .B sh
  94. Shell commands to set and export all the compilation flags in the
  95. environment. The flag values are quoted so the output is ready for
  96. evaluation by a shell.
  97. .TP
  98. .B cmdline
  99. Arguments to pass to a build program's command line to use all the
  100. compilation flags (since dpkg 1.17.0). The flag values are quoted in
  101. shell syntax.
  102. .TP
  103. .B configure
  104. This is a legacy alias for \fBcmdline\fP.
  105. .TP
  106. .B make
  107. Make directives to set and export all the compilation flags in the
  108. environment. Output can be written to a makefile fragment and
  109. evaluated using an \fBinclude\fP directive.
  110. .RE
  111. .TP
  112. .BI \-\-get " flag"
  113. Print the value of the flag on standard output. Exits with 0
  114. if the flag is known otherwise exits with 1.
  115. .TP
  116. .BI \-\-origin " flag"
  117. Print the origin of the value that is returned by \fB\-\-get\fP. Exits
  118. with 0 if the flag is known otherwise exits with 1. The origin can be one
  119. of the following values:
  120. .RS
  121. .TP
  122. .B vendor
  123. the original flag set by the vendor is returned;
  124. .TP
  125. .B system
  126. the flag is set/modified by a system-wide configuration;
  127. .TP
  128. .B user
  129. the flag is set/modified by a user-specific configuration;
  130. .TP
  131. .B env
  132. the flag is set/modified by an environment-specific configuration.
  133. .RE
  134. .TP
  135. .BI \-\-query\-features " area"
  136. Print the features enabled for a given area (since dpkg 1.16.2).
  137. The only currently recognized
  138. areas on Debian and derivatives are \fBqa\fP, \fBreproducible\fP,
  139. \fBsanitize\fP and \fBhardening\fP, see the \fBFEATURE AREAS\fP
  140. section for more details.
  141. Exits with 0 if the area is known otherwise exits with 1.
  142. .IP
  143. The output is in RFC822 format, with one section per feature.
  144. For example:
  145. .IP
  146. .nf
  147. Feature: pie
  148. Enabled: no
  149. Feature: stackprotector
  150. Enabled: yes
  151. .fi
  152. .TP
  153. .B \-\-help
  154. Show the usage message and exit.
  155. .TP
  156. .B \-\-version
  157. Show the version and exit.
  158. .
  159. .SH SUPPORTED FLAGS
  160. .TP
  161. .B CFLAGS
  162. Options for the C compiler. The default value set by the vendor
  163. includes \fB\-g\fP and the default optimization level (\fB\-O2\fP usually,
  164. or \fB\-O0\fP if the \fBDEB_BUILD_OPTIONS\fP environment variable defines
  165. \fInoopt\fP).
  166. .TP
  167. .B CPPFLAGS
  168. Options for the C preprocessor. Default value: empty.
  169. .TP
  170. .B CXXFLAGS
  171. Options for the C++ compiler. Same as \fBCFLAGS\fP.
  172. .TP
  173. .B OBJCFLAGS
  174. Options for the Objective C compiler. Same as \fBCFLAGS\fP.
  175. .TP
  176. .B OBJCXXFLAGS
  177. Options for the Objective C++ compiler. Same as \fBCXXFLAGS\fP.
  178. .TP
  179. .B GCJFLAGS
  180. Options for the GNU Java compiler (gcj). A subset of \fBCFLAGS\fP.
  181. .TP
  182. .B FFLAGS
  183. Options for the Fortran 77 compiler. A subset of \fBCFLAGS\fP.
  184. .TP
  185. .B FCFLAGS
  186. Options for the Fortran 9x compiler. Same as \fBFFLAGS\fP.
  187. .TP
  188. .B LDFLAGS
  189. Options passed to the compiler when linking executables or shared
  190. objects (if the linker is called directly, then
  191. .B \-Wl
  192. and
  193. .B ,
  194. have to be stripped from these options). Default value: empty.
  195. .PP
  196. New flags might be added in the future if the need arises (for example
  197. to support other languages).
  198. .
  199. .SH FEATURE AREAS
  200. .P
  201. Each area feature can be enabled and disabled in the \fBDEB_BUILD_OPTIONS\fP
  202. and \fBDEB_BUILD_MAINT_OPTIONS\fP environment variable's area value with the
  203. \(oq\fB+\fP\(cq and \(oq\fB\-\fP\(cq modifier.
  204. For example, to enable the \fBhardening\fP \(lqpie\(rq feature and disable the
  205. \(lqfortify\(rq feature you can do this in \fBdebian/rules\fP:
  206. .P
  207. export DEB_BUILD_MAINT_OPTIONS=hardening=+pie,\-fortify
  208. .P
  209. The special feature \fBall\fP (valid in any area) can be used to enable or
  210. disable all area features at the same time.
  211. Thus disabling everything in the \fBhardening\fP area and enabling only
  212. \(lqformat\(rq and \(lqfortify\(rq can be achieved with:
  213. .P
  214. export DEB_BUILD_MAINT_OPTIONS=hardening=\-all,+format,+fortify
  215. .
  216. .SS Quality Assurance (QA)
  217. Several compile-time options (detailed below) can be used to help detect
  218. problems in the source code or build system.
  219. .TP
  220. .B bug
  221. This setting (disabled by default) adds any warning option that reliably
  222. detects problematic source code. The warnings are fatal.
  223. .TP
  224. .B canary
  225. This setting (disabled by default) adds dummy canary options to the build
  226. flags, so that the build logs can be checked for how the build flags
  227. propagate and to allow finding any omission of normal build flag settings.
  228. The only currently supported flags are \fBCPPFLAGS\fP, \fBCFLAGS\fP,
  229. \fBOBJCFLAGS\fP, \fBCXXFLAGS\fP and \fBOBJCXXFLAGS\fP with flags set
  230. to \fB\-D__DEB_CANARY_\fP\fIflag\fP_\fIrandom-id\fP\fB__\fP, and
  231. \fBLDFLAGS\fP set to \fB\-Wl,\-z,deb-canary\-\fP\fIrandom-id\fP.
  232. .
  233. .SS Sanitize
  234. Several compile-time options (detailed below) can be used to help sanitize
  235. a resulting binary against memory corruptions, memory leaks, use after free,
  236. threading data races and undefined behavior bugs.
  237. .TP
  238. .B address
  239. This setting (disabled by default) adds \fB\-fsanitize=address\fP to
  240. \fBLDFLAGS\fP and \fB\-fsanitize=address \-fno\-omit\-frame\-pointer\fP to
  241. \fBCFLAGS\fP and \fBCXXFLAGS\fP.
  242. .TP
  243. .B thread
  244. This setting (disabled by default) adds \fB\-fsanitize=thread\fP to
  245. \fBCFLAGS\fP, \fBCXXFLAGS\fP and \fBLDFLAGS\fP.
  246. .TP
  247. .B leak
  248. This setting (disabled by default) adds \fB\-fsanitize=leak\fP to
  249. \fBLDFLAGS\fP. It gets automatically disabled if either the \fBaddress\fP
  250. or the \fBthread\fP features are enabled, as they imply it.
  251. .TP
  252. .B undefined
  253. This setting (disabled by default) adds \fB\-fsanitize=undefined\fP to
  254. \fBCFLAGS\fP, \fBCXXFLAGS\fP and \fBLDFLAGS\fP.
  255. .SS Hardening
  256. Several compile-time options (detailed below) can be used to help harden
  257. a resulting binary against memory corruption attacks, or provide
  258. additional warning messages during compilation.
  259. Except as noted below, these are enabled by default for architectures
  260. that support them.
  261. .TP
  262. .B format
  263. This setting (enabled by default) adds
  264. .B \-Wformat \-Werror=format\-security
  265. to \fBCFLAGS\fP, \fBCXXFLAGS\fP, \fBOBJCFLAGS\fP and \fBOBJCXXFLAGS\fP.
  266. This will warn about improper format
  267. string uses, and will fail when format functions are used in a way
  268. that represent possible security problems. At present, this warns about
  269. calls to \fBprintf\fP and \fBscanf\fP functions where the format string is
  270. not a string literal and there are no format arguments, as in
  271. \fBprintf(foo);\fP instead of \fPprintf("%s", foo);\fP
  272. This may be a security hole if the format string came from untrusted
  273. input and contains \(oq%n\(cq.
  274. .
  275. .TP
  276. .B fortify
  277. This setting (enabled by default) adds
  278. .B \-D_FORTIFY_SOURCE=2
  279. to \fBCPPFLAGS\fP. During code generation the compiler
  280. knows a great deal of information about buffer sizes (where possible), and
  281. attempts to replace insecure unlimited length buffer function calls with
  282. length-limited ones. This is especially useful for old, crufty code.
  283. Additionally, format strings in writable memory that contain \(oq%n\(cq are
  284. blocked. If an application depends on such a format string, it will need
  285. to be worked around.
  286. Note that for this option to have any effect, the source must also
  287. be compiled with \fB\-O1\fP or higher. If the environment variable
  288. \fBDEB_BUILD_OPTIONS\fP contains \fInoopt\fP, then \fBfortify\fP
  289. support will be disabled, due to new warnings being issued by
  290. glibc 2.16 and later.
  291. .TP
  292. .B stackprotector
  293. This setting (enabled by default if stackprotectorstrong is not in use) adds
  294. .B \-fstack\-protector \-\-param=ssp\-buffer\-size=4
  295. to \fBCFLAGS\fP, \fBCXXFLAGS\fP, \fBOBJCFLAGS\fP, \fBOBJCXXFLAGS\fP,
  296. \fBGCJFLAGS\fP, \fBFFLAGS\fP and \fBFCFLAGS\fP.
  297. This adds safety checks against stack
  298. overwrites. This renders many potential code injection attacks into
  299. aborting situations. In the best case this turns code injection
  300. vulnerabilities into denial of service or into non-issues (depending on
  301. the application).
  302. This feature requires linking against glibc (or another provider of
  303. \fB__stack_chk_fail\fP), so needs to be disabled when building with
  304. \fB\-nostdlib\fP or \fB\-ffreestanding\fP or similar.
  305. .
  306. .TP
  307. .B stackprotectorstrong
  308. This setting (enabled by default) adds
  309. .B \-fstack\-protector\-strong
  310. to \fBCFLAGS\fP, \fBCXXFLAGS\fP, \fBOBJCFLAGS\fP, \fBOBJCXXFLAGS\fP,
  311. \fBGCJFLAGS\fP, \fBFFLAGS\fP and \fBFCFLAGS\fP.
  312. This is a stronger variant of \fBstackprotector\fP, but without significant
  313. performance penalties.
  314. Disabling \fBstackprotector\fP will also disable this setting.
  315. This feature has the same requirements as \fBstackprotector\fP, and in
  316. addition also requires gcc 4.9 and later.
  317. .
  318. .TP
  319. .B relro
  320. This setting (enabled by default) adds
  321. .B \-Wl,\-z,relro
  322. to \fBLDFLAGS\fP. During program load, several ELF memory sections need
  323. to be written to by the linker. This flags the loader to turn these
  324. sections read-only before turning over control to the program. Most
  325. notably this prevents GOT overwrite attacks. If this option is disabled,
  326. \fBbindnow\fP will become disabled as well.
  327. .
  328. .TP
  329. .B bindnow
  330. This setting (disabled by default) adds
  331. .B \-Wl,\-z,now
  332. to \fBLDFLAGS\fP. During program load, all dynamic symbols are resolved,
  333. allowing for the entire PLT to be marked read-only (due to \fBrelro\fP
  334. above). The option cannot become enabled if \fBrelro\fP is not enabled.
  335. .
  336. .TP
  337. .B pie
  338. This setting (disabled by default) adds \fB\-fPIE\fP to \fBCFLAGS\fP,
  339. \fBCXXFLAGS\fP, \fBOBJCFLAGS\fP, \fBOBJCXXFLAGS\fP, \fBGCJFLAGS\fP,
  340. \fBFFLAGS\fP and \fBFCFLAGS\fP,
  341. and \fB\-fPIE \-pie\fP to \fBLDFLAGS\fP. Position Independent
  342. Executable are needed to take advantage of Address Space Layout
  343. Randomization, supported by some kernel versions. While ASLR can already
  344. be enforced for data areas in the stack and heap (brk and mmap), the code
  345. areas must be compiled as position-independent. Shared libraries already
  346. do this (\-fPIC), so they gain ASLR automatically, but binary .text
  347. regions need to be build PIE to gain ASLR. When this happens, ROP (Return
  348. Oriented Programming) attacks are much harder since there are no static
  349. locations to bounce off of during a memory corruption attack.
  350. This is not compatible with \fB\-fPIC\fP so care must be taken when
  351. building shared objects.
  352. Additionally, since PIE is implemented via a general register, some
  353. architectures (most notably i386) can see performance losses of up to
  354. 15% in very text-segment-heavy application workloads; most workloads
  355. see less than 1%. Architectures with more general registers (e.g. amd64)
  356. do not see as high a worst-case penalty.
  357. .SS Reproducibility
  358. The compile-time options detailed below can be used to help improve
  359. build reproducibility or provide additional warning messages during
  360. compilation. Except as noted below, these are enabled by default for
  361. architectures that support them.
  362. .TP
  363. .B timeless
  364. This setting (disabled by default) adds
  365. .B \-Wdate\-time
  366. to \fBCPPFLAGS\fP.
  367. This will cause warnings when the \fB__TIME__\fP, \fB__DATE__\fP and
  368. \fB\%__TIMESTAMP__\fP macros are used.
  369. .
  370. .SH ENVIRONMENT
  371. There are 2 sets of environment variables doing the same operations, the
  372. first one (DEB_\fIflag\fP_\fIop\fP) should never be used within
  373. \fBdebian/rules\fP. It's meant for any user that wants to rebuild the
  374. source package with different build flags. The second set
  375. (DEB_\fIflag\fP_MAINT_\fIop\fP) should only be used in \fBdebian/rules\fP
  376. by package maintainers to change the resulting build flags.
  377. .TP
  378. .BI DEB_ flag _SET
  379. .TQ
  380. .BI DEB_ flag _MAINT_SET
  381. This variable can be used to force the value returned for the given
  382. \fIflag\fP.
  383. .TP
  384. .BI DEB_ flag _STRIP
  385. .TQ
  386. .BI DEB_ flag _MAINT_STRIP
  387. This variable can be used to provide a space separated list of options
  388. that will be stripped from the set of flags returned for the given
  389. \fIflag\fP.
  390. .TP
  391. .BI DEB_ flag _APPEND
  392. .TQ
  393. .BI DEB_ flag _MAINT_APPEND
  394. This variable can be used to append supplementary options to the value
  395. returned for the given \fIflag\fP.
  396. .TP
  397. .BI DEB_ flag _PREPEND
  398. .TQ
  399. .BI DEB_ flag _MAINT_PREPEND
  400. This variable can be used to prepend supplementary options to the value
  401. returned for the given \fIflag\fP.
  402. .TP
  403. .B DEB_BUILD_OPTIONS
  404. .TQ
  405. .B DEB_BUILD_MAINT_OPTIONS
  406. These variables can be used by a user or maintainer to disable/enable
  407. various area features that affect build flags.
  408. The \fBDEB_BUILD_MAINT_OPTIONS\fP variable overrides any setting in the
  409. \fBDEB_BUILD_OPTIONS\fP feature areas.
  410. See the \fBFEATURE AREAS\fP section for details.
  411. .
  412. .SH FILES
  413. .SS Configuration files
  414. .TP
  415. .B /etc/dpkg/buildflags.conf
  416. System wide configuration file.
  417. .TP
  418. .BR $XDG_CONFIG_HOME/dpkg/buildflags.conf " or "
  419. .TQ
  420. .BR $HOME/.config/dpkg/buildflags.conf
  421. User configuration file.
  422. .SS Packaging support
  423. .TP
  424. .B /usr/share/dpkg/buildflags.mk
  425. Makefile snippet that will load (and optionally export) all flags
  426. supported by \fBdpkg-buildflags\fP into variables (since dpkg 1.16.1).
  427. .
  428. .SH ENVIRONMENT
  429. .TP
  430. .B DEB_VENDOR
  431. This setting defines the current vendor. If not set, it will discover the
  432. current vendor by reading \fB\%/etc/dpkg/origins/default\fP.
  433. .
  434. .SH EXAMPLES
  435. To pass build flags to a build command in a makefile:
  436. .PP
  437. .RS 4
  438. .nf
  439. $(MAKE) $(shell dpkg\-buildflags \-\-export=cmdline)
  440. \&./configure $(shell dpkg\-buildflags \-\-export=cmdline)
  441. .fi
  442. .RE
  443. .PP
  444. To set build flags in a shell script or shell fragment, \fBeval\fP can be
  445. used to interpret the output and to export the flags in the environment:
  446. .PP
  447. .RS 4
  448. .nf
  449. eval "$(dpkg\-buildflags \-\-export=sh)" && make
  450. .fi
  451. .RE
  452. .PP
  453. or to set the positional parameters to pass to a command:
  454. .PP
  455. .RS 4
  456. .nf
  457. eval "set \-\- $(dpkg\-buildflags \-\-export=cmdline)"
  458. for dir in a b c; do (cd $dir && ./configure "$@" && make); done
  459. .fi
  460. .RE
  461. .
  462. .SS Usage in debian/rules
  463. You should call \fBdpkg\-buildflags\fP or include \fBbuildflags.mk\fP
  464. from the \fBdebian/rules\fP file to obtain the needed build flags to
  465. pass to the build system.
  466. Note that older versions of \fBdpkg\-buildpackage\fP (before dpkg 1.16.1)
  467. exported these flags automatically. However, you should not rely on this,
  468. since this breaks manual invocation of \fBdebian/rules\fP.
  469. .PP
  470. For packages with autoconf-like build systems, you can pass the relevant
  471. options to configure or \fBmake\fP(1) directly, as shown above.
  472. .PP
  473. For other build systems, or when you need more fine-grained control
  474. about which flags are passed where, you can use \fB\-\-get\fP. Or you
  475. can include \fBbuildflags.mk\fP instead, which takes care of calling
  476. \fB\%dpkg\-buildflags\fP and storing the build flags in make variables.
  477. .PP
  478. If you want to export all buildflags into the environment (where they
  479. can be picked up by your build system):
  480. .PP
  481. .RS 4
  482. .nf
  483. DPKG_EXPORT_BUILDFLAGS = 1
  484. include /usr/share/dpkg/buildflags.mk
  485. .fi
  486. .RE
  487. .PP
  488. For some extra control over what is exported, you can manually export
  489. the variables (as none are exported by default):
  490. .PP
  491. .RS 4
  492. .nf
  493. include /usr/share/dpkg/buildflags.mk
  494. export CPPFLAGS CFLAGS LDFLAGS
  495. .fi
  496. .RE
  497. .PP
  498. And you can of course pass the flags to commands manually:
  499. .PP
  500. .RS 4
  501. .nf
  502. include /usr/share/dpkg/buildflags.mk
  503. build\-arch:
  504. \& $(CC) \-o hello hello.c $(CPPFLAGS) $(CFLAGS) $(LDFLAGS)
  505. .fi
  506. .RE