]> git.tdb.fi Git - libs/gl.git/commitdiff
Get function pointers on OS X through weak symbols
authorMikko Rasa <tdb@tdb.fi>
Thu, 10 Oct 2013 12:24:09 +0000 (15:24 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 10 Oct 2013 12:24:09 +0000 (15:24 +0300)
scripts/extgen.py
source/gl.h

index 6c0ef8f1799538b8d675c31640628a134c17675b..7ab8abc3fc83ad8efebe031dc859866d98d6dda0 100755 (executable)
@@ -252,26 +252,26 @@ out.write("""
 
 namespace Msp {
 namespace GL {
+
 """)
 
 if funcs or enums:
-       out.write("\n#ifndef GL_%s\n"%ext)
-       for f in funcs:
-               out.write("typedef %s (*%s)(%s);\n"%(f.return_type, f.typedef, ", ".join(f.params)))
-       if funcs and enums:
-               out.write("\n")
-       for e in enums:
-               out.write("#define %s 0x%04X\n"%(e.name, e.value))
-       out.write("#endif\n\n")
-
-       # Apple's OpenGL implementation doesn't have a GetProcAddress function; link
-       # directly to the OpenGL library
-       out.write("\n#if !defined(__APPLE__) || !defined(GL_%s)\n"%ext)
+       if funcs:
+               out.write("#if defined(__APPLE__) || !defined(GL_%s)\n"%ext)
+               for f in funcs:
+                       out.write("typedef %s (*%s)(%s);\n"%(f.return_type, f.typedef, ", ".join(f.params)))
+               out.write("#endif\n\n")
+
+       if enums:
+               out.write("#ifndef GL_%s\n"%ext)
+               for e in enums:
+                       out.write("#define %s 0x%04X\n"%(e.name, e.value))
+               out.write("#endif\n\n")
+
        for f in funcs:
                out.write("extern %s %s;\n"%(f.typedef, f.name))
-       out.write("#endif\n")
 
-out.write("\nextern Extension %s;\n"%ext)
+out.write("extern Extension %s;\n"%ext)
 
 out.write("""
 } // namespace GL
@@ -284,15 +284,19 @@ out = file(out_base+".cpp", "w")
 out.write("#include \"%s.h\"\n"%ext.lower())
 
 out.write("""
+#ifdef __APPLE__
+#define GET_PROC_ADDRESS(x) ::x
+#else
+#define GET_PROC_ADDRESS(x) get_proc_address(#x)
+#endif
+
 namespace Msp {
 namespace GL {
+
 """)
 
-if funcs:
-       out.write("\n#if !defined(__APPLE__) || !defined(GL_%s)\n"%ext)
-       for f in funcs:
-               out.write("%s %s = 0;\n"%(f.typedef, f.name))
-       out.write("\n#endif\n")
+for f in funcs:
+       out.write("%s %s = 0;\n"%(f.typedef, f.name))
 
 out.write("\nExtension::SupportLevel init_%s()\n{\n"%ext.lower())
 out.write("#ifdef GL_%s\n"%ext)
@@ -302,22 +306,18 @@ if ver:
                out.write(" || is_supported(\"GL_%s\")"%backport_ext)
        out.write(")\n\t{\n")
        if funcs:
-               out.write("#ifndef __APPLE__\n")
                for f in funcs:
-                       out.write("\t\t%s = reinterpret_cast<%s>(get_proc_address(\"%s\"));\n"%(f.name, f.typedef, f.name))
-               out.write("#endif\n")
+                       out.write("\t\t%s = reinterpret_cast<%s>(GET_PROC_ADDRESS(%s));\n"%(f.name, f.typedef, f.name))
        out.write("\t\treturn Extension::CORE;\n")
        out.write("\t}\n")
 if ext!=backport_ext:
        out.write("\tif(is_supported(\"GL_%s\"))\n\t{\n"%(ext))
        if funcs:
-               out.write("#ifndef __APPLE__\n")
                for f in funcs:
                        n = f.name
                        if f.source:
                                n = f.source.name
-                       out.write("\t\t%s = reinterpret_cast<%s>(get_proc_address(\"%s\"));\n"%(f.name, f.typedef, n))
-               out.write("#endif\n")
+                       out.write("\t\t%s = reinterpret_cast<%s>(GET_PROC_ADDRESS(%s));\n"%(f.name, f.typedef, n))
        out.write("\t\treturn Extension::EXTENSION;\n")
        out.write("\t}\n")
 out.write("#endif\n")
index 05844ba5f4666e5b048209ab9340bff69a601d68..255242bf12956fb0559730cd8d8885fdd5813c71 100644 (file)
@@ -5,8 +5,10 @@
 #include <windows.h>
 #endif
 #ifdef __APPLE__
+#define extern extern __attribute__((weak_import))
 #include <OpenGL/gl.h>
 #include <OpenGL/glext.h>
+#undef extern
 #else
 #include <GL/gl.h>
 #include <GL/glext.h>