From 81b48dc3a7480c3df87d4293722b5a3d5f0de2c2 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 1 Feb 2011 10:53:31 +0200 Subject: Pass profile paths to plugin; handle profile options --- odb/makefile | 2 +- odb/odb.cxx | 73 ++++++++------------------------------------------------- odb/plugin.cxx | 31 +++++++++++++++++++++++- odb/profile.cxx | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ odb/profile.hxx | 32 +++++++++++++++++++++++++ 5 files changed, 135 insertions(+), 65 deletions(-) create mode 100644 odb/profile.cxx create mode 100644 odb/profile.hxx (limited to 'odb') diff --git a/odb/makefile b/odb/makefile index 4cbf071..d73f007 100644 --- a/odb/makefile +++ b/odb/makefile @@ -68,7 +68,7 @@ cxx_dtun := odb.cxx # Common units. # -cxx_ctun := database.cxx +cxx_ctun := database.cxx profile.cxx # Options file. # diff --git a/odb/odb.cxx b/odb/odb.cxx index d8576d2..d4d719d 100644 --- a/odb/odb.cxx +++ b/odb/odb.cxx @@ -25,7 +25,6 @@ # include // _O_TEXT #endif -#include #include #include #include // size_t @@ -38,6 +37,7 @@ #include #include +#include #ifdef HAVE_CONFIG_H # include @@ -115,25 +115,9 @@ encode_plugin_option (string const& k, string const& v); // profile_failure, process_failure and invalid_path exceptions. Name // is the program name (argv[0]) for diagnostics. // -struct profile_failure {}; - static paths profile_paths (strings const& args, char const* name); -// Search for the profile options file. -// -struct profile_data -{ - profile_data (paths const& p, char const* n): search_paths (p), name (n) {} - - paths const& search_paths; - set loaded; - char const* name; -}; - -static string -profile_search (char const* profile, void* arg); - static char const* const db_macro[] = { "-DODB_DATABASE_MYSQL", @@ -483,7 +467,15 @@ main (int argc, char* argv[]) args.push_back (encode_plugin_option (k, v)); } - // Reserve space for and remember the position of the --svc-file + // Pass profile search paths (svc-path option). + // + for (paths::const_iterator i (prof_paths.begin ()); + i != prof_paths.end (); ++i) + { + args.push_back (encode_plugin_option ("svc-path", i->string ())); + } + + // Reserve space for and remember the position of the svc-file // option. // size_t svc_file_pos (args.size ()); @@ -824,51 +816,6 @@ profile_paths (strings const& sargs, char const* name) return r; } -static string -profile_search (char const* prof, void* arg) -{ - profile_data* pd (static_cast (arg)); - paths const& ps (pd->search_paths); - - path p (prof), odb ("odb"), r; - p.normalize (); // Convert '/' to the canonical path separator form. - p += ".options"; - - struct stat info; - paths::const_iterator i (ps.begin ()), end (ps.end ()); - - for (; i != end; ++i) - { - // First check in the search directory itself and then try the odb/ - // subdirectory. - // - r = *i / p; - - // Just check that the file exist without checking for permissions, etc. - // - if (stat (r.string ().c_str (), &info) == 0 && S_ISREG (info.st_mode)) - break; - - r = *i / odb / p; - - if (stat (r.string ().c_str (), &info) == 0 && S_ISREG (info.st_mode)) - break; - } - - if (i == end) - { - cerr << pd->name << ": error: unable to locate options file for profile '" - << prof << "'" << endl; - throw profile_failure (); - } - - if (pd->loaded.find (r) != pd->loaded.end ()) - return string (); - - pd->loaded.insert (r); - return r.string (); -} - // // Path manipulation. // diff --git a/odb/plugin.cxx b/odb/plugin.cxx index 5b9efc1..a611dfe 100644 --- a/odb/plugin.cxx +++ b/odb/plugin.cxx @@ -11,9 +11,12 @@ #include // std::strcpy #include +#include + #include #include #include +#include #include #include #include @@ -22,8 +25,14 @@ using namespace std; using namespace semantics; +using cutl::fs::path; +using cutl::fs::invalid_path; + +typedef vector paths; + int plugin_is_GPL_compatible; auto_ptr options_; +paths profile_paths_; // A prefix of the _cpp_file struct. This struct is not part of the // public interface so we have to resort to this technique (based on @@ -154,6 +163,14 @@ plugin_init (plugin_name_args* plugin_info, plugin_gcc_version*) { plugin_argument& a (plugin_info->argv[i]); + // Handle service options. + // + if (strcmp (a.key, "svc-path") == 0) + { + profile_paths_.push_back (path (a.value)); + continue; + } + string opt (strlen (a.key) > 1 ? "--" : "-"); opt += a.key; @@ -168,7 +185,19 @@ plugin_init (plugin_name_args* plugin_info, plugin_gcc_version*) } int argc (static_cast (argv.size ())); - cli::argv_file_scanner scan (argc, &argv[0], "--options-file"); + + profile_data pd (profile_paths_, "odb plugin"); + cli::argv_file_scanner::option_info oi[3]; + oi[0].option = "--options-file"; + oi[0].search_func = 0; + oi[1].option = "-p"; + oi[1].search_func = &profile_search; + oi[1].arg = &pd; + oi[2].option = "--profile"; + oi[2].search_func = &profile_search; + oi[2].arg = &pd; + + cli::argv_file_scanner scan (argc, &argv[0], oi, 3); options_.reset ( new options (scan, cli::unknown_mode::fail, cli::unknown_mode::fail)); diff --git a/odb/profile.cxx b/odb/profile.cxx new file mode 100644 index 0000000..393c953 --- /dev/null +++ b/odb/profile.cxx @@ -0,0 +1,62 @@ +// file : odb/profile.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v3; see accompanying LICENSE file + +#include // stat +#include // stat +#include // stat + +#include + +#include + +using namespace std; + +string +profile_search (char const* prof, void* arg) +{ + typedef profile_data::path path; + typedef profile_data::paths paths; + + profile_data* pd (static_cast (arg)); + paths const& ps (pd->search_paths); + + path p (prof), odb ("odb"), r; + p.normalize (); // Convert '/' to the canonical path separator form. + p += ".options"; + + struct stat info; + paths::const_iterator i (ps.begin ()), end (ps.end ()); + + for (; i != end; ++i) + { + // First check in the search directory itself and then try the odb/ + // subdirectory. + // + r = *i / p; + + // Just check that the file exist without checking for permissions, etc. + // + if (stat (r.string ().c_str (), &info) == 0 && S_ISREG (info.st_mode)) + break; + + r = *i / odb / p; + + if (stat (r.string ().c_str (), &info) == 0 && S_ISREG (info.st_mode)) + break; + } + + if (i == end) + { + cerr << pd->name << ": error: unable to locate options file for profile '" + << prof << "'" << endl; + throw profile_failure (); + } + + if (pd->loaded.find (r) != pd->loaded.end ()) + return string (); + + pd->loaded.insert (r); + return r.string (); +} diff --git a/odb/profile.hxx b/odb/profile.hxx new file mode 100644 index 0000000..e3c5035 --- /dev/null +++ b/odb/profile.hxx @@ -0,0 +1,32 @@ +// file : odb/profile.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v3; see accompanying LICENSE file + +#ifndef ODB_PROFILE_HXX +#define ODB_PROFILE_HXX + +#include +#include +#include + +#include + +struct profile_data +{ + typedef cutl::fs::path path; + typedef std::vector paths; + + profile_data (paths const& p, char const* n): search_paths (p), name (n) {} + + paths const& search_paths; + std::set loaded; + char const* name; +}; + +struct profile_failure {}; + +std::string +profile_search (char const* profile, void* arg); + +#endif // ODB_PROFILE_HXX -- cgit v1.1