]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/tools/build/src/engine/hdrmacro.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / tools / build / src / engine / hdrmacro.cpp
1 /*
2 * Copyright 1993, 2000 Christopher Seiwald.
3 *
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
6
7 /* This file is ALSO:
8 * Copyright 2001-2004 David Abrahams.
9 * Distributed under the Boost Software License, Version 1.0.
10 * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
11 */
12
13 /*
14 * hdrmacro.c - handle header files that define macros used in #include
15 * statements.
16 *
17 * we look for lines like "#define MACRO <....>" or '#define MACRO " "' in
18 * the target file. When found, we then phony up a rule invocation like:
19 *
20 * $(HDRRULE) <target> : <resolved included files> ;
21 *
22 * External routines:
23 * headers1() - scan a target for "#include MACRO" lines and try to resolve
24 * them when needed
25 *
26 * Internal routines:
27 * headers1() - using regexp, scan a file and build include LIST
28 */
29
30 #include "jam.h"
31 #include "hdrmacro.h"
32
33 #include "compile.h"
34 #include "hash.h"
35 #include "lists.h"
36 #include "object.h"
37 #include "parse.h"
38 #include "rules.h"
39 #include "strings.h"
40 #include "subst.h"
41 #include "variable.h"
42 #include "output.h"
43
44
45 /* this type is used to store a dictionary of file header macros */
46 typedef struct header_macro
47 {
48 OBJECT * symbol;
49 OBJECT * filename; /* we could maybe use a LIST here ?? */
50 } HEADER_MACRO;
51
52 static struct hash * header_macros_hash = 0;
53
54
55 /*
56 * headers() - scan a target for include files and call HDRRULE
57 */
58
59 #define MAXINC 10
60
61 void macro_headers( TARGET * t )
62 {
63 static regexp * re = 0;
64 FILE * f;
65 char buf[ 1024 ];
66
67 if ( DEBUG_HEADER )
68 out_printf( "macro header scan for %s\n", object_str( t->name ) );
69
70 /* This regexp is used to detect lines of the form
71 * "#define MACRO <....>" or "#define MACRO "....."
72 * in the header macro files.
73 */
74 if ( !re )
75 {
76 OBJECT * const re_str = object_new(
77 "^[ ]*#[ ]*define[ ]*([A-Za-z][A-Za-z0-9_]*)[ ]*"
78 "[<\"]([^\">]*)[\">].*$" );
79 re = regex_compile( re_str );
80 object_free( re_str );
81 }
82
83 if ( !( f = fopen( object_str( t->boundname ), "r" ) ) )
84 return;
85
86 while ( fgets( buf, sizeof( buf ), f ) )
87 {
88 HEADER_MACRO var;
89 HEADER_MACRO * v = &var;
90
91 if ( regexec( re, buf ) && re->startp[ 1 ] )
92 {
93 OBJECT * symbol;
94 int found;
95 /* we detected a line that looks like "#define MACRO filename */
96 ( (char *)re->endp[ 1 ] )[ 0 ] = '\0';
97 ( (char *)re->endp[ 2 ] )[ 0 ] = '\0';
98
99 if ( DEBUG_HEADER )
100 out_printf( "macro '%s' used to define filename '%s' in '%s'\n",
101 re->startp[ 1 ], re->startp[ 2 ], object_str( t->boundname )
102 );
103
104 /* add macro definition to hash table */
105 if ( !header_macros_hash )
106 header_macros_hash = hashinit( sizeof( HEADER_MACRO ),
107 "hdrmacros" );
108
109 symbol = object_new( re->startp[ 1 ] );
110 v = (HEADER_MACRO *)hash_insert( header_macros_hash, symbol, &found
111 );
112 if ( !found )
113 {
114 v->symbol = symbol;
115 v->filename = object_new( re->startp[ 2 ] ); /* never freed */
116 }
117 else
118 object_free( symbol );
119 /* XXXX: FOR NOW, WE IGNORE MULTIPLE MACRO DEFINITIONS !! */
120 /* WE MIGHT AS WELL USE A LIST TO STORE THEM.. */
121 }
122 }
123
124 fclose( f );
125 }
126
127
128 OBJECT * macro_header_get( OBJECT * macro_name )
129 {
130 HEADER_MACRO * v;
131 if ( header_macros_hash && ( v = (HEADER_MACRO *)hash_find(
132 header_macros_hash, macro_name ) ) )
133 {
134 if ( DEBUG_HEADER )
135 out_printf( "### macro '%s' evaluated to '%s'\n", object_str( macro_name
136 ), object_str( v->filename ) );
137 return v->filename;
138 }
139 return 0;
140 }