From f5872f3f9726a2f91c0189735c0dbcf7b029337f Mon Sep 17 00:00:00 2001
From: Ismael Luceno <ismael@iodev.co.uk>
Date: Fri, 9 Sep 2022 16:56:49 +0200
Subject: [PATCH 2/2] Enable building against libucontext

The ucontext.h API may be implemented outside the libc.

Origin: Source Mage GNU/Linux
Upstream-Status: Submitted [https://github.com/oneapi-src/oneTBB/pull/900]
Signed-off-by: Ismael Luceno <ismael@iodev.co.uk>
---
 cmake/ucontext.cmake   | 20 ++++++++++++++++++++
 src/tbb/CMakeLists.txt |  1 +
 2 files changed, 21 insertions(+)
 create mode 100644 cmake/ucontext.cmake

diff --git a/cmake/ucontext.cmake b/cmake/ucontext.cmake
new file mode 100644
index 000000000000..26f22559c78f
--- /dev/null
+++ b/cmake/ucontext.cmake
@@ -0,0 +1,20 @@
+# The ucontext API may be implemented outside the default library set
+include(CheckSymbolExists)
+if (NOT DEFINED UCONTEXT_LIBRARIES
+    AND CMAKE_SYSTEM_NAME MATCHES "^Linux")
+    check_symbol_exists("getcontext" "ucontext.h" HAVE_UCONTEXT)
+    if (NOT HAVE_UCONTEXT)
+        message(STATUS "Checking for libucontext")
+        find_library(LIBUCONTEXT "ucontext")
+        if (LIBUCONTEXT STREQUAL "LIBUCONTEXT-NOTFOUND")
+            message(WARNING "No ucontext API available")
+            set(LIBUCONTEXT "")
+        else()
+            message(STATUS "Found libucontext: ${LIBUCONTEXT}")
+            set(HAVE_UCONTEXT 1)
+        endif()
+        set(UCONTEXT_LIBRARIES "${LIBUCONTEXT}" CACHE FILEPATH
+          "Library providing the ucontext API (if not in the default set)")
+    endif()
+endif()
+mark_as_advanced(LIBUCONTEXT HAVE_UCONTEXT)
diff --git a/src/tbb/CMakeLists.txt b/src/tbb/CMakeLists.txt
index 7f8228c09d11..c24db1f34bd2 100644
--- a/src/tbb/CMakeLists.txt
+++ b/src/tbb/CMakeLists.txt
@@ -118,6 +118,7 @@ target_link_libraries(tbb
     Threads::Threads
     ${TBB_LIB_LINK_LIBS}
     ${TBB_COMMON_LINK_LIBS}
+    ${UCONTEXT_LIBRARIES}
 )
 
 tbb_install_target(tbb)
-- 
2.37.3

