# MAKEFILE
# 
# Copyright (c) 2008 - Roland Karlsson (roland@proxel.se)
# BSD-style - see readme.txt

# -----------------------------------------------------------
# Usage:
# Write "make" shall build the executables on Mac, Linux or Windows.
# Write "make clean" or "make clobber" cleans up less or more.
# -----------------------------------------------------------

# Set the SYS variable
# -----------------------------------------------------------

  SYS = generic
ifdef COMSPEC
  SYS = windows
else
  OSTYPE = $(shell bash -c 'echo $$OSTYPE')
ifeq (linux-gnu, $(OSTYPE))
  SYS = linux
else
ifeq (darwin, $(findstring darwin,$(OSTYPE)))
  SYS = osx
endif
endif
endif

# Set compilation and load flags etc
# -----------------------------------------------------------

ifeq (windows, $(SYS))
  EXE = .exe
  CFLAGS = -O3 -Wall
  LDFLAGS = -lm
else
ifeq (linux, $(SYS))
  EXE =
  CFLAGS = -O3 -Wall
  LDFLAGS = -lm
else
ifeq (osx, $(SYS))
  EXE =
  ARCH = -arch i386 -arch ppc
  CFLAGS = -O3 -Wall $(ARCH)
  LDFLAGS = -lm $(ARCH)
else
  EXE = 
  CFLAGS = -O3 -Wall
  LDFLAGS = -lm
endif
endif
endif

# Build dependencies
# -----------------------------------------------------------

all: $(SYS)/x3f_extract$(EXE) $(SYS)/x3f_io_test$(EXE)

$(SYS)/x3f_extract$(EXE): $(SYS)/x3f_extract.o $(SYS)/x3f_io.o
	gcc $^ -o $@ $(LDFLAGS)

$(SYS)/x3f_io_test$(EXE): $(SYS)/x3f_io_test.o $(SYS)/x3f_io.o
	gcc $^ -o $@ $(LDFLAGS) 

$(SYS)/x3f_extract.o: x3f_extract.c x3f_io.h $(SYS)/x
	gcc $(CFLAGS) $< -c -o $@

$(SYS)/x3f_io_test.o: x3f_io_test.c x3f_io.h $(SYS)/x
	gcc $(CFLAGS) $< -c -o $@

$(SYS)/x3f_io.o: x3f_io.c x3f_io.h $(SYS)/x
	gcc $(CFLAGS) $< -c -o $@

# This does not work perfect on Windows
# The file x is newer than *.o files. Why?
# So - the first time - all is built twice
$(SYS)/x:
	-@echo make directory $(SYS)
	-@mkdir $(SYS)
	-@touch $(SYS)/x

# Clean up dependencies
# -----------------------------------------------------------

clean:
	-@rm */*.o
	-@rm */x
	-@rm *~

clobber: clean
	-@rm -r windows osx linux generic
