TaggedOpenLibrary constants off by one fix.
[AROS.git] / tools / MetaMake / project.c
blobaa65c3ed24a51936df8d9eb3ab46464c8515f332
1 /* MetaMake - A Make extension
2 Copyright © 1995-2011, 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 ForeachNodeSafe (&prj->genmakefiledeps, node, next)
142 Remove (node);
143 AddTail (&deps, node);
146 ForeachNodeSafe (&deps, node, next)
148 Remove (node);
149 dep = newdepnode (substvars (&prj->vars, node->name));
150 AddTail (&prj->genmakefiledeps, dep);
151 xfree (node->name);
152 xfree (node);
155 if (debug)
157 printf ("project %s.genmfdeps=\n", prj->node.name);
158 printlist (&prj->genmakefiledeps);
161 if (debug)
163 printf ("project %s.vars=", prj->node.name);
164 printvarlist (&prj->vars);
168 static struct Project *
169 initproject (char * name)
171 struct Project * prj = new (struct Project);
173 memset (prj, 0, sizeof(struct Project));
175 debug(printf("MMAKE:project.c->initproject('%s')\n", name));
176 debug(printf("MMAKE:project.c->initproject: Project node @ 0x%p\n", prj));
178 if (!defaultprj)
180 prj->maketool = xstrdup ("make \"TOP=$(TOP)\" \"SRCDIR=$(SRCDIR)\" \"CURDIR=$(CURDIR)\"");
181 prj->defaultmakefilename = xstrdup ("Makefile");
182 prj->srctop = mm_srcdir;
183 prj->buildtop = mm_builddir;
184 prj->defaulttarget = xstrdup ("all");
185 prj->genmakefilescript = NULL;
186 prj->genglobalvarfile = NULL;
188 else
190 prj->maketool = xstrdup (defaultprj->maketool);
191 prj->defaultmakefilename = xstrdup (defaultprj->defaultmakefilename);
192 prj->srctop = xstrdup (defaultprj->srctop);
193 prj->buildtop = xstrdup (defaultprj->buildtop);
194 prj->defaulttarget = xstrdup (defaultprj->defaulttarget);
195 SETSTR (prj->genmakefilescript, defaultprj->genmakefilescript);
196 SETSTR (prj->genglobalvarfile, defaultprj->genglobalvarfile);
199 prj->node.name = xstrdup (name);
201 prj->readvars = 1;
203 NewList(&prj->globalvarfiles);
204 NewList(&prj->genmakefiledeps);
205 NewList(&prj->ignoredirs);
206 NewList(&prj->vars);
207 NewList(&prj->extramakefiles);
209 return prj;
212 static void
213 freeproject (struct Project * prj)
215 assert (prj);
217 cfree (prj->node.name);
218 cfree (prj->maketool);
219 cfree (prj->defaultmakefilename);
220 if (prj->srctop != mm_srcdir)
221 cfree (prj->srctop);
222 if (prj->buildtop != mm_builddir)
223 cfree (prj->buildtop);
224 cfree (prj->defaulttarget);
225 cfree (prj->genmakefilescript);
226 cfree (prj->genglobalvarfile);
228 if (prj->cache)
229 closecache (prj->cache);
231 freelist(&prj->globalvarfiles);
232 freelist (&prj->genmakefiledeps);
233 freelist (&prj->ignoredirs);
234 freevarlist (&prj->vars);
235 freelist (&prj->extramakefiles);
237 xfree (prj);
240 static void
241 callmake (struct Project * prj, const char * tname, struct Makefile * makefile)
243 static char buffer[4096];
244 const char * path = buildpath (makefile->dir);
245 int t;
247 debug(printf("MMAKE:project.c->callmake()\n"));
249 if (makefile->generated)
250 chdir (prj->buildtop);
251 else
252 chdir (prj->srctop);
253 chdir (path);
255 setvar (&prj->vars, "CURDIR", path);
256 setvar (&prj->vars, "TARGET", tname);
258 buffer[0] = '\0';
260 for (t=0; t<mflagc; t++)
262 strcat (buffer, mflags[t]);
263 strcat (buffer, " ");
266 if (strcmp (makefile->node.name, "Makefile")!=0 && strcmp (makefile->node.name, "makefile")!=0);
268 strcat (buffer, "--file=");
269 strcat (buffer, makefile->node.name);
270 strcat (buffer, " ");
273 strcat (buffer, tname);
275 if (!quiet)
276 printf ("Making %s in %s\n", tname, path);
278 if (!execute (prj, prj->maketool, "-", "-", buffer))
280 error ("Error while running make in %s", path);
281 exit (10);
286 void
287 initprojects (void)
289 char * optionfile;
290 char * home;
291 char line[256];
292 FILE * optfh = NULL;
293 struct Project * project;
295 debug(printf("MMAKE:project.c->initprojects()\n"));
297 NewList(&projects);
298 defaultprj = project = initproject ("default");
299 AddTail(&projects, project);
302 /* Try "$MMAKE_CONFIG" */
303 if ((optionfile = getenv ("MMAKE_CONFIG")))
304 optfh = fopen (optionfile, "r");
306 /* Try "$HOME/.mmake.config" */
307 if (!optfh)
309 if ((home = getenv("HOME")))
311 optionfile = xmalloc (strlen(home) + sizeof("/.mmake.config") + 1);
312 sprintf (optionfile, "%s/.mmake.config", home);
313 optfh = fopen (optionfile, "r");
314 free (optionfile);
318 /* Try with $CWD/.mmake.config" */
319 if (!optfh)
320 optfh = fopen (".mmake.config", "r");
322 /* Try with "$CWD/mmake.config */
323 if (!optfh)
324 optfh = fopen ("mmake.config", "r");
326 /* Give up */
327 if (!optfh)
329 fprintf (stderr,
330 "Please set the HOME or MMAKE_CONFIG env var (with setenv or export)\n"
332 error ("Opening mmake.config for reading");
333 exit (10);
336 while (fgets (line, sizeof(line), optfh))
338 if (*line == '\n' || *line == '#') continue;
339 line[strlen(line)-1] = 0;
341 if (*line == '[') /* look for project name */
343 char * name, * ptr;
345 name = ptr = line+1;
346 while (*ptr && *ptr != ']')
347 ptr ++;
349 *ptr = 0;
351 debug(printf("MMAKE:project.c->initprojects: Adding '%s' from MMAKE_CONFIG\n", name));
353 project = initproject (name);
355 AddTail(&projects,project);
357 else
359 char * cmd, * args, * ptr;
361 cmd = line;
362 while (isspace (*cmd))
363 cmd ++;
365 args = cmd;
366 while (*args && !isspace(*args))
368 *args = tolower (*args);
369 args ++;
371 if (*args)
372 *args++ = 0;
373 while (isspace (*args))
374 args ++;
376 ptr = args;
378 while (*ptr && *ptr != '\n')
379 ptr ++;
381 *ptr = 0;
383 if (!strcmp (cmd, "add"))
385 struct Node * n;
386 n = newnode(args);
387 AddTail(&project->extramakefiles, n);
389 else if (!strcmp (cmd, "ignoredir"))
391 struct Node * n;
392 n = newnode(args);
393 AddTail(&project->ignoredirs, n);
395 else if (!strcmp (cmd, "defaultmakefilename"))
397 SETSTR(project->defaultmakefilename,args);
399 else if (!strcmp (cmd, "top"))
401 SETSTR(project->srctop,args);
403 else if (!strcmp (cmd, "defaulttarget"))
405 SETSTR(project->defaulttarget,args);
407 else if (!strcmp (cmd, "genmakefilescript"))
409 SETSTR(project->genmakefilescript,args);
411 else if (!strcmp (cmd, "genmakefiledeps"))
413 struct Node * dep;
414 int depc, t;
415 char ** deps = getargs (args, &depc, NULL);
417 for (t=0; t<depc; t++)
419 dep = addnodeonce (&project->genmakefiledeps, deps[t]);
422 else if (!strcmp (cmd, "globalvarfile"))
424 struct Node *n = newnode(args);
426 if (n)
427 AddTail(&project->globalvarfiles, n);
429 else if (!strcmp (cmd, "genglobalvarfile"))
431 SETSTR(project->genglobalvarfile,args);
433 else if (!strcmp (cmd, "maketool"))
435 SETSTR(project->maketool,args);
437 else
439 setvar(&project->vars, cmd, args);
444 fclose (optfh);
446 /* Clean up memory from getargs */
447 getargs (NULL, NULL, NULL);
449 if (debug)
451 printf ("known projects: ");
452 printlist (&projects);
456 void
457 expungeprojects (void)
459 struct Project *prj, *next;
461 ForeachNodeSafe (&projects, prj, next)
463 Remove (prj);
464 freeproject (prj);
468 struct Project *
469 findproject (const char * pname)
471 return FindNode (&projects, pname);
474 struct Project *
475 getfirstproject (void)
477 struct Project * prj = GetHead (&projects);
479 if (prj && prj == defaultprj)
480 prj = GetNext (prj);
482 return prj;
486 execute (struct Project * prj, const char * cmd, const char * in,
487 const char * out, const char * args)
489 char buffer[4096];
490 char * cmdstr;
491 int rc;
493 debug(printf("MMAKE:project.c->execute(cmd '%s')\n", cmd));
495 strcpy (buffer, cmd);
496 strcat (buffer, " ");
498 if (strcmp (in, "-"))
500 strcat (buffer, "<");
501 strcat (buffer, in);
502 strcat (buffer, " ");
505 if (strcmp (out, "-"))
507 strcat (buffer, ">");
508 strcat (buffer, out);
509 strcat (buffer, " ");
512 strcat (buffer, args);
514 cmdstr = substvars (&prj->vars, buffer);
516 debug(printf("MMAKE:project.c->execute: parsed cmd '%s'\n", buffer));
518 if (verbose)
519 printf ("Executing %s...\n", cmdstr);
521 rc = system (cmdstr);
523 if (rc)
525 printf ("%s failed: %d\n", cmdstr, rc);
528 return !rc;
531 void
532 maketarget (struct Project * prj, char * tname)
534 struct Target * target, * subtarget;
535 struct Node * node;
536 struct MakefileRef * mfref;
537 struct MakefileTarget * mftarget;
538 struct List deps;
540 if (!quiet)
541 printf ("Building %s.%s\n", prj->node.name, tname);
543 NewList (&deps);
545 chdir (prj->srctop);
547 readvars (prj);
549 if (!prj->cache)
550 prj->cache = activatecache (prj);
552 if (!*tname)
553 tname = prj->defaulttarget;
555 target = FindNode (&prj->cache->targets, tname);
557 if (!target)
559 if (!quiet)
560 printf ("Nothing known about target %s in project %s\n", tname, prj->node.name);
561 return;
564 target->updated = 1;
566 ForeachNode (&target->makefiles, mfref)
568 mftarget = FindNode (&mfref->makefile->targets, tname);
570 ForeachNode (&mftarget->deps, node)
571 addnodeonce (&deps, node->name);
574 ForeachNode (&deps, node)
576 subtarget = FindNode (&prj->cache->targets, node->name);
578 if (!subtarget)
580 if (!quiet)
581 printf ("Nothing known about target %s in project %s\n", node->name, prj->node.name);
583 else if (!subtarget->updated)
585 maketarget (prj, node->name);
589 freelist (&deps);
591 ForeachNode (&target->makefiles, mfref)
593 if (!mfref->virtualtarget)
595 callmake (prj, tname, mfref->makefile);
599 freelist (&deps);