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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #!/usr/bin/ruby
  2. require 'optparse'
  3. require 'yaml'
  4. require 'fileutils'
  5. require 'pathname'
  6. @options = {}
  7. @bitness = {
  8. 'i386' => 32,
  9. 'amd64' => 64,
  10. }
  11. def system!(cmd)
  12. system(cmd) or raise "failed to run #{cmd}"
  13. end
  14. def sanitize(str, where)
  15. raise "unsanitary string in #{where}" if (str =~ /[^\w.-]/)
  16. str
  17. end
  18. def sanitize_path(str, where)
  19. raise "unsanitary string in #{where}" if (str =~ /[^\w\/.-]/)
  20. str
  21. end
  22. def info(str)
  23. puts str unless @options[:quiet]
  24. end
  25. def build_one_configuration(suite, arch, build_desc, reference_datetime)
  26. bits = @bitness[arch] or raise "unknown architecture ${arch}"
  27. suitearch = "#{suite}-#{arch}"
  28. info "Stopping target if it is up"
  29. system "stop-target"
  30. sleep 1
  31. unless @options[:skip_image]
  32. info "Making a new image copy"
  33. system! "cp base-#{suitearch}.qcow2 target-#{suitearch}.qcow2"
  34. end
  35. info "Starting target"
  36. system! "start-target #{bits} #{suitearch}&"
  37. $stdout.write "Checking if target is up"
  38. (1..10).each do
  39. system "on-target true 2> /dev/null" and break
  40. sleep 2
  41. $stdout.write '.'
  42. end
  43. info ''
  44. system! "on-target true"
  45. info "Preparing build environment"
  46. system! "on-target bash < target-bin/init-build.sh"
  47. build_desc["files"].each do |filename|
  48. filename = sanitize(filename, "files section")
  49. system! "cd inputs && copy-to-target #{filename} build/"
  50. end
  51. info "Installing additional packages (log in var/install.log)"
  52. system! "on-target -u root apt-get -y install #{build_desc["packages"].join(" ")} > var/install.log 2>&1"
  53. info "Grabbing package manifest"
  54. system! "on-target -u root bash < target-bin/grab-packages.sh > var/base-#{suitearch}.manifest"
  55. info "Creating build script (var/build-script)"
  56. File.open("var/build-script", "w") do |script|
  57. script.puts "#!/bin/bash"
  58. script.puts "set -e"
  59. script.puts "export OUTDIR=$HOME/out"
  60. script.puts "GBUILD_BITS=#{bits}"
  61. script.puts "MAKEOPTS=(-j2)"
  62. (ref_date, ref_time) = reference_datetime.split
  63. script.puts "REFERENCE_DATETIME='#{reference_datetime}'"
  64. script.puts "REFERENCE_DATE='#{ref_date}'"
  65. script.puts "REFERENCE_TIME='#{ref_time}'"
  66. script.puts
  67. build_desc["remotes"].each do |remote|
  68. script.puts "git clone -q #{remote["url"]} build/#{remote["dir"]}"
  69. script.puts "(cd build/#{remote["dir"]} && git checkout -q #{remote["commit"]})"
  70. end
  71. script.puts "cd build"
  72. script.puts build_desc["script"]
  73. end
  74. info "Running build script (log in var/build.log)"
  75. system! "on-target bash < var/build-script > var/build.log 2>&1"
  76. end
  77. ################################
  78. OptionParser.new do |opts|
  79. opts.banner = "Usage: build [options] <build-description>.yml"
  80. opts.on("-i", "--skip-image", "reuse current target image") do |v|
  81. @options[:skip_image] = v
  82. end
  83. opts.on("-q", "--quiet", "be quiet") do |v|
  84. @options[:skip_image] = v
  85. end
  86. opts.on("-c PAIRS", "--commit PAIRS", "comma separated list of DIRECTORY=COMMIT pairs") do |v|
  87. @options[:commit] = v
  88. end
  89. end.parse!
  90. base_dir = Pathname.new(__FILE__).expand_path.dirname.parent
  91. libexec_dir = base_dir + 'libexec'
  92. ENV['PATH'] = libexec_dir.to_s + ":" + ENV['PATH']
  93. ENV['GITIAN_BASE'] = base_dir.to_s
  94. build_desc_file = ARGV.shift or raise "must supply YAML build description file"
  95. build_desc = YAML.load_file(build_desc_file)
  96. in_sums = []
  97. build_dir = 'build'
  98. result_dir = 'result'
  99. FileUtils.rm_rf(build_dir)
  100. FileUtils.mkdir(build_dir)
  101. FileUtils.mkdir_p(result_dir)
  102. package_name = build_desc["name"] or raise "must supply name"
  103. package_name = sanitize(package_name, "package name")
  104. suites = build_desc["suites"] or raise "must supply suites"
  105. archs = build_desc["architectures"] or raise "must supply architectures"
  106. reference_datetime = build_desc["reference_datetime"] or raise "must supply reference_datetime"
  107. desc_sum = `sha256sum #{build_desc_file}`
  108. desc_sum = desc_sum.sub(build_desc_file, "#{package_name}-desc.yml")
  109. in_sums << desc_sum
  110. build_desc["files"].each do |filename|
  111. filename = sanitize(filename, "files section")
  112. in_sums << `cd inputs && sha256sum #{filename}`
  113. end
  114. commits = {}
  115. if @options[:commit]
  116. @options[:commit].split(',').each do |pair|
  117. (dir, commit) = pair.split('=')
  118. commits[dir] = commit
  119. end
  120. end
  121. build_desc["remotes"].each do |remote|
  122. if !remote["commit"]
  123. remote["commit"] = commits[remote["dir"]]
  124. raise "must specify a commit for directory #{remote["dir"]}" unless remote["commit"]
  125. end
  126. end
  127. base_manifests = YAML::Omap.new
  128. suites.each do |suite|
  129. suite = sanitize(suite, "suite")
  130. archs.each do |arch|
  131. info "--- Building for #{suite} #{arch} ---"
  132. arch = sanitize(arch, "architecture")
  133. # Build!
  134. build_one_configuration(suite, arch, build_desc, reference_datetime)
  135. info "Grabbing results"
  136. system! "copy-from-target out #{build_dir}"
  137. base_manifest = File.read("var/base-#{suite}-#{arch}.manifest")
  138. base_manifests["#{suite}-#{arch}"] = base_manifest
  139. end
  140. end
  141. out_dir = File.join(build_dir, "out")
  142. out_sums = {}
  143. info "Generating report"
  144. Dir.glob(File.join(out_dir, '**', '*')).sort.each do |file_in_out|
  145. next if File.directory?(file_in_out)
  146. file = file_in_out.sub(out_dir + File::SEPARATOR, '')
  147. file = sanitize_path(file, file_in_out)
  148. out_sums[file] = `cd #{out_dir} && sha256sum #{file}`
  149. raise "failed to sum #{file}" unless $? == 0
  150. puts out_sums[file] unless @options[:quiet]
  151. end
  152. out_manifest = out_sums.keys.sort.map { |key| out_sums[key] }.join('')
  153. in_manifest = in_sums.join('')
  154. # Use Omap to keep result deterministic
  155. report = YAML::Omap[
  156. 'out_manifest', out_manifest,
  157. 'in_manifest', in_manifest,
  158. 'base_manifests', base_manifests,
  159. ]
  160. result_file = "#{package_name}-res.yml"
  161. File.open(File.join(result_dir, result_file), "w") do |io|
  162. io.write report.to_yaml
  163. end
  164. system!("cd #{result_dir} && sha256sum #{result_file}") unless @options[:quiet]
  165. info "Done."