File: gnu_cs.c

package info (click to toggle)
premake 3.7-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,148 kB
  • sloc: ansic: 20,490; cs: 15,185; makefile: 332
file content (605 lines) | stat: -rw-r--r-- 16,904 bytes parent folder | download
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/**********************************************************************
 * Premake - gnu_cs.c
 * The GNU C# makefile target
 *
 * Copyright (c) 2002-2005 Jason Perkins and the Premake project
 * 
 * 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 in the file LICENSE.txt for details.
 **********************************************************************/

#include <stdio.h>
#include <string.h>
#include "premake.h"
#include "gnu.h"
#include "os.h"

static char buffer[8192];

static const char* assignCodeFiles(const char* name);
static const char* assignEmbeddedFiles(const char* name);
static const char* assignContentFiles(const char* name);

static const char* listCodeFiles(const char* name);
static const char* listEmbeddedFiles(const char* name);
static const char* listLinkedFiles(const char* name);
static const char* listContentFiles(const char* name);
static const char* listContentTargets(const char* name);
static const char* listContentRules(const char* name);
static const char* listCopyLocalFiles(const char* name);
static const char* listCopyLocalRules(const char* name);

static const char* listNonSiblingRefs(const char* name);
static const char* listSiblingRefs(const char* name);
static const char* listReferenceDeps(const char* name);
static const char* listResourceBuildSteps(const char* name);


int gnu_cs()
{
	const char* kind;
	const char* csc;
	const char* resgen;
	int i;
	const char** files;
	const char* appConfigName;

	/* Figure out what I'm building */
	prj_select_config(0);
	kind = prj_get_kind();
	if (prj_is_kind("dll") || prj_is_kind("aspnet"))
	{
		kind = "library";
	}
	else if (prj_is_kind("winexe") || prj_is_kind("exe"))
	{
	}
	else
	{
		printf("** Error: unknown package type '%s'\n", kind);
		return 0;
	}

	/* Figure out what I'm building it with */
	if (g_dotnet == NULL)
	{
		if (os_is("windows"))
		{
			csc = "csc";
			resgen = "resgen";
		}
		else
		{
			csc = "mcs";
			resgen = "resgen";
		}
	}
	else if (matches(g_dotnet, "ms"))
	{
		csc = "csc";
		resgen = "resgen";
	}
	else if (matches(g_dotnet, "pnet"))
	{
		csc = "cscc";
		resgen = "resgen";
	}
	else if (matches(g_dotnet, "mono"))
	{
		csc = "mcs";
		resgen = "resgen";
	}
	else if (matches(g_dotnet, "mono2"))
	{
		csc = "gmcs";
		resgen = "resgen";
	}
	else
	{
		printf("** Error: unknown .NET runtime '%s'\n", g_dotnet);
		return 0;
	}

	/* Open package makefile and write the header */
	if (gnu_pkgOwnsPath())
		strcpy(buffer, path_join(prj_get_pkgpath(), "Makefile", ""));
	else
		strcpy(buffer, path_join(prj_get_pkgpath(), prj_get_pkgname(), DOT_MAKE));
	io_openfile(buffer);

	io_print("# C# ");
	if (prj_is_kind("exe"))
		io_print("Console Executable");
	else if (prj_is_kind("winexe"))
		io_print("Windowed Executable");
	else if (prj_is_kind("dll"))
		io_print("Shared Library");
	else if (prj_is_kind("lib"))
		io_print("Static Library");
	else if (prj_is_kind("aspnet"))
		io_print("ASP.NET");

	io_print(" Makefile autogenerated by premake\n");
	io_print("# Don't edit this file! Instead edit `premake.lua` then rerun `make`\n\n");

	/* Set a default configuration */
	prj_select_config(0);
	io_print("ifndef CONFIG\n");
	io_print("  CONFIG=%s\n", prj_get_cfgname());
	io_print("endif\n\n");

	/* Figure out if I have an App.config file */
	appConfigName = NULL;
	files = prj_get_files();
	for (i = 0; files[i] != NULL; ++i)
	{
		if (matches(files[i], "App.config") || matches(files[i], "app.config"))
		{
			appConfigName = files[i];
			break;
		}
	}

	/* Specify the build tools */
	io_print("CSC := %s\n", csc);
	io_print("RESGEN := %s\n", resgen);
	io_print("\n");

	/* Where to put compiled resources */
	io_print("OBJDIR := %s\n\n", prj_get_objdir());

	/* Process the build configurations */
	for (i = 0; i < prj_get_numconfigs(); ++i)
	{
		prj_select_config(i);

		io_print("ifeq ($(CONFIG),%s)\n", prj_get_cfgname());
		io_print("  BINDIR := %s\n", gnu_escapePath(prj_get_bindir()));
		io_print("  OUTDIR := %s\n", gnu_escapePath(prj_get_outdir()));

		io_print("  FLAGS += /t:%s", kind);
		if (!prj_has_flag("no-symbols"))
		{
			io_print(" /debug");
		}
		if (prj_has_flag("optimize") || prj_has_flag("optimize-size") || prj_has_flag("optimize-speed"))
		{
			/* Mono doesn't support the optimize flag */
			if (!matches(csc, "mcs"))
				io_print(" /optimize");
		}
		if (prj_has_flag("unsafe"))
		{
			io_print(" /unsafe");
		}
		if (prj_has_flag("fatal-warnings"))
		{
			io_print(" /warnaserror");
		}

		print_list(prj_get_defines(), " /d:", "", "", NULL);

		/* VS.NET doesn't allow per-config link paths */
		prj_select_config(0);
		print_list(prj_get_libpaths(), " /lib:\"", "\"", "", NULL);
		print_list(prj_get_links(), " /r:", "", "", listNonSiblingRefs);
		prj_select_config(i);
		print_list(prj_get_links(), " /r:", "", "", listSiblingRefs);
		io_print("\n");

		/* List any sibling packages as dependencies for make */
		prj_select_config(i);
		io_print("  DEPS :=");
		print_list(prj_get_links(), " ", " ", "", listReferenceDeps);
		io_print("\n");

		/* VS.NET doesn't allow per-config target names */
		prj_select_config(0);
		io_print("  TARGET := %s\n", gnu_escapePath(path_getname(prj_get_target())));
		io_print("endif\n\n");
	}

	/* Sort out the files by build action...start by assigning build actions
	 * to well-known file extensions...these don't write anything to the file */
	print_list(prj_get_files(), "", "", "", assignCodeFiles);
	print_list(prj_get_files(), "", "", "", assignEmbeddedFiles);
	print_list(prj_get_files(), "", "", "", assignContentFiles);

	/* Sort out the files by build action */
	io_print("SOURCES := \\\n");
	print_list(prj_get_files(), "\t", " \\\n", "", listCodeFiles);
	io_print("\n");

	io_print("EMBEDDEDFILES := \\\n");
	print_list(prj_get_files(), "\t", " \\\n", "", listEmbeddedFiles);
	io_print("\n");

	io_print("EMBEDDEDCOMMAND := \\\n");
	print_list(prj_get_files(), "\t/resource:", " \\\n", "", listEmbeddedFiles);
	io_print("\n");
	
	io_print("LINKEDFILES := \\\n");
	print_list(prj_get_files(), "\t", " \\\n", "", listLinkedFiles);
	io_print("\n");

	io_print("LINKEDCOMMAND := \\\n");
	print_list(prj_get_files(), "\t/linkresource:", " \\\n", "", listLinkedFiles);
	io_print("\n");

	io_print("CONTENTFILES := \\\n");
	print_list(prj_get_files(), "\t", " \\\n", "", listContentFiles);
	io_print("\n");

	io_print("COPYLOCALFILES := \\\n");
	print_list(prj_get_links(), "\t", " \\\n", "", listCopyLocalFiles);
	io_print("\n");

	io_print("COMPILECOMMAND := $(SOURCES) $(EMBEDDEDCOMMAND) $(LINKEDCOMMAND)\n\n");

	io_print("CMD := $(subst \\,\\\\,$(ComSpec)$(COMSPEC))\n");
	io_print("ifeq (,$(CMD))\n");
	io_print("  CMD_MKOUTDIR := mkdir -p $(OUTDIR)\n");
	io_print("else\n");
	io_print("  CMD_MKOUTDIR := $(CMD) /c if not exist $(subst /,\\\\,$(OUTDIR)) mkdir $(subst /,\\\\,$(OUTDIR))\n");
	io_print("endif\n");
	io_print("\n");

	/* Build targets: add all content files as dependencies so the copy
	 * rules will get fired when they change */
	io_print(".PHONY: clean\n\n");
	io_print("all: \\\n");
	io_print("\t$(OUTDIR)/$(TARGET) \\\n");
	if (appConfigName != NULL)
	{
		io_print("\t$(OUTDIR)/$(TARGET).config \\\n");
	}
	print_list(prj_get_files(), "\t", " \\\n", "", listContentTargets);
	io_print("\n");

	/* The main build target */
	io_print("$(OUTDIR)/$(TARGET): $(SOURCES) $(EMBEDDEDFILES) $(LINKEDFILES) $(COPYLOCALFILES) $(DEPS)\n");
	io_print("\t-@$(CMD_MKOUTDIR)\n");
	io_print("\t@$(CSC) /nologo /out:\"$@\" /lib:$(BINDIR) $(FLAGS) $(COMPILECOMMAND)\n\n");

	/* The App.config rule */
	if (appConfigName != NULL)
	{
		io_print("$(OUTDIR)/$(TARGET).config: %s\n", appConfigName);
		io_print("\t@echo Copying %s\n", appConfigName);
		io_print("\t-@$(CMD_MKOUTDIR)\n");
		io_print("\t@cp \"$^\" \"$@\"\n\n");
	}

	/* Write rules to copy content files */
	print_list(prj_get_files(), "", "", "", listContentRules);

	/* Write rules to copy local assemblies */
	print_list(prj_get_links(), "", "", "", listCopyLocalRules);

	/* The clean target */
	io_print("clean:\n");
	io_print("\t@echo Cleaning %s\n", prj_get_pkgname());
	io_print("\t-@rm -f $(OUTDIR)/$(TARGET)\n");
	io_print("\t-@rm -fR $(OBJDIR)\n");
	print_list(prj_get_files(), "\t-@rm -f ", "\n", "", listContentTargets);
	io_print("\n");

	/* Resource build targets */
	print_list(prj_get_files(), "", "", "", listResourceBuildSteps);

	io_closefile();
	return 1;
}


/************************************************************************
 * Visual Studio .NET automatically adds some extra information
 * to resource names. Simulate that here.
 ***********************************************************************/

static const char* makeVsNetCompatName(const char* name)
{
	char* ptr;

	if (matches(path_getextension(name), ".resx"))
	{
		/* The compiled .resources file goes into objdir */
		strcpy(buffer, prj_get_objdir());
		strcat(buffer, "/");

		/* Prepend the default namespace (slashes will get translated below) */
		strcat(buffer, path_getbasename(prj_get_target()));
		strcat(buffer, ".");

		/* ...followed by the path to the file */
		if (!matches(path_getdir(name), ""))
		{
			strcat(buffer, path_getdir(name));
			strcat(buffer, ".");
		}

		/* ...finish up with the file name */
		strcat(buffer, path_getbasename(name));
		strcat(buffer, ".resources");

		/* Convert any slashes in the name to dots */
		for (ptr = buffer + strlen(prj_get_objdir()) + 1; *ptr != '\0'; ++ptr)
		{
			if (*ptr == '/')
				*ptr = '.';
		}

		return gnu_escapePath(buffer);
	}
	else
	{
		return name;
	}
}


/************************************************************************
 * Assign default build actions to files without one
 ***********************************************************************/

static const char* assignCodeFiles(const char* name)
{
	const char* ext = path_getextension(name);
	prj_select_file(name);
	if (prj_is_buildaction(NULL) && matches(ext, ".cs"))
		prj_set_buildaction("Code");
	return NULL;
}

static const char* assignEmbeddedFiles(const char* name)
{
	const char* ext = path_getextension(name);
	prj_select_file(name);
	if (prj_is_buildaction(NULL) && matches(ext, ".resx"))
		prj_set_buildaction("EmbeddedResource");
	return NULL;
}

static const char* assignContentFiles(const char* name)
{
	const char* ext = path_getextension(name);
	prj_select_file(name);
	if (prj_is_buildaction(NULL) && (matches(ext, ".asax") || matches(ext, ".aspx")))
		prj_set_buildaction("Content");
	return NULL;
}


/************************************************************************
 * Checks to see if a given assembly exists on one of the listed link
 * paths. If so, returns the relative path to the assembly.
 ***********************************************************************/

static const char* findLocalAssembly(const char* name)
{
	const char** paths;
	const char*  result = NULL;

	/* Lib paths are relative to package directory. Have to make package
	 * directory current for io_fileexists() to work */
	strcpy(buffer, io_getcwd());
	io_chdir(prj_get_pkgpath());

	paths = prj_get_libpaths();
	while (*paths != NULL)
	{
		const char* path = path_join(*paths, name, "dll");
		if (io_fileexists(path))
		{
			result = path;
			break;
		}
		++paths;
	}

	io_chdir(buffer);
	return result;
}


/************************************************************************
 * Returns lists of files for each build action
 ***********************************************************************/

static const char* listCodeFiles(const char* name)
{
	prj_select_file(name);
	if (prj_is_buildaction("Code"))
	{
		/* Csc needs backslashes, which GNU make doesn't like */
		const char* src = path_translate(name, NULL);
		char* dst = buffer;
		while (*src)
		{
			if (*src == '\\')
			{
				*(dst++) = '\\';
				*(dst++) = '\\';
			}
			else
			{
				*(dst++) = *src;
			}
			src++;
		}
		*dst ='\0';
		return gnu_escapePath(buffer);
	}
	else
	{
		return NULL;
	}
}

static const char* listEmbeddedFiles(const char* name)
{
	prj_select_file(name);
	if (prj_is_buildaction("EmbeddedResource"))
		return makeVsNetCompatName(name);
	else
		return NULL;
}

static const char* listLinkedFiles(const char* name)
{
	prj_select_file(name);
	if (prj_is_buildaction("LinkedResource"))
		return makeVsNetCompatName(name);
	else
		return NULL;
}

static const char* listContentFiles(const char* name)
{
	prj_select_file(name);
	if (prj_is_buildaction("Content"))
		return gnu_escapePath(name);
	else
		return NULL;
}

static const char* listCopyLocalFiles(const char* name)
{
	const char* path = findLocalAssembly(name);
	if (path != NULL)
	{
		sprintf(buffer, "$(OUTDIR)/%s.dll", gnu_escapePath(name));
		return buffer;
	}
	return NULL;
}


/************************************************************************
 * Checks to see if a reference is to a sibling package. If so, 
 * returns the binary target name of the package.
 ***********************************************************************/

static const char* listNonSiblingRefs(const char* name)
{
	int i = prj_find_package(name);
	if (i < 0)
	{
		return path_join("", name, "dll");
	}
	return NULL;
}


static const char* listSiblingRefs(const char* name)
{
	int i = prj_find_package(name);
	if (i >= 0)
	{
		if (matches(prj_get_language_for(i), "c#"))
		{
			// return prj_get_target_for(i);
			return prj_get_relativetarget_for(i);
		}
	}
	return NULL;
}


/************************************************************************
 * This is called by the code that builds the list of dependencies for 
 * the link step. It looks for sibling projects, and then returns the 
 * full path to that target's output. So if an executable package 
 * depends on a library package, the library filename will be listed 
 * as a dependency
 ***********************************************************************/

static const char* listReferenceDeps(const char* name)
{
	int i = prj_find_package(name);
	if (i >= 0)
	{
		// return prj_get_target_for(i);
		return prj_get_relativetarget_for(i);
	}

	return NULL;
}


/************************************************************************
 * Write out content files as make dependencies
 ***********************************************************************/

static const char* listContentTargets(const char* name)
{
	prj_select_file(name);
	if (prj_is_buildaction("LinkedResource") || prj_is_buildaction("Content"))
	{
		sprintf(buffer, "$(BINDIR)/%s", gnu_escapePath(path_getname(name)));
		return buffer;
	}
	else
	{
		return NULL;
	}
}


/************************************************************************
 * Write out rules to move files to output directory
 ***********************************************************************/

static const char* listContentRules(const char* name)
{
	prj_select_file(name);
	name = gnu_escapePath(name);
	if (prj_is_buildaction("LinkedResource") || prj_is_buildaction("Content"))
	{
		sprintf(buffer, "$(BINDIR)/%s: %s\n\t-@cp -fR $^ \"$@\"\n\n", path_getname(name), name);
		return buffer;
	}
	return NULL;
}

static const char* listCopyLocalRules(const char* name)
{
	const char* path = findLocalAssembly(name);
	name = gnu_escapePath(name);
	if (path != NULL)
	{
		sprintf(buffer, "$(OUTDIR)/%s.dll: %s\n\t@echo Copying %s.dll\n\t-@$(CMD_MKOUTDIR)\n\t-@cp \"$^\" \"$@\"\n\n", name, path, name);
		return buffer;
	}
	return NULL;
}



/************************************************************************
 * Write out resource compilation rules
 ***********************************************************************/

static const char* listResourceBuildSteps(const char* name)
{
	if (matches(path_getextension(name), ".resx"))
	{
		makeVsNetCompatName(name);
		strcat(buffer, ": ");
		strcat(buffer, name);
		strcat(buffer, "\n\t-@if [ ! -d $(OBJDIR) ]; then mkdir -p $(OBJDIR); fi");
		strcat(buffer, "\n\t@$(RESGEN) $^ \"$@\"\n\n");
		return buffer;
	}
	else
	{
		return NULL;
	}
}