2323import sys
2424import os
2525import 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-
3131Usage:
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
4044modinc = None
4145def 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 ()
0 commit comments