Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 883201 - dev-libs/gmp-6.2.1-r2: econf ruins gmp-specific platform autodetection
Summary: dev-libs/gmp-6.2.1-r2: econf ruins gmp-specific platform autodetection
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Gentoo Toolchain Maintainers
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-11-26 17:52 UTC by Nikita Zlobin
Modified: 2023-02-12 22:49 UTC (History)
1 user (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
zhu3d-4.2.6-r2.ebuild (zhu3d-4.2.6-r2.ebuild,1.95 KB, text/plain)
2022-11-29 16:33 UTC, Nikita Zlobin
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nikita Zlobin 2022-11-26 17:52:33 UTC
Problem is already described at forum
https://forums.gentoo.org/viewtopic-t-1078406.html

In short: simple './configure' in sources autodetects what build must be optimized for. In my case it's "sandybridge-pc-linux-gnu". However, from ebuild - it always uses x86_64-pc-linux-gnu for ABI-64 and i686 variant for ABI=32 

(btw, './configure ABI=32' also selects sandybridge rather than something 32bit only even though adds -m32 in compiler options).

One notable difference I noticed in configure output is somewhat truncated MPN_PATH.

For manual ./configure:
##################################
.....
using ABI="64"
      CC="gcc"
      CFLAGS="-march=native"
      CPPFLAGS=""
      MPN_PATH=" x86_64/coreisbr x86_64/coreinhm x86_64/core2 x86_64 generic"
.....
configure: summary of build options:

  Version:           GNU MP 6.2.1
  Host type:         sandybridge-pc-linux-gnu
  ABI:               64
  Install prefix:    /usr/local
  Compiler:          gcc
  Static libraries:  yes
  Shared libraries:  yes
##################################

And for ebuild:
##################################
.....
using ABI="64"
      CC="x86_64-pc-linux-gnu-gcc"
      CFLAGS="-O2 -march=native -pipe"
      CPPFLAGS=""
      CXX="x86_64-pc-linux-gnu-g++"
      CXXFLAGS="-O2 -march=native -pipe"
      MPN_PATH=" x86_64/k8 x86_64 generic"
.....
configure.wrapped: summary of build options:

  Version:           GNU MP 6.2.1
  Host type:         x86_64-pc-linux-gnu
  ABI:               64
  Install prefix:    /usr
  Compiler:          x86_64-pc-linux-gnu-gcc
  Static libraries:  no
  Shared libraries:  yes
##################################

Trick using /etc/portage/env with /etc/portage/package.env is useless:
setting CBUILD does nothing, but with CHOST - nothing for 32bit, but crashes for 64bit right in configure:

##################################
checking build system type... x86_64-pc-linux-gnu
checking host system type... Invalid configuration `sandybridge-pc-linux-gnu': machine `sandybridge-pc' not recognized
configure.wrapped: error: /bin/sh /var/calculate/tmp/portage/dev-libs/gmp-6.2.1-r2/work/gmp-6.2.1/config.sub sandybridge-pc-linux-gn
##################################

One thing I noticed - config.sub is replaced when built from ebuild.
As for build - I don't remember if tried to manually build with ABI=32, but defaults work.

Is there reason, why CBUILD / CHOST are forced to something generic?

I see some sort of hacking in src_prepare, but did not check (perhaps, it's way to reproduce).

Reproducible: Always

Steps to Reproduce:
1. ebuild `equery w dev-libs/gmp` configure
Comment 1 Nikita Zlobin 2022-11-26 19:07:11 UTC
So, I confirm, that it's due to econf, more exactly - at this moment at configure stage:

 * econf: updating gmp-6.2.1/config.sub with /usr/share/gnuconfig/config.sub
 * econf: updating gmp-6.2.1/config.guess with /usr/share/gnuconfig/config.guess

If trying to grep in source root dir: grep -nFl -e sandy ./*
It will show lines from these files:

./acinclude.m4
./_config.guess
./config.guess
./config.in
./_config.sub
./config.sub
./configure.ac
./configure.wrapped

where ./_config.{sub,guess} is backup, I did before to try 'configure' step.

After restoring backuped files, I can just run: ./config.guess, and it will print value:

$ ./config.guess 
sandybridge-pc-linux-gnu

while variants, substituted with econf, print: x86_64-pc-linux-gnu
Comment 2 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2022-11-26 19:39:51 UTC
The reason it forces a "generic" build is because otherwise binpkgs would be unusable. Investigation will be required as to whether the produced build is genuinely generic or not, as this is a bizarre abuse of CHOST (which is typically used for compiler invocations and such).
Comment 3 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2022-11-26 19:42:28 UTC
(In reply to Sam James from comment #2)
> The reason it forces a "generic" build is because otherwise binpkgs would be
> unusable. Investigation will be required as to whether the produced build is
> genuinely generic or not, as this is a bizarre abuse of CHOST (which is
> typically used for compiler invocations and such).

(Wrt genuinely generic: it could well be that either -march makes things work fine, or it does runtime detection instead.)
Comment 4 Nikita Zlobin 2022-11-26 19:46:25 UTC
gmp docs recommend configure --host option only for cross-compilation, for own build like in gentoo --build must be enough. But still - config stuff better be kept intact.

Of course, I really don't know, is it correct to propose e.g. sandybridge or like that for 32bit - but that could be gmp specifics as well.
Comment 5 Nikita Zlobin 2022-11-26 19:48:21 UTC
There could be make.conf variable like GMP_BUILD, similar to OPENBLAS_TARGET from openblas ebuild.
Comment 6 Nikita Zlobin 2022-11-26 19:50:11 UTC
> (Wrt genuinely generic: it could well be that either -march makes things work fine, or it does runtime detection instead.)

gmp optimization is not limited by flags, there are specially coded asms, aimed to push best cpu features to their limits.
Comment 7 Nikita Zlobin 2022-11-29 12:43:50 UTC
(In reply to Sam James from comment #2)
> The reason it forces a "generic" build is because otherwise binpkgs would be
> unusable. Investigation will be required as to whether the produced build is
> genuinely generic or not, as this is a bizarre abuse of CHOST (which is
> typically used for compiler invocations and such).

There is what I learned and consider going fine, as must:

First for above:
I checked "ebuild --info" and found both CBUILD and CHOST set to "x86_64-pc-linux-gnu", so this part is fine. This explains, why exactly this value is picked by gmp ./configure. In case of gmp it's stated, that while --build specifies machine, used to build, machine to run at is set by --host (no wonder, lone CBUILD will not work).
Thus, everything is fine, this part is checked.

About MPN_PATH, printed in begining of ./configure output:
I determined, that it depends on picked host type, and for ebuild it matches to supplied value "x86_64-pc-linux-gnu".
Also nothing to worry about.

The only remaining problem is config* files substitution, as it makes originally valid configurations, specific not only to ABI, but also to CPU targets (nehalem, sandybridge, skylike, etc) not be recognized by configure system and be treated with error, causing the very configuration to fail.

Just one interesting part - when configuring for 32bit, it picks "i686-pc-linux-gnu". I guess - this is due to config* stuff substitution, as ./configure itself has special option for proper ABI= (either 32 or 64), which doesn't affect host type. E.g. if I run: "./configure ABI=32 --build=x86_64-pc-linux-gnu", I get this summary:

  Version:           GNU MP 6.2.1
  Host type:         x86_64-pc-linux-gnu
  ABI:               32
  Install prefix:    /usr/local
  Compiler:          gcc
  Static libraries:  yes
  Shared libraries:  yes
Comment 8 Nikita Zlobin 2022-11-29 12:57:02 UTC
Of, forgot - it would be great to see difference between using i686 and x86_64 used for 32bit ABI. Also 
Watch for MPN_PATH and resulting CFLAGS.
Unrelated noise is intentionally omited.

./configure ABI=32 --build=i686-pc-linux-gnu
using ABI="32"
      CC="gcc"
      CFLAGS="-m32 -O2 -pedantic -fomit-frame-pointer -mtune=pentiumpro -march=pentiumpro"
      CPPFLAGS=""
      MPN_PATH=" x86/p6 x86 generic"

./configure ABI=64 --build=i686-pc-linux-gnu
configure.wrapped: error: ABI=64 is not among the following valid choices: 32

./configure ABI=32 --build=x86_64-pc-linux-gnu
using ABI="32"
      CC="gcc"
      CFLAGS="-m32 -O2 -pedantic -fomit-frame-pointer -mtune=k8 -march=k8"
      CPPFLAGS=""
      MPN_PATH=" x86/k8 x86/k7/mmx x86/k7 x86/mmx x86 generic"

./configure ABI=64 --build=x86_64-pc-linux-gnu:
using ABI="64"
      CC="gcc"
      CFLAGS="-O2 -pedantic -fomit-frame-pointer -m64 -mtune=k8 -march=k8"
      CPPFLAGS=""
      MPN_PATH=" x86_64/k8 x86_64 generic"

./configure ABI=32 --build=sandybridge-pc-linux-gnu
using ABI="32"
      CC="gcc"
      CFLAGS="-m32 -O2 -pedantic -fomit-frame-pointer -mtune=sandybridge -march=sandybridge"
      CPPFLAGS=""
      MPN_PATH=" x86/coreisbr x86/p6/sse2 x86/p6/p3mmx x86/p6/mmx x86/p6 x86/mmx x86 generic"

./configure ABI=64 --build=sandybridge-pc-linux-gnu
using ABI="64"
      CC="gcc"
      CFLAGS="-O2 -pedantic -fomit-frame-pointer -m64 -mtune=sandybridge -march=sandybridge"
      CPPFLAGS=""
      MPN_PATH=" x86_64/coreisbr x86_64/coreinhm x86_64/core2 x86_64 generic"
Comment 9 Nikita Zlobin 2022-11-29 16:33:35 UTC Comment hidden (obsolete)
Comment 10 Nikita Zlobin 2022-11-29 16:35:12 UTC Comment hidden (obsolete)
Comment 11 Nikita Zlobin 2022-11-29 16:36:02 UTC Comment hidden (obsolete)
Comment 12 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2022-11-29 20:03:19 UTC
(In reply to Nikita Zlobin from comment #6)
> > (Wrt genuinely generic: it could well be that either -march makes things work fine, or it does runtime detection instead.)
> 
> gmp optimization is not limited by flags, there are specially coded asms,
> aimed to push best cpu features to their limits.

Various projects correctly deduce this based on preprocessor macros, so again that's not ruled out.
Comment 13 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2022-11-29 20:53:58 UTC
What I'm saying is: I agree there _may_ be a problem and it should be investigated, but obsessing over the thing upstream deviate from vanilla on may be a red herring. We'll see. Rest assured I get your point.
Comment 14 Nikita Zlobin 2022-11-30 04:07:00 UTC
There's no really obsession. I just misunderstood, what exactly happened in my config, so decided to clarify in last two posts :).
Comment 15 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2022-11-30 04:34:36 UTC
(In reply to Nikita Zlobin from comment #14)
> There's no really obsession. I just misunderstood, what exactly happened in
> my config, so decided to clarify in last two posts :).

No worries, didn't mean it derogatorily, I just mean it might be that it's not that. Or it might be and then I'll tear my hair out :(

But in any case, thanks for raising it.
Comment 16 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2022-11-30 07:28:10 UTC
It looks like --enable-fat will give us a truly generic build with lots of extra bits installed for various CPUs.
Comment 17 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2022-11-30 07:28:35 UTC
(In reply to Sam James from comment #16)
> It looks like --enable-fat will give us a truly generic build with lots of
> extra bits installed for various CPUs.

uh, forgive me: generic in the sense that it's going to work with binpkgs (instruction set agnostic) but install bonus loadables which can be used at runtime.
Comment 18 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2022-11-30 07:33:11 UTC
(In reply to Sam James from comment #17)
> (In reply to Sam James from comment #16)
> > It looks like --enable-fat will give us a truly generic build with lots of
> > extra bits installed for various CPUs.
> 
> uh, forgive me: generic in the sense that it's going to work with binpkgs
> (instruction set agnostic) but install bonus loadables which can be used at
> runtime.

Ugh, fat doesn't work for !x86:
>     Fat binary, --enable-fat
>
>    Using --enable-fat selects a “fat binary” build on x86, where optimized low >level subroutines are chosen at runtime according to the CPU detected. This >means more code, but gives good performance on all x86 chips. (This option >might become available for more architectures in the future.)
Comment 19 Larry the Git Cow gentoo-dev 2022-11-30 07:48:52 UTC
The bug has been referenced in the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=78b4d4ebbcee12f5ef4c1dfa89f80c98a582db9d

commit 78b4d4ebbcee12f5ef4c1dfa89f80c98a582db9d
Author:     Sam James <sam@gentoo.org>
AuthorDate: 2022-11-30 07:37:50 +0000
Commit:     Sam James <sam@gentoo.org>
CommitDate: 2022-11-30 07:48:13 +0000

    dev-libs/gmp: pass --enable-fat for x86 (includes more asm)
    
    Vanilla (_not_ from an ebuild) gmp will probe CHOST and then
    deduce its *own* tuple based on the currently used CPU, abusing
    its own custom config.guess (which our econf overwrites). This
    value is used to decide which accelerated asm is compiled in (MPN_PATH).
    
    In ebuilds, we override and refresh config.guess and friends because
    it's a common portability problem to have stale versions which don't
    even recognise e.g. riscv as an arch. But gmp's non-standard use
    of these files poses a problem for us.
    
    We can't simply resort to just not refreshing/changing the files
    because we need to be able to build binary packages on a system
    without them being super specific to the machine they were built
    on with no way of controlling it (we may want to look at a USE
    flag for this going forward though).
    
    For now, we can use --enable-fat which produces a generic libgmp
    which can load various asm at runtime based on the CPU in use - success!
    
    Unfortunately, --enable-fat only does anything on x86 for the time
    being, so !x86 is still in the same situation as before this commit,
    but this is progress at least.
    
    Thanks goes to Nikita Zlobin <nick87720z@gmail.com> for noticing
    the problem originally (and being persistent with investigating it).
    
    Temporarily unkeyworded until I can test this a bit more.
    
    Bug: https://bugs.gentoo.org/883201
    Signed-off-by: Sam James <sam@gentoo.org>

 dev-libs/gmp/gmp-6.2.1-r3.ebuild | 130 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 130 insertions(+)
Comment 20 Larry the Git Cow gentoo-dev 2022-11-30 08:31:43 UTC
The bug has been referenced in the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=bf286a7973d8b95dbef90f17cc8e2d66c19a47c8

commit bf286a7973d8b95dbef90f17cc8e2d66c19a47c8
Author:     Sam James <sam@gentoo.org>
AuthorDate: 2022-11-30 08:30:15 +0000
Commit:     Sam James <sam@gentoo.org>
CommitDate: 2022-11-30 08:31:33 +0000

    dev-libs/gmp: tie --enable-fat to USE=cpudetection
    
    Note that USE=-cpudetection still gives a very generic
    build rather than one detected at build time to be
    precisely the machine compiled on (for now?)
    
    Bug: https://bugs.gentoo.org/883201
    Signed-off-by: Sam James <sam@gentoo.org>

 dev-libs/gmp/gmp-6.2.1-r3.ebuild     |  7 ++++---
 dev-libs/gmp/metadata.xml            | 23 ++++++++++++-----------
 profiles/arch/amd64/package.use.mask |  4 ++++
 profiles/arch/base/package.use.mask  |  4 ++++
 profiles/arch/x86/package.use.mask   |  4 ++++
 5 files changed, 28 insertions(+), 14 deletions(-)
Comment 21 Larry the Git Cow gentoo-dev 2022-12-01 03:42:11 UTC
The bug has been referenced in the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=121152c38caefd8e94fb8c90cb51102693b6d6a8

commit 121152c38caefd8e94fb8c90cb51102693b6d6a8
Author:     Sam James <sam@gentoo.org>
AuthorDate: 2022-12-01 03:26:54 +0000
Commit:     Sam James <sam@gentoo.org>
CommitDate: 2022-12-01 03:41:54 +0000

    dev-libs/gmp: allow automatic detection of host for USE=-cpudetection
    
    Bug: https://bugs.gentoo.org/883201
    Closes: https://bugs.gentoo.org/235697
    Closes: https://bugs.gentoo.org/545442
    Signed-off-by: Sam James <sam@gentoo.org>

 .../{gmp-6.2.1-r3.ebuild => gmp-6.2.1-r4.ebuild}   | 32 +++++++++++++++++++---
 dev-libs/gmp/metadata.xml                          | 13 ++++++++-
 profiles/arch/amd64/package.use.mask               |  4 ---
 profiles/arch/base/package.use.mask                |  4 ---
 profiles/arch/x86/package.use.mask                 |  4 ---
 5 files changed, 40 insertions(+), 17 deletions(-)
Comment 22 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2022-12-01 03:49:40 UTC
Okay, all done, I think:

There's now two improvements:
1. on amd64/x86, we benefit from "fat binaries" which lets you still make binpkgs easily, but if you do USE=-cpudetection (cpudetection is default, "fat mode"), you can have it tuned for your CPU.

2. for non-amd64/x86, the default behaviour for them still sucks a bit because there's no fat support, but they get a warning telling them to do USE=-cpudetection if they don't mind no binpkgs. But the point is now we now allow people to get that native detection if they want it.

In summary:
- amd64/x86 && USE=cpudetection (--enable-fat): *slightly* (100KB?) larger library which contains routines for various subarches, optimised, can use binpkgs as a result
- amd64/x86 && USE=-cpudetection: optimised specifically for CPU used to do the building, can't use binpkgs as a result
- !amd64 && !x86 && USE=cpudetection: the same build as before all of these changes, generic C implementation, can use binpkgs as a result. Upstream does not support fat builds for these arches for now.
- !amd64 && !x86 && USE=-cpudetection: optimised specifically for CPU used to do the building, can't use binpkgs as a result
Comment 23 Nikita Zlobin 2022-12-02 20:49:27 UTC
Nice!
Did not know about cure from Denis (still had to be used from ebuild).
Now we leverage autoconf intrinsics, hope it's stable :)
Comment 24 Larry the Git Cow gentoo-dev 2022-12-06 10:53:13 UTC
The bug has been referenced in the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=2e58af672b41a624f84eb7fada145ab2f6c2b1b6

commit 2e58af672b41a624f84eb7fada145ab2f6c2b1b6
Author:     Sam James <sam@gentoo.org>
AuthorDate: 2022-12-06 10:48:07 +0000
Commit:     Sam James <sam@gentoo.org>
CommitDate: 2022-12-06 10:52:50 +0000

    dev-libs/gmp: further econf fixes
    
    * Use gnuconfig.eclass to update configfsf.{guess,sub} before the wrapped
      versions call them.
    * Export CBUILD too just in case for the deduced host from upstream's wrapped
      config.guess (not the same as gnuconfig's at all, see bugs for details).
    * Save/restore upstream's config.{guess,sub} to make sure that for the second
      multilib build we still have it available to run. In practice, this may not
      have mattered too much, as the exported ac_cv_host should've persisted, but
      it's still worth being (clearly) correct as is the case now.
    * Move cpudetection warning into pkg_pretend so it's easier to spot and
      we also don't then repeat it for each ABI.
    
    Bug: https://bugs.gentoo.org/235697
    Bug: https://bugs.gentoo.org/545442
    Bug: https://bugs.gentoo.org/883201
    See: 78b4d4ebbcee12f5ef4c1dfa89f80c98a582db9d
    See: 3fe1b5370c0cc1537d1249e8ec9d2fbcd8619499
    See: bf286a7973d8b95dbef90f17cc8e2d66c19a47c8
    See: 121152c38caefd8e94fb8c90cb51102693b6d6a8
    Signed-off-by: Sam James <sam@gentoo.org>

 .../{gmp-6.2.1-r4.ebuild => gmp-6.2.1-r5.ebuild}   | 32 ++++++++++++++++++----
 1 file changed, 26 insertions(+), 6 deletions(-)
Comment 25 Nikita Zlobin 2022-12-26 21:34:39 UTC
I can't see, how this can work. There' some mess.
econf runs own update unconditionally, affecting resulting MPN_PATH, but than summary still prints host.

Variables like ac_cv_build / ac_cv_host are only in configure.wrapped, but there's no references for this file. Regular configure is not using them.

Also: Just discovered, that MPN_PATH may be configured, but that's clearly expert-level.
Comment 26 Nikita Zlobin 2022-12-26 21:59:46 UTC
err,  I was blinded by endless mpn/generic in the config end, MPN_PATH is as expected.
Comment 27 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2022-12-27 00:44:13 UTC
(Note that I did check the final binaries are different both in size and in symbols too.)