# -*-makefile-*-

COMMON_GALLIUM_SOURCES = \
        ../common/utils.c \
        ../common/vblank.c \
        ../common/dri_util.c \
        ../common/xmlconfig.c

COMMON_SOURCES = $(COMMON_GALLIUM_SOURCES) \
        ../../common/driverfuncs.c \
        ../common/texmem.c \
        ../common/drirenderbuffer.c

INCLUDES = $(SHARED_INCLUDES) $(EXPAT_INCLUDES)

OBJECTS = $(C_SOURCES:.c=.o) \
	  $(CXX_SOURCES:.cpp=.o) \
	  $(ASM_SOURCES:.S=.o) 


### Include directories
SHARED_INCLUDES = \
	-I. \
	-I$(TOP)/src/mesa/drivers/dri/common \
	-Iserver \
	-I$(TOP)/include \
	-I$(TOP)/src/mapi \
	-I$(TOP)/src/mesa \
	-I$(TOP)/src/egl/main \
	-I$(TOP)/src/egl/drivers/dri \
	$(LIBDRM_CFLAGS)

CFLAGS += $(API_DEFINES)
CXXFLAGS += $(API_DEFINES)

##### RULES #####

.c.o:
	$(CC) -c $(INCLUDES) $(DRI_CFLAGS) $(DRIVER_DEFINES) $< -o $@

.cpp.o:
	$(CC) -c $(INCLUDES) $(DRI_CXXFLAGS) $(DRIVER_DEFINES) $< -o $@

.S.o:
	$(CC) -c $(INCLUDES) $(DRI_CFLAGS) $(DRIVER_DEFINES) $< -o $@


##### TARGETS #####

default: subdirs lib


.PHONY: lib
lib: symlinks subdirs depend
	@$(MAKE) $(LIBNAME) $(TOP)/$(LIB_DIR)/$(LIBNAME)

$(LIBNAME): $(OBJECTS) $(EXTRA_MODULES) $(MESA_MODULES) Makefile \
		$(TOP)/src/mesa/drivers/dri/Makefile.template $(TOP)/src/mesa/drivers/dri/common/dri_test.o
	$(MKLIB) -o $@.tmp -noprefix -linker '$(CXX)' -ldflags '$(LDFLAGS)' \
		$(OBJECTS) $(EXTRA_MODULES) $(DRI_LIB_DEPS)
	$(CXX) $(CFLAGS) -o $@.test $(TOP)/src/mesa/drivers/dri/common/dri_test.o $@.tmp $(DRI_LIB_DEPS)
	@rm -f $@.test
	mv -f $@.tmp $@


$(TOP)/$(LIB_DIR)/$(LIBNAME): $(LIBNAME)
	$(INSTALL) $(LIBNAME) $(TOP)/$(LIB_DIR) 


# If the Makefile defined SUBDIRS, run make in each
.PHONY: subdirs
subdirs:
	@if test -n "$(SUBDIRS)" ; then \
		for dir in $(SUBDIRS) ; do \
			if [ -d $$dir ] ; then \
				(cd $$dir && $(MAKE)) || exit 1; \
			fi \
		done \
	fi


.PHONY: symlinks
symlinks:


depend: $(C_SOURCES) $(CXX_SOURCES) $(ASM_SOURCES) $(SYMLINKS)
	@ echo "running $(MKDEP)"
	@ rm -f depend
	@ touch depend
	@ $(MKDEP) $(MKDEP_OPTIONS) $(DRIVER_DEFINES) $(INCLUDES) \
		$(C_SOURCES) $(CXX_SOURCES) \
		$(ASM_SOURCES) > /dev/null 2>/dev/null


# Emacs tags
tags:
	etags `find . -name \*.[ch]` `find ../include`


# Remove .o and backup files
clean:
	-rm -f *.o */*.o *~ *.so *~ server/*.o $(SYMLINKS)
	-rm -f depend depend.bak


install: $(LIBNAME)
	$(INSTALL) -d $(DESTDIR)$(DRI_DRIVER_INSTALL_DIR)
	$(MINSTALL) -m 755 $(LIBNAME) $(DESTDIR)$(DRI_DRIVER_INSTALL_DIR)


-include depend