# Pull in the .o vars if they haven't been read already
ifeq (,$(MAKEFILE_O_VARS))
include $(DTOOL)/include/Makefile.o.vars
endif

# Get the project root
ifeq (,$(CTPROJROOT))
CTPROJROOT := $(shell $(DTOOL)/bin/ctproj -root)
export CTPROJROOT
endif

# Set the SOLOCATIONS file
SOLOCATIONS := ../../etc/so_locations

# Make sure CT_LIBRARY_PATH is set.
ifeq (,$(CT_LIBRARY_PATH))
ifneq (,$(LD_LIBRARY_PATH))
export CT_LIBRARY_PATH := $(LD_LIBRARY_PATH)
else
export CT_LIBRARY_PATH = .
endif
endif

LPATH_DIRS := $(strip $(shell ctpathadjust $(subst :, ,$(CT_LIBRARY_PATH)))) $(patsubst -L%,%,$(LPATH))

# Legacy.
ifneq (,$(PYTHON_LIB))
  LPATH_DIRS += $(PYTHON_LIB)
endif
ifneq (,$(NSPR_LIB))
  LPATH_DIRS += $(NSPR_LIB)
endif

ifeq (WIN32_VC,$(PENV_COMPILER))
  LPATH := $(foreach path,$(LPATH_DIRS),-LIBPATH:"$(shell cygpath -w $(path))")
else # NON-WIN32_VC
  LPATH := $(patsubst %,-L%,$(LPATH_DIRS))
endif # WIN32_VC

ifeq (SGI,$(PENV))
  CLDFLAGS = -Wl,-force_load -Wl,-transitive_link
  C++LDFLAGS = -Wl,-demangle -Wl,-force_load -Wl,-transitive_link
endif

# Figure out what the linker is.  Start with simple defaults.

ifeq (PS2, $(PENV))
  AR = ee-ar
  SET_LD = ee-ld
else
  AR = ar
  SET_LD = ld
endif

SET_LDFLAGS = 
LINKFLAGS =

# C files
ifneq (,$(CFILES))   
SET_LD := $(CC)
SET_LDFLAGS := $(CLDFLAGS) $(CFLAGS)
endif

# C++ files
ifneq (,$(C++FILES)) # If there are C++ files, make it CC
SET_LD := $(C++)
SET_LDFLAGS := $(C++LDFLAGS) $(C++FLAGS)
endif

# C files in the OFILES list 
ifneq (,$(findstring .c,$(suffix $(OFILES))))
SET_LD := $(CC)
SET_LDFLAGS := $(IPATH) $(CLDFLAGS) $(CFLAGS)
endif

# C++ files in the OFILES list
ifneq (,$(findstring .cxx,$(suffix $(OFILES))))
SET_LD := $(C++)
SET_LDFLAGS := $(IPATH) $(C++LDFLAGS) $(C++FLAGS)
endif

# Making a DSO - use C++ for now
ifeq (.so,$(suffix $(TARGET)))
SET_LD := $(C++)
SET_LDFLAGS := $(IPATH) $(C++LDFLAGS) $(C++FLAGS)
endif

# Making a DLL - use C++ for now
ifeq (.dll,$(suffix $(TARGET)))
SET_LD := $(C++)
SET_LDFLAGS := $(C++LDFLAGS) $(C++FLAGS)
endif

# Making a WIN binary file
ifeq (WIN32,$(PENV))
  SET_LD = link
  SET_LDFLAGS = -NOLOGO 

  ifeq (4,$(OPTIMIZE))
      LINKFLAGS += -fixed:no
  else
    ifeq (3,$(OPTIMIZE))
      LINKFLAGS += -fixed:no
    else
      LINKFLAGS += -debug -incremental:no
    endif
  endif
endif

ifeq ($(LD),ld)
  LD := $(SET_LD)
endif
ifeq ($(LD),)
  LD := $(SET_LD)
endif

LDFLAGS += $(SET_LDFLAGS)

ARFLAGS = ruv


















