xref: /freebsd/release/scripts/pkgbase-stage.lua (revision 62d18f8c4c10a5e680b0b795040abffc7964977e)
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%-.*") then
50			-- Kernels other than FreeBSD-kernel-generic are ignored
51			if package == "FreeBSD-kernel-generic" then
52				table.insert(components["kernel"], package)
53			elseif package == "FreeBSD-kernel-generic-dbg" then
54				table.insert(components["kernel_dbg"], package)
55			end
56		elseif package:match(".*%-dbg$") then
57			table.insert(components["base_dbg"], package)
58		else
59			local found = false
60			for compat in all_libcompats:gmatch("%S+") do
61				if package:match(".*%-dbg%-lib" .. compat .. "$") then
62					table.insert(components["lib" .. compat .. "_dbg"], package)
63					found = true
64					break
65				elseif package:match(".*%-lib" .. compat .. "$") then
66					table.insert(components["lib" .. compat], package)
67					found = true
68					break
69				end
70			end
71			if not found then
72				table.insert(components["base"], package)
73			end
74		end
75	end
76	assert(#components["kernel"] == 1)
77	assert(#components["base"] > 0)
78
79	local selected = {}
80	if media == "disc" then
81		append_list(selected, components["base"])
82		append_list(selected, components["kernel"])
83		append_list(selected, components["kernel_dbg"])
84		append_list(selected, components["src"])
85		append_list(selected, components["tests"])
86		for compat in all_libcompats:gmatch("%S+") do
87			append_list(selected, components["lib" .. compat])
88		end
89	else
90		assert(media == "dvd")
91		append_list(selected, components["base"])
92		append_list(selected, components["base_dbg"])
93		append_list(selected, components["kernel"])
94		append_list(selected, components["kernel_dbg"])
95		append_list(selected, components["src"])
96		append_list(selected, components["tests"])
97		for compat in all_libcompats:gmatch("%S+") do
98			append_list(selected, components["lib" .. compat])
99			append_list(selected, components["lib" .. compat .. "_dbg"])
100		end
101	end
102
103	return selected
104end
105
106local function main()
107	-- Determines package subset selected
108	local media = assert(arg[1])
109	assert(media == "disc" or media == "dvd")
110	-- Local repository to fetch from
111	local source = assert(arg[2])
112	-- Directory to create new repository
113	local target = assert(arg[3])
114	-- =hitespace separated list of all libcompat names (e.g. "32")
115	local all_libcompats = assert(arg[4])
116
117	assert(os.execute("mkdir -p pkgbase-repo-conf"))
118	local f <close> = assert(io.open("pkgbase-repo-conf/FreeBSD-base.conf", "w"))
119	assert(f:write(string.format([[
120	FreeBSD-base: {
121	  url: "file://%s",
122	  enabled: yes
123	}
124	]], source)))
125	assert(f:close())
126
127	local pkg = "pkg -o ASSUME_ALWAYS_YES=yes -o IGNORE_OSVERSION=yes " ..
128	    "-o INSTALL_AS_USER=1 -o PKG_DBDIR=./pkgdb -R ./pkgbase-repo-conf "
129
130	assert(os.execute(pkg .. "update"))
131
132	local packages = select_packages(pkg, media, all_libcompats)
133
134	assert(os.execute(pkg .. "fetch -o " .. target .. " " .. table.concat(packages, " ")))
135	assert(os.execute(pkg .. "repo " .. target))
136end
137
138main()
139