Welcome to mirror list, hosted at ThFree Co, Russian Federation.

project.c « objects « src - github.com/windirstat/premake-4.x-stable.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 636221a4ad47872b1d8aa171e893ee4fe3067bf3 (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
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
/**
 * \file   project.c
 * \brief  The project class.
 * \author Copyright (c) 2002-2008 Jason Perkins and the Premake project
 */

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "premake.h"
#include "solution.h"
#include "base/buffers.h"
#include "base/cstr.h"
#include "base/env.h"
#include "base/guid.h"
#include "base/path.h"
#include "base/strings.h"


FieldInfo ProjectFieldInfo[] =
{
	{ "basedir",    StringField,  NULL            },
	{ "files",      FilesField,   NULL            },
	{ "guid",       StringField,  guid_is_valid   },
	{ "kind",       StringField,  project_is_valid_kind     },
	{ "language",   StringField,  project_is_valid_language },
	{ "location",   StringField,  NULL            },
	{ "name",       StringField,  NULL            },
	{  0,           0,            NULL            }
};

static const char* ValidKinds[] = 
{
	"Console", NULL
};

static const char* ValidLanguages[] =
{
	"C++", NULL
};


DEFINE_CLASS(Project)
{
	Solution solution;
	Blocks blocks;
	Fields fields;
	Strings config_cache[NumBlockFields];
	const char* active_config;
};


/**
 * Create and initialize a new project object.
 */
Project project_create()
{
	int i;

	Project prj = ALLOC_CLASS(Project);
	prj->solution = NULL;
	prj->blocks = blocks_create();
	prj->fields = fields_create(ProjectFieldInfo);
	prj->active_config = NULL;

	for (i = 0; i < NumBlockFields; ++i)
	{
		prj->config_cache[i] = NULL;
	}

	return prj;
}


/**
 * Destroy a project object and release the associated memory.
 */
void project_destroy(Project prj)
{
	int i;

	assert(prj);	
	blocks_destroy(prj->blocks);
	fields_destroy(prj->fields);
	for (i = 0; i < NumBlockFields; ++i)
	{
		if (prj->config_cache[i] != NULL)
			strings_destroy(prj->config_cache[i]);
	}
	free(prj);
}



/**
 * Get the base directory for the project; any properties containing relative
 * paths are relative to this location.
 */
const char* project_get_base_dir(Project prj)
{
	return project_get_value(prj, ProjectBaseDir);
}


/**
 * Retrieve the list of configuration blocks associated with a project.
 */
Blocks project_get_blocks(Project prj)
{
	assert(prj);
	return prj->blocks;
}



/**
 * Scans a list of blocks and appends any values found to the provided list.
 */
static void project_get_values_from_blocks(Project prj, Strings values, Blocks blks, BlockField field)
{
	int i, n = blocks_size(blks);
	for (i = 0; i < n; ++i)
	{
		Block blk = blocks_item(blks, i);

		if (block_applies_to(blk, prj->active_config))
		{
			Strings block_values = block_get_values(blk, field);
			strings_append(values, block_values);
		}
	}
}


/**
 * Retrieve the currently active configuration name; only settings contained by
 * blocks targeted to this configuration will be accessible. Returns NULL if no
 * configuration has been selected, in which case only global settings will 
 * be available.
 */
const char* project_get_config(Project prj)
{
	assert(prj);
	return prj->active_config;
}


/**
 * Retrieve a list value from the project configuration, respecting the current filter set.
 */
Strings project_get_config_values(Project prj, BlockField field)
{
	Strings values;

	assert(prj);
	assert(field >= 0 && field < NumBlockFields);

	values = strings_create();

	/* The "cache" just keeps track of all lists that I return to callers; I will
	 * take responsibility for destroying the lists, so the callers don't have to
	 * do it themselves. A bit of complexity here to simplify the action handers. */
	if (prj->config_cache[field] != NULL)
	{
		strings_destroy(prj->config_cache[field]);
	}
	prj->config_cache[field] = values;

	project_get_values_from_blocks(prj, values, solution_get_blocks(prj->solution), field);
	project_get_values_from_blocks(prj, values, project_get_blocks(prj), field);
	return values;
}


/**
 * Retrieve the list of preprocessor defines, using the current configuration filter.
 */
Strings project_get_defines(Project prj)
{
	Strings values = project_get_config_values(prj, BlockDefines);
	return values;
}


/**
 * Retrieve the fields object for this project; used to unload values from the script.
 */
Fields project_get_fields(Project prj)
{
	assert(prj);
	return prj->fields;
}


/**
 * Get the path to the project output file, using the provided file name and extension.
 * \param   prj      The project object to query.
 * \param   basename The base filename; if NULL the project name will be used.
 * \param   ext      The file extension to be used on the filename; may be NULL.
 * \returns The path to the project file.
 */
const char* project_get_filename(Project prj, const char* basename, const char* ext)
{
	const char* location = project_get_location(prj);
	if (!basename)
	{
		basename = project_get_name(prj);
	}
	return path_assemble(location, basename, ext);
}


/**
 * Get the relative path from the solution to the project file, using the 
 * provided file name and extension.
 * \param   prj      The project object to query.
 * \param   basename The base filename; if NULL the project name will be used.
 * \param   ext      The file extension to be used on the filename; may be NULL.
 * \returns The path to the project file.
 */
const char* project_get_filename_relative(Project prj, const char* basename, const char* ext)
{
	const char* sln_location;
	const char* abs_filename;
	assert(prj);
	assert(prj->solution);

	sln_location = solution_get_location(prj->solution);
	abs_filename = project_get_filename(prj, basename, ext);

	return path_relative(sln_location, abs_filename);
}



/**
 * Retrieve the list of source files associated with a project.
 */
Strings project_get_files(Project prj)
{
	assert(prj);
	return fields_get_values(prj->fields, ProjectFiles);
}


/**
 * Retrieve the GUID associated with a project.
 */
const char* project_get_guid(Project prj)
{
	return project_get_value(prj, ProjectGuid);
}


/**
 * Get the project kind: console, windowed, etc.
 */
const char* project_get_kind(Project prj)
{
	const char* kind = project_get_value(prj, ProjectKind);
	if (kind == NULL && prj->solution != NULL)
	{
		kind = solution_get_kind(prj->solution);
	}
	return kind;
}


/**
 * Get the programming language used by the project.
 */
const char* project_get_language(Project prj)
{
	const char* lang = project_get_value(prj, ProjectLanguage);
	if (lang == NULL && prj->solution != NULL)
	{
		lang = solution_get_language(prj->solution);
	}
	return lang;
}


/**
 * Retrieve the output location (the relative path from the base directory to the
 * target output directory) for this project.
 */
const char* project_get_location(Project prj)
{
	return project_get_value(prj, ProjectLocation);
}


/**
 * Get the name of the project.
 */
const char* project_get_name(Project prj)
{
	return project_get_value(prj, ProjectName);
}


/**
 * Retrieve the output filename for this project, taking into account platform-specific
 * naming conventions. For instance, for a project named "MyProject" this function would
 * return "MyProject.exe" on Windows. No path information is included, use the function
 * project_get_outdir() for that.
 */
const char* project_get_outfile(Project prj)
{
	char* buffer = buffers_next();
	strcpy(buffer, project_get_name(prj));
	if (env_is_os(Windows))
	{
		strcat(buffer, ".exe");
	}
	return buffer;
}


/**
 * Retrieve the session which contains this project.
 */
Session project_get_session(Project prj)
{
	assert(prj);
	return solution_get_session(prj->solution);
}


/**
 * Retrieve the solution associated with this project (internal).
 */
Solution project_get_solution(Project prj)
{
	return prj->solution;
}


/**
 * Retrieve a string (single value) fields from a project, using the field indices.
 */
const char* project_get_value(Project prj, ProjectField field)
{
	assert(prj);
	return fields_get_value(prj->fields, field);
}


/**
 * Returns true if the project flags contain the specified flag.
 */
int project_has_flag(Project prj, const char* flag)
{
	Strings flags = project_get_config_values(prj, BlockFlags);
	return strings_contains(flags, flag);
}


/**
 * Returns true if the project kind matches the parameter.
 */
int project_is_kind(Project prj, const char* kind)
{
	assert(prj);
	return cstr_eqi(project_get_kind(prj), kind);
}


/**
 * Returns true if the project language matches the parameter.
 */
int project_is_language(Project prj, const char* language)
{
	assert(prj);
	return cstr_eqi(project_get_language(prj), language);
}


/**
 * Returns true if the specified language is recognized. See the ValidLanguages at
 * the top of this file for a list of valid values.
 */
int project_is_valid_kind(const char* kind)
{
	const char** i;
	for (i = ValidKinds; (*i) != NULL; ++i)
	{
		if (cstr_eqi((*i), kind))
			return 1;
	}
	return 0;
}


/**
 * Returns true if the specified language is recognized. See the ValidLanguages at
 * the top of this file for a list of valid values.
 */
int project_is_valid_language(const char* language)
{
	const char** i;
	for (i = ValidLanguages; (*i) != NULL; ++i)
	{
		if (cstr_eqi((*i), language))
			return 1;
	}
	return 0;
}


/**
 * Set the base directory of the project.
 */
void project_set_base_dir(Project prj, const char* base_dir)
{
	project_set_value(prj, ProjectBaseDir, base_dir);
}


/**
 * Selects a particular configuration; any subsequent calls to retrieve settings will
 * only return values which are part of this configuration. A value of NULL will clear
 * the configuration, and only return global settings.
 */
void project_set_config(Project prj, const char* cfg_name)
{
	assert(prj);
	prj->active_config = cfg_name;
}


/**
 * Set the GUID associated with a project. The GUID is required by the Visual
 * Studio generators, and must be unique per project.
 */
void project_set_guid(Project prj, const char* guid)
{
	project_set_value(prj, ProjectGuid, guid);
}


/**
 * Set the project kind: console, windowed, etc.
 */
void project_set_kind(Project prj, const char* kind)
{
	project_set_value(prj, ProjectKind, kind);
}


/**
 * Set the programming language used by a project.
 */
void project_set_language(Project prj, const char* language)
{
	project_set_value(prj, ProjectLanguage, language);
}


/**
 * Set the output location (the relative path from the base directory to the
 * target output directory) for this project.
 */
void project_set_location(Project prj, const char* location)
{
	project_set_value(prj, ProjectLocation, location);
}


/**
 * Set the name of the project.
 */
void project_set_name(Project prj, const char* name)
{
	project_set_value(prj, ProjectName, name);
}


/**
 * Associate a solution with this project (internal).
 */
void project_set_solution(Project prj, Solution sln)
{
	assert(prj);
	prj->solution = sln;
}


/**
 * Set a string (single value) field on a project, using the field indices.
 */
void project_set_value(Project prj, ProjectField field, const char* value)
{
	assert(prj);
	fields_set_value(prj->fields, field, value);
}


/**
 * Sets the list of values associated with a field. The field will subsequently
 * "own" the list, and take responsibility to destroying it with the field set.
 */
void project_set_values(Project prj, ProjectField field, Strings values)
{
	assert(prj);
	fields_set_values(prj->fields, field, values);
}