-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.rabs
110 lines (96 loc) · 2.74 KB
/
common.rabs
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
PLATFORM := defined("PLATFORM") or shell("uname"):trim
MACHINE := defined("MACHINE") or shell("uname -m"):trim
DEBUG := defined("DEBUG")
CC := defined("CC") or "cc"
AS := defined("AS") or CC
CFLAGS := ["-pipe"]
LDFLAGS := []
PREBUILDS := []
if defined("CFLAGS") then
CFLAGS:grow(defined("CFLAGS") / " ")
end
if defined("LDFLAGS") then
LDFLAGS:grow(defined("LDFLAGS") / " ")
end
pkgconfig := fun(Args) do
expr('pkg-config {Args}') => fun() shell('pkg-config', Args):trim
end
c_compile := fun(Source, Object) do
execute(CC, '-c', CFLAGS, '-o', Object, Source)
end
c_includes := fun(Target) do
var Lines := shell(CC, '-M -MG', CFLAGS, Target:source)
Lines := Lines:replace("\\\n ", "")
Lines := Lines[Lines:find(": ") + 2, 0]:trim
var Files := Lines / r"[^\\]( +)"
for File in Files do
File := file(File:replace("\\ ", " "))
end
check(Files)
ret Files
end
as_compile := fun(Source, Object) do
execute(AS, '-c', CFLAGS, '-o', Object, Source)
end
as_includes := fun(Target) do
ret []
end
var SourceTypes := {
"c" is [c_includes, c_compile],
"cpp" is [c_includes, c_compile],
"s" is [as_includes, as_compile]
}
c_program := fun(Executable, Objects, Libraries) do
var Sources := []
for Object in Objects or [] do
for Extension, Functions in SourceTypes do
var Source := Object % Extension
if Source:exists then
Sources:put(Source)
var Scan := Source:scan("INCLUDES")[PREBUILDS] => Functions[1]
Object[Source, Scan, PREBUILDS] => (Functions[2] !! [Source])
exit
end
end
end
Executable[Objects, Libraries, PREBUILDS] => fun(Executable) do
execute(CC, '-o', Executable, Objects, Libraries, LDFLAGS)
end
end
c_library := fun(Library, Objects, Libraries) do
var Sources := []
for Object in Objects or [] do
for Extension, Functions in SourceTypes do
var Source := Object % Extension
if Source:exists then
Sources:put(Source)
var Scan := Source:scan("INCLUDES")[PREBUILDS] => Functions[1]
Object[Source, Scan, PREBUILDS] => (Functions[2] !! [Source])
exit
end
end
end
Library[Objects, Libraries, PREBUILDS] => fun(Executable) do
:>execute('ar', 'rcs', Library, Objects)
let Script := file("script.ar"):open("w")
Script:write('CREATE ', Library, '\n')
each Libraries -> Script:write('ADDLIB ', _, '\n')
each Objects -> Script:write('ADDMOD ', _, '\n')
Script:write('SAVE\nEND\n')
Script:close
execute('ar', '-M < script.ar')
end
end
PREFIX := old or file('{getenv("DESTDIR") or ""}{defined("PREFIX") or "/usr/local"}')
INSTALL := meta("install")
install := fun(Source, Target, Mode) do
Target[Source] => fun(Target) do
print(Source, " -> ", Target, "\n")
Target:dir:mkdir
Source:copy(Target)
Mode and execute("chmod", Mode, Target)
end
INSTALL[Target]
ret Target
end
TEST := meta("test")