|
| 1 | +""" SoLoud D wrapper generator """ |
| 2 | + |
| 3 | +import soloud_codegen |
| 4 | + |
| 5 | +fo = open("binddynamic.d", "w") |
| 6 | + |
| 7 | +C_TO_D_TYPES = { |
| 8 | + "string":"string", |
| 9 | + "int":"int", |
| 10 | + "void":"void", |
| 11 | + "const char *":"const(char)*", |
| 12 | + "char *":"char*", |
| 13 | + "unsigned int":"uint", |
| 14 | + "float":"float", |
| 15 | + "double":"double", |
| 16 | + "float *":"float[]", |
| 17 | + "File *":"SoloudObject", |
| 18 | + "unsigned char *":"ubyte*", |
| 19 | + "unsigned char":"ubyte", |
| 20 | + "short *":"short[]" |
| 21 | +} |
| 22 | + |
| 23 | +for soloud_type in soloud_codegen.soloud_type: |
| 24 | + C_TO_D_TYPES[soloud_type + " *"] = "SoloudObject" |
| 25 | + |
| 26 | +def has_ex_variant(funcname): |
| 27 | + """ Checks if this function has an "Ex" variant """ |
| 28 | + if funcname[-2::] == "Ex": |
| 29 | + # Already an Ex.. |
| 30 | + return False |
| 31 | + for func in soloud_codegen.soloud_func: |
| 32 | + if func[1] == (funcname + "Ex"): |
| 33 | + return True |
| 34 | + return False |
| 35 | + |
| 36 | +# Forward declare ALL THE CLASSES |
| 37 | +for soloud_type in soloud_codegen.soloud_type: |
| 38 | + #fo.write("public class %s;\n"%(soloud_type)) |
| 39 | + pass |
| 40 | + |
| 41 | +def external_pointer_fix(param): |
| 42 | + if param == "SoloudObject": |
| 43 | + return "int*" |
| 44 | + return param |
| 45 | + |
| 46 | +function_signatures = "" |
| 47 | +function_decls = "" |
| 48 | +function_loaders = "" |
| 49 | + |
| 50 | +for x in soloud_codegen.soloud_type: |
| 51 | + first = True |
| 52 | + for y in soloud_codegen.soloud_func: |
| 53 | + if (x + "_") == y[1][0:len(x)+1:]: |
| 54 | + if first: |
| 55 | + # Declare creator and destroyer |
| 56 | + function_signatures += (' alias @nogc nothrow int* function() da_%s_create;\n'%(x)) |
| 57 | + function_decls += (' da_%s_create %s_create;\n'%(x, x)) |
| 58 | + function_loaders += (' lib.bindSymbol(cast(void**)&%s_create, \"%s_create\");\n'%(x, x)) |
| 59 | + |
| 60 | + function_signatures += (' alias @nogc nothrow int* function(int* aObjHandle) da_%s_destroy;\n'%(x)) |
| 61 | + function_decls += (' da_%s_destroy %s_destroy;\n'%(x, x)) |
| 62 | + function_loaders += (' lib.bindSymbol(cast(void**)&%s_destroy, \"%s_destroy\");\n'%(x, x)) |
| 63 | + |
| 64 | + first = False |
| 65 | + |
| 66 | + funcname = y[1][len(x)+1::] |
| 67 | + # If the function has the name "Ex", remove the subfix |
| 68 | + if funcname[-2::] == "Ex": |
| 69 | + funcname = funcname[:len(funcname)-2] |
| 70 | + # Skip generating functions that have an Ex variant |
| 71 | + if funcname == "create" or funcname == "destroy" or has_ex_variant(y[1]): |
| 72 | + pass # omit create/destroy, handled by __exit__ / close |
| 73 | + else: |
| 74 | + ret = C_TO_D_TYPES[y[0]] |
| 75 | + function_signatures += (' alias @nogc nothrow %s function(int* aObjHandle'%(ret)) |
| 76 | + |
| 77 | + for z in y[2]: |
| 78 | + if len(z) > 1: |
| 79 | + if z[1] == 'a'+x: |
| 80 | + pass # skip the 'self' pointer |
| 81 | + else: |
| 82 | + function_signatures += ', ' |
| 83 | + function_signatures += external_pointer_fix(C_TO_D_TYPES[z[0]]) + ' ' + z[1] |
| 84 | + function_signatures += ') da_%s;\n' % y[1] |
| 85 | + |
| 86 | + function_decls += ' da_%s %s;\n' % (y[1], y[1]) |
| 87 | + function_loaders += (' lib.bindSymbol(cast(void**)&%s, \"%s\");\n'%(y[1], y[1])) |
| 88 | + |
| 89 | +fo.write( |
| 90 | +"""/* |
| 91 | +Boost Software License - Version 1.0 - August 17th, 2003 |
| 92 | +
|
| 93 | +Permission is hereby granted, free of charge, to any person or organization |
| 94 | +obtaining a copy of the software and accompanying documentation covered by |
| 95 | +this license (the "Software") to use, reproduce, display, distribute, |
| 96 | +execute, and transmit the Software, and to prepare derivative works of the |
| 97 | +Software, and to permit third-parties to whom the Software is furnished to |
| 98 | +do so, all subject to the following: |
| 99 | +
|
| 100 | +The copyright notices in the Software and this entire statement, including |
| 101 | +the above license grant, this restriction and the following disclaimer, |
| 102 | +must be included in all copies of the Software, in whole or in part, and |
| 103 | +all derivative works of the Software, unless such copies or derivative |
| 104 | +works are solely in the form of machine-executable object code generated by |
| 105 | +a source language processor. |
| 106 | +
|
| 107 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 108 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 109 | +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
| 110 | +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
| 111 | +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
| 112 | +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 113 | +DEALINGS IN THE SOFTWARE. |
| 114 | +*/ |
| 115 | +
|
| 116 | +module bindbc.soloud.binddynamic; |
| 117 | +
|
| 118 | +import bindbc.loader; |
| 119 | +import bindbc.soloud.types; |
| 120 | +""" |
| 121 | +) |
| 122 | + |
| 123 | +fo.write( |
| 124 | +""" |
| 125 | +extern(C) |
| 126 | +{ |
| 127 | +%s |
| 128 | +} |
| 129 | +""" % function_signatures |
| 130 | +) |
| 131 | + |
| 132 | +fo.write( |
| 133 | +""" |
| 134 | +__gshared |
| 135 | +{ |
| 136 | +%s |
| 137 | +} |
| 138 | +""" % function_decls |
| 139 | +) |
| 140 | + |
| 141 | +fo.write( |
| 142 | +""" |
| 143 | +private |
| 144 | +{ |
| 145 | + SharedLib lib; |
| 146 | + SLSupport loadedVersion; |
| 147 | +} |
| 148 | +
|
| 149 | +void unloadOpenCL() |
| 150 | +{ |
| 151 | + if (lib != invalidHandle) |
| 152 | + { |
| 153 | + lib.unload(); |
| 154 | + } |
| 155 | +} |
| 156 | +
|
| 157 | +SLSupport loadedSoloudVersion() { return loadedVersion; } |
| 158 | +bool isSoloudLoaded() { return lib != invalidHandle; } |
| 159 | +
|
| 160 | +SLSupport loadSoloud() |
| 161 | +{ |
| 162 | + version(Windows) |
| 163 | + { |
| 164 | + version(X86) |
| 165 | + { |
| 166 | + const(char)[][2] libNames = |
| 167 | + [ |
| 168 | + \"soloud.dll\", |
| 169 | + \"soloud_x86.dll\" |
| 170 | + ]; |
| 171 | + } |
| 172 | + else version(X86_64) |
| 173 | + { |
| 174 | + const(char)[][2] libNames = |
| 175 | + [ |
| 176 | + \"soloud.dll\", |
| 177 | + \"soloud_x64.dll\" |
| 178 | + ]; |
| 179 | + } |
| 180 | + } |
| 181 | + else version(OSX) |
| 182 | + { |
| 183 | + const(char)[][1] libNames = |
| 184 | + [ |
| 185 | + \"libsoloud.dylib\" |
| 186 | + ]; |
| 187 | + } |
| 188 | + else version(Posix) |
| 189 | + { |
| 190 | + const(char)[][2] libNames = |
| 191 | + [ |
| 192 | + \"libsoloud.so.1\", |
| 193 | + \"libsoloud.so\" |
| 194 | + ]; |
| 195 | + } |
| 196 | + else static assert(0, \"bindbc-soloud is not yet supported on this platform.\"); |
| 197 | +
|
| 198 | + SLSupport ret; |
| 199 | + foreach(name; libNames) |
| 200 | + { |
| 201 | + ret = loadSoloud(name.ptr); |
| 202 | + if (ret != SLSupport.noLibrary) |
| 203 | + break; |
| 204 | + } |
| 205 | + return ret; |
| 206 | +} |
| 207 | +
|
| 208 | +""" |
| 209 | +) |
| 210 | + |
| 211 | +fo.write( |
| 212 | +""" |
| 213 | +SLSupport loadSoloud(const(char)* libName) |
| 214 | +{ |
| 215 | + lib = load(libName); |
| 216 | + if(lib == invalidHandle) |
| 217 | + { |
| 218 | + return SLSupport.noLibrary; |
| 219 | + } |
| 220 | +
|
| 221 | + auto errCount = errorCount(); |
| 222 | + loadedVersion = SLSupport.badLibrary; |
| 223 | +
|
| 224 | + %s |
| 225 | + |
| 226 | + loadedVersion = SLSupport.sl20181119; |
| 227 | +
|
| 228 | + if (errorCount() != errCount) |
| 229 | + return SLSupport.badLibrary; |
| 230 | +
|
| 231 | + return loadedVersion; |
| 232 | +} |
| 233 | +""" % function_loaders |
| 234 | +) |
| 235 | + |
| 236 | +fo.close() |
| 237 | + |
| 238 | +print("soloud.d generated") |
0 commit comments