collect-aros, genmodule: support .init_array/.fini_array sections
[AROS.git] / tools / MetaMake / project.c
blob19f72c94a66c448d300b4e9914c1d3ff65f9960d
1 /* MetaMake - A Make extension
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
4 This file is part of MetaMake.
6 MetaMake is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 MetaMake is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 //#define DEBUG_PROJECT
23 #include "config.h"
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <assert.h>
28 #ifdef HAVE_STRING_H
29 # include <string.h>
30 #else
31 # include <strings.h>
32 #endif
34 #include "project.h"
35 #include "var.h"
36 #include "mem.h"
37 #include "dep.h"
38 #include "mmake.h"
40 #if defined(DEBUG_PROJECT)
41 #define debug(a) a
42 #else
43 #define debug(v)
44 #endif
46 struct List projects;
47 static struct Project * defaultprj = NULL;
49 static void
50 readvars (struct Project * prj)
52 struct List deps;
53 struct Node * node, * next;
54 struct Dep * dep;
56 debug(printf("MMAKE:project.c->readvars(Project @ 0x%p)\n", prj));
58 if (!prj->readvars)
59 return;
61 prj->readvars = 0;
63 printf ("Read vars...\n");
65 setvar (&prj->vars, "TOP", prj->buildtop);
66 setvar (&prj->vars, "SRCDIR", prj->srctop);
67 setvar (&prj->vars, "CURDIR", "");
69 ForeachNode(&prj->globalvarfiles, node)
71 char * fn;
72 FILE * fh;
73 char line[256];
74 char * name, * value, * ptr;
76 fn = xstrdup (substvars (&prj->vars, node->name));
77 fh = fopen (fn, "r");
79 /* if the file doesn't exist execute prj->genglobalvarfile */
80 if (!fh && prj->genglobalvarfile)
82 char * gen = xstrdup (substvars (&prj->vars, prj->genglobalvarfile));
84 printf ("Generating %s...\n", fn);
86 if (!execute (prj, gen, "-", "-", ""))
88 error ("Error while creating \"%s\" with \"%s\"", fn, gen);
89 exit (10);
91 else
92 fh = fopen (fn, "r");
94 xfree (gen);
97 if (!fh)
99 error ("readvars():fopen(): Opening \"%s\" for reading", fn);
100 return;
103 xfree (fn);
105 while (fgets (line, sizeof(line), fh))
107 if (*line == '\n' || *line == '#') continue;
108 line[strlen(line)-1] = 0;
110 ptr = line;
111 while (isspace (*ptr)) ptr++;
112 name = ptr;
113 while (*ptr && !isspace(*ptr) && *ptr != ':' && *ptr != '=')
114 ptr ++;
116 if (*ptr)
117 *ptr++ = 0;
119 while (isspace(*ptr) || *ptr == ':' || *ptr == '=')
120 ptr ++;
122 value = ptr;
124 while (*ptr && *ptr != '#')
125 ptr ++;
127 *ptr = 0;
129 if (debug)
130 printf ("%s=%s\n", name, substvars (&prj->vars, value));
132 setvar (&prj->vars, name, substvars (&prj->vars, value));
135 fclose (fh);
138 /* handle prj->genmakefiledeps */
139 NewList(&deps);
140 /* algorithm has changed from:
141 * copying nodes to deps list and then generating dep nodes
142 * which will be put back to genmakefiledeps list
143 * to:
144 * generating new dep nodes and putting them into the deps list,
145 * then copy them back into genmakefiledeps list
146 * reason for change:
147 * in recent versions of gcc (>= 4.6) the first loop over all
148 * genmakefiledeps list nodes was optimized away, so deps list
149 * was always empty and the second loop never did anything */
150 ForeachNodeSafe (&prj->genmakefiledeps, node, next)
152 Remove (node);
153 dep = newdepnode (substvars (&prj->vars, node->name));
154 AddTail (&deps, dep);
155 xfree (node->name);
156 xfree (node);
159 ForeachNodeSafe (&deps, node, next)
161 Remove (node);
162 AddTail (&prj->genmakefiledeps, node);
165 if (debug)
167 printf ("project %s.genmfdeps=\n", prj->node.name);
168 printlist (&prj->genmakefiledeps);
171 if (debug)
173 printf ("project %s.vars=", prj->node.name);
174 printvarlist (&prj->vars);
178 static struct Project *
179 initproject (char * name)
181 struct Project * prj = new (struct Project);
183 memset (prj, 0, sizeof(struct Project));
185 debug(printf("MMAKE:project.c->initproject('%s')\n", name));
186 debug(printf("MMAKE:project.c->initproject: Project node @ 0x%p\n", prj));
188 if (!defaultprj)
190 prj->maketool = xstrdup ("make \"TOP=$(TOP)\" \"SRCDIR=$(SRCDIR)\" \"CURDIR=$(CURDIR)\"");
191 prj->defaultmakefilename = xstrdup ("Makefile");
192 prj->srctop = mm_srcdir;
193 prj->buildtop = mm_builddir;
194 prj->defaulttarget = xstrdup ("all");
195 prj->genmakefilescript = NULL;
196 prj->genglobalvarfile = NULL;
198 else
200 prj->maketool = xstrdup (defaultprj->maketool);
201 prj->defaultmakefilename = xstrdup (defaultprj->defaultmakefilename);
202 prj->srctop = xstrdup (defaultprj->srctop);
203 prj->buildtop = xstrdup (defaultprj->buildtop);
204 prj->defaulttarget = xstrdup (defaultprj->defaulttarget);
205 SETSTR (prj->genmakefilescript, defaultprj->genmakefilescript);
206 SETSTR (prj->genglobalvarfile, defaultprj->genglobalvarfile);
209 prj->node.name = xstrdup (name);
211 prj->readvars = 1;
213 NewList(&prj->globalvarfiles);
214 NewList(&prj->genmakefiledeps);
215 NewList(&prj->ignoredirs);
216 NewList(&prj->vars);
217 NewList(&prj->extramakefiles);
219 return prj;
222 static void
223 freeproject (struct Project * prj)
225 assert (prj);
227 cfree (prj->node.name);
228 cfree (prj->maketool);
229 cfree (prj->defaultmakefilename);
230 if (prj->srctop != mm_srcdir)
231 cfree (prj->srctop);
232 if (prj->buildtop != mm_builddir)
233 cfree (prj->buildtop);
234 cfree (prj->defaulttarget);
235 cfree (prj->genmakefilescript);
236 cfree (prj->genglobalvarfile);
238 if (prj->cache)
239 closecache (prj->cache);
241 freelist(&prj->globalvarfiles);
242 freelist (&prj->genmakefiledeps);
243 freelist (&prj->ignoredirs);
244 freevarlist (&prj->vars);
245 freelist (&prj->extramakefiles);
247 xfree (prj);
250 static void
251 callmake (struct Project * prj, const char * tname, struct Makefile * makefile)
253 static char buffer[4096];
254 const char * path = buildpath (makefile->dir);
255 int t;
257 debug(printf("MMAKE:project.c->callmake()\n"));
259 if (makefile->generated)
260 chdir (prj->buildtop);
261 else
262 chdir (prj->srctop);
263 chdir (path);
265 setvar (&prj->vars, "CURDIR", path);
266 setvar (&prj->vars, "TARGET", tname);
268 buffer[0] = '\0';
270 for (t=0; t<mflagc; t++)
272 strcat (buffer, mflags[t]);
273 strcat (buffer, " ");
276 if (strcmp (makefile->node.name, "Makefile")!=0 && strcmp (makefile->node.name, "makefile")!=0);
278 strcat (buffer, "--file=");
279 strcat (buffer, makefile->node.name);
280 strcat (buffer, " ");
283 strcat (buffer, tname);
285 if (!quiet)
286 printf ("Making %s in %s\n", tname, path);
288 if (!execute (prj, prj->maketool, "-", "-", buffer))
290 error ("Error while running make in %s", path);
291 exit (10);
296 void
297 initprojects (void)
299 char * optionfile;
300 char * home;
301 char line[256];
302 FILE * optfh = NULL;
303 struct Project * project;
305 debug(printf("MMAKE:project.c->initprojects()\n"));
307 NewList(&projects);
308 defaultprj = project = initproject ("default");
309 AddTail(&projects, project);
312 /* Try "$MMAKE_CONFIG" */
313 if ((optionfile = getenv ("MMAKE_CONFIG")))
314 optfh = fopen (optionfile, "r");
316 /* Try "$HOME/.mmake.config" */
317 if (!optfh)
319 if ((home = getenv("HOME")))
321 optionfile = xmalloc (strlen(home) + sizeof("/.mmake.config") + 1);
322 sprintf (optionfile, "%s/.mmake.config", home);
323 optfh = fopen (optionfile, "r");
324 free (optionfile);
328 /* Try with $CWD/.mmake.config" */
329 if (!optfh)
330 optfh = fopen (".mmake.config", "r");
332 /* Try with "$CWD/mmake.config */
333 if (!optfh)
334 optfh = fopen ("mmake.config", "r");
336 /* Give up */
337 if (!optfh)
339 fprintf (stderr,
340 "Please set the HOME or MMAKE_CONFIG env var (with setenv or export)\n"
342 error ("Opening mmake.config for reading");
343 exit (10);
346 while (fgets (line, sizeof(line), optfh))
348 if (*line == '\n' || *line == '#') continue;
349 line[strlen(line)-1] = 0;
351 if (*line == '[') /* look for project name */
353 char * name, * ptr;
355 name = ptr = line+1;
356 while (*ptr && *ptr != ']')
357 ptr ++;
359 *ptr = 0;
361 debug(printf("MMAKE:project.c->initprojects: Adding '%s' from MMAKE_CONFIG\n", name));
363 project = initproject (name);
365 AddTail(&projects,project);
367 else
369 char * cmd, * args, * ptr;
371 cmd = line;
372 while (isspace (*cmd))
373 cmd ++;
375 args = cmd;
376 while (*args && !isspace(*args))
378 *args = tolower (*args);
379 args ++;
381 if (*args)
382 *args++ = 0;
383 while (isspace (*args))
384 args ++;
386 ptr = args;
388 while (*ptr && *ptr != '\n')
389 ptr ++;
391 *ptr = 0;
393 if (!strcmp (cmd, "add"))
395 struct Node * n;
396 n = newnode(args);
397 AddTail(&project->extramakefiles, n);
399 else if (!strcmp (cmd, "ignoredir"))
401 struct Node * n;
402 n = newnode(args);
403 AddTail(&project->ignoredirs, n);
405 else if (!strcmp (cmd, "defaultmakefilename"))
407 SETSTR(project->defaultmakefilename,args);
409 else if (!strcmp (cmd, "top"))
411 SETSTR(project->srctop,args);
413 else if (!strcmp (cmd, "defaulttarget"))
415 SETSTR(project->defaulttarget,args);
417 else if (!strcmp (cmd, "genmakefilescript"))
419 SETSTR(project->genmakefilescript,args);
421 else if (!strcmp (cmd, "genmakefiledeps"))
423 struct Node * dep;
424 int depc, t;
425 char ** deps = getargs (args, &depc, NULL);
427 debug(printf("MMAKE/project.c: genmakefiledeps depc=%d\n", depc));
429 for (t=0; t<depc; t++)
431 dep = addnodeonce (&project->genmakefiledeps, deps[t]);
434 else if (!strcmp (cmd, "globalvarfile"))
436 struct Node *n = newnode(args);
438 if (n)
439 AddTail(&project->globalvarfiles, n);
441 else if (!strcmp (cmd, "genglobalvarfile"))
443 SETSTR(project->genglobalvarfile,args);
445 else if (!strcmp (cmd, "maketool"))
447 SETSTR(project->maketool,args);
449 else
451 setvar(&project->vars, cmd, args);
456 fclose (optfh);
458 /* Clean up memory from getargs */
459 getargs (NULL, NULL, NULL);
461 if (debug)
463 printf ("known projects: ");
464 printlist (&projects);
468 void
469 expungeprojects (void)
471 struct Project *prj, *next;
473 ForeachNodeSafe (&projects, prj, next)
475 Remove (prj);
476 freeproject (prj);
480 struct Project *
481 findproject (const char * pname)
483 return FindNode (&projects, pname);
486 struct Project *
487 getfirstproject (void)
489 struct Project * prj = GetHead (&projects);
491 if (prj && prj == defaultprj)
492 prj = GetNext (prj);
494 return prj;
498 execute (struct Project * prj, const char * cmd, const char * in,
499 const char * out, const char * args)
501 char buffer[4096];
502 char * cmdstr;
503 int rc;
505 debug(printf("MMAKE:project.c->execute(cmd '%s')\n", cmd));
507 strcpy (buffer, cmd);
508 strcat (buffer, " ");
510 if (strcmp (in, "-"))
512 strcat (buffer, "<");
513 strcat (buffer, in);
514 strcat (buffer, " ");
517 if (strcmp (out, "-"))
519 strcat (buffer, ">");
520 strcat (buffer, out);
521 strcat (buffer, " ");
524 strcat (buffer, args);
526 cmdstr = substvars (&prj->vars, buffer);
528 debug(printf("MMAKE:project.c->execute: parsed cmd '%s'\n", buffer));
530 if (verbose)
531 printf ("Executing %s...\n", cmdstr);
533 rc = system (cmdstr);
535 if (rc)
537 printf ("%s failed: %d\n", cmdstr, rc);
540 return !rc;
543 void
544 maketarget (struct Project * prj, char * tname)
546 struct Target * target, * subtarget;
547 struct Node * node;
548 struct MakefileRef * mfref;
549 struct MakefileTarget * mftarget;
550 struct List deps;
552 if (!quiet)
553 printf ("Building %s.%s\n", prj->node.name, tname);
555 NewList (&deps);
557 chdir (prj->srctop);
559 readvars (prj);
561 if (!prj->cache)
562 prj->cache = activatecache (prj);
564 if (!*tname)
565 tname = prj->defaulttarget;
567 target = FindNode (&prj->cache->targets, tname);
569 if (!target)
571 if (!quiet)
572 printf ("Nothing known about target %s in project %s\n", tname, prj->node.name);
573 return;
576 target->updated = 1;
578 ForeachNode (&target->makefiles, mfref)
580 mftarget = FindNode (&mfref->makefile->targets, tname);
582 ForeachNode (&mftarget->deps, node)
583 addnodeonce (&deps, node->name);
586 ForeachNode (&deps, node)
588 subtarget = FindNode (&prj->cache->targets, node->name);
590 if (!subtarget)
592 if (!quiet)
593 printf ("Nothing known about target %s in project %s\n", node->name, prj->node.name);
595 else if (!subtarget->updated)
597 maketarget (prj, node->name);
601 freelist (&deps);
603 ForeachNode (&target->makefiles, mfref)
605 if (!mfref->virtualtarget)
607 callmake (prj, tname, mfref->makefile);
611 freelist (&deps);