From 86a6ea0ca8eb503cfbd461b968c20f89a597b262 Mon Sep 17 00:00:00 2001
From: Florent Pruvost <florent.pruvost@inria.fr>
Date: Thu, 2 May 2024 14:22:44 +0200
Subject: [PATCH] build macosx: check if starpu is installed, if not install it

---
 .gitlab/build.sh         | 16 ++++++++---
 tools/homebrew/starpu.rb | 61 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 4 deletions(-)
 create mode 100644 tools/homebrew/starpu.rb

diff --git a/.gitlab/build.sh b/.gitlab/build.sh
index ee9deb703..bd7b9259e 100755
--- a/.gitlab/build.sh
+++ b/.gitlab/build.sh
@@ -11,11 +11,19 @@ SCAN=""
 
 if [[ "$SYSTEM" != "windows" ]]; then
   if [[ "$SYSTEM" == "macosx" ]]; then
-    if brew ls --versions starpu > /dev/null; then
-      echo "Starpu is already installed with brew";
+    # check starpu is already installed and install it if necessary
+    DEP_INSTALLED=`brew ls --versions starpu | cut -d " " -f 2`
+    if [[ -z "${DEP_INSTALLED}" ]]; then
+      # dep is not installed, we have to install it
+      brew install --build-from-source ./tools/homebrew/starpu.rb
     else
-      echo "Start installing Starpu with brew";
-      brew install --build-from-source ~/brew-repo/starpu.rb;
+      # dep is already installed, check the version with our requirement
+      DEP_REQUIRED=`brew info --json ./tools/homebrew/starpu.rb |jq -r '.[0].versions.stable'`
+      if [[ "${DEP_INSTALLED}" != "${DEP_REQUIRED}" ]]; then
+        # if the installed version is not the required one, re-install
+        brew remove --force --ignore-dependencies starpu
+        brew install --build-from-source ./tools/homebrew/starpu.rb
+      fi
     fi
     # clang is used on macosx and it is not compatible with MORSE_ENABLE_COVERAGE=ON
     # to avoid the Accelerate framework and get Openblas we use BLA_PREFER_PKGCONFIG
diff --git a/tools/homebrew/starpu.rb b/tools/homebrew/starpu.rb
new file mode 100644
index 000000000..3bacca558
--- /dev/null
+++ b/tools/homebrew/starpu.rb
@@ -0,0 +1,61 @@
+###
+#
+#  @file starpu.rb
+#  @copyright 2013-2024 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
+#                       Univ. Bordeaux. All rights reserved.
+#
+#  @brief Homebrew formula for StarPU
+#
+#  @version 1.3.0
+#  @date 2024-05-06
+#
+###
+class Starpu < Formula
+  desc "StarPU is a task programming library for hybrid architectures"
+  homepage "https://starpu.gitlabpages.inria.fr/"
+  url "https://files.inria.fr/starpu/starpu-1.4.5/starpu-1.4.5.tar.gz"
+  sha256 "28f389b34df57a2a4e4743b40554b0f8d098ff2199e9eb8fbbe20aa377b64541"
+  license "GNU GPL v2.1"
+
+  depends_on "autoconf" => :build
+  depends_on "automake" => :build
+  depends_on "libtool" => :build
+  depends_on "pkg-config" => [:build, :test]
+  depends_on "hwloc"
+  depends_on "openmpi"
+
+  def install
+    system "./autogen.sh" if build.head?
+    system "./configure", *std_configure_args
+    system "make", "install"
+  end
+
+  test do
+    (testpath/"test.c").write <<~EOS
+      #include <stdio.h>
+      #include <stdlib.h>
+      #include <starpu.h>
+
+      struct starpu_codelet cl =
+      {
+        .where = STARPU_NOWHERE,
+      };
+
+      int main(int argc, char* argv[])
+      {
+        int ret = starpu_init(NULL);
+	STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
+        ret = starpu_task_insert(&cl, 0);
+	STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
+        ret = starpu_task_wait_for_all();
+	STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
+        starpu_shutdown();
+        return 0;
+      }
+    EOS
+
+    pkg_config_flags = `pkg-config --cflags --libs starpu-1.4`.chomp.split
+    system ENV.cc, "test.c", *pkg_config_flags
+    system "./a.out"
+  end
+end
-- 
GitLab