aboutsummaryrefslogtreecommitdiffstats
path: root/Library
diff options
context:
space:
mode:
authorAdam Vandenberg2009-09-08 15:31:28 -0700
committerMax Howell2009-09-10 18:17:15 +0100
commit981dba1b35f5d570830644d64339080b24023aaa (patch)
treedb40ee072431ae9bcd7fc733e6f4469b47da6222 /Library
parentc3169b56001db03171992d205b86951ea574f0e5 (diff)
downloadbrew-981dba1b35f5d570830644d64339080b24023aaa.tar.bz2
Function to return a binary's Mach-O architectures
Added a utility method to get an array of architecture names for a given executable. This will be useful for, say, figuring out what Python was compiled for, to know what to compile a C-based module as. Signed Off By: Max Howell <max@methylblue.com> I added a test and made the function use `which` if the path provided is not absolute. I considered allowing relative paths, but then it is possible for the function to take eg. the svn binary from the current directory when you meant the one in the path, and that could be a confusing bug.
Diffstat (limited to 'Library')
-rwxr-xr-xLibrary/Homebrew/unittest.rb9
-rw-r--r--Library/Homebrew/utils.rb21
2 files changed, 30 insertions, 0 deletions
diff --git a/Library/Homebrew/unittest.rb b/Library/Homebrew/unittest.rb
index ba93e33ec..ae8b0c1bc 100755
--- a/Library/Homebrew/unittest.rb
+++ b/Library/Homebrew/unittest.rb
@@ -415,6 +415,15 @@ class BeerTasting <Test::Unit::TestCase
assert_equal 10.7, f+0.1
end
+ def test_arch_for_command
+ # NOTE only works on Snow Leopard I bet, pick a better file!
+ arches=arch_for_command '/usr/bin/svn'
+ assert_equal 3, arches.count
+ assert arches.include?(:x86_64)
+ assert arches.include?(:i386)
+ assert arches.include?(:ppc7400)
+ end
+
def test_pathname_plus_yeast
nostdout do
assert_nothing_raised do
diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb
index 7beaba8ad..ff58aabfd 100644
--- a/Library/Homebrew/utils.rb
+++ b/Library/Homebrew/utils.rb
@@ -99,3 +99,24 @@ def exec_editor *args
# we don't have to escape args, and escaping 100% is tricky
exec *(editor.split+args)
end
+
+# provide an absolute path to a command or this function will search the PATH
+def arch_for_command cmd
+ archs = []
+ cmd = `which #{cmd}` if not Pathname.new(cmd).absolute?
+
+ IO.popen("file #{cmd}").readlines.each do |line|
+ case line
+ when /Mach-O executable ppc/
+ archs << :ppc7400
+ when /Mach-O 64-bit executable ppc64/
+ archs << :ppc64
+ when /Mach-O executable i386/
+ archs << :i386
+ when /Mach-O 64-bit executable x86_64/
+ archs << :x86_64
+ end
+ end
+
+ return archs
+end