Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[howto] quick and dirty cflags+ldflags switching [updated]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
d_u_s_t
n00b
n00b


Joined: 11 Jul 2006
Posts: 8
Location: Berlin

PostPosted: Fri Sep 15, 2006 8:34 pm    Post subject: [howto] quick and dirty cflags+ldflags switching [updated] Reply with quote

Hi!
I think we all know the problems we get from excessive c/ldflags usage. Because I needed a faster solution than vi make.conf everytime something breaks I wrote this dirty little script:

Code:

#!/bin/sh
show_current_flags() {
      echo "Current setting:"
      grep '^CFLAGS=' /etc/make.conf
      grep '^LDFLAGS=' /etc/make.conf
}

echo "Flags-O-Matic - Quick and dirty make.conf Flags-Changing" # ;o)
if [ $# -ne 2 ]
then
  if [[ $1 == "d" ]]
  then
    cp -af /etc/make.conf /etc/make.conf.flagsbackup
    sed -e '/^#LASTCFLAGS=.*/D' -e 's/^CFLAGS=\(.*\)/#LASTCFLAGS=\1\nCFLAGS=""/g' \
    -e '/^#LASTLDFLAGS=.*/D' -e 's/^LDFLAGS=\(.*\)/#LASTLDFLAGS=\1\nLDFLAGS=""/g' -i /etc/make.conf \
    && echo "Flags unset!" || echo "Error while unsetting flags in make.conf!"
    echo "---"
    show_current_flags
  else
    echo "Usage: \"$0 <CFLAGS-NUM> <LDFLAGS-NUM>\" to set or \"$0 d\" to unset both"
    echo "---"
    show_current_flags
    echo "---"
    echo "Available CFLAGS:"
    cat -b /etc/cflags.conf
    echo "Available LDFLAGS:"
    cat -b /etc/ldflags.conf
  fi
else
  cp -af /etc/make.conf /etc/make.conf.flagsbackup
  CFLAGS=`cat /etc/cflags.conf | sed -n $1p`
  LDFLAGS=`cat /etc/ldflags.conf | sed -n $2p`
  sed -e '/^#LASTCFLAGS=.*/D' -e "s/^CFLAGS=\(.*\)/#LASTCFLAGS=\1\nCFLAGS=\"$CFLAGS\"/g" \
  -e '/^#LASTLDFLAGS=.*/D' -e "s/^LDFLAGS=\(.*\)/#LASTLDFLAGS=\1\nLDFLAGS=\"$LDFLAGS\"/g" -i /etc/make.conf \
  && echo "Flags changed!" || echo "Error while changing flags in make.conf!"
  echo "---"
  show_current_flags
fi


Save it to e.g. /usr/local/bin/flags.sh and chmod u+x flags.sh.

create the two files /etc/cflags.conf and /etc/ldflags.conf and fill it with your flags, one per line

Example cflags.conf:

Code:

-march=athlon-xp -O2 -pipe -fomit-frame-pointer -fno-ident -fvisibility-inlines-hidden
-march=athlon-xp -O2 -pipe -fomit-frame-pointer -fno-ident
-march=athlon-xp -O3 -pipe -fomit-frame-pointer -fno-ident
-O2


ldflags.conf:

Code:

-Wl,-O1 -Wl,--sort-common -Wl,--enable-new-dtags -Wl,-Bdirect -Wl,-hashvals -Wl,-zdynsort -Wl,--as-needed
-Wl,-O1 -Wl,--sort-common -Wl,--enable-new-dtags -Wl,-hashvals
-Wl,-O1


Now you can view/change your config like this:
/usr/local/bin/flags.sh <CFLAGS-NUM> <LDFLAGS-NUM>
e.g. /usr/local/bin/flags.sh 1 2

It will backup your make.conf to make.conf.flagsbackup everytime it is going to change anything and will keep the last flag-entries as #LASTCFLAGS/#LASTLDFLAGS in your make.conf to help you checking for flags that cause errors.

---

I know this is just dirty and I`m almost sure there is something better around here (although I haven't found anything)but this is working perfectly for me so it might also help somebody else! Don't blame me if your computer explodes!

---

Changelog:
2006-09-16: Better script without flags.conf - thanks to Cardoe


Last edited by d_u_s_t on Sat Sep 16, 2006 9:36 pm; edited 3 times in total
Back to top
View user's profile Send private message
Cardoe
Retired Dev
Retired Dev


Joined: 28 Jun 2002
Posts: 32

PostPosted: Sat Sep 16, 2006 6:28 am    Post subject: Reply with quote

Not to encourage you to rice or anything... but wouldn't this be better...

Code:

#!/bin/sh
if [ $# -ne 2 ]
then
  if [[ $1 == "d" ]]
  then
    sed -e 's/^\(CFLAGS=\)/#\1\nCFLAGS=""/' -e 's/^\(LDFLAGS=\)/#\1\nLDFLAGS=""/' -i /etc/make.conf
  else
    echo "Usage: \"$0 <CFLAGS-NUM> <LDFLAGS-NUM>\" to set or \"$0 d\" to unset both"
    if [[ -f /etc/flags.conf ]]
    then
      echo "---"
      show_current_flags
    fi
    echo "---"
    echo "Available CFLAGS:"
    cat -b /etc/cflags.conf
    echo "Available LDFLAGS:"
    cat -b /etc/ldflags.conf
  fi
else
  CFLAGS=`cat /etc/cflags.conf | sed -n $1p`
  LDFLAGS=`cat /etc/ldflags.conf | sed -n $2p`
  sed -e "s/^CFLAGS=.*/CFLAGS=\"${CFLAGS}\"/" -e "s/^LDFLAGS=.*/LDFLAGS=\"${LDFLAGS}\"/" -i /etc/make.conf
  show_current_flags
fi

show_current_flags() {
      echo "Current setting:"
      grep '^CFLAGS=' /etc/make.conf
      grep '^LDFLAGS=' /etc/make.conf
}


Just note this is entirely untested. If it breaks your system... you keep the pieces. Either way.. that saves you 1 file and less "customization" settings.
_________________
Cardoe

Retired Gentoo Developer
Gentopia, MythTV, D-Bus
Back to top
View user's profile Send private message
d_u_s_t
n00b
n00b


Joined: 11 Jul 2006
Posts: 8
Location: Berlin

PostPosted: Sat Sep 16, 2006 1:10 pm    Post subject: Reply with quote

Nice, much better use of sed! Looks like you are more into that than me... ;-)

Works like a charm, I worked on it a little bit to make it even more smart:
show_current_flags() has to be defined at the beginning (at least for me?!), some cleanup and let's do a backup of make.conf ;-)
Also there are now #LASTFLAGS entries that save the old values and it is more verbose. This sould make it a little bit more secure to use!

Code:


#!/bin/sh
show_current_flags() {
      echo "Current setting:"
      grep '^CFLAGS=' /etc/make.conf
      grep '^LDFLAGS=' /etc/make.conf
}

echo "Flags-O-Matic - Quick and dirty make.conf Flags-Changing" # ;o)
if [ $# -ne 2 ]
then
  if [[ $1 == "d" ]]
  then
    cp -af /etc/make.conf /etc/make.conf.flagsbackup
    sed -e '/^#LASTCFLAGS=.*/D' -e 's/^CFLAGS=\(.*\)/#LASTCFLAGS=\1\nCFLAGS=""/g' \
    -e '/^#LASTLDFLAGS=.*/D' -e 's/^LDFLAGS=\(.*\)/#LASTLDFLAGS=\1\nLDFLAGS=""/g' -i /etc/make.conf \
    && echo "Flags unset!" || echo "Error while unsetting flags in make.conf!"
    echo "---"
    show_current_flags
  else
    echo "Usage: \"$0 <CFLAGS-NUM> <LDFLAGS-NUM>\" to set or \"$0 d\" to unset both"
    echo "---"
    show_current_flags
    echo "---"
    echo "Available CFLAGS:"
    cat -b /etc/cflags.conf
    echo "Available LDFLAGS:"
    cat -b /etc/ldflags.conf
  fi
else
  cp -af /etc/make.conf /etc/make.conf.flagsbackup
  CFLAGS=`cat /etc/cflags.conf | sed -n $1p`
  LDFLAGS=`cat /etc/ldflags.conf | sed -n $2p`
  sed -e '/^#LASTCFLAGS=.*/D' -e "s/^CFLAGS=\(.*\)/#LASTCFLAGS=\1\nCFLAGS=\"$CFLAGS\"/g" \
  -e '/^#LASTLDFLAGS=.*/D' -e "s/^LDFLAGS=\(.*\)/#LASTLDFLAGS=\1\nLDFLAGS=\"$LDFLAGS\"/g" -i /etc/make.conf \
  && echo "Flags changed!" || echo "Error while changing flags in make.conf!"
  echo "---"
  show_current_flags
fi
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum