Add convenience macros OSSL_DEPRECATEDIN_{major}_{minor}
[openssl.git] / include / openssl / macros.h
1 /*
2  * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/opensslconf.h>
11 #include <openssl/opensslv.h>
12
13 #ifndef OPENSSL_MACROS_H
14 # define OPENSSL_MACROS_H
15
16 /* Helper macros for CPP string composition */
17 # define OPENSSL_MSTR_HELPER(x) #x
18 # define OPENSSL_MSTR(x) OPENSSL_MSTR_HELPER(x)
19
20 /*
21  * Sometimes OPENSSSL_NO_xxx ends up with an empty file and some compilers
22  * don't like that.  This will hopefully silence them.
23  */
24 # define NON_EMPTY_TRANSLATION_UNIT static void *dummy = &dummy;
25
26 /*
27  * Generic deprecation macro
28  *
29  * If OPENSSL_SUPPRESS_DEPRECATED is defined, then OSSL_DEPRECATED and
30  * OSSL_DEPRECATED_FOR become no-ops
31  */
32 # ifndef OSSL_DEPRECATED
33 #  undef OSSL_DEPRECATED_FOR
34 #  ifndef OPENSSL_SUPPRESS_DEPRECATED
35 #   if defined(_MSC_VER)
36      /*
37       * MSVC supports __declspec(deprecated) since MSVC 2003 (13.10),
38       * and __declspec(deprecated(message)) since MSVC 2005 (14.00)
39       */
40 #    if _MSC_VER >= 1400
41 #     define OSSL_DEPRECATED(since) \
42           __declspec(deprecated("Since OpenSSL " # since))
43 #     define OSSL_DEPRECATED_FOR(since, message) \
44           __declspec(deprecated("Since OpenSSL " # since ";" message))
45 #    elif _MSC_VER >= 1310
46 #     define OSSL_DEPRECATED(since) __declspec(deprecated)
47 #     define OSSL_DEPRECATED_FOR(since, message) __declspec(deprecated)
48 #    endif
49 #   elif defined(__GNUC__)
50      /*
51       * According to GCC documentation, deprecations with message appeared in
52       * GCC 4.5.0
53       */
54 #    if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
55 #     define OSSL_DEPRECATED(since) \
56           __attribute__((deprecated("Since OpenSSL " # since)))
57 #     define OSSL_DEPRECATED_FOR(since, message) \
58           __attribute__((deprecated("Since OpenSSL " # since ";" message)))
59 #    elif __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 0)
60 #     define OSSL_DEPRECATED(since) __attribute__((deprecated))
61 #     define OSSL_DEPRECATED_FOR(since, message) __attribute__((deprecated))
62 #    endif
63 #   elif defined(__SUNPRO_C)
64 #    if (__SUNPRO_C >= 0x5130)
65 #     define OSSL_DEPRECATED(since) __attribute__ ((deprecated))
66 #     define OSSL_DEPRECATED_FOR(since, message) __attribute__ ((deprecated))
67 #    endif
68 #   endif
69 #  endif
70 # endif
71
72 /* Still not defined?  Then define empty macros */
73 # ifndef OSSL_DEPRECATED
74 #  define OSSL_DEPRECATED(since)
75 #  define OSSL_DEPRECATED_FOR(since, message)
76 # endif
77
78 /*
79  * Applications should use -DOPENSSL_API_COMPAT=<version> to suppress the
80  * declarations of functions deprecated in or before <version>.  If this is
81  * undefined, the value of the macro OPENSSL_CONFIGURED_API (defined in
82  * <openssl/opensslconf.h>) is the default.
83  *
84  * For any version number up until version 1.1.x, <version> is expected to be
85  * the calculated version number 0xMNNFFPPSL.
86  * For version numbers 3.0 and on, <version> is expected to be a computation
87  * of the major and minor numbers in decimal using this formula:
88  *
89  *     MAJOR * 10000 + MINOR * 100
90  *
91  * So version 3.0 becomes 30000, version 3.2 becomes 30200, etc.
92  */
93
94 /*
95  * We use the OPENSSL_API_COMPAT value to define API level macros.  These
96  * macros are used to enable or disable features at that API version boundary.
97  */
98
99 # ifdef OPENSSL_API_LEVEL
100 #  error "OPENSSL_API_LEVEL must not be defined by application"
101 # endif
102
103 /*
104  * We figure out what API level was intended by simple numeric comparison.
105  * The lowest old style number we recognise is 0x00908000L, so we take some
106  * safety margin and assume that anything below 0x00900000L is a new style
107  * number.  This allows new versions up to and including v943.71.83.
108  */
109 # ifdef OPENSSL_API_COMPAT
110 #  if OPENSSL_API_COMPAT < 0x900000L
111 #   define OPENSSL_API_LEVEL (OPENSSL_API_COMPAT)
112 #  else
113 #   define OPENSSL_API_LEVEL                            \
114            (((OPENSSL_API_COMPAT >> 28) & 0xF) * 10000  \
115             + ((OPENSSL_API_COMPAT >> 20) & 0xFF) * 100 \
116             + ((OPENSSL_API_COMPAT >> 12) & 0xFF))
117 #  endif
118 # endif
119
120 /*
121  * If OPENSSL_API_COMPAT wasn't given, we use default numbers to set
122  * the API compatibility level.
123  */
124 # ifndef OPENSSL_API_LEVEL
125 #  if OPENSSL_CONFIGURED_API > 0
126 #   define OPENSSL_API_LEVEL (OPENSSL_CONFIGURED_API)
127 #  else
128 #   define OPENSSL_API_LEVEL \
129            (OPENSSL_VERSION_MAJOR * 10000 + OPENSSL_VERSION_MINOR * 100)
130 #  endif
131 # endif
132
133 # if OPENSSL_API_LEVEL > OPENSSL_CONFIGURED_API
134 #  error "The requested API level higher than the configured API compatibility level"
135 # endif
136
137 /*
138  * Check of sane values.
139  */
140 /* Can't go higher than the current version. */
141 # if OPENSSL_API_LEVEL > (OPENSSL_VERSION_MAJOR * 10000 + OPENSSL_VERSION_MINOR * 100)
142 #  error "OPENSSL_API_COMPAT expresses an impossible API compatibility level"
143 # endif
144 /* OpenSSL will have no version 2.y.z */
145 # if OPENSSL_API_LEVEL < 30000 && OPENSSL_API_LEVEL >= 20000
146 #  error "OPENSSL_API_COMPAT expresses an impossible API compatibility level"
147 # endif
148 /* Below 0.9.8 is unacceptably low */
149 # if OPENSSL_API_LEVEL < 908
150 #  error "OPENSSL_API_COMPAT expresses an impossible API compatibility level"
151 # endif
152
153 /*
154  * Define macros for deprecation and simulated removal purposes.
155  *
156  * The macros OSSL_DEPRECATED_{major}_{minor} are always defined for
157  * all OpenSSL versions we care for.  They can be used as attributes
158  * in function declarations where appropriate.
159  *
160  * The macros OPENSSL_NO_DEPRECATED_{major}_{minor} are defined for
161  * all OpenSSL versions up to or equal to the version given with
162  * OPENSSL_API_COMPAT.  They are used as guards around anything that's
163  * deprecated up to that version, as an effect of the developer option
164  * 'no-deprecated'.
165  */
166
167 # undef OPENSSL_NO_DEPRECATED_3_0
168 # undef OPENSSL_NO_DEPRECATED_1_1_1
169 # undef OPENSSL_NO_DEPRECATED_1_1_0
170 # undef OPENSSL_NO_DEPRECATED_1_0_2
171 # undef OPENSSL_NO_DEPRECATED_1_0_1
172 # undef OPENSSL_NO_DEPRECATED_1_0_0
173 # undef OPENSSL_NO_DEPRECATED_0_9_8
174
175 # if OPENSSL_API_LEVEL >= 30000
176 #  ifndef OPENSSL_NO_DEPRECATED
177 #   define OSSL_DEPRECATEDIN_3_0                OSSL_DEPRECATED(3.0)
178 #   define OSSL_DEPRECATEDIN_3_0_FOR(msg)       OSSL_DEPRECATED_FOR(3.0, msg)
179 #   define DEPRECATEDIN_3_0(f)                  OSSL_DEPRECATEDIN_3_0 f;
180 #  else
181 #   define OPENSSL_NO_DEPRECATED_3_0
182 #   define DEPRECATEDIN_3_0(f)
183 #  endif
184 # else
185 #  define OSSL_DEPRECATEDIN_3_0
186 #  define OSSL_DEPRECATEDIN_3_0_FOR(msg)
187 #  define DEPRECATEDIN_3_0(f)                   f;
188 # endif
189 # if OPENSSL_API_LEVEL >= 10101
190 #  ifndef OPENSSL_NO_DEPRECATED
191 #   define OSSL_DEPRECATEDIN_1_1_1              OSSL_DEPRECATED(1.1.1)
192 #   define OSSL_DEPRECATEDIN_1_1_1_FOR(msg)     OSSL_DEPRECATED_FOR(1.1.1, msg)
193 #   define DEPRECATEDIN_1_1_1(f)                OSSL_DEPRECATEDIN_1_1_1 f;
194 #  else
195 #   define OPENSSL_NO_DEPRECATED_1_1_1
196 #   define DEPRECATEDIN_1_1_1(f)
197 #  endif
198 # else
199 #  define OSSL_DEPRECATEDIN_1_1_1
200 #  define OSSL_DEPRECATEDIN_1_1_1_FOR(msg)
201 #  define DEPRECATEDIN_1_1_1(f)                 f;
202 # endif
203 # if OPENSSL_API_LEVEL >= 10100
204 #  ifndef OPENSSL_NO_DEPRECATED
205 #   define OSSL_DEPRECATEDIN_1_1_0              OSSL_DEPRECATED(1.1.0)
206 #   define OSSL_DEPRECATEDIN_1_1_0_FOR(msg)     OSSL_DEPRECATED_FOR(1.1.0, msg)
207 #   define DEPRECATEDIN_1_1_0(f)                OSSL_DEPRECATEDIN_1_1_0 f;
208 #  else
209 #   define OPENSSL_NO_DEPRECATED_1_1_0
210 #   define DEPRECATEDIN_1_1_0(f)
211 #  endif
212 # else
213 #  define OSSL_DEPRECATEDIN_1_1_0
214 #  define OSSL_DEPRECATEDIN_1_1_0_FOR(msg)
215 #  define DEPRECATEDIN_1_1_0(f)                 f;
216 # endif
217 # if OPENSSL_API_LEVEL >= 10002
218 #  ifndef OPENSSL_NO_DEPRECATED
219 #   define OSSL_DEPRECATEDIN_1_0_2              OSSL_DEPRECATED(1.0.2)
220 #   define OSSL_DEPRECATEDIN_1_0_2_FOR(msg)     OSSL_DEPRECATED_FOR(1.0.2, msg)
221 #   define DEPRECATEDIN_1_0_2(f)                OSSL_DEPRECATEDIN_1_0_2 f;
222 #  else
223 #   define OPENSSL_NO_DEPRECATED_1_0_2
224 #   define DEPRECATEDIN_1_0_2(f)
225 #  endif
226 # else
227 #  define OSSL_DEPRECATEDIN_1_0_2
228 #  define OSSL_DEPRECATEDIN_1_0_2_FOR(msg)
229 #  define DEPRECATEDIN_1_0_2(f)                 f;
230 # endif
231 # if OPENSSL_API_LEVEL >= 10001
232 #  ifndef OPENSSL_NO_DEPRECATED
233 #   define OSSL_DEPRECATEDIN_1_0_1              OSSL_DEPRECATED(1.0.1)
234 #   define OSSL_DEPRECATEDIN_1_0_1_FOR(msg)     OSSL_DEPRECATED_FOR(1.0.1, msg)
235 #   define DEPRECATEDIN_1_0_1(f)                OSSL_DEPRECATEDIN_1_0_1 f;
236 #  else
237 #   define OPENSSL_NO_DEPRECATED_1_0_1
238 #   define DEPRECATEDIN_1_0_1(f)
239 #  endif
240 # else
241 #  define OSSL_DEPRECATEDIN_1_0_1
242 #  define OSSL_DEPRECATEDIN_1_0_1_FOR(msg)
243 #  define DEPRECATEDIN_1_0_1(f)                 f;
244 # endif
245 # if OPENSSL_API_LEVEL >= 10000
246 #  ifndef OPENSSL_NO_DEPRECATED
247 #   define OSSL_DEPRECATEDIN_1_0_0              OSSL_DEPRECATED(1.0.0)
248 #   define OSSL_DEPRECATEDIN_1_0_0_FOR(msg)     OSSL_DEPRECATED_FOR(1.0.0, msg)
249 #   define DEPRECATEDIN_1_0_0(f)                OSSL_DEPRECATEDIN_1_0_0 f;
250 #  else
251 #   define OPENSSL_NO_DEPRECATED_1_0_0
252 #   define DEPRECATEDIN_1_0_0(f)
253 #  endif
254 # else
255 #  define OSSL_DEPRECATEDIN_1_0_0
256 #  define OSSL_DEPRECATEDIN_1_0_0_FOR(msg)
257 #  define DEPRECATEDIN_1_0_0(f)                 f;
258 # endif
259 # if OPENSSL_API_LEVEL >= 908
260 #  ifndef OPENSSL_NO_DEPRECATED
261 #   define OSSL_DEPRECATEDIN_0_9_8              OSSL_DEPRECATED(0.9.8)
262 #   define OSSL_DEPRECATEDIN_0_9_8_FOR(msg)     OSSL_DEPRECATED_FOR(0.9.8, msg)
263 #   define DEPRECATEDIN_0_9_8(f)                OSSL_DEPRECATEDIN_0_9_8 f;
264 #  else
265 #   define OPENSSL_NO_DEPRECATED_0_9_8
266 #   define DEPRECATEDIN_0_9_8(f)
267 #  endif
268 # else
269 #  define OSSL_DEPRECATEDIN_0_9_8
270 #  define OSSL_DEPRECATEDIN_0_9_8_FOR(msg)
271 #  define DEPRECATEDIN_0_9_8(f)                 f;
272 # endif
273
274 /*
275  * Make our own variants of __FILE__ and __LINE__, depending on configuration
276  */
277
278 # ifndef OPENSSL_FILE
279 #  ifdef OPENSSL_NO_FILENAMES
280 #   define OPENSSL_FILE ""
281 #   define OPENSSL_LINE 0
282 #  else
283 #   define OPENSSL_FILE __FILE__
284 #   define OPENSSL_LINE __LINE__
285 #  endif
286 # endif
287
288 /*
289  * __func__ was standardized in C99, so for any compiler that claims
290  * to implement that language level or newer, we assume we can safely
291  * use that symbol.
292  *
293  * GNU C also provides __FUNCTION__ since version 2, which predates
294  * C99.  We can, however, only use this if __STDC_VERSION__ exists,
295  * as it's otherwise not allowed according to ISO C standards (C90).
296  * (compiling with GNU C's -pedantic tells us so)
297  *
298  * If none of the above applies, we check if the compiler is MSVC,
299  * and use __FUNCTION__ if that's the case.
300  */
301 # ifndef OPENSSL_FUNC
302 #  if defined(__STDC_VERSION__)
303 #   if __STDC_VERSION__ >= 199901L
304 #    define OPENSSL_FUNC __func__
305 #   elif defined(__GNUC__) && __GNUC__ >= 2
306 #    define OPENSSL_FUNC __FUNCTION__
307 #   endif
308 #  elif defined(_MSC_VER)
309 #    define OPENSSL_FUNC __FUNCTION__
310 #  endif
311 /*
312  * If all these possibilities are exhausted, we give up and use a
313  * static string.
314  */
315 #  ifndef OPENSSL_FUNC
316 #   define OPENSSL_FUNC "(unknown function)"
317 #  endif
318 # endif
319
320 #endif  /* OPENSSL_MACROS_H */