# File: makefile_APPLE
# Why:  Creates the o2 compiler /compiler using the GNU make program
# Ks:   Depending on your system, this utility might be called gmake or make
#
# O2 placement: 
#   Rlse:   /usr/local/yacco2/bin/o2
#   Dbg:    /usr/local/yacco2/bin/o2_debug
#   Genw:   for the pdf file: yacco2/docs
#           for the *.cpp files
#
# How to run: U must choose the label to execute: Rlse, Dbg, or Genw
#   Rlse --- gen the optimizwd version of O2 with no debug support
#   Dbg --- gen the debug version of O2
#   Genw --- gen the pdf document and the cpp code 
#            from cweb see www.tug.org for details on obtaining code
#            for the literate programming environment aka CWEB
#            and please join Tug. It is a marvelous FOSS
#            Note: U do not need to use Genw as *.cpp and pdf 
#                  document are included in the ``yacco2'' package
#
# ---> Know where the stdc++ library resides and adjust the STDCC_dir variable
#
# Example: gen the compiler/compiler o2 release version using GNU make
#  cd /usr/local//yacco2/compiler/o2
#  make Rlse -f makefile_APPLE
#
# Example: gen programs from cweb
#  make Genw -f makefile_APPLE
#

O2 := /usr/local/yacco2
##CC := /opt/local/bin/c++-mp-4.9
CC := clang++#g++
##LD := /opt/local/bin/ld
LD := ld #apple linker

02_target_dir :=
ifeq ($(MAKECMDGOALS),Rlse)
Prg := o2
O2_target_dir := Release 
Compile_opts :=  -c -pthread  -arch x86_64 #-frepo -fno-implicit-templates -fno-rtti
else
O2_target_dir := Debug
Prg := o2_debug
Compile_opts := -c -g -pthread -arch x86_64
endif

###
# Set up directories
###
Base_o2     := $(O2)
O2_bin      := $(Base_o2)/bin
O2_eternals := $(Base_o2)/externals
O2_docs     := $(Base_o2)/docs
O2_cc       := $(Base_o2)/compiler/o2
O2_docs     := $(Base_o2)/docs
O2_library  := $(Base_o2)/library
O2_library_lib  := $(O2_library)/lib
O2_lib_grms := $(O2_library)/grammars
O2_cc_grms  := $(Base_o2)/compiler/grammars
O2_cc       := $(Base_o2)/compiler/o2
O2_includes :=  -I'$(O2_cc)' -I'$(O2_cc_grms)' -I'$(O2_library)' -I'$(O2_lib_grms)'
O2_req_libraries :=   -lyacco2 -lo2grammars 
O2_lib_directories := -L$(O2_cc_grms)/lib/$(O2_target_dir) -L$(O2_library_lib)/$(O2_target_dir) 

CC_lib      := c++ 
##CC_lib      := stdc++ #gnu c++
LD_PARMS :=  -arch x86_64 -macosx_version_min 10.9 #-stack_size 0x2000000  -no_compact_unwind 

#STDCC_dir := -L/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/
##STDCC_dir := -L/opt/local/lib/gcc49/ #gnu c++
STDCC_dir := -L/usr/lib/

###
#    Compile, link, and move O2 into bin account 
###
Rlse: YYY Move_o2 
Dbg: YYY Move_o2 	

YYY:
	$(CC) -v $(Compile_opts)  $(O2_includes) o2.cpp -o $(Prg).o 
	$(CC) -v $(Compile_opts)  $(O2_includes) common_externs.cpp -o common_externs.o
	$(CC) -v $(Compile_opts)  $(O2_includes) o2_externs.cpp -o o2_externs.o
	$(CC) -v $(Compile_opts)  $(O2_includes) o2_defs.cpp -o o2_defs.o
	$(LD)  $(LD_PARMS) -o $(Prg) $(STDCC_dir) $(O2_lib_directories) $(O2_req_libraries) -l$(CC_lib) -lpthread o2_externs.o o2_defs.o common_externs.o $(Prg).o

Move_o2:
	mv $(Prg) $(O2_bin)
	rm *.o
	
Genw:
	cweave o2_types # struct definitions for o2
	pdftex o2_types
	mv o2_types.pdf $(O2_docs)
	cweave o2 
	pdftex o2
	mv o2.pdf $(O2_docs)
	cweave $(O2_eternals)/common_externs 
	pdftex common_externs
	mv common_externs.pdf $(O2_docs)
	cweave o2externs 
	pdftex o2externs
	mv o2externs.pdf $(O2_docs)
	ctangle -l +e $(O2_eternals)/common_externs
	ctangle -l +e o2externs
	ctangle -l +e o2_defs
	ctangle -l +e o2
	ctangle -l +e o2_types
	sh Makefile_Edit_cweb
	rm *.idx
	rm *.scn
	rm *.toc
	rm *.tex
	rm *.log
	rm *.c
