# File:          Makefile-windows
#
# Makefile for MyroC on a Windows platform
# To utilize, issue the following command to make an appropriate symbolic link
#
#    ln -s Makefile-windows Makefile
#
# Author:        Henry M. Walker
# Created:       20 September 2013
# Simplified:    28 August 2015
# Revised for Windows:  19-20 November 2020
# 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 -lWs2_32 -pthread -ljpeg -lWs2_32 -ljpeg -lturbojpeg -lglew32 -lglu32 -lfreeglut -lopengl32
ESPEAKFLAGS = -leSpeakPackage 
MYROCFLAGS = -lMyroC

# Locations to install the eSpeakPackage header and object file
# do NOT include space characters in the INSTALLPATH !!!
INSTALLPATH = /usr/local
ESPEAKINC = $(INSTALLPATH)/include
ESPEAKBIN = $(INSTALLPATH)/bin

# Locations to install the MyroC header, object file, and image-display-handler
MYROCINC = $(INSTALLPATH)/include
MYROCLIB = $(INSTALLPATH)/lib
MYROCBIN = $(INSTALLPATH)/bin

# Set linker flags to include the relevant libraries
LDFLAGS=-lws2_32 -ljpeg -lturbojpeg -lglew32 -lglu32 -lfreeglut -lopengl32 -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 
all: MyroC.a

# 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-windows-interface-manager.o MyroC-display-windows-opengl-img-handler.exe 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-windows-interface-manager.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 Windows
MyroC-utilities.o:    MyroC-utilities-windows.c MyroC.h MyroC-utilities.h
	$(CC) $(CFLAGS) -fPIC -c MyroC-utilities-windows.c
	mv MyroC-utilities-windows.o MyroC-utilities.o

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

MyroC-display-windows-opengl-img-handler.exe:    MyroC-display-windows-opengl-img-handler.c MyroC.h MyroC-utilities.h
	$(CC) $(CFLAGS) -fPIC MyroC-display-windows-opengl-img-handler.c  -o MyroC-display-windows-opengl-img-handler.exe $(LDFLAGS)

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 library directory
#      basic reference for files:
#      https://forums.ni.com/t5/LabVIEW/create-a-dll-for-windows-with-gcc/td-p/332736?profile.language=en
#    copy header file to include directory
#    compile implementation file to dynamic .o file
#    form dynamic .o files to dynamicly linked .dll file
#    copy .dll file to shared library directory
#    Note:  use /.../bin directories for dynamic .dll files
#           use /.../lib directories for static .d...a files

install/eSpeak:
	# copy eSpeakPackage header file to include directory
	mkdir -p $(ESPEAKINC)   #-p creates directory/path if it does not already exist
	cp eSpeakPackage.h $(ESPEAKINC)/eSpeakPackage.h
	chmod 755 $(ESPEAKINC)/eSpeakPackage.h

	# compile eSpeakPackage implementation file
	$(CC) $(CFLAGS) -fPIC -c eSpeakPackage-windows.c
	mv eSpeakPackage-windows.o eSpeakPackage.o
	gcc -shared -o eSpeakPackage.dll -Wl,--out-implib,libeSpeakPackagedll.a eSpeakPackage.o $(LDFLAGS)

	# copy .dll file to shared library directory
	mkdir -p $(ESPEAKBIN)   #-p creates directory/path if it does not already exist
	cp eSpeakPackage.dll $(ESPEAKBIN)
	chmod 755 $(ESPEAKBIN)/eSpeakPackage.dll

#----------------------------------------------------------------------------
# install MyroC materials to MyroC library directories
#      basic reference for files:
#      https://forums.ni.com/t5/LabVIEW/create-a-dll-for-windows-with-gcc/td-p/332736?profile.language=en

#    copy header file to include directory
#    remove previous *.o files
#    compile image-handler program
#    copy image-handler executable to shared program directory
#    compile each implementation file to dynamic .o file
#    form dynamic .o files to dynamicly linked .dll file
#    copy .dll file to shared library directory
#    Note:  use /.../bin directories for dynamic .dll files
#           use /.../lib directories for static .d...a files

install/MyroC:  MyroC.h  MyroC.a
	# copy header file to include directory
	mkdir -p "$(MYROCINC)"   #-p creates directory/path if it does not already exist
	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 image-handler program
	$(CC) $(CFLAGS) -fPIC MyroC-display-windows-opengl-img-handler.c -o MyroC-display-windows-opengl-img-handler.exe $(LDFLAGS)
	# copy image-handler executable to shared program directory
	mkdir -p $(MYROCBIN)   #-p creates directory/path if it does not already exist
	cp "MyroC-display-windows-opengl-img-handler.exe" $(MYROCBIN)
	chmod 755 $(MYROCBIN)

	# compile each implementation file to dynamic .o file
	$(CC) $(CFLAGS) -fPIC -c MyroC-utilities-windows.c
	mv MyroC-utilities-windows.o MyroC-utilities.o
	$(CC) $(CFLAGS) -fPIC -c MyroC-connect-windows.c
	mv MyroC-connect-windows.o MyroC-connect.o
	$(CC) $(CFLAGS) -fPIC -c MyroC-general.c
	$(CC) $(CFLAGS) -fPIC -c MyroC-sensor.c
	$(CC) $(CFLAGS) -fPIC -c MyroC-movement.c
	$(CC) $(CFLAGS) -fPIC -c MyroC-camera.c
	$(CC) $(CFLAGS) -fPIC -c MyroC-display-windows-interface-manager.c 
	$(CC) $(CFLAGS) -fPIC MyroC-display-windows-opengl-img-handler.c  -o MyroC-display-windows-opengl-img-handler.exe $(LDFLAGS)
	$(CC) $(CFLAGS) -fPIC -c MyroC-image-file.c

	# form dynamic .o files to dynamicly linked .dll file
	gcc -shared -o MyroC.dll -Wl,--out-implib,libMyroCdll.a MyroC-utilities.o MyroC-connect.o MyroC-general.o MyroC-sensor.o MyroC-movement.o MyroC-camera.o MyroC-display-windows-interface-manager.o MyroC-image-file.o $(LDFLAGS)

	# copy .dll file to shared library directory
	mkdir -p $(MYROCBIN)   #-p creates directory/path if it does not already exist
	cp MyroC.dll $(MYROCBIN)
	chmod 755 $(MYROCBIN)/MyroC.dll
