-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.jl
138 lines (128 loc) · 3.59 KB
/
compile.jl
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
#!/usr/bin/julia
# Date : 2022-07-01
# A script to compile Library cgraph in Unix-like and Windows Platforms
# gets source files iteratively from Directory src
PRO = "cgraph"
DIR = "."
INC = joinpath(DIR, "include")
SRC = joinpath(DIR, "src")
SRC_TYPE = joinpath(SRC, "type")
TST = joinpath(DIR, "tests")
LIB = joinpath(DIR, "lib")
CC = "cc"
CFLAGS = split("-std=c89 -Wall -pedantic -fPIC", " ")
CSFLAGS = "-shared"
MODE = "debug"
if MODE == "debug"
push!(CFLAGS, "-g")
push!(CFLAGS, "-DDEBUG")
elseif MODE == "release"
push!(CFLAGS, "-static")
push!(CFLAGS, "-O2")
end
# package shared library
AR = "ar"
ARFLAGS = "-rcs"
if Sys.iswindows()
# target files
LIBSHARED = joinpath(LIB, "lib$PRO.dll")
LIBSTATIC = joinpath(LIB, "lib$PRO.lib")
# test files
TSTFILE = joinpath(TST, "$PRO.c")
TSTTARGET = joinpath(TST, "$PRO.exe")
else
# target files
LIBSHARED = joinpath(LIB, "lib$PRO.so")
LIBSTATIC = joinpath(LIB, "lib$PRO.a")
# test files
TSTFILE = joinpath(TST, "$PRO.c")
TSTTARGET = joinpath(TST, PRO)
end
# source files
# get all subdirectories
function getsubdirs(path, dirs)
for item in readdir(path)
subpath = joinpath(path, item)
if isdir(subpath) && nothing == match(r"^\.", item)
push!(dirs, subpath)
getsubdirs(subpath, dirs)
end
end
end
SRCS = []
getsubdirs(SRC, SRCS)
# get all source files from subdirectories
CFILES = []
for dir in SRCS
for item in readdir(dir)
subpath = joinpath(dir, item)
if isfile(subpath) && nothing != match(r"^((?!\.).)*\.c$", item)
push!(CFILES, subpath)
end
end
end
args = ARGS
if length(args) == 0
mkpath(LIB)
OFILES = []
for file in CFILES
obj = replace(file, r"\.c$"=>".o")
dep = replace(file, r"\.c$"=>".d")
println("compile $file to $obj")
run(`$CC $CFLAGS -I$INC -I$SRC_TYPE -c $file -o $obj -MD -MF $dep`)
push!(OFILES, obj)
end
println("compile $LIBSHARED")
run(`$CC $CSFLAGS -o $LIBSHARED $OFILES`)
println("compile $LIBSTATIC")
run(`$AR $ARFLAGS $LIBSTATIC $OFILES`)
elseif args[1] == "test"
println("compile $TSTFILE to $TSTTARGET")
run(`$CC $CFLAGS -I$INC -o $TSTTARGET $TSTFILE -L$LIB -static -l$PRO -lm`)
tst_path = joinpath(TST, "elements.csv")
println("test $TSTTARGET with $tst_path")
run(`$TSTTARGET $tst_path`)
elseif args[1] == "clean"
for file in CFILES
obj = replace(file, r"\.c$"=>".o")
println("clean ", obj)
rm(obj, force=true)
dep = replace(file, r"\.c$"=>".d")
println("clean ", dep)
rm(dep, force=true)
end
println("clean $LIBSTATIC")
rm(LIBSTATIC, force=true)
println("clean $LIBSHARED")
rm(LIBSHARED, force=true)
println("clean $TSTTARGET")
rm(TSTTARGET, force=true)
elseif args[1] == "distclean"
for file in CFILES
obj = replace(file, r"\.c$"=>".o")
println("clean ", obj)
rm(obj, force=true)
dep = replace(file, r"\.c$"=>".d")
println("clean ", dep)
rm(dep, force=true)
end
println("clean $LIBSTATIC")
rm(LIBSTATIC, force=true)
println("clean $LIBSHARED")
rm(LIBSHARED, force=true)
println("clean $LIB")
rm(LIB, force=true)
println("clean $TSTTARGET")
rm(TSTTARGET, force=true)
elseif args[1] == "help"
println("$PROGRAM_FILE <target>")
println("<target>: ")
println(" compile cgraph")
println(" test test cgraph")
println(" clean clean all the generated files")
println(" distclean clean all the generated files and directories")
println(" help commands to this program")
else
println(args[1], " is an unsupported command")
println("use \"$PROGRAM_FILE help\" to know all supported commands")
end