Class: Metabuild::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/metabuild.rb

Overview

A Target is really at the heart of a build. It handles dependencies, caching …

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, repo, depends = [], results = [], tags = []) ⇒ Target

Returns a new instance of Target.



726
727
728
729
730
# File 'lib/metabuild.rb', line 726

def initialize(name, repo, depends = [], results = [], tags = [])
  @name, @repo, @tags, @results, @depends  = name, repo, tags, results, depends
  @done = false
  @block = lambda { raise "Target #{@name} has empty code block !" }
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



724
725
726
# File 'lib/metabuild.rb', line 724

def block
  @block
end

#dependsObject

Returns the value of attribute depends.



724
725
726
# File 'lib/metabuild.rb', line 724

def depends
  @depends
end

#doneObject

Returns the value of attribute done.



724
725
726
# File 'lib/metabuild.rb', line 724

def done
  @done
end

#nameObject

Returns the value of attribute name.



724
725
726
# File 'lib/metabuild.rb', line 724

def name
  @name
end

#repoObject

Returns the value of attribute repo.



724
725
726
# File 'lib/metabuild.rb', line 724

def repo
  @repo
end

#resultsObject

Returns the value of attribute results.



724
725
726
# File 'lib/metabuild.rb', line 724

def results
  @results
end

#tagsObject

Returns the value of attribute tags.



724
725
726
# File 'lib/metabuild.rb', line 724

def tags
  @tags
end

Instance Method Details

#add_dep(dep) ⇒ Object



740
741
742
743
744
745
746
# File 'lib/metabuild.rb', line 740

def add_dep dep
  if dep.kind_of? Array
    @depends = @depends | dep
  else
    @depends.push dep
  end
end

#add_result(res) ⇒ Object



736
737
738
# File 'lib/metabuild.rb', line 736

def add_result res
  @results.push res
end

#check_cacheObject



772
773
774
# File 'lib/metabuild.rb', line 772

def check_cache
  File.exist?(CACHE + cksum + ".tgz")
end

#cksumObject

The checksum is a mix of the repository checksum + the default tags + local tags Default tags include processor type, kernel name, etc.



766
767
768
769
770
# File 'lib/metabuild.rb', line 766

def cksum
  DEFAULT_TAGS.concat(@tags).inject(@repo.cksum) { |sum, tag|
    Digest::SHA1.hexdigest(tag.call).to_i(16) ^ sum
  }.to_s(16)
end

#each_depObject



752
753
754
755
756
# File 'lib/metabuild.rb', line 752

def each_dep
  @depends.each do |d|
    yield d
  end
end

#each_resultObject



758
759
760
761
762
# File 'lib/metabuild.rb', line 758

def each_result
  @results.each do |r|
    yield r
  end
end

#get_cacheObject



776
777
778
779
# File 'lib/metabuild.rb', line 776

def get_cache
  cmd = "tar zxf " + CACHE + "/" + cksum + ".tgz"
  raise "Error getting target #{@name} in cache" unless  system(cmd)
end

#output_graphObject



846
847
848
849
850
# File 'lib/metabuild.rb', line 846

def output_graph
  each_dep do |d|
    puts "#{@name} -> #{d.name};"
  end
end

#propagate_disableObject



831
832
833
834
835
836
# File 'lib/metabuild.rb', line 831

def propagate_disable
  each_dep do |d|
    d.done = true
    d.propagate_disable
  end
end

#propagate_restart_at(restart_at) ⇒ Object



838
839
840
841
842
843
844
# File 'lib/metabuild.rb', line 838

def propagate_restart_at(restart_at)
    each_dep do |d|
        return true if d.propagate_restart_at restart_at
        d.done = true 
    end
    return @name == restart_at
end

#propagate_run(use_cache = false, parallel = false) ⇒ Object

Run according to dependency list, from bottom to top



809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
# File 'lib/metabuild.rb', line 809

def propagate_run(use_cache=false, parallel=false)
  success = true
  if parallel
    each_dep do |d|
      fork { 
        ENV["METABUILD_PARALLEL"] = "1"
        d.propagate_run(use_cache, parallel) 
      }
    end
    Process.waitall.each { |a| success = false unless a[1].success? } unless @depends.empty?
  else
    each_dep do |d|
      d.propagate_run(use_cache, parallel)
    end
  end
  if success
    run(use_cache) 
  else
    raise "Dependency failed for #{name}"
  end
end

#remove_dep(dep) ⇒ Object



748
749
750
# File 'lib/metabuild.rb', line 748

def remove_dep dep
  @depends.delete dep
end

#run(use_cache = false) ⇒ Object

Run a block that defines all the steps of the build. Use caching if asked to. Note : Whatever the directory changes happening in the block,

when the block exits, we are in the same directory as when it started


790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'lib/metabuild.rb', line 790

def run(use_cache = false)
  return if @done
  old_dir = pwd
  if use_cache
    if check_cache
      puts "getting from cache"
      get_cache
    else
      block.call self
      update_cache
    end
  else
    block.call self
  end
  cd old_dir
  @done = true
end

#to_sObject



732
733
734
# File 'lib/metabuild.rb', line 732

def to_s
  @name
end

#update_cacheObject



781
782
783
784
# File 'lib/metabuild.rb', line 781

def update_cache
  cmd = ":;" + @results.inject("tar zcf " + CACHE + "/" + cksum + ".tgz "){|s, r| s += "#{r} "}
  raise "Error tar for target #{@name}" unless system(cmd)
end