1 /* MetaMake - A Make extension
2 Copyright © 1995-2019, 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. */
30 #ifdef HAVE_SYS_STAT_H
31 # include <sys/stat.h>
33 #ifdef HAVE_NETINET_IN_H
34 # include <netinet/in.h> /* for htonl/ntohl() */
52 #if defined(DEBUG_CACHE)
61 #define ID ((MAJOR << 24) | (MINOR << 16) | REVISION)
62 #define CHECK_ID(id) (((id) & 0xFFFF0000) == ((ID) & 0xFFFF0000))
66 struct Cache publicpart
;
67 struct Project
* project
;
68 struct DirNode
* topdir
;
69 struct List addedfiles
;
83 freetarget (struct Target
* target
)
85 xfree (target
->node
.name
);
87 freelist (&target
->makefiles
);
93 freetargetlist (struct Cache_priv
* cache
)
95 struct Node
* node
, * next
;
97 ForeachNodeSafe(&cache
->publicpart
.targets
,node
,next
)
100 freetarget ((struct Target
*)node
);
105 printtargetlist (struct List
* l
)
112 struct MakefileRef
* mfref
;
113 struct MakefileTarget
* mftarget
;
116 printf ("target %s:\n", n
->node
.name
);
117 printf (" updated=%d\n", n
->updated
);
118 printf (" makefiles=\n");
119 ForeachNode (&n
->makefiles
, mfref
)
120 printf(" \"%s/%s\"\n",
121 buildpath(mfref
->makefile
->dir
),
122 mfref
->makefile
->node
.name
127 ForeachNode (&n
->makefiles
, mfref
)
129 mftarget
= FindNode (&mfref
->makefile
->targets
, n
->node
.name
);
130 ForeachNode (&mftarget
->deps
, node
)
131 addnodeonce (&deps
, node
->name
);
140 static int progcount
;
143 progress_reset (FILE * fh
)
163 readcache (struct Cache_priv
* cache
)
169 strcpy (path
, cache
->project
->buildtop
);
170 strcat (path
, "/mmake.cache");
171 assert (strlen(path
) < sizeof(path
));
173 fh
= fopen (path
, "rb");
177 if (!readuint32 (fh
, &id
) || !CHECK_ID(id
))
190 if (!readstring (fh
, &name
))
199 addnodeonce (&cache
->addedfiles
, name
);
202 while (name
!= NULL
);
204 if (!(cache
->topdir
= readcachedir (fh
)))
213 cache
->topdir
= newnodesize (cache
->project
->srctop
, sizeof (struct DirNode
));
214 cache
->topdir
->parent
= NULL
;
215 NewList(&cache
->topdir
->subdirs
);
216 NewList(&cache
->topdir
->makefiles
);
218 /* Force a check the first time */
219 cache
->topdir
->time
= 0;
227 printf ("readcache()\n");
228 printdirnode (cache
->topdir
, 1);
233 writecache (struct Cache_priv
* cache
)
239 struct Node
*addedfile
;
241 debug(printf("MMAKE:cache.c->writecache()\n"));
246 strcpy (path
, cache
->project
->buildtop
);
247 strcat (path
, "/mmake.cache");
248 assert (strlen(path
) < sizeof(path
));
250 fh
= fopen (path
, "wb");
258 ok
= writeuint32 (fh
, ID
);
262 ForeachNode (&cache
->addedfiles
, addedfile
)
264 ok
= writestring (fh
, addedfile
->name
);
267 error ("writecache/writestring():%d", __LINE__
);
272 ok
= writestring (fh
, NULL
);
275 error("writecache/fwrite():%d", __LINE__
);
279 ok
= writecachedir (fh
, cache
->topdir
);
289 printf ("[MMAKE] %s: Warning! - Creating the cache failed\n", __func__
);
295 checknewsrc (struct Cache_priv
* cache
, struct Makefile
* makefile
, struct List
* regeneratefiles
)
297 char * mfsrc
= xmalloc (strlen (makefile
->node
.name
) + 5);
298 char * mfdst
= xmalloc (strlen (mm_builddir
) + 1 + strlen (buildpath(makefile
->dir
)) + 1 + strlen (makefile
->node
.name
) + 1);
299 struct stat sst
, dst
;
301 debug(printf("[MMAKE] %s('%s')\n", __func__
, makefile
->node
.name
));
303 strcpy (mfsrc
, makefile
->node
.name
);
304 strcat (mfsrc
, ".src");
306 if (stat (mfsrc
, &sst
) == -1)
308 debug(printf("[MMAKE] %s: stat(\"%s\", ...) failed\n", __func__
, mfsrc
));
313 strcpy (mfdst
, mm_builddir
);
315 strcat (mfdst
, buildpath(makefile
->dir
));
317 strcat (mfdst
, makefile
->node
.name
);
319 if (stat (mfdst
, &dst
) == -1
320 || sst
.st_mtime
> dst
.st_mtime
321 || checkdeps (&cache
->project
->genmakefiledeps
, dst
.st_mtime
)
324 static char currdir
[PATH_MAX
];
325 struct Regenerate
*reg
= new (struct Regenerate
);
327 ASSERT(getcwd(currdir
, PATH_MAX
) != NULL
);
328 reg
->dir
= xstrdup (buildpath(makefile
->dir
));
330 reg
->dest
= xstrdup (makefile
->node
.name
);
332 debug(printf("MMAKE/cache.c:Added \"%s\" to be regenerated from \"%s\" in \"%s\"\n",
333 reg
->dest
, reg
->src
, reg
->dir
335 AddTail (regeneratefiles
, reg
);
339 debug(printf("MMAKE/cache.c:\"%s\" Not regenerated\n", makefile
->node
.name
));
346 updatemflist (struct Cache_priv
* cache
, struct DirNode
* node
, struct List
* regeneratefiles
)
348 struct DirNode
* subdir
;
349 struct Makefile
* makefile
;
350 int goup
= 0, reread
= 0;
353 debug(printf("MMAKE:cache.c->updatemflist(\"%s\")\n", node
->node
.name
));
355 if (strlen(node
->node
.name
) != 0)
357 if (getcwd(curdir
, sizeof(curdir
)) == NULL
)
359 error("Could not get current directory");
362 if (chdir(node
->node
.name
) < 0)
364 error("Could not change to dir '%s'", node
->node
.name
);
370 if (scandirnode(node
, cache
->project
->defaultmakefilename
, &cache
->project
->ignoredirs
))
373 ForeachNode(&node
->subdirs
, subdir
)
375 debug(printf("MMAKE:cache.c->updatemflist: checking subdir ..\n"));
376 reread
+= updatemflist(cache
, subdir
, regeneratefiles
);
379 ForeachNode(&node
->makefiles
, makefile
)
381 debug(printf("MMAKE:cache.c->updatemflist: checking makefile ..\n"));
382 checknewsrc(cache
, makefile
, regeneratefiles
);
386 ASSERT(chdir(curdir
) == 0);
394 updatetargetlist (struct Project
* prj
, struct Cache_priv
* cache
, struct DirNode
* node
)
396 struct DirNode
* subdir
;
399 debug(printf("MMAKE:cache.c->updatetargetlist('%s')\n", node
->node
.name
));
401 reread
= scanmakefiles(prj
, node
, &cache
->project
->vars
);
403 ForeachNode(&node
->subdirs
, subdir
)
404 reread
+= updatetargetlist(prj
, cache
, subdir
);
413 int mktempfile(char **name
)
415 char *temp
= getenv("TEMP");
416 size_t pathlen
= strlen(temp
) + 13;
417 char *path
= xmalloc(pathlen
);
420 strcat(path
, "\\genmfXXXXXX");
422 return mkstemps(path
, 0);
427 int mktempfile(char **name
)
429 static char tmpname
[20];
431 strcpy(tmpname
, "/tmp/genmfXXXXXX");
433 return mkstemp(tmpname
);
440 regeneratemf (struct Cache_priv
* cache
, struct List
* regeneratefiles
)
442 struct Regenerate
* reg
,* reg2
;
447 debug(printf("MMAKE:cache.c->regeneratemf()\n"));
449 if (GetHead (regeneratefiles
) == NULL
)
452 fd
= mktempfile(&tmpname
);
455 error ("Could not create temporary file %s", tmpname
);
460 f
= fdopen (fd
, "w");
463 error ("Could not open temporary file %s", tmpname
);
468 ForeachNodeSafe (regeneratefiles
, reg
, reg2
)
470 char * mfsrc
= xmalloc (strlen(cache
->project
->srctop
) + strlen(reg
->dir
) + strlen(reg
->src
) + 3);
471 char * mfdst
= xmalloc (strlen(cache
->project
->buildtop
) + strlen(reg
->dir
) + strlen(reg
->dest
) + 3);
473 strcpy (mfsrc
, cache
->project
->srctop
);
474 if (strlen(reg
->dir
) > 0)
477 strcat (mfsrc
, reg
->dir
);
480 strcat (mfsrc
, reg
->src
);
482 strcpy (mfdst
, cache
->project
->buildtop
);
483 if (strlen(reg
->dir
) > 0)
486 strcat (mfdst
, reg
->dir
);
489 strcat (mfdst
, reg
->dest
);
491 debug(printf("MMAKE:cache.c->regeneratemf: regenerate '%s' as '%s'\n", mfsrc
, mfdst
));
493 fprintf (f
, "%s %s\n", mfsrc
, mfdst
);
506 setvar (&cache
->project
->vars
, "MMLIST", tmpname
);
507 if (!execute (cache
->project
, cache
->project
->genmakefilescript
,"-","-",""))
509 error ("Error regenerating makefile");
513 debug(printf("MMAKE:cache.c->regeneratemf: Finished\n"));
519 buildtargetlist (struct Cache_priv
* cache
, struct DirNode
* node
)
521 struct Makefile
* makefile
;
522 struct MakefileRef
* mfref
;
523 struct MakefileTarget
* mftarget
;
524 struct DirNode
* subdir
;
525 struct Target
* target
;
528 debug(printf("MMAKE:cache.c->buildtargetlist()\n"));
530 ForeachNode (&node
->makefiles
, makefile
)
532 ForeachNode (&makefile
->targets
, mftarget
)
534 if (strchr (mftarget
->node
.name
, '$') != NULL
)
536 char * s
= substvars(&cache
->project
->vars
, mftarget
->node
.name
);
537 SETSTR (mftarget
->node
.name
, s
);
540 ForeachNode (&mftarget
->deps
, n
)
542 if (strchr (n
->name
, '$') != NULL
)
544 char * s
= substvars(&cache
->project
->vars
, n
->name
);
549 target
= FindNode (&cache
->publicpart
.targets
, mftarget
->node
.name
);
553 target
= newnodesize (mftarget
->node
.name
, sizeof(struct Target
));
555 NewList (&target
->makefiles
);
556 AddTail (&cache
->publicpart
.targets
, target
);
559 mfref
= newnodesize ("", sizeof(struct MakefileRef
));
560 mfref
->virtualtarget
= mftarget
->virtualtarget
;
561 mfref
->makefile
= makefile
;
562 AddTail (&target
->makefiles
, mfref
);
566 ForeachNode (&node
->subdirs
, subdir
)
567 buildtargetlist (cache
, subdir
);
571 activatecache (struct Project
*prj
)
573 struct Cache_priv
* cache
;
574 struct List regeneratefiles
;
575 struct Node
* addedfile
, * extrafile
;
576 struct Makefile
* makefile
;
577 struct List newadded
;
580 debug(printf("MMAKE:cache.c->activatecache(Project @ %x)\n", prj
));
582 cache
= new (struct Cache_priv
);
585 NewList (®eneratefiles
);
587 cache
->project
= prj
;
589 debug(printf("MMAKE:cache.c->activatecache: Cache @ %x for Project @ %x\n", cache
, prj
));
591 NewList (&cache
->addedfiles
);
592 NewList (&cache
->publicpart
.targets
);
596 debug(printf("MMAKE:cache.c->activatecache: Cache read.\n"));
598 progress_reset (stdout
);
599 printf ("[MMAKE] Scanning dirs...\n");
600 reread
= updatemflist (cache
, cache
->topdir
, ®eneratefiles
);
601 progress_reset (stdout
);
603 debug(printf("MMAKE:cache.c->activatecache: Updated MF list.\n"));
606 printf ("[MMAKE] Reread %d dirs\n", reread
);
610 printf ("[MMAKE] Directory tree for project %s\n", prj
->node
.name
);
611 printdirnode (cache
->topdir
, 0);
614 /* Add the extra makefiles to the tree if needed */
615 ASSERT(chdir (cache
->project
->buildtop
) == 0);
617 ForeachNode (&cache
->project
->extramakefiles
, extrafile
)
619 addedfile
= FindNode (&cache
->addedfiles
, extrafile
->name
);
620 if (addedfile
== NULL
)
622 makefile
= addmakefile (cache
->topdir
, extrafile
->name
);
624 if (makefile
== NULL
)
626 error("Could not add makefile \"%s\"", extrafile
->name
);
630 addnodeonce (&newadded
, extrafile
->name
);
632 else /* addedfile != NULL => was already added before */
634 makefile
= findmakefile (cache
->topdir
, extrafile
->name
);
636 if (makefile
== NULL
)
638 error("Makefile \"%s\" has disappeared", extrafile
->name
);
643 AddTail (&newadded
, addedfile
);
646 ForeachNode (&cache
->addedfiles
, addedfile
)
648 makefile
= findmakefile (cache
->topdir
, addedfile
->name
);
649 if (makefile
!= NULL
)
652 freemakefile (makefile
);
655 AssignList (&cache
->addedfiles
, &newadded
);
657 regeneratemf (cache
, ®eneratefiles
);
659 debug(printf("MMAKE:cache.c->activatecache: Regenerated MFs\n"));
661 progress_reset (stdout
);
662 printf ("[MMAKE] Scanning makefiles...\n");
663 reread
= updatetargetlist (prj
, cache
, cache
->topdir
);
665 printf ("[MMAKE] Reread %d makefiles\n", reread
);
668 printf ("[MMAKE] Makefile and target tree for project %s\n", prj
->node
.name
);
669 printdirnodemftarget (cache
->topdir
);
674 progress_reset (stdout
);
675 printf ("[MMAKE] Collecting targets...\n");
676 buildtargetlist (cache
, cache
->topdir
);
679 printf ("[MMAKE] Targetlist of project %s\n", prj
->node
.name
);
680 printtargetlist (&cache
->publicpart
.targets
);
683 debug(printf("MMAKE:cache.c->activatecache: Finished\n"));
685 return (struct Cache
*)cache
;
689 closecache (struct Cache
* gl_cache
)
691 struct Cache_priv
* cache
= (struct Cache_priv
*)gl_cache
;
693 freetargetlist (cache
);