aboutsummaryrefslogtreecommitdiff
path: root/bldit.lua
blob: 6750e84457015554ae230e222d4d5224df847f77 (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
--[[

  pkgit - package it!

  Copyright (C) 2026 dacctal
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <https://www.gnu.org/licenses/>.

]]

local bldit = {}
bldit.version = "2.0.0"
bldit.package_version = bldit.version

-- fetchers; entirely made by the developer --
local fetchers = {}
fetchers.git = {
	get_source = function(url, version) return pkgit.run({
		"git clone "..url.." --branch "..version
	}) end,
	get_version = function(url) return io.popen(
		"git -c 'versionsort.suffix=-' \
		ls-remote --exit-code --refs --sort='version:refname' \
		--tags "..url.." '*.*.*' \
		| tail --lines=1 \
		| cut --delimiter='/' --fields=3"
	):read("*a") end,
}
-- fetchers; entirely made by the developer --

-- dependencies; completely optional --
bldit.dependencies = {
	luajit = {
		url = "https://luajit.org/git/luajit.git",
		version = "v2.1",
		fetcher = fetchers.git,
		recipe = bldit.recipe
	}
}
-- dependencies; completely optional --

bldit.opts = {}
if user == nil then
	bldit.opts.prefix = os.getenv("HOME").."/.local"
else
	bldit.opts.prefix = user.dirs.prefix
end
bldit.opts.CC = os.getenv("CC")
if bldit.opts.CC == nil then bldit.opts.CC = "cc" end
bldit.opts.LDFLAGS = os.getenv("LDFLAGS")
if bldit.opts.LDFLAGS == nil then bldit.opts.LDFLAGS = "" end
bldit.opts.CFLAGS = os.getenv("CFLAGS")
if bldit.opts.CFLAGS == nil then
	bldit.opts.CFLAGS = "$(pkg-config --cflags luajit) -I./include "
	.."-Wno-format-truncation -std=c99 -DVERSION='\""
	..bldit.version.."\"'"
else bldit.opts.CFLAGS = bldit.opts.CFLAGS
	.." $(pkg-config --cflags luajit) -I./include"
	.." -Wno-format-truncation -std=c99 -DVERSION='\""
	..bldit.version.."\"'"
end

local function build_cmd()
	return "mkdir -p obj \n"
		.."for f in src/*.c; do \\\n"
		.."echo \"\tCC\t$f\" \n"
		..bldit.opts.CC.." -c \"$f\" -o \"obj/$(basename \"${f%.c}\").o\" \\\n"
		..bldit.opts.CFLAGS.." || exit 1 \n"
		.."done \n"
		..bldit.opts.CC.." "..bldit.opts.CFLAGS
		.." $(pkg-config --libs luajit) "
		..bldit.opts.LDFLAGS.." obj/*.o -o pkgit"
end

local install_cmd = "echo \"\tINSTALL\t\""..bldit.opts.prefix.."\n"
	.."install -d "
	..bldit.opts.prefix.."/bin\n"
	.."install -m 755 pkgit "
	..bldit.opts.prefix.."/bin/pkgit\n"
	.."install -d "
	..bldit.opts.prefix.."/share/man/man1\n"
	.."install -m 644 docs/pkgit.1 "
	..bldit.opts.prefix.."/share/man/man1/pkgit.1"

local remove_cmd = "echo \"\tUNINSTALL\t\""..bldit.opts.prefix.."\n"
	.."rm -f "..bldit.opts.prefix.."/bin/pkgit \n"
	.."rm -f "..bldit.opts.prefix.."/share/man/man1/pkgit.1"

bldit.recipe = {
	clean = function()
		print("\tCLEAN")
		return os.execute("rm -rf obj && rm -f pkgit")
	end,

	build = function()
		return os.execute(build_cmd())
	end,

	gdb = function()
		bldit.opts.CFLAGS = bldit.opts.CFLAGS
			.." -g -O0 -Wall -Wextra -Werror -Wvla -pedantic"
		return os.execute(build_cmd())
	end,

	debug = function()
		bldit.opts.CFLAGS = bldit.opts.CFLAGS
			.." -g -O0 -Wall -Wextra -Werror -Wvla -pedantic -fsanitize=address"
		bldit.opts.LDFLAGS = bldit.opts.LDFLAGS
			.." -fsanitize=address"
		return os.execute(build_cmd())
	end,

	install = function()
		return os.execute(install_cmd)
	end,

	remove = function()
		return os.execute(remove_cmd)
	end,

	defconfig = function()
		return os.execute("if [ \"$(id -u)\" -eq 0 ]; then \\\n"
		.."dir=/etc/pkgit; else dir=\"$HOME/.config/pkgit\"; fi; \\\n"
		.."echo \"installing default config to $dir ...\"; \\\n"
		.."mkdir -p \"$dir/\"; \\\n"
		.."cp -r ./config/* \"$dir/\"; \\\n"
		.."echo 'default config installed'")
	end,

	rmconfig = function()
		return os.execute("if [ \"$(id -u)\" -eq 0 ]; then "
		.."dir=/etc/pkgit; else dir=\"$HOME/.config/pkgit\"; fi; \\\n"
		.."echo \"removing config from $dir ...\"; \\\n"
		.."rm -rf \"$dir/\"; \\\n"
		.."echo 'config successfully removed'")
	end,
}

return bldit