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)
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
40 #if defined(DEBUG_PROJECT)
47 static struct Project
* defaultprj
= NULL
;
50 readvars (struct Project
* prj
)
53 struct Node
* node
, * next
;
56 debug(printf("MMAKE:project.c->readvars(Project @ 0x%p)\n", prj
));
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
)
74 char * name
, * value
, * ptr
;
76 fn
= xstrdup (substvars (&prj
->vars
, node
->name
));
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
);
99 error ("readvars():fopen(): Opening \"%s\" for reading", fn
);
105 while (fgets (line
, sizeof(line
), fh
))
107 if (*line
== '\n' || *line
== '#') continue;
108 line
[strlen(line
)-1] = 0;
111 while (isspace (*ptr
)) ptr
++;
113 while (*ptr
&& !isspace(*ptr
) && *ptr
!= ':' && *ptr
!= '=')
119 while (isspace(*ptr
) || *ptr
== ':' || *ptr
== '=')
124 while (*ptr
&& *ptr
!= '#')
130 printf ("%s=%s\n", name
, substvars (&prj
->vars
, value
));
132 setvar (&prj
->vars
, name
, substvars (&prj
->vars
, value
));
138 /* handle prj->genmakefiledeps */
140 ForeachNodeSafe (&prj
->genmakefiledeps
, node
, next
)
143 AddTail (&deps
, node
);
146 ForeachNodeSafe (&deps
, node
, next
)
149 dep
= newdepnode (substvars (&prj
->vars
, node
->name
));
150 AddTail (&prj
->genmakefiledeps
, dep
);
157 printf ("project %s.genmfdeps=\n", prj
->node
.name
);
158 printlist (&prj
->genmakefiledeps
);
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
));
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
;
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
);
203 NewList(&prj
->globalvarfiles
);
204 NewList(&prj
->genmakefiledeps
);
205 NewList(&prj
->ignoredirs
);
207 NewList(&prj
->extramakefiles
);
213 freeproject (struct Project
* prj
)
217 cfree (prj
->node
.name
);
218 cfree (prj
->maketool
);
219 cfree (prj
->defaultmakefilename
);
220 if (prj
->srctop
!= mm_srcdir
)
222 if (prj
->buildtop
!= mm_builddir
)
223 cfree (prj
->buildtop
);
224 cfree (prj
->defaulttarget
);
225 cfree (prj
->genmakefilescript
);
226 cfree (prj
->genglobalvarfile
);
229 closecache (prj
->cache
);
231 freelist(&prj
->globalvarfiles
);
232 freelist (&prj
->genmakefiledeps
);
233 freelist (&prj
->ignoredirs
);
234 freevarlist (&prj
->vars
);
235 freelist (&prj
->extramakefiles
);
241 callmake (struct Project
* prj
, const char * tname
, struct Makefile
* makefile
)
243 static char buffer
[4096];
244 const char * path
= buildpath (makefile
->dir
);
247 debug(printf("MMAKE:project.c->callmake()\n"));
249 if (makefile
->generated
)
250 chdir (prj
->buildtop
);
255 setvar (&prj
->vars
, "CURDIR", path
);
256 setvar (&prj
->vars
, "TARGET", tname
);
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
);
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
);
293 struct Project
* project
;
295 debug(printf("MMAKE:project.c->initprojects()\n"));
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" */
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");
318 /* Try with $CWD/.mmake.config" */
320 optfh
= fopen (".mmake.config", "r");
322 /* Try with "$CWD/mmake.config */
324 optfh
= fopen ("mmake.config", "r");
330 "Please set the HOME or MMAKE_CONFIG env var (with setenv or export)\n"
332 error ("Opening mmake.config for reading");
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 */
346 while (*ptr
&& *ptr
!= ']')
351 debug(printf("MMAKE:project.c->initprojects: Adding '%s' from MMAKE_CONFIG\n", name
));
353 project
= initproject (name
);
355 AddTail(&projects
,project
);
359 char * cmd
, * args
, * ptr
;
362 while (isspace (*cmd
))
366 while (*args
&& !isspace(*args
))
368 *args
= tolower (*args
);
373 while (isspace (*args
))
378 while (*ptr
&& *ptr
!= '\n')
383 if (!strcmp (cmd
, "add"))
387 AddTail(&project
->extramakefiles
, n
);
389 else if (!strcmp (cmd
, "ignoredir"))
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"))
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
);
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
);
439 setvar(&project
->vars
, cmd
, args
);
446 /* Clean up memory from getargs */
447 getargs (NULL
, NULL
, NULL
);
451 printf ("known projects: ");
452 printlist (&projects
);
457 expungeprojects (void)
459 struct Project
*prj
, *next
;
461 ForeachNodeSafe (&projects
, prj
, next
)
469 findproject (const char * pname
)
471 return FindNode (&projects
, pname
);
475 getfirstproject (void)
477 struct Project
* prj
= GetHead (&projects
);
479 if (prj
&& prj
== defaultprj
)
486 execute (struct Project
* prj
, const char * cmd
, const char * in
,
487 const char * out
, const char * args
)
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
, "<");
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
));
519 printf ("Executing %s...\n", cmdstr
);
521 rc
= system (cmdstr
);
525 printf ("%s failed: %d\n", cmdstr
, rc
);
532 maketarget (struct Project
* prj
, char * tname
)
534 struct Target
* target
, * subtarget
;
536 struct MakefileRef
* mfref
;
537 struct MakefileTarget
* mftarget
;
541 printf ("Building %s.%s\n", prj
->node
.name
, tname
);
550 prj
->cache
= activatecache (prj
);
553 tname
= prj
->defaulttarget
;
555 target
= FindNode (&prj
->cache
->targets
, tname
);
560 printf ("Nothing known about target %s in project %s\n", tname
, prj
->node
.name
);
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
);
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
);
591 ForeachNode (&target
->makefiles
, mfref
)
593 if (!mfref
->virtualtarget
)
595 callmake (prj
, tname
, mfref
->makefile
);