xref: /trunk/main/codemaker/source/cppumaker/cppumaker.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_cppumaker.hxx"
26 
27 #include <stdio.h>
28 
29 #include "sal/main.h"
30 
31 #include "codemaker/typemanager.hxx"
32 #include "codemaker/generatedtypeset.hxx"
33 
34 #include "cppuoptions.hxx"
35 #include "cpputype.hxx"
36 
37 using namespace rtl;
38 
39 namespace {
40 
failed(rtl::OString const & typeName,CppuOptions * options)41 void failed(rtl::OString const & typeName, CppuOptions * options) {
42     fprintf(stderr, "%s ERROR: %s\n", options->getProgramName().getStr(),
43             rtl::OString("cannot dump Type '" + typeName + "'").getStr());
44     exit(99);
45 }
46 
produce(RegistryKey & rTypeKey,bool bIsExtraType,TypeManager const & typeMgr,codemaker::GeneratedTypeSet & generated,CppuOptions * options)47 void produce(
48     RegistryKey& rTypeKey, bool bIsExtraType, TypeManager const & typeMgr,
49     codemaker::GeneratedTypeSet & generated, CppuOptions * options)
50 {
51     if (!produceType(rTypeKey, bIsExtraType, typeMgr, generated, options)) {
52         OString typeName = typeMgr.getTypeName(rTypeKey);
53         failed(typeName, options);
54     }
55 }
56 
produce(rtl::OString const & typeName,TypeManager const & typeMgr,codemaker::GeneratedTypeSet & generated,CppuOptions * options)57 void produce(
58     rtl::OString const & typeName, TypeManager const & typeMgr,
59     codemaker::GeneratedTypeSet & generated, CppuOptions * options)
60 {
61     if (!produceType(typeName, typeMgr, generated, options)) {
62         failed(typeName, options);
63     }
64 }
65 
produceAllTypes(RegistryKey & rTypeKey,bool bIsExtraType,TypeManager const & typeMgr,codemaker::GeneratedTypeSet & generated,CppuOptions * pOptions,sal_Bool bFullScope)66 void produceAllTypes(RegistryKey& rTypeKey, bool bIsExtraType,
67                          TypeManager const & typeMgr,
68                          codemaker::GeneratedTypeSet & generated,
69                          CppuOptions* pOptions,
70                          sal_Bool bFullScope)
71 {
72     OString typeName = typeMgr.getTypeName(rTypeKey);
73 
74     produce(rTypeKey, bIsExtraType, typeMgr, generated, pOptions);
75 
76     RegistryKeyList typeKeys = typeMgr.getTypeKeys(typeName);
77     RegistryKeyList::const_iterator iter = typeKeys.begin();
78     RegistryKey key, subKey;
79     RegistryKeyArray subKeys;
80 
81     while (iter != typeKeys.end())
82     {
83         key = (*iter).first;
84 
85         if (!(*iter).second  && !key.openSubKeys(OUString(), subKeys))
86         {
87             for (sal_uInt32 i = 0; i < subKeys.getLength(); i++)
88             {
89                 subKey = subKeys.getElement(i);
90                 if (bFullScope)
91                 {
92                     produceAllTypes(subKey, (*iter).second, typeMgr,
93                                     generated, pOptions, true);
94                 } else
95                 {
96                     produce(subKey, (*iter).second,
97                             typeMgr, generated, pOptions);
98                 }
99             }
100         }
101 
102         ++iter;
103     }
104 }
105 
produceAllTypes(const OString & typeName,TypeManager const & typeMgr,codemaker::GeneratedTypeSet & generated,CppuOptions * pOptions,sal_Bool bFullScope)106 void produceAllTypes(const OString& typeName,
107                      TypeManager const & typeMgr,
108                      codemaker::GeneratedTypeSet & generated,
109                      CppuOptions* pOptions,
110                      sal_Bool bFullScope)
111 {
112     produce(typeName, typeMgr, generated, pOptions);
113 
114     RegistryKeyList typeKeys = typeMgr.getTypeKeys(typeName);
115     RegistryKeyList::const_iterator iter = typeKeys.begin();
116     RegistryKey key, subKey;
117     RegistryKeyArray subKeys;
118 
119     while (iter != typeKeys.end())
120     {
121         key = (*iter).first;
122         if (!(*iter).second  && !key.openSubKeys(OUString(), subKeys))
123         {
124             for (sal_uInt32 i = 0; i < subKeys.getLength(); i++)
125             {
126                 subKey = subKeys.getElement(i);
127                 if (bFullScope)
128                 {
129                     produceAllTypes(subKey, (*iter).second, typeMgr,
130                                     generated, pOptions, true);
131                 } else
132                 {
133                     produce(subKey, (*iter).second,
134                             typeMgr, generated, pOptions);
135                 }
136             }
137         }
138 
139         ++iter;
140     }
141 }
142 
143 }
144 
SAL_IMPLEMENT_MAIN_WITH_ARGS(argc,argv)145 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
146 {
147     CppuOptions options;
148 
149     try
150     {
151         if (!options.initOptions(argc, argv))
152         {
153             exit(1);
154         }
155     }
156     catch( IllegalArgument& e)
157     {
158         fprintf(stderr, "Illegal option: %s\n", e.m_message.getStr());
159         exit(99);
160     }
161 
162     RegistryTypeManager typeMgr;
163 
164     if (!typeMgr.init(options.getInputFiles(), options.getExtraInputFiles()))
165     {
166         fprintf(stderr, "%s : init registries failed, check your registry files.\n", options.getProgramName().getStr());
167         exit(99);
168     }
169 
170     if (options.isValid("-B"))
171     {
172         typeMgr.setBase(options.getOption("-B"));
173     }
174 
175     codemaker::GeneratedTypeSet generated;
176     try
177     {
178         if (options.isValid("-T"))
179         {
180             OString tOption(options.getOption("-T"));
181 
182             OString typeName, tmpName;
183             sal_Int32 nIndex = 0;
184             do
185             {
186                 typeName = tOption.getToken(0, ';', nIndex);
187 
188                 sal_Int32 nPos = typeName.lastIndexOf( '.' );
189                 tmpName = typeName.copy( nPos != -1 ? nPos+1 : 0 );
190                 if (tmpName == "*")
191                 {
192                     // produce this type and his scope
193                     if (typeName.equals("*"))
194                     {
195                         tmpName = "/";
196                     } else
197                     {
198                         tmpName = typeName.copy(0, typeName.lastIndexOf('.')).replace('.', '/');
199                         if ( tmpName.isEmpty() )
200                             tmpName = "/";
201                         else
202                             tmpName.replace('.', '/');
203                     }
204                     // related to task #116780# the scope is recursively
205                     // generated.  bFullScope = true
206                     produceAllTypes(
207                         tmpName, typeMgr, generated, &options, true);
208                 } else
209                 {
210                     // produce only this type
211                     produce(
212                         typeName.replace('.', '/'), typeMgr, generated, &options);
213                 }
214             } while( nIndex != -1 );
215         } else
216         {
217             // produce all types
218             produceAllTypes("/", typeMgr, generated, &options, true);
219         }
220         // C++ header files generated for the following UNO types are included
221         // in header files in cppu/inc/com/sun/star/uno (Any.hxx, Reference.hxx,
222         // Type.h), so it seems best to always generate those C++ header files:
223         produce("com/sun/star/uno/RuntimeException", typeMgr, generated, &options);
224         produce("com/sun/star/uno/TypeClass", typeMgr, generated, &options);
225         produce("com/sun/star/uno/XInterface", typeMgr, generated, &options);
226     }
227     catch( CannotDumpException& e)
228     {
229         fprintf(stderr, "%s ERROR: %s\n",
230                 options.getProgramName().getStr(),
231                 e.m_message.getStr());
232         exit(99);
233     }
234 
235     return 0;
236 }
237