#*****************************************************************************
#
# $Id$
#
#Filename   : Makefile.vxworks
#Description: makefile to be used in order to compile libcurl for VxWoorks 6.3.
#
#How to use:
#             1. Adjust environment variables at the file begining
#             2. Open the Command Prompt window and change directory ('cd')
#                into the 'lib' folder
#             3. Add <CYGWIN>/bin folder to the PATH environment variable
#                For example type 'set PATH=C:/embedded/cygwin/bin;%PATH%'
#             4. Build the library by typing 'make -f ./Makefile.vxworks'
#             As a result the libcurl.a should be created in the 'lib' folder.
#             To clean package use 'make -f ./Makefile.vxworks clean'
#Requirements:
#             1. WinXP machine
#             2. Full CYGWIN installation (open source) with GNU make version
#                v3.78 or higher
#             3. WindRiver Workbench with vxWorks 6.3 (commercial)
#*****************************************************************************

# ----------------------------------------------------------------------
# Environment
# ----------------------------------------------------------------------

export WIND_HOME := C:/embedded/Workbench2.5.0.1
export WIND_BASE := $(WIND_HOME)/vxworks-6.3
export WIND_HOST_TYPE := x86-win32

# BUILD_TYE:= <debug>|<release> (build with debugging info or optimized)
BUILD_TYPE := debug
USER_CFLAGS:=

# directories where to seek for includes and libraries
OPENSSL_INC := D:/libraries/openssl/openssl-0.9.8a-vxWorks6.3/include
OPENSSL_LIB := D:/libraries/openssl/openssl-0.9.8a-vxWorks6.3
ZLIB_INC    := D:/libraries/zlib/zlib-1.2.3-VxWorks6.3/zlib-1.2.3
ZLIB_LIB    := D:/libraries/zlib/zlib-1.2.3-VxWorks6.3/binaries/vxworks_3.1_gnu/Debug/lib
ARES_INC    :=
ARES_LIB    :=


# ----------------------------------------------------------------------
# Compiler 
# ----------------------------------------------------------------------

CC := ccppc
AR := arppc
LINK := ccppc
CFLAGS := -D__GNUC__ -D__ppc__ -msoft-float -fno-builtin -mcpu=604 -mlongcall -DCPU=PPC604 -D_GNU_TOOL -Wall -W -Winline $(USER_CFLAGS)
LDFLAGS := -nostdlib -Wl,-i -Wl,-X 
INCLUDE_FLAG := -I
C_DEBUGFLAG := -g
C_OPTFLAG := -O2
COMPILE_ONLY_FLAG := -c
OBJ_EXTENSION := .o
CC_OBJ_OUTPUT = -o $@
ARFLAGS := -rc
LIBS_FLAG := -l
LIBS_DIRFLAG:= -L
LD_DEBUGFLAG := $(C_DEBUGFLAG)
EXECUTE_EXTENSION := .out
TOOL_CHAIN_BIN := $(WIND_HOME)/gnu/3.4.4-vxworks-6.3/$(WIND_HOST_TYPE)/bin/

# ----------------------------------------------------------------------

# Add -DINET6 if the OS kernel image was built with IPv6 support
# CFLAGS += -DINET6

# Set up compiler and linker flags for debug or optimization
ifeq ($(BUILD_TYPE), debug)
CFLAGS += $(C_DEBUGFLAG)
LDFLAGS += $(LD_DEBUGFLAG)
else
CFLAGS += $(C_OPTFLAG)
endif

# ----------------------------------------------------------------------

# Main Makefile and possible sub-make files
MAKEFILES := Makefile.vxworks

# List of external include directories
#-----
# IMPORTANT: include OPENSSL directories before system
#            in order to prevent WindRiver OpenSSL to be used.
#-----
INCLUDE_DIRS := ../include $(OPENSSL_INC) $(ZLIB_INC) $(ARES_INC) $(WIND_BASE)/target/h $(WIND_BASE)/target/h/wrn/coreip

# List of external libraries and their directories
LIBS_LIST := .
LIB_DIRS  := .
ifneq ($(OPENSSL_LIB), )
LIBS_LIST += crypto ssl
LIB_DIRS  += $(OPENSSL_LIB)
endif
ifneq ($(ZLIB_LIB), )
LIBS_LIST += z
LIB_DIRS  += $(ZLIB_LIB)
endif
ifneq ($(ARES_LIB), )
LIBS_LIST += ares
LIB_DIRS  += $(ARES_LIB)
endif

# Add include and library directories and libraries
CFLAGS += $(INCLUDE_DIRS:%=$(INCLUDE_FLAG)%)
LDFLAGS += $(LIB_DIRS:%=$(LIBS_DIRFLAG)%)

# List of targets to make for libs target
LIBS_TARGET_LIST := libcurl.a

# List of execuatble applications to make in addition to libs for all target
EXE_TARGET_LIST :=

# Support for echoing rules
# If ECHORULES variable was set (for example, using 'make' command line)
#  some shell commands in the rules will be echoed
ifneq ($(strip $(findstring $(ECHORULES), yes YES 1 true TRUE)),)
_@_ :=
else
_@_ := @
endif

# Directory to hold compilation intermediate files
TMP_DIR := tmp

# Get sources and headers to be compiled
include Makefile.inc

# List of headers
INCLUDE_FILES := $(HHEADERS)
INCLUDE_FILES += $(shell find ../include -name \*.h)

# List of sources
OBJLIST := $(CSOURCES:%.c=$(TMP_DIR)/%$(OBJ_EXTENSION))


# ----------------------------------------------------------------------

#### default rule
# It should be first rule in this file
.PHONY: default
default: libcurl.a

#### Compiling C files
$(TMP_DIR)/%$(OBJ_EXTENSION): %.c $(MAKEFILES)
	@echo Compiling C file $< $(ECHO_STDOUT)
	@[ -d $(@D) ] || mkdir -p $(@D)
	$(_@_) $(TOOL_CHAIN_BIN)$(CC) $(COMPILE_ONLY_FLAG) $(CFLAGS) $< $(CC_OBJ_OUTPUT)

#### Creating library
$(LIBS_TARGET_LIST): $(INCLUDE_FILES) $(MAKEFILES) $(OBJLIST)
	@echo Creating library $@ $(ECHO_STDOUT)
	$(_@_) [ -d $(@D) ] || mkdir -p $(@D)
	$(_@_) rm -f $@
	$(_@_) $(TOOL_CHAIN_BIN)$(AR) $(ARFLAGS) $@ $(filter %$(OBJ_EXTENSION), $^)

#### Creating application
$(EXE_TARGET_LIST): $(INCLUDE_FILES) $(MAKEFILES) $(LIBS_TARGET_LIST)
	@echo Creating application $@
	@[ -d $(@D) ] || mkdir -p $(@D)
	$(_@_) $(TOOL_CHAIN_BIN)$(LINK) $(CC_OBJ_OUTPUT) $($(@)_EXE_OBJ_LIST) $(LDFLAGS) $($(@)_EXE_LIBS_NEEDED:%=$(LIBS_FLAG)%) $(LIBS_LIST:%=$(LIBS_FLAG)%) $(USER_LIBS_LIST) $(USER_LIBS_LIST)

#### Master Targets
libs: $(LIBS_TARGET_LIST)
	@echo All libs made.

all: $(LIBS_TARGET_LIST) $(EXE_TARGET_LIST) $(INCLUDE_TARGET_LIST)
	@echo All targets made.

# Clean up
.PHONY: clean
clean:
	$(_@_) rm -rf $(TMP_DIR)
	@echo libcurl was cleaned.
