f3590a1cde3ed449225e28665c0bcb578ed48894
[oweals/cde.git] / cde / programs / dtappbuilder / src / abmf / make_file.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23
24 /*
25  * $XConsortium: make_file.c /main/6 1996/06/28 08:31:16 mustafa $
26  * 
27  * @(#)make_file.c      3.53 19 Jan 1995        cde_app_builder/src/abmf
28  * 
29  * RESTRICTED CONFIDENTIAL INFORMATION:
30  * 
31  * The information in this document is subject to special restrictions in a
32  * confidential disclosure agreement between HP, IBM, Sun, USL, SCO and
33  * Univel.  Do not distribute this document outside HP, IBM, Sun, USL, SCO,
34  * or Univel without Sun's specific written approval.  This document and all
35  * copies and derivative works thereof must be returned or destroyed at Sun's
36  * request.
37  * 
38  * Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
39  * 
40  */
41
42
43 /*
44  * Create a make file that can be used to test the generated application.
45  */
46
47 #include <sys/param.h>
48 #include <sys/stat.h>
49 #include <stdio.h>
50 #include <ab/util_types.h>
51 #include <ab_private/abio.h>
52 #include "abmfP.h"
53 #include "obj_namesP.h"
54 #include "utilsP.h"
55 #include "write_cP.h"
56 #include "make_fileP.h"
57
58 #define MAX_LIBS        32
59
60 #define CODEGEN_MACRO   "DTCODEGEN"
61
62 typedef unsigned AbmfLibDesc;
63 #define LibNone         ((AbmfLibDesc)0)
64 #define LibDtHelp       ((AbmfLibDesc)1)
65 #define LibDtSvc        ((AbmfLibDesc)2)
66 #define LibDtTerm       ((AbmfLibDesc)3)
67 #define LibDtWidget     ((AbmfLibDesc)4)
68 #define LibX11          ((AbmfLibDesc)5)
69 #define LibXext         ((AbmfLibDesc)6)
70 #define LibXm           ((AbmfLibDesc)7)
71 #define LibXt           ((AbmfLibDesc)8)
72 #define LibWlarchive    ((AbmfLibDesc)9)
73 #define LibWldefault    ((AbmfLibDesc)10)
74 #define LibTt           ((AbmfLibDesc)11)
75 #define LibICE          ((AbmfLibDesc)12)
76 #define LibXp           ((AbmfLibDesc)13)
77 #define LibSM           ((AbmfLibDesc)14)
78
79 typedef struct
80 {
81     int         numLibs;
82     AbmfLibDesc libs[MAX_LIBS];
83 } AbmfLibsRec, *AbmfLibs;
84
85 #define ABMF_LIB_PREPEND        (-1)
86 #define ABMF_LIB_APPEND         (-2)
87 #define ABMF_LIB_REPLACE_DUP    (-3)
88 #define ABMF_LIB_REJECT_DUP     (-4)
89 #define ABMF_LIB_COMPLAIN_DUP   (-5)
90
91 static int      lib_find(AbmfLibs libs, AbmfLibDesc lib);
92 static int      lib_add(AbmfLibs libs, AbmfLibDesc lib, int where, int how);
93 static int      determine_libs(AbmfLibs libs, ABObj project, AB_OS_TYPE osType);
94
95 static STRING   cvt_to_obj(STRING fileName);
96 static int write_os_params(
97                         File            makeFile,
98                         AB_OS_TYPE      osType,
99                         AbmfLibs        libs
100                         );
101 static int write_aix_stuff(File makeFile, AbmfLibs libs);
102 static int write_hpux_stuff(File makeFile, AbmfLibs libs);
103 static int write_sunos_params(File makeFile, AbmfLibs libs);
104 static int write_unixware_params(File makeFile, AbmfLibs libs);
105 static int write_osf1_stuff(File makeFile, AbmfLibs libs);
106 static int write_lnx_params(File makeFile, AbmfLibs libs);
107 static int write_fbsd_params(File makeFile, AbmfLibs libs);
108 static int write_nbsd_params(File makeFile, AbmfLibs libs);
109 static int write_obsd_params(File makeFile, AbmfLibs libs);
110
111 static int      determine_aix_libs(AbmfLibs libs, ABObj project);
112 static int      determine_hpux_libs(AbmfLibs libs, ABObj project);
113 static int      determine_sunos_libs(AbmfLibs libs, ABObj project);
114 static int      determine_unixware_libs(AbmfLibs libs, ABObj project);
115 static int      determine_osf1_libs(AbmfLibs libs, ABObj project);
116 static int      determine_lnx_libs(AbmfLibs libs, ABObj project);
117 static int      determine_fbsd_libs(AbmfLibs libs, ABObj project);
118 static int      determine_nbsd_libs(AbmfLibs libs, ABObj project);
119 static int      determine_obsd_libs(AbmfLibs libs, ABObj project);
120
121 static int      write_file_header(
122                         GenCodeInfo     genCodeInfo, 
123                         ABObj           project, 
124                         AB_OS_TYPE      osType
125                 );
126 static int      write_ab_params(
127                         GenCodeInfo     genCodeInfo, 
128                         ABObj           project
129                 );
130 static int      write_targets(
131                         GenCodeInfo     genCodeInfo,
132                         ABObj           project,
133                         AB_OS_TYPE      osType
134                 );
135
136 static int      write_local_libraries(
137                         File            makeFile, 
138                         AbmfLibs        libs,
139                         AB_OS_TYPE      osType
140                 );
141
142 static int write_codegen_macro(
143                         File            makeFile
144                 );
145
146 /*
147  * Write makefile.
148  */
149 int
150 abmfP_write_make_file(
151                       GenCodeInfo       genCodeInfo,
152                       ABObj             project,
153                       AB_OS_TYPE        osType,
154                       BOOL              useSourceBrowser
155 )
156 {
157     File        makeFile = genCodeInfo->code_file;
158     AbmfLibsRec libs;
159     libs.numLibs = 0;
160
161     determine_libs(&libs, project, osType);
162
163     write_file_header(genCodeInfo, project, osType);
164     write_os_params(makeFile, osType, &libs);
165     write_codegen_macro(makeFile);
166     write_ab_params(genCodeInfo, project);
167
168     /*
169      * Sun needs a NO_PARALLEL directive
170      */
171     if (osType == AB_OS_SUNOS)
172     {
173         abio_printf(genCodeInfo->code_file,
174 "\n"
175 ".NO_PARALLEL: $(TARGETS.h) $(TARGETS.h.merged) $(TARGETS.c) $(TARGETS.c.merged)\n"
176         );
177     }
178
179     write_targets(genCodeInfo, project, osType);
180
181     return 0;
182 }
183
184
185 static int
186 determine_libs(AbmfLibs libs, ABObj project, AB_OS_TYPE osType)
187 {
188     int                 return_value = 0;
189     CGenProjData        projData = mfobj_get_proj_data(project);
190
191     if (projData->has_ui_obj)
192     {
193         lib_add(libs, LibDtWidget, ABMF_LIB_APPEND, ABMF_LIB_REJECT_DUP);
194         lib_add(libs, LibDtHelp, ABMF_LIB_APPEND, ABMF_LIB_REJECT_DUP);
195         lib_add(libs, LibDtSvc, ABMF_LIB_APPEND, ABMF_LIB_REJECT_DUP);
196         lib_add(libs, LibXm, ABMF_LIB_APPEND, ABMF_LIB_REJECT_DUP);
197         lib_add(libs, LibXt, ABMF_LIB_APPEND, ABMF_LIB_REJECT_DUP);
198         lib_add(libs, LibXext, ABMF_LIB_APPEND, ABMF_LIB_REJECT_DUP);
199         lib_add(libs, LibX11, ABMF_LIB_APPEND, ABMF_LIB_REJECT_DUP);
200         lib_add(libs, LibICE, ABMF_LIB_APPEND, ABMF_LIB_REJECT_DUP);
201         lib_add(libs, LibSM, ABMF_LIB_APPEND, ABMF_LIB_REJECT_DUP);
202         lib_add(libs, LibXp, ABMF_LIB_APPEND, ABMF_LIB_REJECT_DUP);
203
204         if (osType == AB_OS_HPUX)
205         {
206             lib_add(libs, LibTt, ABMF_LIB_APPEND, ABMF_LIB_REJECT_DUP);
207         }
208     }
209
210     if (projData->has_terminal)
211     {
212         lib_add(libs, LibDtHelp, ABMF_LIB_PREPEND, ABMF_LIB_REJECT_DUP);
213         lib_add(libs, LibDtTerm, ABMF_LIB_PREPEND, ABMF_LIB_REJECT_DUP);
214     }
215
216 #if defined(USL)
217     if (1) /* Workaround because tooltalk not being used when needed */
218 #else
219     if (obj_get_tooltalk_level(project) != AB_TOOLTALK_NONE)
220 #endif
221         lib_add(libs, LibTt, ABMF_LIB_APPEND, ABMF_LIB_REJECT_DUP);
222
223     switch(osType)
224     {
225         case AB_OS_AIX:
226                 return_value = determine_aix_libs(libs, project);
227                 break;
228
229         case AB_OS_HPUX:
230                 return_value = determine_hpux_libs(libs, project);
231                 break;
232
233         case AB_OS_SUNOS:
234                 return_value = determine_sunos_libs(libs, project);
235                 break;
236         case AB_OS_OSF1:
237                 return_value = determine_osf1_libs(libs, project);
238                 break;
239         case AB_OS_UNIXWARE:
240                 return_value = determine_unixware_libs(libs, project);
241                 break;
242         case AB_OS_LNX:
243                 return_value = determine_lnx_libs(libs, project);
244                 break;
245         case AB_OS_FBSD:
246                 return_value = determine_fbsd_libs(libs, project);
247                 break;
248         case AB_OS_NBSD:
249                 return_value = determine_nbsd_libs(libs, project);
250                 break;
251         case AB_OS_OBSD:
252                 return_value = determine_obsd_libs(libs, project);
253                 break;
254     }
255
256     return return_value;
257 }
258
259 static int
260 determine_aix_libs(AbmfLibs libs, ABObj project)
261 {
262     return 0;
263 }
264
265
266 static int
267 determine_hpux_libs(AbmfLibs libs, ABObj project)
268 {
269     /*
270     lib_add(libs, LibWlarchive, ABMF_LIB_PREPEND, ABMF_LIB_REPLACE_DUP);
271     lib_add(libs, LibWldefault, ABMF_LIB_APPEND, ABMF_LIB_REPLACE_DUP);
272     */
273     return 0;
274 }
275
276
277 static int
278 determine_sunos_libs(AbmfLibs libs, ABObj project)
279 {
280     return 0;
281 }
282
283 static int
284 determine_osf1_libs(AbmfLibs libs, ABObj project)
285 {
286     return 0;
287 }
288
289 static int
290 determine_unixware_libs(AbmfLibs libs, ABObj project)
291 {
292     return 0;
293 }
294
295 static int
296 determine_lnx_libs(AbmfLibs libs, ABObj project)
297 {
298     return 0;
299 }
300
301 static int
302 determine_fbsd_libs(AbmfLibs libs, ABObj project)
303 {
304     return 0;
305 }
306
307 static int
308 determine_nbsd_libs(AbmfLibs libs, ABObj project)
309 {
310     return 0;
311 }
312
313 static int
314 determine_obsd_libs(AbmfLibs libs, ABObj project)
315 {
316     return 0;
317 }
318
319 static STRING
320 get_string_for_lib(AbmfLibDesc lib, AB_OS_TYPE osType)
321 {
322     STRING      libString = NULL;
323
324     switch (lib)
325     {
326         case LibNone:
327             libString = abmfP_str_empty;
328         break;
329
330         case LibDtHelp:
331             libString = "-lDtHelp";
332         break;
333
334         case LibDtSvc:
335             libString = "-lDtSvc";
336         break;
337
338         case LibDtTerm:
339             libString = "-lDtTerm";
340         break;
341
342         case LibDtWidget:
343             libString = "-lDtWidget";
344         break;
345
346         case LibTt:
347             libString = "-ltt";
348         break;
349
350         case LibX11:
351             libString = "-lX11";
352         break;
353
354         case LibXext:
355             libString = "-lXext";
356         break;
357
358         case LibXm:
359             libString = "-lXm";
360         break;
361
362         case LibXt:
363             libString = "-lXt";
364         break;
365
366         case LibXp:
367             libString = "-lXp";
368         break;
369
370         case LibICE:
371             libString = "-lICE";
372         break;
373
374         case LibSM:
375             libString = "-lSM";
376         break;
377
378         case LibWlarchive:
379             libString = "-Wl,-a,archive";
380         break;
381
382         case LibWldefault:
383             libString = "-Wl,-a,default";
384         break;
385     }
386
387     return libString;
388 }
389
390
391 static int
392 lib_find(AbmfLibs libs, AbmfLibDesc lib)
393 {
394     int i;
395     int         numLibs = libs->numLibs;
396
397     for (i = 0; i < numLibs; ++i)
398     {
399         if (libs->libs[i] == lib)
400         {
401             return i;
402         }
403     }
404     return ERR_NOT_FOUND;
405 }
406
407
408 static int
409 lib_add(AbmfLibs libs, AbmfLibDesc lib, int where, int how)
410 {
411     int         pos = 0;
412     int         old_pos = -1;
413     
414     if ((old_pos = lib_find(libs, lib)) >= 0)
415     {
416         if (how == ABMF_LIB_REJECT_DUP)
417         {
418             return ERR_DUPLICATE_KEY;
419         }
420         if (how == ABMF_LIB_REPLACE_DUP)
421         {
422             return ERR_NOT_IMPLEMENTED;
423         }
424     }
425     if (libs->numLibs >= MAX_LIBS)
426     {
427         return ERR_DATA_SPACE_FULL;
428     }
429
430     /*
431      * Figure out where to put it
432      */
433     pos = where;
434     if (pos == ABMF_LIB_PREPEND)
435     {
436         pos = 0;
437     }
438     if (pos == ABMF_LIB_APPEND)
439     {
440         pos = libs->numLibs;
441     }
442
443     /*
444      * Insert it
445      */
446     if (pos < libs->numLibs)
447     {
448         /* insertion */
449         memmove(&(libs->libs[pos +1]), &(libs->libs[pos]),
450                     ((libs->numLibs+1) - pos) * sizeof(libs->libs[0]));
451         libs->libs[pos] = lib;
452         ++(libs->numLibs);
453     }
454     else
455     {
456         libs->libs[(libs->numLibs)++] = lib;
457     }
458
459     return 0;
460 }
461
462
463 static int
464 write_file_header(
465                         GenCodeInfo     genCodeInfo,
466                         ABObj           project,
467                         AB_OS_TYPE      osType
468 )
469 {
470     STRING      osName = util_os_type_to_string(osType);
471     File        makeFile = genCodeInfo->code_file;
472
473     abio_printf(makeFile, 
474     "\n"
475 "###########################################################################\n"
476 "#\n"
477 "#    CDE Application Builder Makefile\n"
478 "#\n"
479 "# This file was generated by `%s' from project file `%s'.\n"
480 "# The target platform for this Makefile is %s.\n"
481 "#\n"
482 "###########################################################################\n",
483         util_get_program_name(),
484         obj_get_file(project),
485         osName);
486
487     return 0;
488 }
489
490
491 static int
492 write_os_params(
493                         File            makeFile,
494                         AB_OS_TYPE      osType,
495                         AbmfLibs        libs
496 )
497 {
498     int         return_value = 0;
499
500     switch(osType)
501     {
502         case AB_OS_AIX:
503                 return_value = write_aix_stuff(makeFile, libs);
504                 break;
505
506         case AB_OS_HPUX:
507                 return_value = write_hpux_stuff(makeFile, libs);
508                 break;
509
510         case AB_OS_SUNOS:
511                 return_value = write_sunos_params(makeFile, libs);
512                 break;
513
514         case AB_OS_UNIXWARE:
515                 return_value = write_unixware_params(makeFile, libs);
516                 break;
517
518         case AB_OS_OSF1:
519                 return_value = write_osf1_stuff(makeFile, libs);
520                 break;
521
522         case AB_OS_LNX:
523                 return_value = write_lnx_params(makeFile, libs);
524                 break;
525
526         case AB_OS_FBSD:
527                 return_value = write_fbsd_params(makeFile, libs);
528                 break;
529
530         case AB_OS_NBSD:
531                 return_value = write_nbsd_params(makeFile, libs);
532                 break;
533
534         case AB_OS_OBSD:
535                 return_value = write_obsd_params(makeFile, libs);
536                 break;
537     }
538
539     return return_value;
540 }
541
542
543 static int
544 write_aix_stuff(File makeFile, AbmfLibs libs)
545 {
546     STRING      osName = util_os_type_to_string(AB_OS_AIX);
547
548     abio_printf(makeFile, 
549 "###########################################################################\n"
550 "# These are the %s-dependent configuration parameters that must be\n"
551 "# set in order for any application to build.\n"
552 "###########################################################################\n",
553         osName);
554
555      abio_puts(makeFile, "\n");
556      abio_puts(makeFile,
557 "        RM = rm -f\n"
558 "        INCLUDES = -I/usr/dt/include -I/X11/include\n"
559 "\n"
560 "        STD_DEFINES = -DSYSV -DAIXV3 -D_ALL_SOURCE"
561         " -D_AIX -D__AIX -D__aix -D_aix"
562         " -Daix -DMULTIBYTE -DNLS16 -DMESSAGE_CAT -DAIX_ILS\n"
563 "        ANSI_DEFINES =\n"
564 "\n"
565 "        CDEBUGFLAGS = -g -D__STR31__\n"
566 "        COPTFLAGS = -O -D__STR31__\n"
567 "        SYS_LIBRARIES = -lm\n"
568 "        CDE_LIBPATH = /usr/dt/lib\n"
569 "        CDE_LDFLAGS = -L$(CDE_LIBPATH)\n"
570 "        ALLX_LIBPATH = /X11/lib\n"
571 "        ALLX_LDFLAGS = -L$(ALLX_LIBPATH)\n"
572 );
573
574     write_local_libraries(makeFile, libs, AB_OS_AIX);
575
576     abio_puts(makeFile,
577 "\n"
578 "        CFLAGS = $(CDEBUGFLAGS) $(INCLUDES) $(STD_DEFINES)"
579             " $(ANSI_DEFINES)\n"
580 "        LDLIBS = $(SYS_LIBRARIES)\n"
581 "        LDOPTIONS = $(CDE_LDFLAGS) $(ALLX_LDFLAGS)\n"
582 "\n"
583 );
584
585     return 0;
586 }
587
588
589 static int
590 write_hpux_stuff(File makeFile, AbmfLibs libs)
591 {
592     STRING      osName = util_os_type_to_string(AB_OS_HPUX);
593
594     abio_printf(makeFile, 
595 "###########################################################################\n"
596 "# These are the %s-dependent configuration parameters that must be\n"
597 "# set in order for any application to build.\n"
598 "###########################################################################\n",
599         osName);
600
601      abio_puts(makeFile, "\n");
602      abio_puts(makeFile,
603 "        RM = rm -f\n"
604 "        INCLUDES = -I/usr/dt/include -I/X11/include\n"
605 "\n"
606 "        STD_DEFINES = -DSYSV -DNLS16 -DMALLOC_0_RETURNS_NULL -DMERGE -DNDEBUG"
607         " -D__hpux -Dhpux -DOSMAJORVERSION=9 -DOSMINORVERSION=0 -DSHMLINK"
608         " -D__hp9000s800 -Dhp9000s800 -Dhp9000s700 -DHPPEX\n"
609 "        ANSI_DEFINES = -Aa -D_HPUX_SOURCE -DANSICPP\n"
610 "\n"
611 "        CDEBUGFLAGS = -g -z\n"
612 "        COPTFLAGS = -O -z\n"
613 "        SYS_LIBRARIES = -lm\n"
614 "        CDE_LIBPATH = /usr/dt/lib\n"
615 "        CDE_LDFLAGS = -L$(CDE_LIBPATH)\n"
616 "        ALLX_LIBPATH = /X11/lib\n"
617 "        ALLX_LDFLAGS = -L$(ALLX_LIBPATH)\n"
618 );
619
620     write_local_libraries(makeFile, libs, AB_OS_HPUX);
621
622     abio_puts(makeFile,
623 "\n"
624 "        CFLAGS = $(CDEBUGFLAGS) $(INCLUDES) $(STD_DEFINES)"
625             " $(ANSI_DEFINES)\n"
626 "        LDLIBS = $(SYS_LIBRARIES)\n"
627 "        LDOPTIONS = $(CDE_LDFLAGS) $(ALLX_LDFLAGS)\n"
628 "\n"
629 );
630
631     return 0;
632 }
633
634
635 static int
636 write_sunos_params(File makeFile, AbmfLibs libs)
637 {
638     STRING      osName = util_os_type_to_string(AB_OS_SUNOS);
639
640     abio_printf(makeFile, 
641     "\n"
642 "###########################################################################\n"
643 "# These are the %s-dependent configuration parameters that must be\n"
644 "# set in order for any application to build.\n"
645 "###########################################################################\n",
646         osName);
647
648      abio_puts(makeFile,
649 "\n"
650 ".KEEP_STATE:\n"
651 "\n"
652 "        RM = rm -f\n"
653 "        INCLUDES = -I/usr/dt/include -I/X11/include\n"
654 "\n"
655 "        STD_DEFINES = \n"
656 "        ANSI_DEFINES = \n"
657 "\n"
658 "        CDEBUGFLAGS = -g\n"
659 "        COPTFLAGS = -O2\n"
660 "        SYS_LIBRARIES = -lgen -lm\n"
661 "        CDE_LIBPATH = /usr/dt/lib\n"
662 "        CDE_LDFLAGS = -L$(CDE_LIBPATH) -R$(CDE_LIBPATH)\n"
663 "        ALLX_LIBPATH = /X11/lib\n"
664 "        ALLX_LDFLAGS = -L$(ALLX_LIBPATH) -R$(ALLX_LIBPATH)\n"
665 );
666     write_local_libraries(makeFile, libs, AB_OS_SUNOS);
667
668     abio_puts(makeFile, 
669 "\n"
670 "        CFLAGS = $(CDEBUGFLAGS) $(INCLUDES) $(STD_DEFINES)"
671             " $(ANSI_DEFINES)\n"
672 "        LDLIBS = $(SYS_LIBRARIES)\n"
673 "        LDOPTIONS = $(CDE_LDFLAGS) $(ALLX_LDFLAGS)\n"
674 "\n"
675 );
676
677     return 0;
678 }
679
680 static int
681 write_osf1_stuff(File makeFile, AbmfLibs libs)
682 {
683     STRING      osName = util_os_type_to_string(AB_OS_OSF1);
684
685     abio_printf(makeFile,
686 "###########################################################################\n"
687 "# These are the %s-dependent configuration parameters that must be\n"
688 "# set in order for any application to build.\n"
689 "###########################################################################\n",
690         osName);
691
692      abio_puts(makeFile, "\n");
693      abio_puts(makeFile,
694 "        RM = rm -f\n"
695 "        INCLUDES = -I/usr/dt/include -I/X11/include\n"
696 "\n"
697 "        STD_DEFINES = -DDEC\n"
698 "        ANSI_DEFINES =\n"
699 "\n"
700 "        CDEBUGFLAGS = -g\n"
701 "        COPTFLAGS = -O2\n"
702 "        SYS_LIBRARIES = -ldnet_stub -lm\n"
703 "        CDE_LIBPATH = /usr/dt/lib\n"
704 "        CDE_LDFLAGS = -L$(CDE_LIBPATH)\n"
705 "        ALLX_LIBPATH = /X11/lib\n"
706 "        ALLX_LDFLAGS = -L$(ALLX_LIBPATH)\n"
707 "        EXTRA_LOAD_FLAGS = -Wl,-rpath,$(CDE_LIBPATH):$(ALLX_LIBPATH)\n"
708 );
709
710     write_local_libraries(makeFile, libs, AB_OS_OSF1);
711
712     abio_puts(makeFile,
713 "\n"
714 "        CFLAGS = $(CDEBUGFLAGS) $(INCLUDES) $(STD_DEFINES)"
715             " $(ANSI_DEFINES)\n"
716 "        LDLIBS = $(SYS_LIBRARIES)\n"
717 "        LDOPTIONS = $(CDE_LDFLAGS) $(ALLX_LDFLAGS) $(EXTRA_LOAD_FLAGS)\n"
718 "\n"
719 );
720
721     return 0;
722 }
723
724 static int
725 write_unixware_params(File makeFile, AbmfLibs libs)
726 {
727     STRING      osName = util_os_type_to_string(AB_OS_UNIXWARE);
728
729     abio_printf(makeFile, 
730     "\n"
731 "###########################################################################\n"
732 "# These are the %s-dependent configuration parameters that must be\n"
733 "# set in order for any application to build.\n"
734 "###########################################################################\n",
735         osName);
736
737      abio_puts(makeFile, "\n");
738      abio_puts(makeFile,
739 "        RM = rm -f\n"
740 "        INCLUDES = -I/usr/dt/include -I/X11/include\n"
741 "\n"
742 "        STD_DEFINES = \n"
743 "        ANSI_DEFINES = \n"
744 "\n"
745 "        CDEBUGFLAGS = -O\n"
746 "        SYS_LIBRARIES = -lnsl -lsocket -lm -ldl -lgen -lresolv -lw\n"
747 "        CDE_LIBPATH = /usr/dt/lib\n"
748 "        CDE_LDFLAGS = -L$(CDE_LIBPATH)\n"
749 "        ALLX_LIBPATH = /X11/lib\n"
750 "        ALLX_LDFLAGS = -L$(ALLX_LIBPATH)\n");
751
752     write_local_libraries(makeFile, libs, AB_OS_UNIXWARE);
753
754      abio_puts(makeFile,
755 "        CFLAGS = $(CDEBUGFLAGS) -D_POSIX_SOURCE=1 $(INCLUDES) $(STD_DEFINES)"
756             " $(ANSI_DEFINES)\n"
757 "        LDLIBS = $(SYS_LIBRARIES)\n"
758 "        LDOPTIONS = $(CDE_LDFLAGS) $(ALLX_LDFLAGS)\n"
759 "\n"
760 );
761
762     return 0;
763 }
764
765 static int
766 write_lnx_params(File makeFile, AbmfLibs libs)
767 {
768     STRING      osName = util_os_type_to_string(AB_OS_LNX);
769
770     abio_printf(makeFile,
771     "\n"
772 "###########################################################################\n"
773 "# These are the %s-dependent configuration parameters that must be\n"
774 "# set in order for any application to build.\n"
775 "###########################################################################\n",
776         osName);
777
778      abio_puts(makeFile,
779 "\n"
780 ".KEEP_STATE:\n"
781 "\n"
782 "        RM = rm -f\n"
783 "        INCLUDES = -I/usr/dt/include -I/X11/include\n"
784 "\n"
785 "        STD_DEFINES = \n"
786 "        ANSI_DEFINES = \n"
787 "\n"
788 "        CDEBUGFLAGS = -g\n"
789 "        COPTFLAGS = -O2\n"
790 "        SYS_LIBRARIES = -lgen -lm\n"
791 "        CDE_LIBPATH = /usr/dt/lib\n"
792 "        CDE_LDFLAGS = -L$(CDE_LIBPATH)\n"
793 "        ALLX_LIBPATH = /X11/lib\n"
794 "        ALLX_LDFLAGS = -L$(ALLX_LIBPATH)\n"
795 );
796     write_local_libraries(makeFile, libs, AB_OS_LNX);
797
798     abio_puts(makeFile,
799 "\n"
800 "        CFLAGS = $(CDEBUGFLAGS) $(INCLUDES) $(STD_DEFINES)"
801             " $(ANSI_DEFINES)\n"
802 "        LDLIBS = $(SYS_LIBRARIES)\n"
803 "        LDOPTIONS = $(CDE_LDFLAGS) $(ALLX_LDFLAGS)\n"
804 "\n"
805 );
806
807     return 0;
808 }
809
810 static int
811 write_fbsd_params(File makeFile, AbmfLibs libs)
812 {
813     STRING      osName = util_os_type_to_string(AB_OS_FBSD);
814
815     abio_printf(makeFile,
816     "\n"
817 "###########################################################################\n"
818 "# These are the %s-dependent configuration parameters that must be\n"
819 "# set in order for any application to build.\n"
820 "###########################################################################\n",
821         osName);
822
823      abio_puts(makeFile,
824 "\n"
825 ".KEEP_STATE:\n"
826 "\n"
827 "        RM = rm -f\n"
828 "        INCLUDES = -I/usr/dt/include -I/X11/include\n"
829 "\n"
830 "        STD_DEFINES = \n"
831 "        ANSI_DEFINES = \n"
832 "\n"
833 "        CDEBUGFLAGS = -g\n"
834 "        COPTFLAGS = -O2\n"
835 "        SYS_LIBRARIES = -lgen -lm\n"
836 "        CDE_LIBPATH = /usr/dt/lib\n"
837 "        CDE_LDFLAGS = -L$(CDE_LIBPATH)\n"
838 "        ALLX_LIBPATH = /X11/lib\n"
839 "        ALLX_LDFLAGS = -L$(ALLX_LIBPATH)\n"
840 );
841     write_local_libraries(makeFile, libs, AB_OS_FBSD);
842
843     abio_puts(makeFile,
844 "\n"
845 "        CFLAGS = $(CDEBUGFLAGS) $(INCLUDES) $(STD_DEFINES)"
846             " $(ANSI_DEFINES)\n"
847 "        LDLIBS = $(SYS_LIBRARIES)\n"
848 "        LDOPTIONS = $(CDE_LDFLAGS) $(ALLX_LDFLAGS)\n"
849 "\n"
850 );
851
852     return 0;
853 }
854
855 static int
856 write_nbsd_params(File makeFile, AbmfLibs libs)
857 {
858     STRING      osName = util_os_type_to_string(AB_OS_NBSD);
859
860     abio_printf(makeFile,
861     "\n"
862 "###########################################################################\n"
863 "# These are the %s-dependent configuration parameters that must be\n"
864 "# set in order for any application to build.\n"
865 "###########################################################################\n",
866         osName);
867
868      abio_puts(makeFile,
869 "\n"
870 ".KEEP_STATE:\n"
871 "\n"
872 "        RM = rm -f\n"
873 "        INCLUDES = -I/usr/dt/include -I/X11/include\n"
874 "\n"
875 "        STD_DEFINES = \n"
876 "        ANSI_DEFINES = \n"
877 "\n"
878 "        CDEBUGFLAGS = -g\n"
879 "        COPTFLAGS = -O2\n"
880 "        SYS_LIBRARIES = -lgen -lm\n"
881 "        CDE_LIBPATH = /usr/dt/lib\n"
882 "        CDE_LDFLAGS = -L$(CDE_LIBPATH)\n"
883 "        ALLX_LIBPATH = /X11/lib\n"
884 "        ALLX_LDFLAGS = -L$(ALLX_LIBPATH)\n"
885 );
886     write_local_libraries(makeFile, libs, AB_OS_NBSD);
887
888     abio_puts(makeFile,
889 "\n"
890 "        CFLAGS = $(CDEBUGFLAGS) $(INCLUDES) $(STD_DEFINES)"
891             " $(ANSI_DEFINES)\n"
892 "        LDLIBS = $(SYS_LIBRARIES)\n"
893 "        LDOPTIONS = $(CDE_LDFLAGS) $(ALLX_LDFLAGS)\n"
894 "\n"
895 );
896
897     return 0;
898 }
899
900 static int
901 write_obsd_params(File makeFile, AbmfLibs libs)
902 {
903     STRING      osName = util_os_type_to_string(AB_OS_OBSD);
904
905     abio_printf(makeFile,
906     "\n"
907 "###########################################################################\n"
908 "# These are the %s-dependent configuration parameters that must be\n"
909 "# set in order for any application to build.\n"
910 "###########################################################################\n",
911         osName);
912
913      abio_puts(makeFile,
914 "\n"
915 ".KEEP_STATE:\n"
916 "\n"
917 "        RM = rm -f\n"
918 "        INCLUDES = -I/usr/dt/include -I/X11/include\n"
919 "\n"
920 "        STD_DEFINES = \n"
921 "        ANSI_DEFINES = \n"
922 "\n"
923 "        CDEBUGFLAGS = -g\n"
924 "        COPTFLAGS = -O2\n"
925 "        SYS_LIBRARIES = -lgen -lm\n"
926 "        CDE_LIBPATH = /usr/dt/lib\n"
927 "        CDE_LDFLAGS = -L$(CDE_LIBPATH)\n"
928 "        ALLX_LIBPATH = /X11/lib\n"
929 "        ALLX_LDFLAGS = -L$(ALLX_LIBPATH)\n"
930 );
931     write_local_libraries(makeFile, libs, AB_OS_OBSD);
932
933     abio_puts(makeFile,
934 "\n"
935 "        CFLAGS = $(CDEBUGFLAGS) $(INCLUDES) $(STD_DEFINES)"
936             " $(ANSI_DEFINES)\n"
937 "        LDLIBS = $(SYS_LIBRARIES)\n"
938 "        LDOPTIONS = $(CDE_LDFLAGS) $(ALLX_LDFLAGS)\n"
939 "\n"
940 );
941
942     return 0;
943 }
944
945 static int      
946 write_local_libraries(
947                         File            makeFile, 
948                         AbmfLibs        libs,
949                         AB_OS_TYPE      osType
950 )
951 {
952     int         numLibs = libs->numLibs;
953     int         i = 0;
954     AbmfLibDesc lib = LibNone;
955     STRING      libString = NULL;
956
957     abio_puts(makeFile, "        LOCAL_LIBRARIES =");
958     for (i = 0; i < numLibs; ++i)
959     {
960         lib = libs->libs[i];
961         libString = get_string_for_lib(lib, osType);
962         if (libString != NULL)
963         {
964             abio_printf(makeFile, " %s", libString);
965         }
966     }
967     abio_puts(makeFile, nlstr);
968
969     return 0;
970 }
971
972
973 static int
974 write_ab_params(
975                 GenCodeInfo     genCodeInfo,
976                 ABObj           project
977 )
978 {
979     File        makeFile = genCodeInfo->code_file;
980     int         numModules = trav_count_cond(project, AB_TRAV_MODULES, obj_is_defined_module);
981     int         thisModuleNum = 0;
982     AB_TRAVERSAL        trav;
983     ABObj               module = NULL;
984
985     abio_printf(makeFile, 
986     "\n"
987 "###########################################################################\n"
988 "#  These parameters are generated by %s according to the structure\n"
989 "#  of the project as defined in the .bip and .bil files\n"
990 "###########################################################################\n",
991         util_get_program_name());
992
993     abio_puts(makeFile, "\n");
994     abio_printf(makeFile, "PROGRAM = %s\n", abmfP_get_exe_file_name(project));
995
996     abio_puts(makeFile, "\n");
997     abio_puts(makeFile, "TARGETS.h = \\\n");
998     abio_printf(makeFile, "\t%s \\\n", 
999         abmfP_get_utils_header_file_name(project));
1000     for (thisModuleNum = 0, trav_open(&trav, project, AB_TRAV_MODULES);
1001         (module = trav_next(&trav)) != NULL;
1002         ++thisModuleNum)
1003     {
1004         if (!obj_is_defined(module))
1005         {
1006             continue;
1007         }
1008
1009         abio_printf(makeFile, "\t%s", abmfP_get_ui_header_file_name(module));
1010         if (thisModuleNum < (numModules-1))
1011         {
1012             abio_puts(makeFile, " \\");
1013         }
1014         abio_puts(makeFile, "\n");
1015
1016     }
1017     trav_close(&trav);
1018
1019     /*
1020      * TARGETS.c
1021      */
1022     abio_puts(makeFile, "\n");
1023     abio_puts(makeFile, "TARGETS.c = \\\n");
1024     abio_printf(makeFile, "\t%s \\\n", abmfP_get_utils_c_file_name(project));
1025     for (thisModuleNum = 0, trav_open(&trav, project, AB_TRAV_MODULES);
1026         (module = trav_next(&trav)) != NULL;
1027         ++thisModuleNum)
1028     {
1029         if (!obj_is_defined(module))
1030         { 
1031             continue; 
1032         } 
1033
1034         abio_printf(makeFile, "\t%s", abmfP_get_ui_c_file_name(module));
1035         if (thisModuleNum < (numModules-1))
1036         {
1037             abio_puts(makeFile, " \\");
1038         }
1039         abio_puts(makeFile, "\n");
1040
1041     }
1042     trav_close(&trav);
1043
1044     /*
1045      * TARGETS.h.merged
1046      *
1047      * These are hand-edited, as well, so we cannot consider them
1048      * target .h files, which can be removed and regenerated on a whim.
1049      */
1050     abio_puts(makeFile, "\n");
1051     abio_puts(makeFile, "TARGETS.h.merged = \\\n");
1052     abio_printf(makeFile, "\t%s\n", 
1053         abmfP_get_project_header_file_name(project));
1054
1055
1056     /*
1057      * TARGETS.c.merged
1058      *
1059      * These are hand-edited, as well, so we cannot consider them
1060      * target .c files, which can be removed and regenerated on a whim.
1061      */
1062     abio_puts(makeFile, "\n");
1063     abio_puts(makeFile, "TARGETS.c.merged = \\\n");
1064     abio_printf(makeFile, "\t%s \\\n", abmfP_get_project_c_file_name(project));
1065     for (thisModuleNum = 0, trav_open(&trav, project, AB_TRAV_MODULES);
1066         (module = trav_next(&trav)) != NULL;
1067         ++thisModuleNum)
1068     {
1069         if (!obj_is_defined(module))
1070         { 
1071             continue; 
1072         } 
1073
1074         abio_printf(makeFile, "\t%s", abmfP_get_stubs_c_file_name(module));
1075         if (thisModuleNum < (numModules-1))
1076         {
1077             abio_puts(makeFile, " \\");
1078         }
1079         abio_puts(makeFile, "\n");
1080
1081     }
1082     trav_close(&trav);
1083
1084
1085     abio_puts(makeFile, "\n");
1086     abio_puts(makeFile, "SOURCES.h = $(TARGETS.h.merged) $(TARGETS.h)\n");
1087
1088     abio_puts(makeFile, "\n");
1089     abio_puts(makeFile, "SOURCES.c = $(TARGETS.c.merged) $(TARGETS.c)\n");
1090
1091     abio_puts(makeFile, "\n");
1092     abio_puts(makeFile, "OBJS = \\\n");
1093     abio_printf(makeFile, "\t%s \\\n",
1094         cvt_to_obj(abmfP_get_project_c_file_name(project)));
1095     abio_printf(makeFile, "\t%s \\\n",
1096         cvt_to_obj(abmfP_get_utils_c_file_name(project)));
1097     for (thisModuleNum = 0, trav_open(&trav, project, AB_TRAV_MODULES);
1098         (module = trav_next(&trav)) != NULL;
1099         ++thisModuleNum)
1100     {
1101         if (!obj_is_defined(module))
1102         { 
1103             continue; 
1104         } 
1105
1106         abio_printf(makeFile, "\t%s \\\n",
1107                 cvt_to_obj(abmfP_get_ui_c_file_name(module)));
1108         abio_printf(makeFile, "\t%s", 
1109                 cvt_to_obj(abmfP_get_stubs_c_file_name(module)));
1110
1111         if (thisModuleNum < (numModules-1))
1112         {
1113             abio_puts(makeFile, " \\");
1114         }
1115         abio_puts(makeFile, "\n");
1116     }
1117     trav_close(&trav);
1118
1119     return 0;
1120 }
1121
1122
1123 static int
1124 write_codegen_macro(File makeFile)
1125 {
1126     abio_printf(makeFile, "        %s = %s\n", 
1127                 CODEGEN_MACRO, util_get_program_name());
1128
1129     return 0;
1130 }
1131
1132
1133 static BOOL
1134 obj_is_module_func(ABObj obj)
1135 {
1136     return obj_is_module(obj);
1137 }
1138
1139 static int
1140 write_targets(
1141         GenCodeInfo     genCodeInfo,
1142         ABObj           project,
1143         AB_OS_TYPE      osType
1144 )
1145 {
1146     File        makeFile = genCodeInfo->code_file;
1147     STRING      prog = util_get_program_name();
1148     STRING      projFile = obj_get_file(project);
1149     STRING      modFile = NULL;
1150     char        modName[MAXPATHLEN+1];
1151     ABObj       firstModule = NULL;
1152     STRING      firstModuleFileName = NULL;
1153     char        projFlag[1024];
1154     AB_TRAVERSAL        moduleTrav;
1155     ABObj               module = NULL;
1156     *projFlag = 0;
1157     *modName = 0;
1158
1159     firstModule = obj_get_child_cond(project, 0, obj_is_module_func);
1160     if (firstModule != NULL)
1161     {
1162         firstModuleFileName = obj_get_file(firstModule);
1163     }
1164     if (obj_is_default(project))
1165     {
1166         strcpy(projFlag, "-nomerge -noproject");
1167     }
1168     else
1169     {
1170         sprintf(projFlag, "-changed -merge -p %s", projFile);
1171     }
1172
1173     abio_puts(makeFile, 
1174     "\n"
1175     "######################################################################\n"
1176     "# Standard targets.                                                  #\n"
1177     "######################################################################\n"
1178     );
1179
1180     abio_puts(makeFile,
1181 "all:: $(PROGRAM)\n"
1182 "\n"
1183 "$(PROGRAM) : $(SOURCES.c) $(SOURCES.h) $(OBJS)\n"
1184 "       $(RM) $@\n"
1185 "       $(CC) -o $@ $(OBJS) $(LDOPTIONS) $(LOCAL_LIBRARIES) $(LDLIBS)\n"
1186 "\n"
1187 );
1188
1189     if (! (obj_is_default(project) && (firstModuleFileName == NULL)) )
1190     {
1191         if (osType == AB_OS_SUNOS)
1192             abio_printf(makeFile, "%s + %s + %s + %s: %s\n",
1193                 abmfP_get_project_c_file_name(project), 
1194                 abmfP_get_project_header_file_name(project), 
1195                 abmfP_get_utils_c_file_name(project),
1196                 abmfP_get_utils_header_file_name(project),
1197                 obj_get_file(project));
1198         else
1199             abio_printf(makeFile, "%s %s %s %s: %s\n",
1200                 abmfP_get_project_c_file_name(project), 
1201                 abmfP_get_project_header_file_name(project), 
1202                 abmfP_get_utils_c_file_name(project),
1203                 abmfP_get_utils_header_file_name(project),
1204                 obj_get_file(project));
1205  
1206         abio_printf(makeFile, 
1207             "\t$(%s) %s -main", CODEGEN_MACRO, projFlag);
1208         if (obj_is_default(project))
1209         {
1210             abio_printf(makeFile, " %s", firstModuleFileName);
1211         }
1212         abio_printf(makeFile, "\n");
1213
1214         abio_printf(makeFile, "%s: %s %s $(TARGETS.h) $(TARGETS.h.merged)\n",
1215             cvt_to_obj(abmfP_get_project_c_file_name(project)),
1216             abmfP_get_project_c_file_name(project),
1217             obj_get_file(project));
1218     }
1219
1220     /*
1221      * Make the project.c file dependent upon the module header files.
1222      * That way, if a module changes, main() and the x-module connections
1223      * will be recompiled.
1224      */
1225     abio_printf(makeFile, "%s: $(TARGETS.h)\n\n",
1226         abmfP_get_project_c_file_name(project));
1227
1228     for (trav_open(&moduleTrav, project, AB_TRAV_MODULES);
1229         (module = trav_next(&moduleTrav)) != NULL; )
1230     {
1231         if (!obj_is_defined(module))
1232         {
1233             continue;
1234         }
1235
1236         modFile = obj_get_file(module);
1237         util_strncpy(modName, obj_get_name(module), MAXPATHLEN+1);
1238
1239         if (osType == AB_OS_SUNOS)
1240             abio_printf(makeFile, "\n%s + %s + %s: %s %s\n",
1241                 abmfP_get_ui_c_file_name(module), 
1242                 abmfP_get_ui_header_file_name(module),
1243                 abmfP_get_stubs_c_file_name(module),
1244                 projFile, 
1245                 modFile);
1246         else
1247             abio_printf(makeFile, "\n%s %s %s: %s %s\n",
1248                 abmfP_get_ui_c_file_name(module), 
1249                 abmfP_get_ui_header_file_name(module),
1250                 abmfP_get_stubs_c_file_name(module),
1251                 projFile, 
1252                 modFile);
1253
1254         abio_printf(makeFile, "\t$(%s) %s %s\n", 
1255                         CODEGEN_MACRO, projFlag, modName);
1256
1257         /*
1258          * .o dependency on .c and .bil file 
1259          */
1260         abio_printf(makeFile, "%s: %s %s %s\n",
1261             cvt_to_obj(abmfP_get_ui_c_file_name(module)),
1262             abmfP_get_ui_c_file_name(module),
1263             abmfP_get_ui_header_file_name(module),
1264             modFile);
1265         abio_printf(makeFile, "%s: %s %s %s\n",
1266             cvt_to_obj(abmfP_get_stubs_c_file_name(module)),
1267             abmfP_get_stubs_c_file_name(module),
1268             abmfP_get_ui_header_file_name(module),
1269             modFile);
1270     }
1271     trav_close(&moduleTrav);
1272
1273     abio_puts(makeFile,
1274 "\n"
1275 "CLEAN_FILES =  core .dtcodegen.log *.BAK *.delta \\\n"
1276 "               $(PROGRAM) $(OBJS) $(TARGETS.c) $(TARGETS.h)\n" 
1277 "clean:\n"
1278 "       $(RM)  $(CLEAN_FILES)\n"
1279 "\n"
1280 "scour:\n"
1281 "       $(RM) $(CLEAN_FILES) $(TARGETS.h.merged) $(TARGETS.c.merged) \\\n"
1282 "             Makefile Makefile.aix Makefile.hpux Makefile.sunos \\\n"
1283 "             Makefile.osf1 Makefile.unixware Makefile.linux \\\n"
1284 "             Makefile.freebsd Makefile.netbsd Makefile.openbsd\n"
1285 );
1286
1287     return 0;
1288 }
1289
1290
1291 /*
1292  * Converts a .c file name to a .o file name
1293  */
1294 static STRING
1295 cvt_to_obj(STRING fileName)
1296 {
1297     static char buf[MAXPATHLEN] = "";
1298     snprintf(buf, sizeof(buf), "%s", fileName);
1299     buf[strlen(buf)-1] = 'o';
1300     return buf;
1301 }
1302
1303