From 8e223c5a2d3c6881ee84470bc121f503062db508 Mon Sep 17 00:00:00 2001 From: Colin Woodbury Date: Tue, 11 Aug 2020 14:14:26 -0700 Subject: [PATCH] Warn if the MUSL target is missing when using `--musl` --- CHANGELOG.md | 5 +++++ src/main.rs | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88412a5..0f59b6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased +#### Added + +- When using `--musl`, the user is warned if they don't have the + `x86_64-unknown-linux-musl` target installed. + #### Changed - Run `strip` on the release binary before `tar`ring it. diff --git a/src/main.rs b/src/main.rs index 6573564..57292ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,11 @@ +use anyhow::anyhow; use gumdrop::{Options, ParsingStyle}; use hmac_sha256::Hash; use itertools::Itertools; use serde_derive::Deserialize; use std::fs; use std::process::{self, Command}; +use std::str; #[derive(Options)] struct Args { @@ -79,6 +81,12 @@ fn main() { } fn work(args: Args) -> anyhow::Result<()> { + // We can't proceed if the user has specified `--musl` but doesn't have the + // target installed. + if args.musl { + musl_check()? + } + let config = cargo_config()?; release_build(args.musl)?; tarball(args.musl, &config.package)?; @@ -179,3 +187,20 @@ fn sha256sum(package: &Package) -> anyhow::Result { let hex = digest.iter().map(|u| format!("{:02x}", u)).collect(); Ok(hex) } + +/// Does the user have the `x86_64-unknown-linux-musl` target installed? +fn musl_check() -> anyhow::Result<()> { + let args = vec!["target", "list", "--installed"]; + let output = Command::new("rustup").args(args).output()?.stdout; + let installed = str::from_utf8(&output)? + .lines() + .any(|tc| tc == "x86_64-unknown-linux-musl"); + + if installed { + Ok(()) + } else { + Err(anyhow!( + "Missing target! Try: rustup target add x86_64-unknown-linux-musl" + )) + } +}