unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Using guile as an extension language for GNU  make
@ 2011-09-18  0:10 Paul Smith
  2011-09-18 12:10 ` Ludovic Courtès
  2011-09-18 15:30 ` Thien-Thi Nguyen
  0 siblings, 2 replies; 21+ messages in thread
From: Paul Smith @ 2011-09-18  0:10 UTC (permalink / raw)
  To: guile-user

Hi all.

I've been experimenting with using Guile as an extension language to GNU
make (optionally built-in of course).  I wonder if experts here can give
me their thoughts and opinions.

The "interesting" thing about this integration is that GNU make is
essentially a big string parser, so most of the internal representations
of things are kept as strings.  So I need to be converting from C
strings into Guile SCM objects (that's easy) and from Guile SCM objects
into strings... this is slightly more complex because make strings have
no quotes.  What I've been doing is checking if the converted object has
quotes and if so, removing them.  However I don't really know if this is
the best method or not.

What I've done so far:

      * Modified GNU make's main to be invoked from scm_boot_guile(), if
        Guile is enabled.

      * Created a new GNU make function, $(guile ...), where the
        argument is passed to Guile for expansion and the result is
        turned into a string and used as the result; the code looks like
        this:
                func_guile (char *o, char **argv, const char *funcname UNUSED)
                {
                  if (argv[0] && argv[0][0] != '\0')
                    {
                      char *str = scm_to_locale_string (scm_object_to_string (scm_c_eval_string (argv[0]),
                                                                              SCM_UNDEFINED));
                      char *s = str;
                      unsigned int l = strlen (s);
                
                      if (s[0] == '"' && s[l-1] == '"')
                        {
                          s[l-1] = '\0';
                          ++s;
                          l -= 2;
                        }
                
                      o = variable_buffer_output (o, s, l);
                      free (str);
                    }
                
                  return o;
                }

      * Created two new functions and registered them with Guile:
        (make-expand <string>) which takes a string argument and expands
        it as a make expression, so it can be something like
        (make-expand "$(VAR)") for example to get the value of the make
        variable VAR.  And (make-eval <string>) which takes a string
        argument and evaluates it as a makefile snippet; this is
        essentially the same as running (make-expand "$(eval <string>)")
        just shorter to type.  This lets you define make constructs like
        rules and variables from within the Guile interpreter.  The code
        looks like this:
                SCM
                guile_eval_wrapper (SCM obj)
                {
                  char *str = scm_to_locale_string (scm_object_to_string (obj, SCM_UNDEFINED));
                  /* We want to avoid the surrounding double-quotes if present. */
                  char *s = str;
                  unsigned int l = strlen (s);
                
                  if (s[0] == '"' && s[l-1] == '"')
                    {
                      s[l-1] = '\0';
                      ++s;
                    }
                
                  DB (DB_BASIC, (_("guile: Evaluating '%s'\n"), s));
                  eval_buffer (s);
                
                  free (str);
                  return scm_from_locale_string ("");
                }
                
                SCM
                guile_expand_wrapper (SCM obj)
                {
                  SCM ret;
                  char *str = scm_to_locale_string (scm_object_to_string (obj, SCM_UNDEFINED));
                  /* We want to avoid the surrounding double-quotes if present. */
                  char *s = str;
                  unsigned int l = strlen (s);
                
                  if (s[0] == '"' && s[l-1] == '"')
                    {
                      s[l-1] = '\0';
                      ++s;
                    }
                
                  DB (DB_BASIC, (_("guile: Expanding '%s'\n"), s));
                  s = allocated_variable_expand (s);
                  ret = scm_from_locale_string (s);
                
                  free (str);
                  free (s);
                  return ret;
                }
                
                void
                set_up_guile ()
                {
                  /* Register a subr for GNU make's eval capability.  */
                  scm_c_define_gsubr ("make-eval", 1, 0, 0, guile_eval_wrapper);
                  scm_c_define_gsubr ("make-expand", 1, 0, 0, guile_expand_wrapper);
                }

Any thoughts, suggestions, improvements, or consideration you all have,
as Guile experts, would be very welcome.

Cheers!

-- 
-------------------------------------------------------------------------------
 Paul D. Smith <psmith@gnu.org>          Find some GNU make tips at:
 http://www.gnu.org                      http://make.mad-scientist.net
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist




^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2011-09-21  8:24 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-09-18  0:10 Using guile as an extension language for GNU make Paul Smith
2011-09-18 12:10 ` Ludovic Courtès
2011-09-18 17:21   ` Embedding vs. Extending (was: Re: Using guile as an extension language for GNU make) Paul Smith
2011-09-18 21:48     ` Embedding vs. Extending Ludovic Courtès
2011-09-18 17:42   ` Using guile as an extension language for GNU make Paul Smith
2011-09-18 21:28     ` Ludovic Courtès
2011-09-18 15:30 ` Thien-Thi Nguyen
2011-09-18 19:28   ` Paul Smith
2011-09-19  0:28     ` Thien-Thi Nguyen
2011-09-19 15:14       ` Paul Smith
2011-09-19 19:41         ` Hans Aberg
2011-09-19 21:56           ` Paul Smith
2011-09-19 22:35             ` Hans Aberg
2011-09-19 23:00             ` Hans Aberg
2011-09-21  2:42             ` Mark H Weaver
2011-09-21  8:24               ` Hans Aberg
2011-09-20 16:17         ` Thien-Thi Nguyen
2011-09-20 17:31           ` Paul Smith
2011-09-20 19:02             ` Paul Smith
2011-09-21  0:48               ` Thien-Thi Nguyen
2011-09-20 20:39             ` Thien-Thi Nguyen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).