1 |
# __rustc: path to the default rustc executable |
2 |
%__rustc /usr/bin/rustc |
3 |
|
4 |
# __rustdoc: path to the default rustdoc executable |
5 |
%__rustdoc /usr/bin/rustdoc |
6 |
|
7 |
# rustflags_opt_level: default optimization level |
8 |
# |
9 |
# It corresponds to the "-Copt-level" rustc command line option. |
10 |
%rustflags_opt_level 3 |
11 |
|
12 |
# rustflags_debuginfo: default verbosity of debug information |
13 |
# |
14 |
# It corresponds to the "-Cdebuginfo" rustc command line option. |
15 |
# In some cases, it might be required to override this macro with "1" or even |
16 |
# "0", if memory usage gets too high during builds on some resource-constrained |
17 |
# architectures (most likely on 32-bit architectures), which will however |
18 |
# reduce the quality of the produced debug symbols. |
19 |
%rustflags_debuginfo 2 |
20 |
|
21 |
# rustflags_codegen_units: default number of parallel code generation units |
22 |
# |
23 |
# The default value of "1" results in generation of better code, but comes at |
24 |
# the cost of longer build times. |
25 |
%rustflags_codegen_units 1 |
26 |
|
27 |
# build_rustflags: default compiler flags for rustc (RUSTFLAGS) |
28 |
# |
29 |
# -Copt-level: set optimization level (default: highest optimization level) |
30 |
# -Cdebuginfo: set debuginfo verbosity (default: full debug information) |
31 |
# -Ccodegen-units: set number of parallel code generation units (default: 1) |
32 |
# -Cforce-frame-pointers: force inclusion of frame pointers (default: enabled |
33 |
# on x86_64 and aarch64 on Fedora 37+) |
34 |
# |
35 |
# Additionally, some linker flags are set which correspond to the default |
36 |
# Fedora compiler flags for hardening and for embedding package versions into |
37 |
# compiled binaries. |
38 |
# |
39 |
# ref. https://doc.rust-lang.org/rustc/codegen-options/index.html |
40 |
%build_rustflags %{shrink: |
41 |
-Copt-level=%rustflags_opt_level |
42 |
-Cdebuginfo=%rustflags_debuginfo |
43 |
-Ccodegen-units=%rustflags_codegen_units |
44 |
-Cstrip=none |
45 |
%{expr:0%{?_include_frame_pointers} && ("%{_arch}" != "ppc64le" && "%{_arch}" != "s390x" && "%{_arch}" != "i386") ? "-Cforce-frame-pointers=yes" : ""} |
46 |
%[0%{?_package_note_status} ? "-Clink-arg=%_package_note_flags" : ""] |
47 |
} |
48 |
|
49 |
# __cargo: cargo command with environment variables |
50 |
# |
51 |
# CARGO_HOME: This ensures cargo reads configuration file from .cargo/config.toml, |
52 |
# and prevents writing any files to $HOME during RPM builds. |
53 |
%__cargo /usr/bin/env CARGO_HOME=.cargo RUSTFLAGS='%{build_rustflags}' /usr/bin/cargo |
54 |
|
55 |
# __cargo_common_opts: common command line flags for cargo |
56 |
# |
57 |
# _smp_mflags: run builds and tests in parallel |
58 |
%__cargo_common_opts %{?_smp_mflags} |
59 |
|
60 |
# cargo_prep: macro to set up build environment for cargo projects |
61 |
# |
62 |
# This involves four steps: |
63 |
# - create the ".cargo" directory if it doesn't exist yet |
64 |
# - dump custom cargo configuration into ".cargo/config.toml" |
65 |
# - remove "Cargo.lock" if it exists (it breaks builds with custom cargo config) |
66 |
# - remove "Cargo.toml.orig" if it exists (it breaks running "cargo package") |
67 |
# |
68 |
# Options: |
69 |
# -V <number> - unpack and use vendored sources from Source<number> tarball |
70 |
# (deprecated; use -v instead) |
71 |
# -v <directory> - use vendored sources from <directory> |
72 |
# -N - Don't set up any registry. Only set up the build configuration. |
73 |
%cargo_prep(V:v:N)\ |
74 |
%{-v:%{-V:%{error:-v and -V are mutually exclusive!}}}\ |
75 |
%{-v:%{-N:%{error:-v and -N are mutually exclusive!}}}\ |
76 |
(\ |
77 |
set -euo pipefail\ |
78 |
%{__mkdir} -p target/rpm\ |
79 |
/usr/bin/ln -s rpm target/release\ |
80 |
%{__rm} -rf .cargo/\ |
81 |
%{__mkdir} -p .cargo\ |
82 |
cat > .cargo/config.toml << EOF\ |
83 |
[build]\ |
84 |
rustc = "%{__rustc}"\ |
85 |
rustdoc = "%{__rustdoc}"\ |
86 |
\ |
87 |
[profile.rpm]\ |
88 |
inherits = "release"\ |
89 |
opt-level = %{rustflags_opt_level}\ |
90 |
codegen-units = %{rustflags_codegen_units}\ |
91 |
debug = %{rustflags_debuginfo}\ |
92 |
strip = "none"\ |
93 |
\ |
94 |
[env]\ |
95 |
CFLAGS = "%{build_cflags}"\ |
96 |
CXXFLAGS = "%{build_cxxflags}"\ |
97 |
LDFLAGS = "%{build_ldflags}"\ |
98 |
\ |
99 |
[install]\ |
100 |
root = "%{buildroot}%{_prefix}"\ |
101 |
\ |
102 |
[term]\ |
103 |
verbose = true\ |
104 |
EOF\ |
105 |
%{-V:%{__tar} -xoaf %{S:%{-V*}}}\ |
106 |
%{!?-N:\ |
107 |
cat >> .cargo/config.toml << EOF\ |
108 |
[source.vendored-sources]\ |
109 |
directory = "%{-v*}%{-V:./vendor}"\ |
110 |
\ |
111 |
[source.crates-io]\ |
112 |
registry = "https://crates.io"\ |
113 |
replace-with = "vendored-sources"\ |
114 |
EOF}\ |
115 |
%{__rm} -f Cargo.toml.orig\ |
116 |
) |
117 |
|
118 |
# __cargo_parse_opts: function-like macro which parses common flags into the |
119 |
# equivalent command-line flags for cargo |
120 |
%__cargo_parse_opts(naf:) %{shrink:\ |
121 |
%{-n:%{-a:%{error:Can't specify both -n and -a}}} \ |
122 |
%{-f:%{-a:%{error:Can't specify both -f(%{-f*}) and -a}}} \ |
123 |
%{-n:--no-default-features} \ |
124 |
%{-a:--all-features} \ |
125 |
%{-f:--features %{-f*}} \ |
126 |
%{nil} \ |
127 |
} |
128 |
|
129 |
# cargo_build: builds the crate with cargo with the specified feature flags |
130 |
%cargo_build(naf:)\ |
131 |
%{shrink: \ |
132 |
%{__cargo} build \ |
133 |
%{__cargo_common_opts} \ |
134 |
--profile rpm \ |
135 |
%{__cargo_parse_opts %{-n} %{-a} %{-f:-f%{-f*}}} \ |
136 |
%* \ |
137 |
} |
138 |
|
139 |
# cargo_test: runs the test suite with cargo with the specified feature flags |
140 |
# |
141 |
# To pass command-line arguments to the cargo test runners directly (for |
142 |
# example, to skip certain tests during package builds), both the cargo_test |
143 |
# macro argument parsing and "cargo test" argument parsing need to be bypassed, |
144 |
# i.e. "%%cargo_test -- -- --skip foo" for skipping all tests with names that |
145 |
# match "foo". |
146 |
%cargo_test(naf:)\ |
147 |
%{shrink: \ |
148 |
%{__cargo} test \ |
149 |
%{__cargo_common_opts} \ |
150 |
--profile rpm \ |
151 |
--no-fail-fast \ |
152 |
%{__cargo_parse_opts %{-n} %{-a} %{-f:-f%{-f*}}} \ |
153 |
%* \ |
154 |
} |
155 |
|
156 |
# cargo_install: install files into the buildroot |
157 |
# |
158 |
# For "binary" crates, this macro installs all "bin" build targets to _bindir |
159 |
# inside the buildroot. The "--no-track" option prevents the creation of the |
160 |
# "$CARGO_HOME/.crates.toml" file, which is used to keep track of which version |
161 |
# of a specific binary has been installed, but which conflicts between builds |
162 |
# of different Rust applications and is not needed when building RPM packages. |
163 |
%cargo_install(t:naf:)\ |
164 |
(\ |
165 |
set -euo pipefail \ |
166 |
%{shrink: \ |
167 |
%{__cargo} install \ |
168 |
%{__cargo_common_opts} \ |
169 |
--profile rpm \ |
170 |
--no-track \ |
171 |
--path . \ |
172 |
%{__cargo_parse_opts %{-n} %{-a} %{-f:-f%{-f*}}} \ |
173 |
%* \ |
174 |
} \ |
175 |
) |
176 |
|
177 |
# cargo_license: print license information for all crate dependencies |
178 |
# |
179 |
# The "no-build,no-dev,no-proc-macro" argument results in only crates which are |
180 |
# linked into the final binary to be considered. |
181 |
# |
182 |
# Additionally, deprecated SPDX syntax ("/" instead of "OR") is normalized |
183 |
# before sorting the results to ensure reproducible output of this macro. |
184 |
# |
185 |
# This macro must be called with the same feature flags as other cargo macros, |
186 |
# in particular, "cargo_build", otherwise its output will be incomplete. |
187 |
# |
188 |
# The "cargo tree" command called by this macro will fail if there are missing |
189 |
# (optional) dependencies. |
190 |
%cargo_license(naf:)\ |
191 |
(\ |
192 |
set -euo pipefail\ |
193 |
%{shrink: \ |
194 |
%{__cargo} tree \ |
195 |
--workspace \ |
196 |
--offline \ |
197 |
--edges no-build,no-dev,no-proc-macro \ |
198 |
--no-dedupe \ |
199 |
%{__cargo_parse_opts %{-n} %{-a} %{-f:-f%{-f*}}} \ |
200 |
--prefix none \ |
201 |
--format "{l}: {p}" \ |
202 |
| sed -e "s: ($(pwd)[^)]*)::g" -e "s: / :/:g" -e "s:/: OR :g" \ |
203 |
| sort -u \ |
204 |
}\ |
205 |
) |
206 |
|
207 |
# cargo_license_summary: print license summary for all crate dependencies |
208 |
# |
209 |
# This macro works in the same way as cargo_license, except that it only prints |
210 |
# a list of licenses, and not the complete license information for every crate |
211 |
# in the dependency tree. This is useful for determining the correct License |
212 |
# tag for packages that contain compiled Rust binaries. |
213 |
%cargo_license_summary(naf:)\ |
214 |
(\ |
215 |
set -euo pipefail\ |
216 |
%{shrink: \ |
217 |
%{__cargo} tree \ |
218 |
--workspace \ |
219 |
--offline \ |
220 |
--edges no-build,no-dev,no-proc-macro \ |
221 |
--no-dedupe \ |
222 |
%{__cargo_parse_opts %{-n} %{-a} %{-f:-f%{-f*}}} \ |
223 |
--prefix none \ |
224 |
--format "# {l}" \ |
225 |
| sed -e "s: / :/:g" -e "s:/: OR :g" \ |
226 |
| sort -u \ |
227 |
}\ |
228 |
) |
229 |
|
230 |
# cargo_vendor_manifest: write list of vendored crates and their versions |
231 |
# |
232 |
# The arguments for the internal "cargo tree" call emulate the logic |
233 |
# that determines which crates are included when running "cargo vendor". |
234 |
# The results are written to "cargo-vendor.txt". |
235 |
# |
236 |
# TODO: --all-features may be overly broad; this should be modified to |
237 |
# use %%__cargo_parse_opts to handle feature flags. |
238 |
%cargo_vendor_manifest()\ |
239 |
(\ |
240 |
set -euo pipefail\ |
241 |
%{shrink: \ |
242 |
%{__cargo} tree \ |
243 |
--workspace \ |
244 |
--offline \ |
245 |
--edges normal,build \ |
246 |
--no-dedupe \ |
247 |
--all-features \ |
248 |
--prefix none \ |
249 |
--format "{p}" \ |
250 |
| grep -v "$(pwd)" \ |
251 |
| sed -e "s: (proc-macro)::" \ |
252 |
| sort -u \ |
253 |
> cargo-vendor.txt \ |
254 |
}\ |
255 |
) |
256 |
|