fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / cli_ure / source / climaker / climaker_app.cxx
blob97191829e10ac2582465601e2b32614ac9c06058
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "sal/config.h"
22 #include <cstdlib>
23 #include <iostream>
24 #include <stdio.h>
25 #include <vector>
27 #include "climaker_share.h"
29 #include "sal/main.h"
30 #include "osl/process.h"
31 #include "osl/file.hxx"
32 #include "osl/thread.h"
33 #include "rtl/ustrbuf.hxx"
34 #include "cppuhelper/bootstrap.hxx"
35 #include "com/sun/star/lang/XComponent.hpp"
36 #include "com/sun/star/container/XHierarchicalNameAccess.hpp"
37 #include "com/sun/star/container/XSet.hpp"
38 #include "com/sun/star/reflection/XTypeDescriptionEnumerationAccess.hpp"
39 #include "com/sun/star/uno/XComponentContext.hpp"
40 #include "unoidl/unoidl.hxx"
42 using namespace ::std;
43 using namespace ::System::Reflection;
46 using namespace ::osl;
47 using namespace ::com::sun::star;
48 using namespace ::com::sun::star::uno;
50 namespace climaker
54 static char const s_usingText [] =
55 "\n"
56 "using: climaker <switches> [registry-file-1 registry-file-2 ...]\n"
57 "\n"
58 "switches:\n"
59 " -O, --out <output-file> output assembly file;\n"
60 " defaults to cli_unotypes.dll if more than one\n"
61 " registry-file is given, else <registry-file>.dll\n"
62 " -T, --types types to be generated (if none is given,\n"
63 " <type1[;type2;...]> then all types of given registries are emitted\n"
64 " -X, --extra <rdb-file> additional rdb to saturate referenced types in\n"
65 " given registry file(s); these types will not be\n"
66 " emitted into the output assembly file\n"
67 " -r, --reference reference metadata from assembly file\n"
68 " <assembly-file>\n"
69 " -k, --keyfile keyfile needed for strong name\n"
70 " --assembly-version <version> sets assembly version\n"
71 " --assembly-description <text> sets assembly description text\n"
72 " --assembly-product <text> sets assembly product name\n"
73 " --assembly-company <text> sets assembly company\n"
74 " --assembly-copyright <text> sets assembly copyright\n"
75 " --assembly-trademark <text> sets assembly trademark\n"
76 " -v, --verbose verbose output to stdout\n"
77 " -h, --help this message\n"
78 "\n"
79 "example: climaker --out cli_mytypes.dll \\\n"
80 " --reference cli_uretypes.dll \\\n"
81 " --extra types.rdb \\\n"
82 " mytypes.rdb\n"
83 "\n";
85 struct OptionInfo
87 char const * m_name;
88 sal_uInt32 m_name_length;
89 sal_Unicode m_short_option;
90 bool m_has_argument;
93 bool g_verbose = false;
96 static const OptionInfo s_option_infos [] = {
97 { RTL_CONSTASCII_STRINGPARAM("out"), 'O', true },
98 { RTL_CONSTASCII_STRINGPARAM("types"), 'T', true },
99 { RTL_CONSTASCII_STRINGPARAM("extra"), 'X', true },
100 { RTL_CONSTASCII_STRINGPARAM("reference"), 'r', true },
101 { RTL_CONSTASCII_STRINGPARAM("keyfile"), 'k', true },
102 { RTL_CONSTASCII_STRINGPARAM("delaySign"), 'd', true },
103 { RTL_CONSTASCII_STRINGPARAM("assembly-version"), '\0', true },
104 { RTL_CONSTASCII_STRINGPARAM("assembly-description"), '\0', true },
105 { RTL_CONSTASCII_STRINGPARAM("assembly-product"), '\0', true },
106 { RTL_CONSTASCII_STRINGPARAM("assembly-company"), '\0', true },
107 { RTL_CONSTASCII_STRINGPARAM("assembly-copyright"), '\0', true },
108 { RTL_CONSTASCII_STRINGPARAM("assembly-trademark"), '\0', true },
109 { RTL_CONSTASCII_STRINGPARAM("verbose"), 'v', false },
110 { RTL_CONSTASCII_STRINGPARAM("help"), 'h', false }
114 static OptionInfo const * get_option_info(
115 OUString const & opt, sal_Unicode copt = '\0' )
117 for ( sal_Int32 pos = 0;
118 pos < (sizeof (s_option_infos) / sizeof (OptionInfo));
119 ++pos )
121 OptionInfo const & option_info = s_option_infos[ pos ];
123 if (opt.getLength() > 0)
125 if (opt.equalsAsciiL(
126 option_info.m_name, option_info.m_name_length ) &&
127 (copt == '\0' || copt == option_info.m_short_option))
129 return &option_info;
132 else
134 OSL_ASSERT( copt != '\0' );
135 if (copt == option_info.m_short_option)
137 return &option_info;
141 OSL_FAIL(
142 OUStringToOString( opt, osl_getThreadTextEncoding() ).getStr() );
143 return 0;
147 static bool is_option(
148 OptionInfo const * option_info, sal_uInt32 * pIndex )
150 OSL_ASSERT( option_info != 0 );
151 if (osl_getCommandArgCount() <= *pIndex)
152 return false;
154 OUString arg;
155 osl_getCommandArg( *pIndex, &arg.pData );
156 sal_Int32 len = arg.getLength();
158 if (len < 2 || arg[ 0 ] != '-')
159 return false;
161 if (len == 2 && arg[ 1 ] == option_info->m_short_option)
163 ++(*pIndex);
164 #if OSL_DEBUG_LEVEL > 1
165 OSL_TRACE(
166 __FILE__": identified option \'%c\'", option_info->m_short_option );
167 #endif
168 return true;
170 if (arg[ 1 ] == '-' && rtl_ustr_ascii_compare(
171 arg.pData->buffer + 2, option_info->m_name ) == 0)
173 ++(*pIndex);
174 #if OSL_DEBUG_LEVEL > 1
175 OSL_TRACE( __FILE__": identified option \'%s\'", option_info->m_name );
176 #endif
177 return true;
179 return false;
183 static inline bool read_option(
184 bool * flag, OptionInfo const * option_info, sal_uInt32 * pIndex )
186 bool ret = is_option( option_info, pIndex );
187 if (ret)
188 *flag = true;
189 return ret;
193 static bool read_argument(
194 OUString * pValue, OptionInfo const * option_info, sal_uInt32 * pIndex )
196 if (is_option( option_info, pIndex ))
198 if (*pIndex < osl_getCommandArgCount())
200 osl_getCommandArg( *pIndex, &pValue->pData );
201 ++(*pIndex);
202 #if OSL_DEBUG_LEVEL > 1
203 OString cstr_val(
204 OUStringToOString( *pValue, osl_getThreadTextEncoding() ) );
205 OSL_TRACE( __FILE__": argument value: %s\n", cstr_val.getStr() );
206 #endif
207 return true;
209 --(*pIndex);
211 return false;
215 static OUString const & path_get_working_dir()
217 static OUString s_workingDir;
218 if (! s_workingDir.getLength())
219 osl_getProcessWorkingDir( &s_workingDir.pData );
220 return s_workingDir;
224 static OUString path_make_absolute_file_url( OUString const & path )
226 OUString file_url;
227 oslFileError rc = osl_getFileURLFromSystemPath(
228 path.pData, &file_url.pData );
229 if (osl_File_E_None == rc)
231 OUString abs;
232 rc = osl_getAbsoluteFileURL(
233 path_get_working_dir().pData, file_url.pData, &abs.pData );
234 if (osl_File_E_None == rc)
236 return abs;
238 else
240 throw RuntimeException(
241 "cannot make absolute: " + file_url );
244 else
246 throw RuntimeException(
247 "cannot get file url from system path: " + path );
253 using namespace ::climaker;
256 SAL_IMPLEMENT_MAIN()
258 sal_uInt32 nCount = osl_getCommandArgCount();
259 if (0 == nCount)
261 puts( s_usingText );
262 return 0;
265 int ret = 0;
266 css::uno::Reference< XComponentContext > xContext;
270 OptionInfo const * info_help =
271 get_option_info( "help" );
272 OptionInfo const * info_verbose =
273 get_option_info( "verbose" );
274 OptionInfo const * info_out =
275 get_option_info( "out" );
276 OptionInfo const * info_types =
277 get_option_info( "types" );
278 OptionInfo const * info_reference =
279 get_option_info( "reference" );
280 OptionInfo const * info_extra =
281 get_option_info( "extra" );
282 OptionInfo const * info_keyfile =
283 get_option_info( "keyfile" );
284 OptionInfo const * info_delaySign =
285 get_option_info( "delaySign" );
286 OptionInfo const * info_version =
287 get_option_info( "assembly-version" );
288 OptionInfo const * info_product =
289 get_option_info( "assembly-product" );
290 OptionInfo const * info_description =
291 get_option_info( "assembly-description" );
292 OptionInfo const * info_company =
293 get_option_info( "assembly-company" );
294 OptionInfo const * info_copyright =
295 get_option_info( "assembly-copyright" );
296 OptionInfo const * info_trademark =
297 get_option_info( "assembly-trademark" );
299 OUString output;
300 vector< OUString > mandatory_registries;
301 vector< OUString > extra_registries;
302 vector< OUString > extra_assemblies;
303 vector< OUString > explicit_types;
304 OUString version, product, description, company, copyright, trademark,
305 keyfile, delaySign;
307 OUString cmd_arg;
308 for ( sal_uInt32 nPos = 0; nPos < nCount; )
310 // options
311 if (is_option( info_help, &nPos ))
313 puts( s_usingText );
314 return 0;
316 else if (read_argument( &cmd_arg, info_types, &nPos ))
318 sal_Int32 index = 0;
321 explicit_types.push_back(
322 cmd_arg.getToken( 0, ';', index ) );
324 while (index >= 0);
326 else if (read_argument( &cmd_arg, info_extra, &nPos ))
328 extra_registries.push_back(
329 path_make_absolute_file_url( cmd_arg ) );
331 else if (read_argument( &cmd_arg, info_reference, &nPos ))
333 extra_assemblies.push_back(
334 path_make_absolute_file_url( cmd_arg ) );
336 else if (!read_option( &g_verbose, info_verbose, &nPos ) &&
337 !read_argument( &output, info_out, &nPos ) &&
338 !read_argument( &version, info_version, &nPos ) &&
339 !read_argument( &description, info_description, &nPos ) &&
340 !read_argument( &product, info_product, &nPos ) &&
341 !read_argument( &company, info_company, &nPos ) &&
342 !read_argument( &copyright, info_copyright, &nPos ) &&
343 !read_argument( &trademark, info_trademark, &nPos ) &&
344 !read_argument( &keyfile, info_keyfile, &nPos ) &&
345 !read_argument( &delaySign, info_delaySign, &nPos ))
347 osl_getCommandArg( nPos, &cmd_arg.pData );
348 ++nPos;
349 cmd_arg = cmd_arg.trim();
350 if (cmd_arg.getLength() > 0)
352 if (cmd_arg[ 0 ] == '-') // is option
354 OptionInfo const * option_info = 0;
355 if (cmd_arg.getLength() > 2 &&
356 cmd_arg[ 1 ] == '-')
358 // long option
359 option_info = get_option_info(
360 cmd_arg.copy( 2 ), '\0' );
362 else if (cmd_arg.getLength() == 2 &&
363 cmd_arg[ 1 ] != '-')
365 // short option
366 option_info = get_option_info(
367 OUString(), cmd_arg[ 1 ] );
369 if (option_info == 0)
371 throw RuntimeException("unknown option " + cmd_arg + "! Use climaker --help to print all options.");
373 else
375 OSL_FAIL( "unhandled valid option?!" );
376 if (option_info->m_has_argument)
377 ++nPos;
380 else
382 mandatory_registries.push_back(
383 path_make_absolute_file_url( cmd_arg ) );
389 // bootstrap uno
390 xContext = ::cppu::defaultBootstrap_InitialComponentContext();
391 css::uno::Reference< container::XHierarchicalNameAccess > xTDmgr(
392 xContext->getValueByName(
393 "/singletons/com.sun.star.reflection."
394 "theTypeDescriptionManager" ),
395 UNO_QUERY_THROW );
397 // The registries are consumed twice, once to insert them into the
398 // TypeDescriptionManager so that TypeEmitter can work on
399 // css.star.reflection.XTypeDescription representation, and once
400 // directly as unoidl::Provider instances to keep track which types are
401 // coming from the mandatory registries for the "no explicit types
402 // given" case (which iterates over the full TypeDescriptionManager
403 // now); a welcome clean-up would be to make TypeEmitter work on
404 // unoidl::Entity directly like the other codemakers:
405 css::uno::Reference< container::XSet > xSet( xTDmgr, UNO_QUERY_THROW );
406 rtl::Reference< unoidl::Manager > unoidlMgr(new unoidl::Manager);
407 std::vector< rtl::Reference< unoidl::Provider > > unoidlMandatoryProvs;
408 for (vector< OUString >::iterator i(extra_registries.begin());
409 i != extra_registries.end(); ++i)
411 xSet->insert(makeAny(*i));
412 unoidlMgr->addProvider(*i);
414 for (vector< OUString >::iterator i(mandatory_registries.begin());
415 i != mandatory_registries.end(); ++i)
417 xSet->insert(makeAny(*i));
418 rtl::Reference< unoidl::Provider > prov(unoidlMgr->addProvider(*i));
419 unoidlMandatoryProvs.push_back(prov);
422 if (0 == output.getLength()) // no output file specified
424 // if only one rdb has been given, then take rdb name
425 if (1 == mandatory_registries.size())
427 output = mandatory_registries[ 0 ];
428 output = output.copy( output.lastIndexOf( '/' ) +1 );
429 sal_Int32 dot = output.lastIndexOf( '.' );
430 if (dot > 0)
431 output = output.copy( 0, dot );
433 else
435 output = "cli_unotypes";
438 output = path_make_absolute_file_url( output );
439 sal_Int32 slash = output.lastIndexOf( '/' );
440 OUString sys_output_dir;
441 if (FileBase::E_None != FileBase::getSystemPathFromFileURL(
442 output.copy( 0, slash ), sys_output_dir ))
444 throw RuntimeException(
445 "cannot get system path from file url " +
446 output.copy( 0, slash ) );
448 OUString filename( output.copy( slash +1 ) );
449 sal_Int32 dot = filename.lastIndexOf( '.' );
450 OUString name( filename );
451 if (dot < 0) // has no extension
452 filename += ".dll";
453 else
454 name = name.copy( 0, dot );
455 ::System::String ^ output_dir = ustring_to_String( sys_output_dir );
456 ::System::String ^ output_file = ustring_to_String( filename );
458 //Get the key pair for making a strong name
459 StrongNameKeyPair^ kp = nullptr;
460 if (keyfile.getLength() > 0)
462 ::System::String ^ sKeyFile = ustring_to_String(keyfile);
463 try {
464 System::IO::FileStream^ fs = gcnew System::IO::FileStream(
465 sKeyFile, System::IO::FileMode::Open,
466 System::IO::FileAccess::Read, System::IO::FileShare::Read);
467 kp = gcnew StrongNameKeyPair(fs);
468 fs->Close();
470 catch (System::IO::FileNotFoundException ^ )
472 throw Exception("Could not find the keyfile. Verify the --keyfile argument!", 0);
475 else
477 if (g_verbose)
479 ::System::Console::Write(
480 "> no key file specified. Cannot create strong name!\n");
483 // setup assembly info: xxx todo set more? e.g. avoid strong versioning
484 AssemblyName ^ assembly_name = gcnew AssemblyName();
485 assembly_name->CodeBase = output_dir;
486 assembly_name->Name = gcnew ::System::String(name.getStr());
487 if (kp != nullptr)
488 assembly_name->KeyPair= kp;
490 if (version.getLength() != 0)
492 assembly_name->Version=
493 gcnew ::System::Version( ustring_to_String( version ) );
496 // app domain
497 ::System::AppDomain ^ current_appdomain =
498 ::System::AppDomain::CurrentDomain;
500 // Weird warning from this statement
501 // warning C4538: 'cli::array<Type> ^' : const/volatile qualifiers on this type are not supported
502 // Could be a compiler bug, says http://stackoverflow.com/questions/12151060/seemingly-inappropriate-compilation-warning-with-c-cli
503 #pragma warning (push)
504 #pragma warning (disable: 4538)
505 // target assembly
506 Emit::AssemblyBuilder ^ assembly_builder =
507 current_appdomain->DefineDynamicAssembly(
508 assembly_name, Emit::AssemblyBuilderAccess::Save, output_dir );
509 #pragma warning (pop)
511 if (product.getLength() != 0)
513 array< ::System::Type^>^ params = gcnew array< ::System::Type^> (1);
514 array< ::System::Object^>^args = gcnew array< ::System::Object^>(1);
515 params[ 0 ] = ::System::String::typeid;
516 args[ 0 ] = ustring_to_String( product );
517 assembly_builder->SetCustomAttribute(
518 gcnew Emit::CustomAttributeBuilder(
519 (AssemblyProductAttribute::typeid)->GetConstructor(
520 params ), args ) );
522 if (description.getLength() != 0)
524 array< ::System::Type^>^ params = gcnew array< ::System::Type^>(1);
525 array< ::System::Object^>^ args = gcnew array< ::System::Object^>(1);
526 params[ 0 ] = ::System::String::typeid;
527 args[ 0 ] = ustring_to_String( description );
528 assembly_builder->SetCustomAttribute(
529 gcnew Emit::CustomAttributeBuilder(
530 (AssemblyDescriptionAttribute::typeid)->GetConstructor(
531 params ), args ) );
533 if (company.getLength() != 0)
535 array< ::System::Type^>^ params = gcnew array< ::System::Type^>(1);
536 array< ::System::Object^>^ args = gcnew array< ::System::Object^>(1);
537 params[ 0 ] = ::System::String::typeid;
538 args[ 0 ] = ustring_to_String( company );
539 assembly_builder->SetCustomAttribute(
540 gcnew Emit::CustomAttributeBuilder(
541 (AssemblyCompanyAttribute::typeid)->GetConstructor(
542 params ), args ) );
544 if (copyright.getLength() != 0)
546 array< ::System::Type^>^ params = gcnew array< ::System::Type^>(1);
547 array< ::System::Object^>^ args = gcnew array< ::System::Object^>(1);
548 params[ 0 ] = ::System::String::typeid;
549 args[ 0 ] = ustring_to_String( copyright );
550 assembly_builder->SetCustomAttribute(
551 gcnew Emit::CustomAttributeBuilder(
552 (AssemblyCopyrightAttribute::typeid)->GetConstructor(
553 params ), args ) );
555 if (trademark.getLength() != 0)
557 array< ::System::Type^>^ params = gcnew array< ::System::Type^>(1);
558 array< ::System::Object^>^ args = gcnew array< ::System::Object^>(1);
559 params[ 0 ] = ::System::String::typeid;
560 args[ 0 ] = ustring_to_String( trademark );
561 assembly_builder->SetCustomAttribute(
562 gcnew Emit::CustomAttributeBuilder(
563 (AssemblyTrademarkAttribute::typeid)->GetConstructor(
564 params ), args ) );
567 // load extra assemblies
568 array<Assembly^>^ assemblies =
569 gcnew array<Assembly^>(extra_assemblies.size());
570 for ( size_t pos = 0; pos < extra_assemblies.size(); ++pos )
572 assemblies[ pos ] = Assembly::LoadFrom(
573 ustring_to_String( extra_assemblies[ pos ] ) );
576 // type emitter
577 TypeEmitter ^ type_emitter = gcnew TypeEmitter(
578 assembly_builder->DefineDynamicModule( output_file ), assemblies );
579 // add handler resolving assembly's types
580 ::System::ResolveEventHandler ^ type_resolver =
581 gcnew ::System::ResolveEventHandler(
582 type_emitter, &TypeEmitter::type_resolve );
583 current_appdomain->TypeResolve += type_resolver;
585 // and emit types to it
586 if (explicit_types.empty())
588 css::uno::Reference< reflection::XTypeDescriptionEnumeration > xTD_enum(
589 css::uno::Reference< reflection::XTypeDescriptionEnumerationAccess >(
590 xTDmgr, UNO_QUERY_THROW )
591 ->createTypeDescriptionEnumeration(
592 OUString() /* all IDL modules */,
593 Sequence< TypeClass >() /* all classes of types */,
594 reflection::TypeDescriptionSearchDepth_INFINITE ) );
595 while (xTD_enum->hasMoreElements())
597 css::uno::Reference< reflection::XTypeDescription > td(
598 xTD_enum->nextTypeDescription());
599 OUString name(td->getName());
600 bool emit = false;
601 for (std::vector< rtl::Reference< unoidl::Provider > >::iterator
602 i(unoidlMandatoryProvs.begin());
603 i != unoidlMandatoryProvs.end(); ++i)
605 if ((*i)->findEntity(name).is()) {
606 emit = true;
607 break;
610 if (emit) {
611 type_emitter->get_type(td);
615 else
617 for ( size_t nPos = explicit_types.size(); nPos--; )
619 type_emitter->get_type(
620 css::uno::Reference< reflection::XTypeDescription >(
621 xTDmgr->getByHierarchicalName( explicit_types[ nPos ] ),
622 UNO_QUERY_THROW ) );
625 type_emitter->~TypeEmitter();
627 if (g_verbose)
629 ::System::Console::Write(
630 "> saving assembly {0}{1}{2}...",
631 output_dir,
632 gcnew ::System::String(
633 ::System::IO::Path::DirectorySeparatorChar, 1 ),
634 output_file );
636 assembly_builder->Save( output_file );
637 if (g_verbose)
639 ::System::Console::WriteLine( "ok." );
641 current_appdomain->TypeResolve -= type_resolver;
643 catch (unoidl::NoSuchFileException & e)
645 std::cerr << "ERROR: No such file <" << e.getUri() << ">\n";
646 return EXIT_FAILURE;
648 catch (unoidl::FileFormatException & e)
650 std::cerr
651 << "ERROR: Bad format of <" << e.getUri() << ">, \""
652 << e.getDetail() << "\"\n";
653 return EXIT_FAILURE;
655 catch (Exception & exc)
657 OString msg(
658 OUStringToOString( exc.Message, osl_getThreadTextEncoding() ) );
659 fprintf(
660 stderr, "\n> error: %s\n> dying abnormally...\n", msg.getStr() );
661 ret = 1;
663 catch (::System::Exception ^ exc)
665 OString msg( OUStringToOString(
666 String_to_ustring( exc->ToString() ),
667 osl_getThreadTextEncoding() ) );
668 fprintf(
669 stderr,
670 "\n> error: .NET exception occurred: %s\n> dying abnormally...",
671 msg.getStr() );
672 ret = 1;
677 css::uno::Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
678 if (xComp.is())
679 xComp->dispose();
681 catch (Exception & exc)
683 OString msg(
684 OUStringToOString( exc.Message, osl_getThreadTextEncoding() ) );
685 fprintf(
686 stderr,
687 "\n> error disposing component context: %s\n"
688 "> dying abnormally...\n",
689 msg.getStr() );
690 ret = 1;
693 return ret;
696 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */