Updating to version 1.13. Libgcrypt depends on libgpg-error
[platform/upstream/libgpg-error.git] / src / gen-posix-lock-obj.c
1 /* gen-posix-lock-obj.c - Build tool to construct the lock object.
2    Copyright (C) 2014 g10 Code GmbH
3
4    This file is part of libgpg-error.
5
6    libgpg-error is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public License
8    as published by the Free Software Foundation; either version 2.1 of
9    the License, or (at your option) any later version.
10
11    libgpg-error is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #ifdef HAVE_W32_SYSTEM
25 # error This module may not be build for Windows.
26 #endif
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <pthread.h>
32
33 #include "posix-lock-obj.h"
34
35 #define PGM "gen-posix-lock-obj"
36
37 /* Check that configure did its job.  */
38 #if SIZEOF_PTHREAD_MUTEX_T < 4
39 # error sizeof pthread_mutex_t is not known.
40 #endif
41
42 static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
43
44
45 int
46 main (void)
47 {
48   unsigned char *p;
49   int i;
50   struct {
51     pthread_mutex_t mtx;
52     long vers;
53   } dummyobj;
54
55   if (sizeof mtx != SIZEOF_PTHREAD_MUTEX_T)
56     {
57       fprintf (stderr, PGM ": pthread_mutex_t mismatch\n");
58       exit (1);
59     }
60
61   if (sizeof (dummyobj) != sizeof (_gpgrt_lock_t))
62     {
63       fprintf (stderr, PGM ": internal and external lock object mismatch\n");
64       exit (1);
65     }
66
67   /* To force a probably suitable alignment of the structure we use a
68      union and include a long and a pointer to a long.  */
69   printf ("## lock-obj-pub.%s.h\n"
70           "## File created by " PGM " - DO NOT EDIT\n"
71           "## To be included by mkheader into gpg-error.h\n"
72           "\n"
73           "typedef struct\n"
74           "{\n"
75           "  long _vers;\n"
76           "  union {\n"
77           "    volatile char _priv[%d];\n"
78           "    long _x_align;\n"
79           "    long *_xp_align;\n"
80           "  } u;\n"
81           "} gpgrt_lock_t;\n"
82           "\n"
83           "#define GPGRT_LOCK_INITIALIZER {%d,{{",
84           HOST_TRIPLET_STRING,
85           SIZEOF_PTHREAD_MUTEX_T,
86           LOCK_ABI_VERSION);
87   p = (unsigned char *)&mtx;
88   for (i=0; i < sizeof mtx; i++)
89     {
90       if (i && !(i % 8))
91         printf (" \\\n%*s", 36, "");
92       printf ("%u", p[i]);
93       if (i < sizeof mtx - 1)
94         putchar (',');
95     }
96   fputs ("}}}\n"
97          "##\n"
98          "## Loc" "al Variables:\n"
99          "## mode: c\n"
100          "## buffer-read-only: t\n"
101          "## End:\n"
102          "##\n", stdout);
103
104   if (ferror (stdout))
105     {
106       fprintf (stderr, PGM ": error writing to stdout: %s\n", strerror (errno));
107       return 1;
108     }
109
110   return 0;
111 }