From 1cd0875860135caa05a0cb49672cc493560a7a1e Mon Sep 17 00:00:00 2001 From: nazarevsky Date: Wed, 3 Dec 2025 15:14:49 +0100 Subject: [PATCH] sdk: Fix gl-sdk bindings generation for multiple OS --- libs/gl-client-py/Makefile | 4 +-- libs/gl-sdk/.tasks.yml | 29 +++++++++++++++++---- libs/gl-sdk/hooks/libglsdk_force_include.py | 23 ++++++++++++++++ libs/gl-sdk/pyproject.toml | 4 +-- 4 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 libs/gl-sdk/hooks/libglsdk_force_include.py diff --git a/libs/gl-client-py/Makefile b/libs/gl-client-py/Makefile index b0ebada24..63cf91168 100644 --- a/libs/gl-client-py/Makefile +++ b/libs/gl-client-py/Makefile @@ -39,8 +39,8 @@ pygrpc: ${PROTOSRC} cd ${PYDIR}; uv run python -m grpc_tools.protoc ${PYPROTOC_OPTS} glclient/greenlight.proto check-py: - #uv run --all-packages mypy ${PYDIR}/glclient - uv run --all-packages pytest tests -n $(shell nproc) ${PYDIR}/tests + #uv run --reinstall-package='gl-sdk' --all-packages mypy ${PYDIR}/glclient + uv run --reinstall-package='gl-sdk' --all-packages pytest tests -n $(shell nproc) ${PYDIR}/tests clean-py: rm -f ${PYPROTOS} ${PYDIR}/build ${PYDIR}/dist diff --git a/libs/gl-sdk/.tasks.yml b/libs/gl-sdk/.tasks.yml index 71a8e053b..2f9957ed9 100644 --- a/libs/gl-sdk/.tasks.yml +++ b/libs/gl-sdk/.tasks.yml @@ -1,5 +1,20 @@ version: "3" +vars: + LIB_EXT: + sh: | + case "$(uname -s)" in + Darwin*) + echo "dylib" + ;; + MINGW*|MSYS*|CYGWIN*) + echo "dll" + ;; + *) + echo "so" + ;; + esac + tasks: build: desc: "Build the gl-sdk library" @@ -20,8 +35,9 @@ tasks: - build-release cmds: - | + cp ${CARGO_TARGET_DIR:-target}/release/libglsdk.{{.LIB_EXT}} ./libs/gl-sdk/glsdk/libglsdk.{{.LIB_EXT}}; cargo run --bin uniffi-bindgen -- generate \ - --library ${CARGO_TARGET_DIR:-target}/release/libglsdk.so \ + --library ${CARGO_TARGET_DIR:-target}/release/libglsdk.{{.LIB_EXT}} \ --language python \ --out-dir ./libs/gl-sdk/bindings @@ -32,8 +48,9 @@ tasks: - build-release cmds: - | + cp ${CARGO_TARGET_DIR:-target}/release/libglsdk.{{.LIB_EXT}} ./libs/gl-sdk/glsdk/libglsdk.{{.LIB_EXT}}; cargo run --bin uniffi-bindgen -- generate \ - --library ${CARGO_TARGET_DIR:-target}/release/libglsdk.so \ + --library ${CARGO_TARGET_DIR:-target}/release/libglsdk.{{.LIB_EXT}} \ --language kotlin \ --out-dir ./libs/gl-sdk/bindings @@ -44,8 +61,9 @@ tasks: - build-release cmds: - | + cp ${CARGO_TARGET_DIR:-target}/release/libglsdk.{{.LIB_EXT}} ./libs/gl-sdk/glsdk/libglsdk.{{.LIB_EXT}}; cargo run --bin uniffi-bindgen -- generate \ - --library ${CARGO_TARGET_DIR:-target}/release/libglsdk.so \ + --library ${CARGO_TARGET_DIR:-target}/release/libglsdk.{{.LIB_EXT}} \ --language swift \ --out-dir ./libs/gl-sdk/bindings @@ -56,8 +74,9 @@ tasks: - build-release cmds: - | + cp ${CARGO_TARGET_DIR:-target}/release/libglsdk.{{.LIB_EXT}} ./libs/gl-sdk/glsdk/libglsdk.{{.LIB_EXT}}; cargo run --bin uniffi-bindgen -- generate \ - --library ${CARGO_TARGET_DIR:-target}/release/libglsdk.so \ + --library ${CARGO_TARGET_DIR:-target}/release/libglsdk.{{.LIB_EXT}} \ --language ruby \ --out-dir ./libs/gl-sdk/bindings @@ -106,6 +125,6 @@ tasks: cmds: - rm -rf ./bindings - rm -rf dist/ build/ *.egg-info - - rm -f glsdk/*.so glsdk/glsdk.py + - rm -f glsdk/*.{{.LIB_EXT}} glsdk/glsdk.py - find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true - find . -type f -name "*.pyc" -delete diff --git a/libs/gl-sdk/hooks/libglsdk_force_include.py b/libs/gl-sdk/hooks/libglsdk_force_include.py new file mode 100644 index 000000000..15314d8c6 --- /dev/null +++ b/libs/gl-sdk/hooks/libglsdk_force_include.py @@ -0,0 +1,23 @@ +from hatchling.builders.hooks.plugin.interface import BuildHookInterface +import platform + +class CustomBuildHook(BuildHookInterface): + def initialize(self, version, build_data): + """ + Sets force-include option for glsdk/libglsdk shared lib. + + Does the same as [tool.hatch.build.targets.wheel.force-include], however + assumes system OS and based on that sets the correct extension. + """ + system = platform.system() + + match system: + case "Darwin": + lib_ext = ".dylib" + case "Windows": + lib_ext = ".dll" + case _: + lib_ext = ".so" + + shared_file = f"glsdk/libglsdk{lib_ext}" + build_data['force_include'][shared_file] = shared_file \ No newline at end of file diff --git a/libs/gl-sdk/pyproject.toml b/libs/gl-sdk/pyproject.toml index 8ec58e643..76e4327ad 100644 --- a/libs/gl-sdk/pyproject.toml +++ b/libs/gl-sdk/pyproject.toml @@ -17,8 +17,8 @@ build-backend = "hatchling.build" packages = ["glsdk"] # Include the shared library as package data -[tool.hatch.build.targets.wheel.force-include] -"glsdk/libglsdk.so" = "glsdk/libglsdk.so" +[tool.hatch.build.hooks.custom] +path = "hooks/libglsdk_force_include.py" [dependency-groups] dev = [