# Make file for GCC compiler on Linux or compatible OS
# The license.html file describes the conditions under which this software may be distributed.

# list of source files
SRC = astyle_main.cpp \
      ASBeautifier.cpp \
      ASFormatter.cpp \
      ASEnhancer.cpp \
      ASResource.cpp

# source directories
vpath %.cpp ../src
vpath %.h   ../src

# define macros
bindir = ../bin
objdir = obj
CBASEFLAGS = -W -Wall -fno-rtti -fno-exceptions
CXX = g++
INSTALL=install -o $(USER) -g $(USER)

# install directory $(ipath)
# set prefix if not defined on the command line
ifndef prefix
    prefix=/usr/bin
endif
ipath=$(prefix)

##################################################

# define compile options for each build
CFLAGS   = -DNDEBUG -O3 $(CBASEFLAGS)
CFLAGSd  = -g $(CBASEFLAGS)
CFLAGSs  = -DNDEBUG -DASTYLE_LIB -O3 -fpic $(CBASEFLAGS)
CFLAGSsd = -DASTYLE_LIB -g -fpic $(CBASEFLAGS)
CFLAGSa  = -DNDEBUG -DASTYLE_LIB -O3 $(CBASEFLAGS)
CFLAGSad = -DASTYLE_LIB -g $(CBASEFLAGS)

# object files are built from the source list $(SRC)
# a suffix is added for each build
OBJ   = $(patsubst %.cpp,$(objdir)/%.o,$(SRC))
OBJd  = $(patsubst %.cpp,$(objdir)/%_d.o,$(SRC))
OBJs  = $(patsubst %.cpp,$(objdir)/%_s.o,$(SRC))
OBJsd = $(patsubst %.cpp,$(objdir)/%_sd.o,$(SRC))
OBJa  = $(patsubst %.cpp,$(objdir)/%_a.o,$(SRC))
OBJad = $(patsubst %.cpp,$(objdir)/%_ad.o,$(SRC))

# define object file rule (with the suffix) for each build

# OBJ
$(objdir)/%.o:  %.cpp  astyle.h
	@ mkdir -p $(objdir)
	$(CXX) $(CFLAGS) -c -o $@ $<

# OBJd
$(objdir)/%_d.o:  %.cpp  astyle.h
	@ mkdir -p $(objdir)
	$(CXX) $(CFLAGSd) -c -o $@ $<

# OBJs
$(objdir)/%_s.o:  %.cpp  astyle.h
	@ mkdir -p $(objdir)
	$(CXX) $(CFLAGSs) -c -o $@ $<

# OBJsd
$(objdir)/%_sd.o:  %.cpp  astyle.h
	@ mkdir -p $(objdir)
	$(CXX) $(CFLAGSsd) -c -o $@ $<

# OBJa
$(objdir)/%_a.o:  %.cpp  astyle.h
	@ mkdir -p $(objdir)
	$(CXX) $(CFLAGSa) -c -o $@ $<

# OBJad
$(objdir)/%_ad.o:  %.cpp  astyle.h
	@ mkdir -p $(objdir)
	$(CXX) $(CFLAGSad) -c -o $@ $<

##################################################
# define build dependencies for each command

release:  astyle
astyle:  $(OBJ)
	@ mkdir -p $(bindir)
	$(CXX) -s -o $(bindir)/$@ $^
	@ echo

debug:  astyled
astyled:  $(OBJd)
	@ mkdir -p $(bindir)
	$(CXX) -o $(bindir)/$@ $^
	@ echo

shared:  libastyle.so
libastyle.so:  $(OBJs)
	@ mkdir -p $(bindir)
	$(CXX) -shared -s -o $(bindir)/$@ $^
	@ echo

shareddebug:  libastyled.so
libastyled.so:  $(OBJsd)
	@ mkdir -p $(bindir)
	$(CXX) -shared -o $(bindir)/$@ $^
	@ echo

static:  libastyle.a
libastyle.a:  $(OBJa)
	@ mkdir -p $(bindir)
	ar crs $(bindir)/$@ $^
	@ echo

staticdebug:  libastyled.a
libastyled.a:  $(OBJad)
	@ mkdir -p $(bindir)
	ar crs $(bindir)/$@ $^
	@ echo

all:  release debug shared shareddebug static staticdebug

clean:
	rm -f $(objdir)/*.o

install:
	@ $(INSTALL) -m 755 -d $(ipath)
	$(INSTALL) -m 755 $(bindir)/astyle  $(ipath)

uninstall:
	rm -f $(ipath)/astyle
