develooper Front page | perl.perl5.porters | Postings from September 2011

probing for <stdbool.h>

Thread Next
From:
Nicholas Clark
Date:
September 14, 2011 13:27
Subject:
probing for <stdbool.h>
Message ID:
20110914202736.GN23881@plum.flirble.org
tl;dr: There's currently no way to #include <mach-o/dyld.h> on OS X.


In the fractal mess of yak shaving that "relocatable @INC" always seems
to expand to, I learned, somewhat belatedly, how to figure out the
absolute path to the executable on OS X:

_NSGetExecutablePath(), then tidy the result with realpath().

To use the former, #include <mach-o/dyld.h>

At which point everything goes badly wrong, because it includes <stdbool.h>,
and *that* #defines bool, to a type which clashes with what handy.h defines
bool to be.

I find this comment in handy.h

/* XXX Configure ought to have a test for a boolean type, if I can
   just figure out all the headers such a test needs.
   Andy Dougherty	August 1996
*/


That predates C99. Now that compilers are getting *close* to full C99
conformance, is it safe to assume that <stdbool.h> is the only header we
need to test for?

If so, does the appended patch make sense? The business part of it is:

diff --git a/handy.h b/handy.h
index b75eafb..5e30512 100644
--- a/handy.h
+++ b/handy.h
@@ -70,10 +70,13 @@ Null SV pointer. (No longer available when C<PERL_CORE> is defined.)
 #define MUTABLE_IO(p)	((IO *)MUTABLE_PTR(p))
 #define MUTABLE_SV(p)	((SV *)MUTABLE_PTR(p))
 
-/* XXX Configure ought to have a test for a boolean type, if I can
-   just figure out all the headers such a test needs.
-   Andy Dougherty	August 1996
-*/
+#ifdef I_STDBOOL
+#  include <stdbool.h>
+#  ifndef HAS_BOOL
+#    define HAS_BOOL 1
+#  endif
+#endif
+
 /* bool is built-in for g++-2.6.3 and later, which might be used
    for extensions.  <_G_config.h> defines _G_HAVE_BOOL, but we can't
    be sure _G_config.h will be included before this file.  _G_config.h


It works on (at least) OS X, Linux and Win32 (verified by George Greer's
smoker), and with it it's possible to include <mach-o/dyld.h> without
error. [And thus implement code to make $^X absolute. Which I have locally,
but I'm not sure how to best probe for a useful realpath, and
_NSGetExecutablePath.]

Nicholas Clark





From e6da76e1068324cb218ae9aab97e615b06d79386 Mon Sep 17 00:00:00 2001
From: Nicholas Clark <nick@ccl4.org>
Date: Wed, 14 Sep 2011 14:03:31 +0200
Subject: [PATCH] Probe for <stdbool.h>, and if found use it in handy.h

This means that the core uses the compiler's bool type if one exists.
This avoids potential problems of clashes between perl's own implementation
of bool and the compiler's bool type, which otherwise occur when one
attempts to include headers which in turn include <stdbool.h>.
---
 Configure                 |    6 ++++++
 Cross/config.sh-arm-linux |    1 +
 Porting/Glossary          |    5 +++++
 Porting/config.sh         |    1 +
 Porting/config_H          |    6 ++++++
 config_h.SH               |    6 ++++++
 epoc/config.sh            |    1 +
 handy.h                   |   11 +++++++----
 plan9/config_sh.sample    |    1 +
 symbian/config.sh         |    1 +
 uconfig.h                 |   10 ++++++++--
 uconfig.sh                |    1 +
 uconfig64.sh              |    1 +
 win32/config.ce           |    1 +
 win32/config.gc           |    1 +
 win32/config.gc64         |    1 +
 win32/config.gc64nox      |    1 +
 win32/config.vc           |    1 +
 win32/config.vc64         |    1 +
 19 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/Configure b/Configure
index 74450a9..d9cdd03 100755
--- a/Configure
+++ b/Configure
@@ -926,6 +926,7 @@ i_pwd=''
 i_sfio=''
 i_shadow=''
 i_socks=''
+i_stdbool=''
 i_stddef=''
 i_stdlib=''
 i_string=''
@@ -21764,6 +21765,10 @@ set i_termio; eval $setvar
 val=$val2; set i_sgtty; eval $setvar
 val=$val3; set i_termios; eval $setvar
 
+: see if stdbool is available
+set stdbool.h i_stdbool
+eval $inhdr
+
 : see if stddef is available
 set stddef.h i_stddef
 eval $inhdr
@@ -23051,6 +23056,7 @@ i_sgtty='$i_sgtty'
 i_shadow='$i_shadow'
 i_socks='$i_socks'
 i_stdarg='$i_stdarg'
+i_stdbool='$i_stdbool'
 i_stddef='$i_stddef'
 i_stdlib='$i_stdlib'
 i_string='$i_string'
diff --git a/Cross/config.sh-arm-linux b/Cross/config.sh-arm-linux
index acbc403..fe81fd9 100644
--- a/Cross/config.sh-arm-linux
+++ b/Cross/config.sh-arm-linux
@@ -676,6 +676,7 @@ i_sgtty='undef'
 i_shadow='define'
 i_socks='undef'
 i_stdarg='define'
+i_stdbool='undef'
 i_stddef='define'
 i_stdlib='define'
 i_string='define'
diff --git a/Porting/Glossary b/Porting/Glossary
index 9134dfa..ceed6a5 100644
--- a/Porting/Glossary
+++ b/Porting/Glossary
@@ -3164,6 +3164,11 @@ i_stdarg (i_varhdr.U):
 	indicates to the C program that <stdarg.h> exists and should
 	be included.
 
+i_stdbool (i_stdbool.U):
+	This variable conditionally defines the I_STDBOOL symbol, which
+	indicates to the C program that <stdbool.h> exists and should
+	be included.
+
 i_stddef (i_stddef.U):
 	This variable conditionally defines the I_STDDEF symbol, which
 	indicates to the C program that <stddef.h> exists and should
diff --git a/Porting/config.sh b/Porting/config.sh
index eb9db2a..d66b9f4 100644
--- a/Porting/config.sh
+++ b/Porting/config.sh
@@ -693,6 +693,7 @@ i_sgtty='undef'
 i_shadow='define'
 i_socks='undef'
 i_stdarg='define'
+i_stdbool='undef'
 i_stddef='define'
 i_stdlib='define'
 i_string='define'
diff --git a/Porting/config_H b/Porting/config_H
index 190ea8e..6ad9bc9 100644
--- a/Porting/config_H
+++ b/Porting/config_H
@@ -713,6 +713,12 @@
  */
 /*#define	I_SFIO		/ **/
 
+/* I_STDBOOL:
+ *	This symbol, if defined, indicates that <stdbool.h> exists and should
+ *	be included.
+ */
+/*#define I_STDBOOL	/ **/
+
 /* I_STDDEF:
  *	This symbol, if defined, indicates that <stddef.h> exists and should
  *	be included.
diff --git a/config_h.SH b/config_h.SH
index 96748fa..f11612b 100755
--- a/config_h.SH
+++ b/config_h.SH
@@ -742,6 +742,12 @@ sed <<!GROK!THIS! >$CONFIG_H -e 's!^#undef\(.*/\)\*!/\*#define\1 \*!' -e 's!^#un
  */
 #$i_sfio	I_SFIO		/**/
 
+/* I_STDBOOL:
+ *	This symbol, if defined, indicates that <stdbool.h> exists and should
+ *	be included.
+ */
+#$i_stdbool I_STDBOOL	/**/
+
 /* I_STDDEF:
  *	This symbol, if defined, indicates that <stddef.h> exists and should
  *	be included.
diff --git a/epoc/config.sh b/epoc/config.sh
index faabef5..a971d13 100644
--- a/epoc/config.sh
+++ b/epoc/config.sh
@@ -620,6 +620,7 @@ i_sgtty='undef'
 i_shadow='undef'
 i_socks='undef'
 i_stdarg='define'
+i_stdbool='undef'
 i_stddef='define'
 i_stdlib='define'
 i_string='define'
diff --git a/handy.h b/handy.h
index b75eafb..5e30512 100644
--- a/handy.h
+++ b/handy.h
@@ -70,10 +70,13 @@ Null SV pointer. (No longer available when C<PERL_CORE> is defined.)
 #define MUTABLE_IO(p)	((IO *)MUTABLE_PTR(p))
 #define MUTABLE_SV(p)	((SV *)MUTABLE_PTR(p))
 
-/* XXX Configure ought to have a test for a boolean type, if I can
-   just figure out all the headers such a test needs.
-   Andy Dougherty	August 1996
-*/
+#ifdef I_STDBOOL
+#  include <stdbool.h>
+#  ifndef HAS_BOOL
+#    define HAS_BOOL 1
+#  endif
+#endif
+
 /* bool is built-in for g++-2.6.3 and later, which might be used
    for extensions.  <_G_config.h> defines _G_HAVE_BOOL, but we can't
    be sure _G_config.h will be included before this file.  _G_config.h
diff --git a/plan9/config_sh.sample b/plan9/config_sh.sample
index 98daaa6..a5e793f 100644
--- a/plan9/config_sh.sample
+++ b/plan9/config_sh.sample
@@ -670,6 +670,7 @@ i_sgtty='undef'
 i_shadow='undef'
 i_socks='undef'
 i_stdarg='define'
+i_stdbool='undef'
 i_stddef='define'
 i_stdlib='define'
 i_string='define'
diff --git a/symbian/config.sh b/symbian/config.sh
index 2e531ae..0641b63 100644
--- a/symbian/config.sh
+++ b/symbian/config.sh
@@ -597,6 +597,7 @@ i_sgtty='undef'
 i_shadow='undef'
 i_socks='undef'
 i_stdarg='define'
+i_stdbool='undef'
 i_stddef='undef'
 i_stdlib='define'
 i_string='define'
diff --git a/uconfig.h b/uconfig.h
index a745017..4d0b3f2 100644
--- a/uconfig.h
+++ b/uconfig.h
@@ -709,6 +709,12 @@
  */
 /*#define	I_SFIO		/ **/
 
+/* I_STDBOOL:
+ *	This symbol, if defined, indicates that <stdbool.h> exists and should
+ *	be included.
+ */
+/*#define I_STDBOOL	/ **/
+
 /* I_STDDEF:
  *	This symbol, if defined, indicates that <stddef.h> exists and should
  *	be included.
@@ -4696,6 +4702,6 @@
 #endif
 
 /* Generated from:
- * 31a462b40046313b877734092b49f853190d949d609c56fc0f03085f3aedc382 config_h.SH
- * 9ea5c7e8b0b32c76caab5a89a371b8cdce6efcf690a5a74962b9b856fab49fed uconfig.sh
+ * e73735b63cc1e7874e6402c8348f594458a1f16be24703d0e14ba9516f2b20df config_h.SH
+ * 572c1e5ebb7563dcf5fa02c1ff0b1ca588d8aab1d52e0ffed24245c95dc7a137 uconfig.sh
  * ex: set ro: */
diff --git a/uconfig.sh b/uconfig.sh
index 1798d0d..0f047ae 100644
--- a/uconfig.sh
+++ b/uconfig.sh
@@ -584,6 +584,7 @@ i_sgtty='undef'
 i_shadow='undef'
 i_socks='undef'
 i_stdarg='define'
+i_stdbool='undef'
 i_stddef='define'
 i_stdlib='define'
 i_string='define'
diff --git a/uconfig64.sh b/uconfig64.sh
index 3878431..7d94741 100644
--- a/uconfig64.sh
+++ b/uconfig64.sh
@@ -585,6 +585,7 @@ i_sgtty='undef'
 i_shadow='undef'
 i_socks='undef'
 i_stdarg='define'
+i_stdbool='undef'
 i_stddef='define'
 i_stdlib='define'
 i_string='define'
diff --git a/win32/config.ce b/win32/config.ce
index 47eeb71..4f76437 100644
--- a/win32/config.ce
+++ b/win32/config.ce
@@ -657,6 +657,7 @@ i_sgtty='undef'
 i_shadow='undef'
 i_socks='undef'
 i_stdarg='define'
+i_stdbool='undef'
 i_stddef='define'
 i_stdlib='define'
 i_string='define'
diff --git a/win32/config.gc b/win32/config.gc
index c49a27b..8c00871 100644
--- a/win32/config.gc
+++ b/win32/config.gc
@@ -665,6 +665,7 @@ i_sgtty='undef'
 i_shadow='undef'
 i_socks='undef'
 i_stdarg='define'
+i_stdbool='undef'
 i_stddef='define'
 i_stdlib='define'
 i_string='define'
diff --git a/win32/config.gc64 b/win32/config.gc64
index 52185f0..d225638 100644
--- a/win32/config.gc64
+++ b/win32/config.gc64
@@ -666,6 +666,7 @@ i_sgtty='undef'
 i_shadow='undef'
 i_socks='undef'
 i_stdarg='define'
+i_stdbool='undef'
 i_stddef='define'
 i_stdlib='define'
 i_string='define'
diff --git a/win32/config.gc64nox b/win32/config.gc64nox
index 9a797a1..4ad66fa 100644
--- a/win32/config.gc64nox
+++ b/win32/config.gc64nox
@@ -666,6 +666,7 @@ i_sgtty='undef'
 i_shadow='undef'
 i_socks='undef'
 i_stdarg='define'
+i_stdbool='undef'
 i_stddef='define'
 i_stdlib='define'
 i_string='define'
diff --git a/win32/config.vc b/win32/config.vc
index 3495a23..74ae275 100644
--- a/win32/config.vc
+++ b/win32/config.vc
@@ -665,6 +665,7 @@ i_sgtty='undef'
 i_shadow='undef'
 i_socks='undef'
 i_stdarg='define'
+i_stdbool='undef'
 i_stddef='define'
 i_stdlib='define'
 i_string='define'
diff --git a/win32/config.vc64 b/win32/config.vc64
index c9b1cb7..ac5e61e 100644
--- a/win32/config.vc64
+++ b/win32/config.vc64
@@ -665,6 +665,7 @@ i_sgtty='undef'
 i_shadow='undef'
 i_socks='undef'
 i_stdarg='define'
+i_stdbool='undef'
 i_stddef='define'
 i_stdlib='define'
 i_string='define'
-- 
1.7.4.3




Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About