📄 comp.g
字号:
print >>f, "static int get_data_size(void);" if options.get("extra_setup"): print >>f, "static int extra_setup(struct state *inst, long extra_arg);" if options.get("extra_cleanup"): print >>f, "static void extra_cleanup(void);" if not options.get("no_convenience_defines"): print >>f, "#define TRUE (1)" print >>f, "#define FALSE (0)" print >>f, "#define true (1)" print >>f, "#define false (0)" print >>f if has_personality: print >>f, "static int export(char *prefix, long extra_arg, long personality) {" else: print >>f, "static int export(char *prefix, long extra_arg) {" print >>f, " char buf[HAL_NAME_LEN + 2];" print >>f, " int r = 0;" if has_array: print >>f, " int j = 0;" print >>f, " int sz = sizeof(struct state) + get_data_size();" print >>f, " struct state *inst = hal_malloc(sz);" print >>f, " memset(inst, 0, sz);" if options.get("extra_setup"): if has_personality: print >>f, " r = extra_setup(inst, extra_arg, &personality);" else: print >>f, " r = extra_setup(inst, extra_arg);" print >>f, " if(r != 0) return r;" if has_personality: print >>f, " inst->_personality = personality;" for name, type, array, dir, value, personality in pins: if personality: print >>f, "if(%s) {" % personality if array: if isinstance(array, tuple): array = array[1] print >>f, " for(j=0; j < (%s); j++) {" % array print >>f, " r = hal_pin_%s_newf(%s, &(inst->%s[j]), comp_id," % ( type, dirmap[dir], to_c(name)) print >>f, " \"%%s%s\", prefix, j);" % to_hal("." + name) print >>f, " if(r != 0) return r;" if value is not None: print >>f, " *(inst->%s[j]) = %s;" % (to_c(name), value) print >>f, " }" else: print >>f, " r = hal_pin_%s_newf(%s, &(inst->%s), comp_id," % ( type, dirmap[dir], to_c(name)) print >>f, " \"%%s%s\", prefix);" % to_hal("." + name) print >>f, " if(r != 0) return r;" if value is not None: print >>f, " *(inst->%s) = %s;" % (to_c(name), value) if personality: print >>f, "}" for name, type, array, dir, value, personality in params: if personality: print >>f, "if(%s) {" % personality if array: if isinstance(array, tuple): array = array[1] print >>f, " for(j=0; j < %s; j++) {" % array print >>f, " r = hal_param_%s_newf(%s, &(inst->%s[j]), comp_id," % ( type, dirmap[dir], to_c(name)) print >>f, " \"%%s%s\", prefix, j);" % to_hal("." + name) print >>f, " if(r != 0) return r;" if value is not None: print >>f, " inst->%s[j] = %s;" % (to_c(name), value) print >>f, " }" else: print >>f, " r = hal_param_%s_newf(%s, &(inst->%s), comp_id," % ( type, dirmap[dir], to_c(name)) print >>f, " \"%%s%s\", prefix);" % to_hal("." + name) if value is not None: print >>f, " inst->%s = %s;" % (to_c(name), value) print >>f, " if(r != 0) return r;" if personality: print >>f, "}" for type, name, array, value in variables: if value is None: continue if array: print >>f, " for(j=0; j < %s; j++) {" % array print >>f, " inst->%s[j] = %s;" % (name, value) print >>f, " }" else: print >>f, " inst->%s = %s;" % (name, value) for name, fp in functions: print >>f, " rtapi_snprintf(buf, HAL_NAME_LEN, \"%%s%s\", prefix);"\ % to_hal("." + name) print >>f, " r = hal_export_funct(buf, (void(*)(void *inst, long))%s, inst, %s, 0, comp_id);" % ( to_c(name), int(fp)) print >>f, " if(r != 0) return r;" print >>f, " inst->_next = first_inst;" print >>f, " first_inst = inst;" print >>f, " return 0;" print >>f, "}" if options.get("count_function"): print >>f, "static int get_count(void);" if options.get("rtapi_app", 1): if options.get("constructable") and not options.get("singleton"): print >>f, "static int export_1(char *prefix, char *argstr) {" print >>f, " int arg = simple_strtol(argstr, NULL, 0);" print >>f, " return export(prefix, arg);" print >>f, "}" if not options.get("singleton") and not options.get("count_function") : print >>f, "static int count = %s;" \ % options.get("default_count", 1) if not options.get("userspace"): print >>f, "RTAPI_MP_INT(count, \"number of %s\");" % comp_name if has_personality: print >>f, "static int personality[16] = {0,};" print >>f, "RTAPI_MP_ARRAY_INT(personality, 16, \"personality of each %s\");" % comp_name print >>f, "int rtapi_app_main(void) {" print >>f, " int r = 0;" if not options.get("singleton"): print >>f, " int i;" if options.get("count_function"): print >>f, " int count = get_count();" print >>f, " comp_id = hal_init(\"%s\");" % comp_name print >>f, " if(comp_id < 0) return comp_id;" if options.get("singleton"): if has_personality: print >>f, " r = export(\"%s\", 0, personality[0]);" % \ to_hal(removeprefix(comp_name, "hal_")) else: print >>f, " r = export(\"%s\", 0);" % \ to_hal(removeprefix(comp_name, "hal_")) else: print >>f, " for(i=0; i<count; i++) {" print >>f, " char buf[HAL_NAME_LEN + 2];" print >>f, " rtapi_snprintf(buf, HAL_NAME_LEN, " \ "\"%s.%%d\", i);" % \ to_hal(removeprefix(comp_name, "hal_")) if has_personality: print >>f, " r = export(buf, i, personality[i%16]);" else: print >>f, " r = export(buf, i);" print >>f, " if(r != 0) break;" print >>f, " }" if options.get("constructable") and not options.get("singleton"): print >>f, " hal_set_constructor(comp_id, export_1);" print >>f, " if(r) {" if options.get("extra_cleanup"): print >>f, " extra_cleanup();" print >>f, " hal_exit(comp_id);" print >>f, " } else {" print >>f, " hal_ready(comp_id);" print >>f, " }" print >>f, " return r;"; print >>f, "}" print >>f print >>f, "void rtapi_app_exit(void) {" if options.get("extra_cleanup"): print >>f, " extra_cleanup();" print >>f, " hal_exit(comp_id);" print >>f, "}" if options.get("userspace"): print >>f, "static void user_mainloop(void);" if options.get("userinit"): print >>f, "static void user_init(int argc, char **argv);" print >>f, "int argc=0; char **argv=0;" print >>f, "int main(int argc_, char **argv_) {" print >>f, " argc = argc_; argv = argv;" print >>f if options.get("userinit", 0): print >>f, " userinit(argc, argv)"; print >>f print >>f, " if(rtapi_app_main() < 0) return 1;" print >>f, " user_mainloop();" print >>f, " rtapi_app_exit();" print >>f, " return 0;" print >>f, "}" print >>f if not options.get("no_convenience_defines"): print >>f, "#define FUNCTION(name) static void name(struct state *inst, long period)" print >>f, "#define EXTRA_SETUP() static int extra_setup(struct state *inst, long extra_arg)" print >>f, "#define EXTRA_CLEANUP() static void extra_cleanup(void)" print >>f, "#define fperiod (period * 1e-9)" for name, type, array, dir, value, personality in pins: if array: if dir == 'in': print >>f, "#define %s(i) (0+*(inst->%s[i]))" % (to_c(name), to_c(name)) else: print >>f, "#define %s(i) (*(inst->%s[i]))" % (to_c(name), to_c(name)) else: if dir == 'in': print >>f, "#define %s (0+*inst->%s)" % (to_c(name), to_c(name)) else: print >>f, "#define %s (*inst->%s)" % (to_c(name), to_c(name)) for name, type, array, dir, value, personality in params: if array: print >>f, "#define %s(i) (inst->%s[i])" % (to_c(name), to_c(name)) else: print >>f, "#define %s (inst->%s)" % (to_c(name), to_c(name)) for type, name, array, value in variables: print >>f, "#define %s (inst->%s)" % (name, name) if has_data: print >>f, "#define data (*(%s*)&(inst->_data))" % options['data'] if has_personality: print >>f, "#define personality (inst->_personality)" if options.get("userspace"): print >>f, "#define FOR_ALL_INSTS() for(inst = first_inst; inst; inst = inst->_next)" print >>f print >>fdef epilogue(f): data = options.get('data') print >>f if data: print >>f, "static int get_data_size(void) { return sizeof(%s); }" % data else: print >>f, "static int get_data_size(void) { return 0; }"INSTALL, COMPILE, PREPROCESS, DOCUMENT, INSTALLDOC, VIEWDOC = range(6)modename = ("install", "compile", "preprocess", "document", "installdoc", "viewdoc")modinc = Nonedef find_modinc(): global modinc if modinc: return modinc d = os.path.abspath(os.path.dirname(os.path.dirname(sys.argv[0]))) for e in ['src', 'etc/emc2', '/etc/emc2']: e = os.path.join(d, e, 'Makefile.modinc') if os.path.exists(e): modinc = e return e raise SystemExit, "Unable to locate Makefile.modinc"def build_usr(tempdir, filename, mode, origfilename): binname = os.path.basename(os.path.splitext(filename)[0]) makefile = os.path.join(tempdir, "Makefile") f = open(makefile, "w") print >>f, "%s: %s" % (binname, filename) print >>f, "\t$(CC) $(EXTRA_CFLAGS) -URTAPI -DULAPI -O2 %s -o $@ $< -Wl,-rpath,$(LIBDIR) -L$(LIBDIR) -lemchal %s" % ( options.get("extra_compile_args", ""), options.get("extra_link_args", "")) print >>f, "include %s" % find_modinc() f.close() result = os.system("cd %s; make -S %s" % (tempdir, binname)) if result != 0: raise SystemExit, result output = os.path.join(tempdir, binname) if mode == INSTALL: shutil.copy(output, os.path.join(BASE, "bin", binname)) elif mode == COMPILE: shutil.copy(output, os.path.join(os.path.dirname(origfilename),binname))def build_rt(tempdir, filename, mode, origfilename): objname = os.path.basename(os.path.splitext(filename)[0] + ".o") makefile = os.path.join(tempdir, "Makefile") f = open(makefile, "w") print >>f, "obj-m += %s" % objname print >>f, "include %s" % find_modinc() print >>f, "EXTRA_CFLAGS += -I%s" % os.path.abspath(os.path.dirname(origfilename)) print >>f, "EXTRA_CFLAGS += -I%s" % os.path.abspath('.') f.close() if mode == INSTALL: target = "modules install" else: target = "modules" result = os.system("cd %s; make -S %s" % (tempdir, target)) if result != 0: raise SystemExit, result if mode == COMPILE: for extension in ".ko", ".so", ".o": kobjname = os.path.splitext(filename)[0] + extension if os.path.exists(kobjname): shutil.copy(kobjname, os.path.basename(kobjname)) break else: raise SystemExit, "Unable to copy module from temporary directory"def finddoc(section=None, name=None): for item in docs: if ((section == None or section == item[0]) and (name == None or name == item[1])): return item return Nonedef finddocs(section=None, name=None): for item in docs: if ((section == None or section == item[0]) and (name == None or name == item[1])): yield itemdef to_hal_man(s): if options.get("singleton"): s = "%s.%s" % (comp_name, s) else: s = "%s.\\fIN\\fB.%s" % (comp_name, s) s = s.replace("_", "-") s = s.rstrip("-") s = s.rstrip(".") s = re.sub("#+", lambda m: "\\fI" + "M" * len(m.group(0)) + "\\fB", s) # s = s.replace("-", "\\-") return s
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -