# Thumb OS Makefile
#
#{{IS_NOTE
#
# Authors:	Tom M. Yeh
# Contributors:
# Create Date:	2000/5/26
# $Header: /cvsroot/jedi/Makefile,v 1.31 2000/10/06 04:45:05 henrichen Exp $
# Purpose:	Makefile of Thumb OS
# Description:
#	make
#	make INIT="a target in test/Makefile"
#	make clean
#	make build_*
#	make clean_*
#
#	To write a Makefile under a subdirectory, you can simply do as follows.
#	1. A list of subdirectories that contain source codes to compile.
#		Define $(SUBDIRS) to have a list of these subdirectories
#		Default: empty
#	2. A list of object to be compiled.
#		Define $(O_LIST) to have a list of these objects
#		Default: all *.cxx, *.c and *.s files (but change extension to $(OEXT))
#		You can define $(O_LIST) as _dummy_ so the default value is not applied.
#	3. A list of object to be linked into the final image
#		Define $(O_LIST_IMAGE)
#		Deafult: $(O_LIST) to have a list of these objects
#		If you don't want to add any object and O_LIST is not empty, you shall
#		define $(O_LIST_IMAGE) as _dummy_
#	4. One or several targets that need special rules
#		Define $(B_TARGET), $(B_TARGET2), ...$(B_TARGET5) as anything you want
#		Define the rule to make $(B_TARGET), ...
#	5. The Makefile is required only if you need to define one of the above
#		macros.
#		If you write the Makefile, remember to include $(TOPDIR)/Rules.make,
#		and all macros shall be defined before Rules.make and all rules,
#		if any, shall be defined after Rules.make (because we have to use
#		its first rule).
#
#	In additions, C_LIST is a list of files to remove during cleanup.
#	If C_LIST is not defined, it is defaulted to $(C_LIST_EXTRA) $(O_LIST) $(B_TARGET)...
#	You might define C_LIST_EXTRA to remove extra files.
#
#	In general, you don't specify any rule before including Rules.make, so
#	'all' is the target of the first rule. To add more dependencies to 'all',
#	you can define $(BUILD_BEFORE) and/or $(BUILD_AFTER) as dependecies that
#	will be built before and after subdirectories. Similarily, there are
#	$(CLEAN_BEFORE) and $(CLEAN_AFTER) for the 'clean' target.
#
#}}IS_NOTE
#
# Copyright (C) 2000 Infoshock Corporation. All Rights Reserved.
#
#{{IS_RIGHT
#}}IS_RIGHT
#

#/////////////////////////////////////////////////
export MAJORVERSION = 0
export MINORVERSION = 0
export PATCHLEVEL = 1
export EXTRALEVEL = 
export API_RELEASE = $(MAJORVERSION).$(MINORVERSION).$(PATCHLEVEL)$(EXTRALEVEL)

#/////////////////////////////////////////////////
include Platform.make

#/////////////////////////////////////////////////
SUBDIRS := jdk lib sys mem ui gdi anakin #filedb
	#Note: jdk shall be compiled before others

#/////////////////////////////////////////////////
#
# Global variables
#
ifeq ($(__OSL__),__WinCE__) #-------------------------
 export PRE_MACRO_LIST := 
 export CeLdFlags:=-nologo commctrl.lib coredll.lib aygshell.lib
 CeNoDefaultLib_LIST:=libc.lib libcd.lib libcmt.lib libcmtd.lib msvcrt.lib msvcrtd.lib

 ifeq ($(__PLATFORM__),__i486__)
  export _WIN32_WCE_EMULATION := 1
  export COMPILE_APPEND:=
  PRE_MACRO_LIST += _X86_ x86 i486 INTERNATIONAL USA INTLMSG_CODEPAGE
  LIBCPU:=x86em
  CeLdFlags += corelibc.lib -MACHINE:IX86 -subsystem:windows -windowsce:emulation
  CeNoDefaultLib_LIST += OLDNAMES.lib

 else #__i486__
  CeLdFlags += -base:0x00010000 -entry:WinMainCRTStartup -subsystem:windowsce,$(CEVER_MAJOR).0

  ifeq ($(__PLATFORM__),__ARM__)
   export COMPILE_APPEND:=arm
   PRE_MACRO_LIST += _M_ARM ARM _ARM_
   LIBCPU:=arm
   CeLdFlags += -align:4096 -MACHINE:ARM
  else
   ERROR_MESSAGE:=$(__PLATFORM__) is not supported yet
  endif
 endif #__PLATFORM__

 ifdef NDEBUG
  CeLdFlags += -RELEASE -OPT:REF -OPT:ICF,10
 else
  CeLdFlags += -DEBUG -PDBTYPE:CON
 endif

 CeLdFlags += -stack:0x10000,0x1000 -incremental:no $(addprefix -nodefaultlib:,$(CeNoDefaultLib_LIST))

 export AR = lib
 export CC = cl$(COMPILE_APPEND)
 export LD = link
 export RC = rc
 export DOT_LIBEXT = .lib

 #set up environment
 ifndef WCEROOT
  ERROR_MESSAGE:=$$WCEROOT, must be defined, e.g., D:/usr/Microsoft eMbedded Tools
 endif
 ifndef SDKROOT
  ERROR_MESSAGE:=$$SDKROOT must be defined, e.g., D:/usr/Windows CE Tools
 endif

 export CEVersion:=$(CEVER_MAJOR)$(CEVER_MINOR)
 export OSVERSION:=wce$(CEVersion)
 export _SDKROOT:=$(subst \,/,$(SDKROOT))
 export INCLUDE:=$(_SDKROOT)/$(OSVERSION)/$(PLATFORM)/include\;$(_SDKROOT)/$(OSVERSION)/$(PLATFORM)/MFC/include\;$(_SDKROOT)/$(OSVERSION)/$(PLATFORM)/ATL/include
 export LIB:=$(_SDKROOT)/$(OSVERSION)/$(PLATFORM)/lib/$(LIBCPU)\;$(_SDKROOT)/$(OSVERSION)/$(PLATFORM)/MFC/lib/$(LIBCPU)\;$(_SDKROOT)/$(OSVERSION)/$(PLATFORM)/ATL/lib/$(LIBCPU)

 export _EXE_PREFIX := ce_
 
else #__WinCE__-------------------------


 ifeq ($(__PLATFORM__),__i486__)
  export COMPILE_PREFIX:=
 else
 ifeq ($(__PLATFORM__),__ARM__)
  export COMPILE_PREFIX:=arm-elf-
 else
 ifeq ($(__PLATFORM__),__RTEMS__)
    export COMPILE_PREFIX=$(shell make -s --no-print-directory -f Makefile.rtems print_target)
 else
    ERROR_MESSAGE:=$(__PLATFORM__) is not supported yet
 endif
 endif
 endif

 export CXX = $(COMPILE_PREFIX)g++
 export AR = $(COMPILE_PREFIX)ar
 export AS = $(COMPILE_PREFIX)as
 export CC = $(COMPILE_PREFIX)gcc
 export CPP = $(CC) -E
 export GDB = $(COMPILE_PREFIX)gdb
 export LD = $(COMPILE_PREFIX)ld
 #export MAKE = make
 export NM = $(COMPILE_PREFIX)nm
 export OBJCOPY = $(COMPILE_PREFIX)objcopy
 export OBJDUMP = $(COMPILE_PREFIX)objdump
 export STRIP = $(COMPILE_PREFIX)strip
 export RC = rc
 export DOT_LIBEXT = .a

 
 ifeq ($(__OSL__),__RTEMS__)
   export _EXE_PREFIX := rtems_
 else
   export _EXE_PREFIX := tvm_
 endif
 
endif #__WinCE__-------------------------

export TOPDIR := $(shell if [ "$$PWD" != "" ]; then echo $$PWD; else pwd; fi)
export JDKDIR := $(TOPDIR)/../jdk
export VERSION_H := $(TOPDIR)/include/jedi/version.h

export _OBJLIST_ := $(TOPDIR)/test/.objlist
		#the total list of object files to link

ifeq ($(__OSL__),__WinCE__)
 SUBDIRS += wince iwf
else
 SUBDIRS += #dibeng drivers
endif

ifeq ($(__OSL__),__RTEMS__)
 SUBDIRS += rtems
endif

ifeq ($(__OSL__),__tvm__)
 SUBDIRS += tvm
endif

ifeq ($(OSTYPE),cygwin32)
 export OSTYPE := cygwin
endif
ifeq ($(OSTYPE),cygwin)
 export DOT_EXEEXT := .exe
endif

ifndef OEXT
 export OEXT := o
 export DOT_OEXT := .$(OEXT)
endif

#
# Control how the all and clean targets are made
#
BUILD_BEFORE := check_deps clean_objlist
BUILD_AFTER := build_test
CLEAN_AFTER := clean_test

#
# Include the pattern and other rules
#
include Rules.make

#
# Extra rules
#
check_deps :
	@if [ ! -d $(TOPDIR)/include$(__OSL__) ] ; then \
		echo Error: $(TOPDIR)/include$(__OSL__) does not exist ; \
		exit 2 ; \
	fi
	@if [ ! -d $(TOPDIR)/include$(__PLATFORM__) ] ; then \
		echo Error: $(TOPDIR)/include$(__PLATFORM__) does not exist ; \
		exit 2 ; \
	fi

clean_objlist :
	@rm -f $(_OBJLIST_)

ifdef INIT
_BUILD_INIT_:= $(_EXE_PREFIX)$(INIT)$(DOT_EXEEXT)
endif

build_test :
	$(MAKE) -C test $(_BUILD_INIT_)

clean_test :
	$(MAKE) -C test clean

#
# VERSION_H
#
ifeq (y,$(shell if [ -f $(VERSION_H) ]; then echo y; fi))
ifeq ("$(API_RELEASE)",$(word 3,$(shell grep API_RELEASE $(VERSION_H))))
_SKIP_VERSION_H_ := y
endif
endif

ifeq (y,$(_SKIP_VERSION_H_))
$(VERSION_H):
else
$(VERSION_H):
	@echo // This is a generated file - changes will be lost if release is changed > .ver
	@echo // Infoshock Corporation >> .ver
	@echo \#define API_RELEASE \"$(API_RELEASE)\" >> .ver
	@echo \#define API_VERSION_CODE `expr $(MAJORVERSION) \\* 65536 + $(MINORVERSION) \\* 256 + $(PATCHLEVEL)` >> .ver
	@echo '#define API_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))' >>.ver
	@mv -f .ver $@
endif
