summaryrefslogtreecommitdiff
path: root/src/tet3/compiler.ccg
blob: 52540e3f480ab15178c9210945b28d36196cfad1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#
# Generalized C/C++ interface for Microsoft C/C++
#

# SCCS: @(#)compiler.ccg	1.3 (98/09/01) TETware release 3.3
#
# this version used when compiling TETware on Windows NT
# Andrew Dingwall, UniSoft Ltd., October 1996
#
# MODIFICATIONS:
#
#	Andrew Dingwall, UniSoft Ltd., July 1998
#	Added support for building DLLs.
#	Commented out the -link option - it wasn't implemented!
#


# CL option prefixes: these are passed through to compiler

Copts = { "-C", "-d", "-D", "-F", "-G", "-H", "-I", "-J",
	  "-O", "-U", "-u", "-V", "-W", "-Z" };

Copts_compile_only = { "-E", "-P" };

coptions = { "-nologo" };
map = 0;
val = 0;
dll = 0;
Libc = "libc.lib";
Libs = {};
Srcs = {};
Objs = {};
Output = "";


for (i in ARGV)
do
	flag = substr(i,1,2);
	if (i == "-c")
		compile_only = 1;
	elif (i == "-S")		# assembler listing
		coptions |= "/Fa";
		compile_only = 1;
	elif (substr(i,-2) == ".c")
		Srcs |= i;
		opath = filepath(substr(i,1,length(i)-1));
		Objs |= opath[length(opath)-1] | "obj ";
	elif (substr(i,-4) == ".asm")
		Srcs |= i;
		opath = filepath(substr(i,1,length(i)-3));
		Objs |= opath[length(opath)-1] | "obj ";
	elif (substr(i,-4) == ".cpp")
		Srcs |= i;
		opath = filepath(substr(i,1,length(i)-3));
		Objs |= opath[length(opath)-1] | "obj ";
	elif (substr(i,-4) == ".obj")
		Objs |= i | " ";
	elif (i == "-o")
		advance i;
		Output = i;
	elif (substr(i,1,3) == "-Fe")		# support -Fe<outputfile>
		Output = substr(i,4);
	elif (index(Copts, flag) != 0)
		coptions |= i;
	elif (index(Copts_compile_only, flag) != 0)
		coptions |= i;
		compile_only = 1;
	elif (i == "-M")
		map = 1;
#	elif (substr(i,1,5) == "-link")
#		LinkOptions |= " " | i;
	elif (substr(i,-4) == ".lib")
		Libs |= i | " ";
	elif (i == "-v")
		verbose = 1;
	elif (i == "-ML")
		coptions |= i;
	elif (i == "-MT")
		coptions |= i;
		Libc = "libcmt.lib";
	elif (i == "-MD")
		coptions |= i;
		Libc = "msvcrt.lib";
	elif (i == "-MLd")
		coptions |= i;
		Libc = "libcd.lib";
	elif (i == "-MTd")
		coptions |= i;
		Libc = "libcmtd.lib";
	elif (i == "-MDd")
		coptions |= i;
		Libc = "msvcrtd.lib";
	elif (i == "-LD")
		dll = 1;
		coptions |= "-MD";
		Libc = "msvcrt.lib";
	elif (i == "-LDd")
		dll = 1;
		coptions |= "-MDd";
		Libc = "msvcrtd.lib";
	elif (substr(i,1,1) == "-")
		println "Unknown option:", i, "(ignored)";
	else
		println "Unknown", i, "(ignored)";
	fi
done

if (! Objs)
	print "No source or object files!\n";
	exit 1;
fi

if (Output == "")
	Output = Objs[0];
	if (dll)
		Output = substr(Output, 1, rindex(Output,".")) | "lib";
	else
		Output = substr(Output, 1, rindex(Output,".")) | "exe";
	fi
fi

# compile all .c files
for (i in Srcs)
do
	if (verbose)
		print "cl ", "-c ";
		for (j in coptions)
		do
			print j, " ";
		done
		print i, "\n\n";
	fi
	val = execv("cl", "-c", coptions, i);
	if (val)
		exit val;
	fi
done

if (compile_only)
	exit val;
fi

ldoptions = "-nologo";
if (dll)
	ldoptions |= " -dll -implib:" | replace(Output, "/", "\\");
	Output = substr(Output, 1, rindex(Output,".")) | "dll";
else
	ldoptions |= " -align:0x1000 -subsystem:console -entry:mainCRTStartup";
fi
ldoptions |= " -out:" | replace(Output, "/", "\\");
if (map)
	ldoptions |= " -map";
fi

Libs |= Libc | " kernel32.lib";

ldfiles = "";
for (i in Objs)
do
	ldfiles |= " " | replace(i, "/", "\\");
done

for (i in Libs)
do
	ldfiles |= " " | replace(i, "/", "\\");
done

if (verbose)
	println "link " | ldoptions | ldfiles, "\n";
fi

exit exec("link " | ldoptions | ldfiles);