Better eval-when-compile example in manual
[emacs.git] / autogen.sh
blobf56966ae0d1fe8f6a4bb38d12b2549cf6e3fb011
1 #!/bin/sh
2 ### autogen.sh - tool to help build Emacs from a repository checkout
4 ## Copyright (C) 2011-2024 Free Software Foundation, Inc.
6 ## Author: Glenn Morris <rgm@gnu.org>
7 ## Maintainer: emacs-devel@gnu.org
9 ## This file is part of GNU Emacs.
11 ## GNU Emacs is free software: you can redistribute it and/or modify
12 ## it under the terms of the GNU General Public License as published by
13 ## the Free Software Foundation, either version 3 of the License, or
14 ## (at your option) any later version.
16 ## GNU Emacs is distributed in the hope that it will be useful,
17 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ## GNU General Public License for more details.
21 ## You should have received a copy of the GNU General Public License
22 ## along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ### Commentary:
26 ## The Emacs repository does not include the configure script (and
27 ## associated helpers). The first time you fetch Emacs from the repo,
28 ## run this script to generate the necessary files.
29 ## For more details, see the file INSTALL.REPO.
31 ### Code:
33 ## Tools we need:
34 ## Note that we respect the values of AUTOCONF etc, like autoreconf does.
35 progs="autoconf"
37 ## Minimum versions we need:
38 autoconf_min=`sed -n 's/^ *AC_PREREQ(\[\([0-9\.]*\)]).*/\1/p' configure.ac`
41 ## $1 = program, eg "autoconf".
42 ## Echo the version string, eg "2.59".
43 ## FIXME does not handle things like "1.4a", but AFAIK those are
44 ## all old versions, so it is OK to fail there.
45 ## Also note that we do not handle micro versions.
46 get_version ()
48 vers=`($1 --version) 2> /dev/null` && expr "$vers" : '[^
49 ]* \([0-9][0-9.]*\).*'
52 ## $1 = version string, eg "2.59"
53 ## Echo the major version, eg "2".
54 major_version ()
56 echo $1 | sed -e 's/\([0-9][0-9]*\)\..*/\1/'
59 ## $1 = version string, eg "2.59"
60 ## Echo the minor version, eg "59".
61 minor_version ()
63 echo $1 | sed -e 's/[0-9][0-9]*\.\([0-9][0-9]*\).*/\1/'
66 ## $1 = program
67 ## $2 = minimum version.
68 ## Return 0 if program is present with version >= minimum version.
69 ## Return 1 if program is missing.
70 ## Return 2 if program is present but too old.
71 ## Return 3 for unexpected error (eg failed to parse version).
72 check_version ()
74 ## Respect, e.g., $AUTOCONF if it is set, like autoreconf does.
75 uprog0=`echo $1 | sed -e 's/-/_/g' -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'`
77 eval uprog=\$${uprog0}
79 if [ x"$uprog" = x ]; then
80 uprog=$1
81 else
82 printf '%s' "(using $uprog0=$uprog) "
85 ## /bin/sh should always define the "command" builtin, but
86 ## sometimes it does not on hydra.nixos.org.
87 ## /bin/sh = "BusyBox v1.27.2", "built-in shell (ash)".
88 ## It seems to be an optional compile-time feature in that shell:
89 ## see ASH_CMDCMD in <https://git.busybox.net/busybox/tree/shell/ash.c>.
90 if command -v command > /dev/null 2>&1; then
91 command -v $uprog > /dev/null || return 1
92 else
93 $uprog --version > /dev/null 2>&1 || return 1
95 have_version=`get_version $uprog` || return 4
97 have_maj=`major_version $have_version`
98 need_maj=`major_version $2`
100 [ x"$have_maj" != x ] && [ x"$need_maj" != x ] || return 3
102 [ $have_maj -gt $need_maj ] && return 0
103 [ $have_maj -lt $need_maj ] && return 2
105 have_min=`minor_version $have_version`
106 need_min=`minor_version $2`
108 [ x"$have_min" != x ] && [ x"$need_min" != x ] || return 3
110 [ $have_min -ge $need_min ] && return 0
111 return 2
114 do_check=true
115 do_autoconf=false
116 do_git=false
118 for arg; do
119 case $arg in
120 --help)
121 exec echo "$0: usage: $0 [--no-check] [target...]
122 Targets are: all autoconf git";;
123 --no-check)
124 do_check=false;;
125 all)
126 do_autoconf=true
127 test -r .git && do_git=true;;
128 autoconf)
129 do_autoconf=true;;
130 git)
131 do_git=true;;
133 echo >&2 "$0: $arg: unknown argument"; exit 1;;
134 esac
135 done
137 case $do_autoconf,$do_git in
138 false,false)
139 do_autoconf=true
140 test -r .git && do_git=true;;
141 esac
143 # Generate Autoconf-related files, if requested.
145 if $do_autoconf; then
147 if $do_check; then
149 echo 'Checking whether you have the necessary tools...
150 (Read INSTALL.REPO for more details on building Emacs)'
152 missing=
154 for prog in $progs; do
156 sprog=`echo "$prog" | sed 's/-/_/g'`
158 eval min=\$${sprog}_min
160 printf '%s' "Checking for $prog (need at least version $min) ... "
162 check_version $prog $min
164 retval=$?
166 case $retval in
167 0) stat="ok" ;;
168 1) stat="missing" ;;
169 2) stat="too old" ;;
170 4) stat="broken?" ;;
171 *) stat="unable to check" ;;
172 esac
174 echo $stat
176 if [ $retval -ne 0 ]; then
177 missing="$missing $prog"
178 eval ${sprog}_why=\""$stat"\"
181 done
184 if [ x"$missing" != x ]; then
186 echo '
187 Building Emacs from the repository requires the following specialized programs:'
189 for prog in $progs; do
190 sprog=`echo "$prog" | sed 's/-/_/g'`
192 eval min=\$${sprog}_min
194 echo "$prog (minimum version $min)"
195 done
198 echo '
199 Your system seems to be missing the following tool(s):'
201 for prog in $missing; do
202 sprog=`echo "$prog" | sed 's/-/_/g'`
204 eval why=\$${sprog}_why
206 echo "$prog ($why)"
207 done
209 echo '
210 If you think you have the required tools, please add them to your PATH
211 and re-run this script.
213 Otherwise, please try installing them.
214 On systems using rpm and yum, try: "yum install PACKAGE"
215 On systems using dpkg and apt, try: "apt-get install PACKAGE"
216 Then re-run this script.
218 If you do not have permission to do this, or if the version provided
219 by your system is too old, it is normally straightforward to build
220 these packages from source. You can find the sources at:
222 https://ftp.gnu.org/gnu/PACKAGE/
224 Download the package (make sure you get at least the minimum version
225 listed above), extract it using tar, then run configure, make,
226 make install. Add the installation directory to your PATH and re-run
227 this script.
229 If you know that the required versions are in your PATH, but this
230 script has made an error, then you can simply re-run this script with
231 the --no-check option.
233 Please report any problems with this script to bug-gnu-emacs@gnu.org .'
235 exit 1
238 echo 'Your system has the required tools.'
240 fi # do_check
242 # Stale caches can confuse autoconf.
243 rm -fr autom4te.cache exec/autom4te.cache || exit
245 # In build-aux save config.guess, config.sub and install-sh
246 # in case autoreconf overwrites them, as we rely on the copies
247 # in Git, which are updated by admin/merge-gnulib.
248 for file in config.guess config.sub install-sh; do
249 cp -p build-aux/$file build-aux/$file.tmp || exit
250 done
252 # Build aclocal.m4 here so that autoreconf need not use aclocal.
253 # aclocal is part of Automake and might not be installed, and
254 # autoreconf skips aclocal if aclocal.m4 is already supplied.
255 ls m4/*.m4 | LC_ALL=C sort | sed 's,.*\.m4$,m4_include([&]),' \
256 > aclocal.m4.tmp || exit
257 if cmp -s aclocal.m4.tmp aclocal.m4; then
258 rm -f aclocal.m4.tmp
259 else
260 echo "Building aclocal.m4 ..."
261 mv aclocal.m4.tmp aclocal.m4
262 fi || exit
264 echo "Running 'autoreconf -fi -I m4' ..."
266 ## Let autoreconf figure out what, if anything, needs doing.
267 ## Use autoreconf's -f option in case autoreconf itself has changed.
268 autoreconf -fi -I m4 || exit
270 echo "Building 'aclocal.m4' in exec ..."
272 # Create a placeholder aclocal.m4 in exec, preventing autoreconf
273 # from running aclocal.
275 echo "" > exec/aclocal.m4
277 echo "Running 'autoreconf -fi' in exec ..."
279 # Now, run autoreconf inside the exec directory to generate its
280 # configure script.
281 autoreconf -fi exec || exit
283 # Restore config.guess etc. in build-aux, and copy them to exec.
284 for file in config.guess config.sub install-sh; do
285 cp build-aux/$file.tmp exec/$file &&
286 mv build-aux/$file.tmp build-aux/$file || exit
287 done
291 # True if the Git setup was OK before autogen.sh was run.
293 git_was_ok=true
295 if $do_git; then
296 case `cp --help 2>/dev/null` in
297 *--backup*--verbose*)
298 cp_options='--backup=numbered --verbose';;
300 cp_options='-f';;
301 esac
305 # Like 'git config NAME VALUE' but verbose on change and exiting on failure.
306 # Also, do not configure unless requested.
308 git_config ()
310 $do_git || return
312 name=$1
313 value=$2
315 ovalue=`git config --get "$name"` && test "$ovalue" = "$value" || {
316 if $git_was_ok; then
317 echo 'Configuring local git repository...'
318 case $cp_options in
319 --backup=*)
320 config=$git_common_dir/config
321 cp $cp_options --force -- "$config" "$config" || exit;;
322 esac
324 echo "git config $name '$value'"
325 git config "$name" "$value" || exit
326 git_was_ok=false
330 ## Configure Git, if requested.
332 # Get location of Git's common configuration directory. For older Git
333 # versions this is just '.git'. Newer Git versions support worktrees.
335 { test -r .git &&
336 git_common_dir=`git rev-parse --no-flags --git-common-dir 2>/dev/null` &&
337 test -n "$git_common_dir"
338 } || git_common_dir=.git
339 hooks=$git_common_dir/hooks
341 # Check hashes when transferring objects among repositories.
343 git_config transfer.fsckObjects true
346 # Configure 'git diff' hunk header format.
348 # This xfuncname is based on Git's built-in 'cpp' pattern.
349 # The first line rejects jump targets and access declarations.
350 # The second line matches top-level functions and methods.
351 # The third line matches preprocessor and DEFUN macros.
352 git_config diff.cpp.xfuncname \
353 '!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])
354 ^((::[[:space:]]*)?[A-Za-z_][A-Za-z_0-9]*[[:space:]]*\(.*)$
355 ^((#define[[:space:]]|DEFUN).*)$'
356 git_config diff.elisp.xfuncname \
357 '^\([^[:space:]]*def[^[:space:]]+[[:space:]]+([^()[:space:]]+)'
358 git_config 'diff.m4.xfuncname' '^((m4_)?define|A._DEFUN(_ONCE)?)\([^),]*'
359 git_config 'diff.make.xfuncname' \
360 '^([$.[:alnum:]_].*:|[[:alnum:]_]+[[:space:]]*([*:+]?[:?]?|!?)=|define .*)'
361 git_config 'diff.shell.xfuncname' \
362 '^([[:space:]]*[[:alpha:]_][[:alnum:]_]*[[:space:]]*\(\)|[[:alpha:]_][[:alnum:]_]*=)'
363 git_config diff.texinfo.xfuncname \
364 '^@node[[:space:]]+([^,[:space:]][^,]+)'
367 # Install Git hooks.
369 tailored_hooks=
370 sample_hooks=
372 for hook in commit-msg pre-commit prepare-commit-msg post-commit \
373 pre-push commit-msg-files.awk; do
374 cmp -- build-aux/git-hooks/$hook "$hooks/$hook" >/dev/null 2>&1 ||
375 tailored_hooks="$tailored_hooks $hook"
376 done
378 git_sample_hook_src ()
380 hook=$1
381 src=$hooks/$hook.sample
382 if test ! -r "$src"; then
383 case $hook in
384 applypatch-msg) src=build-aux/git-hooks/commit-msg;;
385 pre-applypatch) src=build-aux/git-hooks/pre-commit;;
386 esac
389 for hook in applypatch-msg pre-applypatch; do
390 git_sample_hook_src $hook
391 cmp -- "$src" "$hooks/$hook" >/dev/null 2>&1 ||
392 sample_hooks="$sample_hooks $hook"
393 done
395 if test -n "$tailored_hooks$sample_hooks"; then
396 if $do_git; then
397 echo "Installing git hooks..."
399 if test ! -d "$hooks"; then
400 printf "mkdir -p -- '%s'\\n" "$hooks"
401 mkdir -p -- "$hooks" || exit
404 if test -n "$tailored_hooks"; then
405 for hook in $tailored_hooks; do
406 dst=$hooks/$hook
407 cp $cp_options -- build-aux/git-hooks/$hook "$dst" || exit
408 chmod -- a-w "$dst" || exit
409 done
412 if test -n "$sample_hooks"; then
413 for hook in $sample_hooks; do
414 git_sample_hook_src $hook
415 dst=$hooks/$hook
416 cp $cp_options -- "$src" "$dst" || exit
417 chmod -- a-w "$dst" || exit
418 done
420 else
421 git_was_ok=false
425 if test ! -f configure; then
426 echo "You can now run '$0 autoconf'."
427 elif test -r .git && test $git_was_ok = false && test $do_git = false; then
428 echo "You can now run '$0 git'."
429 elif test ! -f config.status ||
430 test -n "`find configure src/config.in -newer config.status`"; then
431 echo "You can now run './configure'."
434 exit 0
436 ### autogen.sh ends here