4

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.

Jan
  • 41
  • 4

2 Answers2

0

You seem to have found part of the solution already. To check whether it is correct you could temporarily put

echo %optflags

To print the value once inside your spec file or

%dump

To dump all macros (not sure whether optflags will be printed this way)

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
0

You don't really have to replace -O2 in the optflags to effectively use -O. You just need to append -O to the optflags because gcc only considers the last occurrence of a -O option:

If you use multiple -O options, with or without level numbers, the last such option is the one that is effective.

gcc(1)

Thus, you don't need to shell-out and can simply append -O like this:

%global optflags %{optflags} -O

You can verify this approach by resolving all macros with rpmspec, e.g.:

$ rpmspec -P example.spec
maxschlepzig
  • 35,645
  • 14
  • 145
  • 182