# File:          Makefile-linux
#
# Makefile for MyroC on a Linux platform
# To utilize, issue the following command to establish an appropriate symbolic link
#
#    ln -s Makefile-linux Makefile
#
# Author:        Henry M. Walker
# Created:       20 September 2013
# Simplified:    28 August 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.0 (and following)
#    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 and utility details vary between Linux and Macintosh
#          MyroC-correct-linux.c   Bluetooth connection
#          MyroC-correct-mac.c   Bluetooth connection
#       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-display.c    rDisplayPicture
#          MyroC-image-file.c storage and retrival of jpeg files

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

TARGET := MyroC
VERSION = 3
SUBVERSION = 0

#----------------------------------------------------------------------------
# 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 -lbluetooth -std=gnu99 -pthread -ljpeg -lglut -lGLU -lGL
ESPEAKFLAGS = -leSpeakPackage 
MYROCFLAGS = -lMyroC

# Location to install the eSpeackPackage header and object file to.. #
INSTALLPATH = ~/MyroCDev
ESPEAKINC = $(INSTALLPATH)/include
ESPEAKLIB = $(INSTALLPATH)/lib

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

# Set linker flags to include the relevant libraries
LDFLAGS=-lglut -lGLU -lGL -L/usr/X11R6/lib -lXmu -lXi -lXext -lX11 -lXt -pthread

#----------------------------------------------------------------------------
# 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: beep-move-demo spirit-song spirit-song-full test-general test-movement test-sensor-1

# 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-variables.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-variables.o MyroC-connect.o MyroC-general.o MyroC-sensor.o MyroC-movement.o MyroC-camera.o MyroC-display.o MyroC-image-file.o

# main MyroC shared library
MyroC.so: MyroC-variables.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
	$(CC) $(CFLAGS) $(LDFLAGS) -shared -Wl,-soname,libMyroC.so.$(VERSION).$(SUBVERSION) -o libMyroC.so.$(VERSION).$(SUBVERSION)  MyroC-variables.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-variables.o:    MyroC-variables.c MyroC.h MyroC-utilities.h
	$(CC) $(CFLAGS) -fPIC -c MyroC-variables.c

MyroC-connect.o:  MyroC-connect-linux.c MyroC.h MyroC-utilities.h
	$(CC) $(CFLAGS) -fPIC -c MyroC-connect-linux.c
	mv MyroC-connect-linux.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.c MyroC.h MyroC-utilities.h
	$(CC) $(CFLAGS) -fPIC -c MyroC-display.c

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

# programs requiring compiled MyroC (but not the installed MyroC library or eSpeakPackage)
spirit-song:  spirit-song.c MyroC.a
	$(CC) $(CFLAGS) -l MyroC -o spirit-song spirit-song.c
#	$(CC) $(CFLAGS) -o spirit-song spirit-song.c MyroC.a

spirit-song-full:  spirit-song-full.c MyroC.o
#	$(CC) $(CFLAGS) -o spirit-song-full spirit-song-full.c MyroC.a
	$(CC) $(CFLAGS) -o spirit-song-full spirit-song-full.c

# programs requiring compiled MyroC and installed eSpeakPackage (but not the MyroC library)
test-general:  test-general.c MyroC.o
	$(CC) $(CFLAGS) $(ESPEAKFLAGS) -o test-general test-general.c MyroC.a

test-movement:  test-movement.c MyroC.o
	$(CC) $(CFLAGS) $(ESPEAKFLAGS) -o test-movement test-movement.c MyroC.a

test-sensor-1:  test-sensor-1.c MyroC.o
	$(CC) $(CFLAGS) $(ESPEAKFLAGS) -o test-sensor-1 test-sensor-1.c MyroC.a

# program required installed MyroC and eSpeakPackage libraries
beep-move-demo:  beep-move-demo.c MyroC.o
	$(CC) $(CFLAGS) $(ESPEAKFLAGS) $(MYROCFLAGS) -o beep-move-demo beep-move-demo.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 shared eSpeakPackage to MyroC and MyroCDev library directories
#    copy header file to include directory
#    make include file public
#    create static implementation file
#    copy static implementation to lib directory
#    make static copy public
#    create dynamic library
#    copy shared library to lib directory 
#    make shared library public


install/eSpeak: 
	 cp eSpeakPackage.h $(ESPEAKINC)/eSpeakPackage.h
	 chmod 755 $(ESPEAKINC)/eSpeakPackage.h
	 $(CC) $(CFLAGS) -fPIC -c eSpeakPackage.c
	 cp eSpeakPackage.o $(ESPEAKLIB)/eSpeakPackage.o
	 chmod 755 $(ESPEAKLIB)/eSpeakPackage.o
	 $(CC) -dynamiclib -o libeSpeakPackage.dylib eSpeakPackage.o
	 cp libeSpeakPackage.dylib $(ESPEAKLIB)/libeSpeakPackage.dylib
	 chmod 755 $(ESPEAKLIB)/libeSpeakPackage.dylib

#----------------------------------------------------------------------------
# install new MyroCDev materials to MyroCDev library directories
#    copy header file 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.a MyroC.so
	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

	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

	 cp libMyroC.so.$(VERSION).$(SUBVERSION) $(MYROCLIB)/libMyroC.so.$(VERSION).$(SUBVERSION)
	 chmod 755 $(MYROCLIB)/libMyroC.so.$(VERSION).$(SUBVERSION)
	 ln -sf $(MYROCLIB)/libMyroC.so.$(VERSION).$(SUBVERSION) $(MYROCLIB)/libMyroC.so.$(VERSION)
	 ln -sf $(MYROCLIB)/libMyroC.so.$(VERSION) $(MYROCLIB)/libMyroC.so
