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