This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH v3 3/4] libcc1: Add 'set compile-gcc'
- From: Jan Kratochvil <jan dot kratochvil at redhat dot com>
- To: gcc-patches at gcc dot gnu dot org
- Cc: Phil Muldoon <pmuldoon at redhat dot com>
- Date: Sun, 24 May 2015 20:55:44 +0200
- Subject: [PATCH v3 3/4] libcc1: Add 'set compile-gcc'
- Authentication-results: sourceware.org; auth=none
- References: <20150524185528 dot 31662 dot 76343 dot stgit at host1 dot jankratochvil dot net>
Hi,
GDB currently searches for /usr/bin/ARCH-OS-gcc and chooses one but one cannot
override which one. GDB would provide new option 'set compile-gcc'.
Jan
include/ChangeLog
2015-05-24 Jan Kratochvil <jan.kratochvil@redhat.com>
* gcc-interface.h (enum gcc_base_api_version): Update comment for
GCC_FE_VERSION_1.
(struct gcc_base_vtable): Rename set_arguments to set_arguments_v0.
Add set_arguments, set_triplet_regexp and set_driver_filename.
libcc1/ChangeLog
2015-05-24 Jan Kratochvil <jan.kratochvil@redhat.com>
* libcc1.cc (libcc1): Add class compiler with field compilerp, class
compiler_triplet_regexp and class compiler_driver_filename.
(libcc1::libcc1): Initialize compilerp.
(libcc1::~libcc1): Delete compilerp.
(libcc1::compiler::find, libcc1::compiler_triplet_regexp::find)
(libcc1::compiler_driver_filename::find): New methods.
(libcc1_set_arguments): Remove parameter triplet_regexp.
(libcc1_set_triplet_regexp, libcc1_set_driver_filename)
(libcc1_set_arguments_v0): New functions.
(vtable): Use libcc1_set_arguments_v0, add libcc1_set_arguments,
libcc1_set_triplet_regexp and libcc1_set_driver_filename.
---
include/gcc-interface.h | 61 ++++++++++++++++-----
libcc1/libcc1.cc | 139 ++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 172 insertions(+), 28 deletions(-)
diff --git a/include/gcc-interface.h b/include/gcc-interface.h
index 0b409d0..1c66c3b 100644
--- a/include/gcc-interface.h
+++ b/include/gcc-interface.h
@@ -46,7 +46,9 @@ enum gcc_base_api_version
{
GCC_FE_VERSION_0 = 0,
- /* Deprecated method compile_v0. Added method set_verbose and compile. */
+ /* Deprecated methods set_arguments_v0 and compile_v0. Added methods
+ set_arguments, set_triplet_regexp, set_driver_filename, set_verbose and
+ compile. */
GCC_FE_VERSION_1 = 1,
};
@@ -67,20 +69,12 @@ struct gcc_base_vtable
unsigned int version;
- /* Set the compiler's command-line options for the next compilation.
- TRIPLET_REGEXP is a regular expression that is used to match the
- configury triplet prefix to the compiler.
- The arguments are copied by GCC. ARGV need not be
- NULL-terminated. The arguments must be set separately for each
- compilation; that is, after a compile is requested, the
- previously-set arguments cannot be reused.
-
- This returns NULL on success. On failure, returns a malloc()d
- error message. The caller is responsible for freeing it. */
+ /* Deprecated GCC_FE_VERSION_0 variant of the GCC_FE_VERSION_1
+ methods set_triplet_regexp and set_arguments. */
- char *(*set_arguments) (struct gcc_base_context *self,
- const char *triplet_regexp,
- int argc, char **argv);
+ char *(*set_arguments_v0) (struct gcc_base_context *self,
+ const char *triplet_regexp,
+ int argc, char **argv);
/* Set the file name of the program to compile. The string is
copied by the method implementation, but the caller must
@@ -125,6 +119,45 @@ struct gcc_base_vtable
int /* bool */ (*compile) (struct gcc_base_context *self,
const char *filename);
+
+ /* Set the compiler's command-line options for the next compilation.
+ The arguments are copied by GCC. ARGV need not be
+ NULL-terminated. The arguments must be set separately for each
+ compilation; that is, after a compile is requested, the
+ previously-set arguments cannot be reused.
+
+ This returns NULL on success. On failure, returns a malloc()d
+ error message. The caller is responsible for freeing it.
+
+ This method is only available since GCC_FE_VERSION_1. */
+
+ char *(*set_arguments) (struct gcc_base_context *self,
+ int argc, char **argv);
+
+ /* Set TRIPLET_REGEXP as a regular expression that is used to match
+ the configury triplet prefix to the compiler. Calling this method
+ overrides possible previous call of itself or set_driver_filename.
+
+ This returns NULL on success. On failure, returns a malloc()d
+ error message. The caller is responsible for freeing it.
+
+ This method is only available since GCC_FE_VERSION_1. */
+
+ char *(*set_triplet_regexp) (struct gcc_base_context *self,
+ const char *triplet_regexp);
+
+ /* DRIVER_FILENAME should be filename of the gcc compiler driver
+ program. It will be searched in PATH components like
+ TRIPLET_REGEXP. Calling this method overrides possible previous
+ call of itself or set_triplet_regexp.
+
+ This returns NULL on success. On failure, returns a malloc()d
+ error message. The caller is responsible for freeing it.
+
+ This method is only available since GCC_FE_VERSION_1. */
+
+ char *(*set_driver_filename) (struct gcc_base_context *self,
+ const char *driver_filename);
};
/* The GCC object. */
diff --git a/libcc1/libcc1.cc b/libcc1/libcc1.cc
index 6fda5b9..cf26928 100644
--- a/libcc1/libcc1.cc
+++ b/libcc1/libcc1.cc
@@ -70,6 +70,53 @@ struct libcc1 : public gcc_c_context
/* Non-zero as an equivalent to gcc driver option "-v". */
bool verbose;
+
+ /* Compiler to set by set_triplet_regexp or set_driver_filename. */
+ class compiler
+ {
+ protected:
+ libcc1 *self_;
+ public:
+ compiler (libcc1 *self) : self_ (self)
+ {
+ }
+ virtual char *find (std::string &compiler) const;
+ virtual ~compiler ()
+ {
+ }
+ } *compilerp;
+
+ /* Compiler to set by set_triplet_regexp. */
+ class compiler_triplet_regexp : public compiler
+ {
+ private:
+ std::string triplet_regexp_;
+ public:
+ virtual char *find (std::string &compiler) const;
+ compiler_triplet_regexp (libcc1 *self, std::string triplet_regexp)
+ : compiler (self), triplet_regexp_ (triplet_regexp)
+ {
+ }
+ virtual ~compiler_triplet_regexp ()
+ {
+ }
+ };
+
+ /* Compiler to set by set_driver_filename. */
+ class compiler_driver_filename : public compiler
+ {
+ private:
+ std::string driver_filename_;
+ public:
+ virtual char *find (std::string &compiler) const;
+ compiler_driver_filename (libcc1 *self, std::string driver_filename)
+ : compiler (self), driver_filename_ (driver_filename)
+ {
+ }
+ virtual ~compiler_driver_filename ()
+ {
+ }
+ };
};
// A local subclass of connection that holds a back-pointer to the
@@ -102,7 +149,8 @@ libcc1::libcc1 (const gcc_base_vtable *v,
print_datum (NULL),
args (),
source_file (),
- verbose (false)
+ verbose (false),
+ compilerp (new libcc1::compiler (this))
{
base.ops = v;
c_ops = cv;
@@ -111,6 +159,7 @@ libcc1::libcc1 (const gcc_base_vtable *v,
libcc1::~libcc1 ()
{
delete connection;
+ delete compilerp;
}
@@ -319,20 +368,21 @@ libcc1_set_verbose (struct gcc_base_context *s, int /* bool */ verbose)
self->verbose = verbose != 0;
}
-static char *
-libcc1_set_arguments (struct gcc_base_context *s,
- const char *triplet_regexp,
- int argc, char **argv)
+char *
+libcc1::compiler::find (std::string &compiler ATTRIBUTE_UNUSED) const
{
- libcc1 *self = (libcc1 *) s;
- regex_t triplet;
- int code;
+ return xstrdup (_("Compiler has not been specified"));
+}
- std::string rx = make_regexp (triplet_regexp, COMPILER_NAME);
- // Simulate fnotice by fprintf.
- if (self->verbose)
+char *
+libcc1::compiler_triplet_regexp::find (std::string &compiler) const
+{
+ std::string rx = make_regexp (triplet_regexp_.c_str (), COMPILER_NAME);
+ if (self_->verbose)
fprintf (stderr, _("searching for compiler matching regex %s\n"),
rx.c_str());
+ regex_t triplet;
+ int code;
code = regcomp (&triplet, rx.c_str (), REG_EXTENDED | REG_NOSUB);
if (code != 0)
{
@@ -348,7 +398,6 @@ libcc1_set_arguments (struct gcc_base_context *s,
(char *) NULL);
}
- std::string compiler;
if (!find_compiler (triplet, &compiler))
{
regfree (&triplet);
@@ -358,8 +407,32 @@ libcc1_set_arguments (struct gcc_base_context *s,
(char *) NULL);
}
regfree (&triplet);
- if (self->verbose)
+ if (self_->verbose)
fprintf (stderr, _("found compiler %s\n"), compiler.c_str());
+ return NULL;
+}
+
+char *
+libcc1::compiler_driver_filename::find (std::string &compiler) const
+{
+ // Simulate fnotice by fprintf.
+ if (self_->verbose)
+ fprintf (stderr, _("using explicit compiler filename %s\n"),
+ driver_filename_.c_str());
+ compiler = driver_filename_;
+ return NULL;
+}
+
+static char *
+libcc1_set_arguments (struct gcc_base_context *s,
+ int argc, char **argv)
+{
+ libcc1 *self = (libcc1 *) s;
+
+ std::string compiler;
+ char *errmsg = self->compilerp->find (compiler);
+ if (errmsg != NULL)
+ return errmsg;
self->args.push_back (compiler);
@@ -369,6 +442,41 @@ libcc1_set_arguments (struct gcc_base_context *s,
return NULL;
}
+static char *
+libcc1_set_triplet_regexp (struct gcc_base_context *s,
+ const char *triplet_regexp)
+{
+ libcc1 *self = (libcc1 *) s;
+
+ delete self->compilerp;
+ self->compilerp = new libcc1::compiler_triplet_regexp (self, triplet_regexp);
+ return NULL;
+}
+
+static char *
+libcc1_set_driver_filename (struct gcc_base_context *s,
+ const char *driver_filename)
+{
+ libcc1 *self = (libcc1 *) s;
+
+ delete self->compilerp;
+ self->compilerp = new libcc1::compiler_driver_filename (self,
+ driver_filename);
+ return NULL;
+}
+
+static char *
+libcc1_set_arguments_v0 (struct gcc_base_context *s,
+ const char *triplet_regexp,
+ int argc, char **argv)
+{
+ char *errmsg = libcc1_set_triplet_regexp (s, triplet_regexp);
+ if (errmsg != NULL)
+ return errmsg;
+
+ return libcc1_set_arguments (s, argc, argv);
+}
+
static void
libcc1_set_source_file (struct gcc_base_context *s,
const char *file)
@@ -531,13 +639,16 @@ libcc1_destroy (struct gcc_base_context *s)
static const struct gcc_base_vtable vtable =
{
GCC_FE_VERSION_1,
- libcc1_set_arguments,
+ libcc1_set_arguments_v0,
libcc1_set_source_file,
libcc1_set_print_callback,
libcc1_compile_v0,
libcc1_destroy,
libcc1_set_verbose,
libcc1_compile,
+ libcc1_set_arguments,
+ libcc1_set_triplet_regexp,
+ libcc1_set_driver_filename,
};
extern "C" gcc_c_fe_context_function gcc_c_fe_context;