summaryrefslogtreecommitdiffstats
path: root/remote/setup_tools
blob: bf3f51573fde905dd0cbfccf3627662e0c54d540 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/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 git locate
	do
		local TEST=$(which ${BIN})
		[ -z "$TEST" ] && pinfo "Installing $BIN..." && apt-get install $BIN
	done
}


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

	[ ! -e ${TOOL_CONFIG} ] && perror "Config for '$TOOL' not found."

	. ${TOOL_CONFIG}
}

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

	[ ! -e ${BUILD_SCRIPT} ] && perror "Build script for specified tool not found."

	. ${BUILD_SCRIPT}
}

copyfileswithdependencies ()
{
	[ ! -d build ] && pinfo "No build directory found, skipping dependency copying" && return 0
	cd build


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

		
	[ ! -z "${REQUIRED_BINARIES}" ] && pinfo "Gathering required binaries from config file..."
	for FILENAME in ${REQUIRED_BINARIES}
	do
	  FILE=$(find . -name "${FILENAME}" -type f -executable | xargs grep -l -E '^(.ELF|#!)' | head -1)
	  if [ -z "$FILE" ]; then
		  pwarning "\tNo Binary found for ${FILENAME}. Skipping." 
		  continue
	  fi
	  pdebug "\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
	  local ldd_exit_code=$?
	  if [ "x$ldd_exit_code" != "x0" ];
	  then
		pdebug "\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//=>/ })
		pdebug "Processing lib ${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
				pdebug "   at ${llib}"
				echo ${llib} >> "${COPYFILES_LIST}"
			done
		else
			if [ -e ${arrIN[1]} ];
			then
				echo ${arrIN[1]} >> "${COPYFILES_LIST}"
				
				if [ ! -L ${arrIN[1]} ];
				then
					pdebug "   at ${arrIN[1]}"
				else
					pdebug "   at ${arrIN[1]} -> $(readlink -f ${arrIN[1]})"
					echo $(readlink -f ${arrIN[1]}) >> ${COPYFILES_LIST}
				fi
			fi
		fi
	  done
	done 

	[ ! -z "${REQUIRED_LIBRARIES}" ] && pinfo "Gathering required libraries from config file..."
	for LIB in ${REQUIRED_LIBRARIES}
	do
		for LOCATION in $(find . -name ${LIB}*)
		do
			pdebug "* $LOCATION"
			echo ${LOCATION} >> ${COPYFILES_LIST}
		done
	done

	[ ! -z "${REQUIRED_DIRECTORIES}" ] && pinfo "Gathering required directories from config file..."
	local CURRENT_PWD=$(pwd) # Prevent calling pwd 50000 times inside the loop below
	for ENTRY in ${REQUIRED_DIRECTORIES}
	do
		pdebug "* ./$ENTRY"
		echo "./${ENTRY}" >> "${COPYFILES_LIST}"
		for BIN in $(find "./${ENTRY}" -type f -not -name '*.a' | xargs grep -l '^.ELF')
		do
			pdebug "   Searching 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//=>/ })
				pdebug "      Searching for ${arrIN[0]}...($i)"
				local LOCAL_MATCHES=$(find . -name "$(echo ${arrIN[0]} | awk -F '.' '{print $1}')".so\*)
				if [ "x${LOCAL_MATCHES}" != "x" ];
				then
					for llib in ${LOCAL_MATCHES};
					do
						pdebug "      Found locally, copying ${llib}"
						echo ${llib} >> "${COPYFILES_LIST}"
					done
				else
					if [ ! -z ${arrIN[1]} ] && [ "x${arrIN[1]}" != "xnot" ];
					then
						pdebug "      Not found locally but in system, copying ${arrIN[1]}"
						echo ${arrIN[1]} >> "${COPYFILES_LIST}"
						[ -L ${arrIN[1]} ] && echo $(readlink -f "${arrIN[1]}") >> "${COPYFILES_LIST}"
					else
						pwarning "Lib '${arrIN[0]}' from required dir '$ENTRY' neither found in build directory nor on this system."
						pwarning "If this lib is not supplied by another module, this module will probably fail in your final system"
					fi
				fi
			done
		done
	done

	[ ! -z "${REQUIRED_FILES}" ] && pinfo "Gathering required files from config file..."
	for ENTRY in ${REQUIRED_FILES}
	do
		echo "./${ENTRY}" >> "${COPYFILES_LIST}"
	done


	#copy to initramfsdir
	pdebug "[stage32] Completed file list generation at ${TOOL_DIR}/${TOOL}/build/${COPYFILES_LIST}."
	if [ -s "$COPYFILES_LIST" ]; then
		local CLISTCOUNT=$(cat "$COPYFILES_LIST" | wc -l)
		pinfo "Copying $CLISTCOUNT files to stage 3.2 target directory."
		tarcopy "$(cat ${COPYFILES_LIST}|sort -u)" "${INIT_DIR}"
	fi
	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
	pinfo "Adding basic libs"
	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
        	        pdebug "Adding $i"
	                BASICLIBS="${BASICLIBS} $i $(readlink -f "$i")"
	        fi
	done
	tarcopy "${BASICLIBS}" "${INIT_DIR}"
}

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
			TOOL_STR="[${TOOL}]"
			pinfo "## ## Processing module ## ##"
	
			#[ "x$DEBUG" != "x1" ] \
			#	&& echo "Logging to ${TOOL_DIR}/${TOOL}/stage32.log" \
			#	&& exec 6>&1 > ${TOOL_DIR}/${TOOL}/stage32.log
			# TODO: Make above work with the new logging system (add function to logging.inc to switch logfile)
	 		cd "${TOOL}"
			pinfo "## Reading config"
			read_config
			pinfo "## Reading build"
			read_build
			pinfo "## Installing dependencies"
			install_dependencies
			pinfo "## Fetching source"
			fetch_source
			pinfo "## Building"
			build
			# remove *.la files as they might confuse libtool/linker of other tool packages
			find "${TOOL_DIR}/${TOOL}/build" -name '*.la' -exec rm -f {} \;
			pinfo "## Copying files with dependencies"
			copyfileswithdependencies
			pinfo "## Post copy"
			post_copy
			cd ${TOOL_DIR}

			# reset pipes
			#[ "x$DEBUG" != "x1" ] && exec 1>&6 6>&- 
			# TODO
			pinfo "## ## Module completed ## ##"
			TOOL_STR=""
		else
			perror "Tool directory for '$TOOL' not found."
			# maybe make this a warning instead of error?
		fi
		shift
	done
}

clean_tools() {
	if [ "x$1" = "x" -o "x$1" = "xall" ]; then
		#clean all
		if [ -d ${INIT_DIR} ]; then
			pinfo "Cleaning ${INIT_DIR}..."
			rm -rf "${INIT_DIR}" || perror "Error deleting $INIT_DIR"
			pinfo "done."
		fi
	        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
	pinfo "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)
	if [ -e ${TOOLDIR}/.built ]; then
		rm "${TOOLDIR}/.built" || perror "Could not clear built flag"
	fi
	if [ -e ${TOOLDIR}/.fetched_source ]; then
		rm "${TOOLDIR}/.fetched_source" || perror "Could not clear fetched_source flag"
	fi
	if [ -d ${TOOLDIR}/build ]; then
		rm -rf "${TOOLDIR}/build" || perror "Could not delete build path"
	fi
	if [ -d ${TOOLDIR}/src ]; then
		rm -rf "${TOOLDIR}/src" || perror "Could not delete src path"
	fi
	if [ -e ${TOOLDIR}/list_binaries_and_files ]; then
		rm "${TOOLDIR}/list_binaries_and_files" || perror "Could not delete list_binaries_and_files"
	fi
	pinfo "done."
}

#generate_stage32 $@