Skip to content
Snippets Groups Projects
  • Matthias Schiffer's avatar
    9e23534e
    build: rework config generation · 9e23534e
    Matthias Schiffer authored
    So far, we were using a sort operation on the generated .config to
    implement precedence of =y packages over =m, and =m over unset.
    Unfortunately, this sort not only used for packages, but for all config
    lines. This made it impossible to override settings from targets/generic
    in a target config when the new setting was sorted before the generic
    setting.
    
    To fix this, track configurations by their keys, so we can properly
    override config keys that were set before. Value-based precedence is
    only preserved for package configuration.
    
    The config() and try_config() calls always take key and value as
    separate arguments now. Strings are quoted automatically; the values
    true, nil and false map to y, m and unset for tristate options. config()
    can take an optional third argument to override the error message to
    display when the setting fails to apply.
    
    All existing target configs generate the same .config with the old and the
    new code. The new code is also a bit faster on targets with many devices.
    build: rework config generation
    Matthias Schiffer authored
    So far, we were using a sort operation on the generated .config to
    implement precedence of =y packages over =m, and =m over unset.
    Unfortunately, this sort not only used for packages, but for all config
    lines. This made it impossible to override settings from targets/generic
    in a target config when the new setting was sorted before the generic
    setting.
    
    To fix this, track configurations by their keys, so we can properly
    override config keys that were set before. Value-based precedence is
    only preserved for package configuration.
    
    The config() and try_config() calls always take key and value as
    separate arguments now. Strings are quoted automatically; the values
    true, nil and false map to y, m and unset for tristate options. config()
    can take an optional third argument to override the error message to
    display when the setting fails to apply.
    
    All existing target configs generate the same .config with the old and the
    new code. The new code is also a bit faster on targets with many devices.
target_config_lib.lua 4.84 KiB
-- Split a string into words
local function split(s)
	local ret = {}
	for w in string.gmatch(s, '%S+') do
		table.insert(ret, w)
	end
	return ret
end

-- Strip leading '-' character
local function strip_neg(s)
	if string.sub(s, 1, 1) == '-' then
		return string.sub(s, 2)
	else
		return s
	end
end

-- Add an element to a list, removing duplicate entries and handling negative
-- elements prefixed with a '-'
local function append_to_list(list, item, keep_neg)
	local match = strip_neg(item)
	local ret = {}
	for _, el in ipairs(list) do
		if strip_neg(el) ~= match then
			table.insert(ret, el)
		end
	end
	if keep_neg ~= false or string.sub(item, 1, 1) ~= '-' then
		table.insert(ret, item)
	end
	return ret
end

local function compact_list(list, keep_neg)
	local ret = {}
	for _, el in ipairs(list) do
		ret  = append_to_list(ret, el, keep_neg)
	end
	return ret
end

return function()
	local lib = dofile('scripts/target_lib.lua')
	local env = lib.env

	local target = env.GLUON_TARGET

	assert(target)
	assert(env.BOARD)
	assert(env.SUBTARGET)

	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_vars(var)
		return lib.exec_capture_raw(string.format([[
	MAKEFLAGS= make print _GLUON_SITE_VARS_=%s --no-print-directory -s -f - <<'END_MAKE'
include $(GLUON_SITEDIR)/site.mk

print:
	echo -n '$(_GLUON_SITE_VARS_)'
END_MAKE
		]], lib.escape(var)))
	end