1 //####COPYRIGHTBEGIN####
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
5 // Copyright (C) 2003 John Dallaway
7 // This program is part of the eCos host tools.
9 // This program is free software; you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by the Free
11 // Software Foundation; either version 2 of the License, or (at your option)
14 // This program is distributed in the hope that it will be useful, but WITHOUT
15 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 // You should have received a copy of the GNU General Public License along with
20 // this program; if not, write to the Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 // ----------------------------------------------------------------------------
25 //####COPYRIGHTEND####
26 //==========================================================================
30 // The implementation of each ecosconfig command
32 //==========================================================================
33 //==========================================================================
34 //#####DESCRIPTIONBEGIN####
39 //####DESCRIPTIONEND####
40 //==========================================================================
43 #include <direct.h> /* for getcwd() */
45 #include <unistd.h> /* for getcwd() */
49 #include <sys/param.h> /* for MAXPATHLEN */
50 #include <sys/cygwin.h> /* for cygwin_conv_to_win32_path() */
53 #include "cdl_exec.hxx"
55 // ----------------------------------------------------------------------------
56 bool cdl_exec::quiet = false;
57 bool cdl_exec::verbose = false;
58 bool cdl_exec::ignore_errors = false;
59 bool cdl_exec::no_updates = false;
60 bool cdl_exec::debug_level_set = false;
61 int cdl_exec::debug_level = 0;
63 cdl_exec::cdl_exec (const std::string repository_arg, const std::string savefile_arg,
64 const std::string install_arg, bool no_resolve_arg)
65 : repository(repository_arg),
66 savefile(savefile_arg),
67 install_prefix(install_arg),
68 no_resolve(no_resolve_arg),
74 // The inference callback does not actually do anything at present.
75 // In future it may be useful for diagnostic purposes.
76 CdlTransactionBody::set_inference_callback_fn (&inference_callback);
78 // Automatic inference is always disabled. The inference engine
79 // only gets invoked explicitly, after a suitable transaction callback
80 // has been invoked. The problem here is that the transaction callback
81 // has to report changes made by the inference engine but there is
82 // no way of distinguishing between inferred values that come out of
83 // savefiles and inferred values determined by the inference engine.
84 CdlTransactionBody::disable_automatic_inference ();
88 cdl_exec::set_quiet_mode(bool new_val)
94 cdl_exec::set_verbose_mode(bool new_val)
97 CdlPackagesDatabaseBody::set_verbose(new_val);
101 cdl_exec::set_ignore_errors_mode(bool new_val)
103 ignore_errors = new_val;
107 cdl_exec::set_no_updates_mode(bool new_val)
109 no_updates = new_val;
113 cdl_exec::set_debug_level(int new_level)
115 debug_level_set = true;
116 debug_level = new_level;
119 // ----------------------------------------------------------------------------
121 cdl_exec::init(bool load_config)
123 pkgdata = CdlPackagesDatabaseBody::make(repository, &diagnostic_handler, &diagnostic_handler);
124 interp = CdlInterpreterBody::make();
126 config = CdlConfigurationBody::load (savefile, pkgdata, interp, &diagnostic_handler, &diagnostic_handler);
130 // ----------------------------------------------------------------------------
132 cdl_exec::delete_cdl_data ()
148 // ----------------------------------------------------------------------------
149 bool cdl_exec::cmd_new (const std::string cdl_hardware,
150 const std::string cdl_template /* = "default" */,
151 const std::string cdl_version /* = "" */)
157 config = CdlConfigurationBody::make ("eCos", pkgdata, interp);
159 // The hardware and template should be loaded in a single transaction.
160 // Validating the target name etc. can be left to libcdl.
161 CdlLocalTransaction transact(config);
162 config->set_hardware(transact.get(), resolve_hardware_alias(cdl_hardware), &diagnostic_handler, &diagnostic_handler);
163 config->set_template(transact.get(), cdl_template, cdl_version, &diagnostic_handler, &diagnostic_handler);
167 if (debug_level_set) {
168 this->update_debug_level();
171 // Unless inference has been suppressed, make sure that the
172 // inference engine gets invoked and that its results get
175 CdlTransactionBody::set_callback_fn(&transaction_callback);
176 config->resolve_all_conflicts();
179 // Now report any conflicts which the inference engine could not report.
182 // A savefile should be generated/updated even if there are conflicts.
183 // Otherwise the user does not have a chance to edit the savefile
186 config->save (savefile);
188 if (ignore_errors || (0 == config->get_all_conflicts().size())) {
191 } catch (CdlStringException exception) {
192 exception_handler (exception);
194 exception_handler ();
201 // ----------------------------------------------------------------------------
203 cdl_exec::cmd_target (const std::string cdl_target)
208 config->set_hardware (resolve_hardware_alias (cdl_target), &diagnostic_handler, &diagnostic_handler);
209 if (debug_level_set) {
210 this->update_debug_level();
213 CdlTransactionBody::set_callback_fn(&transaction_callback);
214 config->resolve_all_conflicts();
218 config->save (savefile);
220 if (ignore_errors || (0 == config->get_all_conflicts().size())) {
223 } catch (CdlStringException exception) {
224 exception_handler (exception);
226 exception_handler ();
233 // ----------------------------------------------------------------------------
235 cdl_exec::cmd_template (const std::string cdl_template, const std::string cdl_version /* = "" */)
240 config->set_template (cdl_template, cdl_version, &diagnostic_handler, &diagnostic_handler);
241 if (debug_level_set) {
242 this->update_debug_level();
245 CdlTransactionBody::set_callback_fn(&transaction_callback);
246 config->resolve_all_conflicts();
250 config->save (savefile);
252 if (ignore_errors || (0 == config->get_all_conflicts().size())) {
255 } catch (CdlStringException exception) {
256 exception_handler (exception);
258 exception_handler ();
265 // ----------------------------------------------------------------------------
267 cdl_exec::cmd_export (const std::string cdl_savefile)
272 if (debug_level_set) {
273 this->update_debug_level();
276 CdlTransactionBody::set_callback_fn(&transaction_callback);
277 config->resolve_all_conflicts();
280 // Exporting to another file should only happen if the
281 // configuration is conflict-free. This is different from
282 // updating the savefile.
283 if (ignore_errors || (0 == config->get_all_conflicts().size())) {
285 config->save (cdl_savefile, /* minimal = */ true);
289 } catch (CdlStringException exception) {
290 exception_handler (exception);
292 exception_handler ();
299 // ----------------------------------------------------------------------------
301 cdl_exec::cmd_import (const std::string cdl_savefile)
306 config->add(cdl_savefile, &diagnostic_handler, &diagnostic_handler);
307 if (debug_level_set) {
308 this->update_debug_level();
311 CdlTransactionBody::set_callback_fn(&transaction_callback);
312 config->resolve_all_conflicts();
316 config->save (savefile);
318 if (ignore_errors || (0 == config->get_all_conflicts().size())) {
321 } catch (CdlStringException exception) {
322 exception_handler (exception);
324 exception_handler ();
331 // ----------------------------------------------------------------------------
333 cdl_exec::cmd_add (const std::vector<std::string> cdl_packages)
338 for (unsigned int n = 0; n < cdl_packages.size (); n++) {
339 config->load_package (resolve_package_alias (cdl_packages [n]), "", &diagnostic_handler, &diagnostic_handler);
341 if (debug_level_set) {
342 this->update_debug_level();
345 CdlTransactionBody::set_callback_fn(&transaction_callback);
346 config->resolve_all_conflicts();
350 config->save (savefile);
352 if (ignore_errors || (0 == config->get_all_conflicts().size())) {
355 } catch (CdlStringException exception) {
356 exception_handler (exception);
358 exception_handler ();
365 // ----------------------------------------------------------------------------
367 cdl_exec::cmd_remove (const std::vector<std::string> cdl_packages)
373 for (n = 0; n < cdl_packages.size (); n++) {
374 if (! config->lookup (resolve_package_alias (cdl_packages [n]))) {
375 throw CdlStringException ("Unknown package " + cdl_packages [n]);
378 for (n = 0; n < cdl_packages.size (); n++) {
379 config->unload_package (resolve_package_alias (cdl_packages [n]));
381 if (debug_level_set) {
382 this->update_debug_level();
385 CdlTransactionBody::set_callback_fn(&transaction_callback);
386 config->resolve_all_conflicts();
390 config->save (savefile);
392 if (ignore_errors || (0 == config->get_all_conflicts().size())) {
395 } catch (CdlStringException exception) {
396 exception_handler (exception);
398 exception_handler ();
405 // ----------------------------------------------------------------------------
407 cdl_exec::cmd_version (const std::string cdl_version, const std::vector<std::string> cdl_packages)
412 for (unsigned int n = 0; n < cdl_packages.size (); n++) {
413 config->change_package_version(resolve_package_alias (cdl_packages [n]), cdl_version,
414 &diagnostic_handler, &diagnostic_handler, true);
416 if (debug_level_set) {
417 this->update_debug_level();
420 CdlTransactionBody::set_callback_fn(&transaction_callback);
421 config->resolve_all_conflicts();
425 config->save (savefile);
427 if (ignore_errors || (0 == config->get_all_conflicts().size())) {
430 } catch (CdlStringException exception) {
431 exception_handler (exception);
433 exception_handler ();
440 // ----------------------------------------------------------------------------
442 cdl_exec::cmd_tree ()
447 if (debug_level_set) {
448 this->update_debug_level();
451 CdlTransactionBody::set_callback_fn(&transaction_callback);
452 config->resolve_all_conflicts();
456 config->save (savefile);
458 // A build tree should only be generated if there are no conflicts,
459 // and suppressed if -n is given.
463 else if (ignore_errors || (0 == config->get_all_conflicts().size())) {
465 char cwd [_MAX_PATH + 1];
467 char cwd [PATH_MAX + 1];
469 getcwd (cwd, sizeof cwd);
471 char cwd_win32 [MAXPATHLEN + 1];
472 cygwin_conv_to_win32_path (cwd, cwd_win32);
473 generate_build_tree (config, cwd_win32, install_prefix);
475 generate_build_tree (config, cwd, install_prefix);
477 config->generate_config_headers (install_prefix.empty () ? "install/include/pkgconf" : install_prefix + "/include/pkgconf");
481 strcpy(buf, "mount.exe -f -t -u x: /ecos-x");
482 //printf("Cwd_win32: %s\n", cwd_win32);
484 if ( cwd_win32[1] == ':' )
486 buf[19] = tolower(cwd_win32[0]);
487 buf[28] = tolower(cwd_win32[0]);
491 //printf("Repository: %s\n", repository.c_str());
493 if ( repository[1] == ':' )
495 buf[19] = tolower(repository[0]);
496 buf[28] = tolower(repository[0]);
499 if ( !install_prefix.empty() )
501 //printf("Install prefix: %s\n", install_prefix.c_str());
502 if ( install_prefix[1] == ':' )
504 buf[19] = tolower(install_prefix[0]);
505 buf[28] = tolower(install_prefix[0]);
511 printf("\nUnable to generate build tree, this configuration still contains conflicts.\n");
512 printf("Either resolve the conflicts or use --ignore-errors\n");
514 } catch (CdlStringException exception) {
515 exception_handler (exception);
517 exception_handler ();
524 // ----------------------------------------------------------------------------
526 cdl_exec::cmd_list ()
532 // list the installed packages
533 std::vector<std::string> packages = pkgdata->get_packages ();
534 std::sort (packages.begin (), packages.end ());
535 for (unsigned int package = 0; package < packages.size (); package++) {
536 const std::vector<std::string> & aliases = pkgdata->get_package_aliases (packages [package]);
537 printf ("Package %s (%s):\n aliases:", packages [package].c_str (), aliases [0].c_str ());
538 for (unsigned int alias = 1; alias < aliases.size (); alias++) {
539 printf (" %s", aliases [alias].c_str ());
541 const std::vector<std::string> & versions = pkgdata->get_package_versions (packages [package]);
542 printf ("\n versions:");
543 for (unsigned int version = 0; version < versions.size (); version++) {
544 printf (" %s", versions [version].c_str ());
549 // list the available targets
550 std::vector<std::string> targets = pkgdata->get_targets ();
551 std::sort (targets.begin (), targets.end ());
552 for (unsigned int target = 0; target < targets.size (); target++) {
553 const std::vector<std::string> & aliases = pkgdata->get_target_aliases (targets [target]);
554 printf ("Target %s (%s):\n aliases:", targets [target].c_str (), aliases [0].c_str ());
555 for (unsigned int alias = 1; alias < aliases.size (); alias++) {
556 printf (" %s", aliases [alias].c_str ());
561 // list the available templates
562 std::vector<std::string> templates = pkgdata->get_templates ();
563 std::sort (templates.begin (), templates.end ());
564 for (unsigned int templ = 0; templ < templates.size (); templ++) {
565 const std::vector<std::string> & versions = pkgdata->get_template_versions (templates [templ]);
566 printf ("Template %s:\n versions:", templates [templ].c_str ());
567 for (unsigned int version = 0; version < versions.size (); version++) {
568 printf (" %s", versions [version].c_str ());
574 } catch (CdlStringException exception) {
575 exception_handler (exception);
577 exception_handler ();
584 // ----------------------------------------------------------------------------
586 cdl_exec::cmd_check ()
593 // check() should never invoke the inference engine. The user
594 // wants to determine the current status, which should not
596 // However, updating the savefile is worthwhile because it
597 // will now contain more accurate information about the state.
598 // Enabling/disabling debugs is allowed for now because that
599 // is unlikely to introduce conflicts.
600 if (debug_level_set) {
601 this->update_debug_level();
604 config->save (savefile);
607 // report current target and template
608 printf ("Target: %s\n", config->get_hardware ().c_str ());
609 printf ("Template: %s\n", config->get_template ().c_str ());
610 std::vector<std::string> template_packages = pkgdata->get_template_packages (config->get_template ());
611 const std::vector<std::string> & hardware_packages = pkgdata->get_target_packages (config->get_hardware ());
612 for (n = 0; n < hardware_packages.size (); n++) {
613 template_packages.push_back (hardware_packages [n]);
616 // report loaded packages not in the templates
617 const std::vector<CdlLoadable> & loadables = config->get_loadables ();
618 std::vector<std::string> added_packages;
619 std::vector<CdlLoadable>::const_iterator loadable_i;
620 for (loadable_i = loadables.begin (); loadable_i != loadables.end (); loadable_i++) {
621 const CdlNode & node = dynamic_cast<CdlNode> (* loadable_i);
622 if (template_packages.end () == std::find (template_packages.begin (), template_packages.end (), node->get_name ())) {
623 added_packages.push_back (node->get_name ());
626 if (added_packages.size ()) {
629 for (n = 0; n < added_packages.size (); n++) {
630 printf (" %s\n", added_packages [n].c_str ());
633 // report template packages not in the configuration
634 std::vector<std::string> removed_packages;
635 for (n = 0; n < template_packages.size (); n++) {
636 if (! config->lookup (template_packages [n])) {
637 removed_packages.push_back (template_packages [n]);
640 if (removed_packages.size ()) {
641 printf ("Removed:\n");
643 for (n = 0; n < removed_packages.size (); n++) {
644 printf (" %s\n", removed_packages [n].c_str ());
647 // report packages of non-default version
648 std::vector<CdlValuable> version_packages;
649 for (loadable_i = loadables.begin (); loadable_i != loadables.end (); loadable_i++) {
650 const CdlValuable & valuable = dynamic_cast<CdlValuable> (* loadable_i);
651 if (pkgdata->get_package_versions (valuable->get_name ()) [0] != valuable->get_value ()) {
652 version_packages.push_back (valuable);
655 if (version_packages.size ()) {
656 printf ("Version(s):\n");
658 for (n = 0; n < version_packages.size (); n++) {
659 printf (" %s %s\n", version_packages [n]->get_name ().c_str (), version_packages [n]->get_value ().c_str ());
663 const std::list<CdlConflict> & conflicts = config->get_all_conflicts ();
664 if (conflicts.size ()) {
665 printf ("%u conflict(s):\n", conflicts.size ());
667 printf ("No conflicts\n");
672 } catch (CdlStringException exception) {
673 exception_handler (exception);
675 exception_handler ();
682 // ----------------------------------------------------------------------------
684 cdl_exec::cmd_resolve ()
690 if (debug_level_set) {
691 this->update_debug_level();
693 CdlTransactionBody::set_callback_fn(&transaction_callback);
694 config->resolve_all_conflicts ();
697 config->save (savefile);
699 if (ignore_errors || (0 == config->get_all_conflicts().size())) {
702 } catch (CdlStringException exception) {
703 exception_handler (exception);
705 exception_handler ();
712 // ----------------------------------------------------------------------------
713 // The inference callback. This could give some useful diagnostics, or it
714 // could do useful things when running in some interactive mode. In batch
715 // mode it should not do anything.
717 CdlInferenceCallbackResult
718 cdl_exec::inference_callback (CdlTransaction transaction)
720 return CdlInferenceCallbackResult_Continue;
723 // ----------------------------------------------------------------------------
724 // Output a message with indentation after newlines.
726 dump_string(unsigned int indent, const std::string& str)
728 bool newline_pending = false;
730 for (i = 0; i < str.size(); i++) {
731 if (newline_pending) {
733 if ('\n' != str[i]) {
734 for (j = 0; j < indent; j++) {
738 newline_pending = false;
740 if ('\n' == str[i]) {
741 newline_pending = true;
746 if (newline_pending) {
747 putchar('\n'); // But not the indentation.
751 // ----------------------------------------------------------------------------
752 // The transaction callback. This should report any changes that have been
753 // made to the configuration. The amount of output depends on the verbosity
754 // level selected by the user.
756 // 1) quiet - no output at all
757 // 2) default - list updates done by the inference engine.
758 // 3) verbose - this does not currently add anything.
760 // There is no reporting of new or resolved conflicts. Resolved
761 // conflicts are probably of no interest in batch mode. New conflicts
762 // will be handled by report_conflicts(). There is also no information
763 // given about active state changes, although arguably there should be
764 // especially in the case of containers.
767 cdl_exec::transaction_callback(const CdlTransactionCallback& callback_data)
774 for (i = 0; i < callback_data.value_changes.size(); i++) {
775 CdlValuable valuable = callback_data.value_changes[i];
776 if (CdlValueSource_Inferred == valuable->get_source()) {
777 CdlEvalContext context(0, valuable, 0);
778 CdlSimpleValue simple_val;
779 CdlSimpleValue::eval_valuable(context, valuable, simple_val);
780 std::string msg = std::string("U ") + valuable->get_name() + ", new inferred value ";
781 std::string value = simple_val.get_value();
793 // ----------------------------------------------------------------------------
794 // Report the remaining conflicts in the configuration. These indicate
795 // problems that the user should fix before going further with the
796 // configuration, e.g. before generating a build tree.
798 // Quiet verbosity level has no effect on this, but at the verbose level
799 // it is a good idea to look for a possible solution to the conflict.
803 cdl_exec::report_conflicts()
805 const std::list<CdlConflict>& all_conflicts = config->get_all_conflicts();
806 std::list<CdlConflict>::const_iterator conf_i;
807 for (conf_i = all_conflicts.begin(); conf_i != all_conflicts.end(); conf_i++) {
808 CdlNode node = (*conf_i)->get_node();
810 std::string msg = std::string("C ") + node->get_name() + ", " + (*conf_i)->get_explanation() + "\n";
813 if (verbose && (*conf_i)->resolution_implemented()) {
814 // See if there is a possible solution to this conflict.
815 // This involves creating a transaction, invoking the
816 // inference engine, and cancelling the transaction
817 // (thus making sure that nothing actually changes).
819 // NOTE: at some stage libcdl may keep track of solutions
820 // globally. However, although it will know when a solution
821 // becomes invalid it will not necessarily try to resolve
822 // all global conflicts after every change, so attempting
823 // to do this in a transaction may still be necessary.
824 CdlTransaction transact = CdlTransactionBody::make(config);
825 transact->resolve(*conf_i);
826 if ((*conf_i)->has_known_solution()) {
827 std::string soln_msg = " Possible solution:\n";
828 const std::vector<std::pair<CdlValuable, CdlValue> > & soln = (*conf_i)->get_solution();
830 for (i = 0; i < soln.size(); i++) {
831 CdlValuable valuable = soln[i].first;
832 soln_msg += valuable->get_name();
834 switch(valuable->get_flavor()) {
835 case CdlValueFlavor_Bool :
836 if (!soln[i].second.is_enabled()) {
837 soln_msg += "0 (disabled)";
839 soln_msg += "1 (enabled)";
842 case CdlValueFlavor_Data:
843 soln_msg += soln[i].second.get_value();
845 case CdlValueFlavor_BoolData:
846 if (!soln[i].second.is_enabled()) {
847 soln_msg += "0 " + soln[i].second.get_value();
849 soln_msg += "1 " + soln[i].second.get_value();
852 // An option with flavor none cannot be involved
855 soln_msg += "<internal error>";
862 // FIXME: currently this member only works for nested sub-transactions.
863 if (transact->user_confirmation_required()) {
864 msg += "This change affects previous user settings.\n";
867 dump_string(4, soln_msg);
875 // ----------------------------------------------------------------------------
877 cdl_exec::diagnostic_handler (std::string message)
879 printf ("%s\n", message.c_str ());
882 void cdl_exec::exception_handler (CdlStringException exception) {
883 printf ("%s\n", exception.get_message ().c_str ());
887 cdl_exec::exception_handler ()
889 printf ("Unknown error\n");
893 // ----------------------------------------------------------------------------
895 cdl_exec::resolve_package_alias (const std::string alias)
897 std::string package = alias;
899 if (! pkgdata->is_known_package (alias)) { // if the alias is not a package name
900 const std::vector<std::string> & packages = pkgdata->get_packages (); // get packages
901 for (unsigned int n = 0; n < packages.size (); n++) { // for each package
902 const std::vector<std::string> & aliases = pkgdata->get_package_aliases (packages [n]); // get package aliases
903 if (aliases.end () != std::find (aliases.begin (), aliases.end (), alias)) { // if alias is found
904 package = packages [n]; // note the package
913 cdl_exec::resolve_hardware_alias (const std::string alias)
915 std::string target = alias;
917 if (! pkgdata->is_known_target (alias)) { // if the alias is not a target name
918 const std::vector<std::string> & targets = pkgdata->get_targets (); // get targets
919 for (unsigned int n = 0; n < targets.size (); n++) { // for each target
920 const std::vector<std::string> & aliases = pkgdata->get_target_aliases (targets [n]); // get target aliases
921 if (aliases.end () != std::find (aliases.begin (), aliases.end (), alias)) { // if alias is found
922 target = targets [n]; // note the target
930 // ----------------------------------------------------------------------------
931 // Enable or disable debugging in a configuration.
933 cdl_exec::update_debug_level()
935 CdlNode node = config->lookup("CYGPKG_INFRA_DEBUG");
936 CdlValuable valuable = 0;
938 valuable = dynamic_cast<CdlValuable>(node);
941 throw CdlStringException("Cannot enable or disable debugging, the infrastructure package is absent");
944 if (debug_level > 0) {
945 valuable->enable(CdlValueSource_User);
947 valuable->disable(CdlValueSource_User);