[r13]: / trunk / wharfie / lib / makefile.py  Maximize  Restore  History

Download this file

105 lines (90 with data), 4.5 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
#!/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");