mkmakefile.c revision 219819
11553Srgrimes/*
21553Srgrimes * Copyright (c) 1993, 19801990
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 4. Neither the name of the University nor the names of its contributors
141553Srgrimes *    may be used to endorse or promote products derived from this software
151553Srgrimes *    without specific prior written permission.
161553Srgrimes *
171553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271553Srgrimes * SUCH DAMAGE.
281553Srgrimes */
291553Srgrimes
301553Srgrimes#ifndef lint
3129451Scharnier#if 0
321553Srgrimesstatic char sccsid[] = "@(#)mkmakefile.c	8.1 (Berkeley) 6/6/93";
3329451Scharnier#endif
3429451Scharnierstatic const char rcsid[] =
3550479Speter  "$FreeBSD: head/usr.sbin/config/mkmakefile.c 219819 2011-03-21 09:40:01Z jeff $";
361553Srgrimes#endif /* not lint */
371553Srgrimes
381553Srgrimes/*
391553Srgrimes * Build the makefile for the system, from
401553Srgrimes * the information in the files files and the
411553Srgrimes * additional files for the machine being compiled to.
421553Srgrimes */
431553Srgrimes
4429451Scharnier#include <ctype.h>
4529451Scharnier#include <err.h>
461553Srgrimes#include <stdio.h>
4720458Sjoerg#include <string.h>
4869004Simp#include <sys/param.h>
4916073Sphk#include "y.tab.h"
501553Srgrimes#include "config.h"
5130638Speter#include "configvers.h"
521553Srgrimes
531553Srgrimes#define next_word(fp, wd) \
5461640Speter	{ char *word = get_word(fp); \
551553Srgrimes	  if (word == (char *)EOF) \
561553Srgrimes		return; \
571553Srgrimes	  else \
581553Srgrimes		wd = word; \
591553Srgrimes	}
601553Srgrimes#define next_quoted_word(fp, wd) \
6161640Speter	{ char *word = get_quoted_word(fp); \
621553Srgrimes	  if (word == (char *)EOF) \
631553Srgrimes		return; \
641553Srgrimes	  else \
651553Srgrimes		wd = word; \
661553Srgrimes	}
671553Srgrimes
6861640Speterstatic char *tail(char *);
6961640Speterstatic void do_clean(FILE *);
7061640Speterstatic void do_rules(FILE *);
7169135Speterstatic void do_xxfiles(char *, FILE *);
7261640Speterstatic void do_objs(FILE *);
7361640Speterstatic void do_before_depend(FILE *);
7472684Speterstatic int opteq(const char *, const char *);
7561640Speterstatic void read_files(void);
7629451Scharnier
771553Srgrimes/*
781553Srgrimes * Lookup a file, by name.
791553Srgrimes */
8045744Speterstatic struct file_list *
8161640Speterfl_lookup(char *file)
821553Srgrimes{
8361640Speter	struct file_list *fp;
841553Srgrimes
85110895Sru	STAILQ_FOREACH(fp, &ftab, f_next) {
861553Srgrimes		if (eq(fp->f_fn, file))
871553Srgrimes			return (fp);
881553Srgrimes	}
891553Srgrimes	return (0);
901553Srgrimes}
911553Srgrimes
921553Srgrimes/*
931553Srgrimes * Make a new file list entry
941553Srgrimes */
9545744Speterstatic struct file_list *
9661640Speternew_fent(void)
971553Srgrimes{
9861640Speter	struct file_list *fp;
991553Srgrimes
100159362Sdelphij	fp = (struct file_list *) calloc(1, sizeof *fp);
101205880Sru	if (fp == NULL)
102205880Sru		err(EXIT_FAILURE, "calloc");
103110895Sru	STAILQ_INSERT_TAIL(&ftab, fp, f_next);
1041553Srgrimes	return (fp);
1051553Srgrimes}
1061553Srgrimes
1071553Srgrimes/*
108207260Simp * Open the correct Makefile and return it, or error out.
1091553Srgrimes */
110207260SimpFILE *
111207260Simpopen_makefile_template(void)
1121553Srgrimes{
113207260Simp	FILE *ifp;
1141553Srgrimes	char line[BUFSIZ];
1151553Srgrimes
11655614Speter	snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
1171553Srgrimes	ifp = fopen(line, "r");
11855614Speter	if (ifp == 0) {
11955614Speter		snprintf(line, sizeof(line), "Makefile.%s", machinename);
12055614Speter		ifp = fopen(line, "r");
12155614Speter	}
12229451Scharnier	if (ifp == 0)
12329451Scharnier		err(1, "%s", line);
124207260Simp	return (ifp);
125207260Simp}
12612772Speter
127207260Simp/*
128207260Simp * Build the makefile from the skeleton
129207260Simp */
130207260Simpvoid
131207260Simpmakefile(void)
132207260Simp{
133207260Simp	FILE *ifp, *ofp;
134207260Simp	char line[BUFSIZ];
135207260Simp	struct opt *op, *t;
136207260Simp
137207260Simp	read_files();
138207260Simp	ifp = open_makefile_template();
13999923Sbde	ofp = fopen(path("Makefile.new"), "w");
14099923Sbde	if (ofp == 0)
14199923Sbde		err(1, "%s", path("Makefile.new"));
142113951Sdes	fprintf(ofp, "KERN_IDENT=%s\n", ident);
143218544Simp	fprintf(ofp, "MACHINE=%s\n", machinename);
144218544Simp	fprintf(ofp, "MACHINE_ARCH=%s\n", machinearch);
145185186Sthompsa	SLIST_FOREACH_SAFE(op, &mkopt, op_next, t) {
146185186Sthompsa		fprintf(ofp, "%s=%s", op->op_name, op->op_value);
147185186Sthompsa		while ((op = SLIST_NEXT(op, op_append)) != NULL)
148185186Sthompsa			fprintf(ofp, " %s", op->op_value);
149185186Sthompsa		fprintf(ofp, "\n");
150185186Sthompsa	}
1511553Srgrimes	if (debugging)
1521553Srgrimes		fprintf(ofp, "DEBUG=-g\n");
15399923Sbde	if (profiling)
15420395Sbde		fprintf(ofp, "PROFLEVEL=%d\n", profiling);
15552653Smarcel	if (*srcdir != '\0')
15652653Smarcel		fprintf(ofp,"S=%s\n", srcdir);
1571553Srgrimes	while (fgets(line, BUFSIZ, ifp) != 0) {
1581553Srgrimes		if (*line != '%') {
1591553Srgrimes			fprintf(ofp, "%s", line);
1601553Srgrimes			continue;
1611553Srgrimes		}
1625325Sgibbs		if (eq(line, "%BEFORE_DEPEND\n"))
1635325Sgibbs			do_before_depend(ofp);
1645325Sgibbs		else if (eq(line, "%OBJS\n"))
1651553Srgrimes			do_objs(ofp);
16669135Speter		else if (strncmp(line, "%FILES.", 7) == 0)
16769135Speter			do_xxfiles(line, ofp);
1681553Srgrimes		else if (eq(line, "%RULES\n"))
1691553Srgrimes			do_rules(ofp);
1706803Sgibbs		else if (eq(line, "%CLEAN\n"))
1716803Sgibbs			do_clean(ofp);
172207260Simp		else if (strncmp(line, "%VERSREQ=", 9) == 0)
173207260Simp			line[0] = '\0'; /* handled elsewhere */
174207260Simp		else
1751553Srgrimes			fprintf(stderr,
1761553Srgrimes			    "Unknown %% construct in generic makefile: %s",
1771553Srgrimes			    line);
1781553Srgrimes	}
1791553Srgrimes	(void) fclose(ifp);
1801553Srgrimes	(void) fclose(ofp);
18113400Speter	moveifchanged(path("Makefile.new"), path("Makefile"));
182153888Sru}
18334619Seivind
184153888Sru/*
185153888Sru * Build hints.c from the skeleton
186153888Sru */
187153888Sruvoid
188153888Srumakehints(void)
189153888Sru{
190163640Simp	FILE *ifp, *ofp;
191153888Sru	char line[BUFSIZ];
192153888Sru	char *s;
193163638Simp	struct hint *hint;
194153888Sru
19561640Speter	ofp = fopen(path("hints.c.new"), "w");
19661640Speter	if (ofp == NULL)
19761640Speter		err(1, "%s", path("hints.c.new"));
19883594Speter	fprintf(ofp, "#include <sys/types.h>\n");
19983594Speter	fprintf(ofp, "#include <sys/systm.h>\n");
20083594Speter	fprintf(ofp, "\n");
20165091Speter	fprintf(ofp, "int hintmode = %d;\n", hintmode);
20261640Speter	fprintf(ofp, "char static_hints[] = {\n");
203163638Simp	STAILQ_FOREACH(hint, &hints, hint_next) {
204163638Simp		ifp = fopen(hint->hint_name, "r");
205163638Simp		if (ifp == NULL)
206163638Simp			err(1, "%s", hint->hint_name);
20761640Speter		while (fgets(line, BUFSIZ, ifp) != 0) {
20861640Speter			/* zap trailing CR and/or LF */
20961640Speter			while ((s = rindex(line, '\n')) != NULL)
21061640Speter				*s = '\0';
21161640Speter			while ((s = rindex(line, '\r')) != NULL)
21261640Speter				*s = '\0';
21361640Speter			/* remove # comments */
21461640Speter			s = index(line, '#');
21561640Speter			if (s)
21661640Speter				*s = '\0';
21761640Speter			/* remove any whitespace and " characters */
21861640Speter			s = line;
21961640Speter			while (*s) {
22061640Speter				if (*s == ' ' || *s == '\t' || *s == '"') {
22161640Speter					while (*s) {
22261640Speter						s[0] = s[1];
22361640Speter						s++;
22461640Speter					}
22561640Speter					/* start over */
22661640Speter					s = line;
22761640Speter					continue;
22861640Speter				}
22961640Speter				s++;
23061640Speter			}
23161640Speter			/* anything left? */
23261640Speter			if (*line == '\0')
23361652Speter				continue;
23461640Speter			fprintf(ofp, "\"%s\\0\"\n", line);
23561640Speter		}
236163640Simp		fclose(ifp);
23761640Speter	}
23861640Speter	fprintf(ofp, "\"\\0\"\n};\n");
23961640Speter	fclose(ofp);
24061640Speter	moveifchanged(path("hints.c.new"), path("hints.c"));
241153888Sru}
24282393Speter
243153888Sru/*
244153888Sru * Build env.c from the skeleton
245153888Sru */
246153888Sruvoid
247153888Srumakeenv(void)
248153888Sru{
249153888Sru	FILE *ifp, *ofp;
250153888Sru	char line[BUFSIZ];
251153888Sru	char *s;
252153888Sru
25382393Speter	if (env) {
25482393Speter		ifp = fopen(env, "r");
25582393Speter		if (ifp == NULL)
25682393Speter			err(1, "%s", env);
25782393Speter	} else {
25882393Speter		ifp = NULL;
25982393Speter	}
26082393Speter	ofp = fopen(path("env.c.new"), "w");
26182393Speter	if (ofp == NULL)
26282393Speter		err(1, "%s", path("env.c.new"));
26383594Speter	fprintf(ofp, "#include <sys/types.h>\n");
26483594Speter	fprintf(ofp, "#include <sys/systm.h>\n");
26583594Speter	fprintf(ofp, "\n");
26682393Speter	fprintf(ofp, "int envmode = %d;\n", envmode);
26782393Speter	fprintf(ofp, "char static_env[] = {\n");
26882393Speter	if (ifp) {
26982393Speter		while (fgets(line, BUFSIZ, ifp) != 0) {
27082393Speter			/* zap trailing CR and/or LF */
27182393Speter			while ((s = rindex(line, '\n')) != NULL)
27282393Speter				*s = '\0';
27382393Speter			while ((s = rindex(line, '\r')) != NULL)
27482393Speter				*s = '\0';
27582393Speter			/* remove # comments */
27682393Speter			s = index(line, '#');
27782393Speter			if (s)
27882393Speter				*s = '\0';
27982393Speter			/* remove any whitespace and " characters */
28082393Speter			s = line;
28182393Speter			while (*s) {
28282393Speter				if (*s == ' ' || *s == '\t' || *s == '"') {
28382393Speter					while (*s) {
28482393Speter						s[0] = s[1];
28582393Speter						s++;
28682393Speter					}
28782393Speter					/* start over */
28882393Speter					s = line;
28982393Speter					continue;
29082393Speter				}
29182393Speter				s++;
29282393Speter			}
29382393Speter			/* anything left? */
29482393Speter			if (*line == '\0')
29582393Speter				continue;
29682393Speter			fprintf(ofp, "\"%s\\0\"\n", line);
29782393Speter		}
29882393Speter	}
29982393Speter	fprintf(ofp, "\"\\0\"\n};\n");
30082393Speter	if (ifp)
30182393Speter		fclose(ifp);
30282393Speter	fclose(ofp);
30382393Speter	moveifchanged(path("env.c.new"), path("env.c"));
3041553Srgrimes}
3051553Srgrimes
306129119Scognetstatic void
307129073Scognetread_file(char *fname)
3081553Srgrimes{
309162936Sru	char ifname[MAXPATHLEN];
3101553Srgrimes	FILE *fp;
311152811Sru	struct file_list *tp;
31261640Speter	struct device *dp;
31361640Speter	struct opt *op;
314152811Sru	char *wd, *this, *compilewith, *depends, *clean, *warning;
315219819Sjeff	const char *objprefix;
316153889Sru	int compile, match, nreqs, std, filetype,
317134542Speter	    imp_rule, no_obj, before_depend, mandatory, nowerror;
3181553Srgrimes
3191553Srgrimes	fp = fopen(fname, "r");
32029451Scharnier	if (fp == 0)
32129451Scharnier		err(1, "%s", fname);
3221553Srgrimesnext:
3231553Srgrimes	/*
324162936Sru	 * include "filename"
325134542Speter	 * filename    [ standard | mandatory | optional ]
326152862Sru	 *	[ dev* [ | dev* ... ] | profiling-routine ] [ no-obj ]
3278857Srgrimes	 *	[ compile-with "compile rule" [no-implicit-rule] ]
3286803Sgibbs	 *      [ dependency "dependency-list"] [ before-depend ]
32954490Speter	 *	[ clean "file-list"] [ warning "text warning" ]
330219819Sjeff	 *	[ obj-prefix "file prefix"]
3311553Srgrimes	 */
3321553Srgrimes	wd = get_word(fp);
3331553Srgrimes	if (wd == (char *)EOF) {
3341553Srgrimes		(void) fclose(fp);
3351553Srgrimes		return;
336129073Scognet	}
3371553Srgrimes	if (wd == 0)
3381553Srgrimes		goto next;
33952098Speter	if (wd[0] == '#')
3401566Srgrimes	{
34152098Speter		while (((wd = get_word(fp)) != (char *)EOF) && wd)
34252098Speter			;
3431566Srgrimes		goto next;
3441566Srgrimes	}
345162936Sru	if (eq(wd, "include")) {
346162936Sru		next_quoted_word(fp, wd);
347162936Sru		if (wd == 0) {
348210144Simp			fprintf(stderr, "%s: missing include filename.\n",
349210144Simp			    fname);
350162936Sru			exit(1);
351162936Sru		}
352162936Sru		(void) snprintf(ifname, sizeof(ifname), "../../%s", wd);
353162936Sru		read_file(ifname);
354162936Sru		while (((wd = get_word(fp)) != (char *)EOF) && wd)
355162936Sru			;
356162936Sru		goto next;
357162936Sru	}
3581553Srgrimes	this = ns(wd);
3591553Srgrimes	next_word(fp, wd);
3601553Srgrimes	if (wd == 0) {
361210144Simp		fprintf(stderr, "%s: No type for %s.\n", fname, this);
3621553Srgrimes		exit(1);
3631553Srgrimes	}
364152811Sru	tp = fl_lookup(this);
365152862Sru	compile = 0;
366152862Sru	match = 1;
3671553Srgrimes	nreqs = 0;
36873199Speter	compilewith = 0;
3694571Sgibbs	depends = 0;
3706803Sgibbs	clean = 0;
37172684Speter	warning = 0;
37230796Sjoerg	std = mandatory = 0;
3734571Sgibbs	imp_rule = 0;
3744571Sgibbs	no_obj = 0;
3755325Sgibbs	before_depend = 0;
37691002Speter	nowerror = 0;
3771553Srgrimes	filetype = NORMAL;
378219819Sjeff	objprefix = "";
37961523Speter	if (eq(wd, "standard")) {
3801553Srgrimes		std = 1;
38130796Sjoerg	/*
38230796Sjoerg	 * If an entry is marked "mandatory", config will abort if it's
38330796Sjoerg	 * not called by a configuration line in the config file.  Apart
38430796Sjoerg	 * from this, the device is handled like one marked "optional".
38530796Sjoerg	 */
38661523Speter	} else if (eq(wd, "mandatory")) {
38730796Sjoerg		mandatory = 1;
38861523Speter	} else if (!eq(wd, "optional")) {
389210144Simp		fprintf(stderr,
390214654Sobrien		    "%s: \"%s\" %s must be optional, mandatory or standard\n",
391214654Sobrien		    fname, wd, this);
3921553Srgrimes		exit(1);
3931553Srgrimes	}
3941553Srgrimesnextparam:
3951553Srgrimes	next_word(fp, wd);
396113397Sphk	if (wd == 0) {
397152862Sru		compile += match;
398152862Sru		if (compile && tp == NULL)
399152862Sru			goto doneparam;
400152862Sru		goto next;
401113397Sphk	}
402152862Sru	if (eq(wd, "|")) {
403152862Sru		if (nreqs == 0) {
404210144Simp			fprintf(stderr, "%s: syntax error describing %s\n",
405152862Sru			    fname, this);
406152862Sru			exit(1);
407152862Sru		}
408152862Sru		compile += match;
409152862Sru		match = 1;
410152862Sru		nreqs = 0;
411152862Sru		goto nextparam;
412152862Sru	}
4134571Sgibbs	if (eq(wd, "no-obj")) {
4144571Sgibbs		no_obj++;
4154571Sgibbs		goto nextparam;
4164571Sgibbs	}
4174571Sgibbs	if (eq(wd, "no-implicit-rule")) {
41873199Speter		if (compilewith == 0) {
419210144Simp			fprintf(stderr, "%s: alternate rule required when "
420210144Simp			    "\"no-implicit-rule\" is specified.\n",
421210144Simp			    fname);
4224571Sgibbs		}
4234571Sgibbs		imp_rule++;
4244571Sgibbs		goto nextparam;
4254571Sgibbs	}
4265325Sgibbs	if (eq(wd, "before-depend")) {
4275325Sgibbs		before_depend++;
4285325Sgibbs		goto nextparam;
4295325Sgibbs	}
4306803Sgibbs	if (eq(wd, "dependency")) {
4314571Sgibbs		next_quoted_word(fp, wd);
4324571Sgibbs		if (wd == 0) {
433210144Simp			fprintf(stderr,
434210144Simp			    "%s: %s missing compile command string.\n",
435210144Simp			    fname, this);
4364571Sgibbs			exit(1);
4374571Sgibbs		}
4384571Sgibbs		depends = ns(wd);
4394571Sgibbs		goto nextparam;
4404571Sgibbs	}
4416803Sgibbs	if (eq(wd, "clean")) {
4425325Sgibbs		next_quoted_word(fp, wd);
4435325Sgibbs		if (wd == 0) {
444210144Simp			fprintf(stderr, "%s: %s missing clean file list.\n",
445210144Simp			    fname, this);
4465325Sgibbs			exit(1);
4475325Sgibbs		}
4486803Sgibbs		clean = ns(wd);
4495325Sgibbs		goto nextparam;
4505325Sgibbs	}
4511553Srgrimes	if (eq(wd, "compile-with")) {
4521553Srgrimes		next_quoted_word(fp, wd);
4531553Srgrimes		if (wd == 0) {
454210144Simp			fprintf(stderr,
455210144Simp			    "%s: %s missing compile command string.\n",
456210144Simp			    fname, this);
4571553Srgrimes			exit(1);
4581553Srgrimes		}
45973199Speter		compilewith = ns(wd);
4601553Srgrimes		goto nextparam;
4611553Srgrimes	}
46254490Speter	if (eq(wd, "warning")) {
46354490Speter		next_quoted_word(fp, wd);
46454490Speter		if (wd == 0) {
465210144Simp			fprintf(stderr,
466210144Simp			    "%s: %s missing warning text string.\n",
467210144Simp			    fname, this);
46854490Speter			exit(1);
46954490Speter		}
47072684Speter		warning = ns(wd);
47154490Speter		goto nextparam;
47254490Speter	}
473219819Sjeff	if (eq(wd, "obj-prefix")) {
474219819Sjeff		next_quoted_word(fp, wd);
475219819Sjeff		if (wd == 0) {
476219819Sjeff			printf("%s: %s missing object prefix string.\n",
477219819Sjeff				fname, this);
478219819Sjeff			exit(1);
479219819Sjeff		}
480219819Sjeff		objprefix = ns(wd);
481219819Sjeff		goto nextparam;
482219819Sjeff	}
4831553Srgrimes	nreqs++;
48438782Snsouch	if (eq(wd, "local")) {
48538782Snsouch		filetype = LOCAL;
48638782Snsouch		goto nextparam;
48738782Snsouch	}
48838782Snsouch	if (eq(wd, "no-depend")) {
48938782Snsouch		filetype = NODEPEND;
49038782Snsouch		goto nextparam;
49138782Snsouch	}
4921553Srgrimes	if (eq(wd, "profiling-routine")) {
4931553Srgrimes		filetype = PROFILING;
4941553Srgrimes		goto nextparam;
4951553Srgrimes	}
49691002Speter	if (eq(wd, "nowerror")) {
49791002Speter		nowerror = 1;
49891002Speter		goto nextparam;
49991002Speter	}
500110895Sru	STAILQ_FOREACH(dp, &dtab, d_next)
501152811Sru		if (eq(dp->d_name, wd)) {
502152811Sru			dp->d_done |= DEVDONE;
503153889Sru			goto nextparam;
504152811Sru		}
50530796Sjoerg	if (mandatory) {
506210144Simp		fprintf(stderr, "%s: mandatory device \"%s\" not found\n",
50730796Sjoerg		       fname, wd);
50830796Sjoerg		exit(1);
50930796Sjoerg	}
5101553Srgrimes	if (std) {
511210144Simp		fprintf(stderr,
512210144Simp		    "standard entry %s has a device keyword - %s!\n",
513210144Simp		    this, wd);
51473193Speter		exit(1);
5151553Srgrimes	}
516110895Sru	SLIST_FOREACH(op, &opt, op_next)
517152811Sru		if (op->op_value == 0 && opteq(op->op_name, wd))
5181553Srgrimes			goto nextparam;
519152862Sru	match = 0;
520152862Sru	goto nextparam;
5211553Srgrimes
5221553Srgrimesdoneparam:
5231553Srgrimes	if (std == 0 && nreqs == 0) {
524210144Simp		fprintf(stderr, "%s: what is %s optional on?\n",
5251553Srgrimes		    fname, this);
5261553Srgrimes		exit(1);
5271553Srgrimes	}
5281553Srgrimes
5291553Srgrimes	if (wd) {
530210144Simp		fprintf(stderr, "%s: syntax error describing %s\n",
5311553Srgrimes		    fname, this);
5321553Srgrimes		exit(1);
5331553Srgrimes	}
5341553Srgrimes	if (filetype == PROFILING && profiling == 0)
5351553Srgrimes		goto next;
536152811Sru	tp = new_fent();
5371553Srgrimes	tp->f_fn = this;
5381553Srgrimes	tp->f_type = filetype;
5394571Sgibbs	if (imp_rule)
5404571Sgibbs		tp->f_flags |= NO_IMPLCT_RULE;
5414571Sgibbs	if (no_obj)
5424571Sgibbs		tp->f_flags |= NO_OBJ;
5435325Sgibbs	if (before_depend)
5445325Sgibbs		tp->f_flags |= BEFORE_DEPEND;
54591002Speter	if (nowerror)
54691002Speter		tp->f_flags |= NOWERROR;
54773199Speter	tp->f_compilewith = compilewith;
5484571Sgibbs	tp->f_depends = depends;
5496803Sgibbs	tp->f_clean = clean;
55072684Speter	tp->f_warn = warning;
551219819Sjeff	tp->f_objprefix = objprefix;
5521553Srgrimes	goto next;
5531553Srgrimes}
5541553Srgrimes
555129073Scognet/*
556129073Scognet * Read in the information about files used in making the system.
557129073Scognet * Store it in the ftab linked list.
558129073Scognet */
559129073Scognetstatic void
560129073Scognetread_files(void)
561129073Scognet{
562129073Scognet	char fname[MAXPATHLEN];
563129073Scognet	struct files_name *nl, *tnl;
564129073Scognet
565129073Scognet	(void) snprintf(fname, sizeof(fname), "../../conf/files");
566129073Scognet	read_file(fname);
567129073Scognet	(void) snprintf(fname, sizeof(fname),
568129073Scognet		       	"../../conf/files.%s", machinename);
569129073Scognet	read_file(fname);
570129073Scognet	for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
571129073Scognet		read_file(nl->f_name);
572129073Scognet		tnl = STAILQ_NEXT(nl, f_next);
573129073Scognet		free(nl->f_name);
574129073Scognet		free(nl);
575129073Scognet	}
576129073Scognet}
577129073Scognet
57845744Speterstatic int
57972684Speteropteq(const char *cp, const char *dp)
5801553Srgrimes{
5811553Srgrimes	char c, d;
5821553Srgrimes
5831553Srgrimes	for (; ; cp++, dp++) {
5841553Srgrimes		if (*cp != *dp) {
5851553Srgrimes			c = isupper(*cp) ? tolower(*cp) : *cp;
5861553Srgrimes			d = isupper(*dp) ? tolower(*dp) : *dp;
5871553Srgrimes			if (c != d)
5881553Srgrimes				return (0);
5891553Srgrimes		}
5901553Srgrimes		if (*cp == 0)
5911553Srgrimes			return (1);
5921553Srgrimes	}
5931553Srgrimes}
5941553Srgrimes
59545744Speterstatic void
59661640Speterdo_before_depend(FILE *fp)
5975325Sgibbs{
59861640Speter	struct file_list *tp;
59961640Speter	int lpos, len;
6005325Sgibbs
6015325Sgibbs	fputs("BEFORE_DEPEND=", fp);
6025325Sgibbs	lpos = 15;
603110895Sru	STAILQ_FOREACH(tp, &ftab, f_next)
6045325Sgibbs		if (tp->f_flags & BEFORE_DEPEND) {
6055325Sgibbs			len = strlen(tp->f_fn);
6065325Sgibbs			if ((len = 3 + len) + lpos > 72) {
6075325Sgibbs				lpos = 8;
6085325Sgibbs				fputs("\\\n\t", fp);
6095325Sgibbs			}
6105325Sgibbs			if (tp->f_flags & NO_IMPLCT_RULE)
6115325Sgibbs				fprintf(fp, "%s ", tp->f_fn);
6125325Sgibbs			else
6135325Sgibbs				fprintf(fp, "$S/%s ", tp->f_fn);
6145325Sgibbs			lpos += len + 1;
6155325Sgibbs		}
6165325Sgibbs	if (lpos != 8)
6175325Sgibbs		putc('\n', fp);
6185325Sgibbs}
6195325Sgibbs
62045744Speterstatic void
62161640Speterdo_objs(FILE *fp)
6221553Srgrimes{
62361640Speter	struct file_list *tp;
62461640Speter	int lpos, len;
62561640Speter	char *cp, och, *sp;
6261553Srgrimes
6271553Srgrimes	fprintf(fp, "OBJS=");
6281553Srgrimes	lpos = 6;
629110895Sru	STAILQ_FOREACH(tp, &ftab, f_next) {
630152811Sru		if (tp->f_flags & NO_OBJ)
6311553Srgrimes			continue;
6321553Srgrimes		sp = tail(tp->f_fn);
6331553Srgrimes		cp = sp + (len = strlen(sp)) - 1;
6341553Srgrimes		och = *cp;
6351553Srgrimes		*cp = 'o';
636219819Sjeff		len += strlen(tp->f_objprefix);
6371553Srgrimes		if (len + lpos > 72) {
6381553Srgrimes			lpos = 8;
6391553Srgrimes			fprintf(fp, "\\\n\t");
6401553Srgrimes		}
641219819Sjeff		fprintf(fp, "%s%s ", tp->f_objprefix, sp);
6421553Srgrimes		lpos += len + 1;
6431553Srgrimes		*cp = och;
6441553Srgrimes	}
6451553Srgrimes	if (lpos != 8)
6461553Srgrimes		putc('\n', fp);
6471553Srgrimes}
6481553Srgrimes
64945744Speterstatic void
65069135Speterdo_xxfiles(char *tag, FILE *fp)
6511553Srgrimes{
65261640Speter	struct file_list *tp;
65369135Speter	int lpos, len, slen;
65469135Speter	char *suff, *SUFF;
6551553Srgrimes
65669135Speter	if (tag[strlen(tag) - 1] == '\n')
65769135Speter		tag[strlen(tag) - 1] = '\0';
65869135Speter
65969135Speter	suff = ns(tag + 7);
66069135Speter	SUFF = ns(suff);
66169135Speter	raisestr(SUFF);
66269135Speter	slen = strlen(suff);
66369135Speter
66469135Speter	fprintf(fp, "%sFILES=", SUFF);
6651553Srgrimes	lpos = 8;
666110895Sru	STAILQ_FOREACH(tp, &ftab, f_next)
667152811Sru		if (tp->f_type != NODEPEND) {
6681553Srgrimes			len = strlen(tp->f_fn);
66969135Speter			if (tp->f_fn[len - slen - 1] != '.')
6701553Srgrimes				continue;
67169135Speter			if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
67269135Speter				continue;
6731553Srgrimes			if ((len = 3 + len) + lpos > 72) {
6741553Srgrimes				lpos = 8;
6751553Srgrimes				fputs("\\\n\t", fp);
6761553Srgrimes			}
67738782Snsouch			if (tp->f_type != LOCAL)
67838782Snsouch				fprintf(fp, "$S/%s ", tp->f_fn);
67938782Snsouch			else
68038782Snsouch				fprintf(fp, "%s ", tp->f_fn);
6811553Srgrimes			lpos += len + 1;
6821553Srgrimes		}
6831553Srgrimes	if (lpos != 8)
6841553Srgrimes		putc('\n', fp);
6851553Srgrimes}
6861553Srgrimes
68745744Speterstatic char *
68861640Spetertail(char *fn)
6891553Srgrimes{
69061640Speter	char *cp;
6911553Srgrimes
6921553Srgrimes	cp = rindex(fn, '/');
6931553Srgrimes	if (cp == 0)
6941553Srgrimes		return (fn);
6951553Srgrimes	return (cp+1);
6961553Srgrimes}
6971553Srgrimes
6981553Srgrimes/*
6991553Srgrimes * Create the makerules for each file
7001553Srgrimes * which is part of the system.
7011553Srgrimes */
70245744Speterstatic void
70361640Speterdo_rules(FILE *f)
7041553Srgrimes{
705160495Sstefanf	char *cp, *np, och;
70661640Speter	struct file_list *ftp;
70773199Speter	char *compilewith;
708209135Simp	char cmd[128];
7091553Srgrimes
710110895Sru	STAILQ_FOREACH(ftp, &ftab, f_next) {
71154490Speter		if (ftp->f_warn)
712210144Simp			fprintf(stderr, "WARNING: %s\n", ftp->f_warn);
7131553Srgrimes		cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
7141553Srgrimes		och = *cp;
7154571Sgibbs		if (ftp->f_flags & NO_IMPLCT_RULE) {
7164571Sgibbs			if (ftp->f_depends)
717219819Sjeff				fprintf(f, "%s%s: %s\n",
718219819Sjeff					ftp->f_objprefix, np, ftp->f_depends);
7194571Sgibbs			else
720219819Sjeff				fprintf(f, "%s%s: \n", ftp->f_objprefix, np);
7211553Srgrimes		}
7224571Sgibbs		else {
7234571Sgibbs			*cp = '\0';
7244571Sgibbs			if (och == 'o') {
725219819Sjeff				fprintf(f, "%s%so:\n\t-cp $S/%so .\n\n",
726219819Sjeff					ftp->f_objprefix, tail(np), np);
7274571Sgibbs				continue;
7284571Sgibbs			}
729116450Smarkm			if (ftp->f_depends) {
730219819Sjeff				fprintf(f, "%s%sln: $S/%s%c %s\n",
731219819Sjeff					ftp->f_objprefix, tail(np), np, och,
732219819Sjeff					ftp->f_depends);
733116450Smarkm				fprintf(f, "\t${NORMAL_LINT}\n\n");
734219819Sjeff				fprintf(f, "%s%so: $S/%s%c %s\n",
735219819Sjeff					ftp->f_objprefix, tail(np), np, och,
736219819Sjeff					ftp->f_depends);
737116450Smarkm			}
738116450Smarkm			else {
739219819Sjeff				fprintf(f, "%s%sln: $S/%s%c\n",
740219819Sjeff					ftp->f_objprefix, tail(np), np, och);
741116450Smarkm				fprintf(f, "\t${NORMAL_LINT}\n\n");
742219819Sjeff				fprintf(f, "%s%so: $S/%s%c\n",
743219819Sjeff					ftp->f_objprefix, tail(np), np, och);
744116450Smarkm			}
7454571Sgibbs		}
74673199Speter		compilewith = ftp->f_compilewith;
74773199Speter		if (compilewith == 0) {
74872684Speter			const char *ftype = NULL;
7491553Srgrimes
7501553Srgrimes			switch (ftp->f_type) {
7511553Srgrimes			case NORMAL:
7521553Srgrimes				ftype = "NORMAL";
7531553Srgrimes				break;
7541553Srgrimes			case PROFILING:
7551553Srgrimes				if (!profiling)
7561553Srgrimes					continue;
7571553Srgrimes				ftype = "PROFILE";
7581553Srgrimes				break;
7591553Srgrimes			default:
760210144Simp				fprintf(stderr,
761210144Simp				    "config: don't know rules for %s\n", np);
7621553Srgrimes				break;
7631553Srgrimes			}
764209135Simp			snprintf(cmd, sizeof(cmd),
765209135Simp			    "${%s_%c%s}\n\t@${NORMAL_CTFCONVERT}", ftype,
76691002Speter			    toupper(och),
76791002Speter			    ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
76873199Speter			compilewith = cmd;
7691553Srgrimes		}
7701553Srgrimes		*cp = och;
771219819Sjeff		if (strlen(ftp->f_objprefix))
772219819Sjeff			fprintf(f, "\t%s $S/%s\n\n", compilewith, np);
773219819Sjeff		else
774219819Sjeff			fprintf(f, "\t%s\n\n", compilewith);
7751553Srgrimes	}
7761553Srgrimes}
7771553Srgrimes
77845744Speterstatic void
77961640Speterdo_clean(FILE *fp)
7806803Sgibbs{
78161640Speter	struct file_list *tp;
78261640Speter	int lpos, len;
7836803Sgibbs
7846803Sgibbs	fputs("CLEAN=", fp);
7856803Sgibbs	lpos = 7;
786110895Sru	STAILQ_FOREACH(tp, &ftab, f_next)
7876803Sgibbs		if (tp->f_clean) {
7886803Sgibbs			len = strlen(tp->f_clean);
7896803Sgibbs			if (len + lpos > 72) {
7906803Sgibbs				lpos = 8;
7916803Sgibbs				fputs("\\\n\t", fp);
7926803Sgibbs			}
7936803Sgibbs			fprintf(fp, "%s ", tp->f_clean);
7946803Sgibbs			lpos += len + 1;
7956803Sgibbs		}
7966803Sgibbs	if (lpos != 8)
7976803Sgibbs		putc('\n', fp);
7986803Sgibbs}
7996803Sgibbs
8001553Srgrimeschar *
80161640Speterraisestr(char *str)
8021553Srgrimes{
80361640Speter	char *cp = str;
8041553Srgrimes
8051553Srgrimes	while (*str) {
8061553Srgrimes		if (islower(*str))
8071553Srgrimes			*str = toupper(*str);
8081553Srgrimes		str++;
8091553Srgrimes	}
8101553Srgrimes	return (cp);
8111553Srgrimes}
812