Imported Upstream version 0.1.0+git20131207+e452e83
[deb_libhybris.git] / utils / generate_wrapper_macros.py
1 #!/usr/bin/python
2 #
3 # Generate wrapper macros: hybris/include/hybris/internal/binding.h
4 #
5 # Usage:
6 # python utils/generate_wrapper_macros.py >hybris/include/hybris/internal/binding.h
7 #
8 # Copyright (C) 2013 Jolla Ltd.
9 # Contact: Thomas Perl <thomas.perl@jollamobile.com>
10 #
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions are met:
13 #
14 # 1. Redistributions of source code must retain the above copyright notice, this
15 # list of conditions and the following disclaimer.
16 # 2. Redistributions in binary form must reproduce the above copyright notice,
17 # this list of conditions and the following disclaimer in the documentation
18 # and/or other materials provided with the distribution.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #
31
32 # Maximum number of arguments to generate wrapper macros for
33 MAX_ARGS = 20
34
35 BEGIN, END = '{', '}'
36
37 AUTO_GENERATED_WARNING = """
38 /**
39 * XXX AUTO-GENERATED FILE XXX
40 *
41 * Do not edit this file directly, but update the templates in
42 * utils/generate_wrapper_macros.py and run it again to build
43 * an updated version of this header file:
44 *
45 * python utils/generate_wrapper_macros.py > \\
46 * hybris/include/hybris/internal/binding.h
47 *
48 * If you need macros with more arguments, just customize the
49 * MAX_ARGS variable in generate_wrapper_macros.py.
50 *
51 * XXX AUTO-GENERATED FILE XXX
52 **/
53 """
54
55 print """
56 /**
57 * Copyright (C) 2013 Simon Busch <morphis@gravedo.de>
58 * 2012 Canonical Ltd
59 * 2013 Jolla Ltd.
60 *
61 * Auto-generated via "generate_wrapper_macros.py"
62 *
63 * Licensed under the Apache License, Version 2.0 (the "License");
64 * you may not use this file except in compliance with the License.
65 * You may obtain a copy of the License at
66 *
67 * http://www.apache.org/licenses/LICENSE-2.0
68 *
69 * Unless required by applicable law or agreed to in writing, software
70 * distributed under the License is distributed on an "AS IS" BASIS,
71 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
72 * See the License for the specific language governing permissions and
73 * limitations under the License.
74 **/
75
76 #ifndef HYBRIS_BINDING_H_
77 #define HYBRIS_BINDING_H_
78
79 /* floating_point_abi.h defines FP_ATTRIB */
80 #include <hybris/internal/floating_point_abi.h>
81
82 void *android_dlopen(const char *filename, int flag);
83 void *android_dlsym(void *name, const char *symbol);
84 int android_dlclose(void *handle);
85 const char *android_dlerror(void);
86 int android_dladdr(const void *addr, void *info);
87 """
88
89 print AUTO_GENERATED_WARNING
90
91 print """
92 #define HYBRIS_DLSYSM(name, fptr, sym) \\
93 if (!name##_handle) \\
94 hybris_##name##_initialize(); \\
95 if (*(fptr) == NULL) \\
96 { \\
97 *(fptr) = (void *) android_dlsym(name##_handle, sym); \\
98 }
99
100 #define HYBRIS_LIBRARY_INITIALIZE(name, path) \\
101 void *name##_handle; \\
102 void hybris_##name##_initialize() \\
103 { \\
104 name##_handle = android_dlopen(path, RTLD_LAZY); \\
105 }
106
107 """
108
109 for count in range(MAX_ARGS):
110 args = ['a%d' % (x+1) for x in range(count)]
111 names = ['n%d' % (x+1) for x in range(count)]
112 wrapper_signature = ', '.join(['name', 'return_type', 'symbol'] + args)
113 signature = ', '.join(args)
114 signature_with_names = ', '.join(' '.join(x) for x in zip(args, names))
115 call_names = ', '.join(names)
116
117 print """
118 #define HYBRIS_IMPLEMENT_FUNCTION{count}({wrapper_signature}) \\
119 return_type symbol({signature_with_names}) \\
120 {BEGIN} \\
121 static return_type (*f)({signature}) FP_ATTRIB = NULL; \\
122 HYBRIS_DLSYSM(name, &f, #symbol); \\
123 return f({call_names}); \\
124 {END}
125 """.format(**locals())
126
127 for count in range(MAX_ARGS):
128 args = ['a%d' % (x+1) for x in range(count)]
129 names = ['n%d' % (x+1) for x in range(count)]
130 wrapper_signature = ', '.join(['name', 'symbol'] + args)
131 signature = ', '.join(args)
132 signature_with_names = ', '.join(' '.join(x) for x in zip(args, names))
133 call_names = ', '.join(names)
134 print """
135 #define HYBRIS_IMPLEMENT_VOID_FUNCTION{count}({wrapper_signature}) \\
136 void symbol({signature_with_names}) \\
137 {BEGIN} \\
138 static void (*f)({signature}) FP_ATTRIB = NULL; \\
139 HYBRIS_DLSYSM(name, &f, #symbol); \\
140 f({call_names}); \\
141 {END}
142 """.format(**locals())
143
144 # Print it again, so people wanting to append new macros will see it
145 print AUTO_GENERATED_WARNING
146
147 print """
148 #endif /* HYBRIS_BINDING_H_ */
149 """
150