#!/bin/bash
# Copyright 2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# Author: Francesco Riosa

# kate: encoding utf-8; eol unix
# kate: indent-width 4; mixedindent off; replace-tabs on;
# kate: remove-trailing-space on; space-indent on
# kate: word-wrap-column 500; word-wrap off

# from http://en.gentoo-wiki.com/wiki/Hardware_CFLAGS (2010)
# echo "int main() { return 0; }" | gcc -march=native -v -E - 2>&1 | grep march
# echo "int main() { return 0; }" | gcc -march=core2 -v -Q -x c - 2>&1

# example output:
# # gcc-native-flags $(tc-getCC)
# -march=corei7-avx -mtune=generic -mcx16 -msahf -mno-movbe -maes -mpclmul -mpopcnt -mno-abm -mno-lwp -mno-fma -mno-fma4 -mno-xop -mno-bmi -mno-tbm -mavx -msse4.2 -msse4.1 --param=l1-cache-size=32 --param=l1-cache-line-size=64 --param=l2-cache-size=8192
# 
# # gcc-native-flags /usr/x86_64-pc-linux-gnu/gcc-bin/4.5.3/x86_64-pc-linux-gnu-gcc
# -march=core2 -mtune=generic -D_FORTIFY_SOURCE=2 -mcx16 -msahf -maes -mpclmul -mpopcnt -mavx --param=l1-cache-size=32 --param=l1-cache-line-size=64 --param=l2-cache-size=8192
# 
# # gcc-native-flags /usr/x86_64-pc-linux-gnu/gcc-bin/4.6.3/x86_64-pc-linux-gnu-gcc
# -march=corei7-avx -mtune=generic -mcx16 -msahf -mno-movbe -maes -mpclmul -mpopcnt -mno-abm -mno-lwp -mno-fma -mno-fma4 -mno-xop -mno-bmi -mno-tbm -mavx -msse4.2 -msse4.1 --param=l1-cache-size=32 --param=l1-cache-line-size=64 --param=l2-cache-size=8192
# 
# # gcc-native-flags /usr/x86_64-pc-linux-gnu/gcc-bin/4.7.1/x86_64-pc-linux-gnu-gcc
# -march=corei7-avx -mtune=generic -mcx16 -msahf -mno-movbe -maes -mpclmul -mpopcnt -mno-abm -mno-lwp -mno-fma -mno-fma4 -mno-xop -mno-bmi -mno-bmi2 -mno-tbm -mavx -mno-avx2 -msse4.2 -msse4.1 -mno-lzcnt -mrdrnd -mf16c -mfsgsbase --param=l1-cache-size=32 --param=l1-cache-line-size=64 --param=l2-cache-size=8192


inherit() {
    local portdir=$(portageq portdir)
    for eclass in $@
    do
        echo "inherit ${eclass}"
        source ${portdir}/eclass/${eclass}.eclass
    done
}

source /etc/make.conf
export CFLAGS CXXFLAGS LDFLAGS CHOST
inherit toolchain-funcs

# 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8<

# @FUNCTION: gcc-native-flags
# @USAGE: [CC compiler]
# @RETURN: 1st march 2nd mtune >=3rd flags
gcc-native-flags() {
    local gcccmd
    local march=''
    local mtune='-mtune=generic'
    declare -a ar flags

    if [[ -n ${1} ]]
    then
        gcccmd="${1} -march=native -v -E -"
    else
        gcccmd="$(tc-getCC) -march=native -v -E -"
    fi

    ar=( $(
        echo -n "int main() { return 0; }" \
        | ${gcccmd} 2>&1 \
        | sed -n \
            -e '/^COLLECT_GCC_OPTIONS/d' \
            -e '/^[^ ]/d'\
            -e 's:^.* - ::' \
            -e 's:--param :--param=:g' \
            -e '/-march=/p' \
        | head -n1
        ) )
    for f in ${ar[@]} ; do
        case ${f} in
            -march=*) march=${f} ;;
            -mtune=*) mtune=${f} ;;
            -*) flags=( ${flags[@]} ${f} ) ;;
        esac
    done

    echo ${march} ${mtune} ${flags[@]}
}

# 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8<

echo 
echo '# gcc-native-flags $(tc-getCC)'
gcc-native-flags

for compiler in /usr/x86_64-pc-linux-gnu/gcc-bin/*/*-gcc ; do
    echo
    echo '# gcc-native-flags '${compiler}
    gcc-native-flags ${compiler}
done
