Skip to content

Commit cacf98b

Browse files
authored
fix(github-release): πŸ› detect the lib version needed (musl/libc) (#899)
This replaces the `prefer_musl` flag by a detection, which makes more sense actually, since it would depend on the current system. Partially reverts 24faed9
1 parent 12f5d81 commit cacf98b

File tree

5 files changed

+68
-32
lines changed

5 files changed

+68
-32
lines changed

β€Žsrc/internal/cache/github_release.rsβ€Ž

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ lazy_static! {
5151
Ok(dist_re) => dist_re,
5252
Err(err) => panic!("failed to create dist regex: {}", err),
5353
};
54-
static ref MUSL_REGEX: Regex = match Regex::new(r"(?i)(\b|_)(musl)(\b|_)") {
55-
Ok(musl_re) => musl_re,
56-
Err(err) => panic!("failed to create musl regex: {}", err),
57-
};
5854
static ref SEPARATOR_MID_REGEX: Regex = match Regex::new(r"([-_]{2,})") {
5955
Ok(separator_re) => separator_re,
6056
Err(err) => panic!("failed to create separator regex: {}", err),
@@ -176,7 +172,6 @@ pub struct GithubReleasesSelector {
176172
pub skip_arch_matching: bool,
177173
pub skip_os_matching: bool,
178174
pub prefer_dist: bool,
179-
pub prefer_musl: bool,
180175
pub checksum_lookup: bool,
181176
pub checksum_algorithm: Option<String>,
182177
pub checksum_asset_name: Option<String>,
@@ -225,11 +220,6 @@ impl GithubReleasesSelector {
225220
self
226221
}
227222

228-
pub fn prefer_musl(mut self, prefer_musl: bool) -> Self {
229-
self.prefer_musl = prefer_musl;
230-
self
231-
}
232-
233223
pub fn checksum_lookup(mut self, checksum_lookup: bool) -> Self {
234224
self.checksum_lookup = checksum_lookup;
235225
self
@@ -286,11 +276,10 @@ impl GithubReleasesSelector {
286276
};
287277

288278
// Add a malus if the asset is (or not) a musl (depending on preferrence)
289-
let musl_pref_malus = if MUSL_REGEX.is_match(&asset_name) == self.prefer_musl {
290-
0
291-
} else {
292-
1
293-
};
279+
#[cfg(target_os = "linux")]
280+
let musl_pref_malus = check_musl(&asset_name);
281+
#[cfg(not(target_os = "linux"))]
282+
let musl_pref_malus = 0;
294283

295284
// Return final score
296285
return match_level + dist_pref_malus + musl_pref_malus;
@@ -502,6 +491,23 @@ impl GithubReleasesSelector {
502491
}
503492
}
504493

494+
#[cfg(target_os = "linux")]
495+
#[inline]
496+
fn check_musl(asset_name: &str) -> i32 {
497+
use crate::internal::utils::detect_libc;
498+
use once_cell::sync::Lazy;
499+
500+
static PREFER_MUSL: Lazy<bool> = Lazy::new(|| !detect_libc());
501+
static MUSL_REGEX: Lazy<Regex> =
502+
Lazy::new(|| Regex::new(r"(?i)(\b|_)(musl)(\b|_)").expect("failed to create musl regex"));
503+
504+
if MUSL_REGEX.is_match(asset_name) == *PREFER_MUSL {
505+
0
506+
} else {
507+
1
508+
}
509+
}
510+
505511
#[derive(Debug, Serialize, Deserialize, Clone)]
506512
pub struct GithubReleases {
507513
pub releases: Vec<GithubReleaseVersion>,

β€Žsrc/internal/config/up/github_release.rsβ€Ž

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -517,14 +517,6 @@ pub struct UpConfigGithubRelease {
517517
)]
518518
pub prefer_dist: bool,
519519

520-
/// Whether to prefer the 'musl' assets over the 'glibc' assets.
521-
/// This defaults to false to use native glibc binaries by default
522-
#[serde(
523-
default = "cache_utils::set_false",
524-
skip_serializing_if = "cache_utils::is_false"
525-
)]
526-
pub prefer_musl: bool,
527-
528520
/// The URL of the GitHub API; this is only required if downloading
529521
/// using Github Enterprise. By default, this is set to the public
530522
/// GitHub API URL (https://api.github.com). If you are using
@@ -572,7 +564,6 @@ impl Default for UpConfigGithubRelease {
572564
skip_os_matching: false,
573565
skip_arch_matching: false,
574566
prefer_dist: false,
575-
prefer_musl: false,
576567
api_url: None,
577568
checksum: GithubReleaseChecksumConfig::default(),
578569
auth: GithubAuthConfig::default(),
@@ -742,11 +733,6 @@ impl UpConfigGithubRelease {
742733
false,
743734
&error_handler.with_key("prefer_dist"),
744735
);
745-
let prefer_musl = config_value.get_as_bool_or_default(
746-
"prefer_musl",
747-
false,
748-
&error_handler.with_key("prefer_musl"),
749-
);
750736
let api_url =
751737
config_value.get_as_str_or_none("api_url", &error_handler.with_key("api_url"));
752738
let checksum = GithubReleaseChecksumConfig::from_config_value(
@@ -769,7 +755,6 @@ impl UpConfigGithubRelease {
769755
skip_os_matching,
770756
skip_arch_matching,
771757
prefer_dist,
772-
prefer_musl,
773758
api_url,
774759
checksum,
775760
auth,
@@ -1321,7 +1306,6 @@ impl UpConfigGithubRelease {
13211306
.skip_os_matching(self.skip_os_matching)
13221307
.skip_arch_matching(self.skip_arch_matching)
13231308
.prefer_dist(self.prefer_dist)
1324-
.prefer_musl(self.prefer_musl)
13251309
.checksum_lookup(self.checksum.is_enabled())
13261310
.checksum_algorithm(self.checksum.algorithm.clone().map(|a| a.to_string()))
13271311
.checksum_asset_name(self.checksum.asset_name.clone()),

β€Žsrc/internal/utils/libc.rsβ€Ž

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::path::Path;
2+
use std::process::Command as StdCommand;
3+
4+
/// Detects the C library used by the current system.
5+
/// Returns true for glibc and false for musl.
6+
#[cfg(target_os = "linux")]
7+
#[inline]
8+
pub fn detect_libc() -> bool {
9+
// First try filesystem check as it's faster
10+
if Path::new("/lib/ld-musl-x86_64.so.1").exists() {
11+
return false;
12+
}
13+
14+
if Path::new("/lib64/ld-linux-x86-64.so.2").exists()
15+
|| Path::new("/lib32/ld-linux.so.2").exists()
16+
{
17+
return true;
18+
}
19+
20+
// Fallback to ldd check
21+
if let Ok(output) = StdCommand::new("ldd")
22+
.arg("--version")
23+
.stderr(std::process::Stdio::piped())
24+
.stdout(std::process::Stdio::piped())
25+
.output()
26+
{
27+
let output_str = String::from_utf8_lossy(&output.stdout).to_lowercase();
28+
if output_str.contains("gnu") || output_str.contains("glibc") {
29+
return true;
30+
} else if output_str.contains("musl") {
31+
return false;
32+
}
33+
34+
let error_str = String::from_utf8_lossy(&output.stderr).to_lowercase();
35+
if error_str.contains("musl") {
36+
return false;
37+
}
38+
}
39+
40+
// Default to glibc if we can't determine
41+
true
42+
}

β€Žsrc/internal/utils/mod.rsβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
pub(crate) mod base62;
22
pub(crate) use base62::encode as base62_encode;
3+
4+
#[cfg(target_os = "linux")]
5+
mod libc;
6+
#[cfg(target_os = "linux")]
7+
pub(crate) use libc::detect_libc;

β€Žwebsite/contents/reference/configuration/parameters/up/github-release.mdβ€Ž

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ This supports authenticated requests using [the `gh` command line interface](htt
4545
| `skip_os_matching` | boolean | Whether to skip the OS matching when downloading assets. If set to `true`, this will download all assets regardless of the OS *(default: `false`)* |
4646
| `skip_arch_matching` | boolean | Whether to skip the architecture matching when downloading assets. If set to `true`, this will download all assets regardless of the architecture *(default: `false`)* |
4747
| `prefer_dist` | boolean | Whether to prefer downloading assets with a `dist` tag in the name, if available; when set to `false`, will prefer downloading assets without `dist` in the name *(default: `false`)* |
48-
| `prefer_musl` | boolean | Whether to prefer downloading assets with a `musl` tag in the name, if available; when set to `false`, will prefer downloading assets without `musl` in the name *(default: `false`)* |
4948
| `api_url` | string | The URL of the GitHub API to use, useful to use GitHub Enterprise (e.g. `https://github.example.com/api/v3`); defaults to `https://api.github.com` |
5049
| `checksum` | object | The configuration to verify the checksum of the downloaded asset; see [checksum configuration](#checksum-configuration) below |
5150
| `auth` | [`Auth`](../github#auth-object) object | The configuration to authenticate the GitHub API requests for this release; if specified, will override the global configuration |

0 commit comments

Comments
Β (0)