-
Notifications
You must be signed in to change notification settings - Fork 29
/
fix_make.c
149 lines (139 loc) · 3.36 KB
/
fix_make.c
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
/*
* Use this program with caution.
* Please make sure what will happen and how to avoid that
* before running this program to rectify any Makefile's.
*
* libwidget.so and libdata_structures.so have mutual dependency,
* and this program will add BOTH -lwidget and -ldata_structures to
* any Makefile that already includes EITHER of them, resulting in
* util/data_structures/Makefile to depend on the library itself is
* going to generate. The same happens on widget/Makefile.
*
* Anyway, the correct way to use this program is:
* #!/bin/bash
* find . -name 'Makefile' -exec bash ./fix_make {} \;
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
static void trim(char *line)
{
int len = strlen(line);
while(len && isspace(line[len-1])) len--;
line[len]=0;
}
const char *libs[] = {"widget", "application", "server_lib", "graph", "client_lib", "geometry", "event", "comm", "config_parser", "data_structures", "color", "c"};
int app[20];
const int std[]={0,1,0,2,3,4,5,6,7,8,9,10,11};
const int depends[20][4]={
{1,5,8,9},
{0,4,7,9},
{3,5,7},
{5,10},
{7},
{0},
{7,9},
{9},
{9},
{0},
{0},
{0},
};
const int depcnt[20]={4,4,3,2,1,0,2,1,1,0,0,0};
void mark_depend(int x)
{
int i;
if(app[x]>0) return;
app[x]++;
for(i=0; i<depcnt[x]; i++) {
mark_depend(depends[x][i]);
}
}
void record_app(char *token)
{
unsigned int i;
if(token==NULL) {
memset(app, 0, sizeof(app));
return;
}
for(i=0; i<sizeof(libs)/sizeof(char*); i++) {
if(!strcmp(token, libs[i])) {
mark_depend(i);
}
}
}
void flush_records(char *new)
{
unsigned int i;
for(i=0; i<sizeof(std)/sizeof(int); i++) {
if(app[std[i]]) {
strcat(new, " -l");
strcat(new, libs[std[i]]);
}
}
record_app(NULL);
}
void process_file(FILE *fin, FILE *fout)
{
static char line[100000], bkp[100000], new[100000];
while(fgets(line, 100000, fin)) {
char *token;
char *nonspace=line;
trim(line);
if(line[0]==0) {
fprintf(fout, "\n");
continue;
}
strcpy(bkp, line);
new[0]=0;
while(isspace(*nonspace)) {
strncat(new, nonspace++, 1);
}
token = strtok(nonspace, " \n\t");
strcat(new, token);
record_app(NULL);
while((token=strtok(NULL, " \n\t"))) {
if(!strncmp(token, "-l", 2)) {
if(!strcmp(token, "-lpthread")) {
strcat(new, " ");
strcat(new, token);
} else {
record_app(token+2);
}
} else {
flush_records(new);
strcat(new, " ");
strcat(new, token);
}
}
flush_records(new);
/*puts(bkp);
puts(new);
fgets(line, 10, stdin);*/
fprintf(fout, "%s\n", new);
}
}
void copy_file(FILE *fout, char *tar)
{
int ch;
rewind(fout);
while((ch=fgetc(fout))!=EOF) {
fputc(ch, stdout);
}
}
int main(int argc, char *argv[])
{
FILE *fin, *fout;
if(argc!=2) {
return 1;
}
fin = fopen(argv[1], "r");
fout = tmpfile();
process_file(fin, fout);
copy_file(fout, argv[1]);
fclose(fin);
fclose(fout);
return 0;
}