From 33e3ac9056f046affd69e3b8871275e5e2697aa4 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Sat, 26 May 2018 16:18:43 +0100 Subject: [PATCH] utils/: Split compiler-version macros into a separate header. This way we can use them in other headers without the namespace mess of `macros.h'. Also, actually document `CLANG_VERSION_P'. --- utils/Makefile.am | 4 ++++ utils/compiler.3 | 35 +++++++++++++++++++++++++++++++++ utils/compiler.h | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils/macros.3 | 12 +++++++++--- utils/macros.h | 21 ++++++-------------- 5 files changed, 112 insertions(+), 18 deletions(-) create mode 100644 utils/compiler.3 create mode 100644 utils/compiler.h diff --git a/utils/Makefile.am b/utils/Makefile.am index b52e7f6..2f6ed7c 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -32,6 +32,10 @@ libutils_la_SOURCES = ###-------------------------------------------------------------------------- ### Component files. +## Compiler checking. +pkginclude_HEADERS += compiler.h +LIBMANS += compiler.3 + ## Utility macros. pkginclude_HEADERS += macros.h LIBMANS += macros.3 diff --git a/utils/compiler.3 b/utils/compiler.3 new file mode 100644 index 0000000..1920be4 --- /dev/null +++ b/utils/compiler.3 @@ -0,0 +1,35 @@ +.\" -*-nroff-*- +.TH compiler 3 "26 May 2018" "Straylight/Edgeware" "mLib utilities library" +.SH NAME +compiler \- detect compiler version +.\" @GCC_VERSION_P +.\" @CLANG_VERSION_P +.SH SYNOPSIS +.nf +.B "#include " + +.BI "int GCC_VERSION_P(" maj ", " min ");" +.BI "int CLANG_VERSION_P(" maj ", " min ");" +.fi +.SH DESCRIPTION +The macro invocation +.BI GCC_VERSION_P( maj ", " min ) +expands to a compile-time constant nonzero value if the present compiler +is GCC version +.IR maj . min +or better, or claims compatibility with it. +This is frequently imperfect, as many compilers claim compatibility +without implementing all of the necessary features, but it works +adequately if one takes care. +.PP +The macro invocation +.BI CLANG_VERSION_P( maj ", " min ) +expands to a compile-time constant nonzero value if the present compiler +is Clang version +.IR maj . min +or better (or claims compatibility with it, but this is less likely +than for GCC. +.SH "SEE ALSO" +.BR mLib (3). +.SH "AUTHOR" +Mark Wooding, diff --git a/utils/compiler.h b/utils/compiler.h new file mode 100644 index 0000000..7e66d12 --- /dev/null +++ b/utils/compiler.h @@ -0,0 +1,58 @@ +/* -*-c-*- + * + * Macros for determining the compiler version + * + * (c) 2018 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of the mLib utilities library. + * + * mLib is free software: you can redistribute it and/or modify it under + * the terms of the GNU Library General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * mLib is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + * License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with mLib. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ + +#ifndef MLIB_COMPILER_H +#define MLIB_COMPILER_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Macros ------------------------------------------------------------*/ + +#if defined(__GNUC__) +# define GCC_VERSION_P(maj, min) \ + (__GNUC__ > (maj) || (__GNUC__ == (maj) && __GNUC_MINOR__ >= (min))) +#else +# define GCC_VERSION_P(maj, min) 0 +#endif + +#ifdef __clang__ +# define CLANG_VERSION_P(maj, min) \ + (__clang_major__ > (maj) || (__clang_major__ == (maj) && \ + __clang_minor__ >= (min))) +#else +# define CLANG_VERSION_P(maj, min) 0 +#endif + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif diff --git a/utils/macros.3 b/utils/macros.3 index bd8cbb6..a2a9003 100644 --- a/utils/macros.3 +++ b/utils/macros.3 @@ -16,7 +16,6 @@ macros \- useful macros .\" @MUFFLE_WARNINGS_DECL .\" @MUFFLE_WARNINGS_EXPR .\" @MUFFLE_WARNINGS_STMT -.\" @GCC_VERSION_P .\" @GCC_WARNING .SH SYNOPSIS .nf @@ -40,7 +39,6 @@ macros \- useful macros .BI "MUFFLE_WARNINGS_EXPR(" warns ", " expr ")" .BI "MUFFLE_WARNINGS_STMT(" warns ", " stmt ")" -.BI "int GCC_VERSION_P(" maj ", " min ");" .BI "GCC_WARNING(" option ")" .BI "CLANG_WARNING(" option ")" .fi @@ -168,7 +166,15 @@ naming a GCC warning option, e.g., The .B CLANG_WARNING is similar, except that it works with the Clang compiler. +.PP +Note that including +.B +also defines the compiler-test macros in +.BR ; +see +.BR compiler (3). .SH "SEE ALSO" -.BR mLib (3). +.BR mLib (3), +.BR compiler (3). .SH "AUTHOR" Mark Wooding, diff --git a/utils/macros.h b/utils/macros.h index 02d820e..bcceecd 100644 --- a/utils/macros.h +++ b/utils/macros.h @@ -32,6 +32,12 @@ extern "C" { #endif +/*----- Header files ------------------------------------------------------*/ + +#ifndef MLIB_COMPILER_H +# include "compiler.h" +#endif + /*----- Miscellaneous utility macros --------------------------------------*/ #define N(v) (sizeof(v)/sizeof(*v)) @@ -46,21 +52,6 @@ /* --- Compiler-specific definitions --- */ -#if defined(__GNUC__) -# define GCC_VERSION_P(maj, min) \ - (__GNUC__ > (maj) || (__GNUC__ == (maj) && __GNUC_MINOR__ >= (min))) -#else -# define GCC_VERSION_P(maj, min) 0 -#endif - -#ifdef __clang__ -# define CLANG_VERSION_P(maj, min) \ - (__clang_major__ > (maj) || (__clang_major__ == (maj) && \ - __clang_minor__ >= (min))) -#else -# define CLANG_VERSION_P(maj, min) 0 -#endif - #if GCC_VERSION_P(2, 5) || CLANG_VERSION_P(3, 3) # define NORETURN __attribute__((noreturn)) # define PRINTF_LIKE(fix, aix) __attribute__((format(printf, fix, aix))) -- 2.11.0