File: cl-wrapper.c

package info (click to toggle)
gtkgl2 1.99.0-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,104 kB
  • ctags: 330
  • sloc: sh: 6,744; ansic: 3,173; makefile: 125
file content (298 lines) | stat: -rw-r--r-- 6,399 bytes parent folder | download | duplicates (4)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* cl-wrapper -- wrapper around the Microsoft C compiler
 * Copyright (C) 2001 Tor Lillqvist
 *
 * This program accepts Unix-style C compiler command line arguments,
 * and runs the Microsoft C compiler (cl) with corresponding arguments.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <io.h>

static char *cmdline;
static char **libraries;
static char **libdirs;
static char **objects;
static char *output_executable = NULL, *executable_type = NULL;
static int lib_ix = 0;
static int libdir_ix = 0;
static int object_ix = 0;

static int compileonly = 0, debug = 0, output = 0, verbose = 0;
static int nsources = 0;

static void
backslashify (char *p)
{
  while ((p = strchr (p, '/')) != NULL)
    {
      *p = '\\';
      p++;
    }
}

static void
process_argv (int argc,
	      char **argv)
{
  int i;
  char *lastdot;

  for (i = 1; i < argc; i++)
    if (strcmp (argv[i], "-c") == 0)
      {
	compileonly++;
	strcat (cmdline, " -c");
      }
    else if (strncmp (argv[i], "-D", 2) == 0)
      {
	strcat (cmdline, " ");
	strcat (cmdline, argv[i]);
	if (strlen (argv[i]) == 2)
	  {
	    i++;
	    strcat (cmdline, argv[i]);
	  }
      }
    else if (strcmp (argv[i], "-E") == 0)
      strcat (cmdline, " -E");
    else if (strcmp (argv[i], "-g") == 0)
      debug++;
    else if (strncmp (argv[i], "-I", 2) == 0)
      {
	strcat (cmdline, " ");
	backslashify (argv[i]);
	strcat (cmdline, argv[i]);
	if (strlen (argv[i]) == 2)
	  {
	    i++;
	    backslashify (argv[i]);
	    strcat (cmdline, argv[i]);
	  }
      }
    else if (strncmp (argv[i], "-l", 2) == 0)
      libraries[lib_ix++] = argv[i] + 2;
    else if (strncmp (argv[i], "-L", 2) == 0)
      {
	if (strlen (argv[i]) > 2)
	  {
	    backslashify (argv[i]);
	    libdirs[libdir_ix++] = argv[i] + 2;
	  }
	else
	  {
	    i++;
	    backslashify (argv[i]);
	    libdirs[libdir_ix++] = argv[i];
	  }
      }
    else if (strcmp (argv[i], "-o") == 0)
      {
	output++;
	i++;
	lastdot = strrchr (argv[i], '.');
	if (lastdot != NULL && (stricmp (lastdot, ".exe") == 0
				|| stricmp (lastdot, ".dll") == 0))
	  {
	    strcat (cmdline, " -Fe");
	    backslashify (argv[i]);
	    strcat (cmdline, argv[i]);
	    output_executable = argv[i];
	    executable_type = strrchr (output_executable, '.');
	  }
	else if (lastdot != NULL && (strcmp (lastdot, ".obj") == 0))
	  {
	    strcat (cmdline, " -Fo");
	    strcat (cmdline, argv[i]);
	  }
	else
	  {
	    strcat (cmdline, " -Fe");
	    strcat (cmdline, argv[i]);
	    strcat (cmdline, ".exe");
	  }
      }
    else if (strcmp (argv[i], "-O") == 0)
      strcat (cmdline, " -O");
    else if (strcmp (argv[i], "-v") == 0)
      verbose++;
    else if (argv[i][0] == '-')
      fprintf (stderr, "Ignored flag %s\n", argv[i]);
    else
      {
	lastdot = strrchr (argv[i], '.');
	if (lastdot != NULL && (stricmp (lastdot, ".c") == 0
				|| stricmp (lastdot, ".cpp") == 0
				|| stricmp (lastdot, ".cc") == 0))
	  {
	    nsources++;
	    strcat (cmdline, " ");
	    if (stricmp (lastdot, ".cc") == 0)
	      strcat (cmdline, "-Tp");
	    strcat (cmdline, argv[i]);
	  }
	else if (lastdot != NULL && stricmp (lastdot, ".obj") == 0)
	  objects[object_ix++] = argv[i];
	else
	  fprintf (stderr, "Ignored argument: %s\n", argv[i]);
      }

}

static void
process_envvar (char *envvar)
{
  int argc = 0;
  char **argv;
  char *p = envvar;

  while (p && *p)
    {
      p = strchr (p, ' ');
      argc++;
      if (p)
	while (*p == ' ')
	  p++;
    }

  argv = malloc (argc * sizeof (char *));

  p = envvar;
  argc = 0;
  while (p && *p)
    {
      char *q = strchr (p, ' ');
      if (q)
	*q = 0;
      argv[argc++] = p;
      if (q)
	{
	  while (*q == ' ')
	    q++;
	  p = q;
	}
    }
  process_argv (argc, argv);
}

int
main (int argc,
      char **argv)
{
  int retval;

  int i, j, k;
  char *p;
  
  libraries = malloc (argc * sizeof (char *));
  libdirs = malloc ((argc+10) * sizeof (char *));
  objects = malloc (argc * sizeof (char *));

  for (k = 0, i = 1; i < argc; i++)
    k += strlen (argv[i]);

  k += 500 + argc;
  cmdline = malloc (k);

  /* -MD: Use msvcrt.dll runtime */
  strcpy (cmdline, "cl -MD");

  p = getenv ("CPPFLAGS");
  if (p && *p)
    process_envvar (p);

  p = getenv ("LDFLAGS");
  if (p && *p)
    process_envvar (p);

  process_argv (argc, argv);

  if (!verbose)
    strcat (cmdline, " -nologo");

  if (output_executable != NULL)
    {
      if (stricmp (executable_type, ".dll") == 0)
	strcat (cmdline, " -LD");
    }

  if (debug)
    strcat (cmdline, " -Zi");

  if (nsources == 0)
    {
      FILE *dummy = fopen ("__dummy__.c", "w");
      fprintf (dummy, "static int foobar = 42;\n");
      fclose (dummy);

      strcat (cmdline, " __dummy__.c");
    }

  if (!output && !compileonly)
    strcat (cmdline, " -Fea.exe");

  if (!compileonly)
    {
      strcat (cmdline, " -link");

      for (i = 0; i < object_ix; i++)
	{
	  strcat (cmdline, " ");
	  strcat (cmdline, objects[i]);
	}

      for (i = 0; i < lib_ix; i++)
	{
	  strcat (cmdline, " ");
	  for (j = 0; j < libdir_ix; j++)
	    {
	      char b[1000];
	      
	      sprintf (b, "%s\\%s.lib", libdirs[j], libraries[i]);
	      if (access (b, 4) == 0)
		{
		  strcat (cmdline, b);
		  break;
		}
	      sprintf (b, "%s\\lib%s.lib", libdirs[j], libraries[i]);
	      if (access (b, 4) == 0)
		{
		  strcat (cmdline, b);
		  break;
		}
	    }
	  
	  if (j == libdir_ix)
	    {
	      strcat (cmdline, libraries[i]);
	      strcat (cmdline, ".lib");
	    }
	}
    }
  
  fprintf (stderr, "%s\n", cmdline);
      
  retval = system (cmdline);

  if (nsources == 0)
    remove ("__dummy__.c");

  return retval;
}