From e309c50e7e0027ca642b2c8dadfd42f462fd8d2e Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Mon, 29 Aug 2011 18:55:37 +0000 Subject: [PATCH] Get libdrm to compile on darwin The added patch uses mach_absolute_time() to get a nanosecond-resolution monotonic clock on darwin systems (See http://developer.apple.com/library/mac/#qa/qa1398/_index.html for an understanding of the added code). In addition, the patch changes one use of clock_gettime to gettimeofday since only resolution of a second is needed at that point. This code compiles on darwin, but the resulting library is not yet tested. svn path=/nixpkgs/trunk/; revision=28879 --- pkgs/development/libraries/libdrm/default.nix | 9 +- .../libraries/libdrm/libdrm-apple.patch | 88 +++++++++++++++++++ pkgs/top-level/all-packages.nix | 4 +- 3 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 pkgs/development/libraries/libdrm/libdrm-apple.patch diff --git a/pkgs/development/libraries/libdrm/default.nix b/pkgs/development/libraries/libdrm/default.nix index ad6f4536604..d37b7dd0b7f 100644 --- a/pkgs/development/libraries/libdrm/default.nix +++ b/pkgs/development/libraries/libdrm/default.nix @@ -1,6 +1,6 @@ {stdenv, fetchurl, pkgconfig, libpthreadstubs}: -stdenv.mkDerivation rec { +stdenv.mkDerivation (rec { name = "libdrm-2.4.24"; src = fetchurl { @@ -10,16 +10,19 @@ stdenv.mkDerivation rec { buildInputs = [ pkgconfig libpthreadstubs ]; + patches = [ ./libdrm-apple.patch ]; + preConfigure = '' # General case: non intel. if test -n "$crossConfig"; then configureFlags="$configureFlags --disable-intel"; fi - ''; + '' + stdenv.lib.optionalString stdenv.isDarwin + "echo : \\\${ac_cv_func_clock_gettime=\'yes\'} > config.cache"; meta = { homepage = http://dri.freedesktop.org/libdrm/; description = "Library for accessing the kernel's Direct Rendering Manager"; license = "bsd"; }; -} +} // (stdenv.lib.optionalAttrs stdenv.isDarwin { configureFlags = [ "-C" ]; })) diff --git a/pkgs/development/libraries/libdrm/libdrm-apple.patch b/pkgs/development/libraries/libdrm/libdrm-apple.patch new file mode 100644 index 00000000000..e9fd63c5fae --- /dev/null +++ b/pkgs/development/libraries/libdrm/libdrm-apple.patch @@ -0,0 +1,88 @@ +diff -Naur libdrm-2.4.26-orig/intel/intel_bufmgr_gem.c libdrm-2.4.26/intel/intel_bufmgr_gem.c +--- libdrm-2.4.26-orig/intel/intel_bufmgr_gem.c 2011-04-01 10:30:51.000000000 -0400 ++++ libdrm-2.4.26/intel/intel_bufmgr_gem.c 2011-08-29 02:17:20.000000000 -0400 +@@ -51,6 +51,7 @@ + #include + #include + #include ++#include + + #include "errno.h" + #include "libdrm_lists.h" +@@ -987,9 +988,9 @@ + if (atomic_dec_and_test(&bo_gem->refcount)) { + drm_intel_bufmgr_gem *bufmgr_gem = + (drm_intel_bufmgr_gem *) bo->bufmgr; +- struct timespec time; ++ struct timeval time; + +- clock_gettime(CLOCK_MONOTONIC, &time); ++ gettimeofday(&time, NULL); + + pthread_mutex_lock(&bufmgr_gem->lock); + drm_intel_gem_bo_unreference_final(bo, time.tv_sec); +diff -Naur libdrm-2.4.26-orig/xf86drm.c libdrm-2.4.26/xf86drm.c +--- libdrm-2.4.26-orig/xf86drm.c 2011-03-21 09:39:24.000000000 -0400 ++++ libdrm-2.4.26/xf86drm.c 2011-08-29 02:17:49.000000000 -0400 +@@ -51,6 +51,9 @@ + #include + #include + #include ++#if defined(__APPLE__) && defined(__MACH__) ++#include ++#endif + + /* Not all systems have MAP_FAILED defined */ + #ifndef MAP_FAILED +@@ -1941,20 +1944,43 @@ + */ + int drmWaitVBlank(int fd, drmVBlankPtr vbl) + { ++#if defined(__APPLE__) && defined(__MACH__) ++ uint64_t start, end, elapsed, elapsedNano; ++ static const uint64_t maxElapsed = 2000000000; ++ static mach_timebase_info_data_t timebaseInfo; ++ if ( timebaseInfo.denom == 0 ) { ++ (void) mach_timebase_info(&timebaseInfo); ++ } ++#else + struct timespec timeout, cur; ++#endif + int ret; + ++#if defined(__APPLE__) && defined(__MACH__) ++ start = mach_absolute_time(); ++#else + ret = clock_gettime(CLOCK_MONOTONIC, &timeout); + if (ret < 0) { + fprintf(stderr, "clock_gettime failed: %s\n", strerror(ret)); + goto out; + } + timeout.tv_sec++; ++#endif + + do { + ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl); + vbl->request.type &= ~DRM_VBLANK_RELATIVE; + if (ret && errno == EINTR) { ++#if defined(__APPLE__) && defined(__MACH__) ++ end = mach_absolute_time(); ++ elapsed = end - start; ++ elapsedNano = elapsed * timebaseInfo.numer / timebaseInfo.denom; ++ if (elapsedNano > maxElapsed) { ++ errno = EBUSY; ++ ret = -1; ++ break; ++ } ++#else + clock_gettime(CLOCK_MONOTONIC, &cur); + /* Timeout after 1s */ + if (cur.tv_sec > timeout.tv_sec + 1 || +@@ -1964,6 +1990,7 @@ + ret = -1; + break; + } ++#endif + } + } while (ret && errno == EINTR); + diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 9b752308bd6..06c94958369 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -3724,10 +3724,10 @@ let libdmtx = callPackage ../development/libraries/libdmtx { }; - libdrm = if stdenv.isDarwin then null else (callPackage ../development/libraries/libdrm { + libdrm = callPackage ../development/libraries/libdrm { inherit fetchurl stdenv pkgconfig; inherit (xorg) libpthreadstubs; - }); + }; libdv = callPackage ../development/libraries/libdv { };