summaryrefslogblamecommitdiffstats
path: root/remote/setup_tools
blob: aaa7a23b1624ac6066bcf3b9552313bd170ddb63 (plain) (tree)
1
2
3
4
5
6
7
8
9

           
                             

                                     

                               
 
                 
 



                                                                                

 
 
               
 
                                                    
 
                                        



                                                                   
                        

 

              
                                                      
 
                                         



                                                                         
                         

 

                            
                                                                                
                
 
 
                                             
                                                        






                                                                               
                                            
          

                                                                                  
                                                                         


                                                                                          
                                                  
                                           

                                                                       




                                           
                                             

                        
                                                                                                                                 
            
                                  

                                                                                             
                                                  
                    
                                                     
                          
                                                     
                                                                 
                            
                               
                    

                                              
                                                                     







                                                                                               

                          
              
                                                                                                                                   
             
 
                                        
          
                                                       
                  
                                                             


                    




                                                                               


                                                     
                                                                                         
                   

                                                                                                                                              

                                                  


                                                                                                             



                                                                     
                                                                                

                                                                                 
                                               
                                    
                                                          
                                                                
                                            
                                                                                                 




                                                                                                                          

                                                                                                                                                 
                    
                                                    

            




                                                                        
                                      
          
                                                     
                                               

            
 
                             

                                                                                                       



                                  


                   
        
                                                 
        
                                          
                                                                   
                    
                                


                                                                                                                    
                                        
                                                                       

                  
                                                                     

 
                     
 

                      

                      


                                                  
                             
                
 
                                                    
                      
                            
                        
                                  
                    






                                                                                                                  
                                  




                                                

                                                                                                       
                                                 
                                 
                                      
 




                                                                                                                  


                                                        

                     

 
               

                                                  

                                                                         









                                                 
 

                              
                                        



                                                                         




                                                                                          
                     


                    
#!/bin/bash

MODULE_DIR=${ROOT_DIR}/remote

#Create tools directory if not exists
TOOL_DIR=${MODULE_DIR}/tools
INIT_DIR=${MODULE_DIR}/stage3.2

initial_checks ()
{
	for BIN in $(which git) $(which locate)
	do
		[ -z $BIN ] && echo "Installing $BIN..." && apt-get install $BIN
	done
}


read_config () 
{
	TOOL_CONFIG=${TOOL_DIR}/${TOOL}/${TOOL}.conf

	if [ ! -e ${TOOL_CONFIG} ]; then
		 echo "ERROR: Config for specified tool not found."
		 exit 1
	fi

	. ${TOOL_CONFIG}
}

read_build () 
{
	BUILD_SCRIPT=${TOOL_DIR}/${TOOL}/${TOOL}.build

	if [ ! -e ${BUILD_SCRIPT} ]; then
		 echo "ERROR: Build script for specified tool not found."
		 exit 1
	fi

	. ${BUILD_SCRIPT}
}

copyfileswithdependencies ()
{
	[ ! -d build ] && echo "No build directory for ${TOOL} found." && return
	cd build


	COPYFILES_LIST="list_wanted_stage3.2"
	[ -e ${COPYFILES_LIST} ] && rm ${COPYFILES_LIST}

		
	echo -e "\n\n*********************************************************"
	echo "*"
	echo "*     Copying required binaries from config file..."
	echo "*"
	echo -e "*********************************************************\n\n"
	for FILENAME in ${REQUIRED_BINARIES}
	do
	  FILE=$(find . -name ${FILENAME} -type f -executable | xargs grep -l ELF)
	  if [ -z "$FILE" ]; then
		  echo -e "\tNo Binary found for ${FILENAME}. Skipping." 
		  echo "-----------------------------------------------------------------"
		  continue
	  fi
	  echo -e "\tFound ${FILENAME} at ${FILE}"
	  echo ${FILE} >> ${COPYFILES_LIST}
	  # fetch dependencies
	  # quick fix to exclude libc*, else it copies unneeded libs...
	  # workaround for 
	  ldd ${FILE} &>/dev/null
          ldd_exit_code=$?
	  if [ "x$ldd_exit_code" != "x0" ];
	  then
		echo -e "\tldd $FILE failed."
		continue
	  fi
	  for i in $(ldd ${FILE} |awk '{print $1 $2 $3}'|grep -v ld-linux|grep -v libc.so*|grep -v linux-gate|grep -v linux-vdso)
	  do
		arrIN=(${i//=>/ })
		echo -en "\t\t${arrIN[0]}"
		LOCAL_MATCHES=$(find . -name $(echo ${arrIN[0]}|awk -F "." '{print $1}').so*)
		if [ "x${LOCAL_MATCHES}" != "x" ];
		then
			for llib in ${LOCAL_MATCHES};
			do
				echo -ne " ->${llib}"
				echo ${llib} >> ${COPYFILES_LIST}
			done
			echo ""
		else
			if [ -e ${arrIN[1]} ];
			then
				echo ${arrIN[1]} >> ${COPYFILES_LIST}
				
				if [ ! -L ${arrIN[1]} ];
				then
					echo -e " -> ${arrIN[1]}"
				else
					echo -e " -> ${arrIN[1]} -> $(readlink -f ${arrIN[1]})"
					echo $(readlink -f ${arrIN[1]}) >> ${COPYFILES_LIST}
				fi
			fi
		fi
	  done
	  echo -e "\t-------------------------------------------------------------------------------------------------------------"
	done 

	for LIB in ${REQUIRED_LIBRARIES}
	do
		for LOCATION in $(find . -name ${LIB}*)
		do
			echo ${LOCATION} >> ${COPYFILES_LIST}
		done
	done

	echo -e "\n\n*********************************************************"
	echo "*"
	echo "*   Copying required directories from config file..."
	echo "*"
	echo -e "*********************************************************\n\n"
	for ENTRY in ${REQUIRED_DIRECTORIES}
	do
		echo ".${ENTRY}" >> ${COPYFILES_LIST}
		for BIN in $(find .${ENTRY} -type f -not -name "*.a" | xargs grep -l ELF)
		do 
			echo -e "\tSearching libs for ${BIN}..."
			for i in $(ldd ${BIN} |awk '{print $1 $2 $3}'|grep -v ld-linux|grep -v libc.so*|grep -v linux-gate|grep -v linux-vdso)
			do
				arrIN=(${i//=>/ })
				echo -e "\t\t---------------------------------------------------------"
				echo -ne "\t\tSearching ${arrIN[0]} under $(pwd)..."
				LOCAL_MATCHES=$(find . -name $(echo ${arrIN[0]}|awk -F "." '{print $1}').so*)
				if [ "x${LOCAL_MATCHES}" != "x" ];
				then
					for llib in ${LOCAL_MATCHES};
					do
						echo -en "\n\t\tCopying ${llib}"
						echo ${llib} >> ${COPYFILES_LIST}
					done
					echo ""
				else
					echo " not found."
					if [ ! -z ${arrIN[1]} ];
					then
						echo -e "\t\tCopying from system ${arrIN[1]} ..."
						echo ${arrIN[1]} >> ${COPYFILES_LIST}
						[ -L ${arrIN[1]} ] && echo $(readlink -f ${arrIN[1]}) >> ${COPYFILES_LIST}
					fi
				fi
			done
	  		echo -e "\tCopied $BIN"
	  		echo -e "\t-------------------------------------------------------------------------------------------------------------"
		done
		echo -e "\t Copied directory $ENTRY"
	done

	echo "*********************************************************"
	echo "*"
	echo "*      Copying required files from config file..."
	echo "*"
	echo "*********************************************************"
	for ENTRY in ${REQUIRED_FILES}
	do
		echo ".${ENTRY}" >> ${COPYFILES_LIST}
		echo -e "\t Copied file $ENTRY"
	done


	#copy to initramfsdir
	echo "[stage32] Completed file list generation at ${TOOL_DIR}/${TOOL}/build/${COPYFILES_LIST}."
	(tar -cpv $(cat ${COPYFILES_LIST}|sort -u) | tar -xpv -C ${INIT_DIR}) &>/dev/null
	unset REQUIRED_BINARIES
	unset REQUIRED_LIBRARIES
	unset REQUIRED_DIRECTORIES
	unset REQUIRED_FILES
}

get_basic_libs () {
	
	[ ! -d ${INIT_DIR} ] && mkdir ${INIT_DIR}
	
	# copy libc and ld-linux separatly
	echo "----------------------------------------------------"
	BASICLIBS=""
	for i in $(ldd ${SHELL})
	do
        	if [ $(echo $i | grep '^/' | grep -c ld) -eq 1 -o  $(echo $i | grep '^/' | grep -c libc.so) -eq 1 ];
	        then
        	        echo "Copied $i"
	                BASICLIBS="${BASICLIBS} $i $(readlink -f "$i")"
	        fi
	done
	(tar -cpv ${BASICLIBS} | tar -xpv -C ${INIT_DIR}) &>/dev/null
}

generate_stage32 () {

	initial_checks

	get_basic_libs

	# if no arguments assume all.
	if [ "x$1" = "x" -o "x$1" = "xall" ]; then
		tools=$(ls ${TOOL_DIR})
		set -- $tools
	fi	

	# now iterate over given tools and copy them
	cd ${TOOL_DIR}
	while (( "$#" )); do
		TOOL=$1	
		if [ -d ${TOOL} ];
		then
			echo "###################################################################################"
			echo "#                               BUILDING $TOOL"
			echo "#"
	
			[ "x$DEBUG" != "x1" ] \
				&& echo "Logging to ${TOOL_DIR}/${TOOL}/stage32.log" \
				&& exec 6>&1 > ${TOOL_DIR}/${TOOL}/stage32.log
	 		cd ${TOOL}
			read_config
			read_build		
			install_dependencies
			fetch_source
			build
			# remove *.la files as they might confuse libtool/linker of other tool packages
			find "${TOOL_DIR}/${TOOL}/build" -name '*.la' -exec rm -f {} \;
			copyfileswithdependencies
			post_copy
			cd ${TOOL_DIR}

			# reset pipes
			[ "x$DEBUG" != "x1" ] && exec 1>&6 6>&-
			echo "#"
			echo "#                                    DONE"
			echo "###################################################################################"
		else
			echo "Tool directory not found."
		fi
		shift
	done
}

clean_tools() {
	if [ "x$1" = "x" -o "x$1" = "xall" ]; then
		#clean all
		[ -d ${INIT_DIR} ] && echo -n "Cleaning ${INIT_DIR}..." \
			&& rm -rf ${INIT_DIR} && echo " done."
	        for TOOL in $(ls ${TOOL_DIR}); do
			clean_tool $TOOL
        	done
	else 
		while (( "$#" )); do
			clean_tool $1
			shift
		done
	fi
}

clean_tool() {
        TOOLDIR=${TOOL_DIR}/$1
	echo -n "Cleaning ${TOOLDIR}..."
	#[ -e ${TOOLDIR}/build/list_wanted_stage3.2 ] && cd ${INIT_DIR} \
	#	&& xargs rm < ${TOOLDIR}/build/list_wanted_stage3.2
	#[ -d ${TOOLDIR}/data ] && cd ${INIT_DIR} \
#		&& xargs rm < $(find ${TOOLDIR}/data -type f)
        [ -e ${TOOLDIR}/.built ] && rm ${TOOLDIR}/.built
        [ -e ${TOOLDIR}/.fetched_source ] && rm ${TOOLDIR}/.fetched_source
        [ -d ${TOOLDIR}/build ] && rm -rf ${TOOLDIR}/build
	[ -d ${TOOLDIR}/src ] && rm -rf ${TOOLDIR}/src
	[ -e ${TOOLDIR}/list_binaries_and_files ] && rm ${TOOLDIR}/list_binaries_and_files
	echo " done."
}

#generate_stage32 $@