]>
git.saurik.com Git - apple/xnu.git/blob - SETUP/config/mkmakefile.c
2 * Copyright (c) 1999-2016 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Mach Operating System
26 * Copyright (c) 1990 Carnegie-Mellon University
27 * Copyright (c) 1989 Carnegie-Mellon University
28 * Copyright (c) 1988 Carnegie-Mellon University
29 * Copyright (c) 1987 Carnegie-Mellon University
30 * All rights reserved. The CMU software License Agreement specifies
31 * the terms and conditions for use and redistribution.
35 * Copyright (c) 1980 Regents of the University of California.
36 * All rights reserved.
38 * Redistribution and use in source and binary forms are permitted
39 * provided that the above copyright notice and this paragraph are
40 * duplicated in all such forms and that any documentation,
41 * advertising materials, and other materials related to such
42 * distribution and use acknowledge that the software was developed
43 * by the University of California, Berkeley. The name of the
44 * University may not be used to endorse or promote products derived
45 * from this software without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
47 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
48 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
52 static char sccsid
[] __attribute__((used
)) = "@(#)mkmakefile.c 5.21 (Berkeley) 6/18/88";
56 * Build the makefile for the system, from
57 * the information in the files files and the
58 * additional files for the machine being compiled to.
62 #include <unistd.h> /* for unlink */
67 void read_files(void);
68 void do_objs(FILE *fp
, const char *msg
, int ext
);
69 void do_files(FILE *fp
, const char *msg
, char ext
);
70 void do_machdep(FILE *ofp
);
71 void do_rules(FILE *f
);
72 void copy_dependencies(FILE *makin
, FILE *makout
);
74 struct file_list
*fl_lookup(char *file
);
75 struct file_list
*fltail_lookup(char *file
);
76 struct file_list
*new_fent(void);
78 void put_source_file_name(FILE *fp
, struct file_list
*tp
);
81 #define next_word(fp, wd) \
82 { const char *word = get_word(fp); \
83 if (word == (char *)EOF) \
89 static struct file_list
*fcur
;
90 const char *tail(const char *fn
);
91 char *allCaps(char *str
);
94 * Lookup a file, by name.
101 for (fp
= ftab
; fp
!= 0; fp
= fp
->f_next
) {
102 if (eq(fp
->f_fn
, file
))
109 * Lookup a file, by final component name.
112 fltail_lookup(char *file
)
114 struct file_list
*fp
;
116 for (fp
= ftab
; fp
!= 0; fp
= fp
->f_next
) {
117 if (eq(tail(fp
->f_fn
), tail(file
)))
124 * Make a new file list entry
129 struct file_list
*fp
;
131 fp
= (struct file_list
*) malloc(sizeof *fp
);
136 fp
->f_extra
= (char *) 0;
150 static char *vpath
= NULL
;
152 if ((vpath
== NULL
) &&
153 ((vpath
= getenv("VPATH")) != NULL
) &&
155 char *buf
= malloc((unsigned)(strlen(vpath
) + 2));
157 vpath
= strcat(strcpy(buf
, ":"), vpath
);
160 return vpath
? vpath
: "";
165 * Build the makefile from the skeleton
177 (void) sprintf(line
, "%s/Makefile.template", config_directory
);
178 ifp
= fopenp(VPATH
, line
, pname
, "r");
183 dfp
= fopen(path("Makefile"), "r");
184 rename(path("Makefile"), path("Makefile.old"));
185 unlink(path("Makefile.old"));
186 ofp
= fopen(path("Makefile"), "w");
188 perror(path("Makefile"));
191 fprintf(ofp
, "SOURCE_DIR=%s\n", source_directory
);
193 fprintf(ofp
, "export CONFIG_DEFINES =");
195 fprintf(ofp
, " -DGPROF");
197 for (op
= opt
; op
; op
= op
->op_next
)
199 fprintf(ofp
, " -D%s=\"%s\"", op
->op_name
, op
->op_value
);
201 fprintf(ofp
, " -D%s", op
->op_name
);
203 for (op
= mkopt
; op
; op
= op
->op_next
)
205 fprintf(ofp
, "%s=%s\n", op
->op_name
, op
->op_value
);
207 fprintf(ofp
, "%s\n", op
->op_name
);
209 while (fgets(line
, BUFSIZ
, ifp
) != 0) {
212 if (profiling
&& strncmp(line
, "COPTS=", 6) == 0) {
215 "GPROF.EX=$(SOURCE_DIR)/machdep/%s/gmon.ex\n", machinename
);
216 cp
= index(line
, '\n');
220 while (*cp
&& (*cp
== ' ' || *cp
== '\t'))
222 COPTS
= malloc((unsigned)(strlen(cp
) + 1));
224 printf("config: out of memory\n");
228 fprintf(ofp
, "%s -pg\n", line
);
231 fprintf(ofp
, "%s", line
);
234 if (eq(line
, "%OBJS\n")) {
235 do_objs(ofp
, "OBJS=", -1);
236 } else if (eq(line
, "%CFILES\n")) {
237 do_files(ofp
, "CFILES=", 'c');
238 do_objs(ofp
, "COBJS=", 'c');
239 } else if (eq(line
, "%CXXFILES\n")) {
240 do_files(ofp
, "CXXFILES=", 'p');
241 do_objs(ofp
, "CXXOBJS=", 'p');
242 } else if (eq(line
, "%SFILES\n")) {
243 do_files(ofp
, "SFILES=", 's');
244 do_objs(ofp
, "SOBJS=", 's');
245 } else if (eq(line
, "%MACHDEP\n")) {
247 } else if (eq(line
, "%RULES\n"))
251 "Unknown %% construct in generic makefile: %s",
256 copy_dependencies(dfp
, ofp
);
264 * Read in the information about files used in making the system.
265 * Store it in the ftab linked list.
271 struct file_list
*tp
, *pf
;
276 const char *devorprof
;
281 char *rest
= (char *) 0;
282 int nreqs
, first
= 1, isdup
;
285 (void) sprintf(fname
, "%s/files", config_directory
);
287 fp
= fopenp(VPATH
, fname
, pname
, "r");
296 * filename [ standard | optional ]
297 * [ dev* | profiling-routine ] [ device-driver]
300 if (wd
== (char *)EOF
) {
303 (void) sprintf(fname
, "%s/files.%s", config_directory
, machinename
);
312 * Allow comment lines beginning witha '#' character.
316 while ((wd
=get_word(fp
)) && wd
!= (char *)EOF
)
324 printf("%s: No type for %s.\n",
328 if ((pf
= fl_lookup(this)) && (pf
->f_type
!= INVISIBLE
|| pf
->f_flags
))
336 if (eq(wd
, "standard"))
338 if (!eq(wd
, "optional")) {
339 printf("%s: %s must be optional or standard\n", fname
, this);
342 if (strncmp(this, "OPTIONS/", 8) == 0)
350 not_option
= !not_option
;
354 if (eq(wd
, "device-driver") || eq(wd
, "profiling-routine")) {
359 if (needs
== 0 && nreqs
== 1)
369 * Allocate a pseudo-device entry which we will insert into
370 * the device list below. The flags field is set non-zero to
371 * indicate an internal entry rather than one generated from
372 * the configuration file. The slave field is set to define
373 * the corresponding symbol as 0 should we fail to find the
374 * option in the option list.
377 tdev
.d_name
= ns(wd
);
378 tdev
.d_type
= PSEUDO_DEVICE
;
382 for (op
=opt
; op
; lop
=op
, op
=op
->op_next
)
384 char *od
= allCaps(ns(wd
));
387 * Found an option which matches the current device
388 * dependency identifier. Set the slave field to
389 * define the option in the header file.
391 if (strcmp(op
->op_name
, od
) == 0)
397 lop
->op_next
= op
->op_next
;
407 for (dp
= dtab
; dp
!= 0; dp
= dp
->d_next
) {
408 if (eq(dp
->d_name
, wd
) && (dp
->d_type
!= PSEUDO_DEVICE
|| dp
->d_slave
)) {
410 goto invis
; /* dont want file if option present */
416 goto nextopt
; /* want file if option missing */
418 for (op
= opt
; op
!= 0; op
= op
->op_next
)
419 if (op
->op_value
== 0 && opteq(op
->op_name
, wd
)) {
428 while ((wd
= get_word(fp
)) != 0)
433 tp
->f_type
= INVISIBLE
;
440 printf("%s: what is %s optional on?\n",
460 rest
= ns(get_rest(fp
));
462 printf("%s: syntax error describing %s\n",
467 if (eq(devorprof
, "profiling-routine") && profiling
== 0)
474 tp
->f_type
= INVISIBLE
;
476 if (eq(devorprof
, "device-driver"))
478 else if (eq(devorprof
, "profiling-routine"))
479 tp
->f_type
= PROFILING
;
484 if (pf
&& pf
->f_type
== INVISIBLE
)
485 pf
->f_flags
= 1; /* mark as duplicate */
490 opteq(const char *cp
, const char *dp
)
494 for (; ; cp
++, dp
++) {
496 c
= isupper(*cp
) ? tolower(*cp
) : *cp
;
497 d
= isupper(*dp
) ? tolower(*dp
) : *dp
;
507 put_source_file_name(FILE *fp
, struct file_list
*tp
)
509 if ((tp
->f_fn
[0] == '.') && (tp
->f_fn
[1] == '/'))
510 fprintf(fp
, "%s ", tp
->f_fn
);
512 fprintf(fp
, "$(SOURCE_DIR)/%s ", tp
->f_fn
);
516 do_objs(FILE *fp
, const char *msg
, int ext
)
518 struct file_list
*tp
;
524 fprintf(fp
, "%s", msg
);
526 for (tp
= ftab
; tp
!= 0; tp
= tp
->f_next
) {
527 if (tp
->f_type
== INVISIBLE
)
531 * Check for '.o' file in list
533 cp
= tp
->f_fn
+ (len
= strlen(tp
->f_fn
)) - 1;
534 if (ext
!= -1 && *cp
!= ext
)
536 else if (*cp
== 'o') {
537 if (len
+ lpos
> 72) {
539 fprintf(fp
, "\\\n\t");
541 put_source_file_name(fp
, tp
);
547 cp
= (char *)sp
+ (len
= strlen(sp
)) - 1;
550 if (len
+ lpos
> 72) {
552 fprintf(fp
, "\\\n\t");
554 fprintf(fp
, "%s ", sp
);
562 do_files(FILE *fp
, const char *msg
, char ext
)
564 struct file_list
*tp
;
565 int lpos
, len
=0; /* dvw: init to 0 */
567 fprintf(fp
, "%s", msg
);
569 for (tp
= ftab
; tp
!= 0; tp
= tp
->f_next
) {
570 if (tp
->f_type
== INVISIBLE
)
572 if (tp
->f_fn
[strlen(tp
->f_fn
)-1] != ext
)
575 * Always generate a newline.
576 * Our Makefile's aren't readable anyway.
580 fprintf(fp
, "\\\n\t");
581 put_source_file_name(fp
, tp
);
588 * Include machine dependent makefile in output
592 do_machdep(FILE *ofp
)
598 (void) sprintf(line
, "%s/Makefile.%s", config_directory
, machinename
);
599 ifp
= fopenp(VPATH
, line
, pname
, "r");
604 while (fgets(line
, BUFSIZ
, ifp
) != 0) {
605 if (profiling
&& (strncmp(line
, "LIBS=", 5) == 0))
606 fprintf(ofp
,"LIBS=${LIBS_P}\n");
618 cp
= rindex(fn
, '/');
625 * Create the makerules for each file
626 * which is part of the system.
627 * Devices are processed with the special c2 option -i
628 * which avoids any problem areas with i/o addressing
629 * (e.g. for the VAX); assembler files are processed by as.
637 struct file_list
*ftp
;
638 const char *extras
= ""; /* dvw: init to "" */
643 for (ftp
= ftab
; ftp
!= 0; ftp
= ftp
->f_next
) {
644 if (ftp
->f_type
== INVISIBLE
)
646 cp
= (np
= ftp
->f_fn
) + strlen(ftp
->f_fn
) - 1;
649 * Don't compile '.o' files
654 * Determine where sources should come from
656 if ((np
[0] == '.') && (np
[1] == '/')) {
660 source_dir
= "$(SOURCE_DIR)/";
662 tp
= tail(np
); /* dvw: init tp before 'if' */
663 fprintf(f
, "-include %sd\n", tp
);
664 fprintf(f
, "%so: %s%s%c\n", tp
, source_dir
, np
, och
);
666 fprintf(f
, "\t${S_RULE_0}\n");
667 fprintf(f
, "\t${S_RULE_1A}%s%.*s${S_RULE_1B}%s\n",
668 source_dir
, (int)(tp
-np
), np
, nl
);
669 fprintf(f
, "\t${S_RULE_2}%s\n", nl
);
673 switch (ftp
->f_type
) {
689 "config: COPTS undefined in generic makefile");
696 och_upper
= och
+ 'A' - 'a';
697 fprintf(f
, "\t${%c_RULE_0%s}\n", och_upper
, extras
);
698 fprintf(f
, "\t${%c_RULE_1A%s}", och_upper
, extras
);
700 fprintf(f
, "%s", ftp
->f_extra
);
701 fprintf(f
, "%s%.*s${%c_RULE_1B%s}%s\n",
702 source_dir
, (int)(tp
-np
), np
, och_upper
, extras
, nl
);
704 /* While we are still using CTF, any build that normally does not support CTF will
705 * a "standard" compile done as well that we can harvest CTF information from; do
708 fprintf(f
, "\t${%c_CTFRULE_1A%s}", och_upper
, extras
);
710 fprintf(f
, "%s", ftp
->f_extra
);
711 fprintf(f
, "%s%.*s${%c_CTFRULE_1B%s}%s\n",
712 source_dir
, (int)(tp
-np
), np
, och_upper
, extras
, nl
);
714 fprintf(f
, "\t${%c_RULE_2%s}%s\n", och_upper
, extras
, nl
);
715 fprintf(f
, "\t${%c_CTFRULE_2%s}%s\n", och_upper
, extras
, nl
);
716 fprintf(f
, "\t${%c_RULE_3%s}%s\n", och_upper
, extras
, nl
);
717 fprintf(f
, "\t${%c_RULE_4A%s}", och_upper
, extras
);
719 fprintf(f
, "%s", ftp
->f_extra
);
720 fprintf(f
, "%s%.*s${%c_RULE_4B%s}%s\n",
721 source_dir
, (int)(tp
-np
), np
, och_upper
, extras
, nl
);
725 printf("Don't know rules for %s\n", np
);
739 *str
= toupper(*str
);
745 #define OLDSALUTATION "# DO NOT DELETE THIS LINE"
747 #define LINESIZE 1024
748 static char makbuf
[LINESIZE
]; /* one line buffer for makefile */
751 copy_dependencies(FILE *makin
, FILE *makout
)
753 int oldlen
= (sizeof OLDSALUTATION
- 1);
755 while (fgets(makbuf
, LINESIZE
, makin
) != NULL
) {
756 if (! strncmp(makbuf
, OLDSALUTATION
, oldlen
))
759 while (fgets(makbuf
, LINESIZE
, makin
) != NULL
) {
762 if (makbuf
[0] == '\n')
767 fputs(makbuf
, makout
);