AS=as
CC=@CC@
CPP=/lib/cpp
CFLAGS=@CFLAGS@
DEFINES=@DEFINES@
LDFLAGS=@LDFLAGS@
INC=-I../../include -I../include -I../audioconvert -Iarch -I. @INCLUDES@ 
LIBS=-L. @LIBS@
MAKE=@MAKE@
SO_EXT=@SHARED_LIBRARY_EXT@
NASM=@NASM@

AL_OBJS=al_buffer.o al_listen.o al_ext.o \
	al_source.o 			 \
	al_bpool.o 			 \
	al_spool.o 			 \
	al_main.o                        \
	al_filter.o                      \
	al_mixer.o                       \
	al_mixfunc.o                     \
	al_mixmanager.o                  \
	al_mspool.o                      \
	al_state.o                       \
	al_able.o                        \
	al_error.o                       \
	al_ext_iasig.o			 \
	al_config.o                      \
	al_debug.o                       \
	al_rctree.o                      \
	al_rcvar.o                       \
	al_queue.o                       \
	al_doppler.o                     \
	al_distance.o                    \
	al_vector.o                      \
	mixaudio16.o

# Major subgroupings
ALUT_OBJS=$(addsuffix .o, $(addprefix alut/alut_, main wav rawadpcm imaadpcm msadpcm ))
ALC_OBJS=$(addsuffix .o, $(addprefix alc/alc_, context speaker error device ))
AC_OBJS=$(addsuffix .o, $(addprefix ../audioconvert/ac_, misc freq bits channels helper adpcm endian))

# extensions that we want compiled in the main so
EXT_OBJS=$(addsuffix .o, $(addprefix extensions/al_ext_, loki mp3 vorbis capture))


# extensions that we do not want compiled in the main so (but can be
# loaded with load-extension via alrc)
EXT_DLL_OBJS=$(addsuffix .o, $(addprefix extensions/al_ext_, ))

MMX_OBJS=arch/i386/mix16.o
i386_OBJS=arch/i386/floatmul.o
PORTABLE_OBJS=arch/portable/floatmul.o

# backend specific stuff
INTERFACE_OBJS=arch/interface/interface_sound.o
ARTS_OBJS=arch/arts/arts.o
ALSA_OBJS=arch/alsa/alsa.o
ESD_OBJS=arch/esd/esd.o
SDL_OBJS=arch/sdl/sdl.o
WAVEOUT_OBJS=arch/waveout/waveout.o
NULL_OBJS=arch/null/null.o
EMU10K1_OBJS=arch/emu10k1/emu10k1.o

BSD_OBJS=arch/bsd/bsd_dsp.o
IRIS_OBJS=arch/irix/iris.o
LIN_OBJS=arch/linux/lin_dsp.o
SOLARIS_OBJS=arch/solaris/solaris_native.o
DARWIN_OBJS=arch/darwin/darwin_native.o
WINDOWS_OBJS=arch/windows/windows_native.o

AL_OBJS+=$(INTERFACE_OBJS) @OS_OBJS@ @ARCH_OBJS@ @SYS_THREAD_OBJS@ @SYS_MUTEX_OBJS@

MAJOR_VERSION=@MAJOR_VERSION@
MINOR_VERSION=@MINOR_VERSION@
MICRO_VERSION=@MICRO_VERSION@
VERSION=$(MAJOR_VERSION).$(MINOR_VERSION).$(MICRO_VERSION)

%.o : %.s ../config.h
	$(CPP) $(DEFINES) $< | $(AS) -o $@
%.o : %.nasm
	$(NASM) -f elf -o $@ $<
%.o : %.c ../config.h
	$(CC) $(DEFINES) $(INC) $(CFLAGS) -c $< -o $@

%.def : %.a libopenal.a
	dlltool -a $< --output-def $@

all: libopenal.a libopenal.$(SO_EXT) extensions $(TEST_PROGS)

clean:
	rm -f $(AL_OBJS)
	rm -f $(ALC_OBJS)
	rm -f $(ALUT_OBJS)
	rm -f $(EXT_OBJS)
	rm -f $(EXT_DLL_OBJS)
	rm -f libopenal.a
	rm -f libopenal.$(SO_EXT).$(VERSION)
	rm -f libopenal.$(SO_EXT).$(MAJOR_VERSION)
	rm -f libopenal.$(SO_EXT)
	rm -f core
	rm -f gmon.out

distclean: clean
	rm -Rf Makefile

libopenal: libopenal.a libopenal.$(SO_EXT)

libopenal.a: $(AL_OBJS) $(ALC_OBJS) $(AC_OBJS) $(ALUT_OBJS) extensions
	ar cru libopenal.a \
		$(AL_OBJS)   \
		$(ALC_OBJS)  \
		$(AC_OBJS)   \
		$(ALUT_OBJS) \
		$(EXT_OBJS)


libopenal.so: $(AL_OBJS) $(ALC_OBJS) $(AC_OBJS) $(ALUT_OBJS) extensions
	$(CC) -shared \
		-Wl,"-soname,libopenal.so.$(MAJOR_VERSION)" \
		-o libopenal.so.$(VERSION) \
		$(LDFLAGS)     \
		$(AL_OBJS)     \
		$(ALC_OBJS)    \
		$(AC_OBJS)     \
		$(ALUT_OBJS)   \
		$(EXT_OBJS)    \
		$(LIBS)

libopenal.dll:  $(AL_OBJS) $(ALC_OBJS) $(AC_OBJS) $(ALUT_OBJS) libopenal.def \
	extensions
	dlltool -k --dllname libopenal.dll --output-lib $@ --def libopenal.def

# extension mojo
extensions: $(EXT_OBJS) $(EXT_DLL_OBJS)


$(EXT_OBJS): %.o : %.c ../config.h
	$(CC) $(DEFINES) $(INC) $(CFLAGS) -c $< -o $@

$(EXT_DLL_OBJS): %.o : %.c ../config.h
	$(CC) $(DEFINES) $(INC) -DOPENAL_EXTENSION $(CFLAGS) $< -shared -o $@
