rules_python and linking with libpython #3153
-
Hello, Our C++ project produces a collection of dynamic libraries. Along with each library we use swig to generate a python interface for the libraries. Swig produces 2 files for each library: a python file, and a C++ source file with the wrapper code. As you can imagine, this swig C++ file contains many calls to the python C API, and as such the resulting swig dynamic library needs to be linked with some version of Our project needs to be able to build in multiple environments (including customer site), including x86_64 vs. aarch64 (both target and host). Because of this, we are leveraging In order to link with the python library, I (perhaps naively) added Upon further examination of the
What would be the idiomatic way to address this? One thought I had was to have two targets in rules_python: the existing one, and another with I'm using bazel 8.3.1 and rules_python 1.1.0. Our workspace uses bzlmod. Would it help if I move ahead to a newer version of rules_python? Thanks for your time and attention. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Hm, weird. There's not intentional, so must be some idiosyncrasy of the cc rules. No idea why one target would use iquote and the other isystem. (isystem should be used, since
Yeah, the link settings are a quagmire. AFAIU, The basic problem is: If it adds alwayslink=true causes a similar problem -- it forces the final binary (be it a cc_binary or shared library) to always link against it. Since consumers can add extra e.g. link opts, but can't remove them, I thought it best to omit those settings. Another reason was it wasn't clear to me what the correct In any case, I couldn't find docs that explained things, so also thought it best to omit it so users could add the necessary values. Clarity, references, and expertise in this area is much welcomed.
Maybe? I don't recall any major changes sinces 1.1, but things like |
Beta Was this translation helpful? Give feedback.
-
@rickeylev Thanks for your response and explanations, much appreciated. I understand the need to keep Can you (or anyone) suggest a way to do this without forcing a hard-code of the library name in I will also move to latest rules_python, we don't have a compelling reason to stay at the current version. |
Beta Was this translation helpful? Give feedback.
-
By hardcoding, i'm guessing you mean adding Two thoughts come to mind: (1) Use a select(). You'll have to enumerate the condition -> values. If that set is small, then this is an easy and teneable option. e.g. (2) Custom rule that reads the settings and populates CcInfo link opts. Something like this:
That should add the appropriate (the above is pretty boiler-platey, so maybe we should add e.g. HTH |
Beta Was this translation helpful? Give feedback.
-
Many thanks, this I think gives me what I need for now. |
Beta Was this translation helpful? Give feedback.
By hardcoding, i'm guessing you mean adding
-lpython3.12
(the version specific part)?Two thoughts come to mind:
(1) Use a select(). You'll have to enumerate the condition -> values. If that set is small, then this is an easy and teneable option. e.g.
linkopts = select({":is_py_312": ["-lpython3.12"], ...})
, whereis_py_312
is e.g.config_setting(<match @rules_python//python/config_settings:python_version_major_minor = 3.12>)
(2) Custom rule that reads the settings and populates CcInfo link opts. Something like this: