####################################################################
#         Makefile rules for compiling ocaml programs              #
####################################################################

####################################################################
### Try to automatically guess OS

ifeq (${OSCOMP},cygwingnuc) # Define this if compiling with Cygwin GNU C
  OSARCH=win32gnuc
  ETAGS=/bin/etags
  buildexecutable:: win32rc/unison.res.lib
else
# Win32 system
ifeq (${OSTYPE},cygwin32) # Cygwin Beta 19
  OSARCH=win32
  ETAGS=/bin/etags
else
ifeq (${OSTYPE},cygwin)	  # Cygwin Beta 20
  OSARCH=win32
  ETAGS=/bin/etags
else

# Unix system
ifeq ($(shell uname),SunOS)
  OSARCH=solaris
else
ifeq ($(shell uname),Darwin)
  OSARCH=osx
else
ifeq ($(shell uname),OpenBSD)
  OSARCH=OpenBSD
endif
endif
endif
ETAGS=etags
endif
endif
endif

# The OCaml lib dir is used by all versions
# It is extracted from 'ocamlc -v' and Windows '\' separators are turned
# to Unix '/' separators, and extraneous control-M's are deleted.
# Unfortunately there is a literal control-M buried in this, I'd rather
# get rid of it...
# OCAMLLIBDIR=$(shell ocamlc -v | tail -n -1 | sed -e 's/.* //g' | sed -e 's/\\/\//g' | sed -e 's///g')
# Better(?) version, June 2005:
OCAMLLIBDIR=$(shell ocamlc -v | tail -n -1 | sed -e 's/.* //g' | sed -e 's/\\/\//g' | tr -d '\r')

## BCP (6/05) an alternative, but not quite working, version
## suggested by Nick Montfort:
# OCAMLLIBDIR=$(shell ocamlc -v | sed -n '$p' | sed -e 's/^Standard library directory: //' | sed -e 's/\\/\//g' | sed -e 's/\r//g')

# User interface style:
#   Legal values are
#     UISTYLE=text
#     UISTYLE=gtk
#     UISTYLE=gtk2
#     UISTYLE=mac     (old and limited, but working)
#     UISTYLE=macnew  (spiffy, but not yet extensively tested)
#
# This should be set to an appropriate value automatically, depending
# on whether the lablgtk library is available
LABLGTKLIB=$(OCAMLLIBDIR)/lablgtk
LABLGTK2LIB=$(OCAMLLIBDIR)/lablgtk2
##BCP [3/2007]: Removed temporarily, since the OSX UI is not working well 
## at the moment and we don't want to confuse people by building it by default
ifeq ($(OSARCH),osx)
  UISTYLE=macnew
else
  ifeq ($(wildcard $(LABLGTK2LIB)),$(LABLGTK2LIB))
    UISTYLE=gtk2
  else
    UISTYLE=text
endif
endif
buildexecutable::
	@echo UISTYLE = $(UISTYLE)

####################################################################
### Default parameters

INCLFLAGS=-I lwt -I ubase
CAMLFLAGS+=$(INCLFLAGS)

ifeq ($(OSARCH),win32)
  # Win32 system
  EXEC_EXT=.exe
  OBJ_EXT=.obj
  CWD=.
  CLIBS+=-cclib win32rc/unison.res
  STATICLIBS+=-cclib win32rc/unison.res
  buildexecutable::
	@echo Building for Windows
else
  # Unix system, or Cygwin with GNU C compiler
  OBJ_EXT=.o
  CWD=$(shell pwd)
  ifeq ($(OSARCH),win32gnuc)
    EXEC_EXT=.exe
    CLIBS+=-cclib win32rc/unison.res.lib
    STATIC=false                      # Cygwin is not MinGW :-(
    buildexecutable::
	@echo Building for Windows with Cygwin GNU C
  else
    EXEC_EXT=
    # openpty is in the libutil library
    ifneq ($(OSARCH),solaris)
      ifneq ($(OSARCH),osx)
        CLIBS+=-cclib -lutil
      endif
    endif
    buildexecutable::
	@echo Building for Unix
  endif
endif

buildexecutable::
	@echo NATIVE = $(NATIVE)
	@echo THREADS = $(THREADS)
	@echo STATIC = $(STATIC)
	@echo OSTYPE = $(OSTYPE)
	@echo OSARCH = $(OSARCH)

ubase/projectInfo.ml: mkProjectInfo
	echo 'let myName = "'$(NAME)'";;' > $@
	echo 'let myVersion = "'$(VERSION)'";;' >> $@
	echo 'let myMajorVersion = "'$(MAJORVERSION)'";;' >> $@

clean::
	$(RM) ubase/projectInfo.ml

####################################################################
### Unison objects and libraries

ifeq ($(UISTYLE),mac)
  buildexecutable:: macexecutable
  UIMACDIR=uimac
else
ifeq ($(UISTYLE),macnew)
  buildexecutable:: macexecutable
  UIMACDIR=uimacnew
else
  buildexecutable:: $(NAME)$(EXEC_EXT)
endif
endif

# NOTE: the OCAMLLIBDIR is not getting passed correctly?
# The two cases for cltool are needed because Xcode 2.1+
# builds in build/Default/, and earlier versions use build/
macexecutable: $(NAME)-blob.o
	sed -e's/@@VERSION@@/$(VERSION)/' $(UIMACDIR)/Info.plist.template > $(UIMACDIR)/Info.plist
	(cd $(UIMACDIR); xcodebuild OCAMLLIBDIR="$(OCAMLLIBDIR)")
	if [ -e $(UIMACDIR)/build/Default ]; then \
	  gcc $(UIMACDIR)/cltool.c -o $(UIMACDIR)/build/Default/Unison.app/Contents/MacOS/cltool -framework Carbon; \
	else \
	  gcc $(UIMACDIR)/cltool.c -o $(UIMACDIR)/build/Unison.app/Contents/MacOS/cltool -framework Carbon; \
	fi

# OCaml objects for the bytecode version
# File extensions will be substituted for the native code version

OCAMLOBJS += \
          ubase/projectInfo.cmo ubase/myMap.cmo ubase/safelist.cmo \
          ubase/uprintf.cmo ubase/util.cmo ubase/rx.cmo ubase/uarg.cmo \
          ubase/prefs.cmo ubase/trace.cmo \
          \
          lwt/pqueue.cmo lwt/lwt.cmo lwt/lwt_util.cmo lwt/lwt_unix.cmo \
          \
          case.cmo pred.cmo uutil.cmo \
          fileutil.cmo name.cmo path.cmo fspath.cmo fingerprint.cmo \
          abort.cmo osx.cmo \
          props.cmo fileinfo.cmo os.cmo lock.cmo clroot.cmo common.cmo \
          tree.cmo checksum.cmo terminal.cmo \
          transfer.cmo xferhint.cmo remote.cmo globals.cmo copy.cmo \
          stasher.cmo update.cmo \
	  files.cmo sortri.cmo recon.cmo transport.cmo \
          strings.cmo uicommon.cmo uitext.cmo test.cmo 

OCAMLOBJS+=main.cmo

# OCaml libraries for the bytecode version
# File extensions will be substituted for the native code version
OCAMLLIBS+=unix.cma str.cma

COBJS+=osxsupport$(OBJ_EXT) pty$(OBJ_EXT)

########################################################################
### User Interface setup

## Text UI
ifeq ($(UISTYLE), text)
  OCAMLOBJS+=linktext.cmo
endif

## Old Mac UI
ifeq ($(UISTYLE),mac)
  OCAMLOBJS+=uimacbridge.cmo
endif

## New Mac UI
ifeq ($(UISTYLE),macnew)
  OCAMLOBJS+=uimacbridgenew.cmo
  THREADS=true
  OCAMLLIBS+=threads.cma
  INCLFLAGS+=-thread 
endif

## Graphic UI

# Setup the lib directories

# Win32 system : this very Makefile must be used with GNU Make, so that we
#                expect CygWin Bash to be used.
# The directory must be provided following one of the model below :
# - unix, relative      ../../ocaml/lib/labltk
# - unix, absolute      d:/home/foobar/ocaml/lib/labltk
# -  dos, relative      ..\\..\\ocaml\\lib\\labltk
# -  dos, absolute      d:\\home\\foobar\\ocaml\\lib\\labltk

# Patch to make a Windows GUI version come up with no
# console when click-started
#  ifeq ($(OSARCH), win32)
#    COBJS+=winmain.c
#    CFLAGS+=-cclib /subsystem:windows
#  endif

# Gtk GUI
ifeq ($(UISTYLE), gtk)
  CAMLFLAGS+=-I +lablgtk
  OCAMLOBJS+=pixmaps.cmo uigtk.cmo linkgtk.cmo
  OCAMLLIBS+=lablgtk.cma
endif

# Gtk2 GUI
ifeq ($(UISTYLE), gtk2)
  CAMLFLAGS+=-I +lablgtk2
  OCAMLOBJS+=pixmaps.cmo uigtk2.cmo linkgtk2.cmo
  OCAMLLIBS+=lablgtk.cma
endif

####################################################################
### Static build setup

ifeq ($(STATIC), true)
  STATICLIBS+=-cclib -static
endif

####################################################################
### Dependencies

# Include an automatically generated list of dependencies
include .depend

ifeq ($(OSARCH), OpenBSD)
  ifeq ($(shell echo type ocamldot | ksh), file)
    OCAMLDOT=true
  endif
else
  ifeq ($(shell echo type -t ocamldot | bash), file)
    OCAMLDOT=true
  endif
endif

# Rebuild dependencies (must be invoked manually)
.PHONY: depend
depend::
	ocamldep $(INCLFLAGS) *.mli *.ml */*.ml */*.mli > .depend
ifdef OCAMLDOT
	echo 'digraph G {' > dot.tmp
	echo '{ rank = same; "Fileinfo"; "Props"; "Fspath"; "Os"; "Path"; }'\
           >>dot.tmp
	echo '{ rank = same; "Uitext"; "Uigtk"; }'>>dot.tmp
	echo '{ rank = same; "Recon"; "Update"; "Transport"; "Files"; }'\
           >>dot.tmp
	echo '{ rank = same; "Tree"; "Safelist"; }'>>dot.tmp
	echo '{ rank = same; "Uarg"; "Prefs"; }'>>dot.tmp
	ocamldot .depend | tail -n +2 >> dot.tmp
	-dot -Tps -o DEPENDENCIES.ps dot.tmp
endif

####################################################################
### Compilation boilerplate

ifeq ($(DEBUGGING), false)
  ifneq ($(OSARCH), win32)
    ifneq ($(OSARCH), osx)
      # Strip the binary (does not work with MS compiler; might not work
      # under OSX)
      CFLAGS+=-cclib -Wl,-s
    endif
  endif
endif

ifeq ($(PROFILING), true)
  OCAMLC=ocamlcp
else
  OCAMLC=ocamlc
endif
OCAMLOPT=ocamlopt

ifeq ($(NATIVE), true)
  ## Set up for native code compilation

  CAMLC=$(OCAMLOPT)
  ifeq ($(PROFILING), true)
    CAMLFLAGS+=-p
    CLIBS+=-cclib -ldl
  endif

  CAMLOBJS=$(subst .cmo,.cmx, $(subst .cma,.cmxa, $(OCAMLOBJS)))
  CAMLLIBS=$(subst .cma,.cmxa, $(OCAMLLIBS))

else
  ## Set up for bytecode compilation

  CAMLC=$(OCAMLC)
  CAMLFLAGS+=-custom
  ifeq ($(DEBUGGING), true)
    CAMLFLAGS+=-g
  endif

  CAMLOBJS=$(OCAMLOBJS)
  CAMLLIBS=$(OCAMLLIBS)

endif

win32rc/unison.res.lib: win32rc/unison.res
	windres win32rc/unison.res win32rc/unison.res.lib

%.ml: %.mll
	-$(RM) $@
	ocamllex $<

%.cmi : %.mli
	@echo "$(CAMLC): $< ---> $@"
	$(CAMLC) $(CAMLFLAGS) -c $(CWD)/$<

%.cmo: %.ml
	@echo "$(OCAMLC): $< ---> $@"
	$(OCAMLC) $(CAMLFLAGS) -c $(CWD)/$<

%.cmx: %.ml
	@echo "$(OCAMLOPT): $< ---> $@"
	$(OCAMLOPT) $(CAMLFLAGS) -c $(CWD)/$<

%.o %.obj: %.c
	@echo "$(OCAMLOPT): $< ---> $@"
	$(CAMLC) $(CAMLFLAGS) -c $(CWD)/$<

$(NAME)$(EXEC_EXT): $(CAMLOBJS) $(COBJS)
	@echo Linking $@
	$(CAMLC) -verbose $(CAMLFLAGS) -o $@ $(CFLAGS) $(CAMLLIBS) $(CLIBS) $^

# Unfortunately -output-obj does not put .o files into the output, only .cmx
# files, so we have to use $(LD) to take care of COBJS.
$(NAME)-blob.o: $(CAMLOBJS) $(COBJS)
	@echo Linking $@
	$(CAMLC) -output-obj -verbose $(CAMLFLAGS) -o u-b.o $(CFLAGS) $(CAMLLIBS) $(CLIBS) $(CAMLOBJS)
	$(LD) -r -o $@ u-b.o $(COBJS)
	$(RM) u-b.o

%$(EXEC_EXT): %.ml
	$(OCAMLC) -verbose -o $@ $^

######################################################################
### Misc

clean::
	-$(RM) -r *.cmi *.cmo *.cmx *.cma *.cmxa TAGS tags
	-$(RM) -r *.o core gmon.out *~ .*~
	-$(RM) -r *.obj *.lib *.exp
	-$(RM) -r *.tmp *.bak?.tmp .*.bak?.tmp

paths:
	@echo PATH = $(PATH)
	@echo OCAMLLIBDIR = $(OCAMLLIBDIR)



syntax highlighted by Code2HTML, v. 0.9.1