# -*- tab-width: 4; -*-
# vi: set sw=2 ts=4:

# GNU make file to build libKTX and doxygen documentation

# ---------------------------------------------------------------
#
# Copyright (c) 2010 The Khronos Group Inc.
# 
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and/or associated documentation files (the
# "Materials"), to deal in the Materials without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Materials, and to
# permit persons to whom the Materials are 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 Materials.
# 
# THE MATERIALS ARE 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 THE AUTHORS OR COPYRIGHT HOLDERS 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
# MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
# 
# ---------------------------------------------------------------

# Author: Mark Callow, HI Corporation
# $Id: 6c4cf06efc4c185ea2fbdbf9032e63e0db4cb01a $

depth := ../..
version := 20

# Location of KTX home page.
# Can pass Khronos SVN location through environment or make command.
KHRONOS_SVN ?= ../khronos
ktxHomePage ?= $(KHRONOS_SVN)/ogles/trunk/sdk/tools/KTX

ktxPubInc := $(depth)/include
ktxLibSrc := $(depth)/lib
ktxTools  := $(depth)/tools
toktxSrc  := $(ktxTools)/toktx
ktxDoxyConfig := ktxDoxy

ktxZipName := ktx$(version).zip
ktxDistZip := ../$(ktxZipName)
ktxZipOut := build/$(ktxZipName)
ktxTgzName := ktx$(version).tgz
ktxDistTgz := ../$(ktxTgzName)
ktxTgzOut := build/$(ktxTgzName)

ktxLibDocOutDir := ../doc

vc9 := ../vc9
toktxWinExe := $(vc9)/Release/toktx.exe
devenv = $(shell cygpath "${VS90COMNTOOLS}/../IDE/devenv.exe")

distFiles := include/ktx.h $(ktxDoxyConfig) lib tests TODO tools \
	         testimages/*.ppm testimages/{etc1,*reference}.ktx \
			 build/GNUmake/GNUmakefile build/GNUmake/lib/GNUmakefile \
	         build/vc9/*.sln build/vc9/*/*.vcproj build/vc9/*.txt \
             build/vc9/*.vsprops 

sources = $(notdir $(wildcard ${ktxLibSrc}/*.c))
sources += etcdec.cxx
headers = $(notdir $(wildcard ${ktxLibSrc}/*.h $(ktxPubInc)/*.h))

vpath %.h $(ktxPubInc) $(ktxLibSrc)
vpath %.c $(ktxLibSrc)
vpath %.cxx $(ktxLibSrc)

SUBDIRS = libktx #toktx gles1_loadtests

.PHONY: clean clobber doc dist help $(ktxDistZip) $(ktxDistTgz) $(SUBDIRS)
.PHONY: updateKtxHome toktxWin

default: $(SUBDIRS)

doc: $(ktxLibDocOutDir)/html

dist: $(ktxDistZip) $(ktxDistTgz)

help:
	@echo "The following targets are available:"
	@echo "    default       - compile the library"
	@echo "    doc           - build the documentation with doxygen"
	@echo "    dist          - build the .zip and .tgz files for distribution"
	@echo "    help          - print this list of targets"
	@echo "    updateKtxHome - runs dist, doc and toktxWin then updates the KTX"
	@echo "                    page in the OpenGL ES SDK. Requires a workspace"
	@echo "	                   for the SDK on the same system. Can specify"
	@echo "                    KHRONOS_SVN or ktxHomePage in the make command"
	@echo "	                   or environment"
	@echo "    toktxWin      - build the windows binary of toktx"

updateKtxHome: $(ktxHomePage) dist $(ktxLibDocOutDir)/html toktxWin
	cp -r $(ktxLibDocOutDir)/html/. $(ktxHomePage)/doc/libktx
	cp $(ktxDistZip) $(ktxDistTgz) $(ktxHomePage)/downloads
	cp $(toktxWinExe) $(ktxHomePage)/downloads

# doxygen is run in the top-level directory so that ancestors of that
# directory will be removed from paths displayed in the documentation.
$(ktxLibDocOutDir)/html: $(depth)/$(ktxDoxyConfig) $(sources) $(headers)
	cd $(depth); doxygen $(ktxDoxyConfig)

# Having to change directory to run zip means $@ cannot be used.
$(ktxDistZip):
	cd $(depth); zip --quiet -r $(ktxZipOut) $(distFiles) --exclude "*/.svn/*" "*/.git/*"

$(ktxDistTgz):
	cd $(depth); tar czf $(ktxTgzOut) $(distFiles) --exclude-vcs

# The project files in ktx.sln use user macros set in $(vc9)/gl.vsprops
# to find the OpenGL include files and libraries. The macros use this
# environment variable as part of the path. When run from cygwin, this
# path in this variable is in UNIX format. It needs to be in Windows
# format for $(devenv).
#
# If you change the macros in $(vc9)/gl.vsprops to point to your local
# copies of the GL files, change or comment out this as appropriate.
toktxWin: export HI_BSLIB_HOME:="$(shell cygpath -w "${HI_BSLIB_HOME}")"
toktxWin:
	"$(devenv)" $(vc9)/ktx.sln /build "Release"

#$(ktxLibDocOutDir):
#	mkdir -P $@

$(SUBDIRS):
	$(MAKE) -C $@ $(MAKECMDGOALS)

clean clobber: $(SUBDIRS)
