summaryrefslogtreecommitdiff
path: root/gcc/melt-upgrade.sh
blob: 36b60fdf78416713b5ed2d122f1964cfe3cc9da2 (plain)
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
#! /bin/sh -x

# Copyright (C) 2013 Free Software Foundation, Inc.
# This file melt-upgrade.sh is part of GCC.

# GCC is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.

# GCC 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 General Public License
# along with GCC; see the file COPYING3.  If not see
# <http://www.gnu.org/licenses/>.  

# invoked from Makefile target upgrade-warmelt as
## $(SHELL) $(srcdir)/melt-upgrade.sh meltbuild-sources $(srcdir)

meltbuild_sourcedir=$1
meltsource_dir=$2


## should be single word programs
melt_unifdef=unifdef
melt_astyle=astyle
melt_tempsuffix=tmp.$$

if [ ! -d $meltbuild_sourcedir ]; then
    echo MELT-upgrade bad generated source directory $meltbuild_sourcedir 1>&2
    exit 1
fi

if [ ! -d $meltsource_dir ]; then
    echo MELT-upgrade bad source directory to upgrade $meltsource_dir 1>&2
    exit 1
fi


## check that we have some non-empty files
for f in  $meltbuild_sourcedir/generated/meltrunsup.h \
  $meltbuild_sourcedir/generated/meltrunsup-inc.cc \
  $meltbuild_sourcedir/warmelt-first.cc \
  $meltbuild_sourcedir/warmelt-first+meltdesc.c \
    ; do
  if [ ! -s "$f" ]; then
     ls -l $f
     ls -Ll $f
     echo MELT-upgrade without file $f  1>&2
    exit 1
  fi
done


## check that we have both unidef and astyle
wf=$(which  "$melt_unifdef")
if [ -s "$wf" ] ; then
    echo MELT-upgrade using unifdef from $wf
else
    echo MELT-upgrade need unifdef program $melt_unifdef 1>&2
    exit 1
fi

wf=$(which "$melt_astyle")
if [ -s "$wf" ] ; then
    echo MELT-upgrade using astyle from $wf
else
    echo MELT-upgrade need astyle program $melt_astyle 1>&2
    exit 1
fi

## upgrade the runtime
for f in $meltbuild_sourcedir/generated/meltrunsup.h  $meltbuild_sourcedir/generated/meltrunsup-inc.cc ; do
    bf=$(basename $f)
    ls -lL $f
    $melt_unifdef -DMELTGCC_NOLINENUMBERING < $f > "$bf-unif-$melt_tempsuffix" 
    if [ ! -s "$bf-unif-$melt_tempsuffix" ]; then
	echo MELT-upgrade failed to unifdef runtime $f
	exit 1
    fi
    $melt_astyle --style=gnu < "$bf-unif-$melt_tempsuffix"  > "$meltsource_dir/melt/generated/$bf-$melt_tempsuffix"
    if [ ! -s "$meltsource_dir/melt/generated/$bf-$melt_tempsuffix" ]; then
	echo MELT-upgrade failed to upgrade runtime file $f 1>&2
	exit 1
    else
	echo MELT-upgrade is upgrading "$meltsource_dir/melt/generated/$bf"
	mv "$meltsource_dir/melt/generated/$bf-$melt_tempsuffix"  "$meltsource_dir/melt/generated/$bf"
    fi
done
#
################
## upgrade the translator
for df in $meltbuild_sourcedir/warmelt*+meltdesc.c ; do
    if [ ! -s "$df" ]; then
	echo MELT-upgrade descriptor file $df does not exist 1>&2
	exit 1
    fi
    bs=$(basename "$df" +meltdesc.c)
    echo MELT-upgrade processing translator file $bs
    tf=$meltbuild_sourcedir/$bs+melttime.h
    if [ ! -s "$tf" ]; then
	echo MELT-upgrade timestamp file $tf does not exist 1>&2
	exit 1
    fi
    ## copy the generated sources first
    for f in $meltbuild_sourcedir/$bs.cc $meltbuild_sourcedir/$bs+[0-9][0-9].cc ; do
	if [ ! -f "$f" ]; then
	    continue
	fi
	bf=$(basename $f)
	$melt_unifdef -DMELTGCC_NOLINENUMBERING < $f > "$bf-unif-$melt_tempsuffix"
	if [ ! -s  "$bf-unif-$melt_tempsuffix" ]; then
	    echo MELT-upgrade failed to unifdef translator file $f 1>&2
	    exit 1
	fi
	$melt_astyle --style=gnu < "$bf-unif-$melt_tempsuffix" > "$meltsource_dir/melt/generated/$bf-$melt_tempsuffix"
	if [ ! -s "$meltsource_dir/melt/generated/$bf-$melt_tempsuffix" ]; then
	    echo MELT-upgrade failed to upgrade translator file $f 1>&2
	    exit 1
	else
	    echo MELT-upgrade is upgrading "$meltsource_dir/melt/generated/$bf"
	    if [ -f "$meltsource_dir/melt/generated/$bf" ]; then
		mv $meltsource_dir/melt/generated/$bf $meltsource_dir/melt/generated/$bf~
	    fi
	    mv "$meltsource_dir/melt/generated/$bf-$melt_tempsuffix"  "$meltsource_dir/melt/generated/$bf"
	fi	
    done
    ## copy verbatim without indentation the descriptor and timestamp files at last
    cp $df "$meltsource_dir/melt/generated/$bs+meltdesc.c-$melt_tempsuffix"
    if [ ! -s "$meltsource_dir/melt/generated/$bs+meltdesc.c-$melt_tempsuffix" ]; then
	echo MELT-upgrade failed to upgrade descriptor file $meltsource_dir/melt/generated/$bs+meltdesc.c 1>&2
	exit 1
    else
	echo MELT-upgrade is upgrading descriptor $meltsource_dir/melt/generated/$bs+meltdesc.c
	    if [ -f "$meltsource_dir/melt/generated/$bs+meltdesc.c" ]; then
		mv "$meltsource_dir/melt/generated/$bs+meltdesc.c" "$meltsource_dir/melt/generated/$bs+meltdesc.c~"
	    fi
	mv  "$meltsource_dir/melt/generated/$bs+meltdesc.c-$melt_tempsuffix" "$meltsource_dir/melt/generated/$bs+meltdesc.c"
    fi
    cp $tf "$meltsource_dir/melt/generated/$bs+melttime.h-$melt_tempsuffix"
    if [ ! -s "$meltsource_dir/melt/generated/$bs+melttime.h-$melt_tempsuffix"  ]; then
	echo MELT-upgrade failed to upgrade timestamp file "$meltsource_dir/melt/generated/$bs+melttime.h" 1>&2
	exit 1
    else
	echo MELT-upgrade is upgrading timestamp file "$meltsource_dir/melt/generated/$bs+melttime.h"
	if [ -f "$meltsource_dir/melt/generated/$bs+melttime.h" ]; then
	    mv "$meltsource_dir/melt/generated/$bs+melttime.h" "$meltsource_dir/melt/generated/$bs+melttime.h~"
	fi
	mv "$meltsource_dir/melt/generated/$bs+melttime.h-$melt_tempsuffix"  "$meltsource_dir/melt/generated/$bs+melttime.h"
    fi
    ## backup the files in the melt/generated/ which are no more in the build tree
    for gf in $meltsource_dir/melt/generated/$bs*.cc  $meltsource_dir/melt/generated/$bs*.[ch]; do
	bf=$(basename $gf)
	if [ ! -f $meltbuild_sourcedir/$bf ]; then
	    mv $gf $gf~
	    echo MELT-upgrade backup removed $gf
	fi
    done
done

date +"MELT-upgrade completed upgrade from $meltbuild_sourcedir to $meltsource_dir/melt/ at %D %T %Z %n"
# eof melt-upgrade.sh