Skip to content

Commit 8b442fa

Browse files
wlammendigama0
andauthored
move sources to specific folder (#44)
* movesource files -> src * update README.TXT * start documenting the build * annotate AC_INIT * update docs * fix last paragraph * more fixes * create a build script * rm a pointless script line * mark script as a draft * Update build/build.sh Co-authored-by: Mario Carneiro <[email protected]> Co-authored-by: Mario Carneiro <[email protected]>
1 parent fc32273 commit 8b442fa

35 files changed

+319
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ configure.ac2
2323
*~7
2424
*~8
2525
*~9
26+
# Doxygen related: configuration file and destination folder
27+
doc/doxy
28+
Doxyfile

README.TXT

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ Instructions
2121
For Windows, click on "metamath.exe" and type "read set.mm".
2222

2323
For Unix/Linux/Cygwin/MacOSX using the gcc compiler, compile with the
24-
command "gcc m*.c -o metamath", then type "./metamath set.mm" to run.
24+
command "cd src && gcc m*.c -o metamath", then type
25+
"./metamath set.mm" to run.
2526

2627
As an alternative, if you have autoconf, automake, and a C compiler, you
27-
can compile with the command "autoreconf -i && ./configure && make".
28+
can compile with the command "cd src && autoreconf -i && ./configure && make".
2829
This "autoconf" approach automatically finds your compiler and its
2930
options, and configure takes the usual options (e.g., "--prefix=/usr").
3031
The resulting executable will typically be faster because it will check for

build/build.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
# create a metamath executable from scratch
3+
4+
# You MUST change to the build folder before running this script.
5+
6+
# Draft version, proof of concept.
7+
8+
BUILDDIR=$(pwd)
9+
TOPDIR=$BUILDDIR/..
10+
SRCDIR=$TOPDIR/src
11+
12+
# clear the build directory, but keep the build.sh
13+
find $BUILDDIR/* -not -name 'build.sh' -delete
14+
15+
#========= symlink files to the build directory ==============
16+
17+
cp --symbolic-link $SRCDIR/* $BUILDDIR
18+
mv $BUILDDIR/configure.ac configure.ac.orig
19+
20+
#========= patch the version in configure.ac ===================
21+
22+
# look in metamath.c for a line matching the pattern ' #define MVERSION "<version>"
23+
# and save the line in VERSION
24+
VERSION=`grep '[[:space:]]*#[[:space:]]*define[[:space:]]*MVERSION[[:space:]]"[^"]*"' $SRCDIR/metamath.c`
25+
# extract the version (without quotes) from the saved line
26+
VERSION=`echo $VERSION | sed 's/[^"]*"\([^"]*\)"/\1/'`
27+
28+
# find the line with the AC_INIT command, prepend the line number at the beginning
29+
AC_INIT_LINE=`grep -n '[[:space:]]*AC_INIT[[:space:]]*(.*' $BUILDDIR/configure.ac.orig`
30+
AC_INIT_LINE_NR=`echo $AC_INIT_LINE | sed 's/\([0-9]*\).*/\1/'`
31+
# remove the line number
32+
AC_INIT_LINE=`echo $AC_INIT_LINE | sed 's/[0-9]:\(.*\)/\1/'`
33+
PATCHED_INIT_LINE=`echo $AC_INIT_LINE | sed "s/\\([[:space:]]*AC_INIT(.*\\),.*,\\(.*\\)/\\1, \[$VERSION\],\\2/"`
34+
35+
# replace the AC_INIT line with new content
36+
head -n $(($AC_INIT_LINE_NR - 1)) $BUILDDIR/configure.ac.orig > $BUILDDIR/configure.ac
37+
echo $PATCHED_INIT_LINE >> $BUILDDIR/configure.ac
38+
tail -n +$(($AC_INIT_LINE_NR + 1)) $BUILDDIR/configure.ac.orig >> $BUILDDIR/configure.ac
39+
40+
rm $BUILDDIR/configure.ac.orig
41+
42+
#=========== run autoconf =====================
43+
44+
cd $BUILDDIR
45+
autoreconf -i
46+
./configure
47+
make

doc/BUILD.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Building the Metamath executable from sources
2+
3+
## Introductionary notes
4+
5+
In this file we describe how to build the executable metamath on a __Unix/Linux__
6+
system from scratch using only the sources and a few auxiliary files, all
7+
distributed through the Github Metamath git repository.
8+
9+
### Git folder of source files
10+
11+
[Git](https://en.wikipedia.org/wiki/Git) is widespread version control system.
12+
It bundles files and subdirectories into a single folder called __repository__.
13+
Unlike a simple folder a repository tracks changes to its contents, so you can
14+
checkout older versions of a file, or different versions if multiple persons
15+
work on files simultaniously. It is out of the scope of this particular
16+
document to detail on how this system works. Instead we assume you have a
17+
fresh set of metamath source files ready in a single folder. It is of no
18+
importance here how you got it. The name of the folder is arbitrary, but for
19+
the sake of simplicity we call it throughout this file __metamath-exe__.
20+
21+
## Unix/Linux deployment
22+
23+
Although we restrict the build process on Unix/Linux, there exist numerous
24+
variants of this operating system (__OS__) as well as lots of flavours of
25+
installed [C](https://en.wikipedia.org/wiki/C_(programming_language) compilers.
26+
It was once a tedious task to provide a build process general enough to cope
27+
with this variety. Determining the particular properties of your OS on your
28+
computer (such as the installed libraries) was an essential first step in any
29+
build process.
30+
31+
The situation has changed quite a bit since around 1995 when the metamath
32+
executable was designed. C meanwhile matured and was subject to a couple of
33+
standardizations like ISO/IEC 9899:1999. In effect the variety of C
34+
installations has shrunk considerably. Any modern
35+
[C99](https://en.wikipedia.org/wiki/C99) (or later) compiler along with their
36+
libraries should now compile the metamath executable out of the box.
37+
38+
Nevertheless, from a user perspective, C programs are deployed with an
39+
installation routine comprising a configure shell script and a make file. We
40+
briefly introduce their concepts here.
41+
42+
### configure
43+
44+
A particular [shell script](https://en.wikipedia.org/wiki/Unix_shell) called
45+
[configure](https://en.wikipedia.org/wiki/Configure_script) tests your OS for
46+
its features. Simply determining _all_ possible properties would be extremely
47+
excessive. For example the Metamath build need not know anything about your
48+
network connections. In fact, only a tiny selection of properties need to be
49+
known. In Metamath this selection is encoded in a file __configure.h.in__.
50+
_configure_ then performs all necessary checks based on its contents and at the
51+
end issues a C header file __config.h__. This header file defines lots of
52+
C macros each reflecting a particular test result.
53+
54+
We give an example here:
55+
In _config.h.in_ we find a line
56+
```
57+
/* Define to 1 if stdbool.h conforms to C99. */
58+
#undef HAVE_STDBOOL_H
59+
```
60+
stdbool.h is a header file nowadays found in virtually every C installation.
61+
A few decades ago that was less obvious, and the system had to be checked for
62+
its existence. The shell script _configure_ carries out this check, and it has
63+
know-how built in to deal with lots of variants of both the file itself, and
64+
where to find it on a system. The test result is then issued to the _config.h_
65+
result. Either the above is copied verbatim, or
66+
```
67+
/* Define to 1 if stdbool.h conforms to C99. */
68+
#define HAVE_STDBOOL_H 1
69+
```
70+
If the sources include this file then code alternatives can be established, as
71+
in
72+
```
73+
#include <config.h>
74+
#if HAVE_STDBOOL_H
75+
#include <stdbool.h>
76+
#else
77+
typedef unsigned char bool;
78+
#endif
79+
```
80+
Peculiar as it is, the Metamath executable never includes this generated file,
81+
meaning its results are prescribed anyway, and must not vary with the system
82+
(if not irreleveant).
83+
84+
### make
85+
86+
The _make_ program is usually part of your OS. This executable is not
87+
deployed, but the file it operates upon, the __Makefile__ is. In a nutshell,
88+
the _Makefile_ contains all information necessary to compile, link and install
89+
the Metamath executable. Besides this primary __goal__ a _Makefile_ may
90+
support others as well, such as uninstalling, generating help and so on.
91+
92+
The Metamath makefile actually supports a plethora of _goals_, some of them
93+
have are standardized meaning, many are for partial results, not meant to be
94+
invoked by the user.
95+
96+
The metamath executable is simple enough to be made without the support of
97+
_make_. Nevertheless, some users expect there to be a _Makefile_. If
98+
metamath happens to be part of a distribution, then a _Makefile_ is not
99+
dispensable any more.
100+
101+
## autotools
102+
103+
Writing any of the above files, _configure_ and _Makefile_, is all but easy, in
104+
particular in a portable way. A set of tools was written to support developers
105+
with this task. This set is called
106+
[Autotools](https://en.wikipedia.org/wiki/GNU_Autotools). The idea is to allow
107+
the developer write replacements for _configure_ and _Makefile_ in a language
108+
hiding portability issues. The necessary know-how is built into these tools
109+
and is automatically applied on translation to properly designed _configure_
110+
and _Makefile_. From a distributors point of view the Autotools are applied as
111+
described in the following paragraphs.
112+
113+
Executing ```autoconf --version``` shows whether you have _Autotools_
114+
installed on your computer.
115+
116+
### autoscan
117+
118+
The first step is to identify all constructs subject to portability issues.
119+
_autoscan_ provides you with a suggestion in a __configure.scan__ file.
120+
This file is meant to be renamed to __autoconf.ac__, possibly extended with
121+
extra commands. In Metamath commands for strengthening compiler flags
122+
are added.
123+
124+
### autoconf.ac
125+
126+
This file encodes the features the OS needs to be tested for. When processed by
127+
_autoconf_ a _configure_ script along with its input file _config.h.in_ is created.
128+
Some instructions aim at replacing system dependent variables in _Makefile.am_,
129+
later used to support _make_. The language used for encoding this is __M4__
130+
along with a couple of built-in commands of __autoconf__. This language is
131+
designed to provide cross-platform descriptions of features of the OS.
132+
133+
### autoconf
134+
135+
This Unix program called __autoconf__, or its sibling __autoreconf__, is
136+
capable of generating a _configure_ shell script from the input _autoconf.ac_.
137+
138+
### Makefile.am
139+
140+
141+
... to be continued

doc/configure.ac.annotated

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# -*- Autoconf -*-
2+
# Process this file with autoconf to produce a configure script.
3+
4+
# this file seems to be created by autoscan and adapted to the
5+
# needs of Metamath
6+
7+
# the version of autoconf needed to translate this file into a portable
8+
# configure script. As of this writing version 2.71 is published.
9+
AC_PREREQ([2.65])
10+
11+
# program name, version, email to report bugs
12+
AC_INIT([metamath], [0.114], [[email protected]])
13+
14+
# unique file name likely not existing outside the source tree.
15+
# catches incorrect --srcdir parameter in autoconf call.
16+
AC_CONFIG_SRCDIR([metamath.c])
17+
18+
# requests a config.h.in file, filled according to other commands
19+
# Seems to be pointless, since config.h is nowhere included.
20+
AC_CONFIG_HEADERS([config.h])
21+
22+
# run automake in relaxed mode. Minimal checks only.
23+
AM_INIT_AUTOMAKE([foreign])
24+
25+
# request a Makefile.am, according to following commands,
26+
# until AC_OUTPUT creates the file.
27+
AC_CONFIG_FILES([Makefile])
28+
29+
# Checks for programs.
30+
31+
# add this to support the tools folder
32+
# AC_PROG_AWK
33+
34+
# test existence of a C compiler
35+
AC_PROG_CC
36+
37+
# find an appropriate install executable
38+
# should be replaced with AC_PROG_MAKE_SET
39+
AC_PROG_INSTALL
40+
41+
# Checks for header files.
42+
43+
# Fill config.h.in with macros HAVE_<header>_H and define them to 1,
44+
# if a standard complient header is found.
45+
AC_CHECK_HEADERS([limits.h stdlib.h string.h])
46+
47+
# config.h: set HAVE_STDBOOL_H
48+
AC_HEADER_STDBOOL
49+
50+
# Checks for typedefs, structures, and compiler characteristics.
51+
AC_TYPE_SIZE_T
52+
53+
# Checks for library functions.
54+
AC_FUNC_MALLOC
55+
AC_FUNC_REALLOC
56+
AC_CHECK_FUNCS([strchr strcspn strstr])
57+
58+
# copied to Makefile.am
59+
# Enable gcc warning flags, but only if they seem to work
60+
new_CFLAGS="-Wall -Wextra"
61+
saved_CFLAGS="$CFLAGS"
62+
CFLAGS="$CFLAGS $new_CFLAGS"
63+
64+
# let configure display this message
65+
AC_MSG_CHECKING([[for gcc warning flags]])
66+
67+
# compile this program with new_CFLAGS enabled, if flags are
68+
# accepted, provide them to automake
69+
AC_LINK_IFELSE(
70+
[AC_LANG_PROGRAM([[
71+
#include <stdio.h>
72+
int f() {
73+
return 0;
74+
}
75+
]])],
76+
[AC_MSG_RESULT([yes])],
77+
[AC_MSG_RESULT([no])
78+
AM_CFLAGS="$AM_CFLAGS $new_CFLAGS"])
79+
80+
# compile the following program with a bunch of optimization flags.
81+
# If accepted, provide them to automake.
82+
# Take care of a possible collision with flag -O2.
83+
# Try to optimize.
84+
AC_MSG_CHECKING([[for optimization flags]])
85+
new_CFLAGS="-O3 -funroll-loops -finline-functions -fomit-frame-pointer"
86+
saved_CFLAGS="$CFLAGS"
87+
CFLAGS="$CFLAGS $new_CFLAGS"
88+
# Remove any existing "-O2", or it will override what we're doing.
89+
CFLAGS=$( printf "%s" "$CFLAGS" | sed -e 's/ -O2/ /' )
90+
AC_LINK_IFELSE(
91+
[AC_LANG_PROGRAM([[
92+
#include <stdio.h>
93+
int f() {
94+
return 0;
95+
}
96+
]])],
97+
[AC_MSG_RESULT([yes])
98+
CFLAGS="$saved_CFLAGS"
99+
CFLAGS=$( printf "%s" "$CFLAGS" | sed -e 's/ -O2/ /' )
100+
AM_CFLAGS="$AM_CFLAGS $new_CFLAGS"],
101+
[AC_MSG_RESULT([no])
102+
CFLAGS="$saved_CFLAGS"])
103+
104+
# Can we use "inline"? We don't use AC_C _INLINE because metamath.c
105+
# does not include the autoconf-generated file.
106+
AC_MSG_CHECKING([[for 'inline' support]])
107+
new_CFLAGS="-DINLINE=inline"
108+
saved_CFLAGS="$CFLAGS"
109+
CFLAGS="$CFLAGS $new_CFLAGS"
110+
AC_COMPILE_IFELSE(
111+
[AC_LANG_SOURCE([[
112+
inline int f(void) {}
113+
]])],
114+
[AC_MSG_RESULT([yes])
115+
AM_CFLAGS="$AM_CFLAGS $new_CFLAGS"],
116+
[AC_MSG_RESULT([no])])
117+
CFLAGS="$saved_CFLAGS"
118+
119+
# replace all @AM_CFLAGS@ and @CFLAGS@ variables in Makefile.am
120+
# with values found here
121+
echo "CFLAGS=$CFLAGS"
122+
AC_SUBST([AM_CFLAGS])
123+
AC_SUBST([CFLAGS])
124+
125+
AC_OUTPUT
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)