Skip to content

Commit 2116e7d

Browse files
authored
INTPYTHON-665 Add support for bson2 (#332)
1 parent d77e91c commit 2116e7d

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

.github/workflows/test-python.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,36 @@ jobs:
9494
env:
9595
UV_PYTHON: ${{matrix.python-version}}
9696
run: just test-no-optional
97+
98+
bson2:
99+
name: bson2
100+
runs-on: ubuntu-latest
101+
102+
steps:
103+
- uses: actions/checkout@v4
104+
with:
105+
persist-credentials: false
106+
- uses: actions/setup-python@v5
107+
- name: Install Deps
108+
run: |
109+
python -m pip install rust-just uv
110+
- name: Set up env
111+
run: |
112+
echo "LIBBSON_INSTALL_DIR=$PWD/libbson" >> $GITHUB_ENV
113+
echo "LD_LIBRARY_PATH=$PWD/libbson/lib" >> $GITHUB_ENV
114+
echo "LIBBSON_VERSION=2.0.1" >> $GITHUB_ENV
115+
- name: Install libbson
116+
run: just build-libbson
117+
- name: Ensure imports with no test deps
118+
run: just import-check
119+
- name: Start MongoDB
120+
if: ${{ startsWith(runner.os, 'Linux') }}
121+
uses: supercharge/mongodb-github-action@90004df786821b6308fb02299e5835d0dae05d0d # 1.12.0
122+
with:
123+
mongodb-version: 4.4
124+
mongodb-replica-set: test-rs
125+
- name: Run the tests
126+
run: just test
97127
docs:
98128
runs-on: ubuntu-latest
99129
steps:

bindings/python/CONTRIBUTING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ $ conda install --channel conda-forge libbson pkg-config
186186
The minimum required version is listed in `pymongoarrow/version.py`. If
187187
you try to build with a lower version a `ValueError` will be raised.
188188

189+
Our minimum supported major version is 1.x, and that is what we include in our wheels.
190+
In order to build with `bson2`, you can `export LIBBSON_VERSION=2.<minor>.<patch>` before
191+
running `just build-libbson` or building the library.
192+
189193
## Build
190194

191195
Typically we will use the provided `just` scripts and will not build

bindings/python/setup.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
# Whether to create libarrow symlinks on posix systems.
1919
CREATE_LIBARROW_SYMLINKS = os.environ.get("MONGO_CREATE_LIBARROW_SYMLINKS", "1")
2020

21+
LIBBSON_VERSION = os.environ.get("LIBBSON_VERSION", "1.0.0")
22+
2123
# Set a default value for MACOSX_DEPLOYMENT_TARGET.
2224
os.environ.setdefault("MACOSX_DEPLOYMENT_TARGET", "10.15")
2325

@@ -40,19 +42,20 @@ def get_min_libbson_version():
4042

4143

4244
def append_libbson_flags(module):
43-
pc_path = "libbson-1.0"
45+
BSON_MAJOR_VERSION = int(LIBBSON_VERSION[0])
46+
pc_path = "libbson-1.0" if BSON_MAJOR_VERSION == 1 else "bson2"
4447
install_dir = os.environ.get("LIBBSON_INSTALL_DIR")
4548
if install_dir:
4649
install_dir = os.path.abspath(install_dir)
4750

4851
# Handle the copy-able library file if applicable.
4952
if COPY_LIBBSON:
5053
if platform == "darwin":
51-
lib_file = "libbson-1.0.0.dylib"
54+
lib_file = "libbson-1.0.0.dylib" if BSON_MAJOR_VERSION == 1 else "bson2.dylib"
5255
elif platform == "linux":
53-
lib_file = "libbson-1.0.so.0"
56+
lib_file = "libbson-1.0.so.0" if BSON_MAJOR_VERSION == 1 else "bson2.so.0"
5457
else: # windows
55-
lib_file = "bson-1.0.dll"
58+
lib_file = "bson-1.0.dll" if BSON_MAJOR_VERSION == 1 else "bson2.dll"
5659
lib_dir = "bin" if IS_WIN else "lib*"
5760
lib_dir = glob.glob(os.path.join(install_dir, lib_dir))
5861
if lib_dir:
@@ -81,16 +84,18 @@ def append_libbson_flags(module):
8184
if IS_WIN:
8285
# Note: we replace any forward slashes with backslashes so the path
8386
# can be parsed by bash.
84-
lib_path = os.path.join(lib_dir, "bson-1.0.lib").replace(os.sep, "/")
87+
bson_lib = "bson-1.0.lib" if BSON_MAJOR_VERSION == 1 else "bson2.lib"
88+
lib_path = os.path.join(lib_dir, bson_lib).replace(os.sep, "/")
8589
if os.path.exists(lib_path):
8690
module.extra_link_args = [lib_path]
87-
include_dir = os.path.join(install_dir, "include", "libbson-1.0").replace(
91+
include_path = "libbson-1.0" if BSON_MAJOR_VERSION == 1 else "bson2"
92+
include_dir = os.path.join(install_dir, "include", include_path).replace(
8893
os.sep, "/"
8994
)
9095
module.include_dirs.append(include_dir)
9196
else:
9297
raise ValueError(f"Could not find the compiled libbson in {install_dir}")
93-
pc_path = os.path.join(install_dir, lib_dir, "pkgconfig", "libbson-1.0.pc")
98+
pc_path = os.path.join(install_dir, lib_dir, "pkgconfig", f"{pc_path}.pc")
9499

95100
elif IS_WIN:
96101
raise ValueError("We require a LIBBSON_INSTALL_DIR with a compiled library on Windows")

0 commit comments

Comments
 (0)