xref: /freebsd/release/scripts/pkgbase-stage.lua (revision 6fa24b29d8ff9af93cd249520b648cc745b1ef60)
1#!/usr/libexec/flua
2
3-- SPDX-License-Identifier: BSD-2-Clause
4--
5-- Copyright(c) 2025 The FreeBSD Foundation.
6--
7-- This software was developed by Isaac Freund <ifreund@freebsdfoundation.org>
8-- under sponsorship from the FreeBSD Foundation.
9
10-- Run a command using the OS shell and capture the stdout
11-- Strips exactly one trailing newline if present, does not strip any other whitespace.
12-- Asserts that the command exits cleanly
13local function capture(command)
14	local p = io.popen(command)
15	local output = p:read("*a")
16	assert(p:close())
17	-- Strip exactly one trailing newline from the output, if there is one
18	return output:match("(.-)\n$") or output
19end
20
21local function append_list(list, other)
22	for _, item in ipairs(other) do
23		table.insert(list, item)
24	end
25end
26
27-- Returns a list of packages to be included in the given media
28local function select_packages(pkg, media, all_libcompats)
29	local components = {
30		kernel = {},
31		kernel_dbg = {},
32		base = {},
33		base_dbg = {},
34		src = {},
35		tests = {},
36	}
37
38	for compat in all_libcompats:gmatch("%S+") do
39		components["lib" .. compat] = {}
40		components["lib" .. compat .. "_dbg"] = {}
41	end
42
43	local rquery = capture(pkg .. "rquery -U -r FreeBSD-base %n")
44	for package in rquery:gmatch("[^\n]+") do
45		if package == "FreeBSD-src" or package:match("^FreeBSD%-src%-.*") then
46			table.insert(components["src"], package)
47		elseif package == "FreeBSD-tests" or package:match("^FreeBSD%-tests%-.*") then
48			table.insert(components["tests"], package)
49		elseif package:match("^FreeBSD%-kernel%-.*") and
50			package ~= "FreeBSD-kernel-man"
51		then
52			-- Kernels other than FreeBSD-kernel-generic are ignored
53			if package == "FreeBSD-kernel-generic" then
54				table.insert(components["kernel"], package)
55			elseif package == "FreeBSD-kernel-generic-dbg" then
56				table.insert(components["kernel_dbg"], package)
57			end
58		elseif package:match(".*%-dbg$") then
59			table.insert(components["base_dbg"], package)
60		else
61			local found = false
62			for compat in all_libcompats:gmatch("%S+") do
63				if package:match(".*%-dbg%-lib" .. compat .. "$") then
64					table.insert(components["lib" .. compat .. "_dbg"], package)
65					found = true
66					break
67				elseif package:match(".*%-lib" .. compat .. "$") then
68					table.insert(components["lib" .. compat], package)
69					found = true
70					break
71				end
72			end
73			if not found then
74				table.insert(components["base"], package)
75			end
76		end
77	end
78	assert(#components["kernel"] == 1)
79	assert(#components["base"] > 0)
80
81	local selected = {}
82	if media == "disc" then
83		append_list(selected, components["base"])
84		append_list(selected, components["kernel"])
85		append_list(selected, components["kernel_dbg"])
86		append_list(selected, components["src"])
87		append_list(selected, components["tests"])
88		for compat in all_libcompats:gmatch("%S+") do
89			append_list(selected, components["lib" .. compat])
90		end
91	else
92		assert(media == "dvd")
93		append_list(selected, components["base"])
94		append_list(selected, components["base_dbg"])
95		append_list(selected, components["kernel"])
96		append_list(selected, components["kernel_dbg"])
97		append_list(selected, components["src"])
98		append_list(selected, components["tests"])
99		for compat in all_libcompats:gmatch("%S+") do
100			append_list(selected, components["lib" .. compat])
101			append_list(selected, components["lib" .. compat .. "_dbg"])
102		end
103	end
104
105	return selected
106end
107
108local function main()
109	-- Determines package subset selected
110	local media = assert(arg[1])
111	assert(media == "disc" or media == "dvd")
112	-- Local repository to fetch from
113	local source = assert(arg[2])
114	-- Directory to create new repository
115	local target = assert(arg[3])
116	-- =hitespace separated list of all libcompat names (e.g. "32")
117	local all_libcompats = assert(arg[4])
118
119	assert(os.execute("mkdir -p pkgbase-repo-conf"))
120	local f <close> = assert(io.open("pkgbase-repo-conf/FreeBSD-base.conf", "w"))
121	assert(f:write(string.format([[
122	FreeBSD-base: {
123	  url: "file://%s",
124	  enabled: yes
125	}
126	]], source)))
127	assert(f:close())
128
129	local pkg = "pkg -o ASSUME_ALWAYS_YES=yes -o IGNORE_OSVERSION=yes " ..
130	    "-o INSTALL_AS_USER=1 -o PKG_DBDIR=./pkgdb -R ./pkgbase-repo-conf "
131
132	assert(os.execute(pkg .. "update"))
133
134	local packages = select_packages(pkg, media, all_libcompats)
135
136	assert(os.execute(pkg .. "fetch -o " .. target .. " " .. table.concat(packages, " ")))
137	assert(os.execute(pkg .. "repo " .. target))
138end
139
140main()
141