Skip to content

Commit c4267ee

Browse files
committed
add libstdcxx version-bounds option
1 parent d3aa2e7 commit c4267ee

File tree

3 files changed

+58
-17
lines changed

3 files changed

+58
-17
lines changed

docs/src/pythoncall.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,24 @@ into it. If you want to use a pre-existing Conda environment, see the previous s
285285
If `conda`, `mamba` or `micromamba` is not in your `PATH` you will also need to set
286286
`JULIA_CONDAPKG_EXE` to its path.
287287

288+
#### If you installed a newer version of libstdc++
289+
On Linux, Julia comes bundled with its own copy of *libstdc++*. Therefore, when interacting with
290+
CondaPkg, PythonCall injects a dependency to bound the allowed versions of the `libstdcxx-ng`
291+
Conda package. To override this value (e.g. if the user replaced the bundled libstdc++ with something
292+
newer), use:
293+
294+
```julia
295+
[PythonCall]
296+
ENV["JULIA_CONDAPKG_LIBSTDCXX_VERSION_BOUND"] = ">=3.4,<=12"
297+
```
298+
299+
To figure out installed version, run
300+
```bash
301+
strings /path/to/julia/lib/julia/libstdc++.so.6 | grep GLIBCXX
302+
```
303+
Then look at <https://gcc.gnu.org/onlinedocs/gcc-12.1.0/libstdc++/manual/manual/abi.html>
304+
for the GCC version compatible with the GLIBCXX version.
305+
288306
## [Installing Python packages](@id python-deps)
289307

290308
Assuming you haven't [opted out](@ref pythoncall-config), PythonCall uses

src/cpython/context.jl

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,31 @@ function _atpyexit()
3030
return
3131
end
3232

33+
# By default, ensure libstdc++ in the Conda environment is compatible with
34+
# the one linked in Julia. This is platform/version dependent, so needs to
35+
# occur at runtime.
36+
#
37+
# Allow the user to override the default. This is useful when the version
38+
# of libstdcxx linked in Julia is customized in the local installation of
39+
# Julia.
40+
#
41+
# To figure out cxx_version for a given Julia version, run
42+
# strings /path/to/julia/lib/julia/libstdc++.so.6 | grep GLIBCXX
43+
# then look at
44+
# https://gcc.gnu.org/onlinedocs/gcc-12.1.0/libstdc++/manual/manual/abi.html
45+
# for the highest GCC version compatible with the highest GLIBCXX version.
46+
function get_libstdcxx_version_bound()
47+
if Base.VERSION <= v"1.6.2"
48+
# GLIBCXX_3.4.26
49+
cxx_version = ">=3.4,<9.2"
50+
else
51+
# GLIBCXX_3.4.29
52+
# checked up to v1.8.0
53+
cxx_version = ">=3.4,<11.4"
54+
end
55+
get(ENV, "JULIA_CONDAPKG_LIBSTDCXX_VERSION_BOUND", cxx_version)
56+
end
57+
3358
function init_context()
3459

3560
CTX.is_embedded = haskey(ENV, "JULIA_PYTHONCALL_LIBPTR")
@@ -60,23 +85,7 @@ function init_context()
6085
exe_path::String
6186
else
6287
if Sys.islinux()
63-
# Ensure libstdc++ in the Conda environment is compatible with the one
64-
# linked in Julia. This is platform/version dependent, so needs to occur at
65-
# runtime.
66-
#
67-
# To figure out cxx_version for a given Julia version, run
68-
# strings /path/to/julia/lib/julia/libstdc++.so.6 | grep GLIBCXX
69-
# then look at
70-
# https://gcc.gnu.org/onlinedocs/gcc-12.1.0/libstdc++/manual/manual/abi.html
71-
# for the highest GCC version compatible with the highest GLIBCXX version.
72-
if Base.VERSION <= v"1.6.2"
73-
# GLIBCXX_3.4.26
74-
cxx_version = ">=3.4,<9.2"
75-
else
76-
# GLIBCXX_3.4.29
77-
# checked up to v1.8.0
78-
cxx_version = ">=3.4,<11.4"
79-
end
88+
cxx_version = get_libstdcxx_version_bound()
8089
CondaPkg.add("libstdcxx-ng", version=cxx_version, channel="conda-forge", temp=true, file=joinpath(@__DIR__, "..", "..", "CondaPkg.toml"), resolve=false)
8190
end
8291
# By default, we use Python installed by CondaPkg.

test/context.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@testitem "libstdc++ version" begin
2+
cxxversion = PythonCall.get_libstdcxx_version_bound()
3+
4+
if VERSION <= v"1.6.2"
5+
@test cxx_version == ">=3.4,<9.2"
6+
else
7+
@test cxx_version == ">=3.4,<11.4"
8+
end
9+
10+
ENV["JULIA_CONDAPKG_LIBSTDCXX_VERSION_BOUND"] = ">=3.4,<=12"
11+
12+
cxxversion = PythonCall.get_libstdcxx_version_bound()
13+
@test cxx_version == ">3.4,<=12"
14+
end

0 commit comments

Comments
 (0)