-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.r
127 lines (118 loc) · 4.18 KB
/
compile.r
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
#!/usr/bin/Rscript
# 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 <- file.path(DIR, "include")
SRC <- file.path(DIR, "src")
SRC_TYPE <- file.path(SRC, "type")
TST <- file.path(DIR, "tests")
LIB <- file.path(DIR, "lib")
CC <- "cc"
CFLAGS <- "-std=c89 -Wall -pedantic -fPIC"
CSFLAGS <- "-shared"
MODE <- "debug"
if (MODE == "debug") {
CFLAGS <- paste(CFLAGS, "-g -DDEBUG", sep=" ")
} else if (MODE == "release") {
CFLAGS <- paste(CFLAGS, "-O2", sep=" ")
}
# package shared library
AR <- "ar"
ARFLAGS <- "-rcs"
# get all source files from subdirectories
CFILES <- lapply(list.files(SRC, pattern="^[^\\.].*?\\.c$", recursive=TRUE, no..=TRUE), function(x) { file.path(SRC, x) })
TEST_FILES <- lapply(list.files(TST, pattern="*\\.c$", no..=TRUE), function(x) { file.path(TST, x) })
if (.Platform$OS.type == "windows") {
# target files
LIBSHARED <- file.path(LIB, paste("lib", PRO, ".dll", sep=""))
LIBSTATIC <- file.path(LIB, paste("lib", PRO, ".lib", sep=""))
# test files
TSTSUFFIX <- ".exe"
} else {
# target files
LIBSHARED <- file.path(LIB, paste("lib", PRO, ".so", sep=""))
LIBSTATIC <- file.path(LIB, paste("lib", PRO, ".a", sep=""))
# test files
TSTSUFFIX <- ""
}
args <- commandArgs(trailingOnly = TRUE)
script_name <- unlist(strsplit(commandArgs()[4], split="="))[2]
if (length(args) == 0) {
if (!file.exists(LIB)) {
dir.create(LIB)
}
OFILES <- character()
for (file in CFILES) {
obj <- gsub("\\.c$", ".o", file)
dep <- gsub("\\.c$", ".d", file)
print(sprintf("compile %s to %s", file, obj))
system(sprintf("%s %s -I%s -I%s -c %s -o %s -MD -MF %s", CC, CFLAGS, INC, SRC_TYPE, file, obj, dep))
OFILES <- append(OFILES, obj, after=length(OFILES))
}
print(sprintf("compile %s", LIBSHARED))
system(sprintf("%s %s -o %s %s", CC, CSFLAGS, LIBSHARED, paste(OFILES, collapse=" ")))
print(sprintf("compile %s", LIBSTATIC))
system(sprintf("%s %s %s %s", AR, ARFLAGS, LIBSTATIC, paste(OFILES, collapse=" ")))
} else if (args[1] == "test") {
for (file in TEST_FILES) {
TSTFILE <- file
TSTTARGET <- gsub("\\.c$", TSTSUFFIX, TSTFILE)
print(sprintf("compile %s to %s", TSTFILE, TSTTARGET))
system(sprintf("%s %s -I%s -o %s %s -L%s -static -l%s -lm", CC, CFLAGS, INC, TSTTARGET, TSTFILE, LIB, PRO))
print(sprintf("test %s", TSTTARGET))
system(TSTTARGET)
}
} else if (args[1] == "clean") {
for (file in CFILES) {
obj <- gsub("\\.c$", ".o", file)
print(sprintf("clean %s", obj))
unlink(file.path(SRC, obj), force=TRUE)
dep <- gsub("\\.c$", ".d", file)
print(sprintf("clean %s", dep))
unlink(file.path(SRC, dep), force=TRUE)
}
print(sprintf("clean %s", LIBSTATIC))
unlink(LIBSTATIC, force=TRUE)
print(sprintf("clean %s", LIBSHARED))
unlink(LIBSHARED, force=TRUE)
for (file in TEST_FILES) {
TSTFILE <- file
TSTTARGET <- gsub("\\.c$", TSTSUFFIX, TSTFILE)
print(sprintf("clean %s", TSTTARGET))
unlink(TSTTARGET, force=TRUE)
}
} else if (args[1] == "distclean") {
for (file in CFILES) {
obj <- gsub("\\.c$", ".o", file)
print(sprintf("clean %s", obj))
unlink(file.path(SRC, obj), force=TRUE)
dep <- gsub("\\.c$", ".d", file)
print(sprintf("clean %s", dep))
unlink(file.path(SRC, dep), force=TRUE)
}
print(sprintf("clean %s", LIBSTATIC))
unlink(LIBSTATIC, force=TRUE)
print(sprintf("clean %s", LIBSHARED))
unlink(LIBSHARED, force=TRUE)
print(sprintf("clean %s", LIB))
unlink(LIB, force=TRUE)
for (file in TEST_FILES) {
TSTFILE <- file
TSTTARGET <- gsub("\\.c$", TSTSUFFIX, TSTFILE)
print(sprintf("clean %s", TSTTARGET))
unlink(TSTTARGET, force=TRUE)
}
} else if (args[1] == "help") {
print(sprintf("%s <target>", script_name))
print("<target>: ")
print(" compile cgraph")
print(" test test cgraph")
print(" clean clean all the generated files")
print(" distclean clean all the generated files and directories")
print(" help commands to this program")
} else {
print(sprintf("%s is an unsupported command", args[1]))
print(sprintf("use \"%s help\" to know all supported commands", script_name))
}