3 -- last change: $Author: kz $ $Date: 2008-03-05 18:27:48 $
6 -- Routines to construct the internal dag.
9 -- This file contains all the routines that are responsible for
10 -- defining and manipulating all objects used by the make facility.
13 -- Dennis Vadura, dvadura@dmake.wticorp.com
16 -- http://dmake.wticorp.com/
19 -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
21 -- This program is NOT free software; you can redistribute it and/or
22 -- modify it under the terms of the Software License Agreement Provided
23 -- in the file <distribution-root>/readme/license.txt.
26 -- Use cvs log to obtain detailed change logs.
35 Set the macro according to its type. In addition to the string value
36 in hp->ht_value a macro can stores a value casted with its type.
40 switch( hp
->ht_flag
& M_VAR_MASK
) /* only one var type per var */
43 *hp
->MV_SVAR
= hp
->ht_value
;
44 /* Add special treatment for PWD/MAKEDIR for .WINPATH. */
45 if( hp
->MV_SVAR
== &Pwd_macval
) {
49 /* Use the "DOSified" path for the macro. */
50 *hp
->MV_SVAR
= hp
->ht_value
= DmStrDup(DO_WINPATH(hp
->ht_value
));
51 DB_PRINT( "smv", ("PWD: %s/%s", Pwd_macval
, Pwd
) );
52 } else if( hp
->MV_SVAR
== &Makedir_macval
) {
55 Makedir
= hp
->ht_value
;
56 /* Use the "DOSified" path for the macro. */
57 *hp
->MV_SVAR
= hp
->ht_value
= DmStrDup(DO_WINPATH(hp
->ht_value
));
58 DB_PRINT( "smv", ("MAKEDIR: %s/%s", Makedir_macval
, Makedir
) );
60 /* No special treatment for TMD needed. */
64 *hp
->MV_CVAR
= (hp
->ht_value
== NIL(char)) ? '\0':*hp
->ht_value
;
69 if( hp
->MV_IVAR
== NIL(int) ) break; /* first time */
71 tvalue
= atoi(hp
->ht_value
);
72 if( hp
->MV_IVAR
== &Buffer_size
) {
73 /* If Buffer_size (MAXLINELENGTH) is modified then make sure
74 * you change the size of the real buffer as well. As the
75 * value will at least be BUFSIZ this might lead to the
76 * situation that the (string) value of MAXLINELENGTH is
77 * smaller than the integer value. */
78 tvalue
= (tvalue
< (BUFSIZ
-2)) ? BUFSIZ
: tvalue
+2;
79 if( Buffer_size
== tvalue
) break;
80 if( Buffer
) FREE(Buffer
);
81 if((Buffer
=MALLOC(tvalue
, char)) == NIL(char)) No_ram();
84 *hp
->MV_IVAR
= tvalue
;
86 if( hp
->MV_IVAR
== &Max_proc
|| hp
->MV_IVAR
== &Max_proclmt
) {
88 Fatal( "Process limit value must be > 1" );
90 #if defined(USE_CREATEPROCESS)
91 if( Max_proclmt
> MAXIMUM_WAIT_OBJECTS
)
92 Fatal( "Specified maximum # of processes (MAXPROCESSLIMIT)"
93 " exceeds OS limit of [%d].", MAXIMUM_WAIT_OBJECTS
);
96 if( Max_proc
> Max_proclmt
)
97 Fatal( "Specified # of processes exceeds limit of [%d]",
100 /* Don't change MAXPROCESS value if .SEQUENTIAL is set. */
101 if( (Glob_attr
& A_SEQ
) && (Max_proc
!= 1) ) {
102 Warning( "Macro MAXPROCESS set to 1 because .SEQUENTIAL is set." );
104 if( hp
->ht_value
!= NIL(char) ) FREE(hp
->ht_value
);
105 hp
->ht_value
= DmStrDup( "1" );
111 /* Bit variables are set to 1 if ht_value is not NULL and 0
114 if( hp
->ht_value
== NIL(char) )
115 *hp
->MV_BVAR
&= ~hp
->MV_MASK
;
117 *hp
->MV_BVAR
|= hp
->MV_MASK
;
118 /* If we're setting .SEQUENTIAL set MAXPROCESS=1. */
119 if( (hp
->MV_MASK
& A_SEQ
) && (Max_proc
!= 1) )
120 Def_macro( "MAXPROCESS", "1", M_MULTI
|M_EXPANDED
);
123 #if defined(__CYGWIN__)
124 /* Global .WINPATH change. Only needed for cygwin. */
125 if(hp
->MV_MASK
& A_WINPATH
) {
126 UseWinpath
= ((Glob_attr
&A_WINPATH
) != 0);
127 /* Change MAKEDIR, PWD according to .WINPATH. During
128 * makefile evaluation this cannot change TMD (it is "."
129 * and later TMD is set in Make() according to the
130 * .WINPATH attribute. */
131 Def_macro( "MAKEDIR", Makedir
, M_FORCE
| M_EXPANDED
);
132 Def_macro( "PWD", Pwd
, M_FORCE
| M_EXPANDED
);
141 Get_name( name
, tab
, define
)/*
142 ===============================
143 Look to see if the name is defined, if it is then return
144 a pointer to its node, if not return NIL(HASH).
145 If define is TRUE and the name is not found it will be added. */
147 char *name
; /* name we are looking for */
148 HASHPTR
*tab
; /* the hash table to look in */
149 int define
; /* TRUE => add to table */
156 DB_ENTER( "Get_name" );
157 DB_PRINT( "name", ("Looking for %s", name
) );
159 hp
= Search_table( tab
, name
, &hv
, &hash_key
);
161 if( hp
== NIL(HASH
) && define
) {
162 /* Check to make sure that CELL name contains only printable chars */
163 for( p
=name
; *p
; p
++ )
164 if( !isprint(*p
) && !iswhite(*p
) && *p
!= '\n' )
165 Fatal( "Name contains non-printable character [0x%02x]", *p
);
167 TALLOC( hp
, 1, HASH
); /* allocate a cell and add it in */
169 hp
->ht_name
= DmStrDup( name
);
170 hp
->ht_hash
= hash_key
;
171 hp
->ht_next
= tab
[ hv
];
174 DB_PRINT( "name", ("Adding %s", name
) );
177 DB_PRINT( "name",("Returning: [%s,%lu]",
178 (hp
== NIL(HASH
)) ? "":hp
->ht_name
, hv
) );
184 Search_table( tab
, name
, phv
, phkey
)
192 *phv
= Hash( name
, phkey
);
194 for( hp
= tab
[ *phv
]; hp
!= NIL(HASH
); hp
= hp
->ht_next
)
195 if( hp
->ht_hash
== *phkey
196 && !strcmp(hp
->ht_name
, name
) )
206 This function pushes hp into the hash of all macros. If a previous
207 instance of the macro exists it is hidden by the new one. If one
208 existed before the new instance inherits some values from the preexisting
209 macro (see below for details).
217 hv
= Hash(hp
->ht_name
, &key
);
219 /* Search for an existing instance of hp->ht_name, if found cur will point
221 for(prev
=NIL(HASH
),cur
=Macs
[hv
]; cur
!=NIL(HASH
); prev
=cur
,cur
=cur
->ht_next
)
222 if( cur
->ht_hash
== key
223 && !strcmp(cur
->ht_name
, hp
->ht_name
) )
226 if (cur
== NIL(HASH
) || prev
== NIL(HASH
)) {
227 /* If no match or was found or the first element of Macs[hv] was
228 * the match insert hp at the beginning. */
229 hp
->ht_next
= Macs
[hv
];
233 /* otherwise insert hp in the chain. */
234 hp
->ht_next
= prev
->ht_next
;
238 /* Inherit some parts of the former instance. Copying cur->var to hp->var
239 * copies the old value. Keeping the M_VAR_MASK (variable type) makes sure
240 * the type stays the same keeping M_PRECIOUS assures that the old values
241 * cannot be overridden if it is/was set. */
243 memcpy((void *)&hp
->var
, (void *)&cur
->var
, sizeof(hp
->var
));
244 hp
->ht_flag
|= ((M_VAR_MASK
|M_PRECIOUS
) & cur
->ht_flag
);
254 This function pops (removes) hp from the hash of all macros. If a previous
255 instance of the macro existed it becomes accessible again.
264 hv
= Hash(hp
->ht_name
, &key
);
266 /* Try to find hp. */
267 for(prev
=NIL(HASH
),cur
=Macs
[hv
]; cur
!= NIL(HASH
);prev
=cur
,cur
=cur
->ht_next
)
271 /* If cur == NIL macros was not found. */
272 if (cur
== NIL(HASH
))
275 /* Remove hp from the linked list. */
277 prev
->ht_next
= cur
->ht_next
;
279 Macs
[hv
] = cur
->ht_next
;
281 /* Look for a previous (older) instance of hp->ht_name. */
282 for(cur
=cur
->ht_next
; cur
!= NIL(HASH
); cur
=cur
->ht_next
)
283 if( cur
->ht_hash
== key
284 && !strcmp(cur
->ht_name
, hp
->ht_name
) )
287 /* If one was found we restore the typecast values. */
289 set_macro_value(cur
);
291 hp
->ht_next
= NIL(HASH
);
298 Def_macro( name
, value
, flags
)/*
299 =================================
300 This routine is used to define a macro, and it's value. A copy of
301 the content of value is stored and not the pointer to the value.
302 The flags indicates if it is a permanent macro or if it's value
303 can be redefined. A flags of M_PRECIOUS means it is a precious
304 macro and cannot be further redefined unless M_FORCE is used.
305 If the flags flag contains the M_MULTI bit it means that the macro
306 can be redefined multiple times and no warning of the redefinitions
308 Once a macro's VAR flags are set they are preserved through all future
311 Macro definitions that have one of the variable bits set are treated
312 specially. In each case the hash table entry var field points at the
313 global variable that can be set by assigning to the macro.
315 bit valued global vars must be computed when the macro value is changed.
316 char valued global vars must have the first char of ht_value copied to
317 them. string valued global vars have the same value as ht_value and should
318 just have the new value of ht_value copied to them. */
320 char *name
; /* macro name to define */
321 char *value
; /* macro value to set */
322 int flags
; /* initial ht_flags */
325 register char *p
, *q
;
327 DB_ENTER( "Def_macro" );
328 DB_PRINT( "mac", ("Defining macro %s = %s, %x", name
, value
, flags
) );
330 /* check to see if name is in the table, if so then just overwrite
331 the previous definition. Otherwise allocate a new node, and
332 stuff it in the hash table, at the front of any linked list */
334 if( Readenv
) flags
|= M_LITERAL
|M_EXPANDED
;
336 hp
= Get_name( name
, Macs
, TRUE
);
338 if ((flags
& M_PUSH
) && hp
->ht_name
!= NIL(char)) {
341 hp
->ht_name
= DmStrDup(thp
->ht_name
);
342 hp
->ht_hash
= thp
->ht_hash
;
344 flags
|= hp
->ht_flag
;
348 if( (hp
->ht_flag
& M_PRECIOUS
) && !(flags
& M_FORCE
) ) {
349 if (Verbose
& V_WARNALL
)
350 Warning( "Macro `%s' cannot be redefined", name
);
354 /* Make sure we don't export macros whose names contain legal macro
355 * assignment operators, since we can't do proper quoting in the
357 if( *DmStrPbrk(name
, "*+:=") != '\0' ) flags
|= M_NOEXPORT
;
359 if( hp
->ht_value
!= NIL(char) ) FREE( hp
->ht_value
);
361 if( (hp
->ht_flag
& M_USED
) && !((flags
| hp
->ht_flag
) & M_MULTI
) )
362 Warning( "Macro `%s' redefined after use", name
);
364 /* If an empty string ("") is given set ht_value to NIL(char) */
365 if( (value
!= NIL(char)) && (*value
) ) {
367 if( !(flags
& M_LITERAL
) ) {
369 /* strip out any \<nl> combinations where \ is the current
370 * CONTINUATION char */
371 for(p
=q
; (p
=strchr(p
,CONTINUATION_CHAR
))!=NIL(char); )
373 size_t len
= strlen(p
+2)+1;
374 memmove ( p
, p
+2, len
);
379 p
= DmStrSpn(q
," \t"); /* Strip white space before ... */
381 size_t len
= strlen(p
)+1;
382 memmove( q
, p
, len
);
386 if( *p
) { /* ... and after the value. */
387 for(q
=p
+strlen(p
)-1; ((*q
== ' ')||(*q
== '\t')); q
--);
393 p
= DmStrDup( value
); /* take string literally */
395 if( !*p
) { /* check if result is "" */
400 else if( *DmStrPbrk( p
, "${}" ) == '\0' )
406 hp
->ht_value
= NIL(char);
410 /* Assign the hash table flag less the M_MULTI flag, it is used only
411 * to silence the warning. But carry it over if it was previously
412 * defined in ht_flag, as this is a permanent M_MULTI variable. Keep
413 * the M_PRECIOUS flag and strip the M_INIT flag. */
415 hp
->ht_flag
= ((flags
& ~(M_MULTI
|M_FORCE
)) |
416 (hp
->ht_flag
& (M_VAR_MASK
|M_MULTI
|M_PRECIOUS
))) & ~M_INIT
;
418 /* Check for macro variables and make the necessary adjustment in the
419 * corresponding global variables */
421 if( hp
->ht_flag
& M_VAR_MASK
) {
422 if( !(flags
& M_EXPANDED
) )
423 Error( "Macro variable '%s' must be assigned with :=", name
);
436 Check if a cell for "name" already exists, if not create a new cell.
437 The value of name is normalized before checking/creating the cell to
438 avoid creating multiple cells for the same target file.
439 The function returns a pointer to the cell. */
444 register CELLPTR lib
;
448 DB_ENTER( "Def_cell" );
450 /* Check to see if the cell is a member of the form lib(member) or
451 * lib((symbol)) and handle the cases appropriately.
452 * What we do is we look at the target, if it is of the above two
453 * forms we get the lib, and add the member/symbol to the list of
454 * prerequisites for the library. If this is a symbol name def'n
455 * we additionally add the attribute A_SYMBOL, so that stat can
456 * try to do the right thing. */
458 if( ((member
= strchr(name
, '(')) != NIL(char)) &&
459 ((end
= strrchr(member
, ')')) != NIL(char)) &&
460 (member
> name
) && (member
[-1] != '$') &&
461 (end
> member
+1) && (end
[1] == '\0') )
463 *member
++ = *end
= '\0';
465 if( (*member
== '(') && (member
[strlen(member
)-1] == ')') ) {
466 member
[ strlen(member
)-1 ] = '\0';
467 cp
= Def_cell( member
+1 );
468 cp
->ce_attr
|= A_SYMBOL
;
471 cp
= Def_cell( member
);
473 lib
= Def_cell( name
);
475 Add_prerequisite( lib
, cp
, FALSE
, FALSE
);
476 lib
->ce_attr
|= A_LIBRARY
| A_COMPOSITE
;
478 if( !Def_targets
) cp
= lib
;
481 /* Normalize the name. */
482 DB_PRINT( "path", ("Normalizing [%s]", name
) );
484 /* The normalizing function returns a pointer to a static buffer. */
485 name
= normalize_path(name
);
487 hp
= Get_name( name
, Defs
, TRUE
);/* get the name from hash table */
489 if( hp
->CP_OWNR
== NIL(CELL
) ) /* was it previously defined */
490 { /* NO, so define a new cell */
491 DB_PRINT( "cell", ("Defining cell [%s]", name
) );
493 TALLOC( cp
, 1, CELL
);
496 cp
->ce_fname
= hp
->ht_name
;
497 cp
->ce_all
.cl_prq
= cp
;
499 else /* YES, so return the old cell */
501 DB_PRINT( "cell", ("Getting cell [%s]", hp
->ht_name
) );
513 Add_prerequisite( cell
, prq
, head
, force
)/*
514 ============================================
515 Add a dependency node to the dag. It adds it to the prerequisites,
516 if any, of the cell and makes certain they are in linear order.
517 If head == 1, then add to head of the prerequisite list, else
524 register LINKPTR lp
, tlp
;
526 DB_ENTER( "Add_prerequisite" );
527 DB_PRINT( "cell", ("Defining prerequisite %s", prq
->CE_NAME
) );
529 if( (prq
->ce_flag
& (F_MAGIC
| F_PERCENT
)) && !force
)
530 Fatal( "Special target [%s] cannot be a prerequisite",
533 if( cell
->ce_prq
== NIL(LINK
) ) { /* it's the first one */
534 TALLOC( lp
, 1, LINK
);
538 else { /* search the list, checking for duplicates */
539 for( lp
= cell
->ce_prq
;
540 (lp
->cl_next
!= NIL(LINK
)) && (lp
->cl_prq
!= prq
);
543 /* If the prq is not found and we are at the last prq in the list,
544 * allocate a new prq and place it into the list, insert it at the
545 * head if head == 1, else we add it to the end. */
547 if( lp
->cl_prq
!= prq
) {
548 TALLOC( tlp
, 1, LINK
);
552 tlp
->cl_next
= cell
->ce_prq
;
568 Clear_prerequisites( cell
)/*
569 =============================
570 Clear out the list of prerequisites, freeing all of the LINK nodes,
571 and setting the list to NULL */
576 DB_ENTER( "Clear_prerequisites" );
577 DB_PRINT( "cell", ("Nuking prerequisites") );
579 if( cell
== NIL(CELL
) ) { DB_VOID_RETURN
; }
581 for( lp
=cell
->ce_prq
; lp
!= NIL(LINK
); lp
=tlp
) {
586 cell
->ce_prq
= NIL(LINK
);
593 Test_circle( cp
, fail
)/*
594 =========================
595 Actually run through the graph */
602 DB_ENTER( "Test_circle" );
603 DB_PRINT( "tc", ("checking [%s]", cp
->CE_NAME
) );
605 if( cp
->ce_flag
& F_MARK
) {
607 Fatal("Detected circular dependency in graph at [%s]", cp
->CE_NAME
);
612 cp
->ce_flag
|= F_MARK
;
613 for( lp
= cp
->ce_prq
; !res
&& lp
!= NIL(LINK
); lp
= lp
->cl_next
)
614 res
= Test_circle( lp
->cl_prq
, fail
);
615 cp
->ce_flag
^= F_MARK
;
623 Def_recipe( rcp
, sp
, white_too
, no_check
)/*
624 =============================================
625 Take the recipe (rcp) and add it to the list of recipes pointed to by
626 sp (sp points to the last element). If white_too == TRUE add the recipe
627 even if it contains only white space or an empty string.
628 Return a pointer to the new recipe (or sp if it was discarded).
629 If no_check is true then don't look for -@ at the start of the recipe
636 register STRINGPTR nsp
;
639 DB_ENTER( "Def_recipe" );
640 DB_PRINT( "rul", ("Defining recipe %s", rcp
) );
642 if( !white_too
) rcp
= DmStrSpn( rcp
, " \t" );
643 if( (rcp
== NIL(char)) || (*rcp
== 0 && !white_too
) )
644 DB_RETURN( sp
); /* return last recipe when new recipe not added */
646 rp
= no_check
? rcp
: DmStrSpn( rcp
, " \t@-+%" );
648 TALLOC(nsp
, 1, STRING
);
649 nsp
->st_string
= DmStrDup( rp
);
651 if( sp
!= NIL(STRING
) ) sp
->st_next
= nsp
;
652 nsp
->st_next
= NIL(STRING
);
654 if( !no_check
) nsp
->st_attr
|= Rcp_attribute( rcp
);
661 Rcp_attribute( rp
)/*
662 ======================
663 Look at the recipe and return the set of attributes that it defines. */
666 t_attr flag
= A_DEFAULT
;
673 case '@' : ++atcount
; break;
674 case '-' : flag
|= A_IGNORE
; break;
675 case '+' : flag
|= A_SHELL
; break;
678 /* Ignore % in the non-MSDOS case. */
686 default: done
= TRUE
; break;
689 if( !(Verbose
& V_FORCEECHO
) && atcount
-- ) {
691 /* hide output if more than one @ are encountered. */