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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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..30).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. if File.directory?("cache/#{build_desc["name"]}")
  61. system! "copy-to-target #{@quiet_flag} cache/#{build_desc["name"]}/ cache/"
  62. end
  63. if File.directory?("cache/common")
  64. system! "copy-to-target #{@quiet_flag} cache/common/ cache/"
  65. end
  66. info "Updating apt-get repository (log in var/install.log)"
  67. system! "on-target -u root apt-get update > var/install.log 2>&1"
  68. info "Installing additional packages (log in var/install.log)"
  69. 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"
  70. info "Grabbing package manifest"
  71. system! "on-target -u root bash < target-bin/grab-packages.sh > var/base-#{suitearch}.manifest"
  72. info "Creating build script (var/build-script)"
  73. File.open("var/build-script", "w") do |script|
  74. script.puts "#!/bin/bash"
  75. script.puts "set -e"
  76. script.puts "export LANG='en_US.UTF-8'"
  77. script.puts "export LC_ALL='en_US.UTF-8'"
  78. script.puts "umask 002"
  79. script.puts "export OUTDIR=$HOME/out"
  80. script.puts "GBUILD_BITS=#{bits}"
  81. script.puts "MAKEOPTS=(-j#{@options[:num_procs]})"
  82. (ref_date, ref_time) = reference_datetime.split
  83. script.puts "REFERENCE_DATETIME='#{reference_datetime}'"
  84. script.puts "REFERENCE_DATE='#{ref_date}'"
  85. script.puts "REFERENCE_TIME='#{ref_time}'"
  86. script.puts
  87. build_desc["remotes"].each do |remote|
  88. dir = sanitize(remote["dir"], remote["dir"])
  89. system! "copy-to-target #{@quiet_flag} inputs/#{dir} build/"
  90. script.puts "(cd build/#{dir} && git reset -q --hard && git clean -q -f -d)"
  91. end
  92. script.puts "cd build"
  93. script.puts build_desc["script"]
  94. end
  95. info "Running build script (log in var/build.log)"
  96. system! "on-target setarch #{@arches[arch]} bash -x < var/build-script > var/build.log 2>&1"
  97. end
  98. ################################
  99. OptionParser.new do |opts|
  100. opts.banner = "Usage: build [options] <build-description>.yml"
  101. opts.on("-i", "--skip-image", "reuse current target image") do |v|
  102. @options[:skip_image] = v
  103. end
  104. opts.on("-q", "--quiet", "be quiet") do |v|
  105. @options[:quiet] = v
  106. end
  107. opts.on("-j PROCS", "--num-make PROCS", "number of processes to use") do |v|
  108. @options[:num_procs] = v
  109. end
  110. opts.on("-m MEM", "--memory MEM", "memory to allocate in MiB") do |v|
  111. @options[:memory] = v
  112. end
  113. opts.on("-c PAIRS", "--commit PAIRS", "comma separated list of DIRECTORY=COMMIT pairs") do |v|
  114. @options[:commit] = v
  115. end
  116. opts.on("-u PAIRS", "--url PAIRS", "comma separated list of DIRECTORY=URL pairs") do |v|
  117. @options[:url] = v
  118. end
  119. end.parse!
  120. if !ENV["USE_LXC"] and !File.exist?("/dev/kvm")
  121. $stderr.puts "\n************* WARNING: kvm not loaded, this will probably not work out\n\n"
  122. end
  123. base_dir = Pathname.new(__FILE__).expand_path.dirname.parent
  124. libexec_dir = base_dir + 'libexec'
  125. ENV['PATH'] = libexec_dir.to_s + ":" + ENV['PATH']
  126. ENV['GITIAN_BASE'] = base_dir.to_s
  127. ENV['NPROCS'] = @options[:num_procs].to_s
  128. ENV['VMEM'] = @options[:memory].to_s
  129. @quiet_flag = @options[:quiet] ? "-q" : ""
  130. build_desc_file = ARGV.shift or raise "must supply YAML build description file"
  131. build_desc = YAML.load_file(build_desc_file)
  132. in_sums = []
  133. build_dir = 'build'
  134. result_dir = 'result'
  135. cache_dir = 'cache'
  136. FileUtils.rm_rf(build_dir)
  137. FileUtils.mkdir(build_dir)
  138. FileUtils.mkdir_p(result_dir)
  139. package_name = build_desc["name"] or raise "must supply name"
  140. package_name = sanitize(package_name, "package name")
  141. FileUtils.mkdir_p(File.join(cache_dir, "common"))
  142. FileUtils.mkdir_p(File.join(cache_dir, package_name))
  143. suites = build_desc["suites"] or raise "must supply suites"
  144. archs = build_desc["architectures"] or raise "must supply architectures"
  145. reference_datetime = build_desc["reference_datetime"] or raise "must supply reference_datetime"
  146. desc_sum = `sha256sum #{build_desc_file}`
  147. desc_sum = desc_sum.sub(build_desc_file, "#{package_name}-desc.yml")
  148. in_sums << desc_sum
  149. build_desc["files"].each do |filename|
  150. filename = sanitize(filename, "files section")
  151. in_sums << `cd inputs && sha256sum #{filename}`
  152. end
  153. commits = {}
  154. if @options[:commit]
  155. @options[:commit].split(',').each do |pair|
  156. (dir, commit) = pair.split('=')
  157. commits[dir] = commit
  158. end
  159. end
  160. urls = {}
  161. if @options[:url]
  162. @options[:url].split(',').each do |pair|
  163. (dir, url) = pair.split('=')
  164. urls[dir] = url
  165. end
  166. end
  167. build_desc["remotes"].each do |remote|
  168. if !remote["commit"]
  169. remote["commit"] = commits[remote["dir"]]
  170. raise "must specify a commit for directory #{remote["dir"]}" unless remote["commit"]
  171. end
  172. if urls[remote["dir"]]
  173. remote["url"] = urls[remote["dir"]]
  174. end
  175. dir = sanitize(remote["dir"], remote["dir"])
  176. unless File.exist?("inputs/#{dir}")
  177. system!("git init inputs/#{dir}")
  178. end
  179. system!("cd inputs/#{dir} && git fetch --update-head-ok #{sanitize_path(remote["url"], remote["url"])} +refs/tags/*:refs/tags/* +refs/heads/*:refs/heads/*")
  180. commit = sanitize(remote["commit"], remote["commit"])
  181. commit = `cd inputs/#{dir} && git log --format=%H -1 #{commit}`.strip
  182. raise "error looking up commit for tag #{remote["commit"]}" unless $?.exitstatus == 0
  183. system!("cd inputs/#{dir} && git checkout -q #{commit}")
  184. in_sums << "git:#{commit} #{dir}"
  185. end
  186. base_manifests = YAML::Omap.new
  187. suites.each do |suite|
  188. suite = sanitize(suite, "suite")
  189. archs.each do |arch|
  190. info "--- Building for #{suite} #{arch} ---"
  191. arch = sanitize(arch, "architecture")
  192. # Build!
  193. build_one_configuration(suite, arch, build_desc, reference_datetime)
  194. info "Grabbing results"
  195. system! "copy-from-target #{@quiet_flag} out #{build_dir}"
  196. info "Grabbing cache"
  197. system! "copy-from-target #{@quiet_flag} cache/#{package_name}/ #{cache_dir}"
  198. system! "copy-from-target #{@quiet_flag} cache/common/ #{cache_dir}"
  199. base_manifest = File.read("var/base-#{suite}-#{arch}.manifest")
  200. base_manifests["#{suite}-#{arch}"] = base_manifest
  201. end
  202. end
  203. out_dir = File.join(build_dir, "out")
  204. out_sums = {}
  205. cache_common_dir = File.join(cache_dir, "common")
  206. cache_package_dir = File.join(cache_dir, "#{package_name}")
  207. cache_common_sums = {}
  208. cache_package_sums = {}
  209. info "Generating report"
  210. Dir.glob(File.join(out_dir, '**', '*'), File::FNM_DOTMATCH).sort.each do |file_in_out|
  211. next if File.directory?(file_in_out)
  212. file = file_in_out.sub(out_dir + File::SEPARATOR, '')
  213. file = sanitize_path(file, file_in_out)
  214. out_sums[file] = `cd #{out_dir} && sha256sum #{file}`
  215. raise "failed to sum #{file}" unless $? == 0
  216. puts out_sums[file] unless @options[:quiet]
  217. end
  218. Dir.glob(File.join(cache_common_dir, '**', '*'), File::FNM_DOTMATCH).sort.each do |file_in_out|
  219. next if File.directory?(file_in_out)
  220. file = file_in_out.sub(cache_common_dir + File::SEPARATOR, '')
  221. file = sanitize_path(file, file_in_out)
  222. cache_common_sums[file] = `cd #{cache_common_dir} && sha256sum #{file}`
  223. raise "failed to sum #{file}" unless $? == 0
  224. end
  225. Dir.glob(File.join(cache_package_dir, '**', '*'), File::FNM_DOTMATCH).sort.each do |file_in_out|
  226. next if File.directory?(file_in_out)
  227. file = file_in_out.sub(cache_package_dir + File::SEPARATOR, '')
  228. file = sanitize_path(file, file_in_out)
  229. cache_package_sums[file] = `cd #{cache_package_dir} && sha256sum #{file}`
  230. raise "failed to sum #{file}" unless $? == 0
  231. end
  232. out_manifest = out_sums.keys.sort.map { |key| out_sums[key] }.join('')
  233. in_manifest = in_sums.join('')
  234. cache_common_manifest = cache_common_sums.keys.sort.map { |key| cache_common_sums[key] }.join('')
  235. cache_package_manifest = cache_package_sums.keys.sort.map { |key| cache_package_sums[key] }.join('')
  236. # Use Omap to keep result deterministic
  237. report = YAML::Omap[
  238. 'out_manifest', out_manifest,
  239. 'in_manifest', in_manifest,
  240. 'base_manifests', base_manifests,
  241. 'cache_common_manifest', cache_common_manifest,
  242. 'cache_package_manifest', cache_package_manifest,
  243. ]
  244. result_file = "#{package_name}-res.yml"
  245. File.open(File.join(result_dir, result_file), "w") do |io|
  246. io.write report.to_yaml
  247. end
  248. system!("cd #{result_dir} && sha256sum #{result_file}") unless @options[:quiet]
  249. info "Done."