Hash :
c37d67c9
Author :
Thomas de Grivel
Date :
2023-02-21T09:46:15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
require 'time'
require __FILE__ + '/../rbpkg/repos'
module Rbpkg
def self.build(repos)
repos.each do |name|
Repos.repo(name).build()
end
end
def self.cc
if `which cc`
"cc"
elsif `which gcc`
"gcc"
elsif `which egcc`
"egcc"
end
end
def self.cc!
r = cc()
raise "C compiler not found" unless r
r
end
def self.checkout(branch, repos)
repos.each do |name|
Repos.repo(name).checkout(branch)
end
end
def self.clean_build(repos)
repos.each do |name|
Repos.repo(name).clean_build()
end
end
def self.clean_configure(repos)
repos.each do |name|
Repos.repo(name).clean_configure()
end
end
def self.clean_sources(repos)
repos.each do |name|
Repos.repo(name).clean_sources()
end
end
def self.configure(repos)
repos.each do |name|
Repos.repo(name).configure()
end
end
@@date = nil
def self.date
@@date || @@date = Time.now.utc.iso8601
end
@@dir = nil
def self.dir
@@dir || @@dir = (ENV["RBPKG_DIR"] || "#{ENV["HOME"]}/rbpkg")
end
def self.ensure_dir(path)
if ! File.directory?(path)
cmd! "mkdir -p #{sh_quote(path)}"
end
end
def self.fetch(repos)
repos.each do |name|
Repos.repo(name).fetch()
end
end
def self.git_clone(repos)
repos.each do |name|
Repos.repo(name).git_clone()
end
end
def self.info(repos)
global_info = inspect()
$log.puts global_info
puts global_info
repos.each do |name|
repo_info = Repos.repo(name).inspect()
$log.puts repo_info
puts repo_info
end
end
def self.init
[dir,
lock_dir,
log_dir
].each do |d|
ensure_dir(d)
end
end
def self.inspect
"""%Rbpkg{
dir: #{dir.inspect},
CC: #{cc.inspect},
ncpu: #{ncpu.inspect},
target: #{target.inspect}
}"""
end
def self.lock(name)
path = lock_path(name)
#verbose(2, "Locking #{path}")
while File.file?(path)
sleep 1000.ms
end
File.write(path, "")
at_exit { FileUtils.rm(path) }
end
def self.lock_dir
"#{dir}/var/lock"
end
def self.lock_path(name)
"#{lock_dir}/#{name}.lock"
end
def self.log_dir
"#{dir}/var/log/shpkg"
end
def self.log_path(name)
"#{log_dir}/#{name}_#{date}_#{$$}.log"
end
@@ncpu = nil
def self.ncpu
@@ncpu || @@ncpu = self.ncpu_fetch
end
def self.ncpu_fetch
`which nproc >/dev/null 2>&1 && nproc || which sysctl >/dev/null 2>&1 && sysctl -n hw.ncpu || echo 1`.strip.to_i
end
def self.pull(repos)
repos.each do |name|
Repos.repo(name).pull()
end
end
def self.src_dir
"#{dir}/src"
end
def self.tag_dir
"#{target_dir}/tag"
end
@@target = nil
def self.target
@@target || @@target = `LC_ALL=C.UTF-8 #{sh_quote(cc!)} -v 2>&1 | grep '^Target: ' | head -n 1 | cut -c 9-`.strip
end
def self.target_dir
"#{dir}/target/#{target}"
end
end
def cmd(string)
verbose(1, string)
IO.popen("#{string} 2>&1", "r") do |pipe|
while line = pipe.gets()
$log.puts line
puts line
end
pipe.close
$?.success?
end
end
def cmd!(string)
r = cmd(string)
raise "command failed: #{string}" unless r
true
end
def sh_quote (str)
if str.scan("\n") == []
if str.match?(/^[-+\/=.,:^_0-9A-Za-z]*$/)
str
else
"\"" + str.gsub(/([$`\\\"])/, "\\\1") + "\""
end
end
end
def verbose(level, msg)
colored = case level
when 1
"\33[0;34m#{msg}\33[0m\n"
when 2
"\33[0;35m#{msg}\33[0m\n"
else
raise "unknown verbose level: #{level}"
end
$log.puts colored
puts colored
end