From 89ebb88d1d73ea8f693f2195321b402c31186abe Mon Sep 17 00:00:00 2001
From: Michal Jires <mjires@suse.cz>
Date: Mon, 13 Jan 2025 01:58:41 +0100
Subject: [PATCH] lto: Fix empty fnctl.h build error with MinGW.

MSYS2+MinGW contains headers without defining expected contents.
This fix checks that the fcntl function is actually defined.

Bootstrapped/regtested on x86_64-linux. Committed as obvious.

gcc/ChangeLog:

	* lockfile.cc (LOCKFILE_USE_FCNTL): New.
	(lockfile::lock_write): Use LOCKFILE_USE_FCNTL.
	(lockfile::try_lock_write): Use LOCKFILE_USE_FCNTL.
	(lockfile::lock_read): Use LOCKFILE_USE_FCNTL.
	(lockfile::unlock): Use LOCKFILE_USE_FCNTL.
	(lockfile::lockfile_supported): Use LOCKFILE_USE_FCNTL.
---
 gcc/lockfile.cc | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/gcc/lockfile.cc b/gcc/lockfile.cc
index b385c295851f..cecbb86491da 100644
--- a/gcc/lockfile.cc
+++ b/gcc/lockfile.cc
@@ -22,6 +22,10 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "lockfile.h"
 
+/* fcntl.h may exist without expected contents.  */
+#if HAVE_FCNTL_H && HOST_HAS_F_SETLKW
+#define LOCKFILE_USE_FCNTL 1
+#endif
 
 /* Unique write lock.  No other lock can be held on this lockfile.
    Blocking call.  */
@@ -32,7 +36,7 @@ lockfile::lock_write ()
   if (fd < 0)
     return -1;
 
-#if HAVE_FCNTL_H
+#ifdef LOCKFILE_USE_FCNTL
   struct flock s_flock;
 
   s_flock.l_whence = SEEK_SET;
@@ -57,7 +61,7 @@ lockfile::try_lock_write ()
   if (fd < 0)
     return -1;
 
-#if HAVE_FCNTL_H
+#ifdef LOCKFILE_USE_FCNTL
   struct flock s_flock;
 
   s_flock.l_whence = SEEK_SET;
@@ -87,7 +91,7 @@ lockfile::lock_read ()
   if (fd < 0)
     return -1;
 
-#if HAVE_FCNTL_H
+#ifdef LOCKFILE_USE_FCNTL
   struct flock s_flock;
 
   s_flock.l_whence = SEEK_SET;
@@ -108,7 +112,7 @@ lockfile::unlock ()
 {
   if (fd < 0)
     {
-#if HAVE_FCNTL_H
+#ifdef LOCKFILE_USE_FCNTL
       struct flock s_flock;
 
       s_flock.l_whence = SEEK_SET;
@@ -128,7 +132,7 @@ lockfile::unlock ()
 bool
 lockfile::lockfile_supported ()
 {
-#if HAVE_FCNTL_H
+#ifdef LOCKFILE_USE_FCNTL
   return true;
 #else
   return false;
-- 
GitLab