User:Daniel Santos/sandbox/script

{{FileBox|title=Script for generating clean CFLAGS for distcc|filename=clean-machine-cflags.sh|lang=bash|1=#!/bin/bash
 * 1) clean-machine-cflags.sh -- A horrible script for deriving clean
 * 2) 			    machine-specific CFLAGS from gcc sources for use
 * 3)                            with distcc.
 * 4) Copyright (C) 2017  Daniel Santos 
 * 5) This program is free software: you can redistribute it and/or modify
 * 6) it under the terms of the GNU General Public License as published by
 * 7) the Free Software Foundation, either version 3 of the License, or
 * 8) (at your option) any later version.
 * 9) This program is distributed in the hope that it will be useful,
 * 10) but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 11) MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * 12) GNU General Public License for more details.
 * 13) You should have received a copy of the GNU General Public License
 * 14) along with this program.  If not, see .
 * 1) You should have received a copy of the GNU General Public License
 * 2) along with this program.  If not, see .
 * 1) along with this program.  If not, see .

src{{=}}"$1" debug{{=}}0

usage { echo "USAGE: $0 [src_file]" >&2 echo >&2 echo " Where src_file is the path to i386.c"    echo >&2 echo "Example: $0 /tmp/gcc-6.3.0/gcc/config/i386/i386.c" >&2 exit 1 }

debugmsg { ((debug)) && echo "$*" >&2 }

if $; then usage fi

if $; then url{{=}}"https://github.com/gcc-mirror/gcc/raw/master/gcc/config/i386/i386.c"   src{{=}}/tmp/i386.c    if  ! -f "$src" ; then wget -O /tmp/i386.c "$url" {{!}}{{!}} exit fi fi

if ! -r "$src" ; then echo "$src not a file or not readable." >&2   usage fi

nativeflags{{=}}$(   gcc -v -E -x c -march{{=}}native -mtune{{=}}native - < /dev/null 2>&1 {{!}}    grep cc1 {{!}}    perl -pe 's/^.* - //g; s/\s+/\n/g;' ) debugmsg nativeflags {{=}} $nativeflags debugmsg
 * 1) Get the native flags that are ugly

arch{{=}}$(   grep -- '^-march{{=}}' <(echo "$nativeflags") {{!}}    perl -pe 's/^-march{{=}}//g;' ) debugmsg arch {{=}} $arch debugmsg

native_mno_flags{{=}}$(   grep '^-mno-' <(echo "$nativeflags") {{!}}    perl -pe 's/^-mno-//g;' ) debugmsg native_mno_flags {{=}} $native_mno_flags debugmsg
 * 1) Extract all native -mno-* flags

native_mflags{{=}}$(   grep '^-m' <(echo "$nativeflags") {{!}}    grep -v -E -- '-m(arch{{!}}tune{{!}}no-)' {{!}}    perl -pe 's/^-m//g;' ) debugmsg native_mflags {{=}} $native_mflags debugmsg
 * 1) Extract all native -m* flags other than -march, -mtune and -mno-*

ext_consts{{=}}$(   cat "$src" {{!}} perl -pe '	# Remove all carriage returns.	s/\n//g;    ' {{!}}    perl -pe '	# Remove everything up to the start of the processor_alias_table	s/^.*const processor_alias_table.+?{\s+{/{/g;
 * 1) Extract the constant base name (upper case) for each CPU extension that
 * 2) $arch has.

# Remove everything after it	s/"generic",\s+PROCESSOR_GENERIC.*$//g;

# Re-add line separators where we want them s/},/},\n/g; ' {{!}}

# Extract line for $arch grep "{\"$arch\"," {{!}}

# Extract the PTA_* flags awk -F, '{print $4}' {{!}}

# Remove the PTA_ prefix and clean them up. perl -pe ' s/\}//g; s/\sPTA_(\w)/$1/g; s/\s*\{{!}}\s*/\n/g; ' ) debugmsg ext_consts {{=}} $ext_consts debugmsg

if -z "$ext_consts" ; then echo "ERROR: Didn't find arch '$arch' in file '$src'" >&2 echo "This means this script is broken." >&2   exit 1 fi

marchexts{{=}}$(   for e in $ext_consts; do	grep -E "{\s*\"-m.*OPTION_MASK_ISA_$e" $src {{!}}	awk -F\" '{print $2}' {{!}}	perl -pe 's/^-m//g;' {{!}}	sort    done ) debugmsg marchexts {{=}} $marchexts debugmsg
 * 1) Convert the constant base name into the actual CLI text (minus the -m)

unset real_mno_flags unset real_mflags

for mno_flag in $native_mno_flags; do   if grep -- "^$mno_flag$" <(echo "$marchexts") > /dev/null; then real_mno_flags{{=}}"$real_mno_flags -mno-$mno_flag" fi done

for mflag in $native_mflags; do   if grep -- "^$mflag$" <(echo "$marchexts") > /dev/null; then dummy{{=}}smarty else real_mflags{{=}}"$real_mflags -m$mflag" fi done

debugmsg real_mflags {{=}} $real_mflags debugmsg real_mno_flags {{=}} $real_mno_flags debugmsg

echo "CFLAGS{{=}}\"$(   gcc -v -E -march{{=}}$arch $real_mflags $real_mno_flags -mtune{{=}}native - < /dev/null 2>&1 {{!}}    grep cc1 {{!}}    perl -pe 's/^.* - //g;'\ )\""}}