summaryrefslogtreecommitdiff
path: root/guile.c
diff options
context:
space:
mode:
authorPaul Smith <psmith@gnu.org>2012-01-15 22:41:53 +0000
committerPaul Smith <psmith@gnu.org>2012-01-15 22:41:53 +0000
commitc992c4d80f51540699f33fed067caf6b7c38df79 (patch)
tree2e8bee26fd19820b85422752724e87639d1b0c77 /guile.c
parent3057357c0a5c2507eef2b61eef9ebfb569b30230 (diff)
downloadgunmake-c992c4d80f51540699f33fed067caf6b7c38df79.tar.gz
Add GNU Guile as an optional embedded scripting language for make.
On configure-enabled systems, configure will detect Guile installed (using pkg-config, which is how GNU Guile is distributed) and enable it if so. On all non-configure-enabled systems, currently, the default is for Guile support to be disabled.
Diffstat (limited to 'guile.c')
-rw-r--r--guile.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/guile.c b/guile.c
new file mode 100644
index 0000000..8fb4c45
--- /dev/null
+++ b/guile.c
@@ -0,0 +1,104 @@
+/* GNU Guile interface for GNU Make.
+Copyright (C) 2011 Free Software Foundation, Inc.
+This file is part of GNU Make.
+
+GNU Make 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.
+
+GNU Make 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, see <http://www.gnu.org/licenses/>. */
+
+#include "make.h"
+#include "debug.h"
+#include "dep.h"
+#include "variable.h"
+
+#include <libguile.h>
+
+static SCM make_mod = SCM_EOL;
+static SCM obj_to_str = SCM_EOL;
+
+/* Convert an SCM object into a string. */
+static char *
+cvt_scm_to_str (SCM obj)
+{
+ return scm_to_locale_string (scm_call_1 (obj_to_str, obj));
+}
+
+/* Perform the GNU make expansion function. */
+static SCM
+guile_expand_wrapper (SCM obj)
+{
+ char *str = cvt_scm_to_str (obj);
+ SCM ret;
+ char *res;
+
+ DB (DB_BASIC, (_("guile: Expanding '%s'\n"), str));
+ res = allocated_variable_expand (str);
+ ret = scm_from_locale_string (res);
+
+ free (str);
+ free (res);
+
+ return ret;
+}
+
+/* Invoked by scm_c_define_module(), in the context of the GNU make module. */
+static void
+guile_define_module (void *data UNUSED)
+{
+/* Ingest the predefined Guile module for GNU make. */
+#include "gmk-default.h"
+
+ /* Register a subr for GNU make's eval capability. */
+ scm_c_define_gsubr ("gmk-expand", 1, 0, 0, guile_expand_wrapper);
+
+ /* Define the rest of the module. */
+ scm_c_eval_string (GUILE_module_defn);
+}
+
+/* Initialize the GNU make Guile module. */
+static void *
+guile_init (void *arg UNUSED)
+{
+ /* Define the module. */
+ make_mod = scm_c_define_module ("gnu make", guile_define_module, NULL);
+
+ /* Get a reference to the object-to-string translator, for later. */
+ obj_to_str = scm_variable_ref (scm_c_module_lookup (make_mod, "obj-to-str"));
+
+ /* Import the GNU make module exports into the generic space. */
+ scm_c_eval_string ("(use-modules (gnu make))");
+
+ return NULL;
+}
+
+static void *
+internal_guile_eval (void *arg)
+{
+ return cvt_scm_to_str (scm_c_eval_string (arg));
+}
+
+/* ----- Public interface ----- */
+
+/* This is the make interface for passing programs to Guile. */
+char *
+guile_eval_string (char *str)
+{
+ return scm_with_guile (internal_guile_eval, str);
+}
+
+void
+setup_guile ()
+{
+ scm_with_guile (guile_init, NULL);
+
+ /* Add 'guile' to the list of features. */
+ do_variable_definition (NILF, ".FEATURES", "guile", o_default, f_append, 0);
+}