#******************************************************************************
#                                  Makefile                                   *
#                                 ----------                                  *
# Description : Builds the GNU Spice GUI (gSpiceUI) C++ source files.         *
# Started     : 18/08/2003                                                    *
# Last update : 16/09/2009                                                    *
# Copyright   : (C) 2003 by MSWaters                                          *
# Email       : M.Waters@bom.gov.au                                           *
#******************************************************************************

#******************************************************************************
#                                                                             *
#     This program is free software; you can redistribute it and/or modify    *
#     it under the terms of the GNU General Public License as published by    *
#     the Free Software Foundation; either version 2 of the License, or       *
#     (at your option) any later version.                                     *
#                                                                             *
#******************************************************************************

#******************************************************************************
# Any or all of the values in this section may be set below or on the command
# line when envoking make eg. :
#
#   make GSPICEUI_DBG=1 GSPICEUI_WXLIB=2.6
#
# Note : Values specified on the command line over-ride those specified below.
#******************************************************************************

# Create bin file containing debug info. (ie. for gdb)
GSPICEUI_DBG = 0

# Specify the version of the wxWidgets library to compile against
GSPICEUI_WXLIB = 2.8

#******************************************************************************
# Specify string values
#******************************************************************************

# Which compiler
CC = g++

# Application name
PROG = gspiceui

# wxWidgets configuration utility
# (Arguments can be passed to wx-config to specify the version of wxWidgets to
# use, whether unicode is required, etc.)
WXCFG = wx-config --unicode --version=$(GSPICEUI_WXLIB)

# Dependency file
DEPS = Makefile.deps

# Directories
#ROOT     := $(shell cd .. ; pwd)
ROOT       = ..
SRCDIR     = $(ROOT)/src
OBJDIR     = obj
BINDIR     = $(ROOT)/bin
INSTALLDIR = /usr/local/bin

# Compiler options
ifeq ($(GSPICEUI_DBG),0)
  # Options for release (not using -Wall since it's GCC specific)
  CXXFLAGS := -O -pipe $(shell $(WXCFG) --cxxflags)
else
  # Options for development
  CXXFLAGS := -Wall -g -pipe $(shell $(WXCFG) --cxxflags)
endif

# Includes
INCLUDES = -I/usr/include -I/usr/X11R6/include -I.

# Libraries
# (The pkg-config stuff was requested by a user, somehow pangox was missing)
LIBS := $(shell $(WXCFG) --libs core,base,html) $(shell pkg-config --libs pangox)

# Objects
OBJS := $(wildcard *.cpp) $(wildcard */*.cpp) $(wildcard */*/*.cpp)
OBJS := $(notdir $(OBJS))
OBJS := $(patsubst %.cpp, obj/%.o, $(OBJS))
OBJS += obj/iStrCmp.o

# Set search paths for the specific file types
vpath  %.cpp  base main netlist process utility                       \
              ngspice ngspice/commands ngspice/dialogs ngspice/panels \
              gnucap  gnucap/commands  gnucap/dialogs  gnucap/panels
vpath  %.c    utility

#******************************************************************************
# Make these targets
#******************************************************************************

all : $(OBJS) $(BINDIR)/$(PROG)

# Compiler Rules :
#   $<  is the name of the first dependency
#   $@  is the file name of the target
#   -c  compile only, don't link, produces object file ie. <name>.o files
#   -o  place output file in $@

obj/%.o : %.cpp
	$(CC) -c $(CXXFLAGS) $(INCLUDES) $< -o $@
obj/%.o : %.c
	$(CC) -c $(CXXFLAGS) $(INCLUDES) $< -o $@

# Linker Rules :
#   -pipe  use pipes rather temporary files for interprocess communications
#   -o     specify the output file name

$(BINDIR)/$(PROG) : $(OBJS)
	$(CC) -pipe -o $(BINDIR)/$(PROG) obj/*.o $(LIBS)
ifeq ($(ROOT)/GSpiceUI.app,$(wildcard $(ROOT)/GSpiceUI.app))
	cp $(BINDIR)/$(PROG) $(ROOT)/GSpiceUI.app/Contents/MacOS/gspiceui
endif

#******************************************************************************
# Dependencies
#******************************************************************************

ifeq ($(DEPS),$(wildcard $(DEPS)))
  include $(DEPS)
endif

#******************************************************************************
# Create the file containing dependency information
#******************************************************************************

deps :
	$(CC) -MM -D wxCHECK_VERSION="" -I. *.cpp */*.cpp */*/*.cpp */*.c | \
	gawk 'BEGIN {} { printf "%s%s\n", (index($$0," ")!=1 ? "$(OBJDIR)/" : ""), $$0 }' \
	> $(DEPS)

#******************************************************************************
# Test utilities
#******************************************************************************

tests : ../bin/testnetlist   ../bin/testcomponent ../bin/testconverttype \
	      ../bin/testcmdngsopt ../bin/testcmdngspr  ../bin/testcmdngsdc    \
	      ../bin/testcmdngsac  ../bin/testcmdngstr  ../bin/testcmdgcpopt   \
	      ../bin/testcmdgcppr  ../bin/testcmdgcpop  ../bin/testcmdgcpdc    \
	      ../bin/testcmdgcpac  ../bin/testcmdgcptr  ../bin/testcmdgcpfo    \
	      ../bin/testcmdgcpgen

../bin/testnetlist     : Component.cpp NetList.cpp iStrCmp.c ConvertType.cpp
	$(CC) $(CXXFLAGS) -Wall -g -D TEST_NETLIST     $(INCLUDES) $^ -o $@ $(LIBS)
../bin/testcomponent   : Component.cpp ConvertType.cpp iStrCmp.c
	$(CC) $(CXXFLAGS) -Wall -g -D TEST_COMPONENT   $(INCLUDES) $^ -o $@ $(LIBS)
../bin/testconverttype : ConvertType.cpp
	$(CC) $(CXXFLAGS) -Wall -g -D TEST_CONVERTTYPE $(INCLUDES) $^ -o $@ $(LIBS)

../bin/testcmdngsopt   : CmdNgSpiceOPT.cpp CmdBase.cpp ConvertType.cpp
	$(CC) $(CXXFLAGS) -Wall -g -D TEST_UTIL        $(INCLUDES) $^ -o $@ $(LIBS)
../bin/testcmdngspr    : CmdNgSpicePR.cpp CmdBase.cpp
	$(CC) $(CXXFLAGS) -Wall -g -D TEST_UTIL        $(INCLUDES) $^ -o $@ $(LIBS)
../bin/testcmdngsdc    : CmdNgSpiceDC.cpp CmdBase.cpp ConvertType.cpp
	$(CC) $(CXXFLAGS) -Wall -g -D TEST_UTIL        $(INCLUDES) $^ -o $@ $(LIBS)
../bin/testcmdngsac    : CmdNgSpiceAC.cpp CmdBase.cpp ConvertType.cpp
	$(CC) $(CXXFLAGS) -Wall -g -D TEST_UTIL        $(INCLUDES) $^ -o $@ $(LIBS)
../bin/testcmdngstr    : CmdNgSpiceTR.cpp CmdBase.cpp ConvertType.cpp
	$(CC) $(CXXFLAGS) -Wall -g -D TEST_UTIL        $(INCLUDES) $^ -o $@ $(LIBS)

../bin/testcmdgcpopt   : CmdGnuCapOPT.cpp CmdBase.cpp ConvertType.cpp
	$(CC) $(CXXFLAGS) -Wall -g -D TEST_UTIL        $(INCLUDES) $^ -o $@ $(LIBS)
../bin/testcmdgcppr    : CmdGnuCapPR.cpp CmdBase.cpp
	$(CC) $(CXXFLAGS) -Wall -g -D TEST_UTIL        $(INCLUDES) $^ -o $@ $(LIBS)
../bin/testcmdgcpop    : CmdGnuCapOP.cpp CmdBase.cpp ConvertType.cpp
	$(CC) $(CXXFLAGS) -Wall -g -D TEST_UTIL        $(INCLUDES) $^ -o $@ $(LIBS)
../bin/testcmdgcpdc    : CmdGnuCapDC.cpp CmdBase.cpp ConvertType.cpp
	$(CC) $(CXXFLAGS) -Wall -g -D TEST_UTIL        $(INCLUDES) $^ -o $@ $(LIBS)
../bin/testcmdgcpac    : CmdGnuCapAC.cpp CmdBase.cpp ConvertType.cpp
	$(CC) $(CXXFLAGS) -Wall -g -D TEST_UTIL        $(INCLUDES) $^ -o $@ $(LIBS)
../bin/testcmdgcptr    : CmdGnuCapTR.cpp CmdBase.cpp ConvertType.cpp
	$(CC) $(CXXFLAGS) -Wall -g -D TEST_UTIL        $(INCLUDES) $^ -o $@ $(LIBS)
../bin/testcmdgcpfo    : CmdGnuCapFO.cpp CmdBase.cpp ConvertType.cpp
	$(CC) $(CXXFLAGS) -Wall -g -D TEST_UTIL        $(INCLUDES) $^ -o $@ $(LIBS)
../bin/testcmdgcpgen   : CmdGnuCapGEN.cpp CmdBase.cpp ConvertType.cpp
	$(CC) $(CXXFLAGS) -Wall -g -D TEST_UTIL        $(INCLUDES) $^ -o $@ $(LIBS)

#******************************************************************************
# Install the application
#******************************************************************************

install :
	cp ../bin/$(PROG) $(INSTALLDIR)/bin

#******************************************************************************
# Uninstall the application
#******************************************************************************

uninstall :
	rm -f $(INSTALLDIR)/bin/$(PROG)

#******************************************************************************
# Remove old versions of the main application binary and object files
#******************************************************************************

clean :
	rm -f $(BINDIR)/$(PROG) $(OBJDIR)/*.o

#******************************************************************************
# Remove old versions of test application binaries
#******************************************************************************

cleantests :
	rm -f $(BINDIR)/test*

#******************************************************************************
# Remove old versions of test application binaries
#******************************************************************************

cleanall : clean cleantests

#******************************************************************************
# Specify phony targets (ie. targets which are not files)
#******************************************************************************

.PHONY : deps tests install uninstall clean cleantests cleanall

#******************************************************************************
