From fbae6a31ce55db84eeed2ef8f2c88e62c66ba3fe Mon Sep 17 00:00:00 2001 From: Ben Beasley Date: Thu, 17 Feb 2022 14:38:09 -0500 Subject: [PATCH] Support dynamically linking against system double-conversion library (#508) Add env vars to build with system double-conversion. Fixes #376 and is useful to Linux distribution packagers. New environment variables UJSON_BUILD_DC_INCLUDES and UJSON_BUILD_DC_LIBS allow overriding the include path for double-conversion and adding linker flags for an external double-conversion library. They should generally be used together. --- README.md | 36 ++++++++++++++++++++++++++++++++++++ setup.py | 20 ++++++++++++++++---- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 11e29ae..b33ae69 100644 --- a/README.md +++ b/README.md @@ -124,3 +124,39 @@ Linux 5.0.0-1032-azure x86_64 #34-Ubuntu SMP Mon Feb 10 19:37:25 UTC 2020 | Complex object | | | | | | | encode | 533 | 582 | | 408 | 431 | | decode | 466 | 454 | | 154 | 164 | + +## Build options + +For those with particular needs, such as Linux distribution packagers, several +build options are provided in the form of environment variables. + +### Debugging symbols + +#### UJSON_BUILD_NO_STRIP + +By default, debugging symbols are stripped on Linux platforms. Setting this +environment variable with a value of `1` or `True` disables this behavior. + +### Using an external or system copy of the double-conversion library + +These two environment variables are typically used together, something like: + +```sh +export UJSON_BUILD_DC_INCLUDES='/usr/include/double-conversion' +export UJSON_BUILD_DC_LIBS='-ldouble-conversion' +``` + +Users planning to link against an external shared library should be aware of +the ABI-compatibility requirements this introduces when upgrading system +libraries or copying compiled wheels to other machines. + +#### UJSON_BUILD_DC_INCLUDES + +One or more directories, delimited by `os.pathsep` (same as the `PATH` +environment variable), in which to look for `double-conversion` header files; +the default is to use the bundled copy. + +#### UJSON_BUILD_DC_LIBS + +Compiler flags needed to link the `double-conversion` library; the default +is to use the bundled copy. diff --git a/setup.py b/setup.py index 3a6c581..b98963b 100644 --- a/setup.py +++ b/setup.py @@ -1,10 +1,22 @@ import platform +import shlex from glob import glob -from os import environ +from os import environ, pathsep from setuptools import Extension, setup -dconv_source_files = glob("./deps/double-conversion/double-conversion/*.cc") +dconv_includes = [ + dir + for dir in environ.get( + "UJSON_BUILD_DC_INCLUDES", + "./deps/double-conversion/double-conversion", + ).split(pathsep) + if dir +] +dconv_libs = shlex.split(environ.get("UJSON_BUILD_DC_LIBS", "")) +dconv_source_files = [] +if not dconv_libs: + dconv_source_files.extend(glob("./deps/double-conversion/double-conversion/*.cc")) dconv_source_files.append("./lib/dconv_wrapper.cc") if platform.system() == "Linux" and environ.get("UJSON_BUILD_NO_STRIP", "0") not in ( @@ -25,9 +37,9 @@ module1 = Extension( "./lib/ultrajsonenc.c", "./lib/ultrajsondec.c", ], - include_dirs=["./python", "./lib", "./deps/double-conversion/double-conversion"], + include_dirs=["./python", "./lib"] + dconv_includes, extra_compile_args=["-D_GNU_SOURCE"], - extra_link_args=["-lstdc++", "-lm"] + strip_flags, + extra_link_args=["-lstdc++", "-lm"] + dconv_libs + strip_flags, ) with open("python/version_template.h") as f: