Experimental new redoconf C/C++ build/autoconfiguration system.

To test it out, try this:
	./do -j10 build
	cd docs/cookbook/c
	redo -j10 test

It should detect all the compilers on your system and make three
separate builds for each one: normal, debug, and optimized.  Then it
tries to run a test program under each one.

If there are windows cross compilers and you also have 'wine'
installed, it'll try running the test program under wine as well.

redoconf currently has no documentation other than the example program.
We'll fix that later.
This commit is contained in:
Avery Pennarun 2019-02-03 01:14:51 -05:00
commit 6dae51f4d2
67 changed files with 1777 additions and 1 deletions

68
redoconf/rc/CC.rc.od Normal file
View file

@ -0,0 +1,68 @@
. ./redoconf.rc
rc_include
rc_helpmsg ARCH "Architecture prefix for output (eg. i686-w64-mingw32-)"
rc_helpmsg CC "C compiler name (cc)"
rc_helpmsg CPPFLAGS "Extra C preprocessor flags (eg. -I... -D...)"
rc_helpmsg CFLAGS "Extra C compiler flags (eg. -O2 -g)"
rc_helpmsg OPTFLAGS "C/C++ compiler flag overrides (eg. -g0)"
rc_helpmsg LINK "Linker name (cc)"
rc_helpmsg LDFLAGS "Extra linker options (eg. -s -static)"
rc_helpmsg LIBS "Extra libraries to always link against (eg. -lsocket)"
rc_helpmsg STATIC "Link libraries and binaries statically"
if [ -n "$CC" ]; then
set -- "$CC"
else
if [ -n "$ARCH" ] && [ "$ARCH" = "${ARCH%-}" ]; then
# Make sure arch name includes trailing dash if nonempty
ARCH="$ARCH-"
fi
set -- \
"${ARCH}cc" "${ARCH}gcc" \
"${ARCH}clang" "/usr/bin/${ARCH}clang"-[0-9]* \
"${ARCH}c++" "${ARCH}g++" \
"${ARCH}clang++" "/usr/bin/${ARCH}clang++"-[0-9]*
fi
rc_appendln CPPFLAGS "-I."
[ -n "$STATIC" ] && rc_appendln LDFLAGS "-static"
for d in "$@"; do
echo "Trying C compiler: '$d'" >&2
if CC="$d" CXX="" LINK="$d" rc_compile cc link 'extern int i;'; then
rc_replaceln CC "$d"
rc_replaceln LINK "$d"
# mingw32 (not mingw64) generates binaries that need
# a dynamic libgcc at runtime for certain operations
# (like 64-bit integer division), which is non-obvious.
# Include it statically if possible, but only on Windows,
# which we'll detect by whether windows.h exists.
#
# If adding the option fails, maybe it's some
# compiler other than gcc, so that's fine.
x="-static-libgcc$NL-static-libstdc++"
appendln LDFLAGS "$x"
prog='#include <windows.h>'
if CC="$d" LINK="$d" rc_compile cc link "$prog"; then
rc_appendln LDFLAGS "$x"
fi
# Set ARCH= to the right compiler prefix, either empty
# (use native compiler) or something like "i686-w64-mingw32-"
# (terminating dash), based on the compiler name.
x="-${CC##*/}"
x="${x%-*}"
x="${x#-}"
[ -n "$x" ] && x="$x-"
rc_replaceln ARCH "$x"
rc_save
exit 0
fi
done
echo "Can't find a working C compiler." >&2
rc_undo
exit 1

35
redoconf/rc/CXX.rc.od Normal file
View file

@ -0,0 +1,35 @@
. ./redoconf.rc
rc_include rc/CC.rc
rc_helpmsg CXX "C++ compiler name (c++)"
rc_helpmsg CXXFLAGS "Extra C++ compiler flags (eg. -O2 -g)"
if [ -n "$CXX" ]; then
set -- "$CXX"
else
# Note: $ARCH has already been set correctly by CC.rc
set -- \
"${ARCH}c++" "${ARCH}g++" \
"${ARCH}clang++" "/usr/bin/${ARCH}clang++"-[0-9]*
fi
for d in "$@"; do
[ -n "$d" ] || continue
echo "Trying C++ compiler: '$d'" >&2
if CC="" CXX="$d" LINK="$d" rc_compile cxx link 'class A {};'; then
rc_replaceln CXX "$d"
# If the project activates CXX.rc, then we
# replace the C linker with C++. This causes
# it to include -lstdc++, etc.
# A future .rc could override this again.
rc_replaceln LINK "$d"
rc_save
exit 0
fi
done
echo "Warning: Can't find a working C++ compiler." >&2
rc_undo
rc_replaceln CXX ""
rc_save
exit 0

9
redoconf/rc/Wall.rc.od Normal file
View file

@ -0,0 +1,9 @@
. ./redoconf.rc
rc_include rc/CC.rc
rc_appendln CPPFLAGS "-Wall"
if rc_compile cc nolink; then
rc_save
else
rc_undo
fi

9
redoconf/rc/Wextra.rc.od Normal file
View file

@ -0,0 +1,9 @@
. ./redoconf.rc
rc_include rc/CC.rc
rc_appendln CPPFLAGS "-Wextra"
if rc_compile cc nolink; then
rc_save
else
rc_undo
fi

8
redoconf/rc/_init.rc.od Normal file
View file

@ -0,0 +1,8 @@
if [ -e _flags ]; then
redo-ifchange _flags
cat _flags >$3
redo-stamp <_flags
else
redo-ifcreate _flags
redo-stamp </dev/null
fi

View file

@ -0,0 +1,18 @@
. ./redoconf.rc
rc_include rc/CC.rc
base="${1#*/}"
lib="${base%.autolib.rc}"
LIB=$(echo "$lib" | tr 'a-z.' 'A-Z_')
x="-l$lib"
rc_appendln "LIB$LIB" "$x"
appendln LIBS "$x"
if rc_compile cc link ""; then
rc_replaceln "HAVE_$LIB" 1
else
rc_undo
rc_replaceln "HAVE_$LIB" ""
rc_replaceln "LIB$LIB" ""
fi
rc_save

View file

@ -0,0 +1,13 @@
. ./redoconf.rc
rc_include rc/CC.rc
base="${1#*/}"
f="${base%.func.rc}"
F=$(echo "$f" | tr 'a-z.' 'A-Z_')
if rc_compile cc link "void $f(); void test() { $f(); }"; then
rc_replaceln "HAVE_$F" 1
else
rc_replaceln "HAVE_$F" ""
fi
rc_save

View file

@ -0,0 +1,23 @@
. ./redoconf.rc
rc_include rc/CC.rc
base="${1#rc/}"
src="${base%.h.precompiled.rc}"
# The existence of the specific gcc warning about
# precompiled headers is a pretty good indicator
# that they are supported in the way we expect.
rc_appendln CFLAGS "-Winvalid-pch"
if rc_compile cc nolink; then
rc_appendln CFLAGS "$x"
rc_appendln CFLAGS_PCH "-include$NL$src.h"
rc_appendln CFLAGS_PCH_FPIC "-include$NL$src.h.fpic"
rc_appendln PRE_CC_TARGETS "$src.h.gch"
rc_appendln PRE_CC_TARGETS_FPIC "$src.h.fpic.gch"
rc_save
else
echo "Precompiled C headers not supported." >&2
rc_undo
fi

View file

@ -0,0 +1,13 @@
. ./redoconf.rc
rc_include rc/CC.rc
base="${1#*/}"
h="${base%.rc}"
H=$(echo "$h" | tr 'a-z.' 'A-Z_')
if rc_compile cc nolink "#include <$h>"; then
rc_replaceln "HAVE_$H" 1
else
rc_replaceln "HAVE_$H" ""
fi
rc_save

View file

@ -0,0 +1,23 @@
. ./redoconf.rc
rc_include rc/CXX.rc
base="${1#rc/}"
src="${base%.hpp.precompiled.rc}"
# The existence of the specific gcc warning about
# precompiled headers is a pretty good indicator
# that they are supported in the way we expect.
appendln CXXFLAGS "-Winvalid-pch"
if rc_compile cxx nolink; then
rc_appendln CXXFLAGS "$x"
rc_appendln CXXFLAGS_PCH "-include$NL$src.hpp"
rc_appendln CXXFLAGS_PCH_FPIC "-include$NL$src.hpp.fpic"
rc_appendln PRE_CXX_TARGETS "$src.hpp.gch"
rc_appendln PRE_CXX_TARGETS_FPIC "$src.hpp.fpic.gch"
rc_save
else
echo "Precompiled C++ headers not supported." >&2
rc_undo
fi

12
redoconf/rc/libgl.rc.od Normal file
View file

@ -0,0 +1,12 @@
. ./redoconf.rc
rc_include rc/CC.rc rc/pkg-config.rc
prog='
#include <GL/gl.h>
int x = GL_VERSION_1_1;
void f() { glPopMatrix(); }
'
rc_pkg_detect LIBGL "gl" \
rc_compile cc link "$prog"
rc_save

12
redoconf/rc/libgtk2.rc.od Normal file
View file

@ -0,0 +1,12 @@
. ./redoconf.rc
rc_include rc/CC.rc rc/pkg-config.rc
prog='
#include <gtk/gtk.h>
int x = GTK_MAJOR_VERSION;
void f() { gtk_widget_child_focus(0, 0); }
'
rc_pkg_detect LIBGTK2 "gtk+-2.0 gio-2.0 gdk-2.0 gdk-pixbuf-2.0" \
rc_compile cc link "$prog"
rc_save

25
redoconf/rc/libm.rc.od Normal file
View file

@ -0,0 +1,25 @@
. ./redoconf.rc
rc_include rc/CC.rc rc/pkg-config.rc
prog='
#include <math.h>
int x = FP_NORMAL;
volatile float y;
void f() { y = sin(y); }
'
rc_pkg_detect LIBM libm \
rc_compile cc link "$prog"
if [ -z "$HAVE_LIBM" ]; then
rc_undo
rc_replaceln HAVE_LIBM 1
rc_appendln LIBM "-lm"
appendln LIBS "$LIBM"
if ! rc_compile cc link "$prog"; then
rc_undo
rc_replaceln HAVE_LIBM ""
rc_replaceln LIBM ""
fi
fi
rc_save

12
redoconf/rc/libpng.rc.od Normal file
View file

@ -0,0 +1,12 @@
. ./redoconf.rc
rc_include rc/CC.rc rc/pkg-config.rc
prog='
#include <png.h>
const char *x = PNG_LIBPNG_VER_STRING;
void f() { png_access_version_number(); }
'
rc_pkg_detect LIBPNG libpng \
rc_compile cc link "$prog"
rc_save

10
redoconf/rc/libqt4.rc.od Normal file
View file

@ -0,0 +1,10 @@
. ./redoconf.rc
rc_include rc/CXX.rc rc/pkg-config.rc
prog='
#include <QVector>
'
rc_pkg_detect LIBQT4 QtCore \
rc_compile cxx link "$prog"
rc_save

12
redoconf/rc/libsdl.rc.od Normal file
View file

@ -0,0 +1,12 @@
. ./redoconf.rc
rc_include rc/CC.rc rc/pkg-config.rc
prog='
#include <SDL.h>
int x = SDL_INIT_TIMER;
void f() { SDL_Init(0); }
'
rc_pkg_detect LIBSDL "sdl" \
rc_compile cc link "$prog"
rc_save

12
redoconf/rc/libx11.rc.od Normal file
View file

@ -0,0 +1,12 @@
. ./redoconf.rc
rc_include rc/CC.rc rc/pkg-config.rc
prog='
#include <X11/Xutil.h>
int x = XYBitmap;
void f() { XCreateRegion(); }
'
rc_pkg_detect LIBX11 "x11" \
rc_compile cc link "$prog"
rc_save

View file

@ -0,0 +1,17 @@
. ./redoconf.rc
rc_include
for d in "$PKG_CONFIG" pkg-config; do
rc_undo
if "$d" --version >/dev/null 2>&1; then
rc_replaceln PKG_CONFIG "$d"
rc_replaceln HAVE_PKG_CONFIG 1
rc_save
exit 0
fi
done
# Failed
rc_replaceln HAVE_PKG_CONFIG ""
rc_replaceln PKG_CONFIG ""
rc_save

27
redoconf/rc/run.rc.od Normal file
View file

@ -0,0 +1,27 @@
. ./redoconf.rc
rc_include rc/CC.rc rc/windows.h.rc
consider() {
echo "Considering RUN=$(shquote "$1")" >&2
if [ -z "$1" ] || type "$1" >/dev/null 2>&1; then
rc_undo
rc_replaceln RUN "$1"
if RUN="$RUN" rc_compile cc run ""; then
rc_replaceln CAN_RUN 1
rc_save
exit 0
fi
fi
}
consider ""
if [ -n "$HAVE_WINDOWS_H" ]; then
consider "wine64"
consider "wine"
consider "wine32"
fi
rc_undo
rc_replaceln RUN ""
rc_replaceln CAN_RUN ""
rc_save

21
redoconf/rc/shlib.rc.od Normal file
View file

@ -0,0 +1,21 @@
. ./redoconf.rc
rc_include rc/CC.rc
appendln CFLAGS "-fPIC"
appendln LDFLAGS "-shared"
prog='
#include <stdlib.h>
void f() { atoi(""); }
'
if [ -n "$STATIC" ]; then
echo "--enable-static specified; not building shared libraries." >&2
rc_replaceln HAVE_SHLIB ""
elif RCC_NO_MAIN=1 rc_compile cc link "$prog"; then
rc_replaceln HAVE_SHLIB 1
else
echo "Not building shared libraries on this platform." >&2
rc_replaceln HAVE_SHLIB ""
fi
rc_save

11
redoconf/rc/zdefs.rc.od Normal file
View file

@ -0,0 +1,11 @@
. ./redoconf.rc
rc_include rc/CC.rc
x="-Wl,-z,defs"
rc_appendln LDFLAGS "$x"
if rc_compile cc link; then
rc_save
else
echo "'$x' doesn't work on this platform; skipped." >&2
rc_undo
fi