2012-03-07 17:29:05 -05:00
|
|
|
require 'formula'
|
2012-03-07 21:30:03 -05:00
|
|
|
require 'bottles'
|
2012-03-07 17:29:05 -05:00
|
|
|
require 'tab'
|
2013-03-11 18:56:26 +00:00
|
|
|
require 'keg'
|
2013-09-21 15:16:45 +01:00
|
|
|
require 'cmd/versions'
|
2013-09-21 21:30:57 +01:00
|
|
|
require 'utils/inreplace'
|
2013-09-21 21:21:42 +01:00
|
|
|
require 'erb'
|
2013-10-31 01:20:28 -07:00
|
|
|
require 'open3'
|
|
|
|
require 'extend/pathname'
|
2012-03-07 17:29:05 -05:00
|
|
|
|
2013-06-08 16:48:43 +01:00
|
|
|
class BottleMerger < Formula
|
|
|
|
# This provides a URL and Version which are the only needed properties of
|
|
|
|
# a Formula. This object is used to access the Formula bottle DSL to merge
|
|
|
|
# multiple outputs of `brew bottle`.
|
|
|
|
url '1'
|
2013-09-21 15:16:16 +01:00
|
|
|
def self.reset_bottle; @bottle = Bottle.new; end
|
2013-06-08 16:48:43 +01:00
|
|
|
end
|
|
|
|
|
2013-09-21 21:21:42 +01:00
|
|
|
BOTTLE_ERB = <<-EOS
|
|
|
|
bottle do
|
|
|
|
<% if prefix.to_s != '/usr/local' %>
|
|
|
|
prefix '<%= prefix %>'
|
|
|
|
<% end %>
|
|
|
|
<% if cellar.is_a? Symbol %>
|
|
|
|
cellar :<%= cellar %>
|
2013-09-23 17:30:33 +01:00
|
|
|
<% elsif cellar.to_s != '/usr/local/Cellar' %>
|
2013-09-21 21:21:42 +01:00
|
|
|
cellar '<%= cellar %>'
|
|
|
|
<% end %>
|
|
|
|
<% if revision > 0 %>
|
|
|
|
revision <%= revision %>
|
|
|
|
<% end %>
|
2013-09-23 17:30:47 +01:00
|
|
|
<% checksums.each do |checksum_type, checksum_values| %>
|
|
|
|
<% checksum_values.each do |checksum_value| %>
|
|
|
|
<% checksum, osx = checksum_value.shift %>
|
2013-09-21 21:21:42 +01:00
|
|
|
<%= checksum_type %> '<%= checksum %>' => :<%= osx %>
|
|
|
|
<% end %>
|
2013-09-23 17:30:47 +01:00
|
|
|
<% end %>
|
2013-09-21 21:21:42 +01:00
|
|
|
end
|
|
|
|
EOS
|
|
|
|
|
2012-03-07 17:29:05 -05:00
|
|
|
module Homebrew extend self
|
2013-09-21 21:30:57 +01:00
|
|
|
class << self
|
|
|
|
include Utils::Inreplace
|
|
|
|
end
|
|
|
|
|
2013-03-11 18:56:26 +00:00
|
|
|
def keg_contains string, keg
|
2013-10-31 01:20:28 -07:00
|
|
|
if not ARGV.homebrew_developer?
|
|
|
|
return quiet_system 'fgrep', '--recursive', '--quiet', '--max-count=1', string, keg
|
|
|
|
end
|
|
|
|
|
|
|
|
# Find all files that still reference the keg via a string search
|
|
|
|
keg_ref_files = `/usr/bin/fgrep --files-with-matches --recursive "#{string}" "#{keg}" 2>/dev/null`
|
|
|
|
keg_ref_files = (keg_ref_files.map{ |file| Pathname.new(file.strip) }).reject(&:symlink?)
|
|
|
|
|
|
|
|
# If there are no files with that string found, return immediately
|
|
|
|
return false if keg_ref_files.empty?
|
|
|
|
|
|
|
|
# Start printing out each file and any extra information we can find
|
|
|
|
opoo "String '#{string}' still exists in these files:"
|
|
|
|
keg_ref_files.each do |file|
|
|
|
|
puts "#{Tty.red}#{file}#{Tty.reset}"
|
|
|
|
|
|
|
|
# If we can't use otool on this file, just skip to the next file
|
|
|
|
next if not file.mach_o_executable? and not file.mach_o_bundle? and not file.dylib? and not file.extname == '.a'
|
|
|
|
|
|
|
|
# Get all libraries this file links to, then display only links to libraries that contain string in the path
|
|
|
|
linked_libraries = `otool -L "#{file}"`.split("\n").drop(1)
|
|
|
|
linked_libraries.map!{ |lib| lib.strip.split()[0] }
|
|
|
|
linked_libraries = linked_libraries.select{ |lib| lib.include? string }
|
|
|
|
|
|
|
|
linked_libraries.each do |lib|
|
|
|
|
puts " #{Tty.gray}-->#{Tty.reset} links to #{lib}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Use strings to search through the file for each string
|
|
|
|
strings = `strings -t x - "#{file}"`.select{ |str| str.include? string }.map{ |s| s.strip }
|
|
|
|
|
|
|
|
# Don't bother reporting a string if it was found by otool
|
|
|
|
strings.reject!{ |str| linked_libraries.include? str.split[1] }
|
|
|
|
strings.each do |str|
|
|
|
|
offset, match = str.split
|
|
|
|
puts " #{Tty.gray}-->#{Tty.reset} match '#{match}' at offset #{Tty.em}0x#{offset}#{Tty.reset}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
puts
|
|
|
|
true
|
2013-03-11 18:56:26 +00:00
|
|
|
end
|
|
|
|
|
2013-06-04 20:39:09 +01:00
|
|
|
def bottle_output bottle
|
2013-09-21 21:21:42 +01:00
|
|
|
erb = ERB.new BOTTLE_ERB
|
|
|
|
erb.result(bottle.instance_eval { binding }).gsub(/^\s*$\n/, '')
|
2013-06-04 20:39:09 +01:00
|
|
|
end
|
|
|
|
|
2012-03-07 17:29:05 -05:00
|
|
|
def bottle_formula f
|
2012-03-15 10:57:34 +13:00
|
|
|
unless f.installed?
|
2012-04-30 14:08:59 +10:00
|
|
|
return ofail "Formula not installed: #{f.name}"
|
2012-03-15 10:57:34 +13:00
|
|
|
end
|
|
|
|
|
2012-08-25 11:31:57 -07:00
|
|
|
unless built_as_bottle? f
|
2012-04-30 14:08:59 +10:00
|
|
|
return ofail "Formula not installed with '--build-bottle': #{f.name}"
|
2012-03-15 10:57:34 +13:00
|
|
|
end
|
2012-03-07 17:29:05 -05:00
|
|
|
|
2013-09-21 15:16:45 +01:00
|
|
|
master_bottle_filenames = f.bottle_filenames 'origin/master'
|
|
|
|
bottle_revision = -1
|
|
|
|
begin
|
|
|
|
bottle_revision += 1
|
|
|
|
filename = bottle_filename(f, bottle_revision)
|
2013-09-23 18:23:23 +01:00
|
|
|
end while not ARGV.include? '--no-revision' \
|
|
|
|
and master_bottle_filenames.include? filename
|
2013-07-04 11:23:09 +01:00
|
|
|
|
|
|
|
if bottle_filename_formula_name(filename).empty?
|
2013-08-04 08:25:51 -07:00
|
|
|
return ofail "Add a new regex to bottle_version.rb to parse the bottle filename."
|
2013-07-04 11:23:09 +01:00
|
|
|
end
|
|
|
|
|
2013-02-09 19:06:54 -08:00
|
|
|
bottle_path = Pathname.pwd/filename
|
|
|
|
sha1 = nil
|
2012-03-07 17:29:05 -05:00
|
|
|
|
2013-03-11 18:56:26 +00:00
|
|
|
prefix = HOMEBREW_PREFIX.to_s
|
|
|
|
tmp_prefix = '/tmp'
|
|
|
|
cellar = HOMEBREW_CELLAR.to_s
|
|
|
|
tmp_cellar = '/tmp/Cellar'
|
|
|
|
|
2013-09-21 21:21:42 +01:00
|
|
|
output = nil
|
|
|
|
|
2012-03-07 17:29:05 -05:00
|
|
|
HOMEBREW_CELLAR.cd do
|
2013-09-21 15:15:50 +01:00
|
|
|
ohai "Bottling #{filename}..."
|
2012-03-07 17:29:05 -05:00
|
|
|
# Use gzip, faster to compress than bzip2, faster to uncompress than bzip2
|
|
|
|
# or an uncompressed tarball (and more bandwidth friendly).
|
2013-02-09 19:06:54 -08:00
|
|
|
safe_system 'tar', 'czf', bottle_path, "#{f.name}/#{f.version}"
|
|
|
|
sha1 = bottle_path.sha1
|
2013-03-11 18:56:26 +00:00
|
|
|
relocatable = false
|
|
|
|
|
2013-09-21 21:22:12 +01:00
|
|
|
if File.size?(bottle_path) > 1*1024*1024
|
|
|
|
ohai "Detecting if #{filename} is relocatable..."
|
|
|
|
end
|
2013-03-11 18:56:26 +00:00
|
|
|
keg = Keg.new f.prefix
|
|
|
|
keg.lock do
|
|
|
|
# Relocate bottle library references before testing for built-in
|
|
|
|
# references to the Cellar e.g. Qt's QMake annoyingly does this.
|
2013-10-02 22:12:13 -07:00
|
|
|
keg.relocate_install_names prefix, tmp_prefix, cellar, tmp_cellar, :keg_only => f.keg_only?
|
2013-03-11 18:56:26 +00:00
|
|
|
|
2013-09-10 21:56:39 +01:00
|
|
|
if prefix == '/usr/local'
|
|
|
|
prefix_check = HOMEBREW_PREFIX/'opt'
|
|
|
|
else
|
|
|
|
prefix_check = HOMEBREW_PREFIX
|
|
|
|
end
|
|
|
|
|
|
|
|
relocatable = !keg_contains(prefix_check, keg)
|
2013-03-11 18:56:26 +00:00
|
|
|
relocatable = !keg_contains(HOMEBREW_CELLAR, keg) if relocatable
|
|
|
|
|
|
|
|
# And do the same thing in reverse to change the library references
|
|
|
|
# back to how they were.
|
|
|
|
keg.relocate_install_names tmp_prefix, prefix, tmp_cellar, cellar
|
|
|
|
end
|
|
|
|
|
2013-06-04 20:39:09 +01:00
|
|
|
bottle = Bottle.new
|
|
|
|
bottle.prefix HOMEBREW_PREFIX
|
|
|
|
bottle.cellar relocatable ? :any : HOMEBREW_CELLAR
|
|
|
|
bottle.revision bottle_revision
|
|
|
|
bottle.sha1 sha1 => bottle_tag
|
2013-03-11 18:56:26 +00:00
|
|
|
|
2012-03-07 17:29:05 -05:00
|
|
|
puts "./#{filename}"
|
2013-09-21 21:21:42 +01:00
|
|
|
output = bottle_output bottle
|
|
|
|
puts output
|
2012-03-07 17:29:05 -05:00
|
|
|
end
|
2013-09-21 21:24:50 +01:00
|
|
|
|
|
|
|
if ARGV.include? '--rb'
|
|
|
|
bottle_base = filename.gsub(bottle_suffix(bottle_revision), '')
|
|
|
|
File.open "#{bottle_base}.bottle.rb", 'w' do |file|
|
|
|
|
file.write output
|
|
|
|
end
|
|
|
|
end
|
2012-03-07 17:29:05 -05:00
|
|
|
end
|
|
|
|
|
2013-09-21 15:16:16 +01:00
|
|
|
def merge
|
|
|
|
merge_hash = {}
|
|
|
|
ARGV.named.each do |argument|
|
|
|
|
formula_name = bottle_filename_formula_name argument
|
|
|
|
merge_hash[formula_name] ||= []
|
|
|
|
bottle_block = IO.read argument
|
|
|
|
merge_hash[formula_name] << bottle_block
|
|
|
|
end
|
|
|
|
merge_hash.keys.each do |formula_name|
|
|
|
|
BottleMerger.reset_bottle
|
|
|
|
ohai formula_name
|
|
|
|
bottle_blocks = merge_hash[formula_name]
|
|
|
|
bottle_blocks.each do |bottle_block|
|
2013-06-08 16:48:43 +01:00
|
|
|
BottleMerger.class_eval bottle_block
|
|
|
|
end
|
|
|
|
bottle = BottleMerger.new.bottle
|
2013-09-21 21:30:57 +01:00
|
|
|
next unless bottle
|
|
|
|
output = bottle_output bottle
|
|
|
|
puts output
|
|
|
|
|
|
|
|
if ARGV.include? '--write'
|
|
|
|
f = Formula.factory formula_name
|
2013-10-31 18:21:03 -07:00
|
|
|
formula_relative_path = "Library/Formula/#{f.name}.rb"
|
|
|
|
if File.exists? formula_relative_path
|
|
|
|
formula_path = Pathname.new(formula_relative_path)
|
|
|
|
else
|
|
|
|
formula_path = HOMEBREW_REPOSITORY+formula_relative_path
|
|
|
|
end
|
2013-10-31 19:03:23 -07:00
|
|
|
has_bottle_block = f.class.send(:bottle).checksums.any?
|
2013-09-21 21:30:57 +01:00
|
|
|
inreplace formula_path do |s|
|
2013-10-31 19:03:23 -07:00
|
|
|
if has_bottle_block
|
|
|
|
s.sub!(/ bottle do.+?end\n/m, output)
|
2013-09-21 21:30:57 +01:00
|
|
|
else
|
2013-10-31 19:03:23 -07:00
|
|
|
s.sub!(/( (url|sha1|head|version) '\S*'\n+)+/m, '\0' + output + "\n")
|
2013-09-21 21:30:57 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-31 19:03:23 -07:00
|
|
|
update_or_add = has_bottle_block ? 'update' : 'add'
|
2013-09-21 21:30:57 +01:00
|
|
|
|
|
|
|
safe_system 'git', 'commit', formula_path, '-m',
|
|
|
|
"#{f.name}: #{update_or_add} bottle."
|
|
|
|
end
|
2013-06-08 16:48:43 +01:00
|
|
|
end
|
2013-09-21 15:16:16 +01:00
|
|
|
exit 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def bottle
|
|
|
|
merge if ARGV.include? '--merge'
|
2013-06-08 16:48:43 +01:00
|
|
|
|
|
|
|
ARGV.formulae.each do |f|
|
2013-06-23 13:02:02 -07:00
|
|
|
bottle_formula f
|
2012-03-07 17:29:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|