RM_F		    = @rm_f@
PERL		    = @perl@
PARROT	    	= @build_dir@/parrot@exe@
#CONDITIONED_LINE(win32):LIBPARROT = @build_dir@/libparrot@a@
#INVERSE_CONDITIONED_LINE(win32):LIBPARROT = @build_dir@/@blib_dir@/libparrot@a@
O		        = @o@
LOAD_EXT	    = @load_ext@
CC         	    = @cc@
CC_INC     	    = @cc_inc@ -I@build_dir@/include
C_LIBS     	    = @libs@ 
CC_SHARED  	    = @cc_shared@
CFLAGS     	    = $(CC_INC) @ccflags@ @cc_debug@ @ccwarn@ @cc_hasjit@ @cg_flag@ @gc_flag@ $(CC_SHARED)
LINK_DYNAMIC  	= @link_dynamic@
LINK       	    = @link@
LINKFLAGS  	    = @linkflags@ @link_debug@ @ld_debug@ 
LD         	    = @ld@
#CONDITIONED_LINE(win32):LDFLAGS = @ldflags@ @ld_debug@ $(LIBPARROT)
#INVERSE_CONDITIONED_LINE(win32):LDFLAGS = @ldflags@ @ld_debug@ -L@build_dir@/blib/lib/ -lparrot
SHARE_EXT       = @share_ext@
LD_SHARE_FLAGS  = @ld_share_flags@
BUILD_PMC   	= $(PERL) @build_dir@/tools/build/dynpmc.pl
SRC_PMC_DIR	    = src/pmc
DEST_PMC_DIR	= @build_dir@/runtime/parrot/dynext
PMC_GROUP       = $(DEST_PMC_DIR)/bcg_group$(LOAD_EXT)
LIB_BCG         = @build_dir@/blib/lib/libbcg$(SHARE_EXT)
SRC_DIR         = src
INCLUDE_DIR     = include
CP              = @cp@

.SUFFIXES : .c .h .pmc .dump $(O) .str .pir .pbc

.c$(O) :
	$(CC) $(CFLAGS) -I$(@D) @cc_o_out@$@ -c $<

BCG_H_FILES = \
    $(INCLUDE_DIR)/bcg.h \
    $(SRC_DIR)/bcg_private.h \
    $(SRC_DIR)/bcg_logger.h \
    $(SRC_DIR)/bcg_utils.h \
    $(SRC_DIR)/bcg_reg_alloc.h \
    $(SRC_DIR)/bcg_emitter.h

BCG_O_FILES  = \
	$(SRC_DIR)/bcg_logger$(O) \
	$(SRC_DIR)/bcg$(O) \
	$(SRC_DIR)/bcg_utils$(O) \
	$(SRC_DIR)/bcg_op$(O) \
	$(SRC_DIR)/bcg_unit$(O) \
        $(SRC_DIR)/bcg_reg_alloc_vanilla$(O) \
        $(SRC_DIR)/bcg_emitter_pasm$(O)

PMCS = \
 bcg

PMC_GEN_FILES = \
 $(SRC_PMC_DIR)/*.dump \
 $(SRC_PMC_DIR)/*.c \
 $(SRC_PMC_DIR)/*.h \
 $(SRC_PMC_DIR)/*$(O) \
 $(SRC_PMC_DIR)/*$(LOAD_EXT) \
 $(PMC_GROUP)

all: pmcs


#
# Build Targets
#
$(BCG_O_FILES): $(BCG_H_FILES)

$(LIB_BCG): $(BCG_O_FILES)
	$(LD) $(LD_SHARE_FLAGS) $(LDFLAGS) @ld_out@$@ $(BCG_O_FILES) @libs@
    $(CP) @build_dir@/blib/lib/libbcg$(SHARE_EXT) @build_dir@

	
pmcs: $(LIB_BCG)
	@cd $(SRC_PMC_DIR) && $(BUILD_PMC) generate $(PMCS)
	@cd $(SRC_PMC_DIR) && $(BUILD_PMC) compile $(PMCS)
	@cd $(SRC_PMC_DIR) && $(BUILD_PMC) linklibs $(PMCS)
	@cd $(SRC_PMC_DIR) && $(BUILD_PMC) copy "--destination=$(DEST_PMC_DIR)" \
		 $(PMCS)


#
# Test Targets
#
test: all
	cd t && prove && cd ..


#
# Clean Targets
#
testclean: 
	$(RM_F) t/*.pir

clean: testclean
	$(RM_F) $(PMC_GEN_FILES)
	$(RM_F) $(BCG_O_FILES)
	$(RM_F) $(LIB_BCG)

realclean: clean

distclean: realclean
