From f85d54877a435b162b9d43a86b776e7dae8489f6 Mon Sep 17 00:00:00 2001
From: Ahmed Ihab <57008633+ahmedihabb2@users.noreply.github.com>
Date: Wed, 26 Apr 2023 08:57:52 +0200
Subject: [PATCH] Build with Ruby and Pass cmake options using env file macOS
 (#428)

* Pass cmake options using env file

* Update metacall-configure.sh

* Update metacall-configure.sh

* Update metacall-environment.sh

---------

Co-authored-by: Vicente Eduardo Ferrer Garcia <7854099+viferga@users.noreply.github.com>
---
 .github/workflows/macos-test.yml | 19 ++++++++++---------
 tools/metacall-configure.sh      | 11 +++++++++++
 tools/metacall-environment.sh    | 21 ++++++++++++++++++++-
 3 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/.github/workflows/macos-test.yml b/.github/workflows/macos-test.yml
index 4ef8abae6..34d40f94b 100644
--- a/.github/workflows/macos-test.yml
+++ b/.github/workflows/macos-test.yml
@@ -35,29 +35,30 @@ jobs:
           npm uninstall npm -g
           rm -rf /usr/local/lib/node_modules/npm
 
+      - name: Uninstall Ruby
+        run: |
+          brew uninstall --force ruby
+          brew autoremove
+          RUBY_FRAMEWORK_DIR=$(xcrun --sdk macosx --show-sdk-path)/System/Library/Frameworks/Ruby.framework
+          sudo rm -rf $RUBY_FRAMEWORK_DIR
+
       - name: Set up the environment
         run: sh ./tools/metacall-environment.sh $METACALL_INSTALL_OPTIONS
         env:
-          METACALL_INSTALL_OPTIONS: base python nodejs java # ruby netcore5 typescript file rpc wasm c cobol rust rapidjson funchook swig pack # clangformat v8rep51 coverage
+          METACALL_INSTALL_OPTIONS: base python nodejs java ruby #netcore5 typescript file rpc wasm c cobol rust rapidjson funchook swig pack # clangformat v8rep51 coverage
 
       - name: Configure
         run: |
-          mkdir -p build
           cd build
           bash ../tools/metacall-configure.sh $METACALL_CONFIGURE_OPTIONS
         env:
-          METACALL_CONFIGURE_OPTIONS: ${{ matrix.buildtype }} scripts ports tests sanitizer python java # nodejs python java sanitizer ruby netcore5 typescript file rpc wasm java c cobol rust examples dynamic install pack benchmarks # v8 coverage
+          METACALL_CONFIGURE_OPTIONS: ${{ matrix.buildtype }} scripts ports tests sanitizer python java ruby # nodejs python java sanitizer netcore5 typescript file rpc wasm java c cobol rust examples dynamic install pack benchmarks # v8 coverage
 
       - name: Build
         working-directory: ./build
         # TODO: Remove the disable option for fork safe once funchook problem is solved
         run: |
-          cmake -DOPTION_FORK_SAFE=OFF \
-             -DPython3_INCLUDE_DIRS="$HOME/.pyenv/versions/3.11.1/include/python3.11" \
-             -DPython3_LIBRARY="$HOME/.pyenv/versions/3.11.1/lib/libpython3.11.dylib" \
-             -DPython3_EXECUTABLE="$HOME/.pyenv/versions/3.11.1/bin/python3.11" \
-             -DPython3_ROOT="$HOME/.pyenv/versions/3.11.1" \
-             -DPython3_VERSION="3.11.1" ..
+          cmake -DOPTION_FORK_SAFE=OFF  ..
           bash ../tools/metacall-build.sh $METACALL_BUILD_OPTIONS
         env:
           METACALL_BUILD_OPTIONS: ${{ matrix.buildtype }} tests
diff --git a/tools/metacall-configure.sh b/tools/metacall-configure.sh
index c4cc12f34..def4dd25f 100755
--- a/tools/metacall-configure.sh
+++ b/tools/metacall-configure.sh
@@ -21,6 +21,7 @@
 
 set -euxo pipefail
 
+ROOT_DIR=$(pwd)
 BUILD_TYPE=Release
 BUILD_PYTHON=0
 BUILD_RUBY=0
@@ -433,6 +434,16 @@ sub_configure() {
 		BUILD_STRING="$BUILD_STRING -DOPTION_BUILD_THREAD_SANITIZER=Off"
 	fi
 
+	# Split env file line by line and add each line to the build string
+	CMAKE_CONFIG_FILE=$ROOT_DIR/CMakeConfig.txt
+	if [ -f $CMAKE_CONFIG_FILE ]; then
+		while IFS= read -r line
+		do
+			echo $line
+			BUILD_STRING="$BUILD_STRING $line"
+		done < "$CMAKE_CONFIG_FILE"
+	fi
+
 	# Build type
 	BUILD_STRING="$BUILD_STRING -DCMAKE_BUILD_TYPE=$BUILD_TYPE"
 	
diff --git a/tools/metacall-environment.sh b/tools/metacall-environment.sh
index 4cbbaa421..2b146e814 100755
--- a/tools/metacall-environment.sh
+++ b/tools/metacall-environment.sh
@@ -147,7 +147,14 @@ sub_python(){
 		pyenv rehash
 		echo -e '\nif command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
 		source ~/.bash_profile
-		which python3
+		mkdir -p build
+		CMAKE_CONFIG_PATH=$ROOT_DIR/build/CMakeConfig.txt
+		echo "-DPython3_INCLUDE_DIRS=\"$HOME/.pyenv/versions/3.11.1/include/python3.11\"" >> $CMAKE_CONFIG_PATH
+		echo "-DPython3_LIBRARY=\"$HOME/.pyenv/versions/3.11.1/lib/libpython3.11.dylib\"" >> $CMAKE_CONFIG_PATH
+		echo "-DPython3_EXECUTABLE=\"$HOME/.pyenv/versions/3.11.1/bin/python3.11\"" >> $CMAKE_CONFIG_PATH
+		echo "-DPython3_ROOT=\"$HOME/.pyenv/versions/3.11.1\"" >> $CMAKE_CONFIG_PATH
+		echo "-DPython3_VERSION=\"3.11.1\"" >> $CMAKE_CONFIG_PATH
+		echo "-DPython3_FIND_FRAMEWORK=\"NEVER\"" >> $CMAKE_CONFIG_PATH
 		pip3 install requests
 		pip3 install setuptools
 		pip3 install wheel
@@ -172,6 +179,18 @@ sub_ruby(){
 		#wget https://deb.nodesource.com/setup_4.x | $SUDO_CMD bash -
 		#$SUDO_CMD apt-get -y --no-install-recommends install nodejs
 		#$SUDO_CMD gem install rails
+	elif [ "${OPERATIVE_SYSTEM}" == "Darwin" ]; then
+		brew install ruby@3.2
+		echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> /Users/runner/.bash_profile
+		source ~/.bash_profile
+		mkdir -p build
+		CMAKE_CONFIG_PATH=$ROOT_DIR/build/CMakeConfig.txt
+		RUBY_PREFIX=$(brew --prefix ruby@3.2)
+		RUBY_VERSION=$(ruby -e 'puts RUBY_VERSION')
+		echo "-DRuby_INCLUDE_DIR=\"$RUBY_PREFIX/include/ruby-3.2.0\"" >> $CMAKE_CONFIG_PATH
+		echo "-DRuby_LIBRARY=\"$RUBY_PREFIX/lib/libruby.3.2.dylib\"" >> $CMAKE_CONFIG_PATH
+		echo "-DRuby_EXECUTABLE=\"$RUBY_PREFIX/bin/ruby\"" >> $CMAKE_CONFIG_PATH
+		echo "-DRuby_VERSION_STRING=\"$RUBY_VERSION\"" >> $CMAKE_CONFIG_PATH
 	fi
 }