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
|
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
typedef struct f_comp_data {
char *f1;
char *f2;
} f_comp_data;
void *f1_newer(void *arg) {
f_comp_data *files = (f_comp_data *) arg;
char *f1 = files->f1;
char *f2 = files->f2;
struct stat f1_attr;
struct stat f2_attr;
stat(f1, &f1_attr);
stat(f2, &f2_attr);
int diff = difftime(f1_attr.st_mtime, f2_attr.st_mtime);
int ret = 0;
if (diff <= 0) {
pthread_exit(&ret);
}
ret = 1;
pthread_exit(&ret);
}
void *create_template(void *arg) {
/* Create simple main.c or makefile boilerplate */
char *path = (char *) arg;
struct stat attr;
if (stat(path, &attr) == 0) {
return NULL;
}
int fd = creat(path, 00644);
FILE *file = fdopen(fd, "w");
switch(path[2]) {
case 'i':
fprintf(file, "#include <stdio.h>\n\nint main() {\n\tprintf(\"Hello world\\n\");\n}");
break;
case 'k':
fprintf(file, "debug: main.c\n\tgcc -Wall main.c -o ./bin/debug\nrelease: main.c\n\tgcc -O2 -march=native main.c -o ./bin/release");
break;
default: break;
}
fclose(file);
return NULL;
}
void new(char *argv[]) {
char *name = argv[2];
mkdir(name, 0777);
chdir(name);
pthread_t s_thread, m_thread;
pthread_create(&s_thread, NULL, create_template, (void *) "main.c");
pthread_create(&m_thread, NULL, create_template, (void *) "makefile");
pthread_join(s_thread, NULL);
pthread_join(m_thread, NULL);
printf("Created project: %s\n", name);
exit(0);
}
int main(int argc, char *argv[]) {
/* Struct to hold files */
f_comp_data files;
files.f1 = "main.c";
files.f2 = "./bin/debug";
/* Parse args to determine which mode to run in
* Not very robust, needs improvement
*/
enum { RUN, BUILD, BUILD_RELEASE, NEW } mode = RUN;
if (argc > 1) {
switch(argv[1][0]) {
case 'r': break;
case 'b':
if (argc > 2) {
mode = BUILD_RELEASE;
files.f2 = "./bin/release";
break;
}
mode = BUILD; break;
case 'n':
new(argv); break;
default: printf("Unknown argument: '%s'\nDefaulting to 'run'\n", argv[1]);
}
}
/* Threads to:
* create makefile
* check age of source vs. bin
*/
pthread_t f_thread, check_thread;
pthread_create(&f_thread, NULL, create_template, (void *) "makefile");
pthread_create(&check_thread, NULL, f1_newer, (void *)&files);
mkdir("./bin", 0777);
pthread_join(f_thread, NULL);
/* Get value from thread which checks if the bin
* is older than the source code
*/
void *newer;
pthread_join(check_thread, &newer);
int recomp = *(int *) newer;
if (recomp) {
pid_t make_pid;
if ((make_pid = fork()) == 0) {
printf("Compiling with: \n");
switch (mode) {
case BUILD_RELEASE:
execl("/usr/bin/make", "make", "release", NULL);
break;
case BUILD:
execl("/usr/bin/make", "make", "debug", NULL);
break;
default:
execl("/usr/bin/make", "make", "debug", NULL);
}
}
if (mode == RUN) printf("\n");
waitpid(make_pid, NULL, 0);
}
else if (mode == BUILD || mode == BUILD_RELEASE) {
printf("Nothing to do: binary file already up to date with source\n");
}
if (mode == RUN) {
pid_t x_pid;
if ((x_pid = fork()) == 0) {
execl(files.f2, "tmp", NULL);
}
waitpid(x_pid, NULL, 0);
}
return 0;
}
|