Add --stats option to print runtime and memory usage statistics.
[deliverable/binutils-gdb.git] / gold / options.cc
index 2f83e10c339f59ce2322cdb961a2db17a2202455..89eecf66bb8635b01e732535ed2c8808f8bda6b4 100644 (file)
@@ -1,8 +1,32 @@
 // options.c -- handle command line options for gold
 
-#include <iostream>
+// Copyright 2006, 2007 Free Software Foundation, Inc.
+// Written by Ian Lance Taylor <iant@google.com>.
+
+// This file is part of gold.
+
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+// MA 02110-1301, USA.
 
 #include "gold.h"
+
+#include <iostream>
+#include <sys/stat.h>
+#include "filenames.h"
+#include "libiberty.h"
+
 #include "options.h"
 
 namespace gold
@@ -196,6 +220,61 @@ help(int, char**, char*, gold::Command_line*)
   return 0;
 }
 
+// Report version information.
+
+int
+version(int, char**, char* opt, gold::Command_line*)
+{
+  gold::print_version(opt[0] == 'v' && opt[1] == '\0');
+  gold::gold_exit(true);
+  return 0;
+}
+
+// If the default sysroot is relocatable, try relocating it based on
+// the prefix FROM.
+
+char*
+get_relative_sysroot(const char* from)
+{
+  char* path = make_relative_prefix(gold::program_name, from,
+                                   TARGET_SYSTEM_ROOT);
+  if (path != NULL)
+    {
+      struct stat s;
+      if (::stat(path, &s) == 0 && S_ISDIR(s.st_mode))
+       return path;
+      free(path);
+    }
+
+  return NULL;
+}
+
+// Return the default sysroot.  This is set by the --with-sysroot
+// option to configure.
+
+std::string
+get_default_sysroot()
+{
+  const char* sysroot = TARGET_SYSTEM_ROOT;
+  if (*sysroot == '\0')
+    return "";
+
+  if (TARGET_SYSTEM_ROOT_RELOCATABLE)
+    {
+      char* path = get_relative_sysroot (BINDIR);
+      if (path == NULL)
+       path = get_relative_sysroot (TOOLBINDIR);
+      if (path != NULL)
+       {
+         std::string ret = path;
+         free(path);
+         return ret;
+       }
+    }
+
+  return sysroot;
+}
+
 } // End anonymous namespace.
 
 namespace gold
@@ -255,6 +334,10 @@ options::Command_line_options::options[] =
   GENERAL_ARG('R', "rpath", N_("Add DIR to runtime search path"),
               N_("-R DIR, -rpath DIR"), ONE_DASH,
               &General_options::add_to_rpath),
+  GENERAL_NOARG('s', "strip-all", N_("Strip all symbols"), NULL,
+               TWO_DASHES, &General_options::set_strip_all),
+  GENERAL_NOARG('S', "strip-debug", N_("Strip debugging information"), NULL,
+               TWO_DASHES, &General_options::set_strip_debug),
   GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"),
                 NULL, TWO_DASHES, &General_options::set_create_eh_frame_hdr),
   GENERAL_ARG('\0', "rpath-link",
@@ -265,6 +348,10 @@ options::Command_line_options::options[] =
                NULL, ONE_DASH, &General_options::set_shared),
   GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
                NULL, ONE_DASH, &General_options::set_static),
+  GENERAL_NOARG('\0', "stats", N_("Print resource usage statistics"),
+               NULL, TWO_DASHES, &General_options::set_stats),
+  GENERAL_ARG('\0', "sysroot", N_("Set target system root directory"),
+             N_("--sysroot DIR"), TWO_DASHES, &General_options::set_sysroot),
   POSDEP_NOARG('\0', "as-needed",
               N_("Only set DT_NEEDED for dynamic libs if used"),
               NULL, TWO_DASHES, &Position_dependent_options::set_as_needed),
@@ -280,7 +367,9 @@ options::Command_line_options::options[] =
                NULL, TWO_DASHES,
                &Position_dependent_options::clear_whole_archive),
   SPECIAL('\0', "help", N_("Report usage information"), NULL,
-         TWO_DASHES, &help)
+         TWO_DASHES, &help),
+  SPECIAL('v', "version", N_("Report version information"), NULL,
+         TWO_DASHES, &version)
 };
 
 const int options::Command_line_options::options_size =
@@ -295,11 +384,14 @@ General_options::General_options()
     optimization_level_(0),
     output_file_name_("a.out"),
     is_relocatable_(false),
+    strip_(STRIP_NONE),
     create_eh_frame_hdr_(false),
     rpath_(),
     rpath_link_(),
     is_shared_(false),
-    is_static_(false)
+    is_static_(false),
+    print_stats_(false),
+    sysroot_()
 {
 }
 
@@ -312,6 +404,66 @@ Position_dependent_options::Position_dependent_options()
 {
 }
 
+// Add the sysroot, if any, to the search paths.
+
+void
+General_options::add_sysroot()
+{
+  if (this->sysroot_.empty())
+    {
+      this->sysroot_ = get_default_sysroot();
+      if (this->sysroot_.empty())
+       return;
+    }
+
+  const char* sysroot = this->sysroot_.c_str();
+  char* canonical_sysroot = lrealpath(sysroot);
+
+  for (Dir_list::iterator p = this->search_path_.begin();
+       p != this->search_path_.end();
+       ++p)
+    p->add_sysroot(sysroot, canonical_sysroot);
+
+  free(canonical_sysroot);
+}
+
+// Search_directory methods.
+
+// This is called if we have a sysroot.  Apply the sysroot if
+// appropriate.  Record whether the directory is in the sysroot.
+
+void
+Search_directory::add_sysroot(const char* sysroot,
+                             const char* canonical_sysroot)
+{
+  gold_assert(*sysroot != '\0');
+  if (this->put_in_sysroot_)
+    {
+      if (!IS_DIR_SEPARATOR(this->name_[0])
+         && !IS_DIR_SEPARATOR(sysroot[strlen(sysroot) - 1]))
+       this->name_ = '/' + this->name_;
+      this->name_ = sysroot + this->name_;
+      this->is_in_sysroot_ = true;
+    }
+  else
+    {
+      // Check whether this entry is in the sysroot.  To do this
+      // correctly, we need to use canonical names.  Otherwise we will
+      // get confused by the ../../.. paths that gcc tends to use.
+      char* canonical_name = lrealpath(this->name_.c_str());
+      int canonical_name_len = strlen(canonical_name);
+      int canonical_sysroot_len = strlen(canonical_sysroot);
+      if (canonical_name_len > canonical_sysroot_len
+         && IS_DIR_SEPARATOR(canonical_name[canonical_sysroot_len]))
+       {
+         canonical_name[canonical_sysroot_len] = '\0';
+         if (FILENAME_CMP(canonical_name, canonical_sysroot) == 0)
+           this->is_in_sysroot_ = true;
+       }
+      free(canonical_name);
+    }
+}
+
 // Input_arguments methods.
 
 // Add a file to the list.
@@ -392,8 +544,12 @@ Command_line::process(int argc, char** argv)
       char first = opt[0];
       int skiparg = 0;
       char* arg = strchr(opt, '=');
+      bool argument_with_equals = arg != NULL;
       if (arg != NULL)
-       *arg = '\0';
+       {
+         *arg = '\0';
+         ++arg;
+       }
       else if (i + 1 < argc)
        {
          arg = argv[i + 1];
@@ -416,6 +572,8 @@ Command_line::process(int argc, char** argv)
                {
                  if (!options[j].takes_argument())
                    {
+                     if (argument_with_equals)
+                       this->usage(_("unexpected argument"), argv[i]);
                      arg = NULL;
                      skiparg = 0;
                    }
@@ -495,10 +653,32 @@ Command_line::process(int argc, char** argv)
     }
 
   // FIXME: We should only do this when configured in native mode.
-  this->options_.add_to_search_path("/lib");
-  this->options_.add_to_search_path("/usr/lib");
+  this->options_.add_to_search_path_with_sysroot("/lib");
+  this->options_.add_to_search_path_with_sysroot("/usr/lib");
+
+  this->options_.add_sysroot();
+
+  // Ensure options don't contradict each other and are otherwise kosher.
+  this->normalize_options();
 }
 
+// Ensure options don't contradict each other and are otherwise kosher.
+
+void
+Command_line::normalize_options()
+{
+  // If the user specifies both -s and -r, convert the -s as -S.
+  // -r requires us to keep externally visible symbols!
+  if (this->options_.strip_all() && this->options_.is_relocatable())
+    {
+      // Clears the strip_all() status, replacing it with strip_debug().
+      this->options_.set_strip_debug();
+    }
+
+  // FIXME: we can/should be doing a lot more sanity checking here.
+}
+
+
 // Apply a command line option.
 
 void
@@ -530,7 +710,7 @@ Command_line::apply_option(const options::One_option& opt,
 void
 Command_line::add_file(const char* name, bool is_lib)
 {
-  Input_file_argument file(name, is_lib, this->position_options_);
+  Input_file_argument file(name, is_lib, "", this->position_options_);
   this->inputs_.add_file(file);
 }
 
This page took 0.025142 seconds and 4 git commands to generate.