From patchwork Tue Mar 26 22:36:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter0x44 X-Patchwork-Id: 87680 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7E75D3858289 for ; Tue, 26 Mar 2024 22:37:20 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from layka.disroot.org (layka.disroot.org [178.21.23.139]) by sourceware.org (Postfix) with ESMTPS id BEB3F385841A for ; Tue, 26 Mar 2024 22:36:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BEB3F385841A Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=disroot.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=disroot.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BEB3F385841A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=178.21.23.139 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711492610; cv=none; b=jxcTBtQm48RKTrqb5jKvTky1eS4euhdbAZEYVPI/dBXFt4alzbpoovNG4sUhGnguHVAn+57lRNRFwWhBYk+Fx5kLWbyMsIAYDy6JxGvKfIHjSM7qYIPg9XBW/j7lANaQZWN1gQr/yXqJQR86rmFwrQ3HZxQX58+ho5h5Clc9Qog= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711492610; c=relaxed/simple; bh=Yj2uU6yx97s+KDHON9BCEVL5bqbcxBNEPOoZeBUiF1c=; h=From:DKIM-Signature:To:Subject:Date:Message-Id:MIME-Version; b=RwRoE8FwawhIN2Jc0vlZalTvIUW5NbnkrlIufaDDKV5XqsZlBBUOoKH541UPmVGr1A0WMfI3b6iPitEwHhEKw2uuBwIevZPy1Rza8HlQlHe/LHITPUbMt0NHOq2iGM3zjg04nyi8Bpvt9KcHdQvo4PL8F+A3esHdlin/E2rcV8E= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id D7F734104B; Tue, 26 Mar 2024 23:36:45 +0100 (CET) X-Virus-Scanned: SPAM Filter at disroot.org Received: from layka.disroot.org ([127.0.0.1]) by localhost (disroot.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ilKnM6eKhAJJ; Tue, 26 Mar 2024 23:36:45 +0100 (CET) From: Peter Damianov DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1711492605; bh=Yj2uU6yx97s+KDHON9BCEVL5bqbcxBNEPOoZeBUiF1c=; h=From:To:Cc:Subject:Date; b=AglbYeajVwsdrv0gw17AdazL+0NZbI6w9ZIhedV78aYT/XdcvJ8kAGiqRsBOp5RF1 E4r1IcAN8UN0p7z769VFY5yT/0ERy0zRIvpkuk9LUFJsqHjv6cnrf0aGsdIHyRruSt VJhoU2oryzjRZhTuxJC0L9WNvvkCWHpzNMGlhS5tT0BPRrwkPXOpM2jO9y4qK5wVT0 Sh8BgTkgIu/o7AASL3LCLBL3SXExg3OrVMVjwlX5WRfXSeHgKh8shsi+SDq/ktHLIB j6l9ExddlMGVdJkpDAtfqh1M3oPfIXoaVj5sP0h95HvphepPTkivTqwpdycQ8hOPVl KsOIv++laEZCA== To: gcc-patches@gcc.gnu.org Cc: Peter Damianov Subject: [PATCH] lto: Don't assume a posix shell is usable on windows [PR110710] Date: Tue, 26 Mar 2024 15:36:02 -0700 Message-Id: <20240326223601.32541-1-peter0x44@disroot.org> MIME-Version: 1.0 X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org lto-wrapper generates Makefiles that use the following: touch -r file file.tmp && mv file.tmp file to truncate files. If there is no suitable "touch" or "mv" available, then this errors with "The system cannot find the file specified". The solution here is to check if sh -c true works, before trying to use it in the Makefile. If it doesn't, then fall back to "copy /y nul file" instead. The fallback doesn't work exactly the same (the modified time of the file gets updated), but this doesn't seem to matter in practice. I tested this both in environments both with and without sh present, and observed no issues. Signed-off-by: Peter Damianov --- gcc/lto-wrapper.cc | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/gcc/lto-wrapper.cc b/gcc/lto-wrapper.cc index 5186d040ce0..8dee0aaa2d8 100644 --- a/gcc/lto-wrapper.cc +++ b/gcc/lto-wrapper.cc @@ -1389,6 +1389,27 @@ make_exists (void) return errmsg == NULL && exit_status == 0 && err == 0; } +/* Test that an sh command is present and working, return true if so. + This is only relevant for windows hosts, where a /bin/sh shell cannot + be assumed to exist. */ + +static bool +sh_exists (void) +{ + const char *sh = "sh"; + const char *sh_args[] = {sh, "-c", "true", NULL}; +#ifdef _WIN32 + int exit_status = 0; + int err = 0; + const char *errmsg + = pex_one (PEX_SEARCH, sh_args[0], CONST_CAST (char **, sh_args), + "sh", NULL, NULL, &exit_status, &err); + return errmsg == NULL && exit_status == 0 && err == 0; +#else + return true; +#endif +} + /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */ static void @@ -1402,6 +1423,7 @@ run_gcc (unsigned argc, char *argv[]) const char *collect_gcc; char *collect_gcc_options; int parallel = 0; + bool have_sh = sh_exists (); int jobserver = 0; bool jobserver_requested = false; int auto_parallel = 0; @@ -2016,6 +2038,7 @@ cont: argv_ptr[5] = NULL; if (parallel) { + fprintf (mstream, "SHELL=%s\n", have_sh ? "sh" : "cmd"); fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]); for (j = 1; new_argv[j] != NULL; ++j) fprintf (mstream, " '%s'", new_argv[j]); @@ -2024,9 +2047,15 @@ cont: truncate them as soon as we have processed it. This reduces temporary disk-space usage. */ if (! save_temps) - fprintf (mstream, "\t@-touch -r \"%s\" \"%s.tem\" > /dev/null " - "2>&1 && mv \"%s.tem\" \"%s\"\n", - input_name, input_name, input_name, input_name); + { + fprintf (mstream, + have_sh + ? "\t@-touch -r \"%s\" \"%s.tem\" > /dev/null " + "2>&1 && mv \"%s.tem\" \"%s\"\n" + : "\t@-copy /y nul \"%s\" > NUL " + "2>&1\n", + input_name, input_name, input_name, input_name); + } } else {