| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
|
| 5 |
import setuptools.command.build_ext |
| 6 |
from setuptools import Distribution, find_packages, setup |
| 7 |
from setuptools.extension import Extension, Library |
| 8 |
|
| 9 |
# despite its name, setuptools.command.build_ext.link_shared_object won't |
| 10 |
# link a shared object on Linux, but a static library and patches distutils |
| 11 |
# for this ... We're patching this back now. |
| 12 |
|
| 13 |
|
| 14 |
def always_link_shared_object( |
| 15 |
self, |
| 16 |
objects, |
| 17 |
output_libname, |
| 18 |
output_dir=None, |
| 19 |
libraries=None, |
| 20 |
library_dirs=None, |
| 21 |
runtime_library_dirs=None, |
| 22 |
export_symbols=None, |
| 23 |
debug=0, |
| 24 |
extra_preargs=None, |
| 25 |
extra_postargs=None, |
| 26 |
build_temp=None, |
| 27 |
target_lang=None, |
| 28 |
): |
| 29 |
self.link( |
| 30 |
self.SHARED_LIBRARY, |
| 31 |
objects, |
| 32 |
output_libname, |
| 33 |
output_dir, |
| 34 |
libraries, |
| 35 |
library_dirs, |
| 36 |
runtime_library_dirs, |
| 37 |
export_symbols, |
| 38 |
debug, |
| 39 |
extra_preargs, |
| 40 |
extra_postargs, |
| 41 |
build_temp, |
| 42 |
target_lang, |
| 43 |
) |
| 44 |
|
| 45 |
|
| 46 |
setuptools.command.build_ext.libtype = "shared" |
| 47 |
setuptools.command.build_ext.link_shared_object = always_link_shared_object |
| 48 |
|
| 49 |
libtype = setuptools.command.build_ext.libtype |
| 50 |
build_ext_cmd = Distribution().get_command_obj("build_ext") |
| 51 |
build_ext_cmd.initialize_options() |
| 52 |
build_ext_cmd.setup_shlib_compiler() |
| 53 |
|
| 54 |
|
| 55 |
def libname(name): |
| 56 |
"""Gets 'name' and returns something like libname.cpython-37m-darwin.so""" |
| 57 |
filename = build_ext_cmd.get_ext_filename(name) |
| 58 |
fn, _ = os.path.splitext(filename) |
| 59 |
return build_ext_cmd.shlib_compiler.library_filename(fn, libtype) |
| 60 |
|
| 61 |
|
| 62 |
pkg_name = "nonpy_rpath" |
| 63 |
crypt_name = "_cryptexample" |
| 64 |
crypt_soname = libname(crypt_name) |
| 65 |
|
| 66 |
build_cmd = Distribution().get_command_obj("build") |
| 67 |
build_cmd.finalize_options() |
| 68 |
build_platlib = build_cmd.build_platlib |
| 69 |
|
| 70 |
|
| 71 |
def link_args(soname=None): |
| 72 |
args = [] |
| 73 |
if soname: |
| 74 |
args += ["-Wl,-soname," + soname] |
| 75 |
loader_path = "$ORIGIN" |
| 76 |
args += ["-Wl,-rpath," + loader_path] |
| 77 |
return args |
| 78 |
|
| 79 |
|
| 80 |
nonpy_rpath_module = Extension( |
| 81 |
pkg_name + "._nonpy_rpath", |
| 82 |
language="c++", |
| 83 |
sources=["nonpy_rpath.cpp"], |
| 84 |
extra_link_args=link_args(), |
| 85 |
extra_objects=[build_platlib + "/nonpy_rpath/" + crypt_soname], |
| 86 |
) |
| 87 |
crypt_example = Library( |
| 88 |
pkg_name + "." + crypt_name, |
| 89 |
language="c++", |
| 90 |
extra_compile_args=["-lcrypt"], |
| 91 |
extra_link_args=[*link_args(crypt_soname), "-lcrypt"], |
| 92 |
sources=["extensions/testcrypt.cpp"], |
| 93 |
) |
| 94 |
|
| 95 |
setup( |
| 96 |
name="nonpy_rpath", |
| 97 |
version="0.0.1", |
| 98 |
packages=find_packages(), |
| 99 |
description="Test package for nonpy_rpath", |
| 100 |
ext_modules=[crypt_example, nonpy_rpath_module], |
| 101 |
) |