lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Jun 11, 2020 at 02:34:46PM -0300, Luiz Henrique de Figueiredo wrote:
> > In a perfect world, the Makefile would include code to detect whether the readline library exists and automatically use it if found.
> 
> If you can contribute simple portable code that does this, it'd be great.

I had an hour to kill. The following was tested and works correctly with the
native make implementations on AIX, FreeBSD, NetBSD, OpenBSD, and Solaris,
as well as with GNU Make. (But see AIX notes.) Excepting the command
substitution constructs and the macro expansion pattern replacement
construct ($(MACRO:PATT=REPL)), as far as I know everything else is POSIX
compliant. In particular, the use of recursive expansion and dynamically
evaluated macro names is sound as far as I understand. Macro expansion
pattern replacement is so well supported that the POSIX standard notes that
it may be mandated in the future.


BOOL,1 = 1
BOOL,0 = 0
BOOL,  = 0

OR,1,1 = 1
OR,1,0 = 1
OR,0,1 = 1
OR,0,0 = 0

HAVE_READLINE.cmd = printf "\043include <readline/readline.h>\n" | $(CC) $(CFLAGS) -E - 2>/dev/null | grep -q rl_readline_name && echo 1 || echo 0
HAVE_READLINE.gnu = $(shell $(HAVE_READLINE.cmd))
HAVE_READLINE.sun = $(HAVE_READLINE.cmd:sh)
HAVE_READLINE.if.aix,1 = 0
HAVE_READLINE.if.aix,0 = $(OR,$(BOOL,$(HAVE_READLINE.gnu)),$(BOOL,$(HAVE_READLINE.sun)))
HAVE_READLINE = $(HAVE_READLINE.if.aix,$(BOOL,$(BOOL,$(CCC:xlC%=1))))

all:
	@printf "HAVE_READLINE=%s\n" "$(HAVE_READLINE)"

# NOTES:
#
# * Tested with the native (non-GNU) make implementations on AIX 7.1,
#   FreeBSD 12.0, NetBSD 8.0, OpenBSD 3.6, and Solaris 11.4. Tested with
#   various GNU Make versions on all platforms, including macOS.
#
# * Macro strings are evaluated lazily, and syntax errors in macro
#   expansions only occur during evaluation.
#
# * All make implementations, as well as POSIX make, support recursive
#   expansion of macro names.
#
# * Expansion of undefined macros is okay and evaluates to the empty string.
#
# * GNU Make supports $(shell ...).
#
# * Solaris make, OpenBSD make, and FreeBSD/NetBSD make all support
#   $(MACRO:sh). OpenBSD doesn't document it, but it's supported
#   nonetheless.
#
# * No implementation supports both $(shell ...) and $(MACRO:sh), though
#   the use of $(OR) should result in correct expansion even if both
#   constructs are supported.
#
# * $(MACRO:sh) is a syntax error for AIX make, but only on evaluation.
# 
# * The BSD (and future POSIX) assignment operator != is also a syntax error
#   for AIX make and Solaris make, yet there's no way to lazily evaluate it
#   and thus no way to make evaluation conditional.
#
# * AIX make does not support any form of command substitution. The best
#   we can do is redirect expansions to something benign.
#
# * AIX make predefines $(CCC) to xlC in both native and POSIX modes, providing
#   a way to distinguish AIX. GNU Make (3.80) on AIX does not predefine
#   $(CCC) at all.
#
# * $(BOOL) expansion is done twice at the end to handle the case where
#   $(CCC) is defined to something other than "xlC", "0", or "1".