/* * Copyright 2019 by its authors. See AUTHORS. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include #include #include #include #ifdef HAVE_CONFIG_H # include #endif #include namespace efl_mono_msbuild_gen { /// Program options. struct options_type { std::string out_file; std::string out_assembly; std::string target_type; std::string platform; std::vector cs_files; std::vector defines; std::vector references; std::vector lib_paths; }; efl::eina::log_domain domain("efl_mono_msbuild_gen"); namespace { void _print_version() { std::cerr << "EFL Mono MSBuild Generator (EFL " << PACKAGE_VERSION << ")" << std::endl; } static void _usage(const char *progname) { std::cerr << progname << " [options] [files]" << std::endl << "Options:" << std::endl << " -o, --out-file The output file name. [default: .eo.hh]" << std::endl << " -v, --version Print the version." << std::endl << " -h, --help Print this help." << std::endl; exit(EXIT_FAILURE); } static void _assert_not_dup(std::string option, std::string value) { if (value != "") { EINA_CXX_DOM_LOG_ERR(efl_mono_msbuild_gen::domain) << "Option -" + option + " already set (" + value + ")"; } } static bool opts_check(efl_mono_msbuild_gen::options_type const& opts) { if (opts.out_file.empty()) { EINA_CXX_DOM_LOG_ERR(efl_mono_msbuild_gen::domain) << "Nowhere to generate?" << std::endl; } else return true; // valid. return false; } static efl_mono_msbuild_gen::options_type opts_get(int argc, char **argv) { efl_mono_msbuild_gen::options_type opts; const struct option long_options[] = { { "out-file", required_argument, 0, 'o' }, { "assembly", required_argument, 0, 'a' }, { "target-type", required_argument, 0, 't' }, { "platform", required_argument, 0, 'p' }, { "define", required_argument, 0, 'd' }, { "reference", required_argument, 0, 'r' }, { "lib-path", required_argument, 0, 'l' }, { "version", no_argument, 0, 'v' }, { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 } }; const char* options = "o:a:t:p:d:r:l:vh"; int c, idx; while ( (c = getopt_long(argc, argv, options, long_options, &idx)) != -1) { if (c == 'o') { _assert_not_dup("o", opts.out_file); opts.out_file = optarg; } else if (c == 'a') { _assert_not_dup("a", opts.out_assembly); opts.out_assembly = optarg; } else if (c == 't') { _assert_not_dup("t", opts.target_type); opts.target_type = optarg; } else if (c == 'p') { _assert_not_dup("p", opts.platform); opts.platform = optarg; } else if (c == 'd') { opts.defines.push_back(optarg); } else if (c == 'r') { opts.references.push_back(optarg); } else if (c == 'l') { opts.lib_paths.push_back(optarg); } else if (c == 'h') { _usage(argv[0]); } else if (c == 'v') { _print_version(); if (argc == 2) exit(EXIT_SUCCESS); } } for (int i = optind; i < argc; ++i) { opts.cs_files.push_back(argv[i]); } if (!efl_mono_msbuild_gen::opts_check(opts)) { _usage(argv[0]); assert(false && "Wrong options passed in command-line"); } return opts; } static void run(options_type const& opts) { std::ofstream os (opts.out_file.c_str()); if(!os.is_open()) { throw std::runtime_error(std::string{"Couldn't open output file "} + opts.out_file); } os << "\n"; os << " \n"; if (opts.target_type == "library") os << " Library\n"; //os << " " << opts.version << "\n"; // TODO if (!opts.defines.empty()) { os << " $(DefineConstants)"; for (auto const& def : opts.defines) { os << ";" << def; } os << "\n"; } os << " \n"; if (!opts.cs_files.empty()) { os << " \n"; for (auto fname : opts.cs_files) { std::replace(fname.begin(), fname.end(), '/', '\\'); os << " \n"; } os << " \n"; } if (!opts.references.empty()) { os << " \n"; for (auto libname : opts.references) { os << " \n"; } os << " \n"; } if (!opts.lib_paths.empty()) { os << " \n"; for (auto libpath : opts.lib_paths) { std::replace(libpath.begin(), libpath.end(), '/', '\\'); os << " \n"; } os << " \n"; } os << " \n"; os << " \n"; os << " \n"; os << "\n"; } } // anonymous namespace } // namespace efl_mono_msbuild_gen int main(int argc, char **argv) { try { efl::eina::eina_init eina_init; efl_mono_msbuild_gen::options_type opts = efl_mono_msbuild_gen::opts_get(argc, argv); efl_mono_msbuild_gen::run(opts); } catch(std::exception const& e) { EINA_CXX_DOM_LOG_ERR(efl_mono_msbuild_gen::domain) << "EFL Mono MSBuild generation for the following reason: " << e.what() << std::endl; return -1; } return 0; }