scripts: avoid global variables in target handler scripts

main
Matthias Schiffer 2019-06-18 01:33:12 +02:00
parent c9f1fdf6ee
commit 09a09d634a
No known key found for this signature in database
GPG Key ID: 16EF3F64CB201D9C
13 changed files with 202 additions and 174 deletions

View File

@ -1,4 +1,5 @@
dofile('scripts/common.inc.lua')
local lib = dofile('scripts/target_lib.lua')
local env = lib.env
local subtarget = env.SUBTARGET
@ -9,9 +10,9 @@ end
local bindir = env.BOARD .. '/' .. subtarget
exec({'rm', '-f', 'openwrt/bin/targets/'..bindir..'/\0'}, true, '2>/dev/null')
lib.exec({'rm', '-f', 'openwrt/bin/targets/'..bindir..'/\0'}, true, '2>/dev/null')
-- Full builds will output the "packages" directory, so clean up first
if (env.GLUON_DEVICES or '') == '' then
exec {'rm', '-rf', 'openwrt/bin/targets/'..bindir..'/packages'}
lib.exec {'rm', '-rf', 'openwrt/bin/targets/'..bindir..'/packages'}
end

View File

@ -1,8 +1,8 @@
dofile('scripts/common.inc.lua')
local lib = dofile('scripts/target_lib.lua')
local env = lib.env
assert(env.GLUON_IMAGEDIR)
assert(env.GLUON_PACKAGEDIR)
assert(env.GLUON_TARGETSDIR)
local target = arg[1]
@ -20,7 +20,7 @@ local bindir = env.BOARD .. '/' .. subtarget
local function mkdir(dir)
exec {'mkdir', '-p', dir}
lib.exec {'mkdir', '-p', dir}
end
mkdir(env.GLUON_IMAGEDIR..'/factory')
@ -28,40 +28,41 @@ mkdir(env.GLUON_IMAGEDIR..'/sysupgrade')
mkdir(env.GLUON_IMAGEDIR..'/other')
dofile(env.GLUON_TARGETSDIR..'/'..target)
lib.include(target)
local function clean(image, name)
local dir, file = image:dest_name(name, '\0', '\0')
exec {'rm', '-f', dir..'/'..file}
lib.exec {'rm', '-f', dir..'/'..file}
end
for _, image in ipairs(images) do
for _, image in ipairs(lib.images) do
clean(image, image.image)
local destdir, destname = image:dest_name(image.image)
local source = string.format('openwrt/bin/targets/%s/openwrt-%s-%s%s%s', bindir, openwrt_target, image.name, image.in_suffix, image.extension)
local source = string.format('openwrt/bin/targets/%s/openwrt-%s-%s%s%s',
bindir, openwrt_target, image.name, image.in_suffix, image.extension)
exec {'cp', source, destdir..'/'..destname}
lib.exec {'cp', source, destdir..'/'..destname}
for _, alias in ipairs(image.aliases) do
clean(image, alias)
local _, aliasname = image:dest_name(alias)
exec {'ln', '-s', destname, destdir..'/'..aliasname}
lib.exec {'ln', '-s', destname, destdir..'/'..aliasname}
end
end
-- Copy opkg repo
if opkg and (env.GLUON_DEVICES or '') == '' then
local package_prefix = string.format('gluon-%s-%s', site_code, env.GLUON_RELEASE)
if lib.opkg and (env.GLUON_DEVICES or '') == '' then
local package_prefix = string.format('gluon-%s-%s', lib.site_code, env.GLUON_RELEASE)
local function dest_dir(prefix)
return env.GLUON_PACKAGEDIR..'/'..prefix..'/'..bindir
end
exec {'rm', '-f', dest_dir('\0')..'/\0'}
exec({'rmdir', '-p', dest_dir('\0')}, true, '2>/dev/null')
lib.exec {'rm', '-f', dest_dir('\0')..'/\0'}
lib.exec({'rmdir', '-p', dest_dir('\0')}, true, '2>/dev/null')
mkdir(dest_dir(package_prefix))
exec {'cp', 'openwrt/bin/targets/'..bindir..'/packages/\0', dest_dir(package_prefix)}
lib.exec {'cp', 'openwrt/bin/targets/'..bindir..'/packages/\0', dest_dir(package_prefix)}
end

View File

@ -1,12 +1,12 @@
dofile('scripts/common.inc.lua')
local lib = dofile('scripts/target_lib.lua')
local env = lib.env
assert(env.GLUON_IMAGEDIR)
assert(env.GLUON_TARGETSDIR)
local target = arg[1]
dofile(env.GLUON_TARGETSDIR..'/'..target)
lib.include(target)
local function strip(s)
@ -14,13 +14,13 @@ local function strip(s)
end
local function generate_line(model, dir, filename, filesize)
local exists = pcall(exec, {'test', '-e', dir..'/'..filename})
local exists = pcall(lib.exec, {'test', '-e', dir..'/'..filename})
if not exists then
return
end
local file256sum = strip(exec_capture {'scripts/sha256sum.sh', dir..'/'..filename})
local file512sum = strip(exec_capture {'scripts/sha512sum.sh', dir..'/'..filename})
local file256sum = strip(lib.exec_capture {'scripts/sha256sum.sh', dir..'/'..filename})
local file512sum = strip(lib.exec_capture {'scripts/sha512sum.sh', dir..'/'..filename})
io.stdout:write(string.format('%s %s %s %s %s\n', model, env.GLUON_RELEASE, file256sum, filesize, filename))
io.stdout:write(string.format('%s %s %s %s\n', model, env.GLUON_RELEASE, file256sum, filename))
@ -29,12 +29,12 @@ end
local function generate(image)
local dir, filename = image:dest_name(image.image)
local exists = pcall(exec, {'test', '-e', dir..'/'..filename})
local exists = pcall(lib.exec, {'test', '-e', dir..'/'..filename})
if not exists then
return
end
local filesize = strip(exec_capture {'scripts/filesize.sh', dir..'/'..filename})
local filesize = strip(lib.exec_capture {'scripts/filesize.sh', dir..'/'..filename})
generate_line(image.image, dir, filename, filesize)
@ -48,7 +48,7 @@ local function generate(image)
end
end
for _, image in ipairs(images) do
for _, image in ipairs(lib.images) do
if image.subdir == 'sysupgrade' then
generate(image)
end

View File

@ -1,75 +0,0 @@
assert(env.BOARD)
assert(env.SUBTARGET)
local target = arg[1]
local extra_packages = arg[2]
local openwrt_config_target
if env.SUBTARGET ~= '' then
openwrt_config_target = env.BOARD .. '_' .. env.SUBTARGET
else
openwrt_config_target = env.BOARD
end
local function site_packages(profile)
return exec_capture_raw(string.format([[
MAKEFLAGS= make print PROFILE=%s --no-print-directory -s -f - <<'END_MAKE'
include $(GLUON_SITEDIR)/site.mk
print:
echo -n '$(GLUON_$(PROFILE)_SITE_PACKAGES)'
END_MAKE
]], escape(profile)))
end
dofile(env.GLUON_TARGETSDIR .. '/generic')
for pkg in string.gmatch(extra_packages, '%S+') do
packages {pkg}
end
dofile(env.GLUON_TARGETSDIR .. '/' .. target)
check_devices()
if not opkg then
config '# CONFIG_SIGNED_PACKAGES is not set'
config 'CONFIG_CLEAN_IPKG=y'
packages {'-opkg'}
end
local default_pkgs = ''
for _, pkg in ipairs(target_packages) do
default_pkgs = default_pkgs .. ' ' .. pkg
if string.sub(pkg, 1, 1) == '-' then
try_config('# CONFIG_PACKAGE_%s is not set', string.sub(pkg, 2))
else
config_package(pkg, 'y')
end
end
for _, dev in ipairs(devices) do
local profile = dev.options.profile or dev.name
local device_pkgs = default_pkgs
local function handle_pkg(pkg)
if string.sub(pkg, 1, 1) ~= '-' then
config_package(pkg, 'm')
end
device_pkgs = device_pkgs .. ' ' .. pkg
end
for _, pkg in ipairs(dev.options.packages or {}) do
handle_pkg(pkg)
end
for pkg in string.gmatch(site_packages(profile), '%S+') do
handle_pkg(pkg)
end
config_message(string.format("unable to enable device '%s'", profile), 'CONFIG_TARGET_DEVICE_%s_DEVICE_%s=y', openwrt_config_target, profile)
config('CONFIG_TARGET_DEVICE_PACKAGES_%s_DEVICE_%s="%s"',
openwrt_config_target, profile, device_pkgs)
end

View File

@ -1,28 +1,22 @@
dofile('scripts/common.inc.lua')
local funcs = {}
function funcs.config_message(config, _, ...)
config(...)
end
function funcs.config_package(config, pkg, value)
config('CONFIG_PACKAGE_%s=%s', pkg, value)
end
local lib = dofile('scripts/target_config_lib.lua')(funcs)
local output = {}
function config(...)
table.insert(output, string.format(...))
for config in pairs(lib.configs) do
table.insert(output, config)
end
try_config = config
function config_message(msg, ...)
config(...)
end
function config_package(pkg, value)
config('CONFIG_PACKAGE_%s=%s', pkg, value)
end
dofile('scripts/target_config.inc.lua')
-- The sort will make =y entries override =m ones
table.sort(output)
for _, line in ipairs(output) do

View File

@ -1,6 +1,3 @@
dofile('scripts/common.inc.lua')
local ret = 0
@ -31,15 +28,10 @@ local function check_config_prefix(pattern)
return match_config(function(line) return string.sub(line, 1, -2) == pattern end)
end
function config(...)
local pattern = string.format(...)
if not check_config(pattern) then
fail("unable to set '%s'", pattern)
end
end
local funcs = {}
function config_message(message, ...)
function funcs.config_message(_, message, ...)
local pattern = string.format(...)
if not check_config(pattern) then
@ -47,9 +39,9 @@ function config_message(message, ...)
end
end
function config_package(pkg, value)
function funcs.config_package(_, pkg, value)
local pattern = string.format('CONFIG_PACKAGE_%s=%s', pkg, value)
local ret
local res
if value == 'y' then
res = check_config(pattern)
else
@ -61,8 +53,14 @@ function config_package(pkg, value)
end
end
local lib = dofile('scripts/target_config_lib.lua')(funcs)
dofile('scripts/target_config.inc.lua')
for config, v in pairs(lib.configs) do
if v == 2 then
if not check_config(config) then
fail("unable to set '%s'", config)
end
end
end
os.exit(ret)

View File

@ -0,0 +1,82 @@
return function(funcs)
local lib = dofile('scripts/target_lib.lua')
local env = lib.env
assert(env.BOARD)
assert(env.SUBTARGET)
local target = arg[1]
local extra_packages = arg[2]
local openwrt_config_target
if env.SUBTARGET ~= '' then
openwrt_config_target = env.BOARD .. '_' .. env.SUBTARGET
else
openwrt_config_target = env.BOARD
end
local function site_packages(profile)
return lib.exec_capture_raw(string.format([[
MAKEFLAGS= make print PROFILE=%s --no-print-directory -s -f - <<'END_MAKE'
include $(GLUON_SITEDIR)/site.mk
print:
echo -n '$(GLUON_$(PROFILE)_SITE_PACKAGES)'
END_MAKE
]], lib.escape(profile)))
end
lib.include('generic')
for pkg in string.gmatch(extra_packages, '%S+') do
lib.packages {pkg}
end
lib.include(target)
lib.check_devices()
if not lib.opkg then
lib.config '# CONFIG_SIGNED_PACKAGES is not set'
lib.config 'CONFIG_CLEAN_IPKG=y'
lib.packages {'-opkg'}
end
local default_pkgs = ''
for _, pkg in ipairs(lib.target_packages) do
default_pkgs = default_pkgs .. ' ' .. pkg
if string.sub(pkg, 1, 1) == '-' then
lib.try_config('# CONFIG_PACKAGE_%s is not set', string.sub(pkg, 2))
else
funcs.config_package(lib.config, pkg, 'y')
end
end
for _, dev in ipairs(lib.devices) do
local profile = dev.options.profile or dev.name
local device_pkgs = default_pkgs
local function handle_pkg(pkg)
if string.sub(pkg, 1, 1) ~= '-' then
funcs.config_package(lib.config, pkg, 'm')
end
device_pkgs = device_pkgs .. ' ' .. pkg
end
for _, pkg in ipairs(dev.options.packages or {}) do
handle_pkg(pkg)
end
for pkg in string.gmatch(site_packages(profile), '%S+') do
handle_pkg(pkg)
end
funcs.config_message(lib.config, string.format("unable to enable device '%s'", profile),
'CONFIG_TARGET_DEVICE_%s_DEVICE_%s=y', openwrt_config_target, profile)
lib.config('CONFIG_TARGET_DEVICE_PACKAGES_%s_DEVICE_%s="%s"',
openwrt_config_target, profile, device_pkgs)
end
return lib
end

View File

@ -1,9 +1,24 @@
env = setmetatable({}, {
__index = function(t, k) return os.getenv(k) end
-- Functions for use in targets/*
local F = {}
-- To be accessed by scripts using target_lib
local M = setmetatable({}, { __index = F })
local funcs = setmetatable({}, {
__index = function(_, k)
return F[k] or _G[k]
end,
})
envtrue = setmetatable({}, {
__index = function(t, k) return (tonumber(os.getenv(k)) or 0) > 0 end
local env = setmetatable({}, {
__index = function(_, k) return os.getenv(k) end
})
F.env = env
local envtrue = setmetatable({}, {
__index = function(_, k) return (tonumber(os.getenv(k)) or 0) > 0 end
})
F.envtrue = envtrue
assert(env.GLUON_SITEDIR)
assert(env.GLUON_TARGETSDIR)
@ -11,11 +26,14 @@ assert(env.GLUON_RELEASE)
assert(env.GLUON_DEPRECATED)
site_code = assert(assert(dofile('scripts/site_config.lua')('site.conf')).site_code)
M.site_code = assert(assert(dofile('scripts/site_config.lua')('site.conf')).site_code)
M.target_packages = {}
M.configs = {}
M.devices = {}
M.images = {}
M.opkg = true
target_packages = {}
local default_options = {
profile = false,
factory = '-squashfs-factory',
@ -73,7 +91,7 @@ end
-- argument are replaced by '\''.
-- To allow using shell wildcards, zero bytes in the arguments are replaced
-- by unquoted asterisks.
function escape(s)
function F.escape(s)
s = string.gsub(s, "'", "'\\''")
s = string.gsub(s, "%z", "'*'")
return "'" .. s .. "'"
@ -82,7 +100,7 @@ end
local function escape_command(command, raw)
local ret = 'exec'
for _, arg in ipairs(command) do
ret = ret .. ' ' .. escape(arg)
ret = ret .. ' ' .. F.escape(arg)
end
if raw then
ret = ret .. ' ' .. raw
@ -90,17 +108,17 @@ local function escape_command(command, raw)
return ret
end
function exec_raw(command, may_fail)
function F.exec_raw(command, may_fail)
local ret = os.execute(command)
assert((ret == 0) or may_fail)
return ret
end
function exec(command, may_fail, raw)
return exec_raw(escape_command(command, raw), may_fail)
function F.exec(command, may_fail, raw)
return F.exec_raw(escape_command(command, raw), may_fail)
end
function exec_capture_raw(command)
function F.exec_capture_raw(command)
local f = io.popen(command)
assert(f)
@ -109,47 +127,47 @@ function exec_capture_raw(command)
return data
end
function exec_capture(command, raw)
return exec_capture_raw(escape_command(command, raw))
function F.exec_capture(command, raw)
return F.exec_capture_raw(escape_command(command, raw))
end
local image_mt = {
__index = {
dest_name = function(image, name, site, release)
return env.GLUON_IMAGEDIR..'/'..image.subdir, 'gluon-'..(site or site_code)..'-'..(release or env.GLUON_RELEASE)..'-'..name..image.out_suffix..image.extension
return env.GLUON_IMAGEDIR..'/'..image.subdir,
'gluon-'..(site or M.site_code)..'-'..(release or env.GLUON_RELEASE)..'-'..name..image.out_suffix..image.extension
end,
},
}
local function add_image(image)
table.insert(images, setmetatable(image, image_mt))
table.insert(M.images, setmetatable(image, image_mt))
end
function F.try_config(...)
M.configs[string.format(...)] = 1
end
-- Variables to be consumed by scripts using common.inc.lua
devices = {}
images = {}
opkg = true
function F.config(...)
M.configs[string.format(...)] = 2
end
function config() end
function try_config() end
function packages(pkgs)
function F.packages(pkgs)
for _, pkg in ipairs(pkgs) do
table.insert(target_packages, pkg)
table.insert(M.target_packages, pkg)
end
end
M.packages = F.packages
function device(image, name, options)
function F.device(image, name, options)
options = merge(default_options, options)
if not want_device(image, options) then
return
end
table.insert(devices, {
table.insert(M.devices, {
image = image,
name = name,
options = options,
@ -198,7 +216,7 @@ function device(image, name, options)
end
end
function factory_image(image, name, ext, options)
function F.factory_image(image, name, ext, options)
options = merge(default_options, options)
if not want_device(image, options) then
@ -221,7 +239,7 @@ function factory_image(image, name, ext, options)
}
end
function sysupgrade_image(image, name, ext, options)
function F.sysupgrade_image(image, name, ext, options)
options = merge(default_options, options)
if not want_device(image, options) then
@ -240,16 +258,22 @@ function sysupgrade_image(image, name, ext, options)
}
end
function no_opkg()
opkg = false
function F.no_opkg()
M.opkg = false
end
function defaults(options)
function F.defaults(options)
default_options = merge(default_options, options)
end
function F.include(name)
local f = assert(loadfile(env.GLUON_TARGETSDIR .. '/' .. name))
setfenv(f, funcs)
return f()
end
function check_devices()
function M.check_devices()
local device_list = {}
for device in pairs(unknown_devices) do
table.insert(device_list, device)
@ -260,3 +284,6 @@ function check_devices()
os.exit(1)
end
end
return M

View File

@ -1,4 +1,4 @@
dofile(env.GLUON_TARGETSDIR .. '/brcm2708.inc')
include 'brcm2708.inc'
device('raspberry-pi', 'rpi', {
manifest_aliases = {

View File

@ -1,4 +1,4 @@
dofile(env.GLUON_TARGETSDIR .. '/brcm2708.inc')
include 'brcm2708.inc'
device('raspberry-pi-2', 'rpi-2', {
manifest_aliases = {

View File

@ -1,3 +1,3 @@
dofile(env.GLUON_TARGETSDIR .. '/brcm2708.inc')
include 'brcm2708.inc'
device('raspberry-pi-3', 'rpi-3')

View File

@ -1,4 +1,4 @@
dofile(env.GLUON_TARGETSDIR .. '/x86.inc')
include 'x86.inc'
factory_image('x86-64', 'combined-squashfs', '.img.gz')
factory_image('x86-64', 'combined-squashfs', '.vdi')

View File

@ -1,4 +1,4 @@
dofile(env.GLUON_TARGETSDIR .. '/x86.inc')
include 'x86.inc'
factory_image('x86-generic', 'combined-squashfs', '.img.gz')
factory_image('x86-generic', 'combined-squashfs', '.vdi')