bundle.common 8.63 KB
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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
#!/bin/bash
#
# SPDX-License-Identifier: LGPL-3.0-or-later
#
# Copyright (C) 2023 Perry Werneck <perry.werneck@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

# Check command-line arguments
argument() {

	local cmdline
	for cmdline in ${BASH_ARGV[*]}
	do
		if [ "$(echo ${cmdline} | sed "s@^--@@g" | sed "s@^-@@g" | cut -d= -f1)" == "${1}" ]; then
			local value
			value="$(echo ${cmdline} | cut -d= -f2)"
			if [ -z "${value}" ]; then
				echo "1"
			else
				echo "${value}"
			fi
			return 0
		fi
	done
	echo ""
	return 2
}

argument "help" > /dev/null
if [ "$?" == "0" ]; then
	echo "Use ${0} options"
	echo ""
	echo "	--help Help options (this screen)"
	echo "	--zip Build zipfile"
	
	if [ -e "${srcdir}/win/${PACKAGE_NAME}.nsi" ]; then
		echo "	--nsi Build nsi installer"
	fi
	echo "	--upload Upload bundle to github"
fi

# Setup default paths
srcdir="$(dirname $(dirname $(readlink -f "${0}")))"
cd ${srcdir}
if [ "$?" != "0" ]; then
	echo "Cant cd to ${srcdir}"
	exit -1
fi

if [ -z ${MINGW_PREFIX} ]; then
	if [ -d "/usr/x86_64-w64-mingw32/sys-root/mingw" ]; then
		MINGW_PREFIX="/usr/x86_64-w64-mingw32/sys-root/mingw"
		PKG_CONFIG="/usr/bin/x86_64-w64-mingw32-pkg-config"
	else
		echo "Cant determine mingw prefix"
		exit -1
	fi
fi

if [ -z ${PKG_CONFIG} ]; then
	PKG_CONFIG=${MINGW_PREFX}/bin/pkg-config
fi

PACKAGE_NAME=$(grep AC_INIT configure.ac | cut -d[ -f2 | cut -d] -f1)
if [ -z ${PACKAGE_NAME} ]; then
	echo "Cant determine package name"
	exit -1
fi

PACKAGE_VERSION=$(grep AC_INIT configure.ac | cut -d[ -f3 | cut -d] -f1)
if [ -z ${PACKAGE_VERSION} ]; then
	echo "Cant determine package name"
	exit -1
fi

mkdir -p .bin/bundle
if [ "$?" != "0" ]; then
	echo "Cant mkdir base buildroot"
	exit -1
fi

buildroot=$(readlink -f .bin/bundle)
if [ -z ${buildroot} ]; then
	echo "Cant detect buildroot ${buildroot}"
	exit -1
fi

mkdir -p "${buildroot}"
if [ "$?" != "0" ]; then
	echo "Cant mkdir ${buildroot}"
	exit -1
fi

rm -fr "${buildroot}/*"
if [ "$?" != "0" ]; then
	echo "Cant clean ${buildroot}"
	exit -1
fi

bindir="${buildroot}${MINGW_PREFIX}/bin"
sysdir="${buildroot}/windows/system32"

if [ -z "${WIN_ROOT}" ]; then
	WIN_ROOT="/c/Windows"
fi

export LANG=C

prepare() {

	argument "help" > /dev/null
	if [ "$?" == "0" ]; then
		exit 0
	fi
	
	rm -fr "${buildroot}"
	mkdir -p "${buildroot}"
} 

install_bin() {

	mkdir -p "${bindir}"

	AGAIN=1
	until [ $AGAIN = 0 ]; do

		SOURCES=$(mktemp)
		REQUIRES=$(mktemp)

		find "${buildroot}" -iname "*.dll" >	${SOURCES}
		find "${buildroot}" -iname "*.exe" >>	${SOURCES}
	
		while read FILENAME
		do
			echo ${FILENAME}
			objdump -p ${FILENAME} | grep "DLL Name:" | cut -d: -f2 | tr "[:upper:]" "[:lower:]" >> ${REQUIRES}
		done < ${SOURCES}

		libs_to_exclude="
			advapi32.dll
			cfgmgr32.dll
			comctl32.dll
			comdlg32.dll
			crypt32.dll
			d3d8.dll
			d3d9.dll
			ddraw.dll
			dnsapi.dll
			dsound.dll
			dwmapi.dll
			gdi32.dll
			gdiplus.dll
			glu32.dll
			glut32.dll
			imm32.dll
			iphlpapi.dll
			kernel32.dll
			ksuser.dll
			mpr.dll
			mscms.dll
			mscoree.dll
			msimg32.dll
			msvcr71.dll
			msvcr80.dll
			msvcr90.dll
			msvcrt.dll
			mswsock.dll
			netapi32.dll
			odbc32.dll
			ole32.dll
			oleacc.dll
			oleaut32.dll
			opengl32.dll
			psapi.dll
			rpcrt4.dll
			secur32.dll
			setupapi.dll
			shell32.dll
			shlwapi.dll
			user32.dll
			usp10.dll
			version.dll
			wininet.dll
			winmm.dll
			wldap32.dll
			ws2_32.dll
			wsock32.dll
			winspool.drv
			ntdll.dll
			winhttp.dll
			hid.dll
			bcrypt.dll
		"

		# Remove system DLLs from list
		for i in $libs_to_exclude; do
			sed -i -e "/${i}/d" ${REQUIRES}
		done

		AGAIN=0
		while read FILENAME
		do

			echo ${FILENAME}
			
			if [ ! -e "${bindir}/${FILENAME}" ]; then

				if [ -e ${MINGW_PREFIX}/bin/${FILENAME} ]; then

					AGAIN=1
					cp -v "${MINGW_PREFIX}/bin/${FILENAME}" "${bindir}/${FILENAME}"
					if [ "$?" != "0" ]; then
						exit -1
					fi

				elif [ -e ${MINGW_PREFIX}/lib/${FILENAME} ]; then

					AGAIN=1
					cp -v "${MINGW_PREFIX}/lib/${FILENAME}" "${bindir}/${FILENAME}"
					if [ "$?" != "0" ]; then
						exit -1
					fi

				elif [ -e  "${WIN_ROOT}/System32/${FILENAME}" ]; then

					echo "Ignoring ${WIN_ROOT}/System32/${FILENAME}"

				else 

					echo "Can't find ${MINGW_PREFIX}/bin/${FILENAME} or ${WIN_ROOT}/System32/${FILENAME}"
					find "${MINGW_PREFIX}" -iname "${FILENAME}"
					exit -1

				fi
			
			fi
		
		done < ${REQUIRES}

		rm -f ${SOURCES}
		rm -f ${REQUIRES}
		
	done
	
	# libhllapi should be available in windows path
	if [ -e "${bindir}/libhllapi.dll" ]; then
		mkdir -p "${sysdir}"
		mv "${bindir}/libhllapi.dll" "${sysdir}"
		if [ "$?" != "0" ]; then
			exit -1
		fi
	fi

}

install_locale() {		

        mkdir -p ${buildroot}${MINGW_PREFIX}/share/locale/pt_BR/LC_MESSAGES

        locales="
                gettext-runtime.mo
                gettext-tools.mo
                glib20.mo
                gtk30.mo
                gtk30-properties.mo
        "

		FILENAMES=$(mktemp)
        for i in ${locales}
		do
 			find "${MINGW_PREFIX}/share/locale" -name ${i} >> ${FILENAMES}
		done

		while read FILENAME
		do
			mkdir -p $(dirname "${buildroot}${FILENAME}")
			cp -v "${FILENAME}" "${buildroot}${FILENAME}"
			if [ "$?" != "0" ]; then
				exit -1
			fi
		done < ${FILENAMES}
		rm -f ${FILENAMES}
}


make_zip() {

	cd ${buildroot}${MINGW_PREFIX}
	if [ "$?" != "0" ]; then
		exit -1
	fi

	rm -f ${srcdir}/${MINGW_PACKAGE_PREFIX}-${PACKAGE_NAME}-${PACKAGE_VERSION}.bundle.zip

	zip -9 -r ${srcdir}/${MINGW_PACKAGE_PREFIX}-${PACKAGE_NAME}-${PACKAGE_VERSION}.bundle.zip .
	if [ "$?" != "0" ]; then
		exit -1
	fi

	cd ${srcdir}
	if [ "$?" != "0" ]; then
		exit -1
	fi
	
	if [ -z $(which gh) ]; then
		return 0
	fi
	
	argument "upload" > /dev/null
	if [ "$?" == "0" ]; then
		gh release upload --clobber "${PACKAGE_VERSION}" ${srcdir}/${MINGW_PACKAGE_PREFIX}-${PACKAGE_NAME}-${PACKAGE_VERSION}.bundle.zip
	fi	

}

make_nsis() {
	makensis \
		-INPUTCHARSET UTF8 \
		-DWITHIPC \
		-DWITHPLUGINS \
		-DWITHSDK \
		-DPKGDIR=${buildroot}${MINGW_PREFIX} \
		${buildroot}${MINGW_PREFIX}/${PACKAGE_NAME}.nsi
		if [ "$?" != "0" ]; then
			echo "Cant build nsis script"
		fi	

	if [ ! -z $(which gh) ]; then
		argument "upload" > /dev/null
		if [ "$?" == "0" ]; then
			gh release upload --clobber "${PACKAGE_VERSION}" ${buildroot}${MINGW_PREFIX}/${PACKAGE_NAME}*.exe
			if [ "$?" != "0" ]; then
				echo "Cant upload nsis installer"
				exit -1
			fi			
		fi	
	fi

	mv -f ${buildroot}${MINGW_PREFIX}/${PACKAGE_NAME}*.exe "${srcdir}"
	if [ "$?" != "0" ]; then
		echo "Cant copy nsis installer"
		exit -1
	fi	
}

install_license() {
	mkdir -p "${buildroot}${MINGW_PREFIX}/usr/share/${PACKAGE_NAME}"
	cp "${srcdir}/LICENSE" "${buildroot}${MINGW_PREFIX}/usr/share/${PACKAGE_NAME}"
	if [ "$?" != "0" ]; then
		echo "Cant copy LICENSE"
		exit -1
	fi
}

install_runtime() {
	install_bin
	install_locale
}

build_package() {

	# Build
	make -C "${srcdir}" all
	if [ "$?" != "0" ]; then
		echo "Build failed"
		exit -1
	fi

	make -C "${srcdir}" "DESTDIR=${buildroot}" install
	if [ "$?" != "0" ]; then
		echo "Install failed"
		exit -1
	fi

}

make_packages() {

	echo "----------------------------------"
	find . -iname *.nsi
	echo "----------------------------------"
	find ${srcdir} -iname *.nsi
	echo "----------------------------------"
	find ${buildroot} -iname *.nsi
	echo "----------------------------------"
	
	if [ -e "${srcdir}/win/${PACKAGE_NAME}.nsi" ]; then
		mkdir -p "${buildroot}${MINGW_PREFIX}/nsi"
		cp "${srcdir}/win/${PACKAGE_NAME}.nsi" "${buildroot}${MINGW_PREFIX}/${PACKAGE_NAME}.nsi" 
		if [ "$?" == "0" ]; then
			echo "Nsis script installed"
		else 
			echo "Error installing nsis"
		fi	
	else
		echo "Cant find ${srcdir}/win/${PACKAGE_NAME}.nsi"
	fi
	
	if [ -e "${srcdir}/LICENSE" ]; then
		mkdir -p "${buildroot}${MINGW_PREFIX}/share/pw3270"
		cp "${srcdir}/LICENSE" "${buildroot}${MINGW_PREFIX}/share/pw3270" 
		if [ "$?" == "0" ]; then
			echo "License installed"
		else
			echo "Error installing license"
		fi	
	else
		echo "Cant find ${srcdir}/LICENSE"
	fi

	argument "nsi" > /dev/null
	if [ "$?" == "0" ]; then
		make_nsis
	fi	

	argument "zip" > /dev/null
	if [ "$?" == "0" ]; then
		make_zip
	fi	

}