From 5363f1714b632a87fdbb7f160ee2a90e77f2db36 Mon Sep 17 00:00:00 2001
From: Ivan Babrou <github@ivan.computer>
Date: Tue, 9 Feb 2021 20:55:08 -0800
Subject: [PATCH] mesa: fix missing timespec_get on aarch64-darwin

This doesn't affect any other target.

See: https://gitlab.freedesktop.org/mesa/mesa/-/issues/1020
---
 .../libraries/mesa/aarch64-darwin.patch       | 33 +++++++++++++++++++
 pkgs/development/libraries/mesa/default.nix   |  4 +++
 2 files changed, 37 insertions(+)
 create mode 100644 pkgs/development/libraries/mesa/aarch64-darwin.patch

diff --git a/pkgs/development/libraries/mesa/aarch64-darwin.patch b/pkgs/development/libraries/mesa/aarch64-darwin.patch
new file mode 100644
index 000000000000..e60a4ffa308a
--- /dev/null
+++ b/pkgs/development/libraries/mesa/aarch64-darwin.patch
@@ -0,0 +1,33 @@
+From 8ac29b952e638ec1ea8c3734a3b91253e50c336d Mon Sep 17 00:00:00 2001
+From: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
+Date: Sun, 24 Jan 2021 21:10:29 -0800
+Subject: [PATCH 4/4] Hack to address build failure when using newer macOS SDKs
+ with older deployment targets
+
+Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
+---
+ include/c11/threads_posix.h | 8 +++++++-
+ 1 file changed, 7 insertions(+), 1 deletion(-)
+
+diff --git a/include/c11/threads_posix.h b/include/c11/threads_posix.h
+index 45cb6075e6e..355d725f7da 100644
+--- a/include/c11/threads_posix.h
++++ b/include/c11/threads_posix.h
+@@ -382,7 +382,13 @@ tss_set(tss_t key, void *val)
+ 
+ /*-------------------- 7.25.7 Time functions --------------------*/
+ // 7.25.6.1
+-#ifndef HAVE_TIMESPEC_GET
++#if !defined(HAVE_TIMESPEC_GET) || defined(__APPLE__)
++
++#ifdef __APPLE__
++#include <time.h>
++#define timespec_get(ts, b) mesa_timespec_get(ts, b)
++#endif
++
+ static inline int
+ timespec_get(struct timespec *ts, int base)
+ {
+-- 
+2.29.2 (Apple Git-129)
+
diff --git a/pkgs/development/libraries/mesa/default.nix b/pkgs/development/libraries/mesa/default.nix
index 36a0b52f357d..e7c87bbc2c72 100644
--- a/pkgs/development/libraries/mesa/default.nix
+++ b/pkgs/development/libraries/mesa/default.nix
@@ -65,6 +65,10 @@ stdenv.mkDerivation {
       url = "https://gitlab.freedesktop.org/mesa/mesa/commit/aebbf819df6d1e.patch";
       sha256 = "17248hyzg43d73c86p077m4lv1pkncaycr3l27hwv9k4ija9zl8q";
     })
+  ] ++ optionals (stdenv.isDarwin && stdenv.isAarch64) [
+    # Fix aarch64-darwin build, remove when upstreaam supports it out of the box.
+    # See: https://gitlab.freedesktop.org/mesa/mesa/-/issues/1020
+    ./aarch64-darwin.patch
   ];
 
   postPatch = ''