2015-11-16 00:00:25 +00:00
|
|
|
#!/usr/bin/env python
|
2015-11-19 16:14:00 +00:00
|
|
|
|
|
|
|
# Module helper script.
|
|
|
|
|
2022-01-01 07:45:51 +00:00
|
|
|
# Copyright 2015-2022 Free Software Foundation, Inc.
|
2015-11-19 16:14:00 +00:00
|
|
|
|
|
|
|
# This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
# GNU Emacs 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 Emacs 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
|
2017-09-13 22:52:52 +00:00
|
|
|
# along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2015-11-19 16:14:00 +00:00
|
|
|
|
2015-11-16 00:00:25 +00:00
|
|
|
import os
|
|
|
|
import string
|
|
|
|
import subprocess as sp
|
|
|
|
import argparse
|
|
|
|
import re
|
|
|
|
|
|
|
|
EMACS = os.path.join('..', 'src', 'emacs')
|
|
|
|
|
|
|
|
def find_modules():
|
|
|
|
modpaths = []
|
|
|
|
for (dirname, dirs, files) in os.walk('.'):
|
|
|
|
if 'Makefile' in files:
|
|
|
|
modpaths.append(dirname)
|
|
|
|
return modpaths
|
|
|
|
|
|
|
|
def cmd_test(args):
|
|
|
|
mods = args.module
|
|
|
|
if not mods:
|
|
|
|
mods = find_modules()
|
|
|
|
|
|
|
|
make_cmd = ['make']
|
|
|
|
if args.force:
|
|
|
|
make_cmd.append('-B')
|
|
|
|
|
|
|
|
failed = []
|
|
|
|
for m in mods:
|
2016-11-16 14:16:15 +00:00
|
|
|
print('[*] %s: ------- start -------' % m)
|
|
|
|
print('[*] %s: running make' % m)
|
2015-11-16 00:00:25 +00:00
|
|
|
r = sp.call(make_cmd, cwd=m)
|
|
|
|
if r != 0:
|
2016-11-16 14:16:15 +00:00
|
|
|
print('[E] %s: make failed' % m)
|
2015-11-16 00:00:25 +00:00
|
|
|
failed += [m]
|
|
|
|
continue
|
|
|
|
|
2016-11-16 14:16:15 +00:00
|
|
|
print('[*] %s: running test' % m)
|
2015-11-16 00:00:25 +00:00
|
|
|
testpath = os.path.join(m, 'test.el')
|
|
|
|
if os.path.isfile(testpath):
|
2015-11-19 19:31:45 +00:00
|
|
|
emacs_cmd = [EMACS, '-batch', '-L', '.', '-l', 'ert',
|
|
|
|
'-l', testpath, '-f', 'ert-run-tests-batch-and-exit']
|
2016-11-16 14:16:15 +00:00
|
|
|
print(' '.join(emacs_cmd))
|
2015-11-16 00:00:25 +00:00
|
|
|
r = sp.call(emacs_cmd)
|
|
|
|
if r != 0:
|
2016-11-16 14:16:15 +00:00
|
|
|
print('[E] %s: test failed' % m)
|
2015-11-16 00:00:25 +00:00
|
|
|
failed += [m]
|
|
|
|
continue
|
|
|
|
else:
|
2016-11-16 14:16:15 +00:00
|
|
|
print('[W] %s: no test to run' % m)
|
2015-11-16 00:00:25 +00:00
|
|
|
|
2016-11-16 14:16:15 +00:00
|
|
|
print('\n[*] %d/%d MODULES OK' % (len(mods)-len(failed), len(mods)))
|
2015-11-16 00:00:25 +00:00
|
|
|
for m in failed:
|
2016-11-16 14:16:15 +00:00
|
|
|
print('\tfailed: %s' % m)
|
2015-11-16 00:00:25 +00:00
|
|
|
|
|
|
|
def to_lisp_sym(sym):
|
|
|
|
sym = re.sub('[_ ]', '-', sym)
|
|
|
|
return sym
|
|
|
|
|
|
|
|
def to_c_sym(sym):
|
|
|
|
sym = re.sub('[- ]', '_', sym)
|
|
|
|
return sym
|
|
|
|
|
|
|
|
def cmd_init(args):
|
|
|
|
if os.path.exists(args.module):
|
2016-11-16 14:16:15 +00:00
|
|
|
print("%s: file/dir '%s' already exists" % (__file__, args.module))
|
2015-11-16 00:00:25 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
os.mkdir(args.module)
|
|
|
|
|
|
|
|
template_vars = {
|
|
|
|
'module': args.module,
|
|
|
|
'func': args.fun,
|
|
|
|
'c_file': '%s.c' % args.module,
|
|
|
|
'c_func': 'F%s_%s' % (to_c_sym(args.module), to_c_sym(args.fun)),
|
|
|
|
'lisp_func': '%s-%s' % (args.module, to_lisp_sym(args.fun)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for path, t in TEMPLATES.items():
|
|
|
|
if isinstance(path, string.Template):
|
|
|
|
path = path.substitute(template_vars)
|
|
|
|
path = os.path.join(args.module, path)
|
2016-11-16 14:16:15 +00:00
|
|
|
print("writing %s..." % path)
|
2015-11-16 00:00:25 +00:00
|
|
|
with open(path, "w+") as f:
|
|
|
|
f.write(t.substitute(template_vars))
|
2016-11-16 14:16:15 +00:00
|
|
|
print("done! you can run %s test %s" % (__file__, args.module))
|
2015-11-16 00:00:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
# path always written relative to this file
|
|
|
|
os.chdir(os.path.dirname(os.path.realpath(__file__)))
|
|
|
|
|
|
|
|
mainp = argparse.ArgumentParser()
|
|
|
|
subp = mainp.add_subparsers()
|
|
|
|
|
|
|
|
testp = subp.add_parser('test', help='run tests')
|
2015-11-19 19:31:45 +00:00
|
|
|
testp.add_argument('-f', '--force', action='store_true',
|
|
|
|
help='force regeneration (make -B)')
|
|
|
|
testp.add_argument('module', nargs='*',
|
|
|
|
help='path to module to test (default all)')
|
2015-11-16 00:00:25 +00:00
|
|
|
testp.set_defaults(func=cmd_test)
|
|
|
|
|
|
|
|
initp = subp.add_parser('init', help='create a test module from a template')
|
|
|
|
initp.add_argument('module', help='name of the new module')
|
2015-11-19 19:31:45 +00:00
|
|
|
initp.add_argument('-f', '--fun', default='fun',
|
2015-11-30 05:50:59 +00:00
|
|
|
help='override name of the default function')
|
2015-11-16 00:00:25 +00:00
|
|
|
initp.set_defaults(func=cmd_init)
|
|
|
|
|
|
|
|
args = mainp.parse_args()
|
|
|
|
args.func(args)
|
|
|
|
|
|
|
|
|
|
|
|
# double the $ to escape python template syntax
|
|
|
|
TEMPLATES = {
|
|
|
|
'Makefile': string.Template('''
|
|
|
|
ROOT = ../..
|
|
|
|
|
|
|
|
CC = gcc
|
|
|
|
LD = gcc
|
|
|
|
CFLAGS = -ggdb3 -Wall
|
|
|
|
LDFLAGS =
|
|
|
|
|
|
|
|
all: ${module}.so ${module}.doc
|
|
|
|
|
|
|
|
%.so: %.o
|
|
|
|
$$(LD) -shared $$(LDFLAGS) -o $$@ $$<
|
|
|
|
|
|
|
|
%.o: %.c
|
|
|
|
$$(CC) $$(CFLAGS) -I$$(ROOT)/src -fPIC -c $$<
|
|
|
|
|
|
|
|
'''),
|
|
|
|
|
|
|
|
string.Template('${c_file}'): string.Template('''
|
2015-11-19 21:50:23 +00:00
|
|
|
#include <emacs-module.h>
|
2015-11-16 00:00:25 +00:00
|
|
|
|
|
|
|
int plugin_is_GPL_compatible;
|
|
|
|
|
2015-11-19 19:31:45 +00:00
|
|
|
static emacs_value
|
2016-11-28 15:48:50 +00:00
|
|
|
${c_func} (emacs_env *env, ptrdiff_t nargs, emacs_value args[], void *data)
|
2015-11-16 00:00:25 +00:00
|
|
|
{
|
|
|
|
return env->intern (env, "t");
|
|
|
|
}
|
|
|
|
|
2015-11-19 19:31:45 +00:00
|
|
|
/* Bind NAME to FUN. */
|
|
|
|
static void
|
|
|
|
bind_function (emacs_env *env, const char *name, emacs_value Sfun)
|
2015-11-16 00:00:25 +00:00
|
|
|
{
|
|
|
|
emacs_value Qfset = env->intern (env, "fset");
|
|
|
|
emacs_value Qsym = env->intern (env, name);
|
|
|
|
emacs_value args[] = { Qsym, Sfun };
|
|
|
|
|
|
|
|
env->funcall (env, Qfset, 2, args);
|
|
|
|
}
|
|
|
|
|
2015-11-19 19:31:45 +00:00
|
|
|
/* Provide FEATURE to Emacs. */
|
|
|
|
static void
|
|
|
|
provide (emacs_env *env, const char *feature)
|
2015-11-16 00:00:25 +00:00
|
|
|
{
|
|
|
|
emacs_value Qfeat = env->intern (env, feature);
|
|
|
|
emacs_value Qprovide = env->intern (env, "provide");
|
|
|
|
emacs_value args[] = { Qfeat };
|
|
|
|
|
|
|
|
env->funcall (env, Qprovide, 1, args);
|
|
|
|
}
|
|
|
|
|
2015-11-19 19:31:45 +00:00
|
|
|
int
|
|
|
|
emacs_module_init (struct emacs_runtime *ert)
|
2015-11-16 00:00:25 +00:00
|
|
|
{
|
|
|
|
emacs_env *env = ert->get_environment (ert);
|
2015-11-19 19:31:45 +00:00
|
|
|
bind_function (env, "${lisp_func}",
|
|
|
|
env->make_function (env, 1, 1, ${c_func}, "doc", NULL));
|
2015-11-16 00:00:25 +00:00
|
|
|
provide (env, "${module}");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
'''),
|
|
|
|
'test.el': string.Template('''
|
|
|
|
(require 'ert)
|
|
|
|
(require 'module-test-common)
|
|
|
|
|
|
|
|
;; #$$ works when loading, buffer-file-name when evaluating from emacs
|
|
|
|
(module-load (module-path (or #$$ (expand-file-name (buffer-file-name)))))
|
|
|
|
|
|
|
|
(ert-deftest ${lisp_func}-test ()
|
|
|
|
(should (eq (${lisp_func} 42) t)))
|
|
|
|
''')
|
|
|
|
}
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|