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