[INSTRM-1785] Fix conda gcc linking of external libraries. Created: 11/Nov/22  Updated: 15/Nov/22  Resolved: 12/Nov/22

Status: Won't Fix
Project: Instrument control development
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Normal
Reporter: cloomis Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Relates
relates to INSTRM-1615 RFC: Consider basing ICS on rubin-env... Open

 Description   

The agcc actor requires building a cython module for the external FLI camera library. That library uses the external libusb-1.0.so to actually connect to the device.

With rubin3-ics, the extension .so is built but does not then load libusb.

Bad:

(conda-ics) pfs-data@AGCC-OptiPlex-755:ics_agccActor-pfsdata$ ldd fli_camera.cpython-38-x86_64-linux-gnu.so
	linux-vdso.so.1 =>  (0x00007ffd177d8000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9688095000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f968845e000)

Good:

(conda-ics) pfs-data@AGCC-OptiPlex-755:ics_agccActor-pfsdata$ ldd fli_camera.cpython-38-x86_64-linux-gnu.so
	linux-vdso.so.1 =>  (0x00007ffcde7f6000)
	libusb-1.0.so.0 => /software/conda/envs/rubin3_ics/lib/libusb-1.0.so.0 (0x00007f2fc1bad000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2fc165d000)
	libudev.so.1 => /software/conda/envs/rubin3_ics/lib/./libudev.so.1 (0x00007f2fc1b60000)
	libatomic.so.1 => /software/conda/envs/rubin3_ics/lib/./libatomic.so.1 (0x00007f2fc1b54000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f2fc143f000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f2fc1a26000)
	librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f2fc1237000)

rubin3-ics switched from using the system compilers to using the conda compiler packages. These are set up by /software/conda/envs/rubin3_ics/etc/conda/activate.d/activate-gcc_linux-64.sh

One flag which conda adds is Wl,as-needed, and that is what causes the external libraries to be trimmed. I have not figured if that is really correct, nor how to bypass it. You can add -Wl,-no-as-needed to the extra_ld_args Extension arguments, but that is added too late in the gcc call: that argument needs to come before the listings of the object files and libraries. So the distutils quick fix does not work here, and I have not found a better one.

This cannot be right. Surely many Extensions out there require external libraries. So I am missing something.

If this is right, we can simply edit the /software/conda/envs/rubin3_ics/etc/conda/activate.d/activate-gcc_linux-64.sh file. Yuck.



 Comments   
Comment by cloomis [ 12/Nov/22 ]

Mistaken. The issue for the agccActor was that with `--as-needed`, the order of libraries matters (again), as it should. Fix pushed there.

FWIW or for reference, the following page was the most helpful one I found: https://wiki.gentoo.org/wiki/Project:Quality_Assurance/As-needed

Comment by chyan [ 15/Nov/22 ]

Actually, I found a possible fix. By removing the default Wl,-as-needed from LDFLAGS, I can compile flight_camera package without problem.

from distutils.extension import Extension
from Cython.Distutils import build_ext
import sdss3tools
import os
import numpy

os.environ['LDFLAGS']="-Wl,-O2 -Wl,--sort-common -Wl,-z,relro -Wl,"\
    "-z,now -Wl,--disable-new-dtags -Wl,--gc-sections -Wl,--allow-shlib-undefined "\
    "-Wl,-rpath,/software/conda/envs/rubin3_ics/lib "\
    "-Wl,-rpath-link,/software/conda/envs/rubin3_ics/lib -L/software/conda/envs/rubin3_ics/lib"

FLI_module = Extension(
    "fli_camera",
    ["python/agccActor/fli/fli_camera.pyx"],
    library_dirs = ["c/libfli-1.999.1-180223"],
    libraries = ["usb-1.0","fli"],
    include_dirs = ["c/libfli-1.999.1-180223",
                    "python/agccActor/fli",
                    numpy.get_include()],
)

sdss3tools.setup(
    name = "agcc",
    description = "Subaru PFI AGCC actor.",
    cmdclass = {"build_ext": build_ext},
    ext_modules = [FLI_module]
)


Comment by cloomis [ 15/Nov/22 ]

It turns out the only problem was the order of the libraries. If you change that line to libraries = ["fli","usb-1.0"] the linking works without any other changes – that change was pushed when I closed the ticket. The --as-needed change just made that order important. Fooled me.

Generated at Sat Feb 10 16:39:26 JST 2024 using Jira 8.3.4#803005-sha1:1f96e09b3c60279a408a2ae47be3c745f571388b.