summaryrefslogtreecommitdiff
path: root/pkg/mpv/gen.lua
blob: d542bcd11eb2107765b8b1458ba623bb39da7404 (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
179
180
181
182
build('awk', '$outdir/version.h', {'$srcdir/VERSION', '|', '$dir/version.awk'}, {
	expr='-f $dir/version.awk',
})

sub('tools.ninja', function()
	toolchain(config.host)
	set('srcdir', '$dir')
	exe('file2string', {'file2string.c'})
end)

cflags{
	'-Wno-deprecated-declarations',
	'-D _GNU_SOURCE',
	'-I $dir',
	'-I $outdir',
	'-I $srcdir',
	'-isystem $basedir/pkg/ffmpeg/src',
	'-isystem $builddir/pkg/ffmpeg/include',
	'-isystem $builddir/pkg/linux-headers/include',
	'-isystem $builddir/pkg/zlib/include',
}
local libs = {
	'ffmpeg/libavcodec.a.d',
	'ffmpeg/libavdevice.a.d',
	'ffmpeg/libavfilter.a.d',
	'ffmpeg/libavformat.a.d',
	'ffmpeg/libavutil.a',
	'ffmpeg/libswresample.a.d',
	'ffmpeg/libswscale.a.d',
	'zlib/libz.a',
}
pkg.deps = {
	'$outdir/version.h',
	'pkg/ffmpeg/headers',
	'pkg/ffmpeg/fetch',
	'pkg/linux-headers/headers',
	'pkg/zlib/headers',
}

build('copy', '$outdir/video/out/wayland/idle-inhibit-v1.h', '$builddir/pkg/wayland-protocols/include/idle-inhibit-unstable-v1-client-protocol.h')
build('copy', '$outdir/video/out/wayland/presentation-time.h', '$builddir/pkg/wayland-protocols/include/presentation-time-client-protocol.h')
build('copy', '$outdir/video/out/wayland/xdg-decoration-v1.h', '$builddir/pkg/wayland-protocols/include/xdg-decoration-unstable-v1-client-protocol.h')
build('copy', '$outdir/video/out/wayland/xdg-shell.h', '$builddir/pkg/wayland-protocols/include/xdg-shell-client-protocol.h')

rule('file2string', '$outdir/file2string $in >$out')
local function file2string(out, inp)
	build('file2string', '$outdir/'..out, {'$srcdir/'..inp, '|', '$outdir/file2string'})
	table.insert(pkg.deps, '$outdir/'..out)
end

file2string('input/input_conf.h', 'etc/input.conf')
file2string('player/builtin_conf.inc', 'etc/builtin.conf')
file2string('sub/osd_font.h', 'sub/osd_font.otf')
for _, f in ipairs{'defaults', 'assdraw', 'options', 'osc', 'ytdl_hook', 'stats', 'console'} do
	file2string('player/lua/'..f..'.inc', 'player/lua/'..f..'.lua')
end

local options = {}
for line in iterlines('config.h', 1) do
	local var, val = line:match('^#define ([^ ]+) ([^ ]+)')
	if var and val == '1' then
		options[var] = true
	end
end
table.insert(pkg.inputs.gen, '$dir/config.h')

-- source inclusion parser
local eval
do
	local ops = {
		{tok='||', fn=function(a, b) return a or b end, binary=true},
		{tok='&&', fn=function(a, b) return a and b end, binary=true},
		{tok='!', fn=function(a) return not a end},
	}
	local function check(x)
		if not x then
			error('invalid expression')
		end
	end
	local function helper(s, i, j)
		local result, var, val
		if s:sub(i, i) == '(' then
			i = s:find('[^%s]', i + 1)
			check(i)
			result, i = parse(s, i, 1)
			check(i and s:sub(i, i) == ')')
			return result, s:find('[^%s]', i + 1)
		end
		if j > #ops then
			local k = select(2, s:find('^[%w_.-]+', i))
			check(k)
			var = s:sub(i, k):upper():gsub('-', '_')
			i = s:find('[^%s]', k + 1)
			return options['HAVE_'..var] or false, i
		end
		local op = ops[j]
		if op.binary then
			result, i = helper(s, i, j + 1)
		end
		if i and s:sub(i, i + #op.tok - 1) == op.tok then
			i = s:find('[^%s]', i + #op.tok)
			check(i)
			val, i = helper(s, i, j)
			result = op.fn(val, result)
		elseif not op.binary then
			result, i = helper(s, i, j + 1)
		end
		return result, i
	end
	eval = function(s, i)
		result, i = helper(s, i, 1)
		check(not i)
		return result
	end
end

local srcs = {}
for line in iterlines('sources.txt', 1) do
	local i = line:find(' ', 1, true)
	local add = true
	if not i or eval(line, i + 1) then
		srcs[line:sub(1, i and i - 1)] = true
	end
end
srcs = table.keys(srcs)

if options['HAVE_ALSA'] then
	cflags{'-isystem $builddir/pkg/alsa-lib/include'}
	table.insert(libs, 'alsa-lib/libasound.a')
	table.insert(pkg.deps, 'pkg/alsa-lib/headers')
end
if options['HAVE_DRM'] then
	cflags{'-isystem $builddir/pkg/libdrm/include'}
	table.insert(libs, 'libdrm/libdrm.a')
	table.insert(pkg.deps, 'pkg/libdrm/headers')
end
if options['HAVE_LIBASS'] then
	cflags{'-isystem $builddir/pkg/libass/include'}
	table.insert(libs, 'libass/libass.a.d')
	table.insert(pkg.deps, 'pkg/libass/headers')
end
if options['HAVE_LUA'] then
	cflags{'-isystem $builddir/pkg/lua/include'}
	table.insert(libs, 'lua/liblua.a')
	table.insert(pkg.deps, 'pkg/lua/headers')
end
if options['HAVE_WAYLAND'] then
	cflags{
		'-isystem $builddir/pkg/libxkbcommon/include',
		'-isystem $builddir/pkg/wayland/include',
	}
	table.insert(libs, {
		'wayland/libwayland-client.a.d',
		'wayland/libwayland-cursor.a.d',
		'libxkbcommon/libxkbcommon.a',
	})
	table.insert(pkg.deps, {
		'$outdir/video/out/wayland/idle-inhibit-v1.h',
		'$outdir/video/out/wayland/presentation-time.h',
		'$outdir/video/out/wayland/xdg-decoration-v1.h',
		'$outdir/video/out/wayland/xdg-shell.h',
		'pkg/libxkbcommon/headers',
		'pkg/wayland/headers',
	})
end

exe('mpv', {
	'input/ipc-unix.c',
	'osdep/main-fn-unix.c',
	'osdep/subprocess-posix.c',
	'osdep/terminal-unix.c',
	'osdep/timer-linux.c',
	'ta/ta.c',
	'ta/ta_talloc.c',
	'ta/ta_utils.c',
	srcs,
	expand{'$builddir/pkg/', libs},
})
file('bin/mpv', '755', '$outdir/mpv')
man{'$dir/mpv.1'}

fetch 'git'