* makeint.h (STOP_SET): [SV 40371] Cast to unsigned char.
[platform/upstream/make.git] / guile.c
1 /* GNU Guile interface for GNU Make.
2 Copyright (C) 2011-2013 Free Software Foundation, Inc.
3 This file is part of GNU Make.
4
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
8 version.
9
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 #include "makeint.h"
18 #include "gnumake.h"
19
20 #include "debug.h"
21 #include "filedef.h"
22 #include "dep.h"
23 #include "variable.h"
24
25 #include <libguile.h>
26
27 /* Pre-2.0 versions of Guile don't have a typedef for gsubr function types.  */
28 #if SCM_MAJOR_VERSION < 2
29 # define GSUBR_TYPE         SCM (*) ()
30 /* Guile 1.x doesn't really support i18n.  */
31 # define EVAL_STRING(_s)    scm_c_eval_string (_s)
32 #else
33 # define GSUBR_TYPE         scm_t_subr
34 # define EVAL_STRING(_s)    scm_eval_string (scm_from_utf8_string (_s))
35 #endif
36
37 static SCM make_mod = SCM_EOL;
38 static SCM obj_to_str = SCM_EOL;
39
40 /* Convert an SCM object into a string.  */
41 static char *
42 cvt_scm_to_str (SCM obj)
43 {
44   return scm_to_locale_string (scm_call_1 (obj_to_str, obj));
45 }
46
47 /* Perform the GNU make expansion function.  */
48 static SCM
49 guile_expand_wrapper (SCM obj)
50 {
51   char *str = cvt_scm_to_str (obj);
52   SCM ret;
53   char *res;
54
55   DB (DB_BASIC, (_("guile: Expanding '%s'\n"), str));
56   res = gmk_expand (str);
57   ret = scm_from_locale_string (res);
58
59   free (str);
60   free (res);
61
62   return ret;
63 }
64
65 /* Perform the GNU make eval function.  */
66 static SCM
67 guile_eval_wrapper (SCM obj)
68 {
69   char *str = cvt_scm_to_str (obj);
70
71   DB (DB_BASIC, (_("guile: Evaluating '%s'\n"), str));
72   gmk_eval (str, 0);
73
74   return SCM_BOOL_F;
75 }
76
77 /* Invoked by scm_c_define_module(), in the context of the GNU make module.  */
78 static void
79 guile_define_module (void *data UNUSED)
80 {
81 /* Ingest the predefined Guile module for GNU make.  */
82 #include "gmk-default.h"
83
84   /* Register a subr for GNU make's eval capability.  */
85   scm_c_define_gsubr ("gmk-expand", 1, 0, 0, (GSUBR_TYPE) guile_expand_wrapper);
86
87   /* Register a subr for GNU make's eval capability.  */
88   scm_c_define_gsubr ("gmk-eval", 1, 0, 0, (GSUBR_TYPE) guile_eval_wrapper);
89
90   /* Define the rest of the module.  */
91   scm_c_eval_string (GUILE_module_defn);
92 }
93
94 /* Initialize the GNU make Guile module.  */
95 static void *
96 guile_init (void *arg UNUSED)
97 {
98   /* Define the module.  */
99   make_mod = scm_c_define_module ("gnu make", guile_define_module, NULL);
100
101   /* Get a reference to the object-to-string translator, for later.  */
102   obj_to_str = scm_variable_ref (scm_c_module_lookup (make_mod, "obj-to-str"));
103
104   /* Import the GNU make module exports into the generic space.  */
105   scm_c_eval_string ("(use-modules (gnu make))");
106
107   return NULL;
108 }
109
110 static void *
111 internal_guile_eval (void *arg)
112 {
113   return cvt_scm_to_str (EVAL_STRING (arg));
114 }
115
116 /* This is the function registered with make  */
117 static char *
118 func_guile (const char *funcname UNUSED, unsigned int argc UNUSED, char **argv)
119 {
120   static int init = 0;
121
122   if (! init)
123     {
124       /* Initialize the Guile interpreter.  */
125       scm_with_guile (guile_init, NULL);
126       init = 1;
127     }
128
129   if (argv[0] && argv[0][0] != '\0')
130     return scm_with_guile (internal_guile_eval, argv[0]);
131
132   return NULL;
133 }
134
135 /* ----- Public interface ----- */
136
137 /* We could send the flocp to define_new_function(), but since guile is
138    "kind of" built-in, that didn't seem so useful.  */
139 int
140 guile_gmake_setup (const gmk_floc *flocp UNUSED)
141 {
142   /* Create a make function "guile".  */
143   gmk_add_function ("guile", func_guile, 0, 1, GMK_FUNC_DEFAULT);
144
145   return 1;
146 }