Skip to content

Commit

Permalink
Dynamically set up gcc and libstdc++ on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
dr8co committed Jun 14, 2024
1 parent badf82c commit 0c11911
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 8 deletions.
17 changes: 13 additions & 4 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
- os: macos-latest
c_compiler: clang
cpp_compiler: clang++-18
env:
GCC_MAJOR: 14

- os: ubuntu-24.04
c_compiler: clang
Expand All @@ -30,20 +32,27 @@ jobs:
run: |
export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=TRUE
brew update
brew install gcc ninja cmake git
brew install ninja cmake git "gcc@${{ env.GCC_MAJOR }}"
brew reinstall llvm
echo 'export PATH="/opt/homebrew/opt/llvm/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/opt/homebrew/opt/gcc/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/opt/homebrew/opt/gcc/lib/gcc/14:$PATH"' >> ~/.bash_profile
echo 'export PATH="/opt/homebrew/opt/gcc/lib/gcc/${{ env.GCC_MAJOR }}:$PATH"' >> ~/.bash_profile
. ~/.bash_profile
- uses: actions/checkout@v4

- name: Set reusable strings
id: strings
shell: bash
working-directory: ${{ github.workspace }}
run: |
echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
# Set the paths to the GCC include and lib directories on macOS
if [ "${{ matrix.os }}" == "macos-latest" ]; then
echo "gcc-include-dir=$(./scripts/search.sh "/opt/homebrew/Cellar/gcc/*/include/c++/${{ env.GCC_MAJOR }}")" >> "$GITHUB_OUTPUT"
echo "gcc-sys-include-dir=$(./scripts/search.sh "/opt/homebrew/Cellar/gcc/*/include/c++/${{ env.GCC_MAJOR }}/*-apple-darwin*")" >> "$GITHUB_OUTPUT"
echo "gcc-lib-dir=$(./scripts/search.sh "/opt/homebrew/Cellar/gcc/*/lib/gcc/${{ env.GCC_MAJOR }}")" >> "$GITHUB_OUTPUT"
fi
# Build the project
- name: Build PrivacyShield
Expand All @@ -62,8 +71,8 @@ jobs:
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DCMAKE_CXX_COMPILER=/opt/homebrew/opt/llvm/bin/clang++
-DCMAKE_C_COMPILER=/opt/homebrew/opt/llvm/bin/clang
-DCMAKE_CXX_FLAGS="-stdlib++-isystem /opt/homebrew/Cellar/gcc/14.1.0_1/include/c++/14 -cxx-isystem /opt/homebrew/Cellar/gcc/14.1.0_1/include/c++/14/aarch64-apple-darwin23"
-DCMAKE_EXE_LINKER_FLAGS="-stdlib=libstdc++ -L /opt/homebrew/Cellar/gcc/14.1.0_1/lib/gcc/14 -Wl,-rpath,/opt/homebrew/opt/gcc/lib/gcc/current"
-DCMAKE_CXX_FLAGS="-stdlib++-isystem ${{ steps.strings.outputs.gcc-include-dir }} -cxx-isystem ${{ steps.strings.outputs.gcc-sys-include-dir }}"
-DCMAKE_EXE_LINKER_FLAGS="-stdlib=libstdc++ -L ${{ steps.strings.outputs.gcc-lib-dir }} -Wl,-rpath,/opt/homebrew/opt/gcc/lib/gcc/current"
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-S ${{ github.workspace }} -G Ninja
# -DCMAKE_CXX_FLAGS="-stdlib=libstdc++ -stdlib++-isystem /opt/homebrew/Cellar/gcc/14.1.0_1/include/c++/14 -cxx-isystem /opt/homebrew/Cellar/gcc/14.1.0_1/include/c++/14/aarch64-apple-darwin23 -L /opt/homebrew/Cellar/gcc/14.1.0_1/lib/gcc/14 -Wl,-rpath,/opt/homebrew/opt/gcc/lib/gcc/current"
Expand Down
17 changes: 13 additions & 4 deletions .github/workflows/cpack-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
- os: macos-latest
c_compiler: clang
cpp_compiler: clang++-18
env:
GCC_MAJOR: 14

- os: ubuntu-24.04
c_compiler: clang
Expand All @@ -30,20 +32,27 @@ jobs:
run: |
export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=TRUE
brew update
brew install gcc ninja cmake git
brew install ninja cmake git "gcc@${{ env.GCC_MAJOR }}"
brew reinstall llvm
echo 'export PATH="/opt/homebrew/opt/llvm/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/opt/homebrew/opt/gcc/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/opt/homebrew/opt/gcc/lib/gcc/14:$PATH"' >> ~/.bash_profile
echo 'export PATH="/opt/homebrew/opt/gcc/lib/gcc/${{ env.GCC_MAJOR }}:$PATH"' >> ~/.bash_profile
. ~/.bash_profile
- uses: actions/checkout@v4

- name: Set reusable strings
id: strings
shell: bash
working-directory: ${{ github.workspace }}
run: |
echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
# Set the paths to the GCC include and lib directories on macOS
if [ "${{ matrix.os }}" == "macos-latest" ]; then
echo "gcc-include-dir=$(./scripts/search.sh "/opt/homebrew/Cellar/gcc/*/include/c++/${{ env.GCC_MAJOR }}")" >> "$GITHUB_OUTPUT"
echo "gcc-sys-include-dir=$(./scripts/search.sh "/opt/homebrew/Cellar/gcc/*/include/c++/${{ env.GCC_MAJOR }}/*-apple-darwin*")" >> "$GITHUB_OUTPUT"
echo "gcc-lib-dir=$(./scripts/search.sh "/opt/homebrew/Cellar/gcc/*/lib/gcc/${{ env.GCC_MAJOR }}")" >> "$GITHUB_OUTPUT"
fi
# Build the project
- name: Build PrivacyShield
Expand All @@ -62,8 +71,8 @@ jobs:
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DCMAKE_CXX_COMPILER=/opt/homebrew/opt/llvm/bin/clang++
-DCMAKE_C_COMPILER=/opt/homebrew/opt/llvm/bin/clang
-DCMAKE_CXX_FLAGS="-stdlib++-isystem /opt/homebrew/Cellar/gcc/14.1.0_1/include/c++/14 -cxx-isystem /opt/homebrew/Cellar/gcc/14.1.0_1/include/c++/14/aarch64-apple-darwin23"
-DCMAKE_EXE_LINKER_FLAGS="-stdlib=libstdc++ -L /opt/homebrew/Cellar/gcc/14.1.0_1/lib/gcc/14 -Wl,-rpath,/opt/homebrew/opt/gcc/lib/gcc/current"
-DCMAKE_CXX_FLAGS="-stdlib++-isystem ${{ steps.strings.outputs.gcc-include-dir }} -cxx-isystem ${{ steps.strings.outputs.gcc-sys-include-dir }}"
-DCMAKE_EXE_LINKER_FLAGS="-stdlib=libstdc++ -L ${{ steps.strings.outputs.gcc-lib-dir }} -Wl,-rpath,/opt/homebrew/opt/gcc/lib/gcc/current"
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-S ${{ github.workspace }} -G Ninja
Expand Down
33 changes: 33 additions & 0 deletions scripts/search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3

import sys
import glob


def search_filesystem(pattern: str) -> None:
"""
Search the filesystem for files or directories that match the pattern
:param pattern: The pattern to search for
:return: None
"""
# Find files or directories that match the pattern
matches = glob.glob(pattern, recursive=True)

# Check if there are any matches
if not matches:
print(f"No matches found for pattern: {pattern}")
else:
# Sort matches in reverse order
matches.sort(reverse=True)

# Select the first match (after sorting in reverse order)
print(matches[0])


if __name__ == "__main__":
# Check if exactly one argument is provided
if len(sys.argv) != 2:
print("Usage: python3 search.py <pattern>")
sys.exit(1)

search_filesystem(sys.argv[1])
24 changes: 24 additions & 0 deletions scripts/search.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

# This script is used to search for files or directories that match a pattern.
# It searches recursively and returns the first match found.

# Check if exactly one argument is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <pattern>"
exit 1
fi

pattern=$1

# Find files or directories that match the pattern and store them in an array
IFS=$'\n' read -d '' -r -a matches < <(find $(dirname "$pattern") -name "$(basename "$pattern")" | sort -r 2>/dev/null)

# Check if any matches were found
if [ ${#matches[@]} -eq 0 ]; then
echo "No matches found."
exit 1
else
# Print the first match
echo "${matches[0]}"
fi

0 comments on commit 0c11911

Please sign in to comment.