# Mesa 3-D graphics library
# Version:  7.0
# 
# Copyright (C) 1999-2003  Brian Paul   All Rights Reserved.
# 
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# MinGW core makefile v1.4 for Mesa
#
#  Copyright (C) 2002 - Daniel Borca
#  Email : dborca@users.sourceforge.net
#  Web   : http://www.geocities.com/dborca

# MinGW core-gl makefile updated for Mesa 7.0
#
#  updated : by Heromyth, on 2007-7-21
#  Email   : zxpmyth@yahoo.com.cn
#  Bugs    : 1) All the default settings work fine. But the setting X86=1 can't work. 
#            The others havn't been tested yet.
#            2) The generated DLLs are *not* compatible with the ones built
#            with the other compilers like VC8, especially for GLUT. 
#            3) Although more tests are needed, it can be used individually!


#
#  Available options:
#
#    Environment variables:
#	CFLAGS
#
#	GLIDE		path to Glide3 SDK; used with FX.
#			default = $(TOP)/glide3
#	FX=1		build for 3dfx Glide3. Note that this disables
#			compilation of most WMesa code and requires fxMesa.
#			As a consequence, you'll need the Win32 Glide3
#			library to build any application.
#			default = no
#	ICD=1		build the installable client driver interface
#			(windows opengl driver interface)
#			default = no
#	X86=1		optimize for x86 (if possible, use MMX, SSE, 3DNow).
#			default = no
#
#    Targets:
#	all:		build GL
#	clean:		remove object files
#


.PHONY: all clean
.INTERMEDIATE: x86/gen_matypes.exe
.SUFFIXES: .rc .res

# Set this to the prefix of your build tools, i.e. mingw32-
TOOLS_PREFIX = mingw32-

ifeq ($(ICD),1)
  LIB_NAME = mesa32
else
  LIB_NAME = opengl32
endif

DLL_EXT = .dll
IMP_EXT = .a
LIB_PRE = lib
STRIP = -s

AR = ar
ARFLAGS = crus
DLLTOOL = dlltool

TOP = ../..
GLIDE ?= $(TOP)/glide3
LIBDIR = $(TOP)/lib

GL_DLL = $(LIB_NAME)$(DLL_EXT)
GL_IMP = $(LIB_PRE)$(LIB_NAME)$(IMP_EXT)

MESA_LIB = libmesa.a
CC = $(TOOLS_PREFIX)gcc

LDLIBS = -lgdi32 -luser32 -liberty
LDFLAGS = $(STRIP) -shared -fPIC -Wl,--kill-at

CFLAGS += -DBUILD_GL32 -D_DLL -DMESA_MINWARN
CFLAGS += -DNDEBUG -DUSE_EXTERNAL_DXTN_LIB=1

ifeq ($(FX),1)
  CFLAGS += -I$(GLIDE)/include -DFX
  LDLIBS += -L$(GLIDE)/lib -lglide3x
  GL_DEF = drivers/windows/fx/fxopengl.def
  GL_RES = drivers/windows/fx/fx.rc
else
  ifeq ($(ICD),1)
    CFLAGS += -DUSE_MGL_NAMESPACE
    GL_DEF = drivers/windows/icd/mesa.def
  else    
    GL_DEF = $(LIB_NAME).def
  endif
endif



UNLINK = del $(subst /,\,$(1))
ifneq ($(wildcard $(addsuffix /rm.exe,$(subst ;, ,$(PATH)))),)
UNLINK = $(RM) $(1)
endif
ifneq ($(wildcard $(addsuffix /rm,$(subst :, ,$(PATH)))),)
UNLINK = $(RM) $(1)
endif

include sources.mak

CFLAGS += $(INCLUDE_DIRS) 

ifeq ($(X86),1)
CFLAGS += -DUSE_X86_ASM
CFLAGS += -DUSE_MMX_ASM
CFLAGS += -DUSE_SSE_ASM
CFLAGS += -DUSE_3DNOW_ASM
X86_SOURCES += $(X86_API)
else
X86_SOURCES =
endif

ifeq ($(FX),1)
DRIVER_SOURCES = \
	$(GLIDE_DRIVER_SOURCES) \
	drivers/windows/fx/fxwgl.c
else
ifeq ($(ICD),1)
DRIVER_SOURCES = \
	drivers/windows/gdi/wmesa.c \
	drivers/windows/icd/icd.c
else
DRIVER_SOURCES = \
	drivers/windows/gdi/wmesa.c \
	drivers/windows/gdi/wgl.c
endif
endif

SOURCES = $(MESA_SOURCES) $(GLAPI_SOURCES) $(X86_SOURCES) $(DRIVER_SOURCES)

OBJECTS = $(addsuffix .o,$(basename $(SOURCES)))

X86_OBJECTS = $(addsuffix .o,$(basename $(X86_SOURCES)))

RESOURCE = $(GL_RES:.rc=.res)

.c.o:
	$(CC) -o $@ $(CFLAGS) -c $<
.s.o:
	$(CC) -o $@ $(CFLAGS) -x assembler-with-cpp -c $<

.rc.res:
	windres -o $@ -Irc -Ocoff $<


all: $(LIBDIR) $(LIBDIR)/$(GL_DLL) $(LIBDIR)/$(GL_IMP)

$(LIBDIR):
	mkdir -p $(LIBDIR)

$(LIBDIR)/$(GL_DLL) $(LIBDIR)/$(GL_IMP): $(OBJECTS) $(RESOURCE)
	$(CC) $(LDFLAGS) -o $(LIBDIR)/$(GL_DLL) $^ $(LDLIBS)
	$(DLLTOOL) --as=as --dllname $(LIB_NAME) --output-def $(LIBDIR)/$(GL_DEF) $^
	$(DLLTOOL) --as=as -k --dllname $(LIB_NAME) --output-lib $(LIBDIR)/$(GL_IMP) --def $(LIBDIR)/$(GL_DEF)


$(X86_OBJECTS): x86/matypes.h

x86/matypes.h: x86/gen_matypes.exe
	$(subst /,\,$< > $@)

x86/gen_matypes.exe: x86/gen_matypes.c
	$(CC) -o $@ $(CFLAGS) -s $<

# [dBorca]
# glapi_x86.S needs some adjustments
# in order to generate correct entrypoints
# Trick: change the following condition to
# be always false if you need C entrypoints
# with USE_X86_ASM (useful for trace/debug)
ifeq (1,1)
x86/glapi_x86.o: x86/glapi_x86.S
	$(CC) -o $@ $(CFLAGS) -DSTDCALL_API -c $<
else
glapi/glapi_dispatch.o: glapi/glapi_dispatch.c
	$(CC) -o $@ $(CFLAGS) -UUSE_X86_ASM -c $<
glapi/glapi.o: glapi/glapi.c
	$(CC) -o $@ $(CFLAGS) -UUSE_X86_ASM -c $<
endif

# [dBorca]
# if we want codegen, we have to stdcall
tnl/t_vtx_x86_gcc.o: tnl/t_vtx_x86_gcc.S
	$(CC) -o $@ $(CFLAGS) -DSTDCALL_API -c $<

clean:
	-$(call UNLINK,glapi/*.o)
	-$(call UNLINK,main/*.o)
	-$(call UNLINK,math/*.o)
	-$(call UNLINK,vbo/*.o)
	-$(call UNLINK,shader/*.o)
	-$(call UNLINK,shader/slang/*.o)
	-$(call UNLINK,sparc/*.o)
	-$(call UNLINK,ppc/*.o)
	-$(call UNLINK,swrast/*.o)
	-$(call UNLINK,swrast_setup/*.o)
	-$(call UNLINK,tnl/*.o)
	-$(call UNLINK,x86/*.o)
	-$(call UNLINK,x86/rtasm/*.o)
	-$(call UNLINK,x86-64/*.o)
	-$(call UNLINK,drivers/common/*.o)
	-$(call UNLINK,drivers/glide/*.o)
	-$(call UNLINK,drivers/windows/fx/*.o)
	-$(call UNLINK,drivers/windows/fx/*.res)
	-$(call UNLINK,drivers/windows/gdi/*.o)
	-$(call UNLINK,drivers/windows/icd/*.o)