Debian Codegraph @

terps/tads/tads3/tcmake.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/tads3/TCMAKE.CPP,v 1.1 1999/07/11 00:46:53 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 tcmake.cpp - TADS 3 Compiler "Make" Engine
15 Function
16 The TADS 3 compiler provides automatic dependency maintenance of
17 derived files through this "make" engine. The TADS 3 compilation
18 process, when a program is constructed from several source files
19 that are compiled separately, is as follows:
20
21 1. For each source file, create a symbol export file.
22 2. For each source file, load all other symbol export files, then
23 create an object file from the source file.
24 3. Load all object files, and create the image file.
25 4. If not compiling for debugging, load the image file, run
26 preinitialization, and write the updated image file.
27
28 The dependency tree is as follows:
29
30 - the ultimate target is the image file
31 - the image file depends upon all of the object files
32 - each object file depends upon all of the symbol export files,
33 plus the single corresponding source file
34 - each symbol export file depends upon the single corresponding
35 source file
36
37 Notes
38
39 Modified
40 07/09/99 MJRoberts - Creation
41 */
42
43 #include <stdlib.h>
44 #include <string.h>
45 #include <stdio.h>
46
47 #include "os.h"
48 #include "t3std.h"
49 #include "tctok.h"
50 #include "resload.h"
51 #include "tcmain.h"
52 #include "tchost.h"
53 #include "tcglob.h"
54 #include "tcprs.h"
55 #include "tctarg.h"
56 #include "vmfile.h"
57 #include "tcmake.h"
58 #include "vmpreini.h"
59 #include "vmhostsi.h"
60 #include "tcvsn.h"
61 #include "vmmaincn.h"
62 #include "vmrunsym.h"
63 #include "vmbignum.h"
64 #include "tcunas.h"
65 #include "vmcrc.h"
66 #include "rcmain.h"
67
68
69 /* ------------------------------------------------------------------------ */
70 /*
71 * Host interface mapping from our tcmain host interface to the rcmain host
72 * interface.
73 */
74 class CRcHostIfcTcmake: public CRcHostIfc
75 {
76 public:
77 CRcHostIfcTcmake(CTcHostIfc *ifc) { ifc_ = ifc; }
78
79 /*
80 * map the rcmain host interface methods to the equivalents in the
81 * tcmain host interface
82 */
83 virtual void display_error(const char *msg)
84 { ifc_->print_err("%s\n", msg); }
85 virtual void display_status(const char *msg)
86 { ifc_->print_step("%s\n", msg); }
87
88 private:
89 /* our underlying tcmain host interface */
90 CTcHostIfc *ifc_;
91 };
92
93 /* ------------------------------------------------------------------------ */
94 /*
95 * TADS 3 Compiler "Make" engine
96 */
97
98 /*
99 * initialize
100 */
101 CTcMake::CTcMake()
102 {
103 /* no modules yet */
104 mod_head_ = mod_tail_ = 0;
105
106 /* no #include paths yet */
107 inc_head_ = inc_tail_ = nonsys_inc_tail_ = 0;
108
109 /* no source paths yet */
110 src_head_ = src_tail_ = nonsys_src_tail_ = 0;
111
112 /* no preprocessor symbols defined yet */
113 def_head_ = def_tail_ = 0;
114
115 /* assume non-verbose mode */
116 verbose_ = FALSE;
117
118 /* assume we won't show error numbers */
119 show_err_numbers_ = FALSE;
120
121 /* assume we'll show standard but not pedantic warnings */
122 show_warnings_ = TRUE;
123 pedantic_ = FALSE;
124 warnings_as_errors_ = FALSE;
125
126 /* we don't have a list of warnings to suppress yet */
127 suppress_list_ = 0;
128 suppress_cnt_ = 0;
129
130 /* assume no debug information */
131 debug_ = FALSE;
132
133 /*
134 * assume we're actually compiling, not just preprocessing, listing
135 * included files, or cleaning up derived files
136 */
137 pp_only_ = FALSE;
138 list_includes_mode_ = FALSE;
139 clean_mode_ = FALSE;
140
141 /* assume we're not in test reporting mode */
142 test_report_mode_ = FALSE;
143
144 /* assume we're not in status percentage mode */
145 status_pct_mode_ = FALSE;
146
147 /* assume we won't want quoted filenames */
148 quoted_fname_mode_ = FALSE;
149
150 /* assume we'll link the program after compiling */
151 do_link_ = TRUE;
152
153 /* assume we'll use default preinit mode based on debug mode */
154 explicit_preinit_ = FALSE;
155
156 /* set an arbitrary data pool XOR mask */
157 data_xor_mask_ = 0xDF;
158
159 /* no source character set has been specified yet */
160 source_charset_ = 0;
161
162 /* presume we won't be capturing strings */
163 string_fp_ = 0;
164
165 /* presume we won't be generating an assembly listing */
166 assembly_listing_fp_ = 0;
167
168 /* assume we won't generate sourceTextGroup properties */
169 src_group_mode_ = FALSE;
170
171 /* presume we won't create output directories */
172 create_dirs_ = FALSE;
173
174 /* initialize the BigNumber internal cache */
175 CVmObjBigNum::init_cache();
176 }
177
178 /*
179 * delete
180 */
181 CTcMake::~CTcMake()
182 {
183 /* delete the modules */
184 while (mod_head_ != 0)
185 {
186 CTcMakeModule *nxt;
187
188 /* remember the next module */
189 nxt = mod_head_->get_next();
190
191 /* delete this one */
192 delete mod_head_;
193
194 /* move on to the next one */
195 mod_head_ = nxt;
196 }
197
198 /* delete the #include path list */
199 while (inc_head_ != 0)
200 {
201 CTcMakePath *nxt;
202
203 /* remember the next one */
204 nxt = inc_head_->get_next();
205
206 /* delete this one */
207 delete inc_head_;
208
209 /* move on to the next one */
210 inc_head_ = nxt;
211 }
212
213 /* delete the source path list */
214 while (src_head_ != 0)
215 {
216 CTcMakePath *nxt;
217
218 /* remember the next one */
219 nxt = src_head_->get_next();
220
221 /* delete this one */
222 delete src_head_;
223
224 /* move on to the next one */
225 src_head_ = nxt;
226 }
227
228 /* delete the preprocessor symbol list */
229 while (def_head_ != 0)
230 {
231 CTcMakeDef *nxt;
232
233 /* remember the next one */
234 nxt = def_head_->get_next();
235
236 /* delete this one */
237 delete def_head_;
238
239 /* move on to the next one */
240 def_head_ = nxt;
241 }
242
243 /* delete the source character set name */
244 lib_free_str(source_charset_);
245
246 /* delete the BigNumber cache */
247 CVmObjBigNum::term_cache();
248 }
249
250 /*
251 * Add a preprocessor symbol definition
252 */
253 void CTcMake::def_pp_sym(const char *sym, const char *expan)
254 {
255 /* if there's no expansion, use a default value of "1" */
256 if (expan == 0)
257 expan = "1";
258
259 /* add the symbol to our list */
260 add_pp_def(sym, expan, TRUE);
261 }
262
263 /*
264 * Undefine a preprocessor symbol
265 */
266 void CTcMake::undef_pp_sym(const char *sym)
267 {
268 /* add the symbol to our list */
269 add_pp_def(sym, 0, FALSE);
270 }
271
272 /*
273 * Add a preprocessor symbol define/undefine to our list
274 */
275 void CTcMake::add_pp_def(const char *sym, const char *expan, int is_def)
276 {
277 CTcMakeDef *def;
278
279 /* create the new list entry */
280 def = new CTcMakeDef(sym, expan, is_def);
281
282 /* link it in at the end of our list */
283 if (def_tail_ != 0)
284 def_tail_->set_next(def);
285 else
286 def_head_ = def;
287 def_tail_ = def;
288 }
289
290 /*
291 * look up a preprocess symbol definition
292 */
293 const char *CTcMake::look_up_pp_sym(const char *sym, size_t len)
294 {
295 CTcMakeDef *def;
296 const char *found_val;
297
298 /* iterate over our symbols to find this definition */
299 for (found_val = 0, def = def_head_ ; def != 0 ; def = def->get_next())
300 {
301 /* if this one matches, note it */
302 if (strlen(def->get_sym()) == len
303 && memcmp(def->get_sym(), sym, len) == 0)
304 {
305 /*
306 * it's a match - if this is a definition, remember it as the
307 * latest definition; if it's an undefinition, forget any
308 * previous definition we've noted
309 */
310 found_val = (def->is_def() ? def->get_expan() : 0);
311 }
312 }
313
314 /* return the definition we found, if any */
315 return found_val;
316 }
317
318 /*
319 * Add a module at the tail of the list
320 */
321 void CTcMake::add_module(CTcMakeModule *mod)
322 {
323 /* add it at the end of our list */
324 if (mod_tail_ != 0)
325 mod_tail_->set_next(mod);
326 else
327 mod_head_ = mod;
328 mod_tail_ = mod;
329 mod->set_next(0);
330 }
331
332 /*
333 * Add a module at the head of the list
334 */
335 void CTcMake::add_module_first(CTcMakeModule *mod)
336 {
337 /* add it at the end of our list */
338 mod->set_next(mod_head_);
339 mod_head_ = mod;
340
341 /* if it's the first thing in the list, it's the tail as well */
342 if (mod_tail_ == 0)
343 mod_tail_ = mod;
344 }
345
346 /*
347 * Add a module by filename
348 */
349 CTcMakeModule *CTcMake::add_module(const char *src_name,
350 const char *sym_name,
351 const char *obj_name,
352 int first)
353 {
354 /* create a module object */
355 CTcMakeModule *mod = new CTcMakeModule();
356
357 /* set the module name */
358 mod->set_module_name(src_name);
359
360 /* set the symbol file name if specified */
361 if (sym_name != 0)
362 mod->set_symbol_name(sym_name);
363
364 /* set the object file name if specified */
365 if (obj_name != 0)
366 mod->set_object_name(obj_name);
367
368 /* add it to the module list */
369 if (first)
370 add_module_first(mod);
371 else
372 add_module(mod);
373
374 /* return the new module */
375 return mod;
376 }
377
378 /*
379 * Add a #include path entry
380 */
381 void CTcMake::add_include_path(const textchar_t *path)
382 {
383 CTcMakePath *inc;
384
385 /* create the entry */
386 inc = new CTcMakePath(path);
387
388 /*
389 * add it at the end of our non-system list - note that system files
390 * might follow this point, so we need to insert the item into the
391 * list after the last existing non-system item
392 */
393 if (nonsys_inc_tail_ == 0)
394 {
395 /* no non-system path yet - insert at the head of the list */
396 inc->set_next(inc_head_);
397 inc_head_ = inc;
398 }
399 else
400 {
401 /* insert after the last non-system item */
402 inc->set_next(nonsys_inc_tail_->get_next());
403 nonsys_inc_tail_->set_next(inc);
404 }
405
406 /* this is the last non-system item */
407 nonsys_inc_tail_ = inc;
408
409 /* if this is the last item overall, advance the list tail */
410 if (inc->get_next() == 0)
411 inc_tail_ = inc;
412 }
413
414 /*
415 * Add a #include path entry if it's not already in our list
416 */
417 void CTcMake::maybe_add_include_path(const textchar_t *path)
418 {
419 CTcMakePath *inc;
420
421 /*
422 * scan our existing list of include paths for a match to this path -
423 * if the path is already in our list, we don't want to bother adding
424 * it again
425 */
426 for (inc = inc_head_ ; inc != 0 ; inc = inc->get_next())
427 {
428 /*
429 * If this existing path matches the new path, we don't need to
430 * bother adding the new path. Note that we just do a
431 * simple-minded string comparison of the paths, which might not
432 * always detect equivalent paths (this strategy can be fooled by
433 * hard/soft links, differing case in a case-insensitive file
434 * system, and relative vs. absolute notation, among other
435 * things); this isn't too important, though, because the worst
436 * that will happen is that we'll lose a little efficiency by
437 * searching the same directory twice.
438 */
439 if (strcmp(inc->get_path(), path) == 0)
440 return;
441 }
442
443 /* didn't find an exact match for the path, so add it */
444 add_include_path(path);
445 }
446
447 /*
448 * Add a system #include path entry
449 */
450 CTcMakePath *CTcMake::add_sys_include_path(const textchar_t *path)
451 {
452 CTcMakePath *inc;
453
454 /* create the entry */
455 inc = new CTcMakePath(path);
456
457 /* add it at the end of our list */
458 if (inc_tail_ != 0)
459 inc_tail_->set_next(inc);
460 else
461 inc_head_ = inc;
462 inc_tail_ = inc;
463 inc->set_next(0);
464
465 /* return the new path object */
466 return inc;
467 }
468
469 /*
470 * Add a source path entry
471 */
472 void CTcMake::add_source_path(const textchar_t *path)
473 {
474 CTcMakePath *src;
475
476 /* create the entry */
477 src = new CTcMakePath(path);
478
479 /*
480 * add it at the end of our non-system list - note that system files
481 * might follow this point, so we need to insert the item into the
482 * list after the last existing non-system item
483 */
484 if (nonsys_src_tail_ == 0)
485 {
486 /* no non-system path yet - insert at the head of the list */
487 src->set_next(src_head_);
488 src_head_ = src;
489 }
490 else
491 {
492 /* insert after the last non-system item */
493 src->set_next(nonsys_src_tail_->get_next());
494 nonsys_src_tail_->set_next(src);
495 }
496
497 /* this is the last non-system item */
498 nonsys_src_tail_ = src;
499
500 /* if this is the last item overall, advance the list tail */
501 if (src->get_next() == 0)
502 src_tail_ = src;
503 }
504
505 /*
506 * Add a system source path entry
507 */
508 CTcMakePath *CTcMake::add_sys_source_path(const textchar_t *path)
509 {
510 CTcMakePath *src;
511
512 /* create the entry */
513 src = new CTcMakePath(path);
514
515 /* add it at the end of our list */
516 if (src_tail_ != 0)
517 src_tail_->set_next(src);
518 else
519 src_head_ = src;
520 src_tail_ = src;
521 src->set_next(0);
522
523 /* return the object */
524 return src;
525 }
526
527 /*
528 * Check a new module we're adding to make sure its name doesn't collide
529 * with an existing module. Each module is required to have a unique root
530 * filename, so flag an error if the module name matches one that's already
531 * in the list.
532 */
533 void CTcMake::check_module_collision(CTcMakeModule *new_mod)
534 {
535 const char *new_root;
536 CTcMakeModule *cur_mod;
537
538 /* get the root name of this module */
539 new_root = os_get_root_name((char *)new_mod->get_source_name());
540
541 /*
542 * Scan the list of modules for a collision. Stop when we get to this
543 * module, because we want to report the collision at the later module,
544 * so that we report basically in the order of adding modules to the
545 * build.
546 */
547 for (cur_mod = mod_head_ ; cur_mod != 0 && cur_mod != new_mod ;
548 cur_mod = cur_mod->get_next())
549 {
550 const char *cur_root;
551
552 /*
553 * Compare this module's root name. Since some file systems are
554 * insensitive to case, ignore case ourselves; this is more
555 * restrictive than is actually necessary on some systems, but will
556 * flag the potential for problems in case the project is later
557 * moved to a different operating system.
558 */
559 cur_root = os_get_root_name((char *)cur_mod->get_source_name());
560 if (stricmp(cur_root, new_root) == 0)
561 {
562 char new_name_buf[OSFNMAX*2 + 10];
563
564 /* if the new module is from a library, so note */
565 if (new_mod->get_source_type() == TCMOD_SOURCE_NORMAL)
566 {
567 /* it's not from a library - just show the module name */
568 sprintf(new_name_buf, "\"%s\"", new_mod->get_source_name());
569 }
570 else
571 {
572 /* it is from a library - show the library name as well */
573 sprintf(new_name_buf,
574 tcerr_get_msg(TCERR_SOURCE_FROM_LIB, FALSE),
575 new_mod->get_source_name(), new_mod->get_from_lib());
576 }
577
578 /*
579 * Flag a warning. If the older module is from a library, use
580 * a separate version of the message, so that we can mention
581 * the library it's from.
582 */
583 if (cur_mod->get_source_type() == TCMOD_SOURCE_NORMAL)
584 {
585 /* the older module isn't from a library */
586 G_tcmain->log_error(0, 0, TC_SEV_ERROR,
587 TCERR_MODULE_NAME_COLLISION,
588 new_name_buf);
589 }
590 else
591 {
592 /* the older module is in a library, so mention the source */
593 G_tcmain->log_error(0, 0, TC_SEV_ERROR,
594 TCERR_MODULE_NAME_COLLISION_WITH_LIB,
595 new_name_buf, cur_mod->get_from_lib());
596 }
597
598 /*
599 * there's no need to find yet another module with the same
600 * name - it's good enough to flag the error once
601 */
602 break;
603 }
604 }
605 }
606
607 /*
608 * Check all modules for name collisions.
609 */
610 void CTcMake::check_all_module_collisions(CTcHostIfc *hostifc,
611 CResLoader *res_loader,
612 int *err_cnt, int *warn_cnt)
613 {
614 err_try
615 {
616 CTcMakeModule *cur;
617
618 /* initialize the compiler */
619 CTcMain::init(hostifc, res_loader, source_charset_);
620
621 /* set options */
622 set_compiler_options();
623
624 /* scan all of the modules in our list */
625 for (cur = mod_head_ ; cur != 0 ; cur = cur->get_next())
626 check_module_collision(cur);
627 }
628 err_finally
629 {
630 /* update the caller's error and warning counters */
631 *err_cnt += G_tcmain->get_error_count();
632 *warn_cnt += G_tcmain->get_warning_count();
633
634 /* terminate the compiler */
635 CTcMain::terminate();
636 }
637 err_end;
638 }
639
640 /*
641 * Build the program
642 */
643 void CTcMake::build(CTcHostIfc *hostifc, int *errcnt, int *warncnt,
644 int force_build, int force_link,
645 CRcResList *res_list, const char *argv0)
646 {
647 CResLoader *res_loader;
648 int fatal_error_count = 0;
649 CTcMakeModule *mod;
650 os_file_stat_t imgstat;
651 int build_image;
652 int run_preinit;
653 char exe_path[OSFNMAX];
654 CVmRuntimeSymbols *volatile runtime_symtab = 0;
655 CVmRuntimeSymbols *volatile runtime_macros = 0;
656 int seqno;
657
658 /* no warnings or errors so far */
659 *errcnt = 0;
660 *warncnt = 0;
661
662 /* create a resource loader */
663 os_get_special_path(exe_path, sizeof(exe_path), argv0, OS_GSP_T3_RES);
664 res_loader = new CResLoader(exe_path);
665
666 /* set the exectuable filename in the loader, if available */
667 if (os_get_exe_filename(exe_path, sizeof(exe_path), argv0))
668 res_loader->set_exe_filename(exe_path);
669
670 /*
671 * if we don't have an explicit preinit setting, turn preinit on if
672 * and only if we're not debugging
673 */
674 if (!explicit_preinit_)
675 run_preinit = !debug_;
676 else
677 run_preinit = preinit_;
678
679 err_try
680 {
681 textchar_t qu_buf[OSFNMAX*2 + 2], qu_buf_out[OSFNMAX*2 + 2];
682 int step_cnt, step_cur = 0;
683 char img_tool_data[4];
684 CVmCRC32 mod_crc;
685
686 /* check for module name collisions - give up if we find an error */
687 check_all_module_collisions(hostifc, res_loader, errcnt, warncnt);
688 if (*errcnt != 0 || (warnings_as_errors_ && *warncnt != 0))
689 goto done;
690
691 /*
692 * If we're merely running the preprocessor to generate the
693 * preprocessed source or to generate a list of #include files, go
694 * through each non-excluded module and preprocess it.
695 */
696 if (pp_only_ || list_includes_mode_)
697 {
698 /* preprocess each module */
699 for (mod = mod_head_ ; mod != 0 ; mod = mod->get_next())
700 {
701 textchar_t srcfile[OSFNMAX];
702
703 /* if it's an excluded module, skip it */
704 if (mod->is_excluded())
705 continue;
706
707 /* derive the source file name */
708 get_srcfile(srcfile, mod);
709
710 /* preprocess it */
711 preprocess_source(hostifc, res_loader, srcfile, mod,
712 errcnt, warncnt);
713 }
714
715 /* that's all we want to do */
716 goto done;
717 }
718
719 /*
720 * If we're in "clean" mode, do nothing except deleting the
721 * derived files implied by the command line.
722 */
723 if (clean_mode_)
724 {
725 /* clean up each module's derived files */
726 for (mod = mod_head_ ; mod != 0 ; mod = mod->get_next())
727 {
728 textchar_t fname[OSFNMAX];
729
730 /* skip any excluded modules */
731 if (mod->is_excluded())
732 continue;
733
734 /* delete the module's derived symbol file */
735 get_symfile(fname, mod);
736 if (!osfacc(fname))
737 {
738 /* report our progress */
739 hostifc->print_step("deleting symbol file %s\n",
740 get_step_fname(qu_buf, fname));
741
742 /* delete the file */
743 if (osfdel(fname))
744 hostifc->print_err(
745 "error: cannot delete %s\n", fname);
746 }
747
748 /* delete the module's derived object file */
749 get_objfile(fname, mod);
750 if (!osfacc(fname))
751 {
752 /* report our progress */
753 hostifc->print_step("deleting object file %s\n",
754 get_step_fname(qu_buf, fname));
755
756 /* delete the file */
757 if (osfdel(fname))
758 hostifc->print_err(
759 "error: cannot delete %s\n", fname);
760 }
761 }
762
763 /* delete the image file */
764 if (!osfacc(image_fname_.get()))
765 {
766 /* report our progress */
767 hostifc->print_step(
768 "deleting image file %s\n",
769 get_step_fname(qu_buf, image_fname_.get()));
770
771 /* delete the file */
772 if (osfdel(image_fname_.get()))
773 hostifc->print_err("error: cannot delete %s\n",
774 image_fname_.get());
775 }
776
777 /* that's all we want to do */
778 goto done;
779 }
780
781 /*
782 * If desired, create the output directories.
783 */
784 if (create_dirs_)
785 {
786 /* initialize the error subsystem for this work */
787 CTcMain::init(hostifc, res_loader, source_charset_);
788
789 /* create the symbol file directory */
790 if (symdir_.is_set())
791 create_dir(hostifc, symdir_.get(), FALSE,
792 &fatal_error_count);
793
794 /* create the object file directory */
795 if (objdir_.is_set())
796 create_dir(hostifc, objdir_.get(),
797 FALSE, &fatal_error_count);
798
799 /* create the image file directory */
800 if (image_fname_.is_set())
801 create_dir(hostifc, image_fname_.get(), TRUE,
802 &fatal_error_count);
803
804 /* done with the error subsystem for now */
805 CTcMain::terminate();
806
807 /* if any of those failed, give up */
808 if (fatal_error_count != 0)
809 goto done;
810 }
811
812 /*
813 * First, run through the files and determine what work we'll need
814 * to do.
815 */
816 for (mod = mod_head_, step_cnt = 0 ; mod != 0 ; mod = mod->get_next())
817 {
818 textchar_t srcfile[OSFNMAX];
819 textchar_t symfile[OSFNMAX];
820 textchar_t objfile[OSFNMAX];
821 os_file_stat_t srcstat, symstat, objstat;
822 int sym_recomp;
823 int obj_recomp;
824
825 /* if this module is excluded, skip it */
826 if (mod->is_excluded())
827 continue;
828
829 /* derive the source, symbol, and object file names */
830 get_srcfile(srcfile, mod);
831 get_symfile(symfile, mod);
832 get_objfile(objfile, mod);
833
834 /*
835 * add this module's source file name into the module-list
836 * checksum - we use this to determine if there have been any
837 * changes to the module list since the last time we linked the
838 * image file, so we can tell if we need to re-link
839 */
840 mod_crc.scan_bytes(srcfile, get_strlen(srcfile));
841
842 /* presume we will not need to recompile this file */
843 sym_recomp = FALSE;
844 obj_recomp = FALSE;
845
846 /*
847 * Check to see if we need to rebuild this symbol file.
848 *
849 * Get the file times for the source and symbol files. If
850 * either file doesn't exist, or the source modification time
851 * is later than the symbol file's modification time, then
852 * we'll need to build the symbol file.
853 */
854 if (force_build
855 || !os_file_stat(srcfile, TRUE, &srcstat)
856 || !os_file_stat(symfile, TRUE, &symstat)
857 || srcstat.mod_time > symstat.mod_time)
858 {
859 /* this symbol file requires recompilation */
860 sym_recomp = TRUE;
861 }
862 else
863 {
864 osfildef *fp;
865 textchar_t sym_fname[OSFNMAX];
866
867 /*
868 * On the basis of the file system timestamps alone, the
869 * symbol file doesn't require rebuilding. Check to see if
870 * the configuration data stored in the symbol file is
871 * different from our current configuration - if so, we'll
872 * need to rebuild the symbol file.
873 */
874 get_symfile(sym_fname, mod);
875 fp = osfoprb(sym_fname, OSFTT3SYM);
876 if (fp == 0)
877 {
878 /* there's no symbol file - definitely recompile */
879 sym_recomp = TRUE;
880 }
881 else
882 {
883
884 /* set up the symbol file descriptor */
885 CVmFile *sym_file = new CVmFile();
886 sym_file->set_file(fp, 0);
887
888 /* seek to the start of our config data */
889 ulong siz =
890 CTcParser::seek_sym_file_build_config_info(sym_file);
891
892 /* compare the configuration */
893 int same_config;
894 err_try
895 {
896 /* compare the configuration */
897 same_config =
898 (siz != 0
899 && compare_build_config_from_sym_file(
900 sym_fname, sym_file));
901 }
902 err_catch_disc
903 {
904 /*
905 * the configuration information is invalid -
906 * assume that recompilation is required
907 */
908 same_config = FALSE;
909 }
910 err_end;
911
912 /*
913 * if we don't have the same configuration, we must
914 * recompile this source file
915 */
916 if (!same_config)
917 {
918 /* note that recompilation is necessary */
919 sym_recomp = TRUE;
920 }
921
922 /* delete the CVmFile object and close the file */
923 delete sym_file;
924 }
925 }
926
927 /* note whether or not we have to build the symbol file */
928 mod->set_needs_sym_recompile(sym_recomp);
929
930 /* check to see if we need to rebuild it */
931 if (sym_recomp)
932 {
933 /* we do - count the step */
934 ++step_cnt;
935
936 /*
937 * delete the current symbol file - if our build attempt
938 * fails before we get here, this will ensure that we'll
939 * pick up this dependency again on subsequent builds
940 */
941 osfdel(symfile);
942 }
943
944 /*
945 * Check to see if we need to rebuild this object file.
946 *
947 * Check the file times - if the object file doesn't exist, or
948 * the source modification time is later than the object file's
949 * modification time, we need to build the object file.
950 *
951 * In addition, if we had to compile the symbol file, it
952 * doesn't matter whether the object file is up-to-date with
953 * the source file - always rebuild the object file if we had
954 * to rebuild the symbol file.
955 *
956 * Finally, we need to recompile the object file if the symbol
957 * file has a more recent timestamp than the object file. This
958 * can happen when a previous build got as far as our symbol
959 * file before encountering an error, in which case the object
960 * file could appear up-to-date.
961 */
962 if (force_build
963 || sym_recomp
964 || !os_file_stat(srcfile, TRUE, &srcstat)
965 || !os_file_stat(objfile, TRUE, &objstat)
966 || !os_file_stat(symfile, TRUE, &symstat)
967 || srcstat.mod_time > objstat.mod_time
968 || symstat.mod_time > objstat.mod_time)
969 {
970 /* we do have to rebuild the object file */
971 obj_recomp = TRUE;
972 }
973
974 /* note whether or not we have to build the object file */
975 mod->set_needs_obj_recompile(obj_recomp);
976
977 /* check to see if we need to rebuild the object file */
978 if (obj_recomp)
979 {
980 /* we do - count the step */
981 ++step_cnt;
982
983 /*
984 * delete the object file, to ensure we eventually compile
985 * it even if this attempt fails
986 */
987 osfdel(objfile);
988 }
989 }
990
991 /*
992 * We've scanned all of the modules now, so we have the checksum
993 * value for the source file list. Store this in our special
994 * reserved tool-data field in the image file signature, so that we
995 * can check on the next build to see if the module list has
996 * changed.
997 */
998 oswp4(img_tool_data, mod_crc.get_crc_val());
999
1000 /*
1001 * Determine if we need to build the image file. If any of the
1002 * existing object files are newer than the image file, we need to
1003 * build the image. If we're going to build any object files,
1004 * they'll definitely be newer, because they don't even exist yet.
1005 * Build the image regardless of dates if we're forcing a full
1006 * build or forcing a link.
1007 *
1008 * First, check to see if the user even wants us to perform linking
1009 * - if not, don't build the image file. Next, check to see if we
1010 * even need to look at at object file; if the image file doesn't
1011 * exist, or we're forcing a full build, we don't need to bother
1012 * checking timestamps.
1013 */
1014 if (!do_link_)
1015 {
1016 /* the user specifically wants to skip linking the image file */
1017 build_image = FALSE;
1018 }
1019 else if (force_build || force_link
1020 || !os_file_stat(image_fname_.get(), TRUE, &imgstat))
1021 {
1022 /* we definitely need to build the image file */
1023 build_image = TRUE;
1024 }
1025 else
1026 {
1027 char sig_buf[45];
1028 osfildef *img_fp;
1029
1030 /* presume we won't need to rebuild the image file */
1031 build_image = FALSE;
1032
1033 /*
1034 * Read the "tool data" bytes stored in the image file. We
1035 * store a CRC-32 checksum of the names of all of the source
1036 * files in this field in the image file, so that we can tell
1037 * if the module list has changed since the last time we
1038 * linked. If the module list has changed, we need to rebuild
1039 * the image file.
1040 */
1041 if ((img_fp = osfoprb(image_fname_.get(), OSFTT3IMG)) == 0
1042 || osfrb(img_fp, sig_buf, 45))
1043 {
1044 /* couldn't open/read the file - definitely rebuild */
1045 build_image = TRUE;
1046 }
1047 else
1048 {
1049 unsigned long old_crc;
1050
1051 /* read the CRC value from the image file */
1052 old_crc = t3rp4u(sig_buf + 41);
1053
1054 /* rebuild if it doesn't match the current signature */
1055 if (old_crc != mod_crc.get_crc_val())
1056 build_image = TRUE;
1057 }
1058
1059 /* close the file if we opened it */
1060 if (img_fp != 0)
1061 osfcls(img_fp);
1062
1063 /* if we didn't just decide to rebuild, check module dates */
1064 if (!build_image)
1065 {
1066 /* scan each object module to see if any are more recent */
1067 for (mod = mod_head_ ; mod != 0 ; mod = mod->get_next())
1068 {
1069 textchar_t objfile[OSFNMAX];
1070 os_file_stat_t objstat;
1071
1072 /* if this module is excluded, skip it */
1073 if (mod->is_excluded())
1074 continue;
1075
1076 /*
1077 * if this module is being recompiled, it'll definitely
1078 * be newer than the image file, since we haven't even
1079 * started building the new object file yet
1080 */
1081 if (mod->get_needs_obj_recompile())
1082 {
1083 /* we need to build the image */
1084 build_image = TRUE;
1085
1086 /* no need to look any further */
1087 break;
1088 }
1089
1090 /* derive the object file name */
1091 get_objfile(objfile, mod);
1092
1093 /* if it's more recent, we need to build the image */
1094 if (!os_file_stat(objfile, TRUE, &objstat)
1095 || objstat.mod_time > imgstat.mod_time)
1096 {
1097 /* we need to build the image */
1098 build_image = TRUE;
1099
1100 /* no need to continue scanning object files */
1101 break;
1102 }
1103 }
1104 }
1105
1106 /*
1107 * if we're still not building the image file, check to see if
1108 * any resource files are newer - if so, we'll need to relink
1109 * to get a clean slate for refreshing the resources
1110 */
1111 if (!build_image && res_list != 0 && res_list->get_count() != 0)
1112 {
1113 /* scan each resource file */
1114 for (CRcResEntry *r = res_list->get_head() ; r != 0 ;
1115 r = r->get_next())
1116 {
1117 /* check this timestamp */
1118 os_file_stat_t resstat;
1119 if (!os_file_stat(r->get_fname(), TRUE, &resstat)
1120 || resstat.mod_time > imgstat.mod_time)
1121 {
1122 /* we need to rebuild the image */
1123 build_image = TRUE;
1124 break;
1125 }
1126 }
1127 }
1128 }
1129
1130 /* if we have to link the image file, count the step */
1131 if (build_image)
1132 {
1133 /* count the link step */
1134 ++step_cnt;
1135
1136 /* count the preinit step if necessary */
1137 if (run_preinit)
1138 ++step_cnt;
1139
1140 /* count the resource bundler step if necessary */
1141 if (res_list != 0 && res_list->get_count() != 0)
1142 ++step_cnt;
1143 }
1144
1145 /* display the initial estimate of the number of build steps */
1146 if (status_pct_mode_)
1147 hostifc->print_step("%%PCT:0/%d\n", step_cnt);
1148 if (step_cnt != 0)
1149 hostifc->print_step("Files to build: %d\n", step_cnt);
1150
1151 /* assign sequence numbers to the modules */
1152 for (mod = mod_head_, seqno = 1 ; mod != 0 ; mod = mod->get_next())
1153 mod->set_seqno(seqno++);
1154
1155 /*
1156 * Build the symbol files. Go through our list of source files.
1157 * For each source file that is more recent than its symbol file,
1158 * or for which no symbol file exists, build the symbol file.
1159 */
1160 for (mod = mod_head_ ; mod != 0 ; mod = mod->get_next())
1161 {
1162 /* recompile if necessary */
1163 if (!mod->is_excluded() && mod->get_needs_sym_recompile())
1164 {
1165 textchar_t srcfile[OSFNMAX];
1166 textchar_t symfile[OSFNMAX];
1167
1168 /* derive the source and symbol file names */
1169 get_srcfile(srcfile, mod);
1170 get_symfile(symfile, mod);
1171
1172 /* display what we're doing */
1173 hostifc->print_step("symbol_export %s -> %s\n",
1174 get_step_fname(qu_buf, srcfile),
1175 get_step_fname(qu_buf_out, symfile));
1176
1177 /* add the percentage display, if desired */
1178 if (status_pct_mode_)
1179 hostifc->print_step("%%PCT:%d/%d\n", step_cur, step_cnt);
1180 ++step_cur;
1181
1182 /* we need to build the symbol file */
1183 build_symbol_file(hostifc, res_loader, srcfile, symfile,
1184 mod, errcnt, warncnt);
1185
1186 /* if any errors occurred, stop now */
1187 if (*errcnt != 0 || (warnings_as_errors_ && *warncnt != 0))
1188 goto done;
1189 }
1190 }
1191
1192 /*
1193 * Build object files. Go through our list of source files.
1194 * For each source file that is more recent than its object
1195 * file, or for which no object file exists, build the object
1196 * file.
1197 */
1198 for (mod = mod_head_ ; mod != 0 ; mod = mod->get_next())
1199 {
1200 /* if this module is excluded, skip it */
1201 if (!mod->is_excluded() && mod->get_needs_obj_recompile())
1202 {
1203 textchar_t srcfile[OSFNMAX];
1204 textchar_t objfile[OSFNMAX];
1205
1206 /* derive the source and object file names */
1207 get_srcfile(srcfile, mod);
1208 get_objfile(objfile, mod);
1209
1210 /* display what we're doing */
1211 hostifc->print_step("compile %s -> %s\n",
1212 get_step_fname(qu_buf, srcfile),
1213 get_step_fname(qu_buf_out, objfile));
1214
1215 /* add the percentage display, if desired */
1216 if (status_pct_mode_)
1217 hostifc->print_step("%%PCT:%d/%d\n", step_cur, step_cnt);
1218 ++step_cur;
1219
1220 /* we need to build the object file */
1221 build_object_file(hostifc, res_loader, srcfile, objfile,
1222 mod, errcnt, warncnt);
1223
1224 /* if any errors occurred, stop now */
1225 if (*errcnt != 0 || (warnings_as_errors_ && *warncnt != 0))
1226 goto done;
1227 }
1228 }
1229
1230 /* if we're building the image file, do so now */
1231 if (build_image)
1232 {
1233 const char *link_output;
1234 char inter_file[OSFNMAX + 10];
1235
1236 /*
1237 * if we're running preinit, link to an intermediate file,
1238 * so that we can wait to store the final preinitialized
1239 * version in the actual image file; otherwise, link
1240 * directly to the final image filename
1241 */
1242 if (run_preinit)
1243 {
1244 /* we'll run preinit - link to an intermediate file */
1245 lib_strcpy(inter_file, OSFNMAX, image_fname_.get());
1246 os_remext(inter_file);
1247 os_addext(inter_file, "t3p");
1248
1249 /* use the intermediate file as the linker output */
1250 link_output = inter_file;
1251
1252 /*
1253 * we're running preinit, so we need to build a global
1254 * symbol table and a macro table to pass to preinit
1255 */
1256 runtime_symtab = new CVmRuntimeSymbols();
1257 runtime_macros = new CVmRuntimeSymbols();
1258 }
1259 else
1260 {
1261 /* no preinit - link directly to the image filename */
1262 link_output = image_fname_.get();
1263 }
1264
1265 /* display what we're doing */
1266 hostifc->print_step("link -> %s\n",
1267 get_step_fname(qu_buf, link_output));
1268
1269 /* add the percentage display, if desired */
1270 if (status_pct_mode_)
1271 hostifc->print_step("%%PCT:%d/%d\n", step_cur, step_cnt);
1272 ++step_cur;
1273
1274 /* build it */
1275 build_image_file(hostifc, res_loader, link_output,
1276 errcnt, warncnt, runtime_symtab, runtime_macros,
1277 img_tool_data);
1278
1279 /* if an error occurred, don't bother with preinit */
1280 if (*errcnt != 0 || (warnings_as_errors_ && *warncnt != 0))
1281 goto done;
1282
1283 /* run preinit if desired */
1284 if (run_preinit)
1285 {
1286 CVmFile *file_in;
1287 CVmFile *file_out;
1288 osfildef *fp_in;
1289 osfildef *fp_out;
1290 CVmHostIfc *vmhostifc;
1291 CVmMainClientConsole clientifc;
1292
1293 /* add the percentage display, if desired */
1294 if (status_pct_mode_)
1295 hostifc->print_step("%%PCT:%d/%d\n", step_cur, step_cnt);
1296 ++step_cur;
1297
1298 /* note what we're doing */
1299 hostifc->print_step(
1300 "preinit -> %s\n",
1301 get_step_fname(qu_buf, image_fname_.get()));
1302
1303 /* open the linker output file for reading */
1304 if ((fp_in = osfoprb(link_output, OSFTT3IMG)) == 0)
1305 {
1306 /* can't open the input file - throw an error */
1307 err_throw_a(TCERR_MAKE_CANNOT_OPEN_IMG, 1,
1308 ERR_TYPE_CHAR, link_output);
1309 }
1310
1311 /* open the final image file for writing */
1312 if ((fp_out = osfopwb(image_fname_.get(), OSFTT3IMG)) == 0)
1313 {
1314 /* close the input file */
1315 osfcls(fp_in);
1316
1317 /* throw an error */
1318 err_throw_a(TCERR_MAKE_CANNOT_CREATE_IMG, 1,
1319 ERR_TYPE_CHAR, image_fname_.get());
1320 }
1321
1322 /* set up the input file object */
1323 file_in = new CVmFile();
1324 file_in->set_file(fp_in, 0);
1325
1326 /* set up the output file object */
1327 file_out = new CVmFile();
1328 file_out->set_file(fp_out, 0);
1329
1330 /* create a stdio host interface */
1331 vmhostifc = new CVmHostIfcStdio(argv0);
1332
1333 /* catch errors that occur during preinit */
1334 err_try
1335 {
1336 /* run preinit to build the image file */
1337 vm_run_preinit(file_in, link_output,
1338 file_out, vmhostifc, &clientifc, 0, 0,
1339 runtime_symtab, runtime_macros);
1340 }
1341 err_finally
1342 {
1343 /* delete our VM host interface */
1344 delete vmhostifc;
1345
1346 /* close and delete our files */
1347 delete file_in;
1348 delete file_out;
1349 }
1350 err_end;
1351
1352 /* delete the intermediate file */
1353 osfdel(link_output);
1354 }
1355
1356 /*
1357 * if we have any resources, add them to the image (or just add
1358 * links to them, if compiling for debugging)
1359 */
1360 if (res_list != 0 && res_list->get_count() != 0)
1361 {
1362 CRcHostIfcTcmake rc_hostifc(hostifc);
1363
1364 /* add the percentage display, if desired */
1365 if (status_pct_mode_)
1366 hostifc->print_step("%%PCT:%d/%d\n", step_cur, step_cnt);
1367 ++step_cur;
1368
1369 /* mention what we're doing */
1370 hostifc->print_step(
1371 "add_resource%ss -> %s\n",
1372 debug_ ? " link" : "",
1373 get_step_fname(qu_buf, image_fname_.get()));
1374
1375 /*
1376 * Add the resources. If we're in debug mode, add them in
1377 * "link mode," meaning that we merely add links from the
1378 * resources to the equivalent local filenames.
1379 */
1380 if (CResCompMain::add_resources(
1381 image_fname_.get(), res_list, &rc_hostifc,
1382 FALSE, OSFTT3IMG, debug_))
1383 {
1384 /* failed - delete the image file */
1385 osfdel(image_fname_.get());
1386 }
1387 }
1388 }
1389
1390 done: ;
1391 }
1392 err_catch(exc)
1393 {
1394 /*
1395 * if it's not a general internal or fatal error, log it; don't
1396 * log general errors, since these will have been logged as
1397 * specific internal errors before being thrown
1398 */
1399 if (exc->get_error_code() != TCERR_INTERNAL_ERROR
1400 && exc->get_error_code() != TCERR_FATAL_ERROR)
1401 {
1402 /* make sure the host interface is set up */
1403 G_hostifc = hostifc;
1404
1405 /* show the error */
1406 CTcMain::S_log_error(
1407 0, 0, errcnt, warncnt,
1408 (verbose_ ? TCMAIN_ERR_VERBOSE : 0)
1409 | (show_err_numbers_ ? TCMAIN_ERR_NUMBERS : 0)
1410 | (show_warnings_ ? TCMAIN_ERR_WARNINGS : 0)
1411 | (pedantic_ ? TCMAIN_ERR_PEDANTIC : 0)
1412 | (test_report_mode_ ? TCMAIN_ERR_TESTMODE : 0)
1413 | (quoted_fname_mode_ ? TCMAIN_ERR_FNAME_QU : 0),
1414 suppress_list_, suppress_cnt_, TC_SEV_FATAL, exc);
1415 }
1416
1417 /* count the fatal error */
1418 ++fatal_error_count;
1419 }
1420 err_end;
1421
1422 /* if we created a runtime symbol table, delete it */
1423 if (runtime_symtab != 0)
1424 delete runtime_symtab;
1425
1426 /* likewise the macro table */
1427 if (runtime_macros != 0)
1428 delete runtime_macros;
1429
1430 /* done with the resource loader */
1431 delete res_loader;
1432
1433 /* if any fatal errors occurred, include them in the error count */
1434 *errcnt += fatal_error_count;
1435 }
1436
1437 /*
1438 * Create a directory
1439 */
1440 void CTcMake::create_dir(CTcHostIfc *hostifc,
1441 const char *path, int is_file, int *errcnt)
1442 {
1443 char dir[OSFNMAX];
1444
1445 /*
1446 * If the path includes a filename portion, remove it to get the parent
1447 * directory path.
1448 */
1449 if (is_file)
1450 {
1451 /* extract the directory path portion */
1452 os_get_path_name(dir, sizeof(dir), path);
1453 path = dir;
1454
1455 /* don't do anything if there's no path */
1456 if (strlen(dir) == 0)
1457 return;
1458 }
1459
1460 /* if the directory doesn't already exist, create it */
1461 if (osfacc(path))
1462 {
1463 /* mention it */
1464 hostifc->print_step("creating output directory %s\n", path);
1465
1466 /* try creating the folder */
1467 if (!os_mkdir(path, TRUE))
1468 {
1469 /* failed - log the error */
1470 G_tcmain->log_error(0, 0, TC_SEV_ERROR,
1471 TCERR_CANNOT_CREATE_DIR, path);
1472
1473 /* count it */
1474 *errcnt += 1;
1475 }
1476 }
1477 }
1478
1479
1480 /*
1481 * Build the version of a filename to show in a progress report.
1482 */
1483 const char *CTcMake::get_step_fname(char *buf, const char *fname)
1484 {
1485 /*
1486 * If we're in test-report mode, show only the root name. We don't
1487 * want to show any paths in this mode, because we want our test output
1488 * to be independent of the local directory structure; this ensures
1489 * that tests can be run on any machine and then diff'ed against
1490 * portable reference logs.
1491 */
1492 if (test_report_mode_)
1493 fname = os_get_root_name((char *)fname);
1494
1495 /* if we want quoted filenames, quote the filename */
1496 if (quoted_fname_mode_)
1497 {
1498 const char *src;
1499 char *dst;
1500
1501 /* add quotes around the filename, and stutter any quotes within */
1502 for (src = fname, dst = buf + 1, buf[0] = '"' ; *src != '\0' ; )
1503 {
1504 /* if this is a quote, stutter it */
1505 if (*src == '"')
1506 *dst++ = '"';
1507
1508 /* add this character */
1509 *dst++ = *src++;
1510 }
1511
1512 /* add the close quote and null terminator */
1513 *dst++ = '"';
1514 *dst = '\0';
1515
1516 /* use the copy in the buffer */
1517 fname = buf;
1518 }
1519
1520 /* return the final filename */
1521 return fname;
1522 }
1523
1524 /*
1525 * preprocess a source file to standard output
1526 */
1527 void CTcMake::preprocess_source(CTcHostIfc *hostifc,
1528 CResLoader *res_loader,
1529 const textchar_t *src_fname,
1530 CTcMakeModule *src_mod,
1531 int *error_count, int *warning_count)
1532 {
1533 CTcTokFileDesc *desc;
1534 long linenum;
1535
1536 err_try
1537 {
1538 int err;
1539
1540 /* initialize the compiler */
1541 CTcMain::init(hostifc, res_loader, source_charset_);
1542
1543 /* set options */
1544 set_compiler_options();
1545
1546 /* set up the tokenizer with the main input file */
1547 err = G_tok->set_source(src_fname, src_mod->get_orig_name());
1548 if (err != 0)
1549 {
1550 G_tcmain->log_error(0, 0, TC_SEV_ERROR, err,
1551 (int)strlen(src_fname), src_fname);
1552 goto done;
1553 }
1554
1555 /*
1556 * Put the tokenizer into the appropriate preprocessing mode -
1557 * this makes changes to the way it expands certain directives to
1558 * make the output suitable for external consumption rather than
1559 * internal use.
1560 */
1561 G_tok->set_mode_pp_only(pp_only_);
1562 G_tok->set_list_includes_mode(list_includes_mode_);
1563
1564 /* we haven't read anything yet, so we have no previous line info */
1565 desc = 0;
1566 linenum = 0;
1567
1568 /*
1569 * if we're writing out the preprocessed source, write a #charset
1570 * directive to indicate that our output is in utf8 format
1571 */
1572 if (pp_only_)
1573 printf("#charset \"utf8\"\n");
1574
1575 /* read the file */
1576 for (;;)
1577 {
1578 /* read the next line, and stop if we've reached end of file */
1579 if (G_tok->read_line_pp())
1580 break;
1581
1582 /*
1583 * If we're generating the preprocessed source, and we're in a
1584 * different stream than for the last line, or the new line
1585 * number is more than the last line number plus 1, add a
1586 * #line directive to the output stream
1587 */
1588 if (pp_only_
1589 && (G_tok->get_last_desc() != desc
1590 || G_tok->get_last_linenum() != linenum + 1))
1591 {
1592 /* we've jumped to a new line - add a #line directive */
1593 printf("#line %ld %s\n", G_tok->get_last_linenum(),
1594 (test_report_mode_
1595 ? G_tok->get_last_desc()->get_dquoted_rootname()
1596 : G_tok->get_last_desc()->get_dquoted_fname()));
1597 }
1598
1599 /* remember the last line we read */
1600 desc = G_tok->get_last_desc();
1601 linenum = G_tok->get_last_linenum();
1602
1603 /* show this line if we're generating preprocessed output */
1604 if (pp_only_)
1605 printf("%s\n", G_tok->get_cur_line());
1606 }
1607
1608 done: ;
1609 }
1610 err_finally
1611 {
1612 /* update the caller's error and warning counters */
1613 *error_count += G_tcmain->get_error_count();
1614 *warning_count += G_tcmain->get_warning_count();
1615
1616 /* terminate the compiler */
1617 CTcMain::terminate();
1618 }
1619 err_end;
1620 }
1621
1622 /*
1623 * build a symbol file
1624 */
1625 void CTcMake::build_symbol_file(CTcHostIfc *hostifc,
1626 CResLoader *res_loader,
1627 const textchar_t *src_fname,
1628 const textchar_t *sym_fname,
1629 CTcMakeModule *src_mod,
1630 int *error_count, int *warning_count)
1631 {
1632 osfildef *fpout;
1633 CVmFile *volatile fp = 0;
1634
1635 err_try
1636 {
1637 int err;
1638
1639 /* initialize the compiler */
1640 CTcMain::init(hostifc, res_loader, source_charset_);
1641
1642 /* set options */
1643 set_compiler_options();
1644
1645 /* set up the tokenizer with the main input file */
1646 err = G_tok->set_source(src_fname, src_mod->get_orig_name());
1647 if (err != 0)
1648 {
1649 G_tcmain->log_error(0, 0, TC_SEV_ERROR, err,
1650 (int)strlen(src_fname), src_fname);
1651 goto done;
1652 }
1653
1654 /* parse in syntax-only mode */
1655 G_prs->set_syntax_only(TRUE);
1656
1657 /* read the first token */
1658 G_tok->next();
1659
1660 /* give the parser the module information */
1661 G_prs->set_module_info(
1662 src_mod->get_orig_name(), src_mod->get_seqno());
1663
1664 /* set the sourceTextGroup mode */
1665 G_prs->set_source_text_group_mode(src_group_mode_);
1666
1667 /* parse at the top level */
1668 G_prs->parse_top();
1669
1670 /* if no errors occurred, write the symbol file */
1671 if (G_tcmain->get_error_count() == 0)
1672 {
1673 /* set up the output file */
1674 fpout = osfopwb(sym_fname, OSFTT3SYM);
1675 if (fpout == 0)
1676 {
1677 G_tcmain->log_error(0, 0, TC_SEV_ERROR,
1678 TCERR_MAKE_CANNOT_CREATE_SYM, sym_fname);
1679 goto done;
1680 }
1681 fp = new CVmFile();
1682 fp->set_file(fpout, 0);
1683
1684 /* write the symbol file */
1685 G_prs->write_symbol_file(fp, this);
1686 }
1687
1688 done: ;
1689 }
1690 err_finally
1691 {
1692 /* set the caller's error and warning counters */
1693 *error_count += G_tcmain->get_error_count();
1694 *warning_count += G_tcmain->get_warning_count();
1695
1696 /* close our symbol file */
1697 if (fp != 0)
1698 {
1699 delete fp;
1700 fp = 0;
1701 }
1702
1703 /*
1704 * if we weren't successful, delete the symbol file, so that we
1705 * don't leave around a partially-built file
1706 */
1707 if (*error_count != 0 || (warnings_as_errors_ && *warning_count != 0))
1708 osfdel(sym_fname);
1709
1710 /* terminate the compiler */
1711 CTcMain::terminate();
1712 }
1713 err_end;
1714 }
1715
1716 /*
1717 * build an object file
1718 */
1719 void CTcMake::build_object_file(CTcHostIfc *hostifc,
1720 CResLoader *res_loader,
1721 const textchar_t *src_fname,
1722 const textchar_t *obj_fname,
1723 CTcMakeModule *src_mod,
1724 int *error_count, int *warning_count)
1725 {
1726 osfildef *fpout;
1727 CVmFile *volatile fp = 0;
1728 CVmFile *volatile symfile = 0;
1729 int err;
1730
1731 err_try
1732 {
1733 CTPNStmProg *node;
1734 CTcMakeModule *mod;
1735
1736 /* initialize the compiler */
1737 CTcMain::init(hostifc, res_loader, source_charset_);
1738
1739 /*
1740 * keep object and property fixups, so that we can link the
1741 * object file with other object files - each object file will
1742 * use its own object and property numbering system, and we must
1743 * reconcile the systems when we link
1744 */
1745 G_keep_objfixups = TRUE;
1746 G_keep_propfixups = TRUE;
1747 G_keep_enumfixups = TRUE;
1748
1749 /* set options */
1750 set_compiler_options();
1751
1752 /* tell the tokenizer to capture strings to the string file */
1753 if (string_fp_ != 0)
1754 G_tok->set_string_capture(string_fp_);
1755
1756 /* tell the code generator to write an assembly listing */
1757 if (assembly_listing_fp_ != 0)
1758 {
1759 /* set up the disassembly output stream */
1760 G_disasm_out = new CTcUnasOutFile(assembly_listing_fp_);
1761
1762 /* introduce the file */
1763 G_disasm_out->print("[%s]\n\n", src_fname);
1764 }
1765
1766 /*
1767 * load each module's symbols (including our own - this provides
1768 * us with forward definitions of any symbols defined in our own
1769 * source module but referenced prior to the definitions)
1770 */
1771 for (mod = mod_head_ ; mod != 0 ; mod = mod->get_next())
1772 {
1773 osfildef *symfp;
1774 char sym_fname[OSFNMAX];
1775
1776 /* if this module is excluded, skip it */
1777 if (mod->is_excluded())
1778 continue;
1779
1780 /* derive the symbol filename */
1781 get_symfile(sym_fname, mod);
1782
1783 /* open the file */
1784 symfp = osfoprb(sym_fname, OSFTT3SYM);
1785 if (symfp == 0)
1786 {
1787 /* log the error */
1788 G_tcmain->log_error(0, 0, TC_SEV_ERROR,
1789 TCERR_MAKE_CANNOT_OPEN_SYM, sym_fname);
1790 }
1791 else
1792 {
1793 /* set up the file object */
1794 symfile = new CVmFile();
1795 symfile->set_file(symfp, 0);
1796
1797 /* load it */
1798 G_prs->read_symbol_file(symfile);
1799
1800 /* done with the symbol file - close it */
1801 delete symfile;
1802 symfile = 0;
1803 }
1804 }
1805
1806 /* if we encountered errors reading symbol files, give up now */
1807 if (G_tcmain->get_error_count() != 0)
1808 goto done;
1809
1810 /* set up the tokenizer with the main input file */
1811 err = G_tok->set_source(src_fname, src_mod->get_orig_name());
1812 if (err != 0)
1813 {
1814 G_tcmain->log_error(0, 0, TC_SEV_ERROR, err,
1815 (int)strlen(src_fname), src_fname);
1816 goto done;
1817 }
1818
1819 /* read the first token */
1820 G_tok->next();
1821
1822 /* give the parser the module information */
1823 G_prs->set_module_info(
1824 src_mod->get_orig_name(), src_mod->get_seqno());
1825
1826 /* set the sourceTextGroup mode */
1827 G_prs->set_source_text_group_mode(src_group_mode_);
1828
1829 /* parse at the top level */
1830 node = G_prs->parse_top();
1831 if (G_tcmain->get_error_count() != 0)
1832 goto done;
1833
1834 /* fold symbolic constants for all nodes */
1835 node->fold_constants(G_prs->get_global_symtab());
1836 if (G_tcmain->get_error_count() != 0)
1837 goto done;
1838
1839 /* set up the output file */
1840 fpout = osfopwb(obj_fname, OSFTT3OBJ);
1841 if (fpout == 0)
1842 {
1843 G_tcmain->log_error(0, 0, TC_SEV_ERROR,
1844 TCERR_MAKE_CANNOT_CREATE_OBJ, obj_fname);
1845 goto done;
1846 }
1847 fp = new CVmFile();
1848 fp->set_file(fpout, 0);
1849
1850 /* generate code and write the object file */
1851 node->build_object_file(fp, this);
1852
1853 /* add an extra blank line in the assembly listing file */
1854 if (G_disasm_out != 0)
1855 G_disasm_out->print("\n");
1856
1857 done: ;
1858 }
1859 err_finally
1860 {
1861 /* set the caller's error and warning counters */
1862 *error_count += G_tcmain->get_error_count();
1863 *warning_count += G_tcmain->get_warning_count();
1864
1865 /* close our output file */
1866 if (fp != 0)
1867 {
1868 delete fp;
1869 fp = 0;
1870 }
1871
1872 /* if we still have a symbol file open, close it */
1873 if (symfile != 0)
1874 {
1875 delete symfile;
1876 symfile = 0;
1877 }
1878
1879 /*
1880 * if any errors occurred, delete the object file in the
1881 * external file system - this prevents us from leaving around
1882 * an incomplete or corrupted object file when compilation
1883 * fails, and helps 'make'-type tools realize that they must
1884 * generate the object file target again on the next build, even
1885 * if source file didn't change
1886 */
1887 if (*error_count != 0 || (warnings_as_errors_ && *warning_count != 0))
1888 osfdel(obj_fname);
1889
1890 /* terminate the compiler */
1891 CTcMain::terminate();
1892 }
1893 err_end;
1894 }
1895
1896 /*
1897 * Read our build configuration information from a symbol file and compare
1898 * it to the current configuration. Returns true if the configurations are
1899 * identical, false if not.
1900 *
1901 * If we find any differences, we won't bother reading further in the file,
1902 * so the file's seek offset might be left in the middle of the
1903 * configuration data block on return.
1904 */
1905 int CTcMake::compare_build_config_from_sym_file(
1906 const char *sym_fname, CVmFile *fp)
1907 {
1908 CTcMakeDef *def;
1909 CTcMakePath *path;
1910 size_t cnt;
1911 char buf[OSFNMAX + 1];
1912 os_file_stat_t sym_stat;
1913
1914 /* check the compiler version */
1915 fp->read_bytes(buf, 5);
1916 if (buf[0] != TC_VSN_MAJOR
1917 || buf[1] != TC_VSN_MINOR
1918 || buf[2] != TC_VSN_REV
1919 || buf[3] != TC_VSN_PATCH
1920 || buf[4] != TC_VSN_DEVBUILD)
1921 {
1922 /*
1923 * the compiler has been updated since this file was compiled;
1924 * force a recompilation in case anything has changed in the
1925 * compiler
1926 */
1927 return FALSE;
1928 }
1929
1930 /*
1931 * read the debug mode - if the debug mode has changed, the
1932 * configuration has changed
1933 */
1934 fp->read_bytes(buf, 1);
1935 if ((buf[0] != 0) != (debug_ != 0))
1936 return FALSE;
1937
1938 /* read the -U/-D options */
1939 for (def = def_head_, cnt = fp->read_int2() ; cnt != 0 ;
1940 --cnt, def = def->get_next())
1941 {
1942 /*
1943 * if we're out of symbols in our list, we seem to have left out
1944 * some symbols this time, so the configuration has changed
1945 */
1946 if (def == 0)
1947 return FALSE;
1948
1949 /* read and compare the symbol */
1950 if (!read_and_compare_config_str(fp, def->get_sym()))
1951 return FALSE;
1952
1953 /* read and compare the expansion */
1954 if (!read_and_compare_config_str(fp, def->get_expan()))
1955 return FALSE;
1956
1957 /* read and compare the define/undefine flag */
1958 fp->read_bytes(buf, 1);
1959 if ((buf[0] != 0) != (def->is_def() != 0))
1960 return FALSE;
1961 }
1962
1963 /* read the #include search path list */
1964 for (path = inc_head_, cnt = fp->read_int2() ; cnt != 0 ;
1965 --cnt, path = path->get_next())
1966 {
1967 /* if we're out of items in our list, the config has changed */
1968 if (path == 0)
1969 return FALSE;
1970
1971 /* compare this entry */
1972 if (!read_and_compare_config_str(fp, path->get_path()))
1973 return FALSE;
1974 }
1975
1976 /*
1977 * note the timestamp of the symbol file - if that fails, return a
1978 * mismatch, because we won't be able to compare the include file
1979 * timestamps to the symbol file timestamp
1980 */
1981 if (!os_file_stat(sym_fname, TRUE, &sym_stat))
1982 return FALSE;
1983
1984 /* read the #include file list */
1985 for (cnt = fp->read_int2() ; cnt != 0 ; --cnt)
1986 {
1987 size_t len;
1988 os_file_stat_t inc_stat;
1989
1990 /*
1991 * read the next name's length - if it's longer than our buffer,
1992 * ignore it and return a mismatch
1993 */
1994 len = fp->read_int2();
1995 if (len > sizeof(buf) - 1)
1996 return FALSE;
1997
1998 /* read the name */
1999 fp->read_bytes(buf, len);
2000 buf[len] = '\0';
2001
2002 /*
2003 * get this file's timestamp - if that fails, the original
2004 * include file doesn't even seem to be accessible to us any
2005 * more, so we have to try a recompile
2006 */
2007 if (!os_file_stat(buf, TRUE, &inc_stat))
2008 return FALSE;
2009
2010 /*
2011 * if the include file has been modified more recently than the
2012 * symbol file, we must recompile the source
2013 */
2014 if (inc_stat.mod_time > sym_stat.mod_time)
2015 return FALSE;
2016 }
2017
2018 /*
2019 * we didn't find any differences in the configuration - indicate
2020 * that the configuration is matches
2021 */
2022 return TRUE;
2023 }
2024
2025 /*
2026 * Read and compare a single configuration string
2027 */
2028 int CTcMake::read_and_compare_config_str(CVmFile *fp, const textchar_t *str)
2029 {
2030 size_t len;
2031
2032 /* read the length */
2033 len = (size_t)fp->read_int2();
2034
2035 /* if the source string is null, the length must be zero to match */
2036 if (str == 0 && len != 0)
2037 return FALSE;
2038
2039 /* if the lengths don't match, the strings can't match */
2040 if (len != get_strlen(str))
2041 return FALSE;
2042
2043 /* keep going until we exhaust the string */
2044 while (len != 0)
2045 {
2046 char buf[256];
2047 size_t cur;
2048
2049 /* read up to a buffer-full, or whatever remains if less */
2050 cur = sizeof(buf);
2051 if (len < cur)
2052 cur = len;
2053
2054 /* read the data */
2055 fp->read_bytes(buf, cur);
2056
2057 /* compare these bytes - if they differ, we don't have a match */
2058 if (memcmp(str, buf, len) != 0)
2059 return FALSE;
2060
2061 /* skip past these bytes of the comparison string */
2062 str += len;
2063
2064 /* deduct this amount from the remaining total length */
2065 len -= cur;
2066 }
2067
2068 /* we didn't find any differences - they match */
2069 return TRUE;
2070 }
2071
2072 /*
2073 * Write our build configuration information to a symbol file
2074 */
2075 void CTcMake::write_build_config_to_sym_file(CVmFile *fp)
2076 {
2077 CTcMakeDef *def;
2078 CTcMakePath *inc;
2079 CTcTokFileDesc *desc;
2080 size_t cnt;
2081 char buf[32];
2082
2083 /* write the compiler version */
2084 buf[0] = TC_VSN_MAJOR;
2085 buf[1] = TC_VSN_MINOR;
2086 buf[2] = TC_VSN_REV;
2087 buf[3] = TC_VSN_PATCH;
2088 buf[4] = TC_VSN_DEVBUILD;
2089 fp->write_bytes(buf, 5);
2090
2091 /* write the debug mode */
2092 buf[0] = (char)debug_;
2093 fp->write_bytes(buf, 1);
2094
2095 /*
2096 * Write our list of pre-defined and pre-undefined symbols (-D and
2097 * -U options). If any changes are made to the -D/-U list, we'll
2098 * have to rebuild, since these options can change the meaning of
2099 * the source code.
2100 */
2101
2102 /* count the symbols in our list */
2103 for (cnt = 0, def = def_head_ ; def != 0 ;
2104 def = def->get_next(), ++cnt) ;
2105
2106 /* write the count */
2107 fp->write_uint2(cnt);
2108
2109 /* write each option */
2110 for (def = def_head_ ; def != 0 ; def = def->get_next())
2111 {
2112 /* write the symbol */
2113 fp->write_uint2(get_strlen(def->get_sym()));
2114 fp->write_bytes(def->get_sym(), get_strlen(def->get_sym()));
2115
2116 /* write the expansion */
2117 if (def->get_expan() != 0)
2118 {
2119 fp->write_uint2(get_strlen(def->get_expan()));
2120 fp->write_bytes(def->get_expan(), get_strlen(def->get_expan()));
2121 }
2122 else
2123 fp->write_uint2(0);
2124
2125 /* write the define/undefine flag */
2126 buf[0] = (char)def->is_def();
2127 fp->write_bytes(buf, 1);
2128 }
2129
2130 /*
2131 * Write the #include search list. If the search list changes, the
2132 * location in which we find a particular header file could change,
2133 * hence the text inserted by a #include directive could change,
2134 * hence we'd have to rebuild.
2135 */
2136
2137 /* count the list */
2138 for (cnt = 0, inc = inc_head_ ; inc != 0 ;
2139 inc = inc->get_next(), ++cnt) ;
2140
2141 /* write the count */
2142 fp->write_uint2(cnt);
2143
2144 /* write the list */
2145 for (inc = inc_head_ ; inc != 0 ; inc = inc->get_next())
2146 {
2147 /* write the entry */
2148 fp->write_uint2(get_strlen(inc->get_path()));
2149 fp->write_bytes(inc->get_path(), get_strlen(inc->get_path()));
2150 }
2151
2152 /*
2153 * Write the actual list of #include files that were included in the
2154 * program. We'll have to check each of these to see if any of them
2155 * have been modified more recently than the symbol file.
2156 */
2157
2158 /* get the count */
2159 cnt = G_tok->get_filedesc_count();
2160
2161 /*
2162 * start with the second descriptor, because the first is the source
2163 * file itself, which isn't part of the include list
2164 */
2165 desc = G_tok->get_first_filedesc();
2166 if (desc != 0)
2167 {
2168 /* skip the first descriptor */
2169 desc = desc->get_next();
2170
2171 /* we're not writing it, so don't count it */
2172 --cnt;
2173 }
2174
2175 /* write the count, excluding the actual source file */
2176 fp->write_uint2(cnt);
2177
2178 /* write the descriptors */
2179 for ( ; desc != 0 ; desc = desc->get_next())
2180 {
2181 /*
2182 * Write the filename string. Store the fully resolved local
2183 * filename, not the original unresolved name, because we want to
2184 * be able to detect a change in the configuration that points us
2185 * to a different resolved local file.
2186 */
2187 fp->write_uint2(get_strlen(desc->get_fname()));
2188 fp->write_bytes(desc->get_fname(), get_strlen(desc->get_fname()));
2189 }
2190 }
2191
2192 /*
2193 * build an image file
2194 */
2195 void CTcMake::build_image_file(CTcHostIfc *hostifc,
2196 CResLoader *res_loader,
2197 const char *image_fname,
2198 int *error_count, int *warning_count,
2199 CVmRuntimeSymbols *runtime_symtab,
2200 CVmRuntimeSymbols *runtime_macros,
2201 const char tool_data[4])
2202 {
2203 osfildef *fpout;
2204 CVmFile *volatile fp = 0;
2205 CVmFile *volatile objfile = 0;
2206
2207 err_try
2208 {
2209 CTcMakeModule *mod;
2210
2211 /* initialize the compiler */
2212 CTcMain::init(hostifc, res_loader, source_charset_);
2213
2214 /* set options */
2215 set_compiler_options();
2216
2217 /* load each object module */
2218 for (mod = mod_head_ ; mod != 0 ; mod = mod->get_next())
2219 {
2220 osfildef *objfp;
2221 char obj_fname[OSFNMAX];
2222
2223 /* if this module is excluded, skip it */
2224 if (mod->is_excluded())
2225 continue;
2226
2227 /* derive the object filename */
2228 get_objfile(obj_fname, mod);
2229
2230 /* open the file */
2231 objfp = osfoprb(obj_fname, OSFTT3OBJ);
2232 if (objfp == 0)
2233 {
2234 /* log the error */
2235 G_tcmain->log_error(0, 0, TC_SEV_ERROR,
2236 TCERR_MAKE_CANNOT_OPEN_OBJ, obj_fname);
2237 }
2238 else
2239 {
2240 const char *report_fname;
2241
2242 /*
2243 * if we're in test reporting mode, use the root name only
2244 * in error reports
2245 */
2246 if (test_report_mode_)
2247 report_fname = os_get_root_name((char *)obj_fname);
2248 else
2249 report_fname = obj_fname;
2250
2251 /* set up the file object */
2252 objfile = new CVmFile();
2253 objfile->set_file(objfp, 0);
2254
2255 /* load it */
2256 G_cg->load_object_file(objfile, report_fname);
2257
2258 /* done with the file - close it */
2259 delete objfile;
2260 objfile = 0;
2261 }
2262 }
2263
2264 /* if we encountered errors loading object files, give up */
2265 if (G_tcmain->get_error_count() != 0)
2266 goto done;
2267
2268 /* check for unresolved externals */
2269 if (G_prs->check_unresolved_externs())
2270 goto done;
2271
2272 /* set up the output file */
2273 fpout = osfopwb(image_fname, OSFTT3IMG);
2274 if (fpout == 0)
2275 {
2276 G_tcmain->log_error(0, 0, TC_SEV_ERROR,
2277 TCERR_MAKE_CANNOT_CREATE_IMG, image_fname);
2278 goto done;
2279 }
2280 fp = new CVmFile();
2281 fp->set_file(fpout, 0);
2282
2283 /* write the image file */
2284 G_cg->write_to_image(fp, data_xor_mask_, tool_data);
2285
2286 done: ;
2287 }
2288 err_finally
2289 {
2290 /* set the caller's error and warning counters */
2291 *error_count += G_tcmain->get_error_count();
2292 *warning_count += G_tcmain->get_warning_count();
2293
2294 /*
2295 * If the build succeeded, build the global symbol table to pass
2296 * to the pre-initializer, if the caller wants us to. (We must
2297 * build a table separate from the compiler global symbols, since
2298 * terminating the compiler will delete the compiler global symbol
2299 * table, and because we need the symbols in a different format
2300 * for the interpreter anyway.)
2301 *
2302 * There's no need to build this table if we got any errors during
2303 * the build, since we're not producing a valid image file in this
2304 * case anyway.
2305 */
2306 if (*error_count == 0)
2307 {
2308 /* build the global symbol table if desired */
2309 if (runtime_symtab != 0)
2310 {
2311 /* enumerate the global symbols into our builder callback */
2312 G_prs->get_global_symtab()
2313 ->enum_entries(&build_runtime_symtab_cb, runtime_symtab);
2314 }
2315
2316 /* build the macro table if desired */
2317 if (runtime_macros != 0)
2318 {
2319 /* enumerate the macros into our builder callback */
2320 G_tok->get_defines_table()
2321 ->enum_entries(&build_runtime_macro_cb, runtime_macros);
2322 }
2323 }
2324
2325 /* close our output file */
2326 if (fp != 0)
2327 {
2328 delete fp;
2329 fp = 0;
2330 }
2331
2332 /* if we still have an object file open, close it */
2333 if (objfile != 0)
2334 {
2335 delete objfile;
2336 objfile = 0;
2337 }
2338
2339 /*
2340 * if any errors occurred, delete the image file in the external
2341 * file system - this prevents us from leaving around an
2342 * incomplete or corrupted output file when compilation fails,
2343 * and helps 'make'-type tools realize that they must generate
2344 * the image file target again on the next build, even if source
2345 * files didn't change
2346 */
2347 if (*error_count != 0 || (warnings_as_errors_ && *warning_count != 0))
2348 osfdel(image_fname);
2349
2350 /* terminate the compiler */
2351 CTcMain::terminate();
2352 }
2353 err_end;
2354 }
2355
2356 /*
2357 * Enumeration callback for building the runtime symbol table from the
2358 * compiler global symbol table
2359 */
2360 void CTcMake::build_runtime_symtab_cb(void *ctx, class CTcSymbol *sym)
2361 {
2362 /* get our runtime symbol table object (it's the context) */
2363 CVmRuntimeSymbols *symtab = (CVmRuntimeSymbols *)ctx;
2364
2365 /* build the value for this symbol according to its type */
2366 sym->add_runtime_symbol(symtab);
2367 }
2368
2369 /*
2370 * Enumeration callback for building the runtime macro table from the
2371 * compiler macro table
2372 */
2373 void CTcMake::build_runtime_macro_cb(void *ctx, class CVmHashEntry *e)
2374 {
2375 int i;
2376 size_t arglen;
2377
2378 /* cast the context and hash entry */
2379 CVmRuntimeSymbols *symtab = (CVmRuntimeSymbols *)ctx;
2380 CTcHashEntryPp *macro = (CTcHashEntryPp *)e;
2381
2382 /* figure the flags - these are the same as the image file MACR flags */
2383 uint flags = 0;
2384 if (macro->has_args())
2385 flags |= 0x0001;
2386 if (macro->has_varargs())
2387 flags |= 0x0002;
2388
2389 /* compute the total string length of the argument names */
2390 for (i = 0, arglen = 0 ; i < macro->get_argc() ; ++i)
2391 arglen += strlen(macro->get_arg_name(i)) + 1;
2392
2393 /* add the macro entry to the table */
2394 vm_runtime_sym *sym = symtab->add_macro(
2395 macro->getstr(), macro->getlen(),
2396 macro->get_orig_expan_len(), flags,
2397 macro->get_argc(), arglen);
2398
2399 /* store the expansion */
2400 memcpy(sym->macro_expansion, macro->get_orig_expansion(),
2401 macro->get_orig_expan_len());
2402
2403 /* store the argument names */
2404 for (i = 0 ; i < macro->get_argc() ; ++i)
2405 {
2406 /* copy the argument name */
2407 const char *p = macro->get_arg_name(i);
2408 size_t len = strlen(p) + 1;
2409 memcpy(sym->macro_args[i], p, len);
2410
2411 /* commit the space */
2412 sym->commit_macro_arg(i, len);
2413 }
2414 }
2415
2416 /*
2417 * Set the compiler options
2418 */
2419 void CTcMake::set_compiler_options()
2420 {
2421 CTcMakePath *inc;
2422 CTcMakeDef *def;
2423 char buf[256];
2424
2425 /* set the error verbosity and warning level modes */
2426 G_tcmain->set_verbosity(verbose_);
2427 G_tcmain->set_show_err_numbers(show_err_numbers_);
2428 G_tcmain->set_pedantic(pedantic_);
2429 G_tcmain->set_suppress_list(suppress_list_, suppress_cnt_);
2430 G_tcmain->set_warnings(show_warnings_);
2431 G_tcmain->set_test_report_mode(test_report_mode_);
2432 G_tcmain->set_quote_filenames(quoted_fname_mode_);
2433
2434 /* set debug mode */
2435 G_debug = debug_;
2436
2437 /* add the #include path entries */
2438 for (inc = inc_head_ ; inc != 0 ; inc = inc->get_next())
2439 G_tok->add_inc_path(inc->get_path());
2440
2441 /* if we're in debug mode, add the __DEBUG pre-defined macro */
2442 if (debug_)
2443 G_tok->add_define("__DEBUG", "1");
2444
2445 /*
2446 * if we're in test report mode, generate __FILE__ expansions with
2447 * root names only
2448 */
2449 if (test_report_mode_)
2450 G_tok->set_test_report_mode(TRUE);
2451
2452 /* add the system-name pre-defined macro */
2453 sprintf(buf, "__TADS_SYS_%s", OS_SYSTEM_NAME);
2454 G_tok->add_define(buf, "1");
2455
2456 /* add the pre-defined macro expanding to the system name */
2457 sprintf(buf, "'%s'", OS_SYSTEM_NAME);
2458 G_tok->add_define("__TADS_SYSTEM_NAME", buf);
2459
2460 /* add the major and minor version number macros */
2461 sprintf(buf, "%d", TC_VSN_MAJOR);
2462 G_tok->add_define("__TADS_VERSION_MAJOR", buf);
2463 sprintf(buf, "%d", TC_VSN_MINOR);
2464 G_tok->add_define("__TADS_VERSION_MINOR", buf);
2465
2466 /* add the preprocess macro format version number */
2467 sprintf(buf, "%d", TCTOK_MACRO_FORMAT_VERSION);
2468 G_tok->add_define("__TADS_MACRO_FORMAT_VERSION", buf);
2469
2470 /* add a symbol for __TADS3 */
2471 G_tok->add_define("__TADS3", "1");
2472
2473 /* add the preprocessor symbol definitions */
2474 for (def = def_head_ ; def != 0 ; def = def->get_next())
2475 {
2476 /* define or undefine the symbol as appropriate */
2477 if (def->is_def())
2478 G_tok->add_define(def->get_sym(), def->get_expan());
2479 else
2480 G_tok->undefine(def->get_sym());
2481 }
2482 }
2483
2484 /*
2485 * Get the source filename for a module
2486 */
2487 void CTcMake::get_srcfile(textchar_t *dst, CTcMakeModule *mod)
2488 {
2489 CTcMakePath *path;
2490
2491 /*
2492 * if the file has an absolute path already, use the name without
2493 * further modification; otherwise, apply our default path
2494 */
2495 if (os_is_file_absolute(mod->get_search_source_name()))
2496 {
2497 /* it's absolute - ignore the search path */
2498 lib_strcpy(dst, OSFNMAX, mod->get_source_name());
2499 return;
2500 }
2501
2502 /*
2503 * If the file exists with the name as given (in other words, relative
2504 * to the current working directory), use the name as given.
2505 */
2506 if (!osfacc(mod->get_source_name()))
2507 {
2508 /* the file exists in the current directory - use the name as given */
2509 lib_strcpy(dst, OSFNMAX, mod->get_source_name());
2510 return;
2511 }
2512
2513 /* search the source path for the file */
2514 for (path = src_head_ ; path != 0 ; path = path->get_next())
2515 {
2516 /* build the full path for this source path */
2517 os_build_full_path(dst, OSFNMAX, path->get_path(),
2518 mod->get_search_source_name());
2519
2520 /* if the resulting file exists, return it */
2521 if (!osfacc(dst))
2522 return;
2523 }
2524
2525 /*
2526 * we failed to find the file anywhere - return the name as given,
2527 * which will let the caller fail when it can't open the file
2528 */
2529 lib_strcpy(dst, OSFNMAX, mod->get_source_name());
2530 }
2531
2532 /*
2533 * Get the symbol filename for a module
2534 */
2535 void CTcMake::get_symfile(textchar_t *dst, CTcMakeModule *mod)
2536 {
2537 /*
2538 * If a symbol path is specified, remove the path part of the module
2539 * name and add the root filename to the symbol path, since the symbol
2540 * path overrides any path specified for the source file itself.
2541 * Otherwise, use the name as given.
2542 */
2543 if (symdir_.is_set())
2544 os_build_full_path(dst, OSFNMAX, symdir_.get(),
2545 os_get_root_name((char *)mod->get_symbol_name()));
2546 else
2547 lib_strcpy(dst, OSFNMAX, mod->get_symbol_name());
2548 }
2549
2550 /*
2551 * Get the object filename for a module
2552 */
2553 void CTcMake::get_objfile(textchar_t *dst, CTcMakeModule *mod)
2554 {
2555 /*
2556 * If an object path is specified, remove the path part of the module
2557 * name and add the root filename to the object path, since the object
2558 * path overrides the source file path. Otherwise, use the name as
2559 * given.
2560 */
2561 if (objdir_.is_set())
2562 os_build_full_path(dst, OSFNMAX, objdir_.get(),
2563 os_get_root_name((char *)mod->get_object_name()));
2564 else
2565 lib_strcpy(dst, OSFNMAX, mod->get_object_name());
2566 }
2567
2568
2569 /* ------------------------------------------------------------------------ */
2570 /*
2571 * Module Object
2572 */
2573
2574 /*
2575 * Set the module name. Sets the source, symbol, and object filenames
2576 * if these are not already set.
2577 */
2578 void CTcMakeModule::set_module_name(const textchar_t *modname)
2579 {
2580 textchar_t buf[OSFNMAX];
2581
2582 /* set the source name, if it's not already set */
2583 if (!src_.is_set())
2584 {
2585 /* add a default extension of ".t" to make the source name */
2586 lib_strcpy(buf, sizeof(buf), modname);
2587 os_defext(buf, "t");
2588 src_.set(buf);
2589 }
2590
2591 /* set the symbol file name, if it's not already set */
2592 if (!sym_.is_set())
2593 {
2594 /* add a default extension of ".t3s" to make the symbol file name */
2595 lib_strcpy(buf, sizeof(buf), modname);
2596 os_remext(buf);
2597 os_addext(buf, "t3s");
2598 sym_.set(buf);
2599 }
2600
2601 /* set the object file name, if it's not already set */
2602 if (!obj_.is_set())
2603 {
2604 /* add a default extension of ".t3o" to make the object name */
2605 lib_strcpy(buf, sizeof(buf), modname);
2606 os_remext(buf);
2607 os_addext(buf, "t3o");
2608 obj_.set(buf);
2609 }
2610 }
2611