########## smooth commands makefile ##########

include $(dir $(firstword $(MAKEFILE_LIST)))/$(SMOOTH_PATH)/Makefile-options

CCOPTS	   = -I"$(SRCDIR)"/$(SMOOTH_PATH)/include $(MYCCOPTS)
RC	   = windres
RCOPTS	   = -DSMOOTH_DLL -O coff
LDOPTS	   = -L"$(SRCDIR)"/$(SMOOTH_PATH)/$(LIBDIR) $(LIBS)

ifndef GLOBALDLL
	CCOPTS += -fvisibility=hidden

ifeq ($(BUILD_WIN32),True)
	LDOPTS += -lsmooth
else
	LDOPTS += -lsmooth-$(VERSION)
endif
endif

ifeq ($(BUILD_WIN32),True)
	CCOPTS += -DUNICODE -D_UNICODE
	LDOPTS += -mwindows -Wl,--dynamicbase,--nxcompat

	ifdef IMPORTLIB
		ifdef GLOBALDLL
			LDOPTS += -Wl,--out-implib,$(SMOOTH_PATH)/$(LIBDIR)/$(IMPORTLIB)
		else
			LDOPTS += -Wl,--out-implib,$(IMPORTLIB)
		endif
	endif

	ifeq ($(BUILD_X86),True)
		RCOPTS += --target=pe-i386
	else ifeq ($(BUILD_X86_64),True)
		RCOPTS += --target=pe-x86-64
	else ifeq ($(BUILD_ARM64),True)
		RC	= aarch64-w64-mingw32-windres
	endif
else
	CCOPTS += -fPIC

	ifneq ($(BUILD_SOLARIS),True)
	ifneq ($(BUILD_HAIKU),True)
		CCOPTS += -pthread
	endif
	endif

	ifdef GLOBALDLL
		ifeq ($(BUILD_OSX),True)
			LDOPTS += -Wl,-dylib_install_name,lib$(GLOBALDLL)-$(VERSION).$(REVISION)$(SHARED)
		else
			LDOPTS += -Wl,-soname,lib$(GLOBALDLL)-$(VERSION)$(SHARED).$(REVISION)
		endif
	endif

	ifeq ($(BUILD_OPENBSD),True)
		LDOPTS += -L/usr/X11R6/lib -L/usr/local/lib
	endif
endif

ifndef GLOBALLIB
ifndef GLOBALEXE
ifndef GLOBALDLL
ifndef LOCALEXE
ifndef LOCALDLL
	CCOPTS += -DSMOOTH_DLL
endif
endif
endif
endif
endif

.SUFFIXES: .mm .rc

TARGETS =

ifdef GLOBALLIB
	TARGETS += $(SMOOTH_PATH)/$(LIBDIR)/$(GLOBALLIB)
endif

ifdef GLOBALEXE
	TARGETS += $(SMOOTH_PATH)/$(BINDIR)/$(GLOBALEXE)$(EXECUTABLE)
endif

ifdef GLOBALDLL
ifeq ($(BUILD_WIN32),True)
	TARGETS += $(SMOOTH_PATH)/$(BINDIR)/$(GLOBALDLL)$(SHARED)
	TARGETS += $(SMOOTH_PATH)/$(LIBDIR)/$(IMPORTLIB)
else
	TARGETS += $(SMOOTH_PATH)/$(LIBDIR)/lib$(GLOBALDLL)-$(VERSION)$(SHARED)
endif
endif

ifdef LOCALEXE
	TARGETS += $(LOCALEXE)$(EXECUTABLE)
endif

ifdef LOCALDLL
	TARGETS += $(LOCALDLL)$(SHARED)

ifeq ($(BUILD_WIN32),True)
	TARGETS += $(IMPORTLIB)
endif
endif

all: allcmds $(OBJECTS) $(TARGETS)

allcmds:
	+ $(ALLCMD1)
	+ $(ALLCMD2)
	+ $(ALLCMD3)
	+ $(ALLCMD4)

clean:
ifdef OBJECTS
	rm -f $(OBJECTS)
endif
ifdef TARGETS
	rm -f $(TARGETS)
endif
	$(CLEANCMD1)
	$(CLEANCMD2)
	$(CLEANCMD3)
	$(CLEANCMD4)

.c.o:
	$(CC) $(CCOPTS) $(CFLAGS) -c $< -o $@

.cc.o:
	$(CXX) $(CCOPTS) $(CXXFLAGS) -c $< -o $@

.cpp.o:
	$(CXX) $(CCOPTS) $(CXXFLAGS) -c $< -o $@

.m.o:
	$(OBJC) $(CCOPTS) $(OBJCFLAGS) -c $< -o $@

.mm.o:
	$(OBJCXX) $(CCOPTS) $(OBJCXXFLAGS) -c $< -o $@

.rc.o:
	$(RC) $(RCOPTS) $< -o $@

ifdef GLOBALLIB
$(SMOOTH_PATH)/$(LIBDIR)/$(GLOBALLIB): $(OBJECTS)
	mkdir -p $(SMOOTH_PATH)/$(LIBDIR)
	$(AR) rs $@ $(OBJECTS)
endif

ifdef GLOBALEXE
$(SMOOTH_PATH)/$(BINDIR)/$(GLOBALEXE)$(EXECUTABLE): $(OBJECTS)
	mkdir -p $(SMOOTH_PATH)/$(BINDIR)
	$(LD) -o $@ $(OBJECTS) $(LDOPTS) $(LDFLAGS) $(LDFLAGS_TOOLS)
endif

ifdef GLOBALDLL
ifeq ($(BUILD_WIN32),True)
$(SMOOTH_PATH)/$(BINDIR)/$(GLOBALDLL)$(SHARED): $(OBJECTS)
	mkdir -p $(SMOOTH_PATH)/$(BINDIR)
else
$(SMOOTH_PATH)/$(LIBDIR)/lib$(GLOBALDLL)-$(VERSION)$(SHARED): $(OBJECTS)
endif
	mkdir -p $(SMOOTH_PATH)/$(LIBDIR)
ifneq ($(BUILD_OSX),True)
	$(LD) --shared -o $@ $(OBJECTS) $(LDOPTS) $(LDFLAGS) $(LDFLAGS_TOOLS)
else
	$(LD) -dynamiclib -o $@ $(OBJECTS) $(LDOPTS) $(LDFLAGS) $(LDFLAGS_TOOLS)
endif

ifeq ($(BUILD_WIN32),True)
$(SMOOTH_PATH)/$(LIBDIR)/$(IMPORTLIB): $(SMOOTH_PATH)/$(BINDIR)/$(GLOBALDLL)$(SHARED)
endif
endif

ifdef LOCALEXE
$(LOCALEXE)$(EXECUTABLE): $(OBJECTS)
	$(LD) -o $@ $(OBJECTS) $(LDOPTS) $(LDFLAGS) $(LDFLAGS_TOOLS)
endif

ifdef LOCALDLL
$(LOCALDLL)$(SHARED): $(OBJECTS)
ifneq ($(BUILD_OSX),True)
	$(LD) --shared -o $@ $(OBJECTS) $(LDOPTS) $(LDFLAGS) $(LDFLAGS_TOOLS)
else
	$(LD) -dynamiclib -o $@ $(OBJECTS) $(LDOPTS) $(LDFLAGS) $(LDFLAGS_TOOLS)
endif

ifeq ($(BUILD_WIN32),True)
$(IMPORTLIB): $(LOCALDLL)$(SHARED)
endif
endif
