rpmrpmbuildrpm-spec

How overwrite optflags direct in rpm spec file?


How correct overwrite %optflags direct in spec file ?

$ rpm --eval %optflags   # Fedora 29
-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection

I need replace -O2 by -O in %optflags macro direct in my spec file ? I found only this one solution:

%global optflags %(echo %{optflags} | sed 's/-O[0-3]/-O/')

It is correct ?

Note rpmbuild -ba --define "optflags -O bla bla" my.spec it is not a solution for my case.


Solution

  • The %{optflags} macro, works as a wrapper with many other macros on it. The -O2 is an individual compiler flag which usually can be defined in CFLAGS.

    When running rpm --showrc|grep '\-O2' we can see it is part of the macro _general_options. So you can adjust it in the spec file as:

    %define _general_options -O
    

    The %{optflags} will inherit this macro and as result the -O2 will become -O

    Alternatively you can use:

    %define _general_option  -O %{?_lto_cflags} -fexceptions -g -grecord-gcc-switches -pipe
    

    This way, you will keep everything but just change the -O2 flag

    A good reference is the buildflags documentation