File: native_wrappCommands.awk

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (384 lines) | stat: -rwxr-xr-x 10,841 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/awk -f
#
# This awk script creates the C functions for wrapping the C command structs in:
# rts/ExternalAI/Interface/AISCommands.h
#
# Before running this script, you have to wrapp the native callback struct
# into functions.
#
# This script uses functions from the following files:
# * common.awk
# * commonDoc.awk
# Variables that can be set on the command-line (with -v):
# * GENERATED_SOURCE_DIR        the root generated sources dir
#                               default: "../src-generated/main"
# * NATIVE_GENERATED_SOURCE_DIR the native generated sources dir
#                               default: GENERATED_SOURCE_DIR + "/native"
#
# usage:
# 	awk -f thisScript.awk -f common.awk -f commonDoc.awk
# 	awk -f thisScript.awk -f common.awk -f commonDoc.awk \
#       -v 'NATIVE_GENERATED_SOURCE_DIR=/tmp/build/AI/Interfaces/Java/src-generated/main/native'
#

BEGIN {
	# initialize things

	# define the field splitter(-regex)
	FS = "[ \t]+";

	# These vars can be assigned externally, see file header.
	# Set the default values if they were not supplied on the command line.
	if (!GENERATED_SOURCE_DIR) {
		GENERATED_SOURCE_DIR = "../src-generated/main";
	}
	if (!NATIVE_GENERATED_SOURCE_DIR) {
		NATIVE_GENERATED_SOURCE_DIR = GENERATED_SOURCE_DIR "/native";
	}

	nativeBridge = "CallbackFunctionPointerBridge";
	bridgePrefix = "bridged__";

	indent = "	";

	ind_cmdTopics = 0;
	ind_cmdStructs = 0;
	insideCmdStruct = 0;
}


# Checks if a field is available and is no comment
function isFieldUsable(f) {

	valid = 0;

	if (f && !match(f, /.*\/\/.*/)) {
		valid = 1;
	}

	return valid;
}




function saveMember(ind_mem, member) {

	name = extractParamName(member);
	type_c = extractCType(member);

	cmdsMembers_name[ind_cmdStructs, ind_mem] = name;
	cmdsMembers_type_c[ind_cmdStructs, ind_mem] = type_c;
}


function doWrapp(ind_cmdStructs_dw) {

	doWrp_dw = 1;

	if (match(cmdsName[ind_cmdStructs_dw], /SharedMemArea/)) {
		doWrp_dw = 0;
	} else if (match(cmdsName[ind_cmdStructs_dw], /^SCallLuaRulesCommand$/)) {
		doWrp_dw = 0;
	}

	return doWrp_dw;
}


function createNativeFileName(fileName_fn, isHeader_fn) {

	absFileName_fn = NATIVE_GENERATED_SOURCE_DIR "/" fileName_fn;
	if (isHeader_fn) {
		absFileName_fn = absFileName_fn ".h";
	} else {
		absFileName_fn = absFileName_fn ".c";
	}

	return absFileName_fn;
}


function printNativeFP2F() {

	outFile_nh = createNativeFileName(nativeBridge, 1);
	outFile_nc = createNativeFileName(nativeBridge, 0);

	print("// END: COMMAND_WRAPPERS") >> outFile_nh;
	print("") >> outFile_nh;
	print("") >> outFile_nc;

	# print the command wrapping functions
	for (cmdIndex=0; cmdIndex < ind_cmdStructs; cmdIndex++) {
		topicName  = cmdsTopicName[cmdIndex];
		topicValue = cmdsTopicNameValue[topicName];
		name       = cmdsName[cmdIndex];
		metaInf    = cmdsMetaInfo[cmdIndex];
		fullName   = metaInf;
		sub(/ .*$/, "", fullName);
		sub(/^[^ \t]*/, "", metaInf);

		hasRetType = 0;
		retType    = "int";
		retParam   = "";
		paramList  = "int _skirmishAIId";
		firstMember = 0;
		if (cmdsNumMembers[cmdIndex] > 0) {
			for (m=firstMember; m < cmdsNumMembers[cmdIndex]; m++) {
				memName   = cmdsMembers_name[cmdIndex, m];
				memType_c = cmdsMembers_type_c[cmdIndex, m];

				if (match(memName, /^ret_/) && !match(memType_c, /\*/)) {
					retParam   = memName;
					retType    = memType_c;
					hasRetType = 1;
					# rewrite the meta info
					sub(memName, "RETURN", metaInf);
				} else {
					paramList = paramList ", " memType_c " " memName;
				}
			}
		}
		if (!hasRetType) {
			metaInf = metaInf " error-return:0=OK";
		}
		paramListNoTypes = removeParamTypes(paramList);
		metaInf = trim(metaInf);


		if (doWrapp(cmdIndex)) {
			# print function declaration to *.h

			# Add the member comments to the main comment as @param attributes
			numCmdDocLines = cmdsDocComment[cmdIndex, "*"];
			for (m=firstMember; m < cmdsNumMembers[cmdIndex]; m++) {
				numLines = cmdMbrsDocComments[cmdIndex*1000 + m, "*"];
				if (numLines > 0) {
					cmdsDocComment[cmdIndex, numCmdDocLines] = "@param " cmdsMembers_name[cmdIndex, m];
				}
				for (l=0; l < numLines; l++) {
					if (l == 0) {
						_preDocLine = "@param " cmdsMembers_name[cmdIndex, m] "  ";
					} else {
						_preDocLine = "       " lengtAsSpaces(cmdsMembers_name[cmdIndex, m]) "  ";
					}
					cmdsDocComment[cmdIndex, numCmdDocLines] = _preDocLine cmdMbrsDocComments[cmdIndex*1000 + m, l];
					numCmdDocLines++;
				}
			}
			cmdsDocComment[cmdIndex, "*"] = numCmdDocLines;

			outName = bridgePrefix fullName;

			commentEol = "";
			if (metaInf != "") {
				commentEol = " // " metaInf;
			}

			if (match(fullName, /^Unit_/)) {
				# To make this fit in smoothly with the OO structure,
				# we want to present each unit command once for the unit class
				# and once for the Group class.

				# An other thing we do, is move the common UnitAICommand params
				# to the end of the params list, so we can supply default values
				# for them more easily later on.
				paramList_commonEnd = paramList;
				sub(/, short options, int timeOut/, "", paramList_commonEnd);
				paramList_commonEnd = paramList_commonEnd ", short options, int timeOut";
				
				# Unit version:
				paramList_unit = paramList_commonEnd;
				sub(/int groupId, /, "", paramList_unit);
				printFunctionComment_Common(outFile_nh, cmdsDocComment, cmdIndex, "");
				print("EXPORT(" retType ") " outName "(" paramList_unit ");" commentEol) >> outFile_nh;
				print("") >> outFile_nh;
				
				# Group version:
				paramList_group = paramList_commonEnd;
				sub(/int unitId, /, "", paramList_group);
				outName_group = outName;
				sub(/Unit_/, "Group_", outName_group);
				printFunctionComment_Common(outFile_nh, cmdsDocComment, cmdIndex, "");
				print("EXPORT(" retType ") " outName_group "(" paramList_group ");" commentEol) >> outFile_nh;
			} else {
				printFunctionComment_Common(outFile_nh, cmdsDocComment, cmdIndex, "");
				print("EXPORT(" retType ") " outName "(" paramList ");" commentEol) >> outFile_nh;
			}
			print("") >> outFile_nh;

			# print function definition to *.c
			print("") >> outFile_nc;
			if (match(fullName, /^Unit_/)) {
				# inner version:
				print("static " retType " _" outName "(" paramList ") {") >> outFile_nc;
			} else {
				print("EXPORT(" retType ") " outName "(" paramList ") {") >> outFile_nc;
			}
			print("") >> outFile_nc;

			print("\t" "struct S" name "Command commandData;") >> outFile_nc;
			for (m=firstMember; m < cmdsNumMembers[cmdIndex]; m++) {
				memName   = cmdsMembers_name[cmdIndex, m];
				memType_c = cmdsMembers_type_c[cmdIndex, m];

				if (memName == retParam) {
					# do nothing
				} else {
					print("\t" "commandData." memName " = " memName ";") >> outFile_nc;
				}
			}
			print("") >> outFile_nc;

			print("\t" "int _ret = id_clb[_skirmishAIId]->Engine_handleCommand(_skirmishAIId, COMMAND_TO_ID_ENGINE, -1, " topicName ", &commandData);") >> outFile_nc;
			print("") >> outFile_nc;

			if (hasRetType) {
				# this is unused, delete
				print("\t" "if (_ret == 0) {") >> outFile_nc;
				print("\t\t" "_ret = commandData." retParam ";") >> outFile_nc;
				print("\t" "} else {") >> outFile_nc;
				print("\t\t" "_ret = 0;") >> outFile_nc;
				print("\t" "}") >> outFile_nc;
			}

			print("\t" "return _ret;") >> outFile_nc;
			print("}") >> outFile_nc;

			if (match(fullName, /^Unit_/)) {
				paramListNoTypes = removeParamTypes(paramList);

				# Unit version:
				print("") >> outFile_nc;
				print("EXPORT(" retType ") " outName "(" paramList_unit ") {" commentEol) >> outFile_nc;
				print("") >> outFile_nc;
				print("\t" "const int groupId = -1;") >> outFile_nc;
				print("\t" "return _" outName "(" paramListNoTypes ");") >> outFile_nc;
				print("}") >> outFile_nc;
				print("") >> outFile_nc;
	
				# Group version:
				print("EXPORT(" retType ") " outName_group "(" paramList_group ") {" commentEol) >> outFile_nc;
				print("") >> outFile_nc;
				print("\t" "const int unitId = -1;") >> outFile_nc;
				print("\t" "return _" outName "(" paramListNoTypes ");") >> outFile_nc;
				print("}") >> outFile_nc;
			}
		} else {
			print("Java-AIInterface: NOTE: native level: Commands: intentionally not wrapped: " fullName);
		}
	}

	print("// END: COMMAND_WRAPPERS") >> outFile_nh;
	print("") >> outFile_nh;
	print("#ifdef __cplusplus") >> outFile_nh;
	print("} // extern \"C\"") >> outFile_nh;
	print("#endif") >> outFile_nh;
	print("") >> outFile_nh;
	print("#endif // __CALLBACK_FUNCTION_POINTER_BRIDGE_H") >> outFile_nh;
	print("") >> outFile_nh;

	print("") >> outFile_nc;

	close(outFile_nh);
	close(outFile_nc);
}






# aggare te los command defines in order
/^[ \t]*COMMAND_.*$/ {

	sub(",", "", $4);
	cmdsTopicNameValue[$2] = $4;
}



# This function has to return true (1) if a doc comment (eg: /** foo bar */)
# can be deleted.
# If there is no special condition you want to apply,
# it should always return true (1),
# cause there are additional mechanism to prevent accidential deleting.
# see: commonDoc.awk
function canDeleteDocumentation() {
	return isInsideCmdStruct != 1;
}

################################################################################
### BEGINN: parsing and saving the command structs

# end of struct S*Command
/^}; \/\/\$ COMMAND_.*$/ {

	cmdsNumMembers[ind_cmdStructs] = ind_cmdMember;
	cmdsTopicName[ind_cmdStructs]  = $3;
	metaInfo = $0;
	sub(/^}; \/\/\$ COMMAND_[^ \t]+/, "", metaInfo);
	metaInfo = trim(metaInfo);
	cmdsMetaInfo[ind_cmdStructs]   = metaInfo;

	if (doWrapp(ind_cmdStructs)) {
		#printCommandJava(ind_cmdStructs);
	}

	ind_cmdStructs++;
	isInsideCmdStruct = 0;
}


# inside of struct S*Command
{
	if (isInsideCmdStruct == 1) {
		size_tmpMembers = split($0, tmpMembers, ";");
		# cause there is an empty part behind the ';'
		size_tmpMembers--;
		for (i=1; i<=size_tmpMembers; i++) {
			tmpMembers[i] = trim(tmpMembers[i]);
			if (tmpMembers[i] == "" || match(tmpMembers[i], /^\/\//)) {
				break;
			}
			# This would bork with more then 1000 members in a command,
			# or more then 1000 commands
			storeDocLines(cmdMbrsDocComments, ind_cmdStructs*1000 + ind_cmdMember);
			saveMember(ind_cmdMember, tmpMembers[i]);
			ind_cmdMember++;
		}
	}
}

# beginn of struct S*Command
/^struct S.*Command( \{)?/ {

	isInsideCmdStruct = 1;
	ind_cmdMember = 0;
	commandName = $2;
	sub(/^S/, "", commandName);
	sub(/Command$/, "", commandName);

	isUnitCommand = match(commandName, /.*Unit$/);

	cmdsIsUnitCmd[ind_cmdStructs] = isUnitCommand;
	cmdsName[ind_cmdStructs] = commandName;
	storeDocLines(cmdsDocComment, ind_cmdStructs);
}

# find COMMAND_TO_ID_ENGINE id
/COMMAND_TO_ID_ENGINE/ {

	cmdToIdEngine = $3;
}

### END: parsing and saving the command structs
################################################################################



END {
	# finalize things

	printNativeFP2F()
}