summaryrefslogtreecommitdiff
path: root/dmake/dmdump.c
blob: 571340fa24dc0b3b73e94ac49b1a1f6011cdc38b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* RCS  $Id: dmdump.c,v 1.4 2007-06-12 06:05:11 obo Exp $
--
-- SYNOPSIS
--      Dump the internal dag to stdout.
-- 
-- DESCRIPTION
--	This file contains the routine that is called to dump a version of
--	the digested makefile to the standard output.  May be useful perhaps
--	to the ordinary user, and invaluable for debugging make.
-- 
-- AUTHOR
--      Dennis Vadura, dvadura@dmake.wticorp.com
--
-- WWW
--      http://dmake.wticorp.com/
--
-- COPYRIGHT
--      Copyright (c) 1996,1997 by WTI Corp.  All rights reserved.
-- 
--      This program is NOT free software; you can redistribute it and/or
--      modify it under the terms of the Software License Agreement Provided
--      in the file <distribution-root>/readme/license.txt.
--
-- LOG
--      Use cvs log to obtain detailed change logs.
*/

#include "extern.h"

#define M_TEST	(M_PRECIOUS | M_VAR_MASK)

static	void	dump_name ANSI((CELLPTR, int, int));
static	void	dump_normal_target ANSI((CELLPTR, CELLPTR, int));
static  void    dump_prerequisites ANSI((LINKPTR, CELLPTR, int, int, int));
static  void    dump_conditionals ANSI((CELLPTR,STRINGPTR,int,int));
static  void    dump_macro ANSI((HASHPTR, int));


PUBLIC void
Dump()/*
========  Dump onto standard output the digested makefile.  Note that
      the form of the dump is not representative of the contents
      of the original makefile contents at all */
{
   HASHPTR      hp;
   int          i;

   DB_ENTER( "Dump" );

   puts( "# Dump of dmake macro variables:" );
   for( i=0; i<HASH_TABLE_SIZE; i++)
      for( hp=Macs[i]; hp != NIL(HASH); hp = hp->ht_next ) {
     int flag = hp->ht_flag;
     dump_macro(hp, flag);
      }

   puts( "\n#====================================" );
   puts( "# Dump of targets:\n" );

   for( i=0; i<HASH_TABLE_SIZE; i++ )
      for( hp = Defs[i]; hp != NIL(HASH); hp = hp->ht_next )
         if( !(hp->CP_OWNR->ce_flag & F_PERCENT) ) {
        if( hp->CP_OWNR == Root )
           puts( "# ******* ROOT TARGET ********" );
        if (Targets->ce_prq && hp->CP_OWNR == Targets->ce_prq->cl_prq)
           puts( "# ******* FIRST USER DEFINED TARGET ******" );
        dump_normal_target( hp->CP_OWNR,NIL(CELL),hp->CP_OWNR->ce_flag);
     }

   puts( "\n#====================================" );
   puts( "# Dump of inference graph\n" );

   for( i=0; i<HASH_TABLE_SIZE; i++ )
      for( hp = Defs[i]; hp != NIL(HASH); hp = hp->ht_next )
         if( (hp->CP_OWNR->ce_flag & F_PERCENT) &&
        !(hp->CP_OWNR->ce_flag & F_MAGIC) )
        dump_normal_target(hp->CP_OWNR,NIL(CELL),hp->CP_OWNR->ce_flag);

   DB_VOID_RETURN;
}



PUBLIC void
Dump_recipe( sp )/*
===================
   Given a string pointer print the recipe line out */
STRINGPTR sp;
{
   char *st;
   char *nl;

   if( sp == NIL(STRING) ) return;

   putchar( '\t' );
   if( sp->st_attr & A_SILENT ) putchar( '@' );
   if( sp->st_attr & A_IGNORE ) putchar( '-' );
   if( sp->st_attr & A_SHELL  ) putchar( '+' );
   if( sp->st_attr & A_SWAP   ) putchar( '%' );

   st = sp->st_string;
   for( nl=strchr(st,'\n'); nl != NIL( char); nl=strchr(st,'\n') ) {
      *nl = '\0';
      printf( "%s\\\n", st );
      *nl = '\n';
      st  = nl+1;
   }
   printf( "%s\n", st );
}


static char *_attrs[] = { ".PRECIOUS", ".SILENT", ".LIBRARY",
   ".EPILOG", ".PROLOG", ".IGNORE", ".SYMBOL", ".NOINFER",
   ".UPDATEALL", ".SEQUENTIAL", ".SETDIR=", ".USESHELL",
#if defined(MSDOS)
   ".SWAP",
#else
# if defined(__CYGWIN__)
   ".WINPATH",
# else
   "- unused -",
# endif
#endif
   ".MKSARGS",
   ".PHONY", ".NOSTATE", ".IGNOREGROUP", ".EXECUTE", ".ERRREMOVE" };

static void
dump_normal_target( cp, namecp, flag )/*
========================================
    Dump in makefile like format the dag information */
CELLPTR cp;
CELLPTR namecp;
int     flag;
{
   register STRINGPTR sp;
   t_attr	      attr;
   unsigned int	      k;

   DB_ENTER( "dump_normal_target" );

   if(!(cp->ce_flag & F_TARGET) && !cp->ce_attr && !cp->ce_prq) {
      DB_VOID_RETURN;
   }

   if(cp->ce_set && cp->ce_set != cp) {
      DB_VOID_RETURN;
   }

   if( cp->ce_flag & F_MULTI ) {
      /* recursively print multi or %-targets. */
      int tflag = cp->ce_prq->cl_prq->ce_flag;
      if( !(cp->ce_flag & F_PERCENT) ) tflag |= F_MULTI;
      dump_conditionals(cp, cp->ce_cond, TRUE, TRUE);
      putchar('\n');

#ifdef DBUG
      /* Output also master targtet. (Only in debug builds) */
      printf("Master name(s) (DBUG build): ");
      dump_name(cp, FALSE, TRUE );
      putchar('\n');
#endif

      /* %-targets set namecp (3rd parameter) to NULL so that the next
       * recursive dump_normal_target() prints the name of cp->ce_prq->cl_prq
       * instead of cp.  This should be the same unless CeMeToo(cp) points
       * to a cell that is the head of an .UPDATEALL list. */
      dump_prerequisites(cp->ce_prq,(cp->ce_flag&F_PERCENT)?NIL(CELL):cp,
             FALSE, TRUE, tflag);
   }
   else {
      dump_name(namecp?namecp:cp, FALSE, TRUE );

      for( k=0, attr=1; attr <= MAX_ATTR; attr <<= 1, k++ )
     if( cp->ce_attr & attr ) {
        printf( "%s%s ", _attrs[k],
            (attr != A_SETDIR) ? "" : (cp->ce_dir?cp->ce_dir:"") );
     }

      putchar( ':' );

      if( flag & F_MULTI )  putchar( ':' );
      if( flag & F_SINGLE ) putchar( '!' );
      putchar( ' ' );

      dump_prerequisites( cp->ce_prq, NIL(CELL), FALSE, FALSE, F_DEFAULT);
      dump_prerequisites( cp->ce_indprq, NIL(CELL),TRUE, FALSE, F_DEFAULT);

      putchar( '\n' );
      if( cp->ce_flag & F_GROUP ) puts( "[" );
      for( sp = cp->ce_recipe; sp != NIL(STRING); sp = sp->st_next )
     Dump_recipe( sp );
      if( cp->ce_flag & F_GROUP ) {
     puts( "]" );
     putchar( '\n' );
      }
      dump_conditionals(cp, cp->ce_cond, flag&F_MULTI, FALSE);
      putchar('\n');
   }

   DB_VOID_RETURN;
}


static void
dump_conditionals( cp, sp, multi, global )
CELLPTR     cp;
STRINGPTR   sp;
int         multi;
int         global;
{
   if (sp) {
      dump_name(cp, FALSE, TRUE);
      printf(".%sCONDITIONALS %s\n", global?"GLOBAL":"",multi?"::":":");

      while(sp) {
     printf("\t%s\n",sp->st_string);
     sp=sp->st_next;
      }
   }
}


static void
dump_macro(hp, flag)
HASHPTR hp;
int     flag;
{
   printf( "%s ", hp->ht_name );
   if(flag & M_EXPANDED)
      putchar( ':' );

   printf( "= " );
   if(hp->ht_value != NIL(char))
      printf( "%s",hp->ht_value );

   if(flag & M_PRECIOUS)
      printf( "\t # PRECIOUS " );

   putchar( '\n' );
}


static void
dump_prerequisites( lp, namecp, quote, recurse, flag )/*
========================================================
   Dump as prerequisites if recurse is FALSE or as targets
   if recurse is TRUE. (For F_MULTI/F_PERCENT targets.) */
LINKPTR lp;
CELLPTR namecp;
int     quote;
int     recurse;
int     flag;
{
   for( ; lp; lp=lp->cl_next )
      if( recurse )
     dump_normal_target(lp->cl_prq, namecp, flag);
      else if( lp->cl_prq )
     dump_name(lp->cl_prq, quote, FALSE);
}


static void
dump_name( cp, quote, all )/*
=============================
   Prints out the first or all (if all is TRUE) names of an lcell list.
   If quote is true enclose in ' quotes, if quote
   is FALSE and the name includes a space enclose in " quotes. */
CELLPTR cp;
int     quote;
int     all;
{
   LINKPTR lp;
   char qc = '\'';

   for(lp=CeMeToo(cp);lp;lp=lp->cl_next) {
      if( !quote && strchr(lp->cl_prq->CE_NAME,' ') != NIL(char)) {
         quote = TRUE;
         qc = '"';
      }

      if (quote) putchar(qc);
      printf( "%s", lp->cl_prq->CE_NAME );
      if (quote) putchar(qc);
      putchar(' ');
      if (!all) break;
   }
}