Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I suppress '-arch', 'x86_64' flags when compiling an OpenGL/SDL application with Waf on OSX?

Tags:

I need to suppress "-arch x86_64 -arch i386" flags Waf is passing to GCC.

I am building an SDL/Opengl application. If I link against 32 bit SDL runtime I get error

    Undefined symbols for architecture i386:
  "_SDL_Quit", referenced from:
      __del_video in SDL_functions.c.2.o
      __init_video in SDL_functions.c.2.o

If I link against 64 bit SDL runtime, I get error "Undefined symbols for architecture x86_64"

The compiler is apparently using flags

-arch x86_64 -arch i386

I understand that this causes GCC on OSX to try to compile for both architectures. I want to either compile for 64 bit, or compile for 32 bit. How do I suppress the flags for one architecture?

like image 296
HaltingState Avatar asked Aug 27 '11 18:08

HaltingState


1 Answers

I found out in my case that the double arch flags were originating here, specifically from distutils.sysconfig.get_config_var('LDFLAGS'). This returns the LDFLAGS that Python thinks you should link Python modules with. In my case, file $(which python) is a "Mach-O universal binary with 2 architectures", so Python thinks you should link with -arch x86_64 -arch i386 -Wl,F.

My problem was that I was building a Python native module that needed to link against Python and another library which was not built with both arches. When building my module with both arches, linking failed with "symbols not found", because both arches were not available in the third-party library.

Since waf unfortunately doesn't allow you to override its computed flags with your own flags, as Automake does, I could only fix this by messing directly with my ctx() object in my wscript:

for var in ['CFLAGS_PYEMBED', 'CFLAGS_PYEXT', 'CXXFLAGS_PYEMBED',
    'CXXFLAGS_PYEXT', 'LINKFLAGS_PYEMBED', 'LINKFLAGS_PYEXT']:
    newvar = []
    for ix, arg in enumerate(ctx.env[var]):
        if '-arch' not in (arg, ctx.env[var][ix - 1]):
            newvar.append(arg)
    ctx.env[var] = newvar

(This removes all -arch flags and their arguments from the relevant variables. Since I was also passing my own -arch flag in my CFLAGS, it now does not get overridden.)

like image 131
ptomato Avatar answered Oct 12 '22 05:10

ptomato