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