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)
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 ("[MMAKE] 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 ("[MMAKE] 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 ("[MMAKE] %s=%s\n", name
, substvars (&prj
->vars
, value
));
132 setvar (&prj
->vars
, name
, substvars (&prj
->vars
, value
));
138 /* handle prj->genmakefiledeps */
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
144 * generating new dep nodes and putting them into the deps list,
145 * then copy them back into genmakefiledeps list
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
)
153 dep
= newdepnode (substvars (&prj
->vars
, node
->name
));
154 AddTail (&deps
, dep
);
159 ForeachNodeSafe (&deps
, node
, next
)
162 AddTail (&prj
->genmakefiledeps
, node
);
167 printf ("[MMAKE] project %s.genmfdeps=\n", prj
->node
.name
);
168 printlist (&prj
->genmakefiledeps
);
173 printf ("[MMAKE] 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
));
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
;
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
);
213 NewList(&prj
->globalvarfiles
);
214 NewList(&prj
->genmakefiledeps
);
215 NewList(&prj
->ignoredirs
);
217 NewList(&prj
->extramakefiles
);
223 freeproject (struct Project
* prj
)
227 cfree (prj
->node
.name
);
228 cfree (prj
->maketool
);
229 cfree (prj
->defaultmakefilename
);
230 if (prj
->srctop
!= mm_srcdir
)
232 if (prj
->buildtop
!= mm_builddir
)
233 cfree (prj
->buildtop
);
234 cfree (prj
->defaulttarget
);
235 cfree (prj
->genmakefilescript
);
236 cfree (prj
->genglobalvarfile
);
239 closecache (prj
->cache
);
241 freelist(&prj
->globalvarfiles
);
242 freelist (&prj
->genmakefiledeps
);
243 freelist (&prj
->ignoredirs
);
244 freevarlist (&prj
->vars
);
245 freelist (&prj
->extramakefiles
);
251 callmake (struct Project
* prj
, const char * tname
, struct Makefile
* makefile
)
253 static char buffer
[4096];
254 const char * path
= buildpath (makefile
->dir
);
257 debug(printf("MMAKE:project.c->callmake()\n"));
259 if (makefile
->generated
)
260 ASSERT(chdir (prj
->buildtop
) == 0);
262 ASSERT(chdir (prj
->srctop
) == 0);
264 ASSERT(chdir (path
) == 0);
266 setvar (&prj
->vars
, "CURDIR", path
);
267 setvar (&prj
->vars
, "TARGET", tname
);
271 for (t
=0; t
<mflagc
; t
++)
273 strcat (buffer
, mflags
[t
]);
274 strcat (buffer
, " ");
277 if (strcmp (makefile
->node
.name
, "Makefile")!=0 && strcmp (makefile
->node
.name
, "makefile")!=0)
279 strcat (buffer
, "--file=");
280 strcat (buffer
, makefile
->node
.name
);
281 strcat (buffer
, " ");
284 strcat (buffer
, tname
);
287 printf ("[MMAKE] Making %s in %s\n", tname
, path
);
289 if (!execute (prj
, prj
->maketool
, "-", "-", buffer
))
291 error ("Error while running make in %s", path
);
304 struct Project
* project
;
306 debug(printf("MMAKE:project.c->initprojects()\n"));
309 defaultprj
= project
= initproject ("default");
310 AddTail(&projects
, project
);
313 /* Try "$MMAKE_CONFIG" */
314 if ((optionfile
= getenv ("MMAKE_CONFIG")))
315 optfh
= fopen (optionfile
, "r");
317 /* Try "$HOME/.mmake.config" */
320 if ((home
= getenv("HOME")))
322 optionfile
= xmalloc (strlen(home
) + sizeof("/.mmake.config") + 1);
323 sprintf (optionfile
, "%s/.mmake.config", home
);
324 optfh
= fopen (optionfile
, "r");
329 /* Try with $CWD/.mmake.config" */
331 optfh
= fopen (".mmake.config", "r");
333 /* Try with "$CWD/mmake.config */
335 optfh
= fopen ("mmake.config", "r");
341 "[MMAKE] Please set the HOME or MMAKE_CONFIG env var (with setenv or export)\n"
343 error ("Opening mmake.config for reading");
347 while (fgets (line
, sizeof(line
), optfh
))
349 if (*line
== '\n' || *line
== '#') continue;
350 line
[strlen(line
)-1] = 0;
352 if (*line
== '[') /* look for project name */
357 while (*ptr
&& *ptr
!= ']')
362 debug(printf("MMAKE:project.c->initprojects: Adding '%s' from MMAKE_CONFIG\n", name
));
364 project
= initproject (name
);
366 AddTail(&projects
,project
);
370 char * cmd
, * args
, * ptr
;
373 while (isspace (*cmd
))
377 while (*args
&& !isspace(*args
))
379 *args
= tolower (*args
);
384 while (isspace (*args
))
389 while (*ptr
&& *ptr
!= '\n')
394 if (!strcmp (cmd
, "add"))
398 AddTail(&project
->extramakefiles
, n
);
400 else if (!strcmp (cmd
, "ignoredir"))
404 AddTail(&project
->ignoredirs
, n
);
406 else if (!strcmp (cmd
, "defaultmakefilename"))
408 SETSTR(project
->defaultmakefilename
,args
);
410 else if (!strcmp (cmd
, "top"))
412 SETSTR(project
->srctop
,args
);
414 else if (!strcmp (cmd
, "defaulttarget"))
416 SETSTR(project
->defaulttarget
,args
);
418 else if (!strcmp (cmd
, "genmakefilescript"))
420 SETSTR(project
->genmakefilescript
,args
);
422 else if (!strcmp (cmd
, "genmakefiledeps"))
426 char ** deps
= getargs (args
, &depc
, NULL
);
428 debug(printf("MMAKE/project.c: genmakefiledeps depc=%d\n", depc
));
430 for (t
=0; t
<depc
; t
++)
432 dep
= addnodeonce (&project
->genmakefiledeps
, deps
[t
]);
435 else if (!strcmp (cmd
, "globalvarfile"))
437 struct Node
*n
= newnode(args
);
440 AddTail(&project
->globalvarfiles
, n
);
442 else if (!strcmp (cmd
, "genglobalvarfile"))
444 SETSTR(project
->genglobalvarfile
,args
);
446 else if (!strcmp (cmd
, "maketool"))
448 SETSTR(project
->maketool
,args
);
452 setvar(&project
->vars
, cmd
, args
);
459 /* Clean up memory from getargs */
460 getargs (NULL
, NULL
, NULL
);
464 printf ("[MMAKE] known projects: ");
465 printlist (&projects
);
470 expungeprojects (void)
472 struct Project
*prj
, *next
;
474 ForeachNodeSafe (&projects
, prj
, next
)
482 findproject (const char * pname
)
484 return FindNode (&projects
, pname
);
488 getfirstproject (void)
490 struct Project
* prj
= GetHead (&projects
);
492 if (prj
&& prj
== defaultprj
)
499 execute (struct Project
* prj
, const char * cmd
, const char * in
,
500 const char * out
, const char * args
)
506 debug(printf("MMAKE:project.c->execute(cmd '%s')\n", cmd
));
508 strcpy (buffer
, cmd
);
509 strcat (buffer
, " ");
511 if (strcmp (in
, "-"))
513 strcat (buffer
, "<");
515 strcat (buffer
, " ");
518 if (strcmp (out
, "-"))
520 strcat (buffer
, ">");
521 strcat (buffer
, out
);
522 strcat (buffer
, " ");
525 strcat (buffer
, args
);
527 cmdstr
= substvars (&prj
->vars
, buffer
);
529 debug(printf("MMAKE:project.c->execute: parsed cmd '%s'\n", buffer
));
532 printf ("[MMAKE] Executing %s...\n", cmdstr
);
534 rc
= system (cmdstr
);
538 printf ("[MMAKE] %s failed: %d\n", cmdstr
, rc
);
545 maketarget (struct Project
* prj
, char * tname
)
547 struct Target
* target
, * subtarget
;
549 struct MakefileRef
* mfref
;
550 struct MakefileTarget
* mftarget
;
554 printf ("[MMAKE] Building %s.%s\n", prj
->node
.name
, tname
);
558 ASSERT(chdir (prj
->srctop
) == 0);
563 prj
->cache
= activatecache (prj
);
566 tname
= prj
->defaulttarget
;
568 target
= FindNode (&prj
->cache
->targets
, tname
);
573 printf ("[MMAKE] Nothing known about target %s in project %s\n", tname
, prj
->node
.name
);
579 ForeachNode (&target
->makefiles
, mfref
)
581 mftarget
= FindNode (&mfref
->makefile
->targets
, tname
);
583 ForeachNode (&mftarget
->deps
, node
)
584 addnodeonce (&deps
, node
->name
);
587 ForeachNode (&deps
, node
)
589 subtarget
= FindNode (&prj
->cache
->targets
, node
->name
);
594 printf ("[MMAKE] Nothing known about target %s in project %s\n", node
->name
, prj
->node
.name
);
596 else if (!subtarget
->updated
)
598 maketarget (prj
, node
->name
);
604 ForeachNode (&target
->makefiles
, mfref
)
606 if (!mfref
->virtualtarget
)
608 callmake (prj
, tname
, mfref
->makefile
);