#
# Asterisk -- A telephony toolkit for Linux.
# 
# Makefile rules
#
# Copyright (C) 2006, Digium, Inc.
#
# Kevin P. Fleming <kpfleming@digium.com>
#
# This program is free software, distributed under the terms of
# the GNU General Public License
#

# Rules for various build phases.
# Each command is preceded by a short comment on what to do.
# Prefixing one or the other with @\# or @ or nothing makes the desired
# behaviour. ECHO_PREFIX prefixes the comment, CMD_PREFIX prefixes the command.

-include $(ASTTOPDIR)/makeopts

.PHONY: dist-clean

# extra cflags to build dependencies. Recursively expanded.
MAKE_DEPS= -MD -MT $@ -MF .$(subst /,_,$@).d -MP

ifeq ($(NOISY_BUILD),)
    ECHO_PREFIX=@
    CMD_PREFIX=@
else
    ECHO_PREFIX=@\#
    CMD_PREFIX=
endif

ifeq ($(findstring DONT_OPTIMIZE,$(MENUSELECT_CFLAGS)),)
    # More GSM codec optimization
    # Uncomment to enable MMXTM optimizations for x86 architecture CPU's
    # which support MMX instructions.  This should be newer pentiums,
    # ppro's, etc, as well as the AMD K6 and K7.  
    #K6OPT=-DK6OPT

    OPTIMIZE?=-O6
    ASTCFLAGS+=$(OPTIMIZE)
endif

# build rules for various targets
%.o: %.c
	$(ECHO_PREFIX) echo "   [CC] $< -> $@"
	$(CMD_PREFIX) $(CC) -o $@ -c $< $(PTHREAD_CFLAGS) $(ASTCFLAGS) $(MAKE_DEPS)

%.o: %.i
	$(ECHO_PREFIX) echo "   [CCi] $< -> $@"
	$(CMD_PREFIX) $(CC) -o $@ -c $< $(PTHREAD_CFLAGS) $(ASTCFLAGS) $(MAKE_DEPS)

%.i: %.c
	$(ECHO_PREFIX) echo "   [CPP] $< -> $@"
	$(CMD_PREFIX) $(CC) -o $@ -E $< $(PTHREAD_CFLAGS) $(ASTCFLAGS) $(MAKE_DEPS)

%.o: %.s
	$(ECHO_PREFIX) echo "   [AS] $< -> $@"
	$(CMD_PREFIX) $(CC) -o $@ -c $< $(PTHREAD_CFLAGS) $(ASTCFLAGS) $(MAKE_DEPS)

%.oo: %.cc
	$(ECHO_PREFIX) echo "   [CXX] $< -> $@"
	$(CMD_PREFIX) $(CXX) -o $@ -c $< $(PTHREAD_CFLAGS) $(filter-out -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations $(AST_DECLARATION_AFTER_STATEMENT),$(ASTCFLAGS)) $(MAKE_DEPS)

%.c: %.y
	$(ECHO_PREFIX) echo "   [BISON] $< -> $@"
	$(CMD_PREFIX) bison -o $@ -d --name-prefix=ast_yy $<

%.c: %.fl
	$(ECHO_PREFIX) echo "   [FLEX] $< -> $@"
	$(CMD_PREFIX) flex -o $@ --full $<

%.so: %.o
	$(ECHO_PREFIX) echo "   [LD] $^ -> $@"
	$(CMD_PREFIX) $(CC) $(STATIC_BUILD) -o $@ $(PTHREAD_CFLAGS) $(ASTLDFLAGS) $(SOLINK) $^ $(PTHREAD_LIBS) $(LIBS)

%.so: %.oo
	$(ECHO_PREFIX) echo "   [LDXX] $^ -> $@"
	$(CMD_PREFIX) $(CXX) $(STATIC_BUILD) -o $@ $(PTHREAD_CFLAGS) $(ASTLDFLAGS) $(SOLINK) $^ $(PTHREAD_LIBS) $(LIBS)

%.eo: %.o
	$(ECHO_PREFIX) echo "   [EMBED] $< -> $@"
	$(CMD_PREFIX) $(ASTTOPDIR)/build_tools/make_linker_eo_script $* > .$@.ld
	$(CMD_PREFIX) $(LD) -r -T .$@.ld -o $@ $<
	$(CMD_PREFIX) rm -f .$@.ld

%.eo: %.oo
	$(ECHO_PREFIX) echo "   [EMBED] $< -> $@"
	$(CMD_PREFIX) $(ASTTOPDIR)/build_tools/make_linker_eo_script $* > .$@.ld
	$(CMD_PREFIX) $(LD) -r -T .$@.ld -o $@ $<
	$(CMD_PREFIX) rm -f .$@.ld

%: %.o
	$(ECHO_PREFIX) echo "   [LD] $^ -> $@"
	$(CMD_PREFIX) $(CXX) $(STATIC_BUILD) -o $@ $(PTHREAD_CFLAGS) $(ASTLDFLAGS) $^ $(PTHREAD_LIBS) $(LIBS)

dist-clean::
