2001-02-24 06:58:54 +00:00
|
|
|
/*
|
|
|
|
qfcc.c
|
|
|
|
|
|
|
|
QuakeForge Code Compiler (main program)
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 id Software, Inc.
|
2001-10-26 06:43:56 +00:00
|
|
|
Copyright (C) 2001 Jeff Teunissen <deek@quakeforge.net>
|
|
|
|
Copyright (C) 2001 Bill Currie <bill@taniwha.org>
|
2001-02-24 06:58:54 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public
|
|
|
|
License along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2001-07-17 05:02:34 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
# include <sys/types.h>
|
|
|
|
#endif
|
2002-01-05 19:13:20 +00:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
# include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
# include <fcntl.h>
|
|
|
|
#endif
|
2001-07-17 05:02:34 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2002-06-01 04:41:25 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
2007-04-11 22:03:36 +00:00
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
2002-06-01 04:41:25 +00:00
|
|
|
#include <stdlib.h>
|
2001-10-26 22:07:35 +00:00
|
|
|
#include <stdio.h>
|
2002-09-10 15:46:50 +00:00
|
|
|
#include <errno.h>
|
2001-10-26 22:07:35 +00:00
|
|
|
|
2002-07-31 16:42:21 +00:00
|
|
|
#include <QF/cbuf.h>
|
2001-04-01 02:12:57 +00:00
|
|
|
#include <QF/crc.h>
|
2002-03-07 18:36:48 +00:00
|
|
|
#include <QF/dstring.h>
|
2001-04-01 06:40:51 +00:00
|
|
|
#include <QF/hash.h>
|
2001-04-01 02:12:57 +00:00
|
|
|
#include <QF/qendian.h>
|
2004-11-11 00:24:00 +00:00
|
|
|
#include <QF/script.h>
|
2001-04-01 03:33:11 +00:00
|
|
|
#include <QF/sys.h>
|
2002-06-01 05:06:19 +00:00
|
|
|
#include <QF/va.h>
|
2001-02-24 06:58:54 +00:00
|
|
|
|
|
|
|
#include "qfcc.h"
|
2002-05-21 21:28:40 +00:00
|
|
|
#include "class.h"
|
2011-01-09 10:41:24 +00:00
|
|
|
#include "codespace.h"
|
2002-06-04 18:44:03 +00:00
|
|
|
#include "cpp.h"
|
2002-06-04 21:23:39 +00:00
|
|
|
#include "debug.h"
|
2002-06-04 18:44:03 +00:00
|
|
|
#include "def.h"
|
2011-01-17 13:33:33 +00:00
|
|
|
#include "defspace.h"
|
2011-01-27 06:39:54 +00:00
|
|
|
#include "diagnostic.h"
|
2002-07-08 20:31:59 +00:00
|
|
|
#include "emit.h"
|
2002-06-04 18:44:03 +00:00
|
|
|
#include "expr.h"
|
|
|
|
#include "function.h"
|
|
|
|
#include "idstuff.h"
|
2002-07-03 20:16:32 +00:00
|
|
|
#include "linker.h"
|
2002-06-28 16:00:01 +00:00
|
|
|
#include "method.h"
|
2002-06-21 20:46:56 +00:00
|
|
|
#include "obj_file.h"
|
2002-06-04 21:23:39 +00:00
|
|
|
#include "opcodes.h"
|
2002-06-04 18:44:03 +00:00
|
|
|
#include "options.h"
|
2002-06-07 17:29:30 +00:00
|
|
|
#include "reloc.h"
|
2002-07-05 20:02:10 +00:00
|
|
|
#include "strpool.h"
|
2002-06-28 16:00:01 +00:00
|
|
|
#include "struct.h"
|
2011-01-19 13:19:26 +00:00
|
|
|
#include "symtab.h"
|
2002-05-09 17:11:14 +00:00
|
|
|
#include "type.h"
|
2011-03-22 03:24:39 +00:00
|
|
|
#include "value.h"
|
2001-02-24 06:58:54 +00:00
|
|
|
|
2001-12-06 19:49:40 +00:00
|
|
|
options_t options;
|
2001-10-26 08:14:05 +00:00
|
|
|
|
2002-06-04 05:25:37 +00:00
|
|
|
const char *sourcedir;
|
|
|
|
const char *progs_src;
|
2001-03-28 17:17:56 +00:00
|
|
|
|
2001-12-06 19:49:40 +00:00
|
|
|
pr_info_t pr;
|
2001-10-26 17:49:23 +00:00
|
|
|
|
2003-02-24 21:51:08 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
char *
|
|
|
|
fix_backslash (char *path)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
for (s = path; *s; s++)
|
|
|
|
if (*s == '\\')
|
|
|
|
*s = '/';
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-02-24 06:58:54 +00:00
|
|
|
InitData (void)
|
|
|
|
{
|
2011-03-05 06:40:08 +00:00
|
|
|
pr_lineno_t *line;
|
|
|
|
|
2002-07-08 20:31:59 +00:00
|
|
|
if (pr.code) {
|
|
|
|
codespace_delete (pr.code);
|
2002-07-05 20:02:10 +00:00
|
|
|
strpool_delete (pr.strings);
|
2002-06-28 16:00:01 +00:00
|
|
|
}
|
2002-07-08 20:31:59 +00:00
|
|
|
memset (&pr, 0, sizeof (pr));
|
2002-07-03 21:32:03 +00:00
|
|
|
pr.source_line = 1;
|
|
|
|
pr.error_count = 0;
|
2002-07-08 20:31:59 +00:00
|
|
|
pr.code = codespace_new ();
|
|
|
|
memset (codespace_newstatement (pr.code), 0, sizeof (dstatement_t));
|
2002-07-05 20:02:10 +00:00
|
|
|
pr.strings = strpool_new ();
|
2002-06-05 17:12:55 +00:00
|
|
|
pr.num_functions = 1;
|
|
|
|
|
2011-03-05 06:40:08 +00:00
|
|
|
pr.num_linenos = 0;
|
|
|
|
line = new_lineno ();
|
|
|
|
line->fa.func = -1;
|
|
|
|
line->line = -1;
|
|
|
|
|
2011-02-24 04:07:36 +00:00
|
|
|
pr.data = defspace_new ();
|
2011-02-09 12:45:29 +00:00
|
|
|
|
2011-02-24 04:07:36 +00:00
|
|
|
pr.far_data = defspace_new ();
|
2011-01-24 06:41:43 +00:00
|
|
|
|
2011-02-24 04:07:36 +00:00
|
|
|
pr.near_data = defspace_new ();
|
2002-06-10 20:54:22 +00:00
|
|
|
pr.near_data->data = calloc (65536, sizeof (pr_type_t));
|
2005-06-10 05:16:20 +00:00
|
|
|
pr.near_data->max_size = 65536;
|
|
|
|
pr.near_data->grow = 0;
|
2011-01-25 03:34:45 +00:00
|
|
|
|
2011-02-24 04:07:36 +00:00
|
|
|
pr.type_data = defspace_new ();
|
|
|
|
defspace_alloc_loc (pr.type_data, 4);// reserve space for a null descriptor
|
2011-02-22 04:36:16 +00:00
|
|
|
|
2011-01-25 03:34:45 +00:00
|
|
|
pr.symtab = new_symtab (0, stab_global);
|
|
|
|
pr.symtab->space = pr.near_data;
|
2002-06-05 17:12:55 +00:00
|
|
|
|
2011-02-24 04:07:36 +00:00
|
|
|
pr.entity_data = defspace_new ();
|
2011-03-06 06:38:19 +00:00
|
|
|
pr.entity_fields = new_symtab (0, stab_global);
|
|
|
|
pr.entity_fields->space = pr.entity_data;;
|
2001-02-24 06:58:54 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static int
|
2011-03-04 09:44:02 +00:00
|
|
|
WriteProgs (dprograms_t *progs, int size)
|
2001-02-24 06:58:54 +00:00
|
|
|
{
|
2011-03-02 11:51:00 +00:00
|
|
|
//pr_debug_header_t debug;
|
2002-09-10 15:36:32 +00:00
|
|
|
QFile *h;
|
2011-03-02 11:51:00 +00:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
dstatement_t *statements;
|
|
|
|
dfunction_t *functions;
|
|
|
|
ddef_t *globaldefs;
|
|
|
|
ddef_t *fielddefs;
|
|
|
|
pr_type_t *globals;
|
|
|
|
|
|
|
|
#define P(t,o) ((t *)((char *)progs + progs->o))
|
|
|
|
statements = P (dstatement_t, ofs_statements);
|
|
|
|
functions = P (dfunction_t, ofs_functions);
|
|
|
|
globaldefs = P (ddef_t, ofs_globaldefs);
|
|
|
|
fielddefs = P (ddef_t, ofs_fielddefs);
|
|
|
|
globals = P (pr_type_t, ofs_globals);
|
|
|
|
#undef P
|
|
|
|
|
|
|
|
for (i = 0; i < progs->numstatements; i++) {
|
|
|
|
statements[i].op = LittleShort (statements[i].op);
|
|
|
|
statements[i].a = LittleShort (statements[i].a);
|
|
|
|
statements[i].b = LittleShort (statements[i].b);
|
|
|
|
statements[i].c = LittleShort (statements[i].c);
|
|
|
|
}
|
|
|
|
for (i = 0; i < (unsigned) progs->numfunctions; i++) {
|
|
|
|
dfunction_t *func = functions + i;
|
|
|
|
func->first_statement = LittleLong (func->first_statement);
|
|
|
|
func->parm_start = LittleLong (func->parm_start);
|
|
|
|
func->locals = LittleLong (func->locals);
|
|
|
|
func->profile = LittleLong (func->profile);
|
|
|
|
func->s_name = LittleLong (func->s_name);
|
|
|
|
func->s_file = LittleLong (func->s_file);
|
|
|
|
func->numparms = LittleLong (func->numparms);
|
|
|
|
}
|
|
|
|
for (i = 0; i < progs->numglobaldefs; i++) {
|
|
|
|
globaldefs[i].type = LittleShort (globaldefs[i].type);
|
|
|
|
globaldefs[i].ofs = LittleShort (globaldefs[i].ofs);
|
|
|
|
globaldefs[i].s_name = LittleLong (globaldefs[i].s_name);
|
|
|
|
}
|
|
|
|
for (i = 0; i < progs->numfielddefs; i++) {
|
|
|
|
fielddefs[i].type = LittleShort (fielddefs[i].type);
|
|
|
|
fielddefs[i].ofs = LittleShort (fielddefs[i].ofs);
|
|
|
|
fielddefs[i].s_name = LittleLong (fielddefs[i].s_name);
|
|
|
|
}
|
|
|
|
for (i = 0; i < progs->numglobals; i++)
|
|
|
|
globals[i].integer_var = LittleLong (globals[i].integer_var);
|
2001-02-24 06:58:54 +00:00
|
|
|
|
2002-09-10 15:46:50 +00:00
|
|
|
if (!(h = Qopen (options.output_file, "wb")))
|
|
|
|
Sys_Error ("%s: %s\n", options.output_file, strerror(errno));
|
2011-03-02 11:51:00 +00:00
|
|
|
Qwrite (h, progs, size);
|
2001-10-31 17:55:35 +00:00
|
|
|
|
2002-09-10 15:36:32 +00:00
|
|
|
Qclose (h);
|
2001-07-14 01:15:40 +00:00
|
|
|
|
2011-03-04 09:44:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2001-07-18 20:31:55 +00:00
|
|
|
|
2011-03-04 09:44:02 +00:00
|
|
|
static int
|
|
|
|
WriteSym (pr_debug_header_t *sym, int size)
|
|
|
|
{
|
|
|
|
//pr_debug_header_t debug;
|
|
|
|
QFile *h;
|
|
|
|
unsigned i;
|
2001-12-06 19:49:40 +00:00
|
|
|
|
2011-03-04 09:44:02 +00:00
|
|
|
pr_auxfunction_t *auxfunctions;
|
|
|
|
pr_lineno_t *linenos;
|
|
|
|
ddef_t *locals;
|
|
|
|
|
|
|
|
#define P(t,o) ((t *)((char *)sym + sym->o))
|
|
|
|
auxfunctions = P (pr_auxfunction_t, auxfunctions);
|
|
|
|
linenos = P (pr_lineno_t, linenos);
|
|
|
|
locals = P (ddef_t, locals);
|
|
|
|
#undef P
|
|
|
|
|
|
|
|
for (i = 0; i < sym->num_auxfunctions; i++) {
|
|
|
|
pr_auxfunction_t *af = auxfunctions++;
|
|
|
|
af->function = LittleLong (af->function);
|
|
|
|
af->source_line = LittleLong (af->source_line);
|
|
|
|
af->line_info = LittleLong (af->line_info);
|
|
|
|
af->local_defs = LittleLong (af->local_defs);
|
|
|
|
af->num_locals = LittleLong (af->num_locals);
|
|
|
|
af->return_type = LittleShort (af->return_type);
|
2001-07-14 01:15:40 +00:00
|
|
|
}
|
2011-03-04 09:44:02 +00:00
|
|
|
for (i = 0; i < sym->num_linenos; i++) {
|
|
|
|
pr_lineno_t *ln = linenos++;
|
|
|
|
ln->fa.addr = LittleLong (ln->fa.addr);
|
|
|
|
ln->line = LittleLong (ln->line);
|
2001-07-14 01:15:40 +00:00
|
|
|
}
|
2011-03-04 09:44:02 +00:00
|
|
|
for (i = 0; i < sym->num_locals; i++) {
|
|
|
|
locals[i].type = LittleShort (locals[i].type);
|
|
|
|
locals[i].ofs = LittleShort (locals[i].ofs);
|
|
|
|
locals[i].s_name = LittleLong (locals[i].s_name);
|
2001-07-14 01:15:40 +00:00
|
|
|
}
|
|
|
|
|
2011-03-09 02:09:46 +00:00
|
|
|
if (!(h = Qopen (options.debug_file, "wb")))
|
|
|
|
Sys_Error ("%s: %s\n", options.debug_file, strerror(errno));
|
2011-03-04 09:44:02 +00:00
|
|
|
Qwrite (h, sym, size);
|
|
|
|
|
2002-09-10 15:36:32 +00:00
|
|
|
Qclose (h);
|
2011-03-04 09:44:02 +00:00
|
|
|
|
2002-06-25 21:36:10 +00:00
|
|
|
return 0;
|
2001-02-24 06:58:54 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-06-07 17:46:58 +00:00
|
|
|
begin_compilation (void)
|
2001-02-24 06:58:54 +00:00
|
|
|
{
|
2002-06-06 21:51:47 +00:00
|
|
|
pr.func_tail = &pr.func_head;
|
2001-02-24 06:58:54 +00:00
|
|
|
|
2002-07-03 21:32:03 +00:00
|
|
|
pr.error_count = 0;
|
2001-02-24 06:58:54 +00:00
|
|
|
}
|
|
|
|
|
2002-04-10 19:52:48 +00:00
|
|
|
const char *
|
|
|
|
strip_path (const char *filename)
|
|
|
|
{
|
|
|
|
const char *p = filename;
|
|
|
|
int i = options.strip_path;
|
|
|
|
|
|
|
|
while (i-- > 0) {
|
|
|
|
while (*p && *p != '/')
|
|
|
|
p++;
|
|
|
|
if (!*p)
|
|
|
|
break;
|
|
|
|
filename = ++p;
|
|
|
|
}
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2002-06-25 21:36:10 +00:00
|
|
|
static void
|
|
|
|
setup_sym_file (const char *output_file)
|
2001-02-24 06:58:54 +00:00
|
|
|
{
|
2002-06-25 21:36:10 +00:00
|
|
|
if (options.code.debug) {
|
2011-03-09 02:09:46 +00:00
|
|
|
dstring_t *str = dstring_strdup (output_file);
|
|
|
|
const char *s;
|
|
|
|
int offset;
|
2001-02-24 06:58:54 +00:00
|
|
|
|
2011-03-09 02:09:46 +00:00
|
|
|
s = str->str + strlen (str->str);
|
|
|
|
while (s-- > str->str) {
|
2002-06-25 21:36:10 +00:00
|
|
|
if (*s == '.')
|
|
|
|
break;
|
|
|
|
if (*s == '/' || *s == '\\') {
|
2011-03-09 02:09:46 +00:00
|
|
|
s = str->str + strlen (str->str);
|
2002-06-25 21:36:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-03-09 02:09:46 +00:00
|
|
|
offset = s - str->str;
|
|
|
|
str->size = offset + 5;
|
|
|
|
dstring_adjust (str);
|
|
|
|
strcpy (str->str + offset, ".sym");
|
|
|
|
options.debug_file = save_string (str->str);
|
2002-06-25 21:36:10 +00:00
|
|
|
if (options.verbosity >= 1)
|
2011-03-09 02:09:46 +00:00
|
|
|
printf ("debug file: %s\n", options.debug_file);
|
2002-06-25 21:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
2001-02-24 06:58:54 +00:00
|
|
|
|
2002-06-28 16:00:01 +00:00
|
|
|
static int
|
|
|
|
compile_to_obj (const char *file, const char *obj)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2006-05-24 14:50:24 +00:00
|
|
|
yyin = preprocess_file (file, 0);
|
2002-08-13 22:02:07 +00:00
|
|
|
if (!yyin)
|
|
|
|
return !options.preprocess_only;
|
2002-06-28 16:00:01 +00:00
|
|
|
|
|
|
|
InitData ();
|
|
|
|
clear_frame_macros ();
|
|
|
|
clear_classes ();
|
|
|
|
clear_immediates ();
|
|
|
|
clear_selectors ();
|
2002-07-16 19:03:56 +00:00
|
|
|
chain_initial_types ();
|
|
|
|
begin_compilation ();
|
2002-07-03 21:32:03 +00:00
|
|
|
pr.source_file = ReuseString (strip_path (file));
|
|
|
|
err = yyparse () || pr.error_count;
|
2002-06-28 16:00:01 +00:00
|
|
|
fclose (yyin);
|
2002-08-13 22:02:07 +00:00
|
|
|
if (cpp_name && !options.save_temps) {
|
2002-06-28 16:00:01 +00:00
|
|
|
if (unlink (tempname->str)) {
|
|
|
|
perror ("unlink");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
2002-07-11 20:17:26 +00:00
|
|
|
if (!err) {
|
2002-07-13 06:09:03 +00:00
|
|
|
qfo_t *qfo;
|
|
|
|
|
|
|
|
class_finish_module ();
|
|
|
|
qfo = qfo_from_progs (&pr);
|
2002-07-11 20:17:26 +00:00
|
|
|
err = qfo_write (qfo, obj);
|
|
|
|
qfo_delete (qfo);
|
|
|
|
}
|
2002-06-28 16:00:01 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-03-02 11:51:00 +00:00
|
|
|
static int
|
|
|
|
finish_link (void)
|
|
|
|
{
|
|
|
|
qfo_t *qfo;
|
|
|
|
int flags;
|
|
|
|
|
2011-03-06 04:40:24 +00:00
|
|
|
if (!options.output_file)
|
|
|
|
options.output_file = "progs.dat";
|
|
|
|
|
2011-03-02 11:51:00 +00:00
|
|
|
flags = (QFOD_GLOBAL | QFOD_CONSTANT | QFOD_INITIALIZED | QFOD_NOSAVE);
|
|
|
|
if (options.code.progsversion != PROG_ID_VERSION) {
|
|
|
|
linker_add_def (".param_size", &type_integer, flags,
|
|
|
|
type_size (&type_param));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.code.debug) {
|
2011-03-04 09:44:02 +00:00
|
|
|
int str;
|
|
|
|
|
|
|
|
setup_sym_file (options.output_file);
|
2011-03-09 02:09:46 +00:00
|
|
|
str = linker_add_string (options.debug_file);
|
2011-03-02 11:51:00 +00:00
|
|
|
linker_add_def (".debug_file", &type_string, flags, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
qfo = linker_finish ();
|
|
|
|
if (!qfo)
|
|
|
|
return 1;
|
|
|
|
if (options.partial_link) {
|
|
|
|
qfo_write (qfo, options.output_file);
|
|
|
|
} else {
|
|
|
|
int size;
|
|
|
|
dprograms_t *progs;
|
|
|
|
|
|
|
|
progs = qfo_to_progs (qfo, &size);
|
|
|
|
//finish_compilation ();
|
|
|
|
|
|
|
|
// write progdefs.h
|
2012-05-02 15:22:24 +00:00
|
|
|
if (options.code.crc || options.progdefs_h) {
|
|
|
|
const char *progdefs_h = "progdefs.h";
|
|
|
|
if (!options.progdefs_h)
|
|
|
|
progdefs_h = 0;
|
|
|
|
progs->crc = WriteProgdefs (progs, progdefs_h);
|
|
|
|
}
|
2011-03-02 11:51:00 +00:00
|
|
|
|
2011-03-04 09:44:02 +00:00
|
|
|
WriteProgs (progs, size);
|
|
|
|
if (options.code.debug) {
|
|
|
|
pr_debug_header_t *sym;
|
|
|
|
int sym_size = 0;
|
|
|
|
sym = qfo_to_sym (qfo, &sym_size);
|
|
|
|
sym->crc = CRC_Block ((byte *) progs, size);
|
|
|
|
WriteSym (sym, sym_size);
|
|
|
|
}
|
2011-03-02 11:51:00 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-25 21:36:10 +00:00
|
|
|
static int
|
|
|
|
separate_compile (void)
|
|
|
|
{
|
|
|
|
const char **file;
|
2002-09-11 18:17:45 +00:00
|
|
|
const char **temp_files;
|
2002-06-25 21:36:10 +00:00
|
|
|
dstring_t *output_file = dstring_newstr ();
|
|
|
|
dstring_t *extension = dstring_newstr ();
|
|
|
|
char *f;
|
2002-06-28 16:38:05 +00:00
|
|
|
int err = 0;
|
2002-09-11 18:17:45 +00:00
|
|
|
int i;
|
2002-06-25 21:36:10 +00:00
|
|
|
|
|
|
|
if (options.compile && options.output_file && source_files[1]) {
|
2002-09-11 18:17:45 +00:00
|
|
|
fprintf (stderr,
|
|
|
|
"%s: cannot use -c and -o together with multiple files\n",
|
|
|
|
this_program);
|
2002-06-25 21:36:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2001-08-04 09:11:50 +00:00
|
|
|
|
2002-09-11 18:17:45 +00:00
|
|
|
for (file = source_files, i = 0; *file; file++)
|
|
|
|
i++;
|
|
|
|
temp_files = calloc (i + 1, sizeof (const char*));
|
|
|
|
|
|
|
|
for (file = source_files, i = 0; *file; file++) {
|
2002-06-25 21:36:10 +00:00
|
|
|
dstring_clearstr (extension);
|
|
|
|
dstring_clearstr (output_file);
|
|
|
|
|
2002-07-17 17:43:51 +00:00
|
|
|
dstring_appendstr (output_file, *file);
|
2002-06-25 21:36:10 +00:00
|
|
|
f = output_file->str + strlen (output_file->str);
|
|
|
|
while (f >= output_file->str && *f != '.' && *f != '/')
|
|
|
|
f--;
|
2003-05-15 05:10:28 +00:00
|
|
|
if (f >= output_file->str && *f == '.') {
|
2002-06-25 21:36:10 +00:00
|
|
|
output_file->size -= strlen (f);
|
|
|
|
dstring_appendstr (extension, f);
|
|
|
|
*f = 0;
|
|
|
|
}
|
2002-07-17 17:43:51 +00:00
|
|
|
if (options.compile && options.output_file) {
|
|
|
|
dstring_clearstr (output_file);
|
|
|
|
dstring_appendstr (output_file, options.output_file);
|
|
|
|
} else {
|
|
|
|
dstring_appendstr (output_file, ".qfo");
|
|
|
|
}
|
2002-06-25 21:36:10 +00:00
|
|
|
if (strncmp (*file, "-l", 2)
|
|
|
|
&& (!strcmp (extension->str, ".r")
|
2004-11-14 23:15:00 +00:00
|
|
|
|| !strcmp (extension->str, ".qc")
|
|
|
|
|| !strcmp (extension->str, ".c"))) {
|
2002-07-17 19:00:16 +00:00
|
|
|
if (options.verbosity >= 2)
|
|
|
|
printf ("%s %s\n", *file, output_file->str);
|
2002-09-11 18:17:45 +00:00
|
|
|
temp_files[i++] = save_string (output_file->str);
|
2002-06-28 16:38:05 +00:00
|
|
|
err = compile_to_obj (*file, output_file->str) || err;
|
2002-06-28 16:00:01 +00:00
|
|
|
|
2002-06-25 21:36:10 +00:00
|
|
|
free ((char *)*file);
|
|
|
|
*file = strdup (output_file->str);
|
|
|
|
} else {
|
|
|
|
if (options.compile)
|
|
|
|
fprintf (stderr, "%s: %s: ignoring object file since linking "
|
|
|
|
"not done\n", this_program, *file);
|
|
|
|
}
|
|
|
|
}
|
2002-06-28 16:38:05 +00:00
|
|
|
if (!err && !options.compile) {
|
2011-03-04 23:35:56 +00:00
|
|
|
chain_initial_types ();
|
2002-07-03 20:16:32 +00:00
|
|
|
linker_begin ();
|
2002-06-25 21:36:10 +00:00
|
|
|
for (file = source_files; *file; file++) {
|
2002-11-01 18:05:12 +00:00
|
|
|
if (strncmp (*file, "-l", 2)) {
|
|
|
|
if (strlen (*file) >= 2
|
|
|
|
&& strcmp (*file + strlen (*file) - 2, ".a") == 0) {
|
|
|
|
err = linker_add_lib (*file);
|
|
|
|
} else {
|
|
|
|
err = linker_add_object_file (*file);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = linker_add_lib (*file);
|
|
|
|
}
|
2002-08-15 18:53:03 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
2002-06-25 21:36:10 +00:00
|
|
|
}
|
2011-03-02 11:51:00 +00:00
|
|
|
err = finish_link ();
|
2002-09-11 18:17:45 +00:00
|
|
|
if (!options.save_temps)
|
|
|
|
for (file = temp_files; *file; file++)
|
|
|
|
unlink (*file);
|
2002-06-25 21:36:10 +00:00
|
|
|
}
|
2002-06-28 16:38:05 +00:00
|
|
|
return err;
|
2002-06-25 21:36:10 +00:00
|
|
|
}
|
|
|
|
|
2002-10-30 02:31:29 +00:00
|
|
|
static const char *
|
|
|
|
load_file (const char *fname)
|
|
|
|
{
|
|
|
|
QFile *file;
|
|
|
|
char *src;
|
2007-04-06 09:44:08 +00:00
|
|
|
FILE *tmpfile;
|
2002-10-30 02:31:29 +00:00
|
|
|
|
2007-04-06 09:44:08 +00:00
|
|
|
tmpfile = preprocess_file (fname, "i1");
|
2007-04-06 14:13:25 +00:00
|
|
|
if (!tmpfile)
|
2002-10-30 02:31:29 +00:00
|
|
|
return 0;
|
2007-04-07 10:24:34 +00:00
|
|
|
file = Qfopen (tmpfile, "rb");
|
2007-04-06 09:44:08 +00:00
|
|
|
if (!file) {
|
|
|
|
perror (fname);
|
|
|
|
return 0;
|
|
|
|
}
|
2002-10-30 02:31:29 +00:00
|
|
|
src = malloc (Qfilesize (file) + 1);
|
|
|
|
src[Qfilesize (file)] = 0;
|
|
|
|
Qread (file, src, Qfilesize (file));
|
|
|
|
Qclose (file);
|
2006-08-20 06:20:30 +00:00
|
|
|
if (cpp_name && (!options.save_temps)) {
|
|
|
|
if (unlink (tempname->str)) {
|
|
|
|
perror ("unlink");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
2002-10-30 02:31:29 +00:00
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
2006-05-24 11:19:48 +00:00
|
|
|
static void
|
|
|
|
parse_cpp_line (script_t *script, dstring_t *filename)
|
|
|
|
{
|
|
|
|
if (Script_TokenAvailable (script, 0)) {
|
|
|
|
Script_GetToken (script, 0);
|
|
|
|
// subtract 1 for \n
|
|
|
|
script->line = atoi (script->token->str) - 1;
|
|
|
|
if (Script_TokenAvailable (script, 0)) {
|
|
|
|
Script_GetToken (script, 0);
|
|
|
|
dstring_copystr (filename, script->token->str);
|
|
|
|
script->file = filename->str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// There shouldn't be anything else, but just to be safe...
|
|
|
|
while (Script_TokenAvailable (script, 0))
|
|
|
|
Script_GetToken (script, 0);
|
|
|
|
}
|
|
|
|
|
2006-05-24 14:35:39 +00:00
|
|
|
static int
|
|
|
|
compile_file (const char *filename)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2006-05-24 14:50:24 +00:00
|
|
|
yyin = preprocess_file (filename, 0);
|
2006-05-24 14:35:39 +00:00
|
|
|
if (!yyin)
|
|
|
|
return !options.preprocess_only;
|
|
|
|
|
|
|
|
pr.source_file = ReuseString (strip_path (filename));
|
|
|
|
pr.source_line = 1;
|
|
|
|
clear_frame_macros ();
|
|
|
|
err = yyparse () || pr.error_count;
|
|
|
|
fclose (yyin);
|
|
|
|
if (cpp_name && (!options.save_temps)) {
|
|
|
|
if (unlink (tempname->str)) {
|
|
|
|
perror ("unlink");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2002-06-25 21:36:10 +00:00
|
|
|
static int
|
|
|
|
progs_src_compile (void)
|
|
|
|
{
|
|
|
|
dstring_t *filename = dstring_newstr ();
|
2006-05-24 11:19:48 +00:00
|
|
|
dstring_t *qc_filename = dstring_newstr ();
|
2006-05-24 14:35:39 +00:00
|
|
|
dstring_t *single_name = dstring_newstr ();
|
2002-07-31 16:42:21 +00:00
|
|
|
const char *src;
|
2011-03-02 11:51:00 +00:00
|
|
|
//int crc = 0;
|
2004-11-11 00:24:00 +00:00
|
|
|
script_t *script;
|
2006-05-24 14:35:39 +00:00
|
|
|
FILE *single = 0;
|
2011-03-02 11:51:00 +00:00
|
|
|
qfo_t *qfo;
|
2002-03-07 18:36:48 +00:00
|
|
|
|
2002-05-31 07:24:54 +00:00
|
|
|
if (options.verbosity >= 1 && strcmp (sourcedir, "")) {
|
2001-02-24 06:58:54 +00:00
|
|
|
printf ("Source directory: %s\n", sourcedir);
|
|
|
|
}
|
2002-05-31 07:24:54 +00:00
|
|
|
if (options.verbosity >= 1 && strcmp (progs_src, "progs.src")) {
|
2002-01-17 08:19:53 +00:00
|
|
|
printf ("progs.src: %s\n", progs_src);
|
|
|
|
}
|
2001-02-24 06:58:54 +00:00
|
|
|
|
2002-02-05 22:08:19 +00:00
|
|
|
if (*sourcedir)
|
2002-06-01 05:06:19 +00:00
|
|
|
dsprintf (filename, "%s/%s", sourcedir, progs_src);
|
2002-02-05 22:08:19 +00:00
|
|
|
else
|
2002-06-01 05:06:19 +00:00
|
|
|
dsprintf (filename, "%s", progs_src);
|
2002-10-30 02:31:29 +00:00
|
|
|
|
2006-05-24 14:35:39 +00:00
|
|
|
if (options.single_cpp) {
|
2006-08-20 06:20:30 +00:00
|
|
|
intermediate_file (single_name, filename->str, "i2", 1);
|
2006-05-24 14:35:39 +00:00
|
|
|
if (!options.save_temps) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
mktemp (single_name->str);
|
|
|
|
#else
|
|
|
|
int tempfd = mkstemp (single_name->str);
|
2007-04-07 10:24:34 +00:00
|
|
|
single = fdopen (tempfd, "wb");
|
2006-05-24 14:35:39 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (!single)
|
2007-04-07 10:24:34 +00:00
|
|
|
single = fopen (single_name->str, "wb");
|
2006-05-24 14:35:39 +00:00
|
|
|
if (!single) {
|
|
|
|
perror (single_name->str);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-30 02:31:29 +00:00
|
|
|
src = load_file (filename->str);
|
2004-01-22 04:16:19 +00:00
|
|
|
if (!src) {
|
2007-04-06 09:44:08 +00:00
|
|
|
fprintf (stderr, "couldn't open %s\n", filename->str);
|
|
|
|
unlink (single_name->str);
|
2004-01-22 04:16:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2004-11-11 00:24:00 +00:00
|
|
|
script = Script_New ();
|
|
|
|
Script_Start (script, filename->str, src);
|
2001-02-24 06:58:54 +00:00
|
|
|
|
2006-05-24 11:19:48 +00:00
|
|
|
while (1) {
|
|
|
|
if (!Script_GetToken (script, 1)) {
|
2007-04-06 09:44:08 +00:00
|
|
|
fprintf (stderr,
|
|
|
|
"No destination filename. qfcc --help for info.\n");
|
2006-05-24 11:19:48 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (strcmp (script->token->str, "#") == 0) {
|
|
|
|
parse_cpp_line (script, filename);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2002-06-25 21:36:10 +00:00
|
|
|
}
|
2001-02-24 06:58:54 +00:00
|
|
|
|
2002-06-24 22:53:21 +00:00
|
|
|
if (!options.output_file)
|
2004-11-11 00:24:00 +00:00
|
|
|
options.output_file = save_string (script->token->str);
|
2001-10-26 22:07:35 +00:00
|
|
|
if (options.verbosity >= 1) {
|
2002-06-24 22:53:21 +00:00
|
|
|
printf ("output file: %s\n", options.output_file);
|
2001-07-14 01:15:40 +00:00
|
|
|
}
|
2002-06-25 21:36:10 +00:00
|
|
|
setup_sym_file (options.output_file);
|
2001-02-24 06:58:54 +00:00
|
|
|
|
2002-06-28 16:00:01 +00:00
|
|
|
InitData ();
|
2002-07-16 19:03:56 +00:00
|
|
|
chain_initial_types ();
|
2002-06-28 16:00:01 +00:00
|
|
|
|
2002-06-07 17:46:58 +00:00
|
|
|
begin_compilation ();
|
2001-02-24 06:58:54 +00:00
|
|
|
|
|
|
|
// compile all the files
|
2004-11-11 00:24:00 +00:00
|
|
|
while (Script_GetToken (script, 1)) {
|
2006-05-24 11:19:48 +00:00
|
|
|
if (strcmp (script->token->str, "#") == 0) {
|
|
|
|
parse_cpp_line (script, filename);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-05-24 14:35:39 +00:00
|
|
|
while (1) {
|
|
|
|
if (*sourcedir)
|
|
|
|
dsprintf (qc_filename, "%s%c%s", sourcedir, PATH_SEPARATOR,
|
|
|
|
script->token->str);
|
|
|
|
else
|
|
|
|
dsprintf (qc_filename, "%s", script->token->str);
|
|
|
|
if (options.verbosity >= 2)
|
2010-09-05 06:35:22 +00:00
|
|
|
printf ("%s:%d: compiling %s\n", script->file, script->line,
|
|
|
|
qc_filename->str);
|
2006-05-24 14:35:39 +00:00
|
|
|
|
|
|
|
if (single) {
|
2007-03-31 13:54:09 +00:00
|
|
|
fprintf (single, "$frame_reset\n");
|
2007-09-16 07:03:12 +00:00
|
|
|
fprintf (single, "#line %d \"%s\"\n", script->line,
|
|
|
|
script->file);
|
2006-05-24 14:35:39 +00:00
|
|
|
fprintf (single, "#include \"%s\"\n", qc_filename->str);
|
|
|
|
} else {
|
|
|
|
if (compile_file (qc_filename->str))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!Script_TokenAvailable (script, 0))
|
|
|
|
break;
|
2004-11-11 00:24:00 +00:00
|
|
|
Script_GetToken (script, 0);
|
|
|
|
}
|
2006-05-24 14:35:39 +00:00
|
|
|
}
|
|
|
|
if (single) {
|
|
|
|
int err;
|
|
|
|
fclose (single);
|
|
|
|
err = compile_file (single_name->str);
|
|
|
|
if (!options.save_temps) {
|
|
|
|
if (unlink (single_name->str)) {
|
2001-07-20 05:22:44 +00:00
|
|
|
perror ("unlink");
|
|
|
|
exit (1);
|
|
|
|
}
|
2001-07-17 05:02:34 +00:00
|
|
|
}
|
2002-06-28 16:00:01 +00:00
|
|
|
if (err)
|
2001-07-24 04:45:53 +00:00
|
|
|
return 1;
|
2001-02-24 06:58:54 +00:00
|
|
|
}
|
|
|
|
|
2011-03-02 11:51:00 +00:00
|
|
|
class_finish_module ();
|
|
|
|
qfo = qfo_from_progs (&pr);
|
2002-06-21 20:46:56 +00:00
|
|
|
if (options.compile) {
|
2002-07-11 20:17:26 +00:00
|
|
|
qfo_write (qfo, options.output_file);
|
2002-06-21 20:46:56 +00:00
|
|
|
} else {
|
2011-03-02 11:51:00 +00:00
|
|
|
linker_begin ();
|
|
|
|
if (linker_add_qfo (qfo) || finish_link ()) {
|
2002-06-25 21:36:10 +00:00
|
|
|
fprintf (stderr, "compilation errors\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2001-02-24 06:58:54 +00:00
|
|
|
|
2002-06-21 20:46:56 +00:00
|
|
|
// write files.dat
|
|
|
|
if (options.files_dat)
|
2002-06-25 21:36:10 +00:00
|
|
|
if (WriteFiles (sourcedir))
|
|
|
|
return 1;
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
2011-03-02 11:51:00 +00:00
|
|
|
qfo_delete (qfo);
|
2001-02-24 06:58:54 +00:00
|
|
|
|
2002-06-25 21:36:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
main
|
|
|
|
|
|
|
|
The nerve center of our little operation
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
double start, stop;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
start = Sys_DoubleTime ();
|
|
|
|
|
2004-02-05 03:27:35 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (putenv ("POSIXLY_INCORRECT_GETOPT=1")) {
|
|
|
|
fprintf (stderr, "Warning: putenv failed\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-02-24 21:51:08 +00:00
|
|
|
this_program = NORMALIZE (argv[0]);
|
2002-06-25 21:36:10 +00:00
|
|
|
|
|
|
|
DecodeArgs (argc, argv);
|
|
|
|
|
|
|
|
tempname = dstring_new ();
|
|
|
|
parse_cpp_name ();
|
|
|
|
|
|
|
|
opcode_init ();
|
2003-04-23 18:25:09 +00:00
|
|
|
InitData ();
|
2011-01-17 23:58:43 +00:00
|
|
|
init_types ();
|
2003-04-23 18:25:09 +00:00
|
|
|
clear_immediates ();
|
2002-06-25 21:36:10 +00:00
|
|
|
|
|
|
|
if (source_files) {
|
|
|
|
res = separate_compile ();
|
|
|
|
} else {
|
|
|
|
res = progs_src_compile ();
|
|
|
|
}
|
|
|
|
if (res)
|
|
|
|
return res;
|
2001-04-01 03:33:11 +00:00
|
|
|
stop = Sys_DoubleTime ();
|
2001-10-26 22:07:35 +00:00
|
|
|
if (options.verbosity >= 0)
|
2001-10-26 06:43:56 +00:00
|
|
|
printf ("Compilation time: %0.3g seconds.\n", (stop - start));
|
2001-03-28 17:17:56 +00:00
|
|
|
return 0;
|
2001-02-24 06:58:54 +00:00
|
|
|
}
|