require 'formula'

class UniversalBrewedPython < Requirement
  satisfy { archs_for_command("python").universal? }

  def message; <<-EOS.undent
    A build of GDB using a brewed Python was requested, but Python is not
    a universal build.

    GDB requires Python to be built as a universal binary or it will fail
    if attempting to debug a 32-bit binary on a 64-bit host.
    EOS
  end
end

class Gdb < Formula
  homepage 'http://www.gnu.org/software/gdb/'
  url 'http://ftpmirror.gnu.org/gdb/gdb-7.6.tar.bz2'
  mirror 'http://ftp.gnu.org/gnu/gdb/gdb-7.6.tar.bz2'
  sha1 'b64095579a20e011beeaa5b264fe23a9606ee40f'

  depends_on 'readline'
  if build.include? 'with-brewed-python'
    depends_on UniversalBrewedPython
  end

  option 'with-brewed-python', 'Use the Homebrew version of Python'

  def install
    args = ["--prefix=#{prefix}",
            "--disable-debug",
            "--disable-dependency-tracking",
            "--with-system-readline"]

    if build.include? 'with-brewed-python'
      args << "--with-python=#{HOMEBREW_PREFIX}"
    else
      args << "--with-python=/usr"
    end

    system "./configure", *args
    system "make"
    system "make install"
  end

  def caveats; <<-EOS.undent
    gdb requires special privileges to access Mach ports.
    You will need to codesign the binary. For instructions, see:

      http://sourceware.org/gdb/wiki/BuildingOnDarwin
    EOS
  end
end

require 'formula'

class Gdb < Formula
  homepage 'http://www.gnu.org/software/gdb/'
  url 'http://ftpmirror.gnu.org/gdb/gdb-7.6.tar.bz2'
  mirror 'http://ftp.gnu.org/gnu/gdb/gdb-7.6.tar.bz2'
  sha1 'b64095579a20e011beeaa5b264fe23a9606ee40f'

  depends_on 'readline'

  def install
    system "./configure", "--prefix=#{prefix}",
                          "--disable-debug",
                          "--disable-dependency-tracking",
                          "--with-python=/usr",
                          "--with-system-readline"
    system "make"
    system "make install"
  end

  def caveats; <<-EOS.undent
    gdb requires special privileges to access Mach ports.
    You will need to codesign the binary. For instructions, see:

      http://sourceware.org/gdb/wiki/BuildingOnDarwin
    EOS
  end
end

--- 
+++ 
@@ -1,4 +1,17 @@
 require 'formula'
+
+class UniversalBrewedPython < Requirement
+  satisfy { archs_for_command("python").universal? }
+
+  def message; <<-EOS.undent
+    A build of GDB using a brewed Python was requested, but Python is not
+    a universal build.
+
+    GDB requires Python to be built as a universal binary or it will fail
+    if attempting to debug a 32-bit binary on a 64-bit host.
+    EOS
+  end
+end
 
 class Gdb < Formula
   homepage 'http://www.gnu.org/software/gdb/'
@@ -7,13 +20,25 @@
   sha1 'b64095579a20e011beeaa5b264fe23a9606ee40f'
 
   depends_on 'readline'
+  if build.include? 'with-brewed-python'
+    depends_on UniversalBrewedPython
+  end
+
+  option 'with-brewed-python', 'Use the Homebrew version of Python'
 
   def install
-    system "./configure", "--prefix=#{prefix}",
-                          "--disable-debug",
-                          "--disable-dependency-tracking",
-                          "--with-python=/usr",
-                          "--with-system-readline"
+    args = ["--prefix=#{prefix}",
+            "--disable-debug",
+            "--disable-dependency-tracking",
+            "--with-system-readline"]
+
+    if build.include? 'with-brewed-python'
+      args << "--with-python=#{HOMEBREW_PREFIX}"
+    else
+      args << "--with-python=/usr"
+    end
+
+    system "./configure", *args
     system "make"
     system "make install"
   end
Support using the homebrewed installation of Python with GDB

Adds a flag, --with-brewed-python, which will build GDB with the
Homebrewed Python installation instead of the system one. This requires
Python to be built with --universal so a check for this has been added
as well.

Closes Homebrew/homebrew#153.

Signed-off-by: Xiyue Deng <4900478725231961caf32165c870e05cea7389d8@gmail.com>
