#{===============================================================================}
#{                                                                               }
#{   CUMMINS ENGINE COMPANY, INC.                                                }
#{   COPYRIGHT 1994 CUMMINS ENGINE COMPANY, INC.  ALL RIGHTS RESERVED            }
#{   This program may not be reproduced, in whole or in part in any form or by an}
#{   means whatsoever without the written permission of:                         }
#{       CUMMINS ENGINE COMPANY, INC.                                            }
#{       1460 National Road                                                      }
#{       Columbus, IN 47201                                                      }
#{                                                                               }
#{   File Name: CM5xx_Makefile                        SDR: None                  }
#{                                                                               }
#{   File Description: This file is the primary make file for the cm5xx sources. }
#{                                                                               }
#{   Project Name:  CM5xx                                                        }
#{                                                                               }
#{   Author: J. Mohos                    Creation Date:    29 JUN 94             }
#{                                                                               }
#{   Limitations:                                                                }
#{                                                                               }
#{      Language:   C, Motorola 68331 Assembler                                  }
#{      Processor:  Motorola 68331                                               }
#{      Platform:   CM5xx module                                                 }
#{                                                                               }
#{   Assumptions:                                                                }
#{                                                                               }
#{   Revision History: Please use ClearCase to get the revision history.         }
#{                                                                               }
#{   DSEE Compatibilty: This version matches rev 16 of cm5xx_base_thread.        }
#{                                                                               }
#{===============================================================================}

#
# Define the suffix types for the automated suffix rules.
#

.SUFFIXES:      .o .c .i .a .s .lst .map .map2 .x .x2

#
# The following are the default options selected for all targets.
# .SILENT is the same as the -s command line option to build silently
# A .SILET: * entry indicates that all builds should be silent.
#

.SILENT:	*


#
# The following macro indicates where the .dd file generation
# tool resides.
#
GEN_NT_TOOL=/vob/core0/tool_lib/gen_nt_report
CHECK_ALIGN=/vob/core0/tool_lib/check_eeprom_placement
NTN_TEST=/vob/core0/tool_lib/ntn_test
TRANSLATE_E2M=/vob/cm/dd/bin/translate_e2m



#
# Define the build diagnostics macros to stamp the object name
# and time of start and stop into a temp file.  MIS has requested
# these diagnostics to monitor build performance.
# They must be located at the very start and end points of each
# translation rule.
#
START_STAMP=echo "Start: "$@ "`date '+%D %T'`" >> /usr/tmp/buildtimes.$$USER
END_STAMP=echo "End:   "$@ "`date '+%D %T'`" >> /usr/tmp/buildtimes.$$USER


#{**********************************************************}
#{** The master make file will generate the CM5xx sources **}
#{**********************************************************}



#
# The final stage of the build is done in this block.
# The CODE_BLOCK and the FINAL_LINK are the dependencies for the
# ultimate calibration result.
#

$(BLDLIB)/$(PROJ).cal:	$(BLDLIB)/$(PROJ)_Code_Block.o	\
			$(BLDLIB)/$(PROJ)_Final_Link.x	\
			$(TOOLLIB_DEPENDS)
	$(START_STAMP)
	@echo "Building $(PROJ) ..."

	#
	# Remove existing results.
	#
	$(RM) -f $(BLDLIB)/$(PROJ).cal
	$(RM) -f $(BLDLIB)/$(PROJ).e2m
	$(RM) -f $(BLDLIB)/$(PROJ).gc2
	$(RM) -f $(BLDLIB)/$(PROJ).dd
	$(RM) -f $(BLDLIB)/$(PROJ).seg
	$(RM) -f $(BLDLIB)/$(PROJ).srt
	$(RM) -f $(BLDLIB)/$(PROJ)_NTN_Check.ntc

	#
	# Creating final calibration file.  This is done by concatenating all
	# of the hex files from all of the pages and eeprom.  Each file has a
	# file terminator, :00000001FF, which is removed using grep.  Obviously
	# the last one is left alone.  Additionally pages 1, 2, and 3 need to
	# have an offset for 32 bit addressing.  This is added using echo and
	# the directive :02000004000xzz. x is the page and zz is the checksum
	# for the line.

	#
	# Dummy Master Header Record
	#
	echo ":040000FF00030000F7"                                      >  $(BLDLIB)/hex_temp

	#
	# Adding block data structure definition
	#
	$(GREP) "BD_OLD_LINE" $(P2LIB)/block_data.h | $(AWK) '{print $$3}'  >> $(BLDLIB)/hex_temp

	#
	# Sort the .x2 file prior to conversion to hex format.
	#
	$(TOOLLIB)/sort_s_record $(BLDLIB)/$(PROJ)_Final_Link.x2	>  $(BLDLIB)/sorted_x2

	#
	# Converting Motorola S record into Intel hex format
	#
	$(TOOLLIB)/mint $(BLDLIB)/sorted_x2				>> $(BLDLIB)/hex_temp

	#
	# Running tool to fixup CRC Checksum in Master Header Record
	#
	$(TOOLLIB)/calibration_crc.bin $(BLDLIB)/hex_temp

	#
	# Move working file into $(PROJ).cal
	#
	$(MV) $(BLDLIB)/hex_temp $(BLDLIB)/$(PROJ).cal

	#
	# Creating e2m file
	#   Run data_dictionary_config_spec through the C preprocessor
	#   Create config.tmp from the data dictionary
	#   Get date using grep,
	#   Run tool that calculates checksum
	#
	$(RM) -f $(BLDLIB)/ddcs.c
	$(RM) -f $(BLDLIB)/ddcs.i
	$(CP) $(CSPECLIB)/p135_base_ddcs.h $(BLDLIB)/ddcs.c
	cd $(BLDLIB);						\
	$(CC) $(MODULE_SPECIFIER) -P $(BLDLIB)/ddcs.c

	#
	# Calling OGC to create both a Reference_Thread and a config.e2m
	# file based on the data dictionary config spec sent to it.
	#
	# NOTE: When making a release add -s SW_RELEASE_NAME to the
	#       end of the next line, otherwise no label will be attached.
	#
	$(BUILD_CONFIG) -f $(BLDLIB)/ddcs.i -o $(BLDLIB) -no_min_range
	$(MV) $(BLDLIB)/Reference_Thread $(BLDLIB)/$(PROJ)_config.ref

	#
	# Inserting proper date stamp
	#
	if [ "$(ACT_DATE)" = "DAY" ]; then	\
		$(GREP) "#define JULIAN_DATE" $(BLDLIB)/flash_date_stamp.h | $(NAWK) '{printf("%s%s",($$3 - 1),($$3 + 1));}' > $(BLDLIB)/config.date;	\
	elif [ "$(ACT_DATE)" != "NONE" ] ; then	\
		echo $(ACT_DATE) > $(BLDLIB)/config.date;	\
	else	\
		echo " " > $(BLDLIB)/config.date; \
	fi
	echo '{                                                                                    '   > $(BLDLIB)/nawk.tmp
	echo ' comment = substr($$15,11);                                                           ' >> $(BLDLIB)/nawk.tmp

	if [ "$(ACT_DATE)" != "NONE" ] ; then	\
		echo ' printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s%s\n",$$1,$$2,$$3,$$4,$$5,$$6,$$7,$$8,$$9,$$10,$$11,$$12,$$13,$$14,df,comment);' >> $(BLDLIB)/nawk.tmp;	\
	else	\
		echo ' print ;' >> $(BLDLIB)/nawk.tmp; \
	fi

	echo '}                                                                                    '  >> $(BLDLIB)/nawk.tmp
	$(NAWK) -f $(BLDLIB)/nawk.tmp -F, df=`cat $(BLDLIB)/config.date` $(BLDLIB)/config.e2m          > $(BLDLIB)/$(PROJ).e2m.modified_dates

	#
	# Do Core to Project-specific unit translation
	# (i.e., %FU  ->  MM3S)
	#
	# Unit conversion is defined by the $(BLDLIB)/translate_e2m.definition file.
	#
	$(TRANSLATE_E2M) $(BLDLIB)/translate_e2m.definition \
		  $(BLDLIB)/$(PROJ).e2m.modified_dates      \
		> $(BLDLIB)/$(PROJ).e2m.translated_units

	#
	# Run GCC to:
	#   1. Check the e2m file for tools compliance
	#   2. Add the CRC/checksum
	#
	$(GREP) "#define JULIAN_DATE" $(BLDLIB)/flash_date_stamp.h | $(AWK) '{print $$3}'               > $(BLDLIB)/config.date
	$(DEVSYSLIB)/GCC -d `cat $(BLDLIB)/config.date` -o $(BLDLIB)/$(PROJ).e2m -c $(BLDLIB)/$(PROJ).e2m.translated_units -h $(BLDLIB)/$(PROJ).cal -i -k cm5xx -s 8 -v 320 > $(BLDLIB)/$(PROJ).gc2


	#
	# Print genconfig warnings to standard output
	#
	if						\
		$(GREP) -i warn: $(BLDLIB)/$(PROJ).gc2;	\
	then						\
		exit 0;					\
	fi

	#
	# Print genconfig errors to standard output
	#
	if						 \
		$(GREP) -i error: $(BLDLIB)/$(PROJ).gc2; \
	then						 \
		exit 0;					 \
	fi

	if								\
		$(GREP) -i "Segmentation fault" $(BLDLIB)/$(PROJ).gc2;	\
	then								\
		exit 0;							\
	fi

	#
	# Generate the .seg and .srt files.
	# Add the current software phase to the head of the sort file.
	#
	cd $(BLDLIB);                                                                                                                   \
	$(TOOLLIB)/gen_cm5xx_seg_srt $(PROJ)_Final_Link.map;                                                                            \
	echo    "CPN,CP;EOH;SN,SA,ST,SL; " >  $(PROJ).srt;                                                                              \
	echo -n "Core_Phase "              >> $(PROJ).srt;                                                                              \
	$(GREP) "Core_Phase_Designation" $(BLDLIB)/version.h | $(GREP) "0x" |                                                           \
	$(NAWK) '{start = (index($$0,"0x")+2); end = index($$0,";"); size = (end-start); print substr($$0,start,size)}' >> $(PROJ).srt; \
	cat $(PROJ)_Final_Link.srt >> $(PROJ).srt;
	$(RM) -f $(PROJ)_Final_Link.srt;                                                                                                \
 	$(MV) -f $(PROJ)_Final_Link.seg $(PROJ).seg;                                                                                    \
	touch $(BLDLIB)/$(PROJ).srt;
	touch $(BLDLIB)/$(PROJ).seg;

	#
	# Testing to see if a graphic console is available.  The .dd file generation
	# tool requires a graphic console to bring up the MasterWorks control panal.
	# Without this the simulator will not run.
	# If a graphic console is available then the .dd file will be generated using the
	# current data dictionary .ref file and the current IEEE .x file.  The MasterWorks
	# control panel has to come up in order to run the software simulator.
	#

	echo "*********************************************"
	echo "**              W A R N I N G              **"
	echo "**   No Premier.dd file is being built,    **"
	echo "**      there is only a dummy file         **"
	echo "**   NTN Check is also not being performed **"
	echo "*********************************************"

	echo "**** This is a Dummy .dd file ****" >>$(BLDLIB)/$(PROJ).dd

#	if [ "$$DISPLAY" = ":0.0" ] || [ "$$DISPLAY" = "" ]; then					\
#		echo "";										\
#		echo "======================================================================="; 	\
#		echo "Could not generate file $(BLDLIB)/$(PROJ).dd file because";			\
#		echo "your DISPLAY environment variable is not set to a graphic console!";		\
#		echo "Example: setenv DISPLAY oregon:0.0 ";						\
#		echo "======================================================================="; 	\
#		exit 25;										\
#	else													\
#		$(RM) -f $(BLDLIB)/temp.dd;									\
#		$(TOOLLIB)/gen_nt_header $(BLDLIB)/$(PROJ).seg > $(BLDLIB)/$(PROJ).dd;				\
#		$(GREP) "Core_Phase_Designation" $(BLDLIB)/version.h | $(GREP) "0x" |					\
#		$(NAWK) '{start = (index($$0,"0x")+2); end = index($$0,";"); size = (end-start); printf(";CORE BASE PHASE: "); print substr($$0,start,size)}' >> $(BLDLIB)/$(PROJ).dd; \
#		$(GEN_NT_TOOL) $(BLDLIB)/$(PROJ)_config.ref $(BLDLIB)/$(PROJ)_Final_Link.x $(BLDLIB)/temp.dd;		\
#		cat $(BLDLIB)/temp.dd >> $(BLDLIB)/$(PROJ).dd;				\
#		$(RM) -f $(BLDLIB)/temp.dd;						\
#		echo "-----------------------------------------------------------------------------";	\
#		echo "Missing software name fields:";					\
#		if									\
#			$(GREP) "NO_SOFTWARE_NAME" $(BLDLIB)/$(PROJ).dd;		\
#		then									\
#			echo "Total missing software names:" `$(GREP) -c "NO_SOFTWARE_NAME" $(BLDLIB)/$(PROJ).dd`;	\
#		else									\
#			echo "None";							\
#		fi;									\
#		echo "-----------------------------------------------------------------------------";	\
#		echo "Invalid software name fields:";					\
#		if									\
#			$(GREP) "INVALID_SOFTWARE_NAME" $(BLDLIB)/$(PROJ).dd;		\
#		then									\
#			echo "Total invalid software names:" `$(GREP) -c "INVALID_SOFTWARE_NAME" $(BLDLIB)/$(PROJ).dd`; \
#		else									\
#			echo "None";							\
#		fi;									\
#		echo "-----------------------------------------------------------------------------";	\
#
#	fi
#
        #
        # Checking Name Table Name alignment
        #

#	$(NTN_TEST) $(BLDLIB)/$(PROJ).dd $(BLDLIB)/$(PROJ)_Final_Link.map $(BLDLIB)/$(PROJ)_Final_Link.x2 $(P2LIB)/name_table.h > $(BLDLIB)/$(PROJ)_NTN_Check.ntc
#	if                                                                                 \
#		$(GREP) -s "ERROR" $(BLDLIB)/$(PROJ)_NTN_Check.ntc;                        \
#	then                                                                               \
#		echo "Alignment Error: check $(BLDLIB)/$(PROJ)_NTN_Check.ntc for details"; \
#		exit 1;                                                                    \
#	fi

        #
        # Remove the temporary files.
        #

	$(RM) -f $(BLDLIB)/$(PROJ).e2m.modified_dates
#	$(RM) -f $(BLDLIB)/$(PROJ).e2m.translated_units
	$(RM) -f $(BLDLIB)/ddcs.c
	$(RM) -f $(BLDLIB)/config.date
	$(RM) -f $(BLDLIB)/sorted_x2
	$(RM) -f $(BLDLIB)/config.e2m
	$(RM) -f $(BLDLIB)/nawk.tmp
	$(RM) -f $(BLDLIB)/$(PROJ).untranslated

	$(END_STAMP)

#
# Translation rules used to generate the Boot Loader declaration file
#

$(P2LIB)/boot_loader_code.h:	$(RELEASED_BOOT_LOADER) 	\
				$(TOOLLIB)/s_record_to_const
	@echo "Generating boot_loader_code.h"
	$(RM) -f $(P2LIB)/boot_loader_code.h
	$(TOOLLIB)/s_record_to_const $(RELEASED_BOOT_LOADER) > $(P2LIB)/boot_loader_code.h
	@echo "Successfully built boot_loader_code.h"

$(P2LIB)/prog_bootloader.o:	$(P2LIB)/boot_loader_code.h


#
# Translation rules used to generate the AHAC tailorable files.
#

$(IFACELIB)/convert_name.o:		$(AHAC_FCS_OUTPUT).i
$(P2LIB)/dx_multipacket_data.o: 	$(AHAC_FCS_OUTPUT).i
$(P2LIB)/index_table.o: 		$(AHAC_FCS_OUTPUT).i
$(P2LIB)/j1708_here_is_data.o:          $(AHAC_FCS_OUTPUT).i
$(P2LIB)/name_table.o:			$(AHAC_FCS_OUTPUT).i


$(AHAC_FCS_OUTPUT).i:	$(AHAC_FCS)
	@echo "Generating index_table.h and name_table.h"
	$(RM) -f $(AHAC_FCS_OUTPUT).c	
	$(RM) -f $(AHAC_FCS_OUTPUT).i
	$(RM) -f $(P2LIB)/index_table.h
	$(RM) -f $(P2LIB)/name_table.h
	$(CP) $(AHAC_FCS) $(AHAC_FCS_OUTPUT).c
	cd $(BLDLIB);										\
	$(RM) -f /usr/tmp/ahac.$$USER.$$$$;							\
	$(CC) $(MODULE_SPECIFIER) -P $(AHAC_FCS_OUTPUT).c > /usr/tmp/ahac.$$USER.$$$$;		\
	if											\
		$(GREP) -s "directive ignored" /usr/tmp/ahac.$$USER.$$$$;			\
	then											\
		echo "ERROR: missing include file.";						\
		$(RM) -f /usr/tmp/ahac.$$USER.$$$$;						\
		exit 1; 									\
	fi;											\
	$(RM) -f /usr/tmp/ahac.$$USER.$$$$;							\
        $(AHAC_TABLE_GEN) -F $(AHAC_FCS_OUTPUT).i -small_name_table -a -o $(P2LIB);             \
        cd $(BLDLIB);                                                                           \
	$(RM) -f $(AHAC_FCS_OUTPUT).c
	touch $(P2LIB)/index_table.h
	touch $(P2LIB)/name_table.h
	@echo "Successfully built index_table.h and name_table.h"

	echo "Finished generating AHAC configuration files."
	echo "Ready to build application code..."


#
# The final linking is performed in this block of commands.
#
# A list of linker commands will be collected into one base linker command
# file.  This base linker command file contains LOAD statements for all of
# the source objects as well as memory map information.  From this base
# linker command file two final link command files are generated.
# The first linker command file will generate a debug .x file. The
# second will generate a Motorola S format hex file.  Two map files will
# also be generated.
# $(PROJ)_Final_Link.x, $(PROJ)_Final_Link.x2, $(PROJ)_Final_Link.map and
# $(PROJ)_Final_Link.map2 are the derived objects.
#
$(BLDLIB)/$(PROJ)_Final_Link.x:	$(AHAC_FCS_OUTPUT).i		\
				$(BLDLIB)/$(PROJ)_Code_Block.o	\
				$(FINAL_LINK_DEPENDS_OBJECTS)	\
				$(FINAL_LINK_DEPENDS_TOOLS)	\
				$(BLDLIB)/CM5xx_Makefile	\
				$(BLDLIB)/CM5xx_Makefile.options

	$(START_STAMP)
	@echo "Linking $(PROJ)_Final_Link ..."

	#
	# Remove the currently existing final link files.
	#
	$(RM) -f $(BLDLIB)/$(PROJ)_Final_Link.x
	$(RM) -f $(BLDLIB)/$(PROJ)_Final_Link.map
	$(RM) -f $(BLDLIB)/$(PROJ)_Final_Link.x2
	$(RM) -f $(BLDLIB)/$(PROJ)_Final_Link.map2
	$(RM) -f $(BLDLIB)/$(PROJ)_Align_Check.alc

	#
	# Assemble a base linker specification file.
	#
	echo "NAME final_link"					         >  $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "CHIP $(M683XX_CHIP)"				         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "BASE 0"						         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "SECT .flash0"    $(BOOT_LOADER_FIXED_DATA_LOGICAL_START)	 >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "SECT .ndlflash1" $(FLASH_BLOCK_1_LOGICAL_START)	         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "SECT .dlflash1"  $(DWN_LOADED_FLASH_BLOCK_1_LOGICAL_START) >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "SECT .flash3"    $(FLASH_BLOCK_3_LOGICAL_START)	         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "SECT .flash5"    $(FLASH_BLOCK_5_LOGICAL_START)            >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "SECT .flash6"    $(FLASH_BLOCK_6_LOGICAL_START)            >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "SECT .ndlee"     $(EEPROM_LOGICAL_START)	                 >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "SECT .dlee"      $(DWN_LOADED_EEPROM_LOGICAL_START)	 >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "SECT zerovars"   $(RAM_LOGICAL_START)		         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "ALIAS .flash3,literals"				         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "ALIAS zerovars, tags"				         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "ALIAS .flash3, asm_lib"				         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "ALIGNMOD global, 256"				         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "LOAD_SYMBOLS $(P2LIB)/boot_loader.o"		         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "LOAD_SYMBOLS $(P2LIB)/non_dwn_loaded_parameter_block.o"	 >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "LOAD $(P2LIB)/parameter_block.o"		                 >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "LOAD $(P2LIB)/fixed_downloaded_parameters.o"       	 >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "LOAD $(P2LIB)/index_table.o"			         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "LOAD $(P2LIB)/adjustable_parameter_limits.o"	         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "LOAD $(P2LIB)/version.o"				         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "LOAD $(P2LIB)/flash3_code_start.o"                         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "LOAD $(BLDLIB)/$(PROJ)_Code_Block.o"		         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "LOAD $(P2LIB)/name_table.o"			         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "LOAD $(P2LIB)/flash_code_end.o"                            >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "LOAD $(P2LIB)/calibrations.o"			         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "LOAD $(P2LIB)/ram_image.o"			         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "LOAD $(P2LIB)/global.o"				         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "LOAD_SYMBOLS $(P2LIB)/non_dwn_loaded_eeprom.o"		 >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "LOAD $(P2LIB)/eeprom.o"				         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec
	echo "END"						         >> $(BLDLIB)/$(PROJ)_Base_Link_Spec

	#
	# Assemble a final linker specification file for link 1.
	#
	echo "LISTMAP PUBLICS/ADDR CROSSREF"			>  $(BLDLIB)/$(PROJ)_Final_Link_1
	echo "LISTABS PUBLICS INTERNALS"			>> $(BLDLIB)/$(PROJ)_Final_Link_1
	echo "DEBUG_SYMBOLS"					>> $(BLDLIB)/$(PROJ)_Final_Link_1
	echo "FORMAT IEEE"					>> $(BLDLIB)/$(PROJ)_Final_Link_1
	cat  $(BLDLIB)/$(PROJ)_Base_Link_Spec			>> $(BLDLIB)/$(PROJ)_Final_Link_1

	#
	# Assemble a final linker specification file for link 2.
	#
	echo "FORMAT S"						>  $(BLDLIB)/$(PROJ)_Final_Link_2
	cat  $(BLDLIB)/$(PROJ)_Base_Link_Spec			>> $(BLDLIB)/$(PROJ)_Final_Link_2

	#
	# Perform link 1.
	#
	$(LNK)	-c $(BLDLIB)/$(PROJ)_Final_Link_1	   \
		-o $(BLDLIB)/$(PROJ)_Final_Link.x      \
		-m > $(BLDLIB)/$(PROJ)_Final_Link.map

	#
	# Perform link 2.
	#
	$(LNK)	-c $(BLDLIB)/$(PROJ)_Final_Link_2       \
		-o $(BLDLIB)/$(PROJ)_Final_Link.tmp     \
		-m > $(BLDLIB)/$(PROJ)_Final_Link.map2

        #
        # Check calibration/eeprom/parameter_block and ram_image alignment
        #
	$(CHECK_ALIGN) $(BLDLIB)/$(PROJ)_Final_Link.map > $(BLDLIB)/$(PROJ)_Align_Check.alc
	if                                                                                   \
		$(GREP) -s "ERROR" $(BLDLIB)/$(PROJ)_Align_Check.alc;                        \
	then                                                                                 \
		echo "Alignment Error: check $(BLDLIB)/$(PROJ)_Align_Check.alc for details"; \
		exit 1;                                                                      \
	fi

        #
        # Calculate and insert checksum over code segment in s record file
        #
	start_address=`$(GREP) "^_Flash3_Code_Start_Marker" $(BLDLIB)/$(PROJ)_Final_Link.map | $(AWK) '{ printf("%s\n",$$3); }' | sort -u`; \
	end_address=`$(GREP) "^_Flash_Code_End_Marker"   $(BLDLIB)/$(PROJ)_Final_Link.map | $(AWK) '{ printf("%s\n",$$3); }' | sort -u`; \
	checksum_address=`$(GREP) "^_Application_Executable_Code_Checksum" $(BLDLIB)/$(PROJ)_Final_Link.map | $(AWK) '{ printf("%s\n",$$3); }' | sort -u`; \
	checksum=`$(TOOLLIB)/s_record_checksum $(BLDLIB)/$(PROJ)_Final_Link.tmp $$start_address $$end_address`; \
	$(TOOLLIB)/s_record_modify  $(BLDLIB)/$(PROJ)_Final_Link.tmp $$checksum_address $$checksum > $(BLDLIB)/$(PROJ)_Final_Link.x2

	#
	# Remove the temporary files.
	#
	$(RM) -f $(BLDLIB)/$(PROJ)_Base_Link_Spec
	$(RM) -f $(BLDLIB)/$(PROJ)_Final_Link_1
	$(RM) -f $(BLDLIB)/$(PROJ)_Final_Link_2
	$(RM) -f $(BLDLIB)/$(PROJ)_Final_Link.tmp
	$(END_STAMP)	


#
# The Code block is build in this block of commands.
#
# A list of linker commands will be collected into one linker command file and
# then sent to the linker.  In the command file will be a LOAD statement for each
# code source file.  After linking, a map file and a master object file are created.
# $(PROJ)_Code_Block.o and $(PROJ)_Code_Block.map are the derived objects.
#
$(BLDLIB)/$(PROJ)_Code_Block.o:	$(AHAC_FCS_OUTPUT).i			\
				$(CODE_BLOCK_DEPENDS_TOOLS)		\
				$(FLASH3_CODE_BLOCK_DEPENDS_OBJECTS)

	$(START_STAMP)
	@echo "Linking $(PROJ)_Code_Block ..."
	#
	# Remove the existing Code Block objects
	#
	$(RM) -f $(BLDLIB)/$(PROJ)_Code_Block.o
	$(RM) -f $(BLDLIB)/$(PROJ)_Code_Block.map

	echo ""	> $(BLDLIB)/$(PROJ)_Code_Block_Link_Spec;	
	echo "LISTMAP PUBLICS/ADDR CROSSREF"			>> $(BLDLIB)/$(PROJ)_Code_Block_Link_Spec;
	echo "LISTABS PUBLICS INTERNALS"			>> $(BLDLIB)/$(PROJ)_Code_Block_Link_Spec;
	#
	# The following for statement will take each file in the dependant objects list
	# and prepend a LOAD statement to it.  All of these LOAD statements are saved into
	# a link spec file along with an END statement at the tail.  This link spec file
	# will then be used by the linker to link the objects files into one code block object file.
	#
	for object_file in $(FLASH3_CODE_BLOCK_DEPENDS_OBJECTS);			\
	do                                                       	                 \
		echo "LOAD " $$object_file	>> $(BLDLIB)/$(PROJ)_Code_Block_Link_Spec; \
	done
	echo "END"				>> $(BLDLIB)/$(PROJ)_Code_Block_Link_Spec

	#
	# Start the code block linking.
	#
	$(LNK)	-c $(BLDLIB)/$(PROJ)_Code_Block_Link_Spec	        \
		-o $(BLDLIB)/$(PROJ)_Code_Block.o			\
		-m -r > $(BLDLIB)/$(PROJ)_Code_Block.map


	#
	# Remove the temporary files.
	#
	$(RM) -f $(BLDLIB)/$(PROJ)_Code_Block_Link_Spec
	$(END_STAMP)


#
# The following block will compile all of the default .c files into object files.
#

.c.o:   $(C_SOURCE_TOOLS)       \
	$(P2LIB)/tpu_mask.h

	@echo "Compiling ${@:.o=} ..."
	$(START_STAMP)

	#
	# Remove the existing object and list files.
	#
	$(RM) -f ${@}
	$(RM) -f ${@:.o=.lst}


	#
	# Call the compiler and generate the .o and .lst files.
	#
	$(CC) $(C_SOURCE_STANDARD_OPTIONS)			\
		$(C_SOURCE_FLASH3_VARIABLE_OPTIONS)     	\
		$(MODULE_SPECIFIER)				\
		-I$(BLDLIB) 					\
                -I$(HPILIB)                                     \
		-I$(APPLIB)					\
		-I$(APPLIB)/ambient_fueling			\
		-I$(APPLIB)/ambient_timing			\
		-I$(APPLIB)/coolant_fueling			\
		-I$(APPLIB)/coolant_timing			\
                -I$(APPLIB)/tta_min_fuel_adj                    \
		-I$(EPFLIB)					\
		-I$(GOVLIB)					\
		-I$(IFACELIB)					\
		-I$(JCOMMLIB)					\
		-I$(P2LIB)					\
                -I/vob/core0/automotive/sw/base                 \
		-I$(INDLIB)/alt_torque_fuel			\
		-I$(INDLIB)/automatic_idle_speed_setting	\
		-I$(INDLIB)/aux_speed_governor			\
		-I$(CINDLIB)/dc_monitor				\
		-I$(INDLIB)/deceleration_throttle               \
		-I$(INDLIB)/ded_pwm				\
		-I$(INDLIB)/dual_output 			\
		-I$(INDLIB)/electronic_fan_clutch		\
		-I$(INDLIB)/ether_injection                     \
                -I$(INDLIB)/fuel_rate_log			\
                -I$(INDLIB)/frequency_throttle                  \
		-I$(INDLIB)/industrial_throttle			\
		-I$(INDLIB)/eng_startup_limiting 		\
                -I$(INDLIB)/hot_shutdown_mon                    \
		-I$(INDLIB)/hsg_gain_scheduling			\
                -I$(INDLIB)/hybrid_governor                     \
		-I$(INDLIB)/idle_shutdown			\
		-I$(INDLIB)/low_oil_sensor 	  		\
		-I$(INDLIB)/mult_sync				\
		-I$(INDLIB)/mult_throttle			\
		-I$(INDLIB)/oil_ch_mon				\
		-I$(INDLIB)/pto_status				\
		-I$(INDLIB)/pwm_process 			\
		-I$(INDLIB)/remote_throttle			\
		-I$(INDLIB)/sw_alt_low_idle			\
		-I$(INDLIB)/throttle_activated_diagnostics	\
		-I$(INDLIB)/switched_droop			\
                -I$(INDLIB)/tran_det_by_spd_derivative          \
                -I$(INDLIB)/transient_timing_advance            \
                -I$(INDLIB)/transmission_synch			\
                -I$(INDLIB)/tsg                                 \
		-l${@:.o=.lst} 					\
		-o$@						\
		$<


	#
	# Testing to see if any of the include files were missing during build.
	# If the directive ignored statement exists in the list file then the
	# build is stopped and an error is posted.
	#
	if									\
		$(GREP) -s "directive ignored" ${@:.o=.lst} 1> /dev/null;	\
	then									\
		echo "ERROR: missing include file, see the list file for details.";	\
		exit 1; 							\
	fi

        #
        # If the build successfully completed then we want to remove the .lst
        # file since it can take up a large amount of space.
	$(RM) -f ${@:.o=.lst}

	@echo "Successfully compiled ${@:.o=}"
	$(END_STAMP)

#
# The following block will compile all of the default .s files into object files.
#

.s.o:	$(ASM_SOURCE_TOOLS)
	$(START_STAMP)
	@echo "Assembling ${@:.o=} ..."

	#
	# Remove the existing object and list files.
	#
	$(RM) -f ${@}
	$(RM) -f ${@:.o=.lst}
	#
	# Call the assembler and generate the .o and .lst files.
	#
	$(ASM)	$(ASM_SOURCE_OPTIONS)	\
	$(MODULE_SPECIFIER)	\
		-o$@			\
		-l $< > ${@:.o=.lst}

	#
	# If the build successfully completed, then we want to remove the .lst
	# file since it can take up a large amount of space.
	$(RM) -f ${@:.o=.lst}

	@echo "Successfully assembled ${@:.o=}"
	$(END_STAMP)

#
# The following block will compile the security sources.
# The macro SECURITY_SOURCE_DIR will instruct as to which sources
# to use.
#

$(SECURITY_SOURCE_DIR)/security.o:	$(C_SOURCE_TOOLS)
	$(START_STAMP)
	@echo "Compiling security from source directory $(SECURITY_SOURCE_DIR) ..."

	#
	# Remove the existing object and list files.
	#
	$(RM) -f $(SECURITY_SOURCE_DIR)/security.o;
	$(RM) -f $(SECURITY_SOURCE_DIR)/security.lst;

		
	#
	# Call the compiler and generate the .o and .lst files.
	#
	$(CC) $(C_SOURCE_STANDARD_OPTIONS)			\
		$(C_SOURCE_FLASH3_VARIABLE_OPTIONS)     	\
		$(MODULE_SPECIFIER)				\
		$(INCLUDE_PATH)  -Wa,"-l>${@:.o=.lst}"  \
		-l$(SECURITY_SOURCE_DIR)/security.lst		\
		-o$(SECURITY_SOURCE_DIR)/security.o 		\
		-I$(IFACELIBn)					\
		-I$(P2LIB)					\
		-I$(SECURITY_SOURCE_DIR)			\
		$(SECURITY_SOURCE_DIR)/security.c

	#
	# If the build successfully completed, then we want to remove the .lst
	# file since it can take up a large amount of space.
	$(RM) -f ${@:.o=.lst}

	@echo "Successfully compiled security."
	$(END_STAMP)
