xref: /trunk/main/codemaker/source/cppumaker/cppuoptions.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_codemaker.hxx"
30*cdf0e10cSrcweir #include <stdio.h>
31*cdf0e10cSrcweir #include <string.h>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "cppuoptions.hxx"
34*cdf0e10cSrcweir #include "osl/thread.h"
35*cdf0e10cSrcweir #include "osl/process.h"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #ifdef SAL_UNX
38*cdf0e10cSrcweir #define SEPARATOR '/'
39*cdf0e10cSrcweir #else
40*cdf0e10cSrcweir #define SEPARATOR '\\'
41*cdf0e10cSrcweir #endif
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir using namespace rtl;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir sal_Bool CppuOptions::initOptions(int ac, char* av[], sal_Bool bCmdFile)
46*cdf0e10cSrcweir     throw( IllegalArgument )
47*cdf0e10cSrcweir {
48*cdf0e10cSrcweir     sal_Bool    ret = sal_True;
49*cdf0e10cSrcweir     sal_uInt16  i=0;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir     if (!bCmdFile)
52*cdf0e10cSrcweir     {
53*cdf0e10cSrcweir         bCmdFile = sal_True;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir         OString name(av[0]);
56*cdf0e10cSrcweir         sal_Int32 index = name.lastIndexOf(SEPARATOR);
57*cdf0e10cSrcweir         m_program = name.copy((index > 0 ? index+1 : 0));
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir         if (ac < 2)
60*cdf0e10cSrcweir         {
61*cdf0e10cSrcweir             fprintf(stderr, "%s", prepareHelp().getStr());
62*cdf0e10cSrcweir             ret = sal_False;
63*cdf0e10cSrcweir         }
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir         i = 1;
66*cdf0e10cSrcweir     } else
67*cdf0e10cSrcweir     {
68*cdf0e10cSrcweir         i = 0;
69*cdf0e10cSrcweir     }
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir     char    *s=NULL;
72*cdf0e10cSrcweir     for( ; i < ac; i++)
73*cdf0e10cSrcweir     {
74*cdf0e10cSrcweir         if (av[i][0] == '-')
75*cdf0e10cSrcweir         {
76*cdf0e10cSrcweir             switch (av[i][1])
77*cdf0e10cSrcweir             {
78*cdf0e10cSrcweir                 case 'O':
79*cdf0e10cSrcweir                     if (av[i][2] == '\0')
80*cdf0e10cSrcweir                     {
81*cdf0e10cSrcweir                         if (i < ac - 1 && av[i+1][0] != '-')
82*cdf0e10cSrcweir                         {
83*cdf0e10cSrcweir                             i++;
84*cdf0e10cSrcweir                             s = av[i];
85*cdf0e10cSrcweir                         } else
86*cdf0e10cSrcweir                         {
87*cdf0e10cSrcweir                             OString tmp("'-O', please check");
88*cdf0e10cSrcweir                             if (i <= ac - 1)
89*cdf0e10cSrcweir                             {
90*cdf0e10cSrcweir                                 tmp += " your input '" + OString(av[i+1]) + "'";
91*cdf0e10cSrcweir                             }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir                             throw IllegalArgument(tmp);
94*cdf0e10cSrcweir                         }
95*cdf0e10cSrcweir                     } else
96*cdf0e10cSrcweir                     {
97*cdf0e10cSrcweir                         s = av[i] + 2;
98*cdf0e10cSrcweir                     }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir                     m_options["-O"] = OString(s);
101*cdf0e10cSrcweir                     break;
102*cdf0e10cSrcweir                 case 'B':
103*cdf0e10cSrcweir                     if (av[i][2] == '\0')
104*cdf0e10cSrcweir                     {
105*cdf0e10cSrcweir                         if (i < ac - 1 && av[i+1][0] != '-')
106*cdf0e10cSrcweir                         {
107*cdf0e10cSrcweir                             i++;
108*cdf0e10cSrcweir                             s = av[i];
109*cdf0e10cSrcweir                         } else
110*cdf0e10cSrcweir                         {
111*cdf0e10cSrcweir                             OString tmp("'-B', please check");
112*cdf0e10cSrcweir                             if (i <= ac - 1)
113*cdf0e10cSrcweir                             {
114*cdf0e10cSrcweir                                 tmp += " your input '" + OString(av[i+1]) + "'";
115*cdf0e10cSrcweir                             }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir                             throw IllegalArgument(tmp);
118*cdf0e10cSrcweir                         }
119*cdf0e10cSrcweir                     } else
120*cdf0e10cSrcweir                     {
121*cdf0e10cSrcweir                         s = av[i] + 2;
122*cdf0e10cSrcweir                     }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir                     m_options["-B"] = OString(s);
125*cdf0e10cSrcweir                     break;
126*cdf0e10cSrcweir                 case 'T':
127*cdf0e10cSrcweir                     if (av[i][2] == '\0')
128*cdf0e10cSrcweir                     {
129*cdf0e10cSrcweir                         if (i < ac - 1 && av[i+1][0] != '-')
130*cdf0e10cSrcweir                         {
131*cdf0e10cSrcweir                             i++;
132*cdf0e10cSrcweir                             s = av[i];
133*cdf0e10cSrcweir                         } else
134*cdf0e10cSrcweir                         {
135*cdf0e10cSrcweir                             OString tmp("'-T', please check");
136*cdf0e10cSrcweir                             if (i <= ac - 1)
137*cdf0e10cSrcweir                             {
138*cdf0e10cSrcweir                                 tmp += " your input '" + OString(av[i+1]) + "'";
139*cdf0e10cSrcweir                             }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir                             throw IllegalArgument(tmp);
142*cdf0e10cSrcweir                         }
143*cdf0e10cSrcweir                     } else
144*cdf0e10cSrcweir                     {
145*cdf0e10cSrcweir                         s = av[i] + 2;
146*cdf0e10cSrcweir                     }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir                     if (m_options.count("-T") > 0)
149*cdf0e10cSrcweir                     {
150*cdf0e10cSrcweir                         OString tmp(m_options["-T"]);
151*cdf0e10cSrcweir                         tmp = tmp + ";" + s;
152*cdf0e10cSrcweir                         m_options["-T"] = tmp;
153*cdf0e10cSrcweir                     } else
154*cdf0e10cSrcweir                     {
155*cdf0e10cSrcweir                         m_options["-T"] = OString(s);
156*cdf0e10cSrcweir                     }
157*cdf0e10cSrcweir                     break;
158*cdf0e10cSrcweir                 case 'L':
159*cdf0e10cSrcweir                     if (av[i][2] != '\0')
160*cdf0e10cSrcweir                     {
161*cdf0e10cSrcweir                         OString tmp("'-L', please check");
162*cdf0e10cSrcweir                         if (i <= ac - 1)
163*cdf0e10cSrcweir                         {
164*cdf0e10cSrcweir                             tmp += " your input '" + OString(av[i]) + "'";
165*cdf0e10cSrcweir                         }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir                         throw IllegalArgument(tmp);
168*cdf0e10cSrcweir                     }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir                     if (isValid("-C") || isValid("-CS"))
171*cdf0e10cSrcweir                     {
172*cdf0e10cSrcweir                         OString tmp("'-L' could not be combined with '-C' or '-CS' option");
173*cdf0e10cSrcweir                         throw IllegalArgument(tmp);
174*cdf0e10cSrcweir                     }
175*cdf0e10cSrcweir                     m_options["-L"] = OString("");
176*cdf0e10cSrcweir                     break;
177*cdf0e10cSrcweir                 case 'C':
178*cdf0e10cSrcweir                     if (av[i][2] == 'S')
179*cdf0e10cSrcweir                     {
180*cdf0e10cSrcweir                         if (av[i][3] != '\0')
181*cdf0e10cSrcweir                         {
182*cdf0e10cSrcweir                             OString tmp("'-CS', please check");
183*cdf0e10cSrcweir                             if (i <= ac - 1)
184*cdf0e10cSrcweir                             {
185*cdf0e10cSrcweir                                 tmp += " your input '" + OString(av[i]) + "'";
186*cdf0e10cSrcweir                             }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir                             throw IllegalArgument(tmp);
189*cdf0e10cSrcweir                         }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir                         if (isValid("-L") || isValid("-C"))
192*cdf0e10cSrcweir                         {
193*cdf0e10cSrcweir                             OString tmp("'-CS' could not be combined with '-L' or '-C' option");
194*cdf0e10cSrcweir                             throw IllegalArgument(tmp);
195*cdf0e10cSrcweir                         }
196*cdf0e10cSrcweir                         m_options["-CS"] = OString("");
197*cdf0e10cSrcweir                         break;
198*cdf0e10cSrcweir                     } else
199*cdf0e10cSrcweir                     if (av[i][2] != '\0')
200*cdf0e10cSrcweir                     {
201*cdf0e10cSrcweir                         OString tmp("'-C', please check");
202*cdf0e10cSrcweir                         if (i <= ac - 1)
203*cdf0e10cSrcweir                         {
204*cdf0e10cSrcweir                             tmp += " your input '" + OString(av[i]) + "'";
205*cdf0e10cSrcweir                         }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir                         throw IllegalArgument(tmp);
208*cdf0e10cSrcweir                     }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir                     if (isValid("-L") || isValid("-CS"))
211*cdf0e10cSrcweir                     {
212*cdf0e10cSrcweir                         OString tmp("'-C' could not be combined with '-L' or '-CS' option");
213*cdf0e10cSrcweir                         throw IllegalArgument(tmp);
214*cdf0e10cSrcweir                     }
215*cdf0e10cSrcweir                     m_options["-C"] = OString("");
216*cdf0e10cSrcweir                     break;
217*cdf0e10cSrcweir                 case 'G':
218*cdf0e10cSrcweir                     if (av[i][2] == 'c')
219*cdf0e10cSrcweir                     {
220*cdf0e10cSrcweir                         if (av[i][3] != '\0')
221*cdf0e10cSrcweir                         {
222*cdf0e10cSrcweir                             OString tmp("'-Gc', please check");
223*cdf0e10cSrcweir                             if (i <= ac - 1)
224*cdf0e10cSrcweir                             {
225*cdf0e10cSrcweir                                 tmp += " your input '" + OString(av[i]) + "'";
226*cdf0e10cSrcweir                             }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir                             throw IllegalArgument(tmp);
229*cdf0e10cSrcweir                         }
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir                         m_options["-Gc"] = OString("");
232*cdf0e10cSrcweir                         break;
233*cdf0e10cSrcweir                     } else
234*cdf0e10cSrcweir                     if (av[i][2] != '\0')
235*cdf0e10cSrcweir                     {
236*cdf0e10cSrcweir                         OString tmp("'-G', please check");
237*cdf0e10cSrcweir                         if (i <= ac - 1)
238*cdf0e10cSrcweir                         {
239*cdf0e10cSrcweir                             tmp += " your input '" + OString(av[i]) + "'";
240*cdf0e10cSrcweir                         }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir                         throw IllegalArgument(tmp);
243*cdf0e10cSrcweir                     }
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir                     m_options["-G"] = OString("");
246*cdf0e10cSrcweir                     break;
247*cdf0e10cSrcweir                 case 'X': // support for eXtra type rdbs
248*cdf0e10cSrcweir                 {
249*cdf0e10cSrcweir                     if (av[i][2] == '\0')
250*cdf0e10cSrcweir                     {
251*cdf0e10cSrcweir                         if (i < ac - 1 && av[i+1][0] != '-')
252*cdf0e10cSrcweir                         {
253*cdf0e10cSrcweir                             i++;
254*cdf0e10cSrcweir                             s = av[i];
255*cdf0e10cSrcweir                         } else
256*cdf0e10cSrcweir                         {
257*cdf0e10cSrcweir                             OString tmp("'-X', please check");
258*cdf0e10cSrcweir                             if (i <= ac - 1)
259*cdf0e10cSrcweir                             {
260*cdf0e10cSrcweir                                 tmp += " your input '" + OString(av[i+1]) + "'";
261*cdf0e10cSrcweir                             }
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir                             throw IllegalArgument(tmp);
264*cdf0e10cSrcweir                         }
265*cdf0e10cSrcweir                     } else
266*cdf0e10cSrcweir                     {
267*cdf0e10cSrcweir                         s = av[i] + 2;
268*cdf0e10cSrcweir                     }
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir                     m_extra_input_files.push_back( s );
271*cdf0e10cSrcweir                     break;
272*cdf0e10cSrcweir                 }
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir                 default:
275*cdf0e10cSrcweir                     throw IllegalArgument("the option is unknown" + OString(av[i]));
276*cdf0e10cSrcweir             }
277*cdf0e10cSrcweir         } else
278*cdf0e10cSrcweir         {
279*cdf0e10cSrcweir             if (av[i][0] == '@')
280*cdf0e10cSrcweir             {
281*cdf0e10cSrcweir                 FILE* cmdFile = fopen(av[i]+1, "r");
282*cdf0e10cSrcweir                 if( cmdFile == NULL )
283*cdf0e10cSrcweir                 {
284*cdf0e10cSrcweir                     fprintf(stderr, "%s", prepareHelp().getStr());
285*cdf0e10cSrcweir                     ret = sal_False;
286*cdf0e10cSrcweir                 } else
287*cdf0e10cSrcweir                 {
288*cdf0e10cSrcweir                     int rargc=0;
289*cdf0e10cSrcweir                     char* rargv[512];
290*cdf0e10cSrcweir                     char  buffer[512];
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir                     while ( fscanf(cmdFile, "%s", buffer) != EOF )
293*cdf0e10cSrcweir                     {
294*cdf0e10cSrcweir                         rargv[rargc]= strdup(buffer);
295*cdf0e10cSrcweir                         rargc++;
296*cdf0e10cSrcweir                     }
297*cdf0e10cSrcweir                     fclose(cmdFile);
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir                     ret = initOptions(rargc, rargv, bCmdFile);
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir                     for (long j=0; j < rargc; j++)
302*cdf0e10cSrcweir                     {
303*cdf0e10cSrcweir                         free(rargv[j]);
304*cdf0e10cSrcweir                     }
305*cdf0e10cSrcweir                 }
306*cdf0e10cSrcweir             } else
307*cdf0e10cSrcweir             {
308*cdf0e10cSrcweir                 if (bCmdFile)
309*cdf0e10cSrcweir                 {
310*cdf0e10cSrcweir                     m_inputFiles.push_back(av[i]);
311*cdf0e10cSrcweir                 } else
312*cdf0e10cSrcweir                 {
313*cdf0e10cSrcweir                     OUString system_filepath;
314*cdf0e10cSrcweir                     if (osl_getCommandArg( i-1, &system_filepath.pData )
315*cdf0e10cSrcweir                         != osl_Process_E_None)
316*cdf0e10cSrcweir                     {
317*cdf0e10cSrcweir                         OSL_ASSERT(false);
318*cdf0e10cSrcweir                     }
319*cdf0e10cSrcweir                     m_inputFiles.push_back(OUStringToOString(system_filepath, osl_getThreadTextEncoding()));
320*cdf0e10cSrcweir                 }
321*cdf0e10cSrcweir             }
322*cdf0e10cSrcweir         }
323*cdf0e10cSrcweir     }
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir     return ret;
326*cdf0e10cSrcweir }
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir OString CppuOptions::prepareHelp()
329*cdf0e10cSrcweir {
330*cdf0e10cSrcweir     OString help("\nusing: ");
331*cdf0e10cSrcweir     help += m_program + " [-options] file_1 ... file_n\nOptions:\n";
332*cdf0e10cSrcweir     help += "    -O<path>   = path describes the root directory for the generated output.\n";
333*cdf0e10cSrcweir     help += "                 The output directory tree is generated under this directory.\n";
334*cdf0e10cSrcweir     help += "    -T<name>   = name specifies a type or a list of types. The output for this\n";
335*cdf0e10cSrcweir     help += "      [t1;...]   type is generated. If no '-T' option is specified,\n";
336*cdf0e10cSrcweir     help += "                 then output for all types is generated.\n";
337*cdf0e10cSrcweir     help += "                 Example: 'com.sun.star.uno.XInterface' is a valid type.\n";
338*cdf0e10cSrcweir     help += "    -B<name>   = name specifies the base node. All types are searched under this\n";
339*cdf0e10cSrcweir     help += "                 node. Default is the root '/' of the registry files.\n";
340*cdf0e10cSrcweir     help += "    -L         = UNO type functions are generated lightweight, that means only\n";
341*cdf0e10cSrcweir     help += "                 the name and typeclass are given and everything else is retrieved\n";
342*cdf0e10cSrcweir     help += "                 from the type library dynamically. The default is that UNO type\n";
343*cdf0e10cSrcweir     help += "                 functions provides enough type information for boostrapping C++.\n";
344*cdf0e10cSrcweir     help += "                 '-L' should be the default for external components.\n";
345*cdf0e10cSrcweir     help += "    -C         = UNO type functions are generated comprehensive that means all\n";
346*cdf0e10cSrcweir     help += "                 necessary information is available for bridging the type in UNO.\n";
347*cdf0e10cSrcweir     help += "    -G         = generate only target files which does not exists.\n";
348*cdf0e10cSrcweir     help += "    -Gc        = generate only target files which content will be changed.\n";
349*cdf0e10cSrcweir     help += "    -X<file>   = extra types which will not be taken into account for generation.\n\n";
350*cdf0e10cSrcweir     help += prepareVersion();
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir     return help;
353*cdf0e10cSrcweir }
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir OString CppuOptions::prepareVersion()
356*cdf0e10cSrcweir {
357*cdf0e10cSrcweir     OString version(m_program);
358*cdf0e10cSrcweir     version += " Version 2.0\n\n";
359*cdf0e10cSrcweir     return version;
360*cdf0e10cSrcweir }
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir 
363