Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 775839 - dev-lang/python: find_library broken on macOS
Summary: dev-lang/python: find_library broken on macOS
Status: IN_PROGRESS
Alias: None
Product: Gentoo/Alt
Classification: Unclassified
Component: Prefix Support (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Gentoo Prefix
URL:
Whiteboard:
Keywords: PATCH
Depends on:
Blocks: 860831
  Show dependency tree
 
Reported: 2021-03-13 17:29 UTC by Sam James
Modified: 2022-07-25 06:36 UTC (History)
0 users

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


Attachments
0001-Lib-ctypes-macholib-dyld.py-tweak-search-paths-for-P.patch (file_775839.txt,1.49 KB, patch)
2021-03-13 17:31 UTC, Sam James
Details | Diff
0001-Lib-ctypes-macholib-dyld.py-tweak-search-paths-for-P.patch (file_775839.txt,1.56 KB, patch)
2021-03-14 01:06 UTC, Sam James
Details | Diff
python-3.9.1_p2-r1.ebuild.patch (file_775839.txt,807 bytes, text/plain)
2021-03-14 01:14 UTC, Sam James
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Sam James archtester Gentoo Infrastructure gentoo-dev Security 2021-03-13 17:29:52 UTC
I noticed an issue when building pkgcheck on macOS 11 (Prefix) [0]:

>OSError: dlopen(/Users/sam/Gentoo/usr/lib/python3.8/site-packages/pkgcheck/_bash-lang.so, 6): Symbol not found: >__ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4copyEPcmm
>  Referenced from: /Users/sam/Gentoo/usr/lib/python3.8/site-packages/pkgcheck/_bash-lang.so
>  Expected in: flat namespace
> in /Users/sam/Gentoo/usr/lib/python3.8/site-packages/pkgcheck/_bash-lang.so

The relevant code from pkgcheck tries to find which c++ stdlib to use:
>     compiler = new_compiler()
>     if cpp:
>        if find_library("stdc++"):
>            compiler.add_library("stdc++")
>        elif find_library("c++"):
>            compiler.add_library("c++")
>

Now:
1) Python 3.8 gave nothing(!)
2) Python 3.9 gave c++ (libcxx - LLVM/Clang).

It turned out that:
1) This is because Python 3.8 lacks support for macOS 11's dyld cache and the fixes haven't been backported from Python 3.9.
2) While Python 3.9 contains the above fixes, it has the wrong library and framework search dirs:

>Lib/ctypes/macholib/dyld.py:
>
> DEFAULT_FRAMEWORK_FALLBACK = [
>     os.path.expanduser("~/Library/Frameworks"),
>     "/Library/Frameworks",
>     "/Network/Library/Frameworks",
>     [...]
>
> DEFAULT_LIBRARY_FALLBACK = [
>     os.path.expanduser("~/lib"),
>     "/usr/local/lib",
>     "/lib",

So, while Python 3.9 has been fixed upstream, it's not Prefix-adapted.


[0] https://github.com/pkgcore/pkgcheck/issues/299#issuecomment-791887928
Comment 1 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2021-03-13 17:31:32 UTC
Created attachment 691203 [details, diff]
0001-Lib-ctypes-macholib-dyld.py-tweak-search-paths-for-P.patch

Using
>sed -i -e "s:\${EPREFIX}:${EPREFIX}:" Lib/ctypes/macholib/dyld.py || die
in the ebuild seems fine.

We should probably guard it, just in case though.
Comment 2 Fabian Groffen gentoo-dev 2021-03-13 20:36:07 UTC
Can you use @GENTOO_PORTAGE_EPREFIX@ instead of ${EPREFIX} in the code, and then call eprefixify on the file?
Comment 3 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2021-03-13 20:48:21 UTC
(In reply to Fabian Groffen from comment #2)
> Can you use @GENTOO_PORTAGE_EPREFIX@ instead of ${EPREFIX} in the code, and
> then call eprefixify on the file?

Of course - I'll do that later. Didn't think about that!
Comment 4 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2021-03-14 01:06:39 UTC
Created attachment 691260 [details, diff]
0001-Lib-ctypes-macholib-dyld.py-tweak-search-paths-for-P.patch
Comment 5 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2021-03-14 01:06:51 UTC Comment hidden (obsolete)
Comment 6 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2021-03-14 01:14:46 UTC
Created attachment 691263 [details]
python-3.9.1_p2-r1.ebuild.patch

For completeness.
Comment 7 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2021-03-17 02:55:47 UTC
Actually, I need a bit of help. We need some way of adding detection for libstdc++ which Python doesn't cater for.

It's in ${EPREFIX}/usr/x86_64-apple-darwin20/lib/gcc/libstdc++.6.dylib and I don't really want to hardcode anything GCC, just reference it if we can.

I suppose there won't be anything magic here, just search in ${EPREFIX}/usr/${CHOST}/lib/gcc/* if it exists...?
Comment 8 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2021-03-17 03:17:53 UTC
(In reply to Sam James from comment #7)
> Actually, I need a bit of help. We need some way of adding detection for
> libstdc++ which Python doesn't cater for.
> 
> It's in ${EPREFIX}/usr/x86_64-apple-darwin20/lib/gcc/libstdc++.6.dylib and I
> don't really want to hardcode anything GCC, just reference it if we can.
> 
> I suppose there won't be anything magic here, just search in
> ${EPREFIX}/usr/${CHOST}/lib/gcc/* if it exists...?

${EPREFIX}/usr/lib/gcc/${CHOST}/10.2.0/ of course for the general lib* (forgot /lib/)
Comment 9 Fabian Groffen gentoo-dev 2021-03-17 07:05:46 UTC
Ok, so a dlopen of some lib is done, that depends on c++.  The lib in question, is that a module/bundle or a shared library?  In case of the latter, does that shared library depend on the c++ runtime?
Comment 10 Fabian Groffen gentoo-dev 2021-04-04 08:44:14 UTC
if I understand this well, the reproducer for this is:

from ctypes.util import find_library
find_library("stdc++")
Comment 11 Fabian Groffen gentoo-dev 2021-04-04 08:52:18 UTC
https://docs.python.org/3/library/ctypes.html#finding-shared-libraries

documents that on Linux, a search is done using external tools (like ld), while on macOS it sticks to the paths that you found, and adapted.

This explains why find_library finds our prefix libs just fine without help on e.g. Solaris, and we have this particular problem on macOS.  To make it find our GCC libs, we probably have to see if we can actually adapt the implementation to also do a runtime search.
Comment 12 Fabian Groffen gentoo-dev 2022-07-25 06:36:43 UTC
what I think is missing in this patch is the path to the GCC libs, e.g. $EPREFIX /usr/lib/gcc/arm64-apple-darwin21/12.1.0, for that's where one can find the correct libstdc++.dylib.

I think we need to add Gentoo-specific magic here to load at runtime the current list of paths, we could simply read $EPREFIX/etc/ld.so.conf, which contains the paths as they should be.

DEFAULT_LIBRARY_FALLBACK should be populated with ld.so.conf material in front
DEFAULT_FRAMEWORK_FALLBACK should use $EPREFIX/Frameworks followed by the sdk one.