# Standard variable for creation of .o files and things that use .o
# files (lib, bin).

ifeq (,$(MAKEFILE_O_VARS))
MAKEFILE_O_VARS = TRUE

ifeq (,$(MAKEFILE_PENV_VARS))
  include $(DTOOL)/include/Makefile.penv.vars
endif

# First, examine the OPTIMIZE variable.  Make sure it is set to one
# of the allowed values.
ifeq (1,$(OPTIMIZE))
  # Optimize 1 : no compiler optimizations, full debugging symbols.
  DEBUGSYMBOLS = yes
else
  ifeq (2,$(OPTIMIZE))
    # Optimize 2 : full compiler optimizations, full debugging symbols.
    DEBUGSYMBOLS = yes
  else
    ifeq (3,$(OPTIMIZE))
      # Optimize 3 : full compiler optimizations, no debugging symbols.
    else
      ifeq (4,$(OPTIMIZE))
        # Optimize 4 : full compiler optimizations, no debugging symbols,
        #              and assertion tests eliminated via NDEBUG.
      else
        ifeq (,$(OPTIMIZE))
          # OPTIMIZE unspecified: assume Optimize 1.
          OPTIMIZE = 1
          DEBUGSYMBOLS = yes
        else
          # OPTIMIZE is none of the above: assume Optimize 4.
          OPTIMIZE = 4
        endif # old opt
      endif # opt4
    endif # opt3
  endif # opt2
endif # opt1

BASETARGET := $(TARGET)
DEBUGNAME :=

ifeq (WIN32,$(PENV))
  # If we are compiling with symbols, we rename the target to target_d.
  # This helps us keep debug vs. nondebug libraries separate, and also
  # helps us play along with Python's importing conventions.  But it's
  # a big mess here in the makefiles.

  ifeq (yes,$(DEBUGSYMBOLS))
    DEBUGNAME := _d
  endif

  ifneq (,$(findstring .so,$(suffix $(TARGET))))
    TARGET := $(TARGET:%.so=%$(DEBUGNAME).dll)
  else
    ifneq (,$(findstring .a,$(suffix $(TARGET))))
      TARGET := $(TARGET:%.a=%$(DEBUGNAME).lib)
    else
      TARGET := $(TARGET:%=%.exe)
    endif # .a
  endif # .so
endif # WIN32
ifeq (OSX,$(PENV))
  ifneq (,$(findstring .so,$(suffix $(TARGET))))
    TARGET := $(TARGET:%.so=%.dylib)
  endif # .so
endif # OSX

IGATESYSPATH = -S$(DTOOL)/include/parser-inc -DCPPPARSER -D__cplusplus $(SYSTEM_IGATE_FLAGS)

# Delete the default suffixes
.SUFFIXES:

# Make sure that CT_INCLUDE_PATH is set.
ifeq (,$(CT_INCLUDE_PATH))
  export CT_INCLUDE_PATH = .
endif

IPATH_DIRS := $(strip $(shell ctpathadjust $(CT_INCLUDE_PATH))) $(patsubst -I%,%,$(IPATH))

# Legacy.
ifneq (,$(PYTHON_INCLUDE))
  IPATH_DIRS += $(PYTHON_INCLUDE)
endif
ifneq (,$(NSPR_INCLUDE))
  IPATH_DIRS += $(NSPR_INCLUDE)
endif

# Add one more for dtool_config.h
IPATH_DIRS += $(DTOOL)

# Convert CT_INCLUDE_PATH to cc/CC compatible flag string
ifeq (WIN32_VC,$(PENV_COMPILER))
  IPATH := $(foreach path,$(IPATH_DIRS),-I"$(shell cygpath -w $(path))")
  SYS_IPATH := -I"$(shell cygpath -w /msvc98/Include)"
else
  IPATH := $(patsubst %,-I"%",$(IPATH_DIRS))
endif

##### Playstation2 can't handle .so's.  The rename to .a's is handled here
##### and acted upon in Makefile.so.rules.  (CSN)

ifeq (PS2, $(PENV))
  IPATH += -I/for/program/sce/ee/include

  # Uncommenting this line will make the ps2 look for files on the dvd
  # instead of on the host dev machine.

  PS2_FILE_PREFIX := #### PS2_DVD_FILES

  ifneq (,$(findstring .so,$(suffix $(TARGET))))
    TARGET := $(TARGET:%.so=%.a)
  endif
endif

##### End ps2

COMPILE_FLAGS =
COMPILE_DEFS =
OPTIMIZE_FLAGS =

ifeq (SGI,$(PENV))
  # Standard applications and flags.
  CC            = cc $(SGI_BINFORMAT_CC)
  C++FLAGS      = -fullwarn $(WOFF_LIST)

  # MIPSPro compiler cannot simultaneously optimize and encode
  # debugging symbols.

  ifeq (1,$(OPTIMIZE))
    COMPILE_FLAGS = -g
  else
    ifeq (2,$(OPTIMIZE))
      COMPILE_FLAGS =
      OPTIMIZE_FLAGS = -O2 -OPT:Olimit=2500
    else
      ifeq (3,$(OPTIMIZE))
        COMPILE_DEFS = -DOPTIMIZE
        OPTIMIZE_FLAGS = -O2 -OPT:Olimit=2500
      else
        ifeq (4,$(OPTIMIZE))
          COMPILE_DEFS = -DOPTIMIZE -DNDEBUG
          OPTIMIZE_FLAGS = -O2 -OPT:Olimit=2500
        endif # opt4
      endif # opt3
    endif # opt2
  endif # opt1
  
  C++           = CC $(SGI_BINFORMAT_CC)

  ifeq ($(SGI_BINFORMAT),o32)
    
    # These are the warning numbers according to the o32 compiler
    # 3317 - controlling expression is constant, e.g. if (0) { ... }
    # 3262 - function 'blah' was declared but never used
    WOFF_LIST   = -woff 3317,3262
    
  else  # ($(SGI_BINFORMAT),n32)
    
    # These are the warning numbers according to the n32 compiler
    # 1174 - function 'blah' was declared but never used
    # 1201 - trailing comma is nonstandard.
    # 1209 - controlling expression is constant, e.g. if (0) { ... }
    # 1234 - access control not specified, 'public' by default
    # 1355 - extra ";" ignored
    # 1375 - destructor for base class is not virtual.
    #    this one actually is bad.  But we got alot of them from the classes
    #    that we've derived from STL collections.  Beware of this.
    # 3322 - omission of explicit type is nonstandard ("int" assumed)
    WOFF_LIST   = -woff 1174,1201,1209,1234,1355,1375,3322

    # Linker warnings
    # 85 - definition of SOMESYMBOL in SOMELIB preempts that of definition in
    #      SOMEOTHERLIB.
    WOFF_LIST   += -Wl,-LD_MSG:off=85
    
    # We'll also woff this one if we are compiling DELIVERY_PLAYER, which
    # leaves out large chunks of code.
    # 3201 - parameter was never referenced.
    ifneq (,$(DELIVERY_PLAYER))
      WOFF_LIST += -woff 3201
    endif
  endif   # $(SGI_BINFORMAT)

  CFLAGS += -DPENV_SGI
  C++FLAGS += -DPENV_SGI
endif     # (SGI,$(PENV))

ifeq (OSX,$(PENV))
  # Standard applications and flags
  CC = cc

  ifeq (1,$(OPTIMIZE))
    COMPILE_FLAGS = -g
  else
    ifeq (2,$(OPTIMIZE))
      COMPILE_FLAGS = -g
      OPTIMIZE_FLAGS = -O2
    else
      ifeq (3,$(OPTIMIZE))
        COMPILE_FLAGS = -g
        COMPILE_DEFS = -DOPTIMIZE
        OPTIMIZE_FLAGS = -O2
      else
        ifeq (4,$(OPTIMIZE))
          COMPILE_DEFS = -DOPTIMIZE -DNDEBUG
          OPTIMIZE_FLAGS = -O2
        endif # (4,$(OPTIMIZE))
      endif # (3,$(OPTIMIZE))
    endif # (2,$(OTPIMIZE))
  endif  # (1,$(OPTIMIZE))

  C++ = cc
  TMPTARGETTHING := __$(BASETARGET:%.so=%_so)_find_me__
  CFLAGS += -DPENV_OSX -DLIB_SYMBOL_NAME=$(TMPTARGETTHING)
  C++FLAGS += -DPENV_OSX -DLIB_SYMBOL_NAME=$(TMPTARGETTHING) -ftemplate-depth-30
endif     # (OSX,$(PENV))

ifeq (Linux,$(PENV))
  # Standard applications and flags.
  CC            = gcc

  ifeq (1,$(OPTIMIZE))
    COMPILE_FLAGS = -g
  else
    ifeq (2,$(OPTIMIZE))
      COMPILE_FLAGS = -g
      OPTIMIZE_FLAGS = -O2
    else
      ifeq (3,$(OPTIMIZE))
        COMPILE_FLAGS = -g
        COMPILE_DEFS = -DOPTIMIZE
        OPTIMIZE_FLAGS = -O2
      else
        ifeq (4,$(OPTIMIZE))
          COMPILE_DEFS = -DOPTIMIZE -DNDEBUG
          OPTIMIZE_FLAGS = -O2
        endif # opt4
      endif # opt3
    endif # opt2
  endif # opt1
  
  C++           = g++

  CFLAGS += -DPENV_LINUX
  C++FLAGS += -DPENV_LINUX -ftemplate-depth-30
endif     # $(Linux,$(PENV))

ifeq (PS2,$(PENV))
  # Playstation2 apps and flags.

  CC = ee-gcc
  C++ = ee-g++

  ifeq (1,$(OPTIMIZE))
    COMPILE_FLAGS = -g
  else
    ifeq (2,$(OPTIMIZE))
      COMPILE_FLAGS = -g
      OPTIMIZE_FLAGS = -O2
    else
      ifeq (3,$(OPTIMIZE))
        COMPILE_FLAGS = -g
        COMPILE_DEFS = -DOPTIMIZE
        OPTIMIZE_FLAGS = -O2
      else
        ifeq (4,$(OPTIMIZE))
          COMPILE_DEFS = -DOPTIMIZE -DNDEBUG
          OPTIMIZE_FLAGS = -O2
        endif # opt4
      endif # opt3
    endif # opt2
  endif # opt1

  CFLAGS += -DPENV_PS2
  C++FLAGS += -DPENV_PS2 -ftemplate-depth-30

endif	# $(PS2,$(PENV))

ifeq (WIN32,$(PENV))
  # Standard applications and flags.
  # Note:  You must also set LDFLAGS and LINKFLAGS in Makefile.bin.vars
  CC 	  = cl
  C++     = cl

  # tell cl.exe to generate .sbr files, which bscmake can compile into Msdev source browser info
  ifeq (1,$(CL_MAKE_BROWSE_INFO))
      BROWSEINFO_FLAG := -Fr 
  endif

  ifeq (1,$(OPTIMIZE))
    # -Fd is change to write out per-obj .pdb, to allow parallelization
    COMPILE_FLAGS = -MDd -GZ -Zi $(BROWSEINFO_FLAG) -Fd$(basename $@) 
    COMPILE_DEFS = -D_DEBUG
    LDTYPE := -DLL -DEBUG
  else
    ifeq (2,$(OPTIMIZE))
      COMPILE_FLAGS = -MDd -Zi -Fd$(basename $@)
      COMPILE_DEFS = -D_DEBUG
      OPTIMIZE_FLAGS = -O2 -Ob1 -Ogity -G6
      LDTYPE := -DLL -DEBUG
    else
      ifeq (3,$(OPTIMIZE))
        COMPILE_FLAGS = -MD
        COMPILE_DEFS = -DOPTIMIZE
        OPTIMIZE_FLAGS = -O2 -Ob1 -Ogity -G6 -Gi-
	LDTYPE := -DLL
      else
        ifeq (4,$(OPTIMIZE))
          COMPILE_FLAGS = -MD
          COMPILE_DEFS = -DOPTIMIZE -DNDEBUG
          OPTIMIZE_FLAGS = -O2 -Ob1 -Ogity -G6 -Gi-
          LDTYPE := -DLL
        endif # opt4
      endif # opt3
    endif # opt2
  endif # opt1

   # removed -FD (generate file-depedency vc60.idb files) to allow parallelization
   CFLAGS += -nologo -W3 -EHsc -Zm250 -D_WINDOWS -DWIN32 -D_WINDLL -DSTRICT -DPENV_WIN32 -DWIN32_VC
   C++FLAGS += -nologo -W3 -EHsc -Zm250  -D_WINDOWS -DWIN32 -D_WINDLL -DSTRICT -DPENV_WIN32 -DWIN32_VC

endif     # $(WIN32,$(PENV))

ifeq (SunOS,$(PENV))
  # Standard applications and flags.
  CC            = egcc

  ifeq (1,$(OPTIMIZE))
    OPTIMIZE_FLAGS = -g
  else
    ifeq (2,$(OPTIMIZE))
      OPTIMIZE_FLAGS = -O2 -g
    else
      ifeq (3,$(OPTIMIZE))
        OPTIMIZE_FLAGS = -O2 -g -DOPTIMIZE -DNDEBUG
      else
        ifeq (4,$(OPTIMIZE))
          OPTIMIZE_FLAGS = -O2 -DOPTIMIZE -DNDEBUG
        endif # opt4
      endif # opt3
    endif # opt2
  endif # opt1
  
  C++           = eg++-new -DUsePthreads

  CFLAGS += -DPENV_SunOS
  C++FLAGS += -DPENV_SunOS
endif     # $(SunOS,$(PENV))

INTERROGATE	= interrogate
IGATEFLAGS	=


ODIR            = .

# Set the default target
install all: $(TARGET)

endif   # MAKEFILE_O_VARS

