BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsls_ident.h
Go to the documentation of this file.
1/// @file bsls_ident.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsls_ident.h -*-C++-*-
8#ifndef INCLUDED_BSLS_IDENT
9#define INCLUDED_BSLS_IDENT
10
11/// @defgroup bsls_ident bsls_ident
12/// @brief Provide macros for inserting SCM Ids into source files.
13/// @addtogroup bsl
14/// @{
15/// @addtogroup bsls
16/// @{
17/// @addtogroup bsls_ident
18/// @{
19///
20/// <h1> Outline </h1>
21/// * <a href="#bsls_ident-purpose"> Purpose</a>
22/// * <a href="#bsls_ident-classes"> Classes </a>
23/// * <a href="#bsls_ident-macros"> Macros </a>
24/// * <a href="#bsls_ident-description"> Description </a>
25/// * <a href="#bsls_ident-macro-summary"> Macro Summary </a>
26/// * <a href="#bsls_ident-usage"> Usage </a>
27///
28/// # Purpose {#bsls_ident-purpose}
29/// Provide macros for inserting SCM Ids into source files.
30///
31/// # Classes {#bsls_ident-classes}
32///
33///
34/// # Macros {#bsls_ident-macros}
35///
36/// - BSLS_IDENT(identifier): inset `identifier` into `.comment` section
37/// - BSLS_IDENT_RCSID(tag, identifier): alternatively, use add `tag` as static
38/// - BSLS_IDENT_PRAGMA_ONCE: encapsulate `_Pragma("once")`
39///
40/// # Description {#bsls_ident-description}
41/// The purpose of this component is to provide macros for
42/// inserting SCM (Source Control Management) Ids into source files. SCM Ids
43/// are useful for identifying source revisions in binaries. Additional
44/// information about SCM Ids may be obtained from the following man pages:
45/// `man ident` `man strings` (`strings -a` produces more verbose output)
46/// `man mcs` (Solaris-only)
47///
48/// Note that these SCM Ids are only present if the `BSLS_IDENT_ON` macro is
49/// defined at compilation time. By default, this macro is *not* defined, and
50/// ident strings are *not* added to object files.
51///
52/// SCM systems may replace Ids with their expanded forms. Note that we will
53/// replace the key symbol `$` with `(DOLLAR)` to avoid any expansion within
54/// this header file's documentation.
55///
56/// SCM Ids usually take the form "(DOLLAR)Id: (DOLLAR)" which is expanded,
57/// automatically, by the source control system into an identifier which maps to
58/// specific source revision:
59/// @code
60/// '(DOLLAR)Id: bsls_ident.h 141104 2010-09-17 00:30:47Z mgiroux (DOLLAR)'
61/// @endcode
62/// This specifies that the file was checked in on 2010-09-17 at the specified
63/// time by user `mgiroux`, and can be retrieved from the SCM system using
64/// revision `141104`.
65///
66/// `BSLS_IDENT_PRAGMA_ONCE` can optionally be used in headers and encapsulates
67/// a non-standard pragma (_Pragma("once")) supported on a number of platforms
68/// and which indicates that a header should only be included and parsed once.
69/// Use of this macro can help reduce compile times by eliminating extraneous
70/// I/O when headers are included more than once in the same translation unit.
71/// Note that this macro should *not* be used for any header that cannot use
72/// include guards: this is unusual, but can happen for certain low-level
73/// headers.
74///
75/// ## Macro Summary {#bsls_ident-macro-summary}
76///
77///
78/// The following are the macros provided by this component.
79///
80/// * `BSLS_IDENT(identifier)`
81/// > This macro inserts the specified `identifier` into the object's
82/// > `.comment` section, if supported on the current platform.
83///
84/// * `BSLS_IDENT_RCSID(tag, identifier)`
85/// > This macro inserts the specified `identifier` into the object, using
86/// > `BSLS_IDENT`, if possible on the current platform. If `BSLS_IDENT` is
87/// > not available, the specified `tag` may be used to declare a static char
88/// > array containing the `tag`.
89///
90/// * `BSLS_IDENT_PRAGMA_ONCE`
91/// > This macro encapsulates the `_Pragma("once")` functionality if available
92/// > on the current platform. If available, this functions in the same way
93/// > as redundant include guards, avoiding re-opening already-included header
94/// > files.
95///
96/// ## Usage {#bsls_ident-usage}
97///
98///
99/// Include `bsls_ident.h` and use the BSLS_IDENT macro. For header files this
100/// should be done directly after the include guards, e.g., bsls_somefile.h:
101/// @code
102/// // bsls_somefile.h -*-C++-*-
103/// #ifndef INCLUDED_BSLS_SOMEFILE
104/// #define INCLUDED_BSLS_SOMEFILE
105///
106/// #include <bsls_ident.h>
107/// BSLS_IDENT("(DOLLAR)Id: (DOLLAR)") // In real usage, replace '(DOLLAR)'
108/// // with '$'
109///
110/// // ...
111///
112/// #endif // INCLUDED_BSLS_SOMEFILE
113/// @endcode
114/// For cpp files it should be done directly after the comment for the file name
115/// and the language, e.g., bsls_somefile.cpp:
116/// @code
117/// // bsls_ident.t.cpp -*-C++-*-
118///
119/// #include <bsls_ident.h>
120/// BSLS_IDENT("(DOLLAR)Id: (DOLLAR)") // In real usage, replace '(DOLLAR)'
121/// // with '$'
122/// @endcode
123/// @}
124/** @} */
125/** @} */
126
127/** @addtogroup bsl
128 * @{
129 */
130/** @addtogroup bsls
131 * @{
132 */
133/** @addtogroup bsls_ident
134 * @{
135 */
136
137#undef BDE_OMIT_INTERNAL_DEPRECATED
138#ifdef BDE_FORCE_OMIT_INTERNAL_DEPRECATED
139 #define BDE_OMIT_INTERNAL_DEPRECATED
140#endif
141
142#undef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
143#ifdef BDE_FORCE_DONT_ALLOW_TRANSITIVE_INCLUDES
144 #define BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
145#endif
146
147/* ident string intentionally omitted for this header (do not add to binaries)
148 * Its use is expected to be so extensive that the cost outweighs benefit
149 * of including an ident string for every file that includes this header
150 * (present here so that programs like update_rcsid do not accidentally add) */
151#if 0
152#define BSLS_IDENT_RCSID(tag,str)
153BSLS_IDENT_RCSID(sysutil_ident_h,"$Id: $")
154#endif
155
156// Enabling BSLS_IDENT by default causes significant bloat - see internal
157// ticket D29644737.
158#ifndef BSLS_IDENT_ON
159#ifndef BSLS_IDENT_OFF
160#define BSLS_IDENT_OFF
161#endif // ifndef BSLS_IDENT_OFF
162#endif // ifndef BSLS_IDENT_ON
163
164/* BSLS_IDENT() - insert string into .comment binary segment (if supported)*/
165
166#if defined(__GNUC__)
167 #if !defined(_AIX)
168 /* does not work with AIX as; might work with GNU as (gas) (not tested) */
169 /* gcc has no pragma equivalent for #ident */
170 #define _BSLS_IDENT(str) __asm__(#str);
171 #define BSLS_IDENT(str) _BSLS_IDENT(.ident str)
172 #endif
173#elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
174 /* Sun Studio CC does not support _Pragma() until Sun Studio 12 */
175 #if (!defined(__cplusplus)) || (__SUNPRO_CC >= 0x590)
176 #define _BSLS_IDENT(str) _Pragma(#str)
177 #define BSLS_IDENT(str) _BSLS_IDENT(ident str)
178 #endif
179#elif defined(__IBMC__) || defined(__IBMCPP__)
180 #define _BSLS_IDENT(str) _Pragma(#str)
181 #define BSLS_IDENT(str) _BSLS_IDENT(comment (user, str))
182#elif defined(_MSC_VER) /* Microsoft Visual Studio Compiler */
183 /* Microsoft linker ignores __pragma(comment (user, "str"))
184 * http://msdn.microsoft.com/en-us/library/7f0aews7.aspx */
185 #if 0 /* disable SYSUTIL_IDENT() with Microsoft compiler */
186 #define _BSLS_IDENT(str) __pragma(comment (user, #str))
187 #define BSLS_IDENT(str) _BSLS_IDENT(str)
188 #endif
189#elif defined(__HP_cc) || defined(__HP_aCC)
190 #define _BSLS_IDENT(str) _Pragma(#str)
191 #define BSLS_IDENT(str) _BSLS_IDENT(versionid str)
192#endif
193
194#ifndef BSLS_IDENT
195#define BSLS_IDENT(str)
196#endif
197
198
199/* multi-level indirection to force macro expansion before concatenation */
200/* (order of concatenation/macro expansion unspecified in ANSI standard) */
201#define BSLS_IDENT_JOIN2(x,y) x ## y
202#define BSLS_IDENT_JOIN(x,y) BSLS_IDENT_JOIN2(x,y)
203
204
205/* BSLS_IDENT_RCSID() - insert ident str (specific to platform/compiler) */
206
207#if defined(__GNUC__)
208 #if !defined(_AIX)
209 #define BSLS_IDENT_RCSID(tag,str) BSLS_IDENT(str)
210 #else /* _AIX */
211 /* (XXX: look into further if we ever use gcc to build prod tasks on AIX)
212 */
213 #ifndef lint
214 #define BSLS_IDENT_RCSID(tag,str) \
215 static char BSLS_IDENT_JOIN(ident_,tag)[] __attribute__((__unused__)) \
216 = str;
217 #endif
218 #endif
219#elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
220 #if (!defined(__cplusplus)) || (__SUNPRO_CC >= 0x590)
221 #define BSLS_IDENT_RCSID(tag,str) BSLS_IDENT(str)
222 #else /* Sun Studio CC does not support _Pragma() until Sun Studio 12 */
223 #ifndef lint
224 #define BSLS_IDENT_RCSID(tag,str) \
225 static char BSLS_IDENT_JOIN(ident_,tag)[] = str;
226 #endif
227 #endif
228#elif defined(__IBMC__) || defined(__IBMCPP__)
229 #if ((defined(__IBMC__) && __IBMC__ >= 1010) \
230 || (defined(__IBMCPP__) && __IBMCPP__ >= 1010))
231 #define BSLS_IDENT_RCSID(tag,str) BSLS_IDENT(str)
232 #else
233 /* Early versions of IBM xlc did not preserve .comment in binary
234 * executables or pre-linked libraries. Fixed by IBM in following
235 * releases:
236 * xlC v8.0 in May 2008 PTF (8.0.0.19) with -qxflag=new_pragma_comment_user
237 * xlC v9.0 in July 2008 PTF (9.0.0.5)
238 * xlC v10.1 GA
239 * Enabled above only for xlC 10.1 or later, which can be detected reliably
240 * (Use C compiler and printf("%s\n", __xlc__) to see x.x.x.x version,
241 * but strings are not comparable in macros)
242 * Note that using updated linker (bind64) (circa late 2010) is also needed
243 * for the ident strings to be present in pre-linked libs and executables.
244 */
245 /* various ways to create C string, including const, volatile, more */
246 /* static char instead of static const char with xlC -qnoro -qnoroconst */
247 /* xlC might end up optimizing this away, anyway */
248 #ifndef lint
249 #define BSLS_IDENT_RCSID(tag,str) \
250 static char BSLS_IDENT_JOIN(ident_,tag)[] = str;
251 #endif
252 #endif
253#elif defined(_MSC_VER)
254 #define BSLS_IDENT_RCSID(tag,str) BSLS_IDENT(str)
255#elif defined(__HP_cc) || defined(__HP_aCC)
256 #define BSLS_IDENT_RCSID(tag,str) BSLS_IDENT(str)
257#else
258 /* others: add conditions above for compilers able to support BSLS_IDENT */
259 #ifndef lint
260 #define BSLS_IDENT_RCSID(tag,str) \
261 static char BSLS_IDENT_JOIN(ident_,tag)[] = str;
262 #endif
263#endif
264
265#ifndef BSLS_IDENT_RCSID
266#define BSLS_IDENT_RCSID(tag,str)
267#endif
268
269
270/* timestamp compilation with BUILDID, if provided. (occurs once per object)
271 * Use BSLS_IDENT() so only occurs in .comment section if supported.
272 * example usage in a Makefile target:
273 * $(CC) -c -DBUILDID="$(@F) `date +%Y%m%d_%H%M%S`" -o file.o file.c
274 */
275#ifdef BUILDID
276 #define _BSLS_IDENT_BUILDID_IMP2(str) BSLS_IDENT(#str)
277 #define _BSLS_IDENT_BUILDID_IMP(str) _BSLS_IDENT_BUILDID_IMP2(str)
278 _BSLS_IDENT_BUILDID_IMP($cc: BUILDID $)
279 #undef _BSLS_IDENT_BUILDID_IMP
280 #undef _BSLS_IDENT_BUILDID_IMP2
281#endif
282
283
284#ifdef BSLS_IDENT_OFF
285
286#undef BSLS_IDENT
287#undef BSLS_IDENT_RCSID
288#define BSLS_IDENT(str)
289#define BSLS_IDENT_RCSID(tag,str)
290
291#endif /* !BSLS_IDENT_OFF */
292
293
294#if defined(__GNUC__)
295 #define BSLS_IDENT_PRAGMA_ONCE _Pragma("once")
296#elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
297 /* Sun Studio does not support #pragma once. Instead, it natively detects if
298 * the entire non-comment portion of a file is wrapped by #ifndef, and if so,
299 * optimizes away reopens of the file if the #ifndef condition is false */
301#elif defined(__IBMC__) || defined(__IBMCPP__)
302 #define BSLS_IDENT_PRAGMA_ONCE _Pragma("once")
303#elif defined(_MSC_VER)
304 #define BSLS_IDENT_PRAGMA_ONCE __pragma(once)
305#elif (defined(__HP_cc) && __HP_cc-0 >= 62500) \
306 || (defined(__HP_aCC) && __HP_aCC-0 >= 62500)
307 /* supported in aCC A.06.25 (had not been fully supported in aCC A.06.20) */
308 #define BSLS_IDENT_PRAGMA_ONCE _Pragma("once")
309#else
311#endif
313
314
315/* Technical notes:
316 *
317 * There is a cost to including ident extra information in the binary objects,
318 * which may include disk space usage and runtime memory usage, as well as
319 * side effects such as locality of strings in memory at runtime.
320 *
321 * Some vendors provide a mechanism to include ident strings in a non-loadable
322 * .comment section of the binary so that strings are available for review in
323 * the on-disk binary, but are not loaded into memory at runtime.
324 * (Alternatively, this separate non-loadable section might selectively be
325 * stripped from the binary, saved in a database with the checksum of the
326 * stripped binary, and then the resulting smaller binary moved to production.)
327 *
328 * bsls_ident.h encapsulates ident mechanisms so that includers of this
329 * header need not be concerned with the mechanism applied. The mechanism may
330 * be changed as better methods become available and the includer can obtain
331 * the changes simply by recompiling (without needing to modify all other
332 * source code).
333 *
334 * 'ident' shows only ident-style strings. 'man ident' for more info.
335 * The tokens passed to the pragmas are not necessarily ident-style.
336 * They are still visible with 'strings -a'.
337 *
338 * IBM xlC supports #pragma comment
339 * IBM xlC warns about #pragma ident and errors for #ident
340 * Sun CC supports #pragma ident and #ident
341 * Sun CC silently ignores #pragma comment
342 * GCC supports #ident
343 * GCC warns about unrecognized #pragma ident and #pragma comment
344 * While these can all be worked around by disabling specific warnings/errors,
345 * doing so might mask other warnings/errors. This header allows for
346 * encapsulation of all the conditional logic to use the correct pragma with
347 * each compiler without having to duplicate these conditions in every source
348 * file.
349 *
350 * #ident, #pragma ident, #pragma comment each can take a -single- user-defined
351 * token (no concatenation of string constants or preprocessor macro ##
352 * concatenation) (MS Visual Studio will do string concatenation)
353 *
354 * C99 _Pragma() can expanded in macros and can be used in place of #pragma.
355 * GCC does not implement a #pragma version of gcc #ident preprocessor
356 * directive
357 *
358 * AIX 'strip' removes strings inserted by #pragma comment
359 * Solaris mcs -d removes strings inserted by #ident and #pragma ident
360 * (equivalent to compiling with cc -mr)
361 * mcs -c uniquifies the strings (equivalent to compiling with cc -mc)
362 * ('strip' does not remove these strings)
363 * GNU strip: 'strip -R comment' will remove .comment section
364 *
365 * AIX multiple #pragma comment end up concatenated on a single line, allowing
366 * for pasting together of individual tokens. 'ident' prints each ident-style
367 * string on its own line. Solaris #ident places each token on its own line.
368 *
369 * Sun Studio CC (C++ compiler) < Sun Studio 12 CC do not support _Pragma()
370 * Sun Studio 12 CC supports _Pragma() but clips the first character of string
371 * (This leads to strings being present in .comment section, minus the first
372 * character, and then not showing up when 'ident' is run, but present with
373 * strings -a) (Bug filed with Sun and has been fixed in latest Studio 12).
374 */
375
376#endif // INCLUDED_BSLS_IDENT
377
378// ----------------------------------------------------------------------------
379// Copyright 2013 Bloomberg Finance L.P.
380//
381// Licensed under the Apache License, Version 2.0 (the "License");
382// you may not use this file except in compliance with the License.
383// You may obtain a copy of the License at
384//
385// http://www.apache.org/licenses/LICENSE-2.0
386//
387// Unless required by applicable law or agreed to in writing, software
388// distributed under the License is distributed on an "AS IS" BASIS,
389// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
390// See the License for the specific language governing permissions and
391// limitations under the License.
392// ----------------------------- END-OF-FILE ----------------------------------
393
394/** @} */
395/** @} */
396/** @} */
#define BSLS_IDENT_RCSID(tag, str)
Definition bsls_ident.h:260
#define BSLS_IDENT_PRAGMA_ONCE
Definition bsls_ident.h:310
#define BSLS_IDENT_OFF
Definition bsls_ident.h:160
#define BSLS_IDENT(str)
Definition bsls_ident.h:195