-
Notifications
You must be signed in to change notification settings - Fork 0
/
jmakebuilder.c
310 lines (256 loc) · 8.24 KB
/
jmakebuilder.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#define MAX_FILES 1000
#define MAX_FILE_LEN 100
#define MAX_PATH_LEN 1000
#define MAX_ALIAS_LEN 100
typedef struct
{
char name [MAX_FILE_LEN];
char path [MAX_PATH_LEN];
char type;
} file_t;
typedef struct
{
char alias[MAX_ALIAS_LEN];
char path[MAX_PATH_LEN];
} route_t;
char * get_path_alias (route_t *routes, int *n_routes, char *path)
{
// Obtener el nombre de la última carpeta en la ruta
char *last_folder = strrchr(path, '/');
if (last_folder == NULL)
{
last_folder = path;
}
else
{
last_folder++;
}
// Crear el alias
char alias[MAX_ALIAS_LEN];
sprintf(alias, "DIR-%04d-VAR", *n_routes);
// Verificar si la ruta ya existe en el array de rutas
for (int i = 0; i < *n_routes; i++)
{
if (strcmp(routes[i].path, path) == 0)
{
return routes[i].alias; // La ruta ya existe, devolver el alias correspondiente
}
}
// Si la ruta no existe, añadirla al array de rutas
if (*n_routes < MAX_PATH_LEN)
{
strcpy(routes[*n_routes].alias, alias);
strcpy(routes[*n_routes].path, path);
(*n_routes)++;
return routes[*n_routes].alias;
}
else
{
printf("Se ha alcanzado el límite máximo de rutas.\n");
exit(EXIT_FAILURE);
}
}
void list_files(char * path, file_t * files, int * n_files)
{
DIR * dir;
struct dirent * ent;
char filepath [MAX_PATH_LEN];
dir = opendir (path);
if (dir == NULL)
{
perror ("opendir");
exit (EXIT_FAILURE);
}
while ((ent = readdir(dir)) != NULL)
{
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
{
continue;
}
snprintf(filepath, MAX_PATH_LEN, "%s/%s", path, ent->d_name);
if (ent->d_type == DT_DIR)
{
list_files(filepath, files, n_files); // Si es un directorio, llamar recursivamente a la función
}
else if (ent->d_type == DT_REG)
{
char* ext = strrchr(ent->d_name, '.'); // Obtener la extensión del archivo
if (ext != NULL)
{
if ((strcmp(ext, ".c") == 0) || (strcmp(ext, ".h") == 0) || (strcmp(ext, ".cpp") == 0))
{
if (*n_files < MAX_FILES)
{
char * file = strrchr(filepath, '/');
long length = (long)file - (long)filepath;
strncpy(files[*n_files].path, filepath, length);
strcpy(files[*n_files].name, ent->d_name);
if (strcmp(ext, ".c") == 0)
files[*n_files].type = 'c';
else if (strcmp(ext, ".h") == 0)
files[*n_files].type = 'h';
else if (strcmp(ext, ".cpp") == 0)
files[*n_files].type = 'p';
else
files[*n_files].type = 'x';
(*n_files)++;
}
else
{
printf("Se ha alcanzado el límite máximo de archivos.\n");
closedir(dir);
exit(EXIT_FAILURE);
}
}
}
}
}
closedir(dir);
}
int create_make (char *path, route_t *routes, int *n_routes, file_t * files, int * n_files)
{
FILE * make_file;
char make_path [MAX_PATH_LEN + 25];
char * alias;
sprintf(make_path, "%s/%s", path, "Makefile.temp");
make_file = fopen (make_path, "w");
fprintf (make_file, "####################");
fprintf (make_file, "\n# DEFINITIONS");
fprintf (make_file, "\n####################");
fprintf (make_file, "\n");
fprintf (make_file, "\n# MAIN CONFIGURATION");
fprintf (make_file, "\nCOMPILER-VAR = g++");
fprintf (make_file, "\nCFLAGS-VAR =");
for (int i = 0; i < *n_routes; i++)
{
fprintf (make_file, " -I$(%s)", routes[i].alias);
}
fprintf (make_file, "\nARMGCC_DIR= ~/toolchain/var-mcuxpresso/gcc-arm-none-eabi-10.3-2021.10");
fprintf (make_file, "\n# gcc -B $ARMGCC_DIR archivo.c -o archivo_compilado");
fprintf (make_file, "\n");
fprintf (make_file, "\n# EXTERNAL LIBRARIES IF NEEDED");
fprintf (make_file, "\nLIBS-VAR = # -lm");
fprintf (make_file, "\nDIR-LIBS-VAR = ./lib");
fprintf (make_file, "\n");
fprintf (make_file, "\nTARGET-VAR = exe");
fprintf (make_file, "\n");
fprintf (make_file, "\n# DEBUG CONFIGURATION");
fprintf (make_file, "\n# 0: NO DEBUG");
fprintf (make_file, "\n# 1: MINIMAL");
fprintf (make_file, "\n# 2: DEFAULT (IF EMPTY)");
fprintf (make_file, "\n# 3: MAXIMAL");
fprintf (make_file, "\nDEBUG = 3");
fprintf (make_file, "\n");
fprintf (make_file, "\n# COMPILATION PATHS");
fprintf (make_file, "\nDIR-TARGET-VAR = ./deploy");
fprintf (make_file, "\nDIR-OBJ-VAR = %s/.obj", path);
for (int i = 0; i < *n_routes; i++)
fprintf (make_file, "\n%s = %s", routes[i].alias, routes[i].path);
fprintf (make_file, "\n");
fprintf (make_file, "\n####################");
fprintf (make_file, "\n# ELEMENTS");
fprintf (make_file, "\n####################");
fprintf (make_file, "\n");
fprintf (make_file, "\n# LIST OF HEADER FILES");
fprintf (make_file, "\nDEPS-VAR =");
for (int i = 0; i < *n_files; i++)
if (files[i].type == 'h')
{
alias = get_path_alias (routes, n_routes, files[i].path);
fprintf (make_file, " \\\n\t$(%s)/%s", alias, files[i].name);
}
fprintf (make_file, "\n");
fprintf (make_file, "\n# LIST OF SOURCE FILES");
fprintf (make_file, "\nSRC-VAR =");
for (int i = 0; i < *n_files; i++)
if ((files[i].type == 'c') || (files[i].type == 'p'))
fprintf (make_file, " \\\n\t%s", files[i].name);
fprintf (make_file, "\n");
fprintf (make_file, "\n# LIST OF OBJECT FILES");
fprintf (make_file, "\nOBJ-VAR = $(patsubst %%.c,$(DIR-OBJ-VAR)/%%.o,$(SRC-VAR)) $(patsubst %%.cpp,$(DIR-OBJ-VAR)/%%.o,$(SRC-VAR))");
fprintf (make_file, "\n");
fprintf (make_file, "\n####################");
fprintf (make_file, "\n# MAKE RULES");
fprintf (make_file, "\n####################");
fprintf (make_file, "\n");
fprintf (make_file, "\n# DEFAULT RULE FOR MAKE");
fprintf (make_file, "\n.PHONY: all");
fprintf (make_file, "\nall: $(DIR-TARGET-VAR)/$(TARGET-VAR)");
fprintf (make_file, "\n");
fprintf (make_file, "\n# MAIN COMPILATION");
fprintf (make_file, "\n$(DIR-TARGET-VAR)/$(TARGET-VAR): $(OBJ-VAR)");
fprintf (make_file, "\n\techo \"Compilation\"");
fprintf (make_file, "\n\tmkdir -p $(DIR-TARGET-VAR)");
fprintf (make_file, "\n\t$(COMPILER-VAR) -g$(DEBUG) -o $@ $^ $(DEPS-VAR) $(CFLAGS-VAR) $(LIBS-VAR)");
fprintf (make_file, "\n");
fprintf (make_file, "\n# OBJECT COMPILATION");
for (int i = 0; i < *n_files; i++)
if ((files[i].type == 'c') || (files[i].type == 'p'))
{
fprintf (make_file, "\n");
alias = get_path_alias (routes, n_routes, files[i].path);
char * file = strrchr(files[i].name, '.');
long length = (long)file - (long)&files[i].name;
char name_aux [MAX_FILE_LEN] = "\0";
strncpy(name_aux, files[i].name, length);
fprintf (make_file, "\n$(DIR-OBJ-VAR)/%s.o: $(%s)/%s $(DEPS-VAR)", name_aux, alias, files[i].name);
fprintf (make_file, "\n\tmkdir -p $(DIR-OBJ-VAR)");
fprintf (make_file, "\n\t$(COMPILER-VAR) -g$(DEBUG) -c -o $@ $< $(CFLAGS-VAR)");
}
fclose (make_file);
return 0;
}
int main (int argc, char** argv)
{
char * path;
if (argc < 2)
{
printf("Uso: %s <ruta de la carpeta>\n", argv[0]);
return EXIT_FAILURE;
}
path = argv[1];
file_t files [MAX_FILES];
int n_files = 0;
list_files(path, files, &n_files);
route_t routes[MAX_PATH_LEN];
int n_routes = 0;
for (int i = 0; i < n_files; i++)
{
get_path_alias (routes, &n_routes, files[i].path);
}
printf ("\nLibrerias detectadas:");
for (int i = 0; i < n_files; i++)
{
if (files[i].type == 'h')
{
printf ("\n-- .h --> %s/%s", files[i].path ,files[i].name);
char *alias = get_path_alias (routes, &n_routes, files[i].path);
}
}
printf ("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
printf ("\nCodigo .c detectado:");
for (int i = 0; i < n_files; i++)
{
if (files[i].type == 'c')
printf ("\n-- .c --> %s/%s", files[i].path ,files[i].name);
if (files[i].type == 'p')
printf ("\n- .cpp -> %s/%s", files[i].path ,files[i].name);
}
printf ("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
printf ("\nOtros:");
for (int i = 0; i < n_files; i++)
{
if (files[i].type == 'x')
printf ("\n-- xx --> %s/%s", files[i].path ,files[i].name);
}
for (int i = 0; i < n_routes; i++)
{
printf ("\n--> %s + %s", routes[i].alias, routes[i].path);
}
create_make (path, routes, &n_routes, files, &n_files);
printf ("\n");
}