1create-native-map(1) General Commands Manual create-native-map(1)
2
3
4
6 create-native-map - C/C# Mapping Creator
7
9 create-native-map [OPTIONS]* ASSEMBLY-FILE-NAME OUPUT-PREFIX
10
12 --autoconf-header=HEADER
13 HEADER is a header file name in the syntax typically used with
14 the C #include statement, e.g. #include <stdio.h> or #include
15 "local.h" .
16
17 An Autoconf-formatted macro is generated from the include name,
18 and a #include directive is wrapped within a #ifdef block for
19 the Autoconf macro within the generated .c file.
20
21 For example, --autoconf-header=<stdio.h> would generate the
22 code:
23
24 #ifndef HAVE_STDIO_H
25 #include <stdio.h>
26 #endif /* ndef HAVE_STDIO_H */
27
28
29 --autoconf-member=MEMBER
30 Specify that any access to MEMBER should be wrapped within a
31 #ifdef HAVE_MEMBER block. MEMBER can be either a field-name or
32 a class-name . field-name combination.
33
34 For example, given the C# declaration:
35
36 [Mono.Unix.Native.Map ("struct dirent")]
37 struct Dirent {
38 public long d_off;
39 }
40
41 then --autoconf-member=d_off would generate the code similar to:
42
43 int
44 ToDirent (struct dirent *from, struct Dirent *to)
45 {
46 #ifdef HAVE_STRUCT_DIRENT_D_OFF
47 to->d_off = from->d_off;
48 #endif /* ndef HAVE_STRUCT_DIRENT_D_OFF */
49 }
50
51
52 --exclude-native-symbol=SYMBOL
53 SYMBOL is a [DllImport] -marked method that should not have a
54 prototype generated for it.
55
56 --impl-header=HEADER
57 Insert a #include statement within the generated .c file for
58 HEADER .
59
60 For example, --impl-header=<stdlib.h> generates
61
62 #include <stdlib.h>
63
64
65 --impl-macro=MACRO
66 Insert a #define statement within the generated .c file. MACRO
67 can contain a = to separate the macro name from the macro value.
68
69 For example, --impl-macro=FOO=42 generates
70
71 #define FOO 42
72
73
74 --library=LIBRARY
75 Create prototypes for [DllImport] -marked methods which refer‐
76 ence the native library LIBRARY into the generated .h file.
77
78 --public-header=HEADER
79 Insert a #include statement within the generated .h file for
80 HEADER .
81
82 For example, --public-header=<stdlib.h> generates
83
84 #include <stdlib.h>
85
86
87 --public-macro=MACRO
88 Insert a #define statement within the generated .h file. MACRO
89 can contain a = to separate the macro name from the macro value.
90
91 For example, --public-macro=FOO=42 generates
92
93 #define FOO 42
94
95
96 --rename-member=FROM=TO
97 This is used when FROM is a C macro, and thus must be altered in
98 order to be used sanely. All generated references to the man‐
99 aged representation will use TO instead of FROM .
100
101 For example, given the C# declaration:
102
103 [Mono.Unix.Native.Map ("struct stat")]
104 struct Stat {
105 public long st_atime;
106 }
107
108 and the argument --rename-member=st_atime=st_atime_ , the gener‐
109 ated .h file would contain:
110
111 struct Stat {
112 gint64 st_atime_;
113 };
114
115 (note the altered field name), while the generated .c file would
116 contain:
117
118 ToStat (struct stat *from, struct Stat *to)
119 {
120 to->st_atime_ = from->st_atime;
121 }
122
123
124 --rename-namespace=FROM=TO
125 By default, the C "namespace" (symbol prefix) is the C# names‐
126 pace; types within the C# namespace Mono.Unix.Native would be in
127 the C "namespace" Mono_Unix_Native . Use --rename-namespace to
128 modify the default, e.g. --rename-names‐
129 pace=Mono.Unix.Native=Mono_Posix .
130
132 create-native-map is a program for a specific scenario: keeping code
133 which is tightly coupled between C and C# in sync with each other,
134 based upon the C# types.
135
136 Platform Invoke is only useful if the managed code knows the exact
137 types and layout of all unmanaged structures it uses. This is usually
138 the case on Windows, but it is not the case on Unix. For example,
139 struct stat makes use of types with sizes that will vary from platform
140 to platform (or even based on the compiler macros defined!). For exam‐
141 ple, off_t is usually a signed 32-bit integer on ILP32 platforms, but
142 may be a signed 64-bit integer on LP64 platforms, but may also be a
143 64-bit signed integer on ILP32 platforms if the _FILE_OFFSET_BITS macro
144 has the value 64. In short, everything is flexible within Unix, and
145 managed code can't deal with such flexibility.
146
147 Thus, the niche for create-native-map : assume a fixed ABI that managed
148 code can target, and generate code to "thunk" the managed representa‐
149 tions to the corresponding native representations. This needs to be
150 done for everything that can vary between platforms and compiler flags,
151 from enumeration values ( SIGBUS has the value 10 on FreeBSD but 7 on
152 Linux) to structure members (how big is off_t ?).
153
154 create-native-map will inspect ASSEMBLY-FILE-NAME and output the fol‐
155 lowing files:
156
157 OUTPUT-PREFIX.h
158 Contains enumeration values, class and structure declara‐
159 tions, delegate declarations, and [DllImport] -marked
160 methods (from the library specified by --library ) within
161 the assembly ASSEMBLY-FILE-NAME .
162
163 OUTPUT-PREFIX.c
164 Contains the implementation of enumeration and structure
165 conversion functions.
166
167 OUTPUT-PREFIX.cs
168 Contains a partial class NativeConvert containing enumer‐
169 ation translation methods.
170
171 OUTPUT-PREFIX.xml
172 Generates ECMA XML documentation stubs for the enumera‐
173 tion translation methods in OUTPUT-PREFIX.cs .
174
175 create-native-map primarily looks for MapAttribute -decorated types,
176 and makes use of two MapAttribute properties:
177
178 NativeType
179 Contains the corresponding C type. Only useful if
180 applied to classes, structures, and fields.
181
182 SuppressFlags
183 When specified on an enumeration member of a [Flags]
184 -decorated enumeration type, disables the normal code
185 generator support for bit-masking enumeration types.
186
187 This is useful when bitmask and non-bitmask information
188 is stored within the same type, and bitmask checking
189 shouldn't be used for the non-bitmask values. Example:
190 Mono.Unix.Native.FilePermissions.S_IFREG , which is not a
191 bitmask value, while most of FilePermissions consists of
192 bitmask values ( FilePermissions.S_IRUSR , FilePermis‐
193 sions.S_IWUSR , etc.).
194
195 The MapAttribute attribute can be specified on classes, structures,
196 delegates, fields, and enumerations.
197
198 Delegates
199 Code generation for delegates ignores the MapAt‐
200 tribute.NativeType property, and generates a function pointer
201 typedef that best matches the delegate declaration into the .h
202 file.
203
204 For example,
205
206 namespace Foo {
207 [Map]
208 delegate string MyCallback (string s);
209 }
210
211 generates the typedef :
212
213 typedef char* (*Foo_MyCallback) (const char *s);
214
215
216 Classes and Structures
217 A [Map] -decorated class or structure will get a C structure
218 declaration within the .h file:
219
220 [Map]
221 struct Foo {
222 public int i;
223 }
224
225 becomes
226
227 struct Foo {
228 public int i;
229 };
230
231 If the MapAttribute.NativeType property is set, then conversion
232 functions will be declared within the .h file and created within
233 the .c file:
234
235 namespace Foo {
236 [Map ("struct stat")]
237 struct Stat {
238 public uint st_uid;
239 }
240 }
241
242 becomes
243
244 /* The .h file */
245 struct Foo_Stat {
246 unsigned int st_uid;
247 };
248 int
249 Foo_FromStat (struct Foo_Stat *from, struct stat *to);
250 int
251 Foo_ToStat (struct stat *to, sxtruct Foo_Stat *to);
252
253 /* The .c file */
254 int
255 Foo_FromStat (struct Foo_Stat *from, struct stat *to)
256 {
257 memset (to, 0, sizeof(*to);
258 to->st_uid = from->st_uid;
259 return 0;
260 }
261
262 int
263 Foo_ToStat (struct stat *to, sxtruct Foo_Stat *to)
264 {
265 memset (to, 0, sizeof(*to);
266 to->st_uid = from->st_uid;
267 return 0;
268 }
269
270 For classes, the conversion functions will only copy fields
271 declared in the class itself. Fields declared in parent classes
272 will not be copied. (This is because create-native-map does not
273 know how the inheritance is implemented in C. Therefore copying
274 fields from parent classes is left to the caller of the conver‐
275 sion functions.)
276
277 Fields If a field (1) has the MapAttribute attribute, and (2) has the
278 MapAttribute.NativeType property set, then the specified native
279 type will be used for overflow checking. For example:
280
281 namespace Foo {
282 [Map ("struct stat")]
283 struct Stat {
284 [Map ("off_t")] public long st_size;
285 }
286 }
287
288 generates
289
290 /* The .h file */
291 struct Foo_Stat {
292 gint64 st_size;
293 };
294 int
295 Foo_FromStat (struct Foo_Stat *from, struct stat *to);
296 int
297 Foo_ToStat (struct stat *to, sxtruct Foo_Stat *to);
298
299 /* The .c file */
300 int
301 Foo_FromStat (struct Foo_Stat *from, struct stat *to)
302 {
303 _cnm_return_val_if_overflow (off_t, from->st_size, -1);
304
305 memset (to, 0, sizeof(*to);
306 to->st_size = from->st_size;
307 return 0;
308 }
309
310 int
311 Foo_ToStat (struct stat *to, sxtruct Foo_Stat *to)
312 {
313 _cnm_return_val_if_overflow (gint64, from->st_size, -1);
314
315 memset (to, 0, sizeof(*to);
316 to->st_size = from->st_size;
317 return 0;
318 }
319
320 This is useful for better error checking within the conversion
321 functions. MapAttribute.NativeType is required for this as
322 there is no other way to know what the native type is (without
323 parsing the system header files...).
324
325 Enumerations
326 Generates a C enumeration and macros for each of the members
327 within the enumeration. To and From functions are also declared
328 in the .h file and implemented in the .c file.
329
330 For example,
331
332 namespace Foo {
333 [Map]
334 enum Errno {
335 EINVAL
336 }
337 }
338
339 would generate the following in the .h file:
340
341 enum Foo_Errno {
342 Foo_Errno_EINVAL = 0,
343 #define Foo_Errno_EINVAL Foo_Errno_EINVAL
344 };
345 int Foo_FromErrno (int from, int *to);
346 int Foo_ToErrno (int from, int *to);
347
348 and generates the following in the the .c file:
349
350 int
351 Foo_FromErrno (int from, int *to)
352 {
353 *to = 0;
354 if (from == Foo_Errno_EPERM)
355 #ifdef EINVAL
356 {*to = EINVAL;}
357 #else
358 {errno = EINVAL; return -1;}
359 #endif
360 return 0;
361 }
362
363 int
364 Foo_ToErrno (int from, int *to)
365 {
366 *to = 0;
367 #ifdef EINVAL
368 if (from == EINVAL)
369 {*to = Foo_Errno_EPERM; return 0;}
370 #endif
371 return -1;
372 }
373
374 Different code will be generated if the managed enum is a
375 [Flags] -decorated enumeration (to account for bitwise flags),
376 but this is the basic idea.
377
379 Visit http://lists.ximian.com/mailman/listinfo/mono-devel-list for
380 details.
381
383 Visit http://www.mono-project.com for details
384
385
386
387 create-native-map(1)