# File:          Makefile-mac
#
# Makefile for compiling and installing MyroC on a Macintosh platform
# To utilize, issue the following command to establish an appropriate symbolic link
#
#    ln -s Makefile-mac Makefile
#
# Author:        Henry M. Walker
# Created:       20 September 2013
# Simplified:    15 October 2015
# Acknowledgements:  basic functionality adapted from an example 
#                       by Marge Coahran,
#                    installation functionality modeled by the MyroC Makefile
#                       by David Cowden and Dilan Ustek
# Organization for Version 3.3
#    MyroC.h provides a full header with all user functions identified
#    MyroC-utilities.h identifies behind-the-scenes functions used 
#          in several user functions
#       helper functions for an individual user function are included in the 
#          implementation file for that function
#    MyroC implementation divided into multiple files:
#       Bluetooth connection details vary for Linux, Macintosh, and Windows
#          MyroC-connect-linux.c   Bluetooth connection
#          MyroC-connect-mac.c     Bluetooth connection
#          MyroC-connect-windows.c Bluetooth connection
#       Bluetooth utility details vary for Linux/Macintosh, and Windows
#          MyroC-utilities-linux-mac.c
#          MyroC-utilities-windows.c
#       MyroC-general.c    rsend, rreceive, general MyroC commands
#       NyroC-sensor.c     sensor commands
#       MyroC-movement.c   movement commands
#       image processing is divided into robot, display, and file/iO sections
#          MyroC-camera.c     rTakePicture
#          MyroC-image-file.c storage and retrival of jpeg files
#          image display      rDisplayPicture, implemented in OS-specific versions
#               MyroC-display-linux-mac.c
#               MyroC-display-windows-interface-manager.c   MyroC-oriented code
#               MyroC-display-windows-opengl-img-handler.c  separate image program
#               

#----------------------------------------------------------------------------

# This program and all MyroC software is licensed under the Creative Commons
# Attribution-Noncommercial-Share Alike 3.0 United States License.
# Details may be found at http://creativecommons.org/licenses/by-nc-sa/3.0/us/

#----------------------------------------------------------------------------
# Version details for MyroC

TARGET := MyroC
VERSION = 3
SUBVERSION = 3

#----------------------------------------------------------------------------
# Use the gcc compiler
CC = gcc

# Set compilation flags
#   -ansi (check syntax against the American National Standard for C
CFLAGS = -ansi -Wall -Wno-deprecated-declarations -std=gnu99 -pthread
LDFLAGS= -framework OpenGL -framework GLUT -ljpeg

# Location to install the MyroC header and object file
INSTALLPATH=/usr/local
MYROCINC = $(INSTALLPATH)/include
MYROCLIB = $(INSTALLPATH)/lib

# Set dynamic library path
#DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH;/usr/local/lib

#----------------------------------------------------------------------------
# build rules:
#
# Each rule takes the following form>  (Note there MUST be a tab,
# as opposed to several spaces, preceeding each command.
#
# target_name:  dependency_list
#	command(s)

all: MyroC.a MyroC.dylib

# List program components, what they depend on, and how to compile each
#   as noted below, several  programs require previous installation of eSpeackPackage

# main MyroC archive package utilizes functions divided into multiple implementation files
MyroC.a: MyroC-utilities.o MyroC-connect.o MyroC-general.o MyroC-sensor.o MyroC-movement.o MyroC-camera.o MyroC-display.o MyroC-image-file.o MyroC.h MyroC-utilities.h
	ar rvs -c MyroC.a MyroC-utilities.o MyroC-connect.o MyroC-general.o MyroC-sensor.o MyroC-movement.o MyroC-camera.o MyroC-display.o MyroC-image-file.o

MyroC.dylib: MyroC-utilities.o MyroC-connect.o MyroC-general.o MyroC-sensor.o MyroC-movement.o MyroC-camera.o MyroC-display.o MyroC-image-file.o MyroC.h MyroC-utilities.h MyroC.a
	$(CC) -dynamiclib $(LDFLAGS) -o libMyroC.dylib.$(VERSION).$(SUBVERSION) MyroC-utilities.o MyroC-connect.o MyroC-general.o MyroC-sensor.o MyroC-movement.o MyroC-camera.o MyroC-display.o MyroC-image-file.o

# use connection for Macintosh
MyroC-utilities.o:    MyroC-utilities-linux-mac.c MyroC.h MyroC-utilities.h
	$(CC) $(CFLAGS) -fPIC -c MyroC-utilities-linux-mac.c
	mv MyroC-utilities-linux-mac.o MyroC-utilities.o

MyroC-connect.o:  MyroC-connect-mac.c MyroC.h MyroC-utilities.h
	$(CC) $(CFLAGS) -fPIC -c MyroC-connect-mac.c
	mv MyroC-connect-mac.o MyroC-connect.o

MyroC-general.o:    MyroC-general.c MyroC.h MyroC-utilities.h
	$(CC) $(CFLAGS) -fPIC -c MyroC-general.c

MyroC-sensor.o: MyroC-sensor.c MyroC.h MyroC-utilities.h 
	$(CC) $(CFLAGS) -fPIC -c MyroC-sensor.c

MyroC-movement.o:   MyroC-movement.c MyroC.h MyroC-utilities.h
	$(CC) $(CFLAGS) -fPIC -c MyroC-movement.c

MyroC-camera.o:     MyroC-camera.c MyroC.h MyroC-utilities.h
	$(CC) $(CFLAGS) -fPIC -c MyroC-camera.c

MyroC-display.o:    MyroC-display-linux-mac.c MyroC.h MyroC-utilities.h
	$(CC) $(CFLAGS) -framework OpenGL -framework GLUT -fPIC -c MyroC-display-linux-mac.c
	mv MyroC-display-linux-mac.o MyroC-display.o

MyroC-image-file.o: MyroC-image-file.c MyroC.h MyroC-utilities.h
	$(CC) $(CFLAGS)  -ljpeg -fPIC -c MyroC-image-file.c

#----------------------------------------------------------------------------
# cleanup rules: To invoke this command, type "make clean".
# Use this target to clean up your directory, deleting (without warning) 
#   object files, old emacs source versions, and core dumps.

clean:
	rm -f *.o *.a *.so *~ core*

#----------------------------------------------------------------------------
# install new MyroCDev materials to MyroCDev library directories
#    copy header files (MyroC.h, MyroC-utilities.h) to include directory
#    make include file public
#    create static implementation file
#    copy static implementation to lib directory
#    make static copy public
#    create symbolic link from MyroC.o to MyroC.o with version and subversion numbes

#    create dyamic library
#    copy shared library to lib directory 
#    make shared library public
#    create symbolic links from MyroC to MyroC with version and subversion numbes
#    create symbolic link to bluetooth library
install/MyroC:  MyroC.h MyroC-utilities.h  MyroC.a MyroC.dylib
#       compile and install MyroC.h
	cp MyroC.h $(INSTALLPATH)/include/MyroC.h.$(VERSION).$(SUBVERSION)
	chmod 755 $(INSTALLPATH)/include/MyroC.h.$(VERSION).$(SUBVERSION)
	ln -sf $(INSTALLPATH)/include/MyroC.h.$(VERSION).$(SUBVERSION) $(INSTALLPATH)/include/MyroC.h.$(VERSION)
	ln -sf $(INSTALLPATH)/include/MyroC.h.$(VERSION) $(INSTALLPATH)/include/MyroC.h
#       compile and install MyroC-utilities.h
	cp MyroC-utilities.h $(INSTALLPATH)/include/MyroC-utilities.h.$(VERSION).$(SUBVERSION)
	chmod 755 $(INSTALLPATH)/include/MyroC-utilities.h.$(VERSION).$(SUBVERSION)
	ln -sf $(INSTALLPATH)/include/MyroC-utilities.h.$(VERSION).$(SUBVERSION) $(INSTALLPATH)/include/MyroC-utilities.h.$(VERSION)
	ln -sf $(INSTALLPATH)/include/MyroC-utilities.h.$(VERSION) $(INSTALLPATH)/include/MyroC-utilities.h

#	$(CC) -lbluetooth -fPIC -c MyroC.c
	cp MyroC.a $(INSTALLPATH)/lib/MyroC.a.$(VERSION).$(SUBVERSION)
	chmod 755 $(INSTALLPATH)/lib/MyroC.a.$(VERSION).$(SUBVERSION)
	ln -sf $(INSTALLPATH)/lib/MyroC.a.$(VERSION).$(SUBVERSION) $(INSTALLPATH)/lib/MyroC.a.$(VERSION)
	ln -sf $(INSTALLPATH)/lib/MyroC.a.$(VERSION) $(INSTALLPATH)/lib/MyroC.a

#	$(CC) -dynamiclib -o libMyroC.dylib.$(VERSION).$(SUBVERSION) MyroC.a
	cp libMyroC.dylib.$(VERSION).$(SUBVERSION) $(MYROCLIB)/libMyroC.dylib.$(VERSION).$(SUBVERSION)
	chmod 755 $(MYROCLIB)/libMyroC.dylib.$(VERSION).$(SUBVERSION)
	ln -sf $(MYROCLIB)/libMyroC.dylib.$(VERSION).$(SUBVERSION) $(MYROCLIB)/libMyroC.dylib.$(VERSION)
	ln -sf $(MYROCLIB)/libMyroC.dylib.$(VERSION) $(MYROCLIB)/libMyroC.dylib
