summaryrefslogtreecommitdiff
path: root/usr/src/make_src/Make/bin/make/common/rep.cc
blob: d6b35f99a8cc63e2020ee37034fab9939abc4416 (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
 * Use is subject to license terms.
 */
/*
 * @(#)rep.cc 1.25 06/12/12
 */

#pragma	ident	"@(#)rep.cc	1.25	06/12/12"

/*
 *	rep.c
 *
 *	This file handles the .nse_depinfo file
 */

/*
 * Included files
 */ 
#include <mk/defs.h>
#include <mksh/misc.h>		/* retmem() */
#include <vroot/report.h>	/* NSE_DEPINFO */

/*
 * Static variables
 */
static	Recursive_make	recursive_list;
static	Recursive_make	*bpatch = &recursive_list;
static	Boolean		changed;

/*
 * File table of contents
 */


/*
 *	report_recursive_init()
 *
 *	Read the .nse_depinfo file and make a list of all the
 *	.RECURSIVE entries.
 *
 *	Parameters:
 *
 *	Static variables used:
 *		bpatch		Points to slot where next cell should be added
 *
 *	Global variables used:
 *		recursive_name	The Name ".RECURSIVE", compared against
 */

void
report_recursive_init(void)
{
	char		*search_dir;
	char		nse_depinfo[MAXPATHLEN];
	FILE		*fp;
	int		line_size, line_index;
	wchar_t		*line;
	wchar_t		*bigger_line;
	wchar_t		*colon;
	wchar_t		*dollar; 
	Recursive_make	rp;

	/*
	 * This routine can be called more than once,  don't do
	 * anything after the first time.
	 */
	if (depinfo_already_read) {
		return;
	} else {
		depinfo_already_read = true;
	}
					
	search_dir = getenv(NOCATGETS("NSE_DEP"));
	if (search_dir == NULL) {
		return;
	}
	(void) sprintf(nse_depinfo, "%s/%s", search_dir, NSE_DEPINFO);
	fp = fopen(nse_depinfo, "r");
	if (fp == NULL) {
		return;
	}
	line_size = MAXPATHLEN;
	line_index = line_size - 1;
	line = ALLOC_WC(line_size);
	Wstring rns(recursive_name);
	wchar_t * wcb = rns.get_string();
	while (fgetws(line, line_size, fp) != NULL) {
		while (wslen(line) == line_index) {
			if (line[wslen(line) - 1] == '\n') {
				continue;
			}
			bigger_line = ALLOC_WC(2 * line_size);
			wscpy(bigger_line, line);
			retmem(line);
			line = bigger_line;
			if (fgetws(&line[line_index], line_size, fp) == NULL)
				continue;
			line_index = 2 * line_index;
			line_size = 2 * line_size;
		}

		colon = (wchar_t *) wschr(line, (int) colon_char);
		if (colon == NULL) {
			continue;
		}
		dollar = (wchar_t *) wschr(line, (int) dollar_char);
		line[wslen(line) - 1] = (int) nul_char;
		if (IS_WEQUALN(&colon[2], wcb,
	            (int) recursive_name->hash.length)) {
			/*
			 * If this entry is an old entry, ignore it
			 */
			MBSTOWCS(wcs_buffer, DEPINFO_FMT_VERSION);
			if (dollar == NULL ||
			    !IS_WEQUALN(wcs_buffer, (dollar+1) - VER_LEN, VER_LEN)){
				continue;
			    }
			rp = ALLOC(Recursive_make);
			(void) memset((char *) rp, 0, sizeof (Recursive_make_rec));
			/*
			 * set conditional_macro_string if string is present
			 */
			rp->oldline = (wchar_t *) wcsdup(line);
			if ( dollar != NULL ){
				rp->cond_macrostring = 
				    (wchar_t *) wcsdup(dollar - VER_LEN + 1);
			}
			/* 
			 * get target name into recursive struct
			 */
			*colon = (int) nul_char;
			rp->target = (wchar_t *) wcsdup(line);
			*bpatch = rp;
			bpatch = &rp->next;
		}
	}
	(void) fclose(fp);
}

/*
 *	report_recursive_dep(target, line)
 *
 *	Report a target as recursive.
 *
 *	Parameters:
 *		line		Dependency line reported
 *
 *	Static variables used:
 *		bpatch		Points to slot where next cell should be added
 *		changed		Written if report set changed
 */
void
report_recursive_dep(Name target, wchar_t *line)
{
	Recursive_make	rp;
	wchar_t		rec_buf[STRING_BUFFER_LENGTH];
	String_rec	string;

	INIT_STRING_FROM_STACK(string, rec_buf);
	cond_macros_into_string(target, &string);
	/* 
	 * find an applicable recursive entry, if there isn't one, create it
	 */
	rp = find_recursive_target(target);
	if (rp == NULL) {
		rp = ALLOC(Recursive_make);
		(void) memset((char *) rp, 0, sizeof (Recursive_make_rec));
		wchar_t * wcb = get_wstring(target->string_mb); // XXX Tolik: needs retmem
                rp->target = wcb;
		rp->newline = (wchar_t *) wcsdup(line);
		rp->cond_macrostring = (wchar_t *) wcsdup(rec_buf);
		*bpatch = rp;
		bpatch = &rp->next;
		changed = true;
	} else {
		if ((rp->oldline != NULL) && !IS_WEQUAL(rp->oldline, line)) {
			rp->newline = (wchar_t *) wcsdup(line);
			changed = true;
		}
		rp->removed = false;
	}
}

/*
 *	find_recursive_target(target)
 *
 *	Search the list for a given target.
 *
 *	Return value:
 *				The target cell
 *
 *	Parameters:
 *		target		The target we need
 *		top_level_target more info used to determinde the 
 *				 target we need
 *
 *	Static variables used:
 *		recursive_list	The list of targets
 */
Recursive_make
find_recursive_target(Name target)
{
	Recursive_make	rp;
	String_rec	string;
	wchar_t		rec_buf[STRING_BUFFER_LENGTH]; 

	INIT_STRING_FROM_STACK(string, rec_buf);
	cond_macros_into_string(target, &string);

	Wstring tstr(target);
	wchar_t * wcb = tstr.get_string();
	for (rp = recursive_list; rp != NULL; rp = rp->next) {
		/* 
		 * If this entry has already been removed, ignore it.
		 */
		if (rp->removed)
			continue;
		/* 
		 * If this target, and the target on the list are the same
		 * and if one of them contains conditional macro info, while
		 * the other doesn't,  remove this entry from the list of
		 * recursive entries.  This can only happen if the Makefile
		 * has changed to no longer contain conditional macros.
		 */
		if (IS_WEQUAL(rp->target, wcb)) {
			if (rp->cond_macrostring[VER_LEN] == '\0' &&
			    string.buffer.start[VER_LEN] != '\0'){
				rp->removed = true;
				continue;
			} else if (rp->cond_macrostring[VER_LEN] != '\0' &&
			    string.buffer.start[VER_LEN] == '\0'){
				rp->removed = true;
				continue;
			} 
		}
		/*
		 * If this is not a VERS2 entry,  only need to match
		 * the target name.  toptarg information from VERS1 entries
		 * are ignored.
		 */
		MBSTOWCS(wcs_buffer, DEPINFO_FMT_VERSION);
		if (IS_WEQUALN(wcs_buffer, string.buffer.start, VER_LEN)) { 
			if (IS_WEQUAL(rp->cond_macrostring, 
			    string.buffer.start) &&
			    IS_WEQUAL(rp->target, wcb)) {
				return rp;
			}
		} else {
			if (IS_WEQUAL(rp->target, wcb)) {
				return rp;
			}
		}
	}
	return NULL;
}

/*
 *	remove_recursive_dep(target, top_level_target)
 *
 *	Mark a target as no longer recursive.
 *
 *	Parameters:
 *		target		The target we want to remove
 *		top_level_target target we want to remove must be built from 
 *				 the same top level target
 *
 *	Static variables used:
 *		changed		Written if report set changed
 */
void
remove_recursive_dep(Name target)
{
	Recursive_make	rp;

	rp = find_recursive_target(target);

	if ( rp != NULL ) {
		rp->removed = true;	
		changed = true;
		if(rp->target) {
			retmem(rp->target);
			rp->target = NULL;
		}
		if(rp->newline) {
			retmem(rp->newline);
			rp->newline = NULL;
		}
		if(rp->oldline) {
			retmem(rp->oldline);
			rp->oldline = NULL;
		}
		if(rp->cond_macrostring) {
			retmem(rp->cond_macrostring);
			rp->cond_macrostring = NULL;
		}
	}
}

#ifdef NSE
/*
 *	report_recursive_done()
 *
 *	Write the .nse_depinfo file.
 *
 *	Parameters:
 *
 *	Static variables used:
 *		recursive_list	The list of targets
 *		changed		Written if report set changed
 *
 *	Global variables used:
 *		recursive_name	The Name ".RECURSIVE", compared against
 */
void
report_recursive_done(void)
{
	char		*search_dir;
	char		nse_depinfo[MAXPATHLEN];
	char		tmpfile[MAXPATHLEN];
	FILE		*ofp;
	FILE		*ifp;
	wchar_t		*space;
	wchar_t		*data;
	wchar_t		*line;
	wchar_t		*bigger_line;
	int		line_size, line_index;
	int		lock_err;
	Recursive_make	rp;

	if (changed == false) {
		return;
	}

	search_dir = getenv(NOCATGETS("NSE_DEP"));
	if (search_dir == NULL) {
		return;
	}
	(void) sprintf(nse_depinfo, "%s/%s", search_dir, NSE_DEPINFO);
	(void) sprintf(tmpfile, "%s.%d", nse_depinfo, getpid());
	ofp = fopen(tmpfile, "w");
	if (ofp == NULL) {
		(void) fprintf(stderr,
			       catgets(catd, 1, 116, "Cannot open `%s' for writing\n"),
			       tmpfile);
		return;
	}
	(void) sprintf(nse_depinfo_lockfile, 
		       "%s/%s", search_dir, NSE_DEPINFO_LOCK);
	if (lock_err = file_lock(nse_depinfo, 
				 nse_depinfo_lockfile, 
				 (int *) &nse_depinfo_locked, 0)) {
		(void) fprintf(stderr,
			       catgets(catd, 1, 117, "writing .RECURSIVE lines to %s\n"),
			       tmpfile);
		(void) fprintf(stderr,
			       catgets(catd, 1, 118, "To recover, merge .nse_depinfo.%d with .nse_depinfo\n"),
			       getpid(),
			       catgets(catd, 1, 119, "with .nse_depinfo"));
	}

	if (nse_depinfo_locked) {
		ifp = fopen(nse_depinfo, "r");
		if (ifp != NULL) {
			/*
			 * Copy all the non-.RECURSIVE lines from 
			 * the old file to the new one.
			 */
			line_size = MAXPATHLEN;
			line_index = line_size - 1;
			line = ALLOC_WC(line_size);
			while (fgetws(line, line_size, ifp) != NULL) {
				while (wslen(line) == line_index) {
					if (line[wslen(line) - 1] == '\n') {
						continue;
					}
					bigger_line = ALLOC_WC(2 * line_size);
					wscpy(bigger_line, line);
					retmem(line);
					line = bigger_line;
					if (fgetws(&line[line_index], 
						  line_size, ifp) == NULL)
					  continue;
					line_index = 2 * line_index;
					line_size = 2 * line_size;
				}

				space = wschr(line, (int) space_char);
				if (space != NULL && 
				    IS_WEQUALN(&space[1],
					      recursive_name->string,
					      (int) recursive_name->hash.length)) {
					continue;
				}
				WCSTOMBS(mbs_buffer, line);
				(void) fprintf(ofp, "%s", mbs_buffer);
			}
			(void) fclose(ifp);
		}
	}

	/*
	 * Write out the .RECURSIVE lines.
	 */
	for (rp = recursive_list; rp != NULL; rp = rp->next) {
		if (rp->removed) {
			continue;
		}
		if (rp->newline != NULL) {
			data = rp->newline;
		} else {
			data = rp->oldline;
		}
		if (data != NULL) {
			WCSTOMBS(mbs_buffer, data);
			(void) fprintf(ofp, "%s\n", mbs_buffer);
		}
	}
	(void) fclose(ofp);

	if (nse_depinfo_locked) {
		(void) rename(tmpfile, nse_depinfo);
		(void) unlink(nse_depinfo_lockfile);
		nse_depinfo_locked = false;
		nse_depinfo_lockfile[0] = '\0';
		(void) chmod(nse_depinfo, 0666);
	}
}
#endif // NSE

/* gather_recursive_deps()
 *
 *	Create or update list of recursive targets.  
 */
void
gather_recursive_deps(void)
{
	Name_set::iterator	np, e;
	String_rec		rec;
	wchar_t			rec_buf[STRING_BUFFER_LENGTH];
	register Property	lines;
	Boolean			has_recursive;
	Dependency		dp;

	report_recursive_init();

	/* Go thru all targets and dump recursive dependencies */
	for (np = hashtab.begin(), e = hashtab.end(); np != e; np++) {
		if (np->has_recursive_dependency){
			has_recursive = false;
			/* 
			 * start .RECURSIVE line with target:
			 */
			INIT_STRING_FROM_STACK(rec, rec_buf);
			APPEND_NAME(np, &rec, FIND_LENGTH);
			append_char((int) colon_char, &rec);
			append_char((int) space_char, &rec);
			
			for (lines = get_prop(np->prop,recursive_prop); 
			    lines != NULL;
			    lines = get_prop(lines->next, recursive_prop)) {
				/* 
				 * if entry is already in depinfo
				 * file or entry was not built, ignore it
				 */
				if (lines->body.recursive.in_depinfo)
					continue;
				if (!lines->body.recursive.has_built)
					continue;
				has_recursive = true;
				lines->body.recursive.in_depinfo=true;
				
				/* 
				* Write the remainder of the
				* .RECURSIVE line 
				*/
				APPEND_NAME(recursive_name, &rec, 
				    FIND_LENGTH);
				append_char((int) space_char, &rec);
				APPEND_NAME(lines->body.recursive.directory,
					&rec, FIND_LENGTH);
				append_char((int) space_char, &rec);
				APPEND_NAME(lines->body.recursive.target,
					&rec, FIND_LENGTH);
				append_char((int) space_char, &rec);
				
				/* Complete list of makefiles used */
				for (dp = lines->body.recursive.makefiles; 
				    dp != NULL; 
				    dp = dp->next) {
					APPEND_NAME(dp->name, &rec,  FIND_LENGTH);
					append_char((int) space_char, &rec);
				}
			}  
			/* 
			 * dump list of conditional targets,
			 * and report recursive entry, if needed
			 */
			cond_macros_into_string(np, &rec);
			if (has_recursive){
				report_recursive_dep(np, rec.buffer.start);
			}

		} else if ( np->has_built ) {
			remove_recursive_dep(np);
		}
	}
}