-
Notifications
You must be signed in to change notification settings - Fork 125
/
generate_buildinfos.sh
executable file
·162 lines (141 loc) · 4.1 KB
/
generate_buildinfos.sh
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
#!/usr/bin/env bash
set -e
set -u
# Copyright © 2011 MLstate
# This file is part of OPA.
# OPA is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License, version 3, as published by
# the Free Software Foundation.
# OPA 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 Affero General Public License for
# more details.
# You should have received a copy of the GNU Affero General Public License
# along with OPA. If not, see <http://www.gnu.org/licenses/>.
# This script is used to generate the module BuildInfos
# it is called by bld at build time of the repositories.
# Since this module is very high regarding to the dependencies
# it is regenerated only in release mode, or after a clean.
MLSTATE_DIFFING=${MLSTATE_DIFFING:-"0"}
help () {
cat <<EOF
generating buildInfos.ml
Use:
$0 opageneral-dir [options]
Options:
--version file.txt precise the file to read the name of opa version
--release release mode
EOF
}
OPA_VERSION="S?"
ROOTDIR=""
IS_RELEASE="false"
if [ -z "$1" ]; then
help
echo "opageneral-dir is not specified"
exit 1
else
ROOTDIR="$1"
shift
fi
while [ "$#" -gt 0 ]; do
case "$1" in
-h|--help|help)
help
exit 0
;;
--release)
IS_RELEASE="true"
;;
--version)
if [ -z "$2" ]; then echo "Error: option $1 requires an argument"; exit 1; fi
shift;
OPA_VERSION="$(cat $1)"
;;
*)
help
exit 1
;;
esac
shift
done
ROOT_REPO="opalang"
REPOS="$ROOT_REPO"
PATH_TO_REPOS="repos"
git_version_cmd () { git log --pretty=oneline | wc -l; }
git_sha_cmd () { git log -n1 --pretty=format:%h; }
git_date_cmd () { git log -n1 --pretty=format:%ad --date=short; }
# special case for opalang, the origin is the publication
git_opalang_version_cmd () {
local ORIGIN_SHA='fccc6851ccd2cb4fd9e83ba7b8c8d6d780ed3e13'
git log --pretty=oneline "$ORIGIN_SHA".. | awk '/^'$ORIGIN_SHA'/ { x=1 } END { print NR+1-x }'
}
is_git_root () {
LOOKED_FOR_REPO=$1
P=`pwd`
# in case we are in another sub-dir, for instance _buuild...
cd $ROOTDIR
if [ "$LOOKED_FOR_REPO" != "$ROOT_REPO" ]; then
cd $PATH_TO_REPOS/$LOOKED_FOR_REPO
fi
if [ -d .git ] && git remote show -n origin | grep -q 'URL:.*'$LOOKED_FOR_REPO; then
cd $P
return 0
else
cd $P
return 1
fi
}
in_repo () {
REPO=$1; shift
P=$PWD
cd $ROOTDIR
if [ "$REPO" != "$ROOT_REPO" ]; then
cd $PATH_TO_REPOS/$REPO
fi
if ! is_git_root $REPO; then
echo "[!] Git repo info for $REPO not found" >&2; exit 1
fi
"$@"
cd $P
}
if [ "$MLSTATE_DIFFING" = 1 ] ; then
cat <<EOF
(*
The following informations are not correct, because the toggle var
MLSTATE_DIFFING was set during the generation of this file.
This debug var is meant to reduce as much as possible the output
of the compiler for comparing 2 branches.
*)
EOF
fi
# opa version
echo "let opa_version_name = \"${OPA_VERSION}\""
# git infos
for repo in $REPOS ; do
if [ "$MLSTATE_DIFFING" = 1 ] ; then
echo "let ${repo}_git_version = 0"
echo "let ${repo}_git_sha = \"diffing\""
elif ! is_git_root $repo; then
echo "let ${repo}_git_version = 0"
echo "let ${repo}_git_sha = \"\""
else
if [ "$repo" = "$ROOT_REPO" ] ; then
echo "let ${repo}_git_version = $(in_repo $repo git_opalang_version_cmd)"
else
echo "let ${repo}_git_version = $(in_repo $repo git_version_cmd)"
fi
echo "let ${repo}_git_sha = \"$(in_repo $repo git_sha_cmd)\""
fi
done
echo
# date
if [ "$MLSTATE_DIFFING" = 1 ] ; then
echo "let date = \"diffing\""
echo "let year = \"diffing\""
else
echo "let date = \"$(in_repo $ROOT_REPO git_date_cmd)\""
echo "let year = \"$(date +%Y)\""
fi
# release
echo "let is_release = $IS_RELEASE"