Skip to content

Commit 758d36f

Browse files
authored
Merge pull request #3415 from BsAtHome/backport_2.9_fix_modcompile
Backport: fix modcompile issues
2 parents 807682f + d48900f commit 758d36f

6 files changed

Lines changed: 114 additions & 42 deletions

File tree

docs/man/man1/modcompile.1

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,38 @@
2626
.SH NAME
2727
modcompile \- Utility for compiling Modbus drivers
2828

29+
.SH SYNOPSIS
30+
31+
\fBmodcompile -h\fR
32+
33+
\fBmodcompile* [\fB-k\fR] [\fB-n\fR] [\fB-v\fR] all
34+
35+
\fBmodcompile* [\fB-k\fR] [\fB-n\fR] [\fB-v\fR] module.mod ...
36+
2937
.SH DESCRIPTION
30-
\fBmodcompile\fR is used to compile modbus drivers that use the Mesa
31-
card UARTs.
38+
The \fBmodcompile\fR utility is used to compile modbus drivers that use the PktUART
39+
interface of Mesa cards.
3240

3341
See the main LinuxCNC documentation for more details.
3442

3543
http://linuxcnc.org/docs/stable/html/drivers/mesa_modbus.html
36-
.
44+
45+
.SH OPTIONS
46+
.TP
47+
\fB-h\fR, \fB--help\fR
48+
Brief help message.
49+
.TP
50+
\fB-k\fR, \fB--keep\fR
51+
Keep the temporary files. The program will print a line where these are
52+
located. You will be responsible for deleting them.
53+
.TP
54+
\fB-n\fR, \fB--noinstall\fR
55+
Do not run the install step for the compiled module. This option is probably
56+
only useful if you also use the \fB--keep\fR option.
57+
.TP
58+
\fB-v\fR, \fB--verbose\fR
59+
Do a verbose build, showing all steps detailed while being performed.
60+
3761
.SH "SEE ALSO"
3862
\fBLinuxCNC(1)\fR
3963

src/hal/drivers/mesa-hostmot2/modbus/analogue.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ static const hm2_modbus_chan_descriptor_t channels[] = {
2323
internal workings. 1 is default (errors only) */
2424

2525
#define DEBUG 1
26+
27+
// vim: syn=c

src/hal/drivers/mesa-hostmot2/modbus/mesa_modbus.c.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static const char *error_codes[]={
4848
"Server Device Busy",
4949
"Negative Acknowledge",
5050
"Memory Parity Error",
51-
"Unknown exception code 9"
51+
"Unknown exception code 9",
5252
"Gateway Path Unavailable",
5353
"Gateway Failed to Respond",
5454
"Comm Timeout"
@@ -894,4 +894,4 @@ static uint16_t RTU_CRC(rtapi_u8* buf, int len){
894894
return crc;
895895
}
896896

897-
// vim: syn=c
897+
// vim: syn=c ts=4

src/hal/drivers/mesa-hostmot2/modbus/mesa_modbus.mod.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ static const hm2_modbus_chan_descriptor_t channels[] = {
2929
// Create 7 scaled FP HAL pins to control holfing registers at 0x400-0x406
3030
{HAL_FLOAT, 16, 0x0300, 1, "more_floats"},
3131
};
32+
33+
// vim: syn=c

src/hal/drivers/mesa-hostmot2/modbus/modcompile.py

Lines changed: 80 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,23 @@
2323
import sys
2424
import os
2525
import tempfile
26+
import getopt
27+
import shutil
2628

27-
28-
def usage(exitval=0):
29+
def usage():
2930
print("""Build and install Modbus components that use the Mesa PktUART
30-
3131
Usage:
32-
modcompile device.mod Compile and install a driver defined in
33-
device.h
34-
modcompile all Compile and install all definition files
35-
in this directory.
36-
32+
modcompile [opts] device.mod... Compile and install a driver defined in
33+
device.mod
34+
modcompile [opts] all Compile and install all .mod definition
35+
files in this directory.
36+
Options:
37+
-h|--help This message
38+
-k|--keep Keep temporary files
39+
-n|--noinstall Don't perform the install step
40+
-v|--verbose Verbose compile
3741
""")
38-
raise SystemExit(exitval)
42+
sys.exit(2)
3943

4044
modinc = None
4145
def find_modinc():
@@ -49,31 +53,70 @@ def find_modinc():
4953
return e
5054
raise SystemExit("Unable to locate Makefile.modinc")
5155

52-
if len(sys.argv) < 2:
53-
usage(0)
54-
55-
if sys.argv[1] == "all":
56-
names = [f for f in os.listdir(".") if f.endswith(".mod")]
57-
else:
58-
names = [sys.argv[1]]
59-
60-
tempdir = tempfile.mkdtemp()
61-
BASE = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), ".."))
62-
print(os.path.join(tempdir, "mesa_modbus.c"))
63-
os.symlink(os.path.join(BASE, "share", "linuxcnc", "mesa_modbus.c.tmpl"), os.path.join(tempdir, "mesa_modbus.c"))
64-
for f in names:
65-
b = os.path.splitext(os.path.basename(f))[0]
66-
# The module definition is #included as mesa_modbus.h
67-
m = open(os.path.join(tempdir, "Makefile"), "w")
68-
print("obj-m += %s.o" % b,file=m)
69-
print("%s-objs:=mesa_modbus.o" % b,file=m)
70-
print("include %s" % find_modinc(), file=m)
71-
print("EXTRA_CFLAGS += -I%s" % tempdir, file=m)
72-
print("EXTRA_CFLAGS += -DMODFILE=%s" % os.path.abspath(f), file=m)
73-
print("EXTRA_CFLAGS += -D_COMP_NAME_=%s" % b, file=m)
74-
m.close()
75-
os.system("touch mesa_modbus.c") # Force a recompile
76-
result = os.system("cd %s && make -S modules install" % tempdir)
77-
78-
if result != 0:
79-
raise SystemExit(os.WEXITSTATUS(result) or 1)
56+
def main():
57+
# Get the command-line options
58+
try:
59+
opts, args = getopt.getopt(sys.argv[1:], "hklnv", ["help", "keep", "noinstall", "verbose"])
60+
except getopt.GetoptError as err:
61+
raise SystemExit(err) # Something like "option -a not recognized"
62+
63+
# Check the options
64+
verbose = ""
65+
install = " install"
66+
keep = False
67+
for o, a in opts:
68+
if o in ("-v", "--verbose"):
69+
verbose = " V=1"
70+
elif o in ("-n", "--noinstall"):
71+
install = ""
72+
elif o in ("-k", "--keep"):
73+
keep = True
74+
elif o in ("-h", "--help"):
75+
usage()
76+
else:
77+
raise SystemExit("Unhandled option: '%s'" % o);
78+
79+
if len(args) < 1:
80+
raise SystemExit("Must have at least one 'file.mod' argument or 'all' for all.");
81+
82+
if len(args) > 1 and "all" in args:
83+
raise SystemExit("Cannot do both 'all' modules and specific modules.");
84+
if args[0] == "all":
85+
args = [f for f in os.listdir(".") if f.endswith(".mod")]
86+
# 'args' now contains all modules to compile
87+
88+
if len(args) < 1:
89+
raise SystemExit("No modules found (*.mod files) to compile.");
90+
91+
# Create a temporary directory and copy the C template into it
92+
tempdir = tempfile.mkdtemp(prefix="modcompile")
93+
BASE = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), ".."))
94+
csrc = os.path.join(tempdir, "mesa_modbus.c")
95+
if verbose:
96+
print(csrc)
97+
shutil.copy(os.path.join(BASE, "share", "linuxcnc", "mesa_modbus.c.tmpl"), csrc)
98+
99+
# For each module to compile...
100+
for f in args:
101+
modname = os.path.splitext(os.path.basename(f))[0]
102+
# The module definition is #included as mesa_modbus.h
103+
m = open(os.path.join(tempdir, "Makefile"), "w")
104+
print("obj-m += %s.o" % modname, file=m)
105+
print("%s-objs:=mesa_modbus.o" % modname, file=m)
106+
print("include %s" % find_modinc(), file=m)
107+
print("EXTRA_CFLAGS += -I%s" % tempdir, file=m)
108+
print("EXTRA_CFLAGS += -DMODFILE=%s" % os.path.abspath(f), file=m)
109+
print("EXTRA_CFLAGS += -D_COMP_NAME_=%s" % modname, file=m)
110+
m.close()
111+
result = os.system("cd %s && make -B -S modules %s%s" % (tempdir, install, verbose))
112+
if result != 0:
113+
raise SystemExit(os.WEXITSTATUS(result) or 1)
114+
115+
# Cleanup step
116+
if keep:
117+
print("Sources and build files kept in: %s" % tempdir)
118+
else:
119+
shutil.rmtree(tempdir)
120+
121+
if __name__ == "__main__":
122+
main()

src/hal/drivers/mesa-hostmot2/modbus/relayboard.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ static const hm2_modbus_chan_descriptor_t channels[] = {
2727
Use 3 to see a lot of information about the modbus
2828
internal workings. 1 is default (errors only) */
2929

30+
// vim: syn=c

0 commit comments

Comments
 (0)