# File:          Makefile-linux
#
# Makefile for MyroC on a Linux platform
# To utilize, issue the following command to make 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.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
#       Common Bluetooth and 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 4.0 International License.
# Details may be found at http://creativecommons.org/licenses/by-nc-sa/4.0/

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

TARGET := MyroC
VERSION = 3
SUBVERSION = 3

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

# Set compilation flags
CFLAGS = -Wall -Wno-deprecated-declarations -lbluetooth -std=gnu99 -pthread -ljpeg -lglut -lGLU -lGL
ESPEAKFLAGS = -leSpeakPackage 
MYROCFLAGS = -lMyroC

# Location to install the eSpeakPackage header and object file to.. #
INSTALLPATH = /usr/local
#INSTALLPATH = /home/walker/MyroC

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: MyroC.a MyroC.so 

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

# 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

# main MyroC shared library
MyroC.so: 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
	$(CC) $(CFLAGS) $(LDFLAGS) -shared -Wl,-soname,libMyroC.so.$(VERSION).$(SUBVERSION) -o libMyroC.so.$(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 Linux
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-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-linux-mac.c MyroC.h MyroC-utilities.h
	$(CC) $(CFLAGS) -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) -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 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 eSpeak/eSpeakPackage.h $(ESPEAKINC)/eSpeakPackage.h
	 chmod 755 $(ESPEAKINC)/eSpeakPackage.h
	 $(CC) $(CFLAGS) -fPIC -c eSpeak/eSpeakPackage.c
	 cp eSpeak/eSpeakPackage.o $(ESPEAKLIB)/eSpeakPackage.o
	 chmod 755 $(ESPEAKLIB)/eSpeakPackage.o
	 $(CC) -shared -Wl,-soname,libeSpeakPackage.so -o libeSpeakPackage.so eSpeakPackage.o
	 cp libeSpeakPackage.so $(ESPEAKLIB)/libeSpeakPackage.so
	 chmod 755 $(ESPEAKLIB)/libeSpeakPackage.so

#----------------------------------------------------------------------------
# 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/subversion numbes

#    create dyamic library
#    copy shared library to lib directory 
#    make shared library public
#    create symbolic links from MyroC to MyroC with version/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
