Hash :
9e041f1b
Author :
Thomas de Grivel
Date :
2023-02-22T17:32:56
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
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 system("which cc >/dev/null 2>&1")
"cc"
elsif system("which gcc >/dev/null 2>&1")
"gcc"
elsif system("which egcc >/dev/null 2>&1")
"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_all(repos)
repos.each do |name|
Repos.repo(name).clean_all()
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_fake(repos)
repos.each do |name|
Repos.repo(name).clean_fake()
end
end
def self.clean_package(repos)
repos.each do |name|
Repos.repo(name).clean_package()
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.fake(repos)
repos.each do |name|
Repos.repo(name).fake()
end
end
def self.fake_dir
"#{target_dir}/fake"
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{
CC: #{cc.inspect},
dir: #{dir.inspect},
ncpu: #{ncpu.inspect},
sha256: #{sha256.inspect},
target: #{target.inspect}
}"""
end
def self.install(repos)
repos.each do |name|
Repos.repo(name).install()
end
end
def self.installed_dir
"#{dir}/var/db/installed"
end
def self.lib_dir
"#{dir}/lib"
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.obj_dir
"#{target_dir}/obj"
end
@@os = nil
def self.os
os_fetch unless @@os
@@os
end
@@os_dir = nil
def self.os_dir
os_fetch unless @@os_dir
@@os_dir
end
def self.os_fetch
uname = `uname`
raise "uname failed" unless $?.success?
if uname == "Linux"
distrib = `uname -a | grep -io -e Debian -e gentoo -e Ubuntu | sed -e 's|gentoo|Gentoo|'`
@@os_dir = "#{uname}/#{distrib}"
@@os = "#{uname}_#{distrib}"
else
@@os = uname
@@os_dir = uname
end
end
def self.package(repos)
repos.each do |name|
Repos.repo(name).package()
end
end
def self.package_dir
"#{target_dir}/package"
end
def self.prefix
dir
end
def self.pull(repos)
repos.each do |name|
Repos.repo(name).pull()
end
end
@@sha256 = nil
def self.sha256
@@sha256 || @@sha256 = if cmd "which sha256"
"sha256"
elsif cmd "which sha256sum"
"sha256sum"
else
raise "sha256 executable not found"
end
end
def self.sha256_file(path)
if File.basename(sha256).match?(/sum/)
c = "#{sh_quote(sha256)} #{sh_quote(path)} | cut -d ' ' -f 1"
r = `#{c}`
raise "#{sha256} #{path.inspect} failed" unless $?.success?
return "SHA256 (#{path}) = #{r}"
end
c = "#{sh_quote(sha256)} #{sh_quote(path)}"
verbose(1, c)
r = `#{c}`
raise "#{sha256} #{path.inspect} failed" unless $?.success?
r
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
def self.test(repos)
repos.each do |name|
Repos.repo(name).test()
end
end
def self.uninstall(repos)
repos.each do |name|
Repos.repo(name).uninstall()
end
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