#################################################################################
#										#
#		Linux TPM 1.2 TSS Makefile					#
#			     Written by Ken Goldman				#
#		       IBM Thomas J. Watson Research Center			#
#										#
# (c) Copyright IBM Corporation 2018 - 2019					#
# 										#
# All rights reserved.								#
# 										#
# Redistribution and use in source and binary forms, with or without		#
# modification, are permitted provided that the following conditions are	#
# met:										#
# 										#
# Redistributions of source code must retain the above copyright notice,	#
# this list of conditions and the following disclaimer.				#
# 										#
# Redistributions in binary form must reproduce the above copyright		#
# notice, this list of conditions and the following disclaimer in the		#
# documentation and/or other materials provided with the distribution.		#
# 										#
# Neither the names of the IBM Corporation nor the names of its			#
# contributors may be used to endorse or promote products derived from		#
# this software without specific prior written permission.			#
# 										#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS		#
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT		#
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR		#
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT		#
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,	#
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT		#
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,		#
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY		#
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT		#
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE		#
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.		#
#										#
#################################################################################

# C compiler

CC = /usr/bin/gcc

# compile - common flags for TSS library and applications

CCFLAGS += 	-DTPM_POSIX

# example of pointing to a locally built openssl 1.1
# CCFLAGS += 	-I/home/kgold/openssl/include

# compile - for TSS library

# include the hardening flag PIC needed for compiling for dynamic
# linking

CCLFLAGS += 	-I. 		\
		-fPIC		\
		-DTPM_TPM12

# to compile out printf's.  Regression test will fail because it tries
# to print a structure -DTPM_TSS_NO_PRINT

# example of changing the default interface type
#	-DTPM_INTERFACE_TYPE_DEFAULT="\"dev\""

# compile - for applications

# include the hardening flag PIE needed for compiling for
# static linking

CCAFLAGS += 	-I.		\
		-DTPM_TPM12	\
		-fPIE

# link - common flags flags TSS library and applications

LNFLAGS += 	-DTPM_POSIX		\
		-L.

# This seems to be required on some Ubuntu distros due to an issue with the gold linker
#		-fuse-ld=bfd

# example of pointing to a locally built openssl 1.1
# LNFLAGS +=	 -L/home/kgold/openssl
# This also requires setting the environment variable LD_LIBRARY_PATH.  E.g.,
# setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:/home/kgold/openssl-1.1.0c

# link - for TSS library

# hardening flags for linking shared objects
LNLFLAGS += -shared -Wl,-z,now

# This is an alternative to using the bfd linker on Ubuntu
LNLLIBS += -lcrypto

# link - for applications, TSS path, TSS and OpenSSl libraries

# hardening flags for linking executables
LNAFLAGS += -pie -Wl,-z,now -Wl,-rpath,.

LNALIBS +=  -libmtss

# shared library

# versioned shared library
LIBTSSVERSIONED=libibmtss.so.2.1

# soname field of the shared library
# which will be made symbolic link to the versioned shared library
# this is used to provide version backward-compatibility information
LIBTSSSONAME=libibmtss.so.2

# symbolic link to the versioned shared library
# this allows linking to the shared library with '-libmtss' 

os := $(shell uname -o)
ifeq ($(os),Cygwin)
  LIBTSS=libibmtss.dll
else
  LIBTSS=libibmtss.so
endif

# TSS utilities shared library

LIBTSSUTILSVERSIONED=libibmtssutils.so.2.1
LIBTSSUTILSSONAME=libibmtssutils.so.2
LIBTSSUTILS=libibmtssutils.so

# executable extension

EXE =

# 

ALL = 
TSS_HEADERS=

# default TSS library

TSS_OBJS =	tssfile.o 		\
		tsscryptoh.o 		\
		tsscrypto.o

TSSUTILS_OBJS = cryptoutils.o	\
		ekutils.o	\
		imalib.o	\
		eventlib.o

# common to all builds

include makefile-common
include makefile-common12

# default build target

all:	$(ALL)

# TSS shared library source

tss.o: 		$(TSS_HEADERS) tss.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tss.c
tssproperties.o: $(TSS_HEADERS) tssproperties.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tssproperties.c
tssauth.o: 	$(TSS_HEADERS) tssauth.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tssauth.c
tssmarshal.o: 	$(TSS_HEADERS) tssmarshal.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tssmarshal.c
tsscryptoh.o: 	$(TSS_HEADERS) tsscryptoh.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tsscryptoh.c
tsscrypto.o: 	$(TSS_HEADERS) tsscrypto.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tsscrypto.c
tssutils.o: 	$(TSS_HEADERS) tssutils.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tssutils.c
tssfile.o: 	$(TSS_HEADERS) tssfile.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tssfile.c
tsssocket.o: 	$(TSS_HEADERS) tsssocket.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tsssocket.c
tssdev.o: 	$(TSS_HEADERS) tssdev.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tssdev.c
tsstransmit.o: 	$(TSS_HEADERS) tsstransmit.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tsstransmit.c
tssresponsecode.o: $(TSS_HEADERS) tssresponsecode.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tssresponsecode.c
tssccattributes.o: $(TSS_HEADERS) tssccattributes.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tssccattributes.c
tssprint.o: 	$(TSS_HEADERS) tssprint.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tssprint.c
tssprintcmd.o: 	$(TSS_HEADERS) tssprintcmd.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tssprintcmd.c
Unmarshal.o: 	$(TSS_HEADERS) Unmarshal.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) Unmarshal.c
Commands.o: 	$(TSS_HEADERS) Commands.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) Commands.c
CommandAttributeData.o: 	$(TSS_HEADERS) CommandAttributeData.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) CommandAttributeData.c
ntc2lib.o:	$(TSS_HEADERS) ntc2lib.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) ntc2lib.c
tssntc.o:	$(TSS_HEADERS) tssntc.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tssntc.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tss20.c
# TPM 1.2

tss12.o: 	$(TSS_HEADERS) tss12.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tss12.c
tssauth12.o: 	$(TSS_HEADERS) tssauth12.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tssauth12.c
tssmarshal12.o:	$(TSS_HEADERS) tssmarshal12.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tssmarshal12.c
Unmarshal12.o: 	$(TSS_HEADERS) Unmarshal12.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) Unmarshal12.c
Commands12.o: 	$(TSS_HEADERS) Commands12.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) Commands12.c
tssccattributes12.o: $(TSS_HEADERS) tssccattributes12.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) tssccattributes12.c
CommandAttributeData12.o: 	$(TSS_HEADERS) CommandAttributeData12.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) CommandAttributeData12.c

# TSS utilities shared library source

cryptoutils.o: 	$(TSS_HEADERS) cryptoutils.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) cryptoutils.c
ekutils.o: 	$(TSS_HEADERS) ekutils.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) ekutils.c
imalib.o: 	$(TSS_HEADERS) imalib.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) imalib.c
eventlib.o: 	$(TSS_HEADERS) eventlib.c
		$(CC) $(CCFLAGS) $(CCLFLAGS) eventlib.c

# TSS shared library build

$(LIBTSS): 	$(TSS_OBJS)
		$(CC) $(LNFLAGS) $(LNLFLAGS) -Wl,-soname,$(LIBTSSSONAME) -o $(LIBTSSVERSIONED) $(TSS_OBJS) $(LNLLIBS)
		rm -f $(LIBTSSSONAME)
		ln -sf $(LIBTSSVERSIONED) $(LIBTSSSONAME)
		rm -f $(LIBTSS)
		ln -sf $(LIBTSSSONAME) $(LIBTSS)

# TSS utilities shared library

$(LIBTSSUTILS):	$(TSSUTILS_OBJS)
		$(CC) $(LNFLAGS) $(LNLFLAGS) -Wl,-soname,$(LIBTSSUTILSSONAME) -o $(LIBTSSUTILSVERSIONED) $(TSSUTILS_OBJS) $(LNLLIBS)
		rm -f $(LIBTSSSUTILSONAME)
		ln -sf $(LIBTSSUTILSVERSIONED) $(LIBTSSUTILSSONAME)
		rm -f $(LIBTSSUTILS)
		ln -sf $(LIBTSSUTILSSONAME) $(LIBTSSUTILS)

.PHONY:		clean
.PRECIOUS:	%.o

clean:		
		rm -f *.o  *~ 		\
		h*.bin			\
		$(LIBTSSSONAME)		\
		$(LIBTSSVERSIONED) 	\
		$(LIBTSSUTILSSONAME) 	\
		$(LIBTSSUTILSVERSIONED)	\
		$(ALL)

# applications are in .../utils12

# for applications, not for TSS library

%.o:		%.c ibmtss/tss.h 
		$(CC) $(CCFLAGS) $(CCAFLAGS) $< -o $@

