You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gbuild 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #!/usr/bin/ruby
  2. require 'optparse'
  3. require 'yaml'
  4. require 'fileutils'
  5. require 'pathname'
  6. @options = {:num_procs => 2, :memory => 2000}
  7. @bitness = {
  8. 'i386' => 32,
  9. 'amd64' => 64,
  10. }
  11. @arches = {
  12. 'i386' => 'i386',
  13. 'amd64' => 'x86_64',
  14. }
  15. def system!(cmd)
  16. system(cmd) or raise "failed to run #{cmd}"
  17. end
  18. def sanitize(str, where)
  19. raise "unsanitary string in #{where}" if (str =~ /[^\w.-]/)
  20. str
  21. end
  22. def sanitize_path(str, where)
  23. raise "unsanitary string in #{where}" if (str =~ /[^\w\/.:+-]/)
  24. str
  25. end
  26. def info(str)
  27. puts str unless @options[:quiet]
  28. end
  29. def build_one_configuration(suite, arch, build_desc, reference_datetime)
  30. FileUtils.rm_f("var/build.log")
  31. bits = @bitness[arch] or raise "unknown architecture ${arch}"
  32. if ENV["USE_LXC"]
  33. ENV["LXC_ARCH"] = arch
  34. ENV["LXC_SUITE"] = suite
  35. end
  36. suitearch = "#{suite}-#{arch}"
  37. info "Stopping target if it is up"
  38. system "stop-target"
  39. sleep 1
  40. unless @options[:skip_image]
  41. info "Making a new image copy"
  42. system! "make-clean-vm --suite #{suite} --arch #{arch}"
  43. end
  44. info "Starting target"
  45. system! "start-target #{bits} #{suitearch}&"
  46. $stdout.write "Checking if target is up"
  47. (1..10).each do
  48. system "on-target true 2> /dev/null" and break
  49. sleep 2
  50. $stdout.write '.'
  51. end
  52. info ''
  53. system! "on-target true"
  54. info "Preparing build environment"
  55. system! "on-target setarch #{@arches[arch]} bash < target-bin/init-build.sh"
  56. build_desc["files"].each do |filename|
  57. filename = sanitize(filename, "files section")
  58. system! "copy-to-target #{@quiet_flag} inputs/#{filename} build/"
  59. end
  60. info "Updating apt-get repository (log in var/install.log)"
  61. system! "on-target -u root apt-get update > var/install.log 2>&1"
  62. info "Installing additional packages (log in var/install.log)"
  63. system! "on-target -u root -e DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends -y install #{build_desc["packages"].join(" ")} > var/install.log 2>&1"
  64. info "Grabbing package manifest"
  65. system! "on-target -u root bash < target-bin/grab-packages.sh > var/base-#{suitearch}.manifest"
  66. info "Creating build script (var/build-script)"
  67. File.open("var/build-script", "w") do |script|
  68. script.puts "#!/bin/bash"
  69. script.puts "set -e"
  70. script.puts "export OUTDIR=$HOME/out"
  71. script.puts "GBUILD_BITS=#{bits}"
  72. script.puts "MAKEOPTS=(-j#{@options[:num_procs]})"
  73. (ref_date, ref_time) = reference_datetime.split
  74. script.puts "REFERENCE_DATETIME='#{reference_datetime}'"
  75. script.puts "REFERENCE_DATE='#{ref_date}'"
  76. script.puts "REFERENCE_TIME='#{ref_time}'"
  77. script.puts
  78. build_desc["remotes"].each do |remote|
  79. dir = sanitize(remote["dir"], remote["dir"])
  80. system! "copy-to-target #{@quiet_flag} inputs/#{dir} build/"
  81. script.puts "(cd build/#{dir} && git reset -q --hard && git clean -q -f -d)"
  82. end
  83. script.puts "cd build"
  84. script.puts build_desc["script"]
  85. end
  86. info "Running build script (log in var/build.log)"
  87. system! "on-target setarch #{@arches[arch]} bash -x < var/build-script > var/build.log 2>&1"
  88. end
  89. ################################
  90. OptionParser.new do |opts|
  91. opts.banner = "Usage: build [options] <build-description>.yml"
  92. opts.on("-i", "--skip-image", "reuse current target image") do |v|
  93. @options[:skip_image] = v
  94. end
  95. opts.on("-q", "--quiet", "be quiet") do |v|
  96. @options[:quiet] = v
  97. end
  98. opts.on("-j PROCS", "--num-make PROCS", "number of processes to use") do |v|
  99. @options[:num_procs] = v
  100. end
  101. opts.on("-m MEM", "--memory MEM", "memory to allocate in MiB") do |v|
  102. @options[:memory] = v
  103. end
  104. opts.on("-c PAIRS", "--commit PAIRS", "comma separated list of DIRECTORY=COMMIT pairs") do |v|
  105. @options[:commit] = v
  106. end
  107. end.parse!
  108. if !ENV["USE_LXC"] and !File.exist?("/dev/kvm")
  109. $stderr.puts "\n************* WARNING: kvm not loaded, this will probably not work out\n\n"
  110. end
  111. base_dir = Pathname.new(__FILE__).expand_path.dirname.parent
  112. libexec_dir = base_dir + 'libexec'
  113. ENV['PATH'] = libexec_dir.to_s + ":" + ENV['PATH']
  114. ENV['GITIAN_BASE'] = base_dir.to_s
  115. ENV['NPROCS'] = @options[:num_procs].to_s
  116. ENV['VMEM'] = @options[:memory].to_s
  117. @quiet_flag = @options[:quiet] ? "-q" : ""
  118. build_desc_file = ARGV.shift or raise "must supply YAML build description file"
  119. build_desc = YAML.load_file(build_desc_file)
  120. in_sums = []
  121. build_dir = 'build'
  122. result_dir = 'result'
  123. FileUtils.rm_rf(build_dir)
  124. FileUtils.mkdir(build_dir)
  125. FileUtils.mkdir_p(result_dir)
  126. package_name = build_desc["name"] or raise "must supply name"
  127. package_name = sanitize(package_name, "package name")
  128. suites = build_desc["suites"] or raise "must supply suites"
  129. archs = build_desc["architectures"] or raise "must supply architectures"
  130. reference_datetime = build_desc["reference_datetime"] or raise "must supply reference_datetime"
  131. desc_sum = `sha256sum #{build_desc_file}`
  132. desc_sum = desc_sum.sub(build_desc_file, "#{package_name}-desc.yml")
  133. in_sums << desc_sum
  134. build_desc["files"].each do |filename|
  135. filename = sanitize(filename, "files section")
  136. in_sums << `cd inputs && sha256sum #{filename}`
  137. end
  138. commits = {}
  139. if @options[:commit]
  140. @options[:commit].split(',').each do |pair|
  141. (dir, commit) = pair.split('=')
  142. commits[dir] = commit
  143. end
  144. end
  145. build_desc["remotes"].each do |remote|
  146. if !remote["commit"]
  147. remote["commit"] = commits[remote["dir"]]
  148. raise "must specify a commit for directory #{remote["dir"]}" unless remote["commit"]
  149. end
  150. dir = sanitize(remote["dir"], remote["dir"])
  151. unless File.exist?("inputs/#{dir}")
  152. system!("git init inputs/#{dir}")
  153. end
  154. system!("cd inputs/#{dir} && git fetch --update-head-ok #{sanitize_path(remote["url"], remote["url"])} +refs/tags/*:refs/tags/* +refs/heads/*:refs/heads/*")
  155. commit = sanitize(remote["commit"], remote["commit"])
  156. commit = `cd inputs/#{dir} && git log --format=%H -1 #{commit}`.strip
  157. raise "error looking up commit for tag #{remote["commit"]}" unless $?.exitstatus == 0
  158. system!("cd inputs/#{dir} && git checkout -q #{commit}")
  159. in_sums << "git:#{commit} #{dir}"
  160. end
  161. base_manifests = YAML::Omap.new
  162. suites.each do |suite|
  163. suite = sanitize(suite, "suite")
  164. archs.each do |arch|
  165. info "--- Building for #{suite} #{arch} ---"
  166. arch = sanitize(arch, "architecture")
  167. # Build!
  168. build_one_configuration(suite, arch, build_desc, reference_datetime)
  169. info "Grabbing results"
  170. system! "copy-from-target #{@quiet_flag} out #{build_dir}"
  171. base_manifest = File.read("var/base-#{suite}-#{arch}.manifest")
  172. base_manifests["#{suite}-#{arch}"] = base_manifest
  173. end
  174. end
  175. out_dir = File.join(build_dir, "out")
  176. out_sums = {}
  177. info "Generating report"
  178. Dir.glob(File.join(out_dir, '**', '*'), File::FNM_DOTMATCH).sort.each do |file_in_out|
  179. next if File.directory?(file_in_out)
  180. file = file_in_out.sub(out_dir + File::SEPARATOR, '')
  181. file = sanitize_path(file, file_in_out)
  182. out_sums[file] = `cd #{out_dir} && sha256sum #{file}`
  183. raise "failed to sum #{file}" unless $? == 0
  184. puts out_sums[file] unless @options[:quiet]
  185. end
  186. out_manifest = out_sums.keys.sort.map { |key| out_sums[key] }.join('')
  187. in_manifest = in_sums.join('')
  188. # Use Omap to keep result deterministic
  189. report = YAML::Omap[
  190. 'out_manifest', out_manifest,
  191. 'in_manifest', in_manifest,
  192. 'base_manifests', base_manifests,
  193. ]
  194. result_file = "#{package_name}-res.yml"
  195. File.open(File.join(result_dir, result_file), "w") do |io|
  196. io.write report.to_yaml
  197. end
  198. system!("cd #{result_dir} && sha256sum #{result_file}") unless @options[:quiet]
  199. info "Done."