File: gccmacsetup.sh

package info (click to toggle)
pistache 0.4.26%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,484 kB
  • sloc: cpp: 29,406; ansic: 1,190; sh: 510; makefile: 17
file content (106 lines) | stat: -rwxr-xr-x 3,122 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash

#
# SPDX-FileCopyrightText: 2024 Duncan Greatwood
#
# SPDX-License-Identifier: Apache-2.0
#

# This script configures gcc on macOS. You should call it in each
# terminal from which you'll build Pistache with gcc.
#   - Installs gcc if not yet installed
#   - Configures the CXX and CC environment variables
#   - Replaces brew googletest if needed and on user confirmation
#
# Use "source ..." to invoke the script, like:
#   source bldscripts/gccmacsetup.sh
#
# Add the "-y' option to proceed to replace brew googletest if needed
# without prompting for confirmation, like this:
#   source bldscripts/gccmacsetup.sh -y

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    echo "Error: script was invoked directly, please invoke with source."
    echo "Like this:"
    echo "  source $(printf %q "$BASH_SOURCE")$((($#)) && printf ' %q' "$@")"
    exit 1
fi

if [ "$(uname)" != "Darwin" ]; then
    echo "Error: This script is for use solely on macOS"
    return 1
fi

if ! type "brew" > /dev/null; then
    echo "Error: brew command not found. Please install Homebrew."
    return 1
fi

gcc_installed_here="N"
if ! brew list gcc &>/dev/null; then
    echo "Installing gcc with brew"
    brew install gcc
    gcc_installed_here="Y"
fi

if ! [ "x$CXX" = "x" ] && [ ${#CXX} -ge 6 ]; then
    if [ ${CXX:0:4} = g++- ] || [ ${CXX:0:4} = gcc- ]; then
        if [ "x$gcc_installed_here" = "xY" ]; then
            echo "Success: gcc installed and configured"
        else
            echo "Success: gcc already configured, exiting"
        fi
        return 0
    fi
fi

gcc_lns=`brew list gcc | grep "bin/g++-[0-9][0-9]" | sort -r`
gcc_fstln=`echo "$gcc_lns" | head -1`
export CXX=`basename $gcc_fstln`
if [ "x$CXX" = "x" ] || [ ${#CXX} -lt 6 ] || ! [ ${CXX:0:4} = g++- ]; then
    echo "Error: CXX (which has value: $CXX) not configured"
    return 1
fi

gcc_lns=`brew list gcc | grep "bin/gcc-[0-9][0-9]" | sort -r`
gcc_fstln=`echo "$gcc_lns" | head -1`
export CC=`basename $gcc_fstln`
if [ "x$CC" = "x" ] || [ ${#CC} -lt 6 ] || ! [ ${CC:0:4} = gcc- ]; then
    echo "Error: CC (which has value: $CC) not configured"
    return 1
fi

echo "Success: CXX is $CXX; CC is $CC"

if brew list googletest &>/dev/null; then
    echo ""
    echo "You have brew's googletest installed"
    echo "Brew's googletest may not be compatible with building with gcc"
    replace_gt="N"
    if [ $# -ge 1 ] && [ $1 = "-y" ]; then
        replace_gt="Y"
    else
        read -e -p \
          'Replace googletest with gcc-compatible version (uses sudo)? [y/N]> '
        if [[ "$REPLY" == [Yy]* ]]; then
            replace_gt="Y"
        fi
    fi
    if [ "x$replace_gt" = "xY" ]; then
        echo "Replacing googletest"
        brew remove googletest

        git clone https://github.com/google/googletest
        cd googletest
        mkdir build
        cd build
        cmake ..
        make
        echo ""
        echo "Installing gcc-compatible googletest using \"sudo make install\""
        sudo make install
        cd "../.."
    else
        echo "You may see undefined symbols if linking Pistache's tests"
    fi
fi