aboutsummaryrefslogtreecommitdiff
path: root/devtools/create_project/xcode.h
blob: a32e6679377c513d4dd23c2d1089fc151f8c66dc (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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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 the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifndef TOOLS_CREATE_PROJECT_XCODE_H
#define TOOLS_CREATE_PROJECT_XCODE_H

#include "create_project.h"

#include <algorithm>
#include <vector>

namespace CreateProjectTool {

class XcodeProvider : public ProjectProvider {
public:
	XcodeProvider(StringList &global_warnings, std::map<std::string, StringList> &project_warnings, const int version = 0);

protected:

	void createWorkspace(const BuildSetup &setup);

	void createOtherBuildFiles(const BuildSetup &setup);

	void addResourceFiles(const BuildSetup &setup, StringList &includeList, StringList &excludeList);

	void createProjectFile(const std::string &name, const std::string &uuid, const BuildSetup &setup, const std::string &moduleDir,
	                       const StringList &includeList, const StringList &excludeList);

	void writeFileListToProject(const FileNode &dir, std::ofstream &projectFile, const int indentation,
	                            const StringList &duplicate, const std::string &objPrefix, const std::string &filePrefix);
private:
	enum {
		kSettingsAsList        = 0x01,
		kSettingsSingleItem    = 0x02,
		kSettingsNoQuote       = 0x04,
		kSettingsQuoteVariable = 0x08,
		kSettingsNoValue       = 0x10
	};

	// File properties
	struct FileProperty {
		std::string _fileEncoding;
		std::string _lastKnownFileType;
		std::string _fileName;
		std::string _filePath;
		std::string _sourceTree;

		FileProperty(const std::string &fileType = "", const std::string &name = "", const std::string &path = "", const std::string &source = "")
				: _fileEncoding(""), _lastKnownFileType(fileType), _fileName(name), _filePath(path), _sourceTree(source) {
		}
	};

	//////////////////////////////////////////////////////////////////////////
	// XCObject and children
	typedef std::vector<std::string> ValueList;

	struct Entry {
		std::string _value;
		std::string _comment;

		Entry(std::string val, std::string cmt) : _value(val), _comment(cmt) {}
	};

	typedef std::vector<Entry> EntryList;

	struct Setting {
		EntryList _entries;
		int _flags;
		int _indent;
		int _order;

		Setting(std::string value = "", std::string comment = "", int flgs = 0, int idt = 0, int ord = -1) : _flags(flgs), _indent(idt), _order(ord) {
			_entries.push_back(Entry(value, comment));
		}

		Setting(ValueList values, int flgs = 0, int idt = 0, int ord = -1) : _flags(flgs), _indent(idt), _order(ord) {
			for (unsigned int i = 0; i < values.size(); i++)
				_entries.push_back(Entry(values[i], ""));
		}

		Setting(EntryList ents, int flgs = 0, int idt = 0, int ord = -1) : _entries(ents), _flags(flgs), _indent(idt), _order(ord) {}

		void addEntry(std::string value, std::string comment = "") {
			_entries.push_back(Entry(value, comment));
		}
	};

	typedef std::map<std::string, Setting> SettingList;
	typedef std::pair<std::string, Setting> SettingPair;
	typedef std::vector<SettingPair> OrderedSettingList;

	static bool OrderSortPredicate(const SettingPair &s1, const SettingPair &s2) {
		return s1.second._order < s2.second._order;
	}

	struct Property {
	public:
		SettingList _settings;
		int _flags;
		bool _hasOrder;

		Property() : _flags(0), _hasOrder(false) {}

		// Constructs a simple Property
		Property(std::string name, std::string value = "", std::string comment = "", int flgs = 0, int indent = 0, bool order = false) : _flags(flgs), _hasOrder(order) {
			_settings[name] = Setting(value, comment, _flags, indent);
		}

		Property(std::string name, ValueList values, int flgs = 0, int indent = 0, bool order = false) : _flags(flgs), _hasOrder(order) {
			_settings[name] = Setting(values, _flags, indent);
		}

		OrderedSettingList getOrderedSettingList() {
			OrderedSettingList list;

			// Prepare vector to sort
			for (SettingList::const_iterator setting = _settings.begin(); setting != _settings.end(); ++setting)
				list.push_back(SettingPair(setting->first, setting->second));

			// Sort vector using setting order
			if (_hasOrder)
				std::sort(list.begin(), list.end(), OrderSortPredicate);

			return list;
		}
	};

	typedef std::map<std::string, Property> PropertyList;

	// Main object struct
	// This is all a big hack unfortunately, but making everything all properly abstracted would
	// be overkill since we only have to generate a single project
	struct Object {
	public:
		std::string _id;                // Unique identifier for this object
		std::string _name;              // Name (may not be unique - for ex. configuration entries)
		std::string _refType;           // Type of object this references (if any)
		std::string _comment;           // Main comment (empty for no comment)

		PropertyList _properties;       // List of object properties, including output configuration

		// Constructs an object and add a default type property
		Object(XcodeProvider *objectParent, std::string objectId, std::string objectName, std::string objectType, std::string objectRefType = "", std::string objectComment = "")
		    : _id(objectId), _name(objectName), _refType(objectRefType), _comment(objectComment), _parent(objectParent) {
			assert(objectParent);
			assert(!objectId.empty());
			assert(!objectName.empty());
			assert(!objectType.empty());

			addProperty("isa", objectType, "", kSettingsNoQuote | kSettingsNoValue);
		}

		// Add a simple Property with just a name and a value
		void addProperty(std::string propName, std::string propValue, std::string propComment = "", int propFlags = 0, int propIndent = 0) {
			_properties[propName] = Property(propValue, "", propComment, propFlags, propIndent);
		}

		std::string toString(int flags = 0) {
			std::string output;
			output = "\t\t" + _parent->getHash(_id) + (_comment.empty() ? "" : " /* " + _comment + " */") + " = {";

			if (flags & kSettingsAsList)
				output += "\n";

			// Special case: always output the isa property first
			output += _parent->writeProperty("isa", _properties["isa"], flags);

			// Write each property
			for (PropertyList::iterator property = _properties.begin(); property != _properties.end(); ++property) {
				if (property->first == "isa")
					continue;

				output += _parent->writeProperty(property->first, property->second, flags);
			}

			if (flags & kSettingsAsList)
				output += "\t\t";

			output += "};\n";

			return output;
		}

		// Slight hack, to allow Group access to parent.
	protected:
		XcodeProvider *_parent;
	private:
		// Returns the type property (should always be the first in the properties map)
		std::string getType() {
			assert(!_properties.empty());
			assert(!_properties["isa"]._settings.empty());

			SettingList::iterator it = _properties["isa"]._settings.begin();

			return it->first;
		}
	};

	struct ObjectList {
	private:
		std::map<std::string, bool> _objectMap;

	public:
		std::vector<Object *> _objects;
		std::string _comment;
		int _flags;

		void add(Object *obj) {
			std::map<std::string, bool>::iterator it = _objectMap.find(obj->_id);
			if (it != _objectMap.end() && it->second == true)
				return;

			_objects.push_back(obj);
			_objectMap[obj->_id] = true;
		}

		Object *find(std::string id) {
			for (std::vector<Object *>::iterator it = _objects.begin(); it != _objects.end(); ++it) {
				if ((*it)->_id == id) {
					return *it;
				}
			}
			return NULL;
		}

		std::string toString() {
			std::string output;

			if (!_comment.empty())
				output = "\n/* Begin " + _comment + " section */\n";

			for (std::vector<Object *>::iterator object = _objects.begin(); object != _objects.end(); ++object)
				output += (*object)->toString(_flags);

			if (!_comment.empty())
				output += "/* End " + _comment + " section */\n";

			return output;
		}
	};

	// A class to maintain a folder-reference group-hierarchy, which together with the functionality below
	// allows for breaking up sub-paths into a chain of groups. This helps with merging engines into the
	// overall group-layout.
	class Group : public Object {
		int _childOrder;
		std::map<std::string, Group *> _childGroups;
		std::string _treeName;
		void addChildInternal(const std::string &id, const std::string &comment);
	public:
		Group(XcodeProvider *objectParent, const std::string &groupName, const std::string &uniqueName, const std::string &path);
		void addChildFile(const std::string &name);
		void addChildByHash(const std::string &hash, const std::string &name);
		// Should be passed the hash for the entry
		void addChildGroup(const Group *group);
		void ensureChildExists(const std::string &name);
		Group *getChildGroup(const std::string &name);
		std::string getHashRef() const { return _parent->getHash(_id); }
	};

	// The path used by the root-source group
	std::string _projectRoot;
	// The base source group, currently also re-purposed for containing the various support-groups.
	Group *_rootSourceGroup;
	// Helper function to create the chain of groups for the various subfolders. Necessary as
	// create_project likes to start in engines/
	Group *touchGroupsForPath(const std::string &path);
	// Functionality for adding file-refs and build-files, as Group-objects need to be able to do this.
	void addFileReference(const std::string &id, const std::string &name, FileProperty properties);
	void addProductFileReference(const std::string &id, const std::string &name);
	void addBuildFile(const std::string &id, const std::string &name, const std::string &fileRefId, const std::string &comment);
	// All objects
	std::map<std::string, std::string> _hashDictionnary;
	ValueList _defines;

	// Targets
	ValueList _targets;

	// Lists of objects
	ObjectList _buildFile;
	ObjectList _copyFilesBuildPhase;
	ObjectList _fileReference;
	ObjectList _frameworksBuildPhase;
	ObjectList _groups;
	ObjectList _nativeTarget;
	ObjectList _project;
	ObjectList _resourcesBuildPhase;
	ObjectList _sourcesBuildPhase;
	ObjectList _buildConfiguration;
	ObjectList _configurationList;

	void outputMainProjectFile(const BuildSetup &setup);

	// Setup objects
	void setupCopyFilesBuildPhase();
	void setupFrameworksBuildPhase(const BuildSetup &setup);
	void setupNativeTarget();
	void setupProject();
	void setupResourcesBuildPhase();
	void setupSourcesBuildPhase();
	void setupBuildConfiguration(const BuildSetup &setup);
	void setupImageAssetCatalog(const BuildSetup &setup);
	void setupAdditionalSources(std::string targetName, Property &files, int &order);

	// Misc
	void setupDefines(const BuildSetup &setup); // Setup the list of defines to be used on build configurations

	// Retrieve information
	ValueList& getResourceFiles() const;

	// Hash generation
	std::string getHash(std::string key);
#ifdef MACOSX
	std::string md5(std::string key);
#endif
	std::string newHash() const;

	// Output
	std::string writeProperty(const std::string &variable, Property &property, int flags = 0) const;
	std::string writeSetting(const std::string &variable, std::string name, std::string comment = "", int flags = 0, int indent = 0) const;
	std::string writeSetting(const std::string &variable, const Setting &setting) const;
};

} // End of CreateProjectTool namespace

#endif // TOOLS_CREATE_PROJECT_XCODE_H