| 1 |
|
| 2 |
|
| 3 |
from cffi import FFI, PkgConfigError |
| 4 |
import sys, os |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
if sys.platform.startswith("openbsd"): |
| 9 |
incdirs = ['/usr/local/include/tcl8.5', '/usr/local/include/tk8.5', '/usr/X11R6/include'] |
| 10 |
linklibs = ['tk85', 'tcl85'] |
| 11 |
libdirs = ['/usr/local/lib', '/usr/X11R6/lib'] |
| 12 |
elif sys.platform.startswith("freebsd"): |
| 13 |
incdirs = ['/usr/local/include/tcl8.6', '/usr/local/include/tk8.6', '/usr/local/include/X11', '/usr/local/include'] |
| 14 |
linklibs = ['tk86', 'tcl86'] |
| 15 |
libdirs = ['/usr/local/lib'] |
| 16 |
elif sys.platform == 'win32': |
| 17 |
incdirs = [] |
| 18 |
linklibs = ['tcl86t', 'tk86t'] |
| 19 |
libdirs = [] |
| 20 |
elif sys.platform == 'darwin': |
| 21 |
|
| 22 |
homebrew = os.environ.get('HOMEBREW_PREFIX', '') |
| 23 |
incdirs = ['/usr/local/opt/tcl-tk/include'] |
| 24 |
linklibs = ['tcl8.6', 'tk8.6'] |
| 25 |
libdirs = [] |
| 26 |
if homebrew: |
| 27 |
incdirs.append(homebrew + '/opt/tcl-tk@8/include/tcl-tk') |
| 28 |
libdirs.append(homebrew + '/opt/tcl-tk@8/lib') |
| 29 |
else: |
| 30 |
|
| 31 |
|
| 32 |
libdirs = [] |
| 33 |
found = False |
| 34 |
for _ver in ['', '8.6', '8.5']: |
| 35 |
incdirs = ['/usr/include/tcl' + _ver] |
| 36 |
linklibs = ['tcl' + _ver, 'tk' + _ver] |
| 37 |
if os.path.isdir(incdirs[0]): |
| 38 |
found = True |
| 39 |
break |
| 40 |
if not found: |
| 41 |
for _ver in ['8.6', '8.5', '']: |
| 42 |
incdirs = [] |
| 43 |
linklibs = ['tcl' + _ver, 'tk' + _ver] |
| 44 |
for lib in ['/usr/lib/lib', '/usr/lib64/lib']: |
| 45 |
if os.path.isfile(''.join([lib, linklibs[1], '.so'])): |
| 46 |
found = True |
| 47 |
break |
| 48 |
if found: |
| 49 |
break |
| 50 |
if not found: |
| 51 |
sys.stderr.write("*** TCL libraries not found! Falling back...\n") |
| 52 |
incdirs = [] |
| 53 |
linklibs = ['tcl', 'tk'] |
| 54 |
|
| 55 |
config_ffi = FFI() |
| 56 |
config_ffi.cdef(""" |
| 57 |
#define TK_HEX_VERSION ... |
| 58 |
#define HAVE_WIDE_INT_TYPE ... |
| 59 |
""") |
| 60 |
|
| 61 |
oldval = os.environ.get("DISTUTILS_DEBUG", None) |
| 62 |
os.environ["DISTUTILS_DEBUG"] = "1" |
| 63 |
config_lib = config_ffi.verify(""" |
| 64 |
#include <tk.h> |
| 65 |
#define TK_HEX_VERSION ((TK_MAJOR_VERSION << 24) | \ |
| 66 |
(TK_MINOR_VERSION << 16) | \ |
| 67 |
(TK_RELEASE_LEVEL << 8) | \ |
| 68 |
(TK_RELEASE_SERIAL << 0)) |
| 69 |
#ifdef TCL_WIDE_INT_TYPE |
| 70 |
#define HAVE_WIDE_INT_TYPE 1 |
| 71 |
#else |
| 72 |
#define HAVE_WIDE_INT_TYPE 0 |
| 73 |
#endif |
| 74 |
""", |
| 75 |
include_dirs=incdirs, |
| 76 |
libraries=linklibs, |
| 77 |
library_dirs = libdirs, |
| 78 |
) |
| 79 |
if oldval is None: |
| 80 |
os.environ.pop("DISTUTILS_DEBUG") |
| 81 |
else: |
| 82 |
os.environ["DISTUTILS_DEBUG"] = oldval |
| 83 |
|
| 84 |
TK_HEX_VERSION = config_lib.TK_HEX_VERSION |
| 85 |
|
| 86 |
HAVE_LIBTOMMATH = int((0x08050208 <= TK_HEX_VERSION < 0x08060000) or |
| 87 |
(0x08060200 <= TK_HEX_VERSION)) |
| 88 |
HAVE_WIDE_INT_TYPE = config_lib.HAVE_WIDE_INT_TYPE |
| 89 |
|
| 90 |
tkffi = FFI() |
| 91 |
|
| 92 |
tkffi.cdef(""" |
| 93 |
char *get_tk_version(); |
| 94 |
char *get_tcl_version(); |
| 95 |
char *get_tk_patch_level(); |
| 96 |
char *get_tcl_patch_level(); |
| 97 |
#define HAVE_LIBTOMMATH ... |
| 98 |
#define HAVE_WIDE_INT_TYPE ... |
| 99 |
|
| 100 |
#define TCL_READABLE ... |
| 101 |
#define TCL_WRITABLE ... |
| 102 |
#define TCL_EXCEPTION ... |
| 103 |
#define TCL_ERROR ... |
| 104 |
#define TCL_OK ... |
| 105 |
|
| 106 |
#define TCL_LEAVE_ERR_MSG ... |
| 107 |
#define TCL_GLOBAL_ONLY ... |
| 108 |
#define TCL_EVAL_DIRECT ... |
| 109 |
#define TCL_EVAL_GLOBAL ... |
| 110 |
|
| 111 |
#define TCL_DONT_WAIT ... |
| 112 |
|
| 113 |
typedef unsigned short Tcl_UniChar; |
| 114 |
typedef ... Tcl_Interp; |
| 115 |
typedef ...* Tcl_ThreadId; |
| 116 |
typedef ...* Tcl_Command; |
| 117 |
|
| 118 |
typedef struct Tcl_ObjType { |
| 119 |
const char *name; |
| 120 |
...; |
| 121 |
} Tcl_ObjType; |
| 122 |
typedef struct Tcl_Obj { |
| 123 |
char *bytes; |
| 124 |
int length; |
| 125 |
const Tcl_ObjType *typePtr; |
| 126 |
union { /* The internal representation: */ |
| 127 |
long longValue; /* - an long integer value. */ |
| 128 |
double doubleValue; /* - a double-precision floating value. */ |
| 129 |
struct { /* - internal rep as two pointers. */ |
| 130 |
void *ptr1; |
| 131 |
void *ptr2; |
| 132 |
} twoPtrValue; |
| 133 |
} internalRep; |
| 134 |
...; |
| 135 |
} Tcl_Obj; |
| 136 |
|
| 137 |
Tcl_Interp *Tcl_CreateInterp(); |
| 138 |
void Tcl_DeleteInterp(Tcl_Interp* interp); |
| 139 |
int Tcl_Init(Tcl_Interp* interp); |
| 140 |
int Tk_Init(Tcl_Interp* interp); |
| 141 |
|
| 142 |
void Tcl_Free(void* ptr); |
| 143 |
|
| 144 |
const char *Tcl_SetVar(Tcl_Interp* interp, const char* varName, const char* newValue, int flags); |
| 145 |
const char *Tcl_SetVar2(Tcl_Interp* interp, const char* name1, const char* name2, const char* newValue, int flags); |
| 146 |
const char *Tcl_GetVar(Tcl_Interp* interp, const char* varName, int flags); |
| 147 |
Tcl_Obj *Tcl_SetVar2Ex(Tcl_Interp* interp, const char* name1, const char* name2, Tcl_Obj* newValuePtr, int flags); |
| 148 |
Tcl_Obj *Tcl_GetVar2Ex(Tcl_Interp* interp, const char* name1, const char* name2, int flags); |
| 149 |
int Tcl_UnsetVar2(Tcl_Interp* interp, const char* name1, const char* name2, int flags); |
| 150 |
const Tcl_ObjType *Tcl_GetObjType(const char* typeName); |
| 151 |
|
| 152 |
Tcl_Obj *Tcl_NewStringObj(const char* bytes, int length); |
| 153 |
Tcl_Obj *Tcl_NewUnicodeObj(const Tcl_UniChar* unicode, int numChars); |
| 154 |
Tcl_Obj *Tcl_NewLongObj(long longValue); |
| 155 |
Tcl_Obj *Tcl_NewBooleanObj(int boolValue); |
| 156 |
Tcl_Obj *Tcl_NewDoubleObj(double doubleValue); |
| 157 |
|
| 158 |
void Tcl_IncrRefCount(Tcl_Obj* objPtr); |
| 159 |
void Tcl_DecrRefCount(Tcl_Obj* objPtr); |
| 160 |
|
| 161 |
int Tcl_GetBoolean(Tcl_Interp* interp, const char* src, int* boolPtr); |
| 162 |
int Tcl_GetInt(Tcl_Interp* interp, const char* src, int* intPtr); |
| 163 |
int Tcl_GetDouble(Tcl_Interp* interp, const char* src, double* doublePtr); |
| 164 |
int Tcl_GetBooleanFromObj(Tcl_Interp* interp, Tcl_Obj* objPtr, int* valuePtr); |
| 165 |
char *Tcl_GetString(Tcl_Obj* objPtr); |
| 166 |
char *Tcl_GetStringFromObj(Tcl_Obj* objPtr, int* lengthPtr); |
| 167 |
unsigned char *Tcl_GetByteArrayFromObj(Tcl_Obj* objPtr, int* lengthPtr); |
| 168 |
Tcl_Obj *Tcl_NewByteArrayObj(unsigned char *bytes, int length); |
| 169 |
|
| 170 |
int Tcl_ExprBoolean(Tcl_Interp* interp, const char *expr, int *booleanPtr); |
| 171 |
int Tcl_ExprLong(Tcl_Interp* interp, const char *expr, long* longPtr); |
| 172 |
int Tcl_ExprDouble(Tcl_Interp* interp, const char *expr, double* doublePtr); |
| 173 |
int Tcl_ExprString(Tcl_Interp* interp, const char *expr); |
| 174 |
|
| 175 |
Tcl_UniChar *Tcl_GetUnicode(Tcl_Obj* objPtr); |
| 176 |
int Tcl_GetCharLength(Tcl_Obj* objPtr); |
| 177 |
|
| 178 |
Tcl_Obj *Tcl_NewListObj(int objc, Tcl_Obj* const objv[]); |
| 179 |
int Tcl_ListObjGetElements(Tcl_Interp *interp, Tcl_Obj *listPtr, int *objcPtr, Tcl_Obj ***objvPtr); |
| 180 |
int Tcl_ListObjLength(Tcl_Interp* interp, Tcl_Obj* listPtr, int* intPtr); |
| 181 |
int Tcl_ListObjIndex(Tcl_Interp* interp, Tcl_Obj* listPtr, int index, Tcl_Obj** objPtrPtr); |
| 182 |
int Tcl_SplitList(Tcl_Interp* interp, char* list, int* argcPtr, const char*** argvPtr); |
| 183 |
char* Tcl_Merge(int argc, const char * const* argv); |
| 184 |
|
| 185 |
int Tcl_Eval(Tcl_Interp* interp, const char* script); |
| 186 |
int Tcl_EvalFile(Tcl_Interp* interp, const char* filename); |
| 187 |
int Tcl_EvalObjv(Tcl_Interp* interp, int objc, Tcl_Obj** objv, int flags); |
| 188 |
Tcl_Obj *Tcl_GetObjResult(Tcl_Interp* interp); |
| 189 |
const char *Tcl_GetStringResult(Tcl_Interp* interp); |
| 190 |
void Tcl_SetObjResult(Tcl_Interp* interp, Tcl_Obj* objPtr); |
| 191 |
|
| 192 |
typedef void* ClientData; |
| 193 |
typedef int Tcl_CmdProc( |
| 194 |
ClientData clientData, |
| 195 |
Tcl_Interp *interp, |
| 196 |
int argc, |
| 197 |
const char *argv[]); |
| 198 |
typedef void Tcl_CmdDeleteProc( |
| 199 |
ClientData clientData); |
| 200 |
Tcl_Command Tcl_CreateCommand(Tcl_Interp* interp, const char* cmdName, Tcl_CmdProc proc, ClientData clientData, Tcl_CmdDeleteProc deleteProc); |
| 201 |
int Tcl_DeleteCommand(Tcl_Interp* interp, const char* cmdName); |
| 202 |
|
| 203 |
Tcl_ThreadId Tcl_GetCurrentThread(); |
| 204 |
int Tcl_DoOneEvent(int flags); |
| 205 |
|
| 206 |
int Tk_GetNumMainWindows(); |
| 207 |
void Tcl_FindExecutable(char *argv0); |
| 208 |
""") |
| 209 |
|
| 210 |
if HAVE_WIDE_INT_TYPE: |
| 211 |
tkffi.cdef(""" |
| 212 |
typedef int... Tcl_WideInt; |
| 213 |
|
| 214 |
int Tcl_GetWideIntFromObj(Tcl_Interp *interp, Tcl_Obj *obj, Tcl_WideInt *value); |
| 215 |
Tcl_Obj *Tcl_NewWideIntObj(Tcl_WideInt value); |
| 216 |
""") |
| 217 |
|
| 218 |
if HAVE_LIBTOMMATH: |
| 219 |
tkffi.cdef(""" |
| 220 |
#define MP_OKAY ... |
| 221 |
#define MP_ZPOS ... |
| 222 |
#define MP_NEG ... |
| 223 |
typedef struct { |
| 224 |
int sign; |
| 225 |
...; |
| 226 |
} mp_int; |
| 227 |
|
| 228 |
int Tcl_GetBignumFromObj(Tcl_Interp *interp, Tcl_Obj *obj, mp_int *value); |
| 229 |
Tcl_Obj *Tcl_NewBignumObj(mp_int *value); |
| 230 |
|
| 231 |
int mp_unsigned_bin_size(mp_int *a); |
| 232 |
int mp_to_unsigned_bin_n(mp_int * a, unsigned char *b, unsigned long *outlen); |
| 233 |
int mp_read_radix(mp_int *a, const char *str, int radix); |
| 234 |
int mp_init(mp_int *a); |
| 235 |
void mp_clear(mp_int *a); |
| 236 |
""") |
| 237 |
|
| 238 |
c_header =""" |
| 239 |
#define HAVE_LIBTOMMATH %(HAVE_LIBTOMMATH)s |
| 240 |
#define HAVE_WIDE_INT_TYPE %(HAVE_WIDE_INT_TYPE)s |
| 241 |
#include <tcl.h> |
| 242 |
#include <tk.h> |
| 243 |
|
| 244 |
#if HAVE_LIBTOMMATH |
| 245 |
#include <tclTomMath.h> |
| 246 |
#endif |
| 247 |
|
| 248 |
char *get_tk_version(void) { return TK_VERSION; } |
| 249 |
char *get_tcl_version(void) { return TCL_VERSION; } |
| 250 |
char *get_tk_patch_level(void) { return TK_PATCH_LEVEL; } |
| 251 |
char *get_tcl_patch_level(void) { return TCL_PATCH_LEVEL; } |
| 252 |
""" % globals() |
| 253 |
|
| 254 |
try: |
| 255 |
tkffi.set_source_pkgconfig("_tkinter.tklib_cffi", ["tk", "tcl"], c_header) |
| 256 |
print("used pkg-config") |
| 257 |
except PkgConfigError: |
| 258 |
tkffi.set_source("_tkinter.tklib_cffi", c_header, |
| 259 |
include_dirs=incdirs, |
| 260 |
libraries=linklibs, |
| 261 |
library_dirs = libdirs |
| 262 |
) |
| 263 |
|
| 264 |
if __name__ == "__main__": |
| 265 |
tkffi.compile(os.path.join(os.path.dirname(sys.argv[0]), '..')) |