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
|
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::{env, fs, str};
/// Static library that will be built
const LIB_NAME: &str = "musl_math_prefixed";
/// Files that have more than one symbol. Map of file names to the symbols defined in that file.
const MULTIPLE_SYMBOLS: &[(&str, &[&str])] = &[
(
"__invtrigl",
&["__invtrigl", "__invtrigl_R", "__pio2_hi", "__pio2_lo"],
),
("__polevll", &["__polevll", "__p1evll"]),
("erf", &["erf", "erfc"]),
("erff", &["erff", "erfcf"]),
("erfl", &["erfl", "erfcl"]),
("exp10", &["exp10", "pow10"]),
("exp10f", &["exp10f", "pow10f"]),
("exp10l", &["exp10l", "pow10l"]),
("exp2f_data", &["exp2f_data", "__exp2f_data"]),
("exp_data", &["exp_data", "__exp_data"]),
("j0", &["j0", "y0"]),
("j0f", &["j0f", "y0f"]),
("j1", &["j1", "y1"]),
("j1f", &["j1f", "y1f"]),
("jn", &["jn", "yn"]),
("jnf", &["jnf", "ynf"]),
("lgamma", &["lgamma", "__lgamma_r"]),
("remainder", &["remainder", "drem"]),
("remainderf", &["remainderf", "dremf"]),
("lgammaf", &["lgammaf", "lgammaf_r", "__lgammaf_r"]),
("lgammal", &["lgammal", "lgammal_r", "__lgammal_r"]),
("log2_data", &["log2_data", "__log2_data"]),
("log2f_data", &["log2f_data", "__log2f_data"]),
("log_data", &["log_data", "__log_data"]),
("logf_data", &["logf_data", "__logf_data"]),
("pow_data", &["pow_data", "__pow_log_data"]),
("powf_data", &["powf_data", "__powf_log2_data"]),
("signgam", &["signgam", "__signgam"]),
("sqrt_data", &["sqrt_data", "__rsqrt_tab"]),
];
fn main() {
let cfg = Config::from_env();
if cfg.target_env == "msvc"
|| cfg.target_family == "wasm"
|| cfg.target_features.iter().any(|f| f == "thumb-mode")
{
println!(
"cargo::warning=Musl doesn't compile with the current \
target {}; skipping build",
&cfg.target_string
);
return;
}
build_musl_math(&cfg);
}
#[allow(dead_code)]
#[derive(Debug)]
struct Config {
manifest_dir: PathBuf,
out_dir: PathBuf,
musl_dir: PathBuf,
musl_arch: String,
target_arch: String,
target_env: String,
target_family: String,
target_os: String,
target_string: String,
target_vendor: String,
target_features: Vec<String>,
}
impl Config {
fn from_env() -> Self {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let target_features = env::var("CARGO_CFG_TARGET_FEATURE")
.map(|feats| feats.split(',').map(ToOwned::to_owned).collect())
.unwrap_or_default();
let musl_dir = manifest_dir.join("musl");
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let musl_arch = if target_arch == "x86" {
"i386".to_owned()
} else {
target_arch.clone()
};
println!(
"cargo::rerun-if-changed={}/c_patches",
manifest_dir.display()
);
println!("cargo::rerun-if-changed={}", musl_dir.display());
Self {
manifest_dir,
out_dir: PathBuf::from(env::var("OUT_DIR").unwrap()),
musl_dir,
musl_arch,
target_arch,
target_env: env::var("CARGO_CFG_TARGET_ENV").unwrap(),
target_family: env::var("CARGO_CFG_TARGET_FAMILY").unwrap(),
target_os: env::var("CARGO_CFG_TARGET_OS").unwrap(),
target_string: env::var("TARGET").unwrap(),
target_vendor: env::var("CARGO_CFG_TARGET_VENDOR").unwrap(),
target_features,
}
}
}
/// Build musl math symbols to a static library
fn build_musl_math(cfg: &Config) {
let musl_dir = &cfg.musl_dir;
let math = musl_dir.join("src/math");
let arch_dir = musl_dir.join("arch").join(&cfg.musl_arch);
assert!(
math.exists(),
"musl source not found. You may need to run `./ci/update-musl.sh`."
);
let source_map = find_math_source(&math, cfg);
let out_path = cfg.out_dir.join(format!("lib{LIB_NAME}.a"));
// Run configuration steps. Usually done as part of the musl `Makefile`.
let obj_include = cfg.out_dir.join("musl_obj/include");
fs::create_dir_all(&obj_include).unwrap();
fs::create_dir_all(obj_include.join("bits")).unwrap();
let sed_stat = Command::new("sed")
.arg("-f")
.arg(musl_dir.join("tools/mkalltypes.sed"))
.arg(arch_dir.join("bits/alltypes.h.in"))
.arg(musl_dir.join("include/alltypes.h.in"))
.stderr(Stdio::inherit())
.output()
.unwrap();
assert!(
sed_stat.status.success(),
"sed command failed: {:?}",
sed_stat.status
);
fs::write(obj_include.join("bits/alltypes.h"), sed_stat.stdout).unwrap();
let mut cbuild = cc::Build::new();
cbuild
.extra_warnings(false)
.warnings(false)
.flag_if_supported("-Wno-bitwise-op-parentheses")
.flag_if_supported("-Wno-literal-range")
.flag_if_supported("-Wno-parentheses")
.flag_if_supported("-Wno-shift-count-overflow")
.flag_if_supported("-Wno-shift-op-parentheses")
.flag_if_supported("-Wno-unused-but-set-variable")
.flag_if_supported("-std=c99")
.flag_if_supported("-ffreestanding")
.flag_if_supported("-nostdinc")
.define("_ALL_SOURCE", "1")
.define(
"ROOT_INCLUDE_FEATURES",
Some(musl_dir.join("include/features.h").to_str().unwrap()),
)
// Our overrides are in this directory
.include(cfg.manifest_dir.join("c_patches"))
.include(musl_dir.join("arch").join(&cfg.musl_arch))
.include(musl_dir.join("arch/generic"))
.include(musl_dir.join("src/include"))
.include(musl_dir.join("src/internal"))
.include(obj_include)
.include(musl_dir.join("include"))
.file(cfg.manifest_dir.join("c_patches/alias.c"));
for (sym_name, src_file) in source_map {
// Build the source file
cbuild.file(src_file);
// Trickery! Redefine the symbol names to have the prefix `musl_`, which allows us to
// differentiate these symbols from whatever we provide.
if let Some((_names, syms)) = MULTIPLE_SYMBOLS
.iter()
.find(|(name, _syms)| *name == sym_name)
{
// Handle the occasional file that defines multiple symbols
for sym in *syms {
cbuild.define(sym, Some(format!("musl_{sym}").as_str()));
}
} else {
// If the file doesn't define multiple symbols, the file name will be the symbol
cbuild.define(&sym_name, Some(format!("musl_{sym_name}").as_str()));
}
}
if cfg!(windows) {
// On Windows we don't have a good way to check symbols, so skip that step.
cbuild.compile(LIB_NAME);
return;
}
let objfiles = cbuild.compile_intermediates();
// We create the archive ourselves with relocations rather than letting `cc` do it so we can
// encourage it to resolve symbols now. This should help avoid accidentally linking the wrong
// thing.
let stat = cbuild
.get_compiler()
.to_command()
.arg("-r")
.arg("-o")
.arg(&out_path)
.args(objfiles)
.status()
.unwrap();
assert!(stat.success());
println!("cargo::rustc-link-lib={LIB_NAME}");
println!("cargo::rustc-link-search=native={}", cfg.out_dir.display());
validate_archive_symbols(&out_path);
}
/// Build a map of `name -> path`. `name` is typically the symbol name, but this doesn't account
/// for files that provide multiple symbols.
fn find_math_source(math_root: &Path, cfg: &Config) -> BTreeMap<String, PathBuf> {
let mut map = BTreeMap::new();
let mut arch_dir = None;
// Locate all files and directories
for item in fs::read_dir(math_root).unwrap() {
let path = item.unwrap().path();
let meta = fs::metadata(&path).unwrap();
if meta.is_dir() {
// Make note of the arch-specific directory if it exists
if path.file_name().unwrap() == cfg.target_arch.as_str() {
arch_dir = Some(path);
}
continue;
}
// Skip non-source files
if path.extension().is_some_and(|ext| ext == "h") {
continue;
}
let sym_name = path.file_stem().unwrap();
map.insert(sym_name.to_str().unwrap().to_owned(), path.to_owned());
}
// If arch-specific versions are available, build those instead.
if let Some(arch_dir) = arch_dir {
for item in fs::read_dir(arch_dir).unwrap() {
let path = item.unwrap().path();
let sym_name = path.file_stem().unwrap();
if path.extension().unwrap() == "s" {
// FIXME: we never build assembly versions since we have no good way to
// rename the symbol (our options are probably preprocessor or objcopy).
continue;
}
map.insert(sym_name.to_str().unwrap().to_owned(), path);
}
}
map
}
/// Make sure we don't have something like a loose unprefixed `_cos` called somewhere, which could
/// wind up linking to system libraries rather than the built musl library.
fn validate_archive_symbols(out_path: &Path) {
const ALLOWED_UNDEF_PFX: &[&str] = &[
// PIC and arch-specific
".TOC",
"_GLOBAL_OFFSET_TABLE_",
"__x86.get_pc_thunk",
// gcc/compiler-rt/compiler-builtins symbols
"__add",
"__aeabi_",
"__div",
"__eq",
"__extend",
"__fix",
"__float",
"__gcc_",
"__ge",
"__gt",
"__le",
"__lshr",
"__lt",
"__mul",
"__ne",
"__stack_chk_fail",
"__stack_chk_guard",
"__sub",
"__trunc",
"__undef",
// string routines
"__bzero",
"bzero",
// FPENV interfaces
"feclearexcept",
"fegetround",
"feraiseexcept",
"fesetround",
"fetestexcept",
];
// List global undefined symbols
let out = Command::new("nm")
.arg("-guj")
.arg(out_path)
.stderr(Stdio::inherit())
.output()
.unwrap();
let undef = str::from_utf8(&out.stdout).unwrap();
let mut undef = undef.lines().collect::<Vec<_>>();
undef.retain(|sym| {
// Account for file formats that add a leading `_`
!ALLOWED_UNDEF_PFX
.iter()
.any(|pfx| sym.starts_with(pfx) || sym[1..].starts_with(pfx))
});
assert!(
undef.is_empty(),
"found disallowed undefined symbols: {undef:#?}"
);
// Find any symbols that are missing the `_musl_` prefix`
let out = Command::new("nm")
.arg("-gUj")
.arg(out_path)
.stderr(Stdio::inherit())
.output()
.unwrap();
let defined = str::from_utf8(&out.stdout).unwrap();
let mut defined = defined.lines().collect::<Vec<_>>();
defined.retain(|sym| {
!(sym.starts_with("_musl_")
|| sym.starts_with("musl_")
|| sym.starts_with("__x86.get_pc_thunk"))
});
assert!(defined.is_empty(), "found unprefixed symbols: {defined:#?}");
}
|