main.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  5. * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "Namespaces.h"
  10. #include <AK/Debug.h>
  11. #include <AK/LexicalPath.h>
  12. #include <LibCore/ArgsParser.h>
  13. #include <LibCore/File.h>
  14. #include <LibIDL/IDLParser.h>
  15. #include <LibIDL/Types.h>
  16. extern Vector<StringView> s_header_search_paths;
  17. namespace IDL {
  18. void generate_constructor_header(IDL::Interface const&, StringBuilder&);
  19. void generate_constructor_implementation(IDL::Interface const&, StringBuilder&);
  20. void generate_prototype_header(IDL::Interface const&, StringBuilder&);
  21. void generate_prototype_implementation(IDL::Interface const&, StringBuilder&);
  22. void generate_iterator_prototype_header(IDL::Interface const&, StringBuilder&);
  23. void generate_iterator_prototype_implementation(IDL::Interface const&, StringBuilder&);
  24. void generate_global_mixin_header(IDL::Interface const&, StringBuilder&);
  25. void generate_global_mixin_implementation(IDL::Interface const&, StringBuilder&);
  26. }
  27. ErrorOr<int> serenity_main(Main::Arguments arguments)
  28. {
  29. Core::ArgsParser args_parser;
  30. StringView path;
  31. StringView import_base_path;
  32. StringView output_path = "-"sv;
  33. StringView depfile_path;
  34. StringView depfile_target;
  35. bool constructor_header_mode = false;
  36. bool constructor_implementation_mode = false;
  37. bool prototype_header_mode = false;
  38. bool prototype_implementation_mode = false;
  39. bool iterator_prototype_header_mode = false;
  40. bool iterator_prototype_implementation_mode = false;
  41. bool global_mixin_header_mode = false;
  42. bool global_mixin_implementation_mode = false;
  43. args_parser.add_option(constructor_header_mode, "Generate the constructor .h file", "constructor-header", 'C');
  44. args_parser.add_option(constructor_implementation_mode, "Generate the constructor .cpp file", "constructor-implementation", 'O');
  45. args_parser.add_option(prototype_header_mode, "Generate the prototype .h file", "prototype-header", 'P');
  46. args_parser.add_option(prototype_implementation_mode, "Generate the prototype .cpp file", "prototype-implementation", 'R');
  47. args_parser.add_option(iterator_prototype_header_mode, "Generate the iterator prototype .h file", "iterator-prototype-header", 0);
  48. args_parser.add_option(iterator_prototype_implementation_mode, "Generate the iterator prototype .cpp file", "iterator-prototype-implementation", 0);
  49. args_parser.add_option(global_mixin_header_mode, "Generate the global object mixin .h file", "global-mixin-header", 0);
  50. args_parser.add_option(global_mixin_implementation_mode, "Generate the global object mixin .cpp file", "global-mixin-implementation", 0);
  51. args_parser.add_option(Core::ArgsParser::Option {
  52. .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
  53. .help_string = "Add a header search path passed to the compiler",
  54. .long_name = "header-include-path",
  55. .short_name = 'i',
  56. .value_name = "path",
  57. .accept_value = [&](StringView s) {
  58. s_header_search_paths.append(s);
  59. return true;
  60. },
  61. });
  62. args_parser.add_option(output_path, "Path to output generated file into", "output-path", 'o', "output-path");
  63. args_parser.add_option(depfile_path, "Path to write dependency file to", "depfile", 'd', "depfile-path");
  64. args_parser.add_option(depfile_target, "Name of target in the depfile (default: output path)", "depfile-target", 't', "target");
  65. args_parser.add_positional_argument(path, "IDL file", "idl-file");
  66. args_parser.add_positional_argument(import_base_path, "Import base path", "import-base-path", Core::ArgsParser::Required::No);
  67. args_parser.parse(arguments);
  68. auto file = TRY(Core::File::open(path, Core::File::OpenMode::Read));
  69. LexicalPath lexical_path(path);
  70. auto& namespace_ = lexical_path.parts_view().at(lexical_path.parts_view().size() - 2);
  71. auto data = TRY(file->read_until_eof());
  72. if (import_base_path.is_null())
  73. import_base_path = lexical_path.dirname();
  74. auto output_file = TRY(Core::File::open_file_or_standard_stream(output_path, Core::File::OpenMode::Write));
  75. IDL::Parser parser(path, data, import_base_path);
  76. auto& interface = parser.parse();
  77. if (IDL::libweb_interface_namespaces.span().contains_slow(namespace_)) {
  78. StringBuilder builder;
  79. builder.append(namespace_);
  80. builder.append("::"sv);
  81. builder.append(interface.name);
  82. interface.fully_qualified_name = builder.to_deprecated_string();
  83. } else {
  84. interface.fully_qualified_name = interface.name;
  85. }
  86. if constexpr (BINDINGS_GENERATOR_DEBUG) {
  87. dbgln("Attributes:");
  88. for (auto& attribute : interface.attributes) {
  89. dbgln(" {}{}{}{} {}",
  90. attribute.inherit ? "inherit " : "",
  91. attribute.readonly ? "readonly " : "",
  92. attribute.type->name(),
  93. attribute.type->is_nullable() ? "?" : "",
  94. attribute.name);
  95. }
  96. dbgln("Functions:");
  97. for (auto& function : interface.functions) {
  98. dbgln(" {}{} {}",
  99. function.return_type->name(),
  100. function.return_type->is_nullable() ? "?" : "",
  101. function.name);
  102. for (auto& parameter : function.parameters) {
  103. dbgln(" {}{} {}",
  104. parameter.type->name(),
  105. parameter.type->is_nullable() ? "?" : "",
  106. parameter.name);
  107. }
  108. }
  109. dbgln("Static Functions:");
  110. for (auto& function : interface.static_functions) {
  111. dbgln(" static {}{} {}",
  112. function.return_type->name(),
  113. function.return_type->is_nullable() ? "?" : "",
  114. function.name);
  115. for (auto& parameter : function.parameters) {
  116. dbgln(" {}{} {}",
  117. parameter.type->name(),
  118. parameter.type->is_nullable() ? "?" : "",
  119. parameter.name);
  120. }
  121. }
  122. }
  123. StringBuilder output_builder;
  124. if (constructor_header_mode)
  125. IDL::generate_constructor_header(interface, output_builder);
  126. if (constructor_implementation_mode)
  127. IDL::generate_constructor_implementation(interface, output_builder);
  128. if (prototype_header_mode)
  129. IDL::generate_prototype_header(interface, output_builder);
  130. if (prototype_implementation_mode)
  131. IDL::generate_prototype_implementation(interface, output_builder);
  132. if (iterator_prototype_header_mode)
  133. IDL::generate_iterator_prototype_header(interface, output_builder);
  134. if (iterator_prototype_implementation_mode)
  135. IDL::generate_iterator_prototype_implementation(interface, output_builder);
  136. if (global_mixin_header_mode)
  137. IDL::generate_global_mixin_header(interface, output_builder);
  138. if (global_mixin_implementation_mode)
  139. IDL::generate_global_mixin_implementation(interface, output_builder);
  140. TRY(output_file->write_until_depleted(output_builder.string_view().bytes()));
  141. if (!depfile_path.is_null()) {
  142. auto depfile = TRY(Core::File::open_file_or_standard_stream(depfile_path, Core::File::OpenMode::Write));
  143. StringBuilder depfile_builder;
  144. depfile_builder.append(depfile_target.is_null() ? output_path : depfile_target);
  145. depfile_builder.append(':');
  146. for (auto const& path : parser.imported_files()) {
  147. depfile_builder.append(" \\\n "sv);
  148. depfile_builder.append(path);
  149. }
  150. depfile_builder.append('\n');
  151. TRY(depfile->write_until_depleted(depfile_builder.string_view().bytes()));
  152. }
  153. return 0;
  154. }