From 6ea387df9362d84e3f217574e22eb5b5ff82252f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Thu, 9 Aug 2018 08:59:58 +0200 Subject: [PATCH] bpo-34366: fix FreeBSD with both system lib and libuuid present It is entirely possible for a FreeBSD (or another) system to have both system uuid.h + -lc and external uuid/uuid.h + -luuid libraries installed simultaneously. If that is the case, prefer the system library (to avoid unnecessary dependencies) and make sure that the code does not attempt to combine both. --- Modules/_uuidmodule.c | 21 ++++++++++++--------- setup.py | 20 +++++++++++++++----- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/Modules/_uuidmodule.c b/Modules/_uuidmodule.c index 0b7f2a2545d4eb..3d7cfedd8200bb 100644 --- a/Modules/_uuidmodule.c +++ b/Modules/_uuidmodule.c @@ -1,11 +1,12 @@ #define PY_SSIZE_T_CLEAN #include "Python.h" +#ifdef HAVE_UUID_H +#include +#else #ifdef HAVE_UUID_UUID_H #include #endif -#ifdef HAVE_UUID_H -#include #endif @@ -14,12 +15,7 @@ py_uuid_generate_time_safe(PyObject *Py_UNUSED(context), PyObject *Py_UNUSED(ignored)) { uuid_t uuid; -#ifdef HAVE_UUID_GENERATE_TIME_SAFE - int res; - - res = uuid_generate_time_safe(uuid); - return Py_BuildValue("y#i", (const char *) uuid, sizeof(uuid), res); -#elif defined(HAVE_UUID_CREATE) +#ifdef HAVE_UUID_CREATE /* uuid.h variant */ uint32_t status; uuid_create(&uuid, &status); # if defined(HAVE_UUID_ENC_BE) @@ -29,6 +25,11 @@ py_uuid_generate_time_safe(PyObject *Py_UNUSED(context), # else return Py_BuildValue("y#i", (const char *) &uuid, sizeof(uuid), (int) status); # endif +#elif defined(HAVE_UUID_GENERATE_TIME_SAFE) /* uuid/uuid.h variants */ + int res; + + res = uuid_generate_time_safe(uuid); + return Py_BuildValue("y#i", (const char *) uuid, sizeof(uuid), res); #else uuid_generate_time(uuid); return Py_BuildValue("y#O", (const char *) uuid, sizeof(uuid), Py_None); @@ -53,7 +54,9 @@ PyInit__uuid(void) { PyObject *mod; assert(sizeof(uuid_t) == 16); -#ifdef HAVE_UUID_GENERATE_TIME_SAFE +/* we prefer the system uuid.h library, so HAVE_UUID_GENERATE_TIME_SAFE + * does not matter when HAVE_UUID_CREATE is present */ +#if defined(HAVE_UUID_GENERATE_TIME_SAFE) && !defined(HAVE_UUID_CREATE) int has_uuid_generate_time_safe = 1; #else int has_uuid_generate_time_safe = 0; diff --git a/setup.py b/setup.py index 37c5dd58a6d2d7..7c38e6de5e37a4 100644 --- a/setup.py +++ b/setup.py @@ -1614,12 +1614,22 @@ class db_found(Exception): pass missing.append('_tkinter') # Build the _uuid module if possible - uuid_incs = find_file("uuid.h", inc_dirs, ["/usr/include/uuid"]) + # Note: we explicitly need to distinguish uuid.h and uuid/uuid.h + # as those are two libraries that can be installed simultaneously + uuid_incs = find_file("uuid.h", inc_dirs, []) + if uuid_incs is not None: + # this is the built-in system library + uuid_libs = [] + else: + uuid_incs = find_file("uuid/uuid.h", inc_dirs, []) + if uuid_incs is not None: + # this is the external library, so we need -luuid + if self.compiler.find_library_file(lib_dirs, 'uuid'): + uuid_libs = ['uuid'] + else: + uuid_incs = None + if uuid_incs is not None: - if self.compiler.find_library_file(lib_dirs, 'uuid'): - uuid_libs = ['uuid'] - else: - uuid_libs = [] self.extensions.append(Extension('_uuid', ['_uuidmodule.c'], libraries=uuid_libs, include_dirs=uuid_incs))