--- a +++ b/trunk/wharfie/lib/makefile.py @@ -0,0 +1,104 @@ +#!/usr/bin/python +# +# Copyright 2017 Ingo Hornberger <ingo_@gmx.net> +# +# This software is licensed under the MIT License +# +# Permission is hereby granted, free of charge, to any person obtaining a copy of this +# software and associated documentation files (the "Software"), to deal in the Software +# without restriction, including without limitation the rights to use, copy, modify, +# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to the following +# conditions: +# +# The above copyright notice and this permission notice shall be included in all copies +# or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE +# OR OTHER DEALINGS IN THE SOFTWARE. +# +################################################################################ +# We use templates, which are checking their execution context, +# to be sure, that target commands are really only executed +# in the change root of the image. +templateTrgCmd = "#!/bin/bash\n[ -f ../Wharfile ] && exit 2;\n%s\n" +templateHostCmd = "#!/bin/bash\n[ ! -f ../Wharfile ] && exit 2;\n%s" + +archiveName = 'rootfs.tar'; +makeTargets = list(); +environment = list(); +finalTarget = list(); + +# +# Write a Makefile +# +def write_makefile(filename, dry_run, installpath='.'): + f = open(filename, 'w'); + # write header + f.write("ifneq (VERBOSE,y)\n") + f.write("Q=@\n") + f.write("endif\n") + f.write("\n") + f.write("OUTPUT_FILE=%s\n" % archiveName) + f.write("%s: %s\n" % (archiveName, "".join(finalTarget))); + f.write("\tcp $< $@\n"); + f.write("\n"); + + + # write all environment variables + for env in environment: + l = env.lstrip().split(" ") + f.write("export %s=%s\n" % (l[0], " ".join(l[1:]))); + + # write all targets + for target in makeTargets: + if 'comment' in target: + f.write("# %s\n" % target['comment']); + f.write("%s: %s\n" % (target['name'], " ".join(target['dep']))); + f.write("\t${Q}-mkdir $$(basename $@ .tar)\n"); + + if 'trgcmd' in target: + cmd = templateTrgCmd % (target['trgcmd'].replace('$', '\\$$').replace('"', '\\"')); + f.write("\t${Q}echo '******************************'\n"); + f.write("\t${Q}echo '%s'\n" % target['comment']); + f.write("\t${Q}echo '******************************'\n"); + f.write("\t${Q}(echo -e \"%s\") | tee ./$$(basename $@ .tar)/.trg.sh\n" % cmd.replace('\\n', '\\\\\\n').replace('\n', '\\n').replace('!', '"\'!\'"')); + f.write("\t${Q}chmod a+x ./$$(basename $@ .tar)/.trg.sh\n"); + + if 'hostcmd' in target: + cmd = templateHostCmd % (target['hostcmd'].replace('$', '\\$$').replace('"', '\\"')); + f.write("\t${Q}echo '******************************'\n"); + f.write("\t${Q}echo '%s'\n" % target['comment']); + f.write("\t${Q}echo '******************************'\n"); + f.write("\t${Q}(echo -e \"%s\") | tee ./$$(basename $@ .tar)/.hst.sh\n" % cmd.replace('\\n', '\\\\\\\\n').replace('\n', '\\n').replace('!', '"\'!\'"')); + f.write("\t${Q}chmod a+x ./$$(basename $@ .tar)/.hst.sh\n"); + + # start command here ... + f.write("\t${Q}${SUDO} bash -c \""); + + f.write("cd $$(basename $@ .tar); tar -xf ../$<; "); + if not dry_run: + f.write("[ -f .trg.sh ] && chroot . ./.trg.sh; rm -f ./.trg.sh; "); + f.write("[ -f .hst.sh ] && ./.hst.sh; rm -f ./.hst.sh; "); + if not 'temporary' in target or not target['temporary']: + f.write("tar -cf '../$@' .;"); + f.write("\"\n"); + # ... end command + + + if 'temporary' in target and target['temporary']: + f.write("\t${Q}cp $< $@\n"); + + f.write("\t${Q}-${SUDO} rm -Rf ./$$(basename $@ .tar)\n"); + f.write("\n"); + + # write footer + f.write("include %s/wharfie.mk\n" % installpath); + if installpath != '.': + f.write("-include wharfie.mk\n"); + f.write("\n"); +