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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. end.parse!
  87. base_dir = Pathname.new(__FILE__).expand_path.dirname.parent
  88. libexec_dir = base_dir + 'libexec'
  89. ENV['PATH'] = libexec_dir.to_s + ":" + ENV['PATH']
  90. ENV['GITIAN_BASE'] = base_dir.to_s
  91. build_desc_file = ARGV.shift or raise "must supply YAML build description file"
  92. build_desc = YAML.load_file(build_desc_file)
  93. in_sums = []
  94. build_dir = 'build'
  95. result_dir = 'result'
  96. FileUtils.rm_rf(build_dir)
  97. FileUtils.mkdir(build_dir)
  98. FileUtils.mkdir_p(result_dir)
  99. package_name = build_desc["name"] or raise "must supply name"
  100. package_name = sanitize(package_name, "package name")
  101. suites = build_desc["suites"] or raise "must supply suites"
  102. archs = build_desc["architectures"] or raise "must supply architectures"
  103. reference_datetime = build_desc["reference_datetime"] or raise "must supply reference_datetime"
  104. desc_sum = `sha256sum #{build_desc_file}`
  105. desc_sum = desc_sum.sub(build_desc_file, "#{package_name}-desc.yml")
  106. in_sums << desc_sum
  107. build_desc["files"].each do |filename|
  108. filename = sanitize(filename, "files section")
  109. in_sums << `cd inputs && sha256sum #{filename}`
  110. end
  111. base_manifests = YAML::Omap.new
  112. suites.each do |suite|
  113. suite = sanitize(suite, "suite")
  114. archs.each do |arch|
  115. info "--- Building for #{suite} #{arch} ---"
  116. arch = sanitize(arch, "architecture")
  117. # Build!
  118. build_one_configuration(suite, arch, build_desc, reference_datetime)
  119. info "Grabbing results"
  120. system! "copy-from-target out #{build_dir}"
  121. base_manifest = File.read("var/base-#{suite}-#{arch}.manifest")
  122. base_manifests["#{suite}-#{arch}"] = base_manifest
  123. end
  124. end
  125. out_dir = File.join(build_dir, "out")
  126. out_sums = {}
  127. info "Generating report"
  128. Dir.glob(File.join(out_dir, '**', '*')).sort.each do |file_in_out|
  129. next if File.directory?(file_in_out)
  130. file = file_in_out.sub(out_dir + File::SEPARATOR, '')
  131. file = sanitize_path(file, file_in_out)
  132. out_sums[file] = `cd #{out_dir} && sha256sum #{file}`
  133. raise "failed to sum #{file}" unless $? == 0
  134. puts out_sums[file] unless @options[:quiet]
  135. end
  136. out_manifest = out_sums.keys.sort.map { |key| out_sums[key] }.join('')
  137. in_manifest = in_sums.join('')
  138. # Use Omap to keep result deterministic
  139. report = YAML::Omap[
  140. 'out_manifest', out_manifest,
  141. 'in_manifest', in_manifest,
  142. 'base_manifests', base_manifests,
  143. ]
  144. result_file = "#{package_name}-res.yml"
  145. File.open(File.join(result_dir, result_file), "w") do |io|
  146. io.write report.to_yaml
  147. end
  148. system!("cd #{result_dir} && sha256sum #{result_file}") unless @options[:quiet]
  149. info "Done."