summarylogtreecommitdiffstats
path: root/gen-PKGBUILD.py
blob: 713f76eab4adbadc278b5939fe5aab4b75891df4 (plain)
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
from debian import deb822
import re
import gzip

header_tpl = """# Maintainer: Janusz Lewandowski <lew21@xtreeme.org>
# Autogenerated from AMD's Packages file

pkgver_base=16.20.3
pkgver_build=294842

pkgbase=amdgpu-pro-installer
pkgname={PACKAGES}
pkgver=${{pkgver_base}}.${{pkgver_build}}
pkgrel=1
arch=('x86_64')
url="http://www.amd.com"

url_ref="http://support.amd.com/en-us/kb-articles/Pages/AMDGPU-PRO-Beta-Driver-for-Vulkan-Release-Notes.aspx"
DLAGENTS="https::/usr/bin/curl --referer ${{url_ref}} -o %o %u"

source=(https://www2.ati.com/drivers/beta/amdgpu-pro_${{pkgver_base}}-${{pkgver_build}}.tar.xz)
sha256sums=('4a2988f5047e858c3c6ae22528162b2537780deeaf5dda5d8a8f61e213058a66')
"""

package_header_tpl = """
package_{NAME} () {{
	pkgdesc={DESC}
	depends={DEPENDS}
	arch=('{ARCH}')

	rm -Rf "${{srcdir}}"/{Package}_{Version}_{Architecture}
	mkdir "${{srcdir}}"/{Package}_{Version}_{Architecture}
	cd "${{srcdir}}"/{Package}_{Version}_{Architecture}
	ar x "${{srcdir}}"/amdgpu-pro-driver/{Filename}
	tar -C "${{pkgdir}}" -xf data.tar.xz
"""

package_footer = """}
"""

special_ops = {
	"amdgpu-pro-core": """
	mv ${pkgdir}/lib ${pkgdir}/usr/
	mkdir -p ${pkgdir}/etc/ld.so.conf.d/
	ln -s /usr/lib/amdgpu-pro/ld.conf ${pkgdir}/etc/ld.so.conf.d/10-amdgpu-pro.conf
	mkdir -p ${pkgdir}/etc/modprobe.d/
	ln -s /usr/lib/amdgpu-pro/modprobe.conf ${pkgdir}/etc/modprobe.d/amdgpu-pro.conf
	install=amdgpu-pro-core.install
""",
	"xserver-xorg-video-amdgpu-pro": "\tln -sfn 1.18 ${pkgdir}/usr/lib/x86_64-linux-gnu/amdgpu-pro/xorg",
}

replace_deps = {
	"libc6": None,
	"libgcc1": None,
	"libstdc++6": None,
	"libx11-6": "libx11",
	"libx11-xcb1": None,
	"libxcb-dri2-0": "libxcb",
	"libxcb-dri3-0": "libxcb",
	"libxcb-present0": "libxcb",
	"libxcb-sync1": "libxcb",
	"libxcb-glx0": "libxcb",
	"libxcb1": "libxcb",
	"libxext6": "libxext",
	"libxshmfence1": "libxshmfence",
	"libxdamage1": "libxdamage",
	"libxfixes3": "libxfixes",
	"libxxf86vm1": "libxxf86vm",
	"libudev1": "libsystemd",
	"libpciaccess0": "libpciaccess",
	"libepoxy0": "libepoxy",
	"libelf1": None, # no lib32- package in Arch, just disabling for now
	"xserver-xorg-core": "xorg-server",
	"libcunit1": "cunit",
	"libdrm-radeon1": "libdrm",
	"amdgpu-pro-firmware": "linux-firmware",
	"libssl1.0.0": "openssl",
	"zlib1g": "zlib",
}

dependency = re.compile(r"([^ ]+)(?: \((.+)\))?")

arch_map = {
	"amd64": "x86_64",
	"i386": "i686",
	"all": "any"
}

deb_archs={}

def quote(string):
	return "\"" + string.replace("\\", "\\\\").replace("\"", "\\\"") + "\""

def convertName(name):
	if info["Architecture"] == "i386" and (name not in deb_archs or "any" not in deb_archs[name]):
		return "lib32-" + name
	return name

def convertVersionSpecifier(name, spec, names):
	if name == "linux-firmware":
		return ""
	if name in names:
		return ""
	if not spec:
		return ""

	sign, spec = spec.split(" ", 1)

	spec = spec.strip()
	if ":" in spec:
		whatever, spec = spec.rsplit(":", 1)
	return sign + spec

def convertPackage(info, names):
	if info["Architecture"] == "i386":
		name = "lib32-" + info["Package"]
		arch = "x86_64"
	else:
		name = info["Package"]
		arch = arch_map[info["Architecture"]]

	try:
		deps = info["Depends"].split(", ")
	except:
		deps = []

	deps = [dependency.match(dep).groups() for dep in deps]
	deps = [(replace_deps[name] if name in replace_deps else name, version) for name, version in deps]
	deps = ["'" + convertName(name) + convertVersionSpecifier(name, version, names) + "'" for name, version in deps if name]
	deps2 = []
	for dep in deps:
		if not dep in deps2:
			deps2.append(dep)
	deps = "(" + " ".join(deps2) + ")"

	special_op = special_ops[info["Package"]] if info["Package"] in special_ops else ""

	desc = info["Description"].split("\n")
	if len(desc) > 2:
		desc = desc[0]
	else:
		desc = " ".join(x.strip() for x in desc)

	ret = package_header_tpl.format(DEPENDS=deps, NAME=name, ARCH=arch, DESC=quote(desc), **info)
	if special_op:
		ret += special_op + "\n"
	if info["Architecture"] == "i386":
		ret += "\trm -Rf ${pkgdir}/usr/share/doc ${pkgdir}/usr/include\n"
	ret += package_footer

	return ret

with gzip.open("src/amdgpu-pro-driver/Packages.gz", "r") as f:
	package_list=[]

	for info in deb822.Packages.iter_paragraphs(f):
		if info["Package"] == "amdgpu-pro-firmware":
			continue
		if info["Filename"].startswith("./dkms/dst/amdgpu-pro/"):
			continue

		if not info["Package"] in deb_archs:
			deb_archs[info["Package"]] = set()

		deb_archs[info["Package"]].add(info["Architecture"])

		package_list.append(info)

	names = ["lib32-" + info["Package"] if info["Architecture"] == "i386" else info["Package"] for info in package_list]

	print(header_tpl.format(PACKAGES="(" + " ".join(names) + ")"))

	f.seek(0)

	for info in package_list:
		print(convertPackage(info, names))