#  
# Makefile for Brutus test/sample code.
# Copyright (C) 2004-2007 OMC Denmark ApS.
# 
# 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 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


ifndef TOP_INVOKED
    # ACE/TAO settings
    ifndef ACE_ROOT
        $(message ACE_ROOT not defined, assuming system wide installation)
        ORB_INCLUDE := -I/usr/include/
        ORB_LDFLAGS := -L/usr/lib
        TAO_IDL := /usr/bin/tao_idl
    else
        $(message ACE_ROOT defined, assuming local ACE/TAO source installation)
        ifndef TAO_ROOT
            TAO_ROOT := $(ACE_ROOT)/TAO
        endif
        ORB_INCLUDE := -I$(ACE_ROOT) -I$(TAO_ROOT) 
        ORB_LDFLAGS := -L$(ACE_ROOT)/lib
        TAO_IDL := $(ACE_ROOT)/bin/tao_idl
    endif

    # Compiler settings
    CC := gcc
    OPT = -Os
    DEBUG := -g -O0 -fno-inline
    CFLAGS := -Wall $(DEBUG) 
    INCLUDE := $(ORB_INCLUDE) -I$(shell pwd)/../idl_output  -I$(shell pwd)/.. 
    LDFLAGS := $(ORB_LDFLAGS)
    LIBS := -lTAO_PortableServer -lTAO -lACE
endif

# Directories
IDL_ROOT := $(shell pwd)/../../../brutus_loader/idl
IDL_OUTPUT_DIR := $(shell pwd)

# TAO
TAO_IDLFLAGS := -o $(IDL_OUTPUT_DIR) -GI -GIh S_implskel.h -GIs S_implskel.cpp -in -Ce -ci "C.i" -si "S.i"

DEPS := deps

IDL_FILES := $(IDL_ROOT)/types.idl \
             $(IDL_ROOT)/BrutusCheck.idl \
             $(IDL_ROOT)/IMAPIAdviseSink.idl \
             $(IDL_ROOT)/IMAPIProgress.idl

# Get IDL file names without directory prefix
BARE_NAMES := $(notdir $(IDL_FILES))

# Get names of *S.cpp files
SKEL_SOURCES := $(subst .idl,S.cpp,$(subst $(IDL_ROOT),$(IDL_OUTPUT_DIR),$(IDL_FILES)))

# Get names of *S_impl.cpp files
IMPL_SOURCES := $(BARE_NAMES:.idl=S_impl.cpp)

# Get skeleton objects
SKEL_OBJS := $(SKEL_SOURCES:.cpp=.o)

# Get implementation objects
IMPL_OBJS := $(BARE_NAMES:.idl=S_impl.o)

# All sources and objects
SOURCES := $(SKEL_SOURCES) $(IMPL_SOURCES)
OBJS := $(SKEL_OBJS) $(IMPL_OBJS)


all: depend $(OBJS)

stubs: very_clean idl

.cpp.o:
	$(CC) $(CFLAGS) $(INCLUDE) -c $<

idl: $(IDL_FILES)
	@echo
	@for file in $(IDL_FILES); do \
		(echo Compiling $$file ; cd $(IDL_ROOT) && $(TAO_IDL) $(TAO_IDLFLAGS) $$file); \
	done

depend :
	$(CC) $(INCLUDE) -MM -E $(SOURCES) > $(DEPS)

clean:
	-rm -f $(OBJS) *~

very_clean:
	-rm -f $(OBJS) $(IDL_OUTPUT_DIR)/*S.* $(IDL_OUTPUT_DIR)/*C.* $(IDL_OUTPUT_DIR)/*S_implskel.* *~


.PHONY: all stubs idl depend clean very_clean

sinclude $(DEPS)


