[m-rev.] for review: Check availability of getopt() directly.

Peter Wang novalazy at gmail.com
Fri May 31 18:13:17 AEST 2019


configure.ac:
Mmake.common.in:
    Check for availability of getopt() in the C library,
    setting the HAVE_GETOPT variable as appropriate.

    Delete GETOPT_H_AVAILABLE variable.

util/Mmakefile:
    Use getopt() if available. This can be true even without getopt.h
    as getopt() is declared in <unistd.h> according to POSIX.

util/getopt.h:
    Add a comment.

diff --git a/Mmake.common.in b/Mmake.common.in
index dcc92ea64..aeca60c9c 100644
--- a/Mmake.common.in
+++ b/Mmake.common.in
@@ -252,8 +252,8 @@ OBJFILE_OPT=@OBJFILE_OPT@$(empty_string)
 # Are we compiling using the MS C compiler?
 USING_MICROSOFT_CL_COMPILER=@USING_MICROSOFT_CL_COMPILER@
 
-# Is the getopt.h header file available?
-GETOPT_H_AVAILABLE=@GETOPT_H_AVAILABLE@
+# Is getopt() available?
+HAVE_GETOPT=@HAVE_GETOPT@
 
 #-----------------------------------------------------------------------------#
 
diff --git a/configure.ac b/configure.ac
index 88b52bd7c..3ca4a4b95 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1422,13 +1422,6 @@ MERCURY_CHECK_FOR_HEADERS( \
         semaphore.h pthread.h time.h spawn.h fenv.h sys/mman.h sys/sem.h \
         sched.h utmpx.h dispatch/dispatch.h stdint.h sys/select.h )
 
-if test "$MR_HAVE_GETOPT_H" = 1; then
-    GETOPT_H_AVAILABLE=yes
-else
-    GETOPT_H_AVAILABLE=no
-fi
-AC_SUBST(GETOPT_H_AVAILABLE)
-
 if test "$MR_HAVE_UCONTEXT_H" != 1; then
     MERCURY_CHECK_FOR_HEADERS(sys/ucontext.h)
 fi
@@ -1484,6 +1477,16 @@ if test "$mercury_cv_have_builtin_bswap64" = yes; then
     AC_DEFINE(MR_HAVE_BUILTIN_BSWAP64)
 fi
 
+#-----------------------------------------------------------------------------#
+#
+# Check whether getopt() is available
+#
+
+AC_CHECK_FUNC(getopt,
+    [HAVE_GETOPT=yes],
+    [HAVE_GETOPT=no])
+AC_SUBST(HAVE_GETOPT)
+
 #-----------------------------------------------------------------------------#
 #
 # Check the basics of sigaction
diff --git a/util/Mmakefile b/util/Mmakefile
index 8ccce6a91..d8e22ee08 100644
--- a/util/Mmakefile
+++ b/util/Mmakefile
@@ -30,8 +30,10 @@ PROGS=mkinit mkinit_erl mdemangle mfiltercc info_to_mdb
 PROGFILENAMES=$(PROGS:%=%$(EXT_FOR_EXE))
 SRC=$(PROGS:%=%.c)
 
-# Only if getopt.h doesn't exist do we link in a version of getopt.
-ifeq ("$(GETOPT_H_AVAILABLE)","no")
+# Link in local copy of getopt() only if required.
+# XXX The files in runtime/GETOPT may not compile cleanly with modern
+# compilers. We should replace it with another implementation.
+ifeq ("$(HAVE_GETOPT)","no")
 	GETOPT_SRC=$(RUNTIME_DIR)/GETOPT/getopt.c $(RUNTIME_DIR)/GETOPT/getopt1.c
 else
 	GETOPT_SRC=
diff --git a/util/getopt.h b/util/getopt.h
index 0498c7500..c4702dadb 100644
--- a/util/getopt.h
+++ b/util/getopt.h
@@ -11,6 +11,7 @@
 ** because different systems have different ideas about
 ** where the `const's should go on the declaration of getopt().
 ** Also, some systems might have getopt() but not <getopt.h>.
+** According to POSIX, getopt() is declared in <unistd.h>.
 */
 
 #ifndef	MERCURY_UTIL_GETOPT_H
-- 
2.21.0



More information about the reviews mailing list