@@ -300,8 +300,72 @@ in writing setup scripts:
300
300
Template files
301
301
--------------
302
302
303
- XXX: Describe how files with extensions ``.f.src ``, ``.pyf.src ``,
304
- ``.c.src ``, etc. are pre-processed by the ``build_src `` command.
303
+ NumPy Distutils preprocesses C source files (extension: :file: `.c.src `) written
304
+ in a custom templating language to generate C code. The :c:data: `@ ` symbol is
305
+ used to wrap macro-style variables to empower a string substitution mechansim
306
+ that might describe (for instance) a set of data types.
307
+
308
+ As a more detailed scenario, a loop in the NumPy C source code may
309
+ have a :c:data: `@TYPE@ ` variable, targeted for string substitution,
310
+ which is preprocessed to a number of otherwise identical loops with
311
+ several strings such as :c:data: `INT, LONG, UINT, ULONG `. The :c:data: `@TYPE@ `
312
+ style syntax thus reduces code duplication and maintenance burden by
313
+ mimicking languages that have generic type support. By convention,
314
+ and as required by the preprocessor, generically typed blocks are preceded
315
+ by comment blocks that enumerate the intended string substitutions.
316
+
317
+ The template language blocks are delimited by :c:data: `/**begin repeat `
318
+ and :c:data: `/**end repeat**/ ` lines, which may also be nested using
319
+ consecutively numbered delimiting lines such as :c:data: `/**begin repeat1 `
320
+ and :c:data: `/**end repeat1**/ `. String replacement specifications are started
321
+ and terminated using :c:data: `# `. This may be clearer in the following
322
+ template source example:
323
+
324
+ .. code-block :: C
325
+ :linenos:
326
+ :emphasize-lines: 3, 13, 29, 31
327
+
328
+ /* TIMEDELTA to non-float types */
329
+
330
+ /**begin repeat
331
+ *
332
+ * #TOTYPE = BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG,
333
+ * LONGLONG, ULONGLONG, DATETIME,
334
+ * TIMEDELTA#
335
+ * #totype = npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, npy_uint,
336
+ * npy_long, npy_ulong, npy_longlong, npy_ulonglong,
337
+ * npy_datetime, npy_timedelta#
338
+ */
339
+
340
+ /**begin repeat1
341
+ *
342
+ * #FROMTYPE = TIMEDELTA#
343
+ * #fromtype = npy_timedelta#
344
+ */
345
+ static void
346
+ @FROMTYPE@_to_@TOTYPE@(void *input, void *output, npy_intp n,
347
+ void *NPY_UNUSED(aip), void *NPY_UNUSED(aop))
348
+ {
349
+ const @fromtype@ *ip = input;
350
+ @totype@ *op = output;
351
+
352
+ while (n--) {
353
+ *op++ = (@totype@)*ip++;
354
+ }
355
+ }
356
+ /**end repeat1**/
357
+
358
+ /**end repeat**/
359
+
360
+ The preprocessing of generically typed C source files (whether in NumPy
361
+ proper or in any third party package using NumPy Distutils) is performed
362
+ by `conv_template.py `_.
363
+ The type specific C files generated (extension: :file: `.c `) by these modules
364
+ during the build process are ready to be compiled. This form
365
+ of generic typing is also supported for C header files (preprocessed to
366
+ produce :file: `.h ` files).
367
+
368
+ .. _conv_template.py : https://github.com/numpy/numpy/blob/master/numpy/distutils/conv_template.py
305
369
306
370
Useful functions in ``numpy.distutils.misc_util ``
307
371
-------------------------------------------------
0 commit comments