From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Paul Smith Newsgroups: gmane.lisp.guile.user Subject: Using guile as an extension language for GNU make Date: Sat, 17 Sep 2011 20:10:16 -0400 Organization: GNU's Not Unix! Message-ID: <1316304616.28907.118.camel@homebase> Reply-To: psmith@gnu.org NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1316304632 21237 80.91.229.12 (18 Sep 2011 00:10:32 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sun, 18 Sep 2011 00:10:32 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sun Sep 18 02:10:25 2011 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1R54xl-0001h6-3n for guile-user@m.gmane.org; Sun, 18 Sep 2011 02:10:25 +0200 Original-Received: from localhost ([::1]:37930 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R54xk-0006UB-J2 for guile-user@m.gmane.org; Sat, 17 Sep 2011 20:10:24 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:48554) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R54xh-0006U6-RU for guile-user@gnu.org; Sat, 17 Sep 2011 20:10:22 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1R54xf-0002k8-TY for guile-user@gnu.org; Sat, 17 Sep 2011 20:10:21 -0400 Original-Received: from oproxy1-pub.bluehost.com ([66.147.249.253]:35300) by eggs.gnu.org with smtp (Exim 4.71) (envelope-from ) id 1R54xf-0002k2-Ks for guile-user@gnu.org; Sat, 17 Sep 2011 20:10:19 -0400 Original-Received: (qmail 17818 invoked by uid 0); 18 Sep 2011 00:10:18 -0000 Original-Received: from unknown (HELO box531.bluehost.com) (74.220.219.131) by oproxy1.bluehost.com with SMTP; 18 Sep 2011 00:10:18 -0000 Original-Received: from 146-115-71-23.c3-0.lex-ubr1.sbo-lex.ma.cable.rcn.com ([146.115.71.23] helo=[172.31.1.105]) by box531.bluehost.com with esmtpsa (SSLv3:AES256-SHA:256) (Exim 4.76) (envelope-from ) id 1R54xd-0002Jh-Vr for guile-user@gnu.org; Sat, 17 Sep 2011 18:10:18 -0600 X-Mailer: Evolution 2.32.2 X-Identified-User: {678:box531.bluehost.com:madscie1:mad-scientist.us} {sentby:smtp auth 146.115.71.23 authed with paul+mad-scientist.us} X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 66.147.249.253 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:8796 Archived-At: 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 ) 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 ) which takes a string argument and evaluates it as a makefile snippet; this is essentially the same as running (make-expand "$(eval )") 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 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