Add language option 77/163577/2
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 12 Dec 2017 07:12:05 +0000 (16:12 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 12 Dec 2017 07:12:05 +0000 (16:12 +0900)
+-----------------------------------------------------------+
| Usage:                                                    |
|  tidlc [OPTION...]                                        |
|                                                           |
| Options:                                                  |
|  -p, --proxy                 Generate proxy code          |
|  -s, --stub                  Generate stub code           |
|                                                           |
| Additional Options:                                       |
| -l, --language=LANGUAGE      Select generating language   |
| -i, --input=INPUT           a tidl interface file.        |
| -o, --output=OUTPUT         The generated interface file. |
+-----------------------------------------------------------+

Change-Id: I245d41636ada7409270054d971c9b97050391b94
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
idlc/CMakeLists.txt
idlc/main.cc
packaging/tidl.spec

index 7628085..8b56ec4 100644 (file)
@@ -1,9 +1,16 @@
 CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
 PROJECT(TIDLC CXX)
 
+INCLUDE(FindPkgConfig)
+PKG_CHECK_MODULES(LIBPKGS REQUIRED glib-2.0)
+
 FIND_PACKAGE(BISON REQUIRED)
 FIND_PACKAGE(FLEX REQUIRED)
 
+FOREACH(flag ${LIBPKGS_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
 SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wno-unused-function -Wno-sign-compare")
 SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wl,-zdefs" )
 SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden")
@@ -36,5 +43,6 @@ bison_target(TIDLC ${CMAKE_CURRENT_SOURCE_DIR}/tidlc.yy ${CMAKE_CURRENT_SOURCE_D
 SET(TILDC_deps ${FLEX_TIDLC_OUTPUTS})
 
 ADD_EXECUTABLE(tidlc ${BISON_TIDLC_OUTPUTS} ${FLEX_TIDLC_OUTPUTS} ${SOURCES})
+TARGET_LINK_LIBRARIES(tidlc ${LIBPKGS_LDFLAGS})
 INSTALL(TARGETS tidlc DESTINATION bin)
 
index 8a96c6b..04ff390 100644 (file)
  * limitations under the License.
  */
 
+#include <glib.h>
+
 #include <iostream>
 #include <cstring>
+#include <memory>
 
 #include "idlc/parser.h"
 #include "idlc/proxy_gen.h"
 
 namespace {
 
-enum class Option {
-  NONE = 0,
-  PROXY,
-  STUB
+class Options {
+ public:
+  ~Options() = default;
+
+  static std::unique_ptr<Options> Parse(int argc, char** argv);
+  bool IsProxy() const { return isProxy_; }
+  std::string GetLanguage() const { return language_; }
+  std::string GetInput() const { return input_; }
+  std::string GetOutput() const { return output_; }
+
+ private:
+  enum Cmd {
+    CMD_PROXY,
+    CMD_STUB,
+    CMD_MAX
+  };
+
+  enum Opt {
+    OPT_LANGUAGE,
+    OPT_INPUT,
+    OPT_OUTPUT,
+    OPT_MAX
+  };
+
+  void PrintUsage();
+
+ private:
+  bool isProxy_;
+  std::string language_;
+  std::string input_;
+  std::string output_;
+  std::string help_;
 };
 
-void PrintUsage() {
-  std::cout << "usage: tidlc <option> <input_file> <output_file>"
-  << std::endl
-  << "options:" << std::endl
-  << "    -p generate proxy code" << std::endl
-  << "    -s generate stub code" << std::endl;
+void Options::PrintUsage() {
+  std::cerr << help_ << std::endl;
 }
 
-}  // namespace
+std::unique_ptr<Options> Options::Parse(int argc, char** argv) {
+  gpointer cmd[CMD_MAX] = { 0, };
+  static GOptionEntry cmdEntries[] = {
+    { "proxy", 'p', 0, G_OPTION_ARG_NONE, &cmd[CMD_PROXY],
+       "Generate proxy code", NULL },
+    { "stub", 's', 0, G_OPTION_ARG_NONE, &cmd[CMD_STUB],
+      "Generate stub code", NULL },
+    { NULL }
+  };
+  gpointer opt[OPT_MAX] = { 0, };
+  static GOptionEntry optEntries[] = {
+    { "language", 'l', 0, G_OPTION_ARG_STRING, &opt[OPT_LANGUAGE],
+      "Select generating language (C, C++, C#).", "LANGUAGE" },
+    { "input", 'i', 0, G_OPTION_ARG_STRING, &opt[OPT_INPUT],
+      "A tidl interface file.", "INPUT" },
+    { "output", 'o', 0, G_OPTION_ARG_STRING, &opt[OPT_OUTPUT],
+      "The generated interface file.", "OUTPUT" },
+    { NULL }
+  };
 
-int main(int argc, char** argv) {
-  if (argc != 4) {
-    ::PrintUsage();
-    exit(1);
+  GOptionContext* context = g_option_context_new(NULL);
+  if (!context) {
+    std::cerr << "Failed to create context" << std::endl;
+    return std::unique_ptr<Options>(nullptr);
+  }
+  g_option_context_add_main_entries(context, cmdEntries, NULL);
+
+  GOptionGroup* optGroup = g_option_group_new("option", "Additional Options:",
+      "Additional options", NULL, NULL);
+  if (!optGroup) {
+    std::cerr << "Failed to create option group" << std::endl;
+    g_option_context_free(context);
+    return std::unique_ptr<Options>(nullptr);
   }
+  g_option_group_add_entries(optGroup, optEntries);
+  g_option_context_add_group(context, optGroup);
 
-  ::Option opt = ::Option::NONE;
+  GError* err = NULL;
+  if (!g_option_context_parse(context, &argc, &argv, &err)) {
+    std::cerr << argv[0] << ": " << err->message << std::endl;
+    g_option_context_free(context);
+    g_clear_error(&err);
+    return std::unique_ptr<Options>(nullptr);
+  }
 
-  if (strcmp(argv[1], "-p") == 0)
-    opt = ::Option::PROXY;
+  gchar* help = g_option_context_get_help(context, TRUE, NULL);
 
-  if (strcmp(argv[1], "-s") == 0)
-    opt = ::Option::STUB;
+  std::unique_ptr<Options> options(new Options());
+  options->help_ = help;
 
-  if (opt == ::Option::NONE) {
-    PrintUsage();
-    exit(1);
+  g_free(help);
+  g_option_context_free(context);
+
+  if ((!cmd[CMD_PROXY] && !cmd[CMD_STUB]) ||
+       !opt[OPT_LANGUAGE] || !opt[OPT_INPUT] ||
+       !opt[OPT_OUTPUT]) {
+    options->PrintUsage();
+    return std::unique_ptr<Options>(nullptr);
   }
 
+  options->isProxy_ = cmd[CMD_PROXY] ? true : false;
+  options->language_ = static_cast<char*>(opt[OPT_LANGUAGE]);
+  options->input_ = static_cast<char*>(opt[OPT_INPUT]);
+  options->output_ = static_cast<char*>(opt[OPT_OUTPUT]);
+
+  return options;
+}
+
+}  // namespace
+
+int main(int argc, char** argv) {
+  std::unique_ptr<::Options> options = ::Options::Parse(argc, argv);
+  if (!options)
+    exit(1);
+
   tidl::Parser ps;
-  std::string path(argv[2]);
+  std::string path(options->GetInput());
 
   if (!ps.ParseFromFile(path))
     exit(1);
 
-  if (opt == ::Option::PROXY) {
+  if (options->IsProxy()) {
     tidl::ProxyGen proxy(ps.GetDoc());
-    proxy.Run(argv[3]);
+    proxy.Run(options->GetOutput());
   } else {
     tidl::StubGen stub(ps.GetDoc());
-    stub.Run(argv[3]);
+    stub.Run(options->GetOutput());
   }
 
   return 0;
index 07c4871..3bb8584 100644 (file)
@@ -15,6 +15,7 @@ BuildRequires:  cmake
 BuildRequires:  flex
 BuildRequires:  bison
 BuildRequires:  gtest-devel
+BuildRequires:  pkgconfig(glib-2.0)
 
 %description
 Tizen Interface Definition Language