root/lu/lub.lua

Revision 590 (checked in by rsz, 4 years ago)

cleanup

Line 
1 --------------------------------------------------------------------------------
2 -- Title:               lua
3 -- Description:         Like a square peg in a round hole
4 -- Author:              Raphaël Szwarc http://alt.textdrive.com/lua/
5 -- Creation Date:       February 1, 2005
6 -- Legal:               Copyright (C) 2005 Raphaël Szwarc
7 --------------------------------------------------------------------------------
8
9 -- Constants
10 local CompileCommand = "gcc -O2 -fno-common -c -o"
11 local LinkCommand = "gcc -bundle -undefined dynamic_lookup -o"
12
13 local SourceSuffix = ".c"
14 local ObjectSuffix = ".o"
15 local ModuleSuffix = ".so"
16
17 local DataPath = "./lubfile"
18
19 -- Variables
20 local _data = nil
21
22 -- Functions
23
24 local data = function()
25         if _data == nil then
26                 local aFile = io.open( DataPath, "r" )
27
28                 if aFile ~= nil then
29                         local aContent = aFile:read( "*all" )
30
31                         aFile:close()
32                        
33                         if aContent ~= nil then
34                                 local aChunk, aStatus = loadstring( "return " .. aContent )
35
36                                 if aStatus == nil then
37                                         _data = aChunk()
38                                 else
39                                         print( "(warning) data: failed to load data '" .. aStatus .. "'" )
40                                         os.exit( 1 )
41                                end
42                         else
43                                 print( "(warning) data: empty file '" .. DataPath .. "'" )
44                                 os.exit( 1 )
45                         end
46                 else
47                         print( "(warning) data: no data found in '" .. DataPath .. "'" )
48                         os.exit( 1 )
49                 end
50         end
51        
52         return _data
53 end
54
55 local moduleName = function()
56         local someData = data()
57        
58         if someData ~= nil then
59                 local aName = someData[ "module.name" ]
60                
61                 if aName ~= nil then
62                         return aName
63                 else
64                         print( "(warning) moduleName: no module name found" )
65                         os.exit( 1 )
66                 end
67         else
68                 print( "(warning) moduleName: no data found" )
69                 os.exit( 1 )
70         end
71 end
72
73 local objectNames = function()
74         local someData = data()
75        
76         if someData ~= nil then
77                 local someNames = someData[ "object.names" ]
78                
79                 if someNames ~= nil and table.getn( someNames ) > 0 then
80                         return someNames
81                 else
82                         print( "(warning) objectNames: no object names found" )
83                         os.exit( 1 )
84                 end
85         else
86                 print( "(warning) objectNames: no data found" )
87                 os.exit( 1 )
88         end
89 end
90
91 local clean = function( cleanModule )
92         local someObjectNames = objectNames()
93        
94         if someObjectNames ~= nil then
95                 local count = table.getn( someObjectNames )
96
97                 for index = 1, count do
98                         local aName = someObjectNames[ index ]
99                         local aCommand = aName .. ObjectSuffix
100
101                         -- print ( "(info) clean: '" .. aCommand .. "'" )
102
103                         os.remove( aCommand )
104                 end
105         else
106                 print( "(warning) clean: no object names found" )
107                 os.exit( 1 )
108         end
109        
110         if cleanModule == true then
111                 local aModuleName = moduleName()
112                
113                 if aModuleName ~= nil then
114                         local aCommand = aModuleName .. ModuleSuffix
115
116                         -- print ( "(info) clean: '" .. aCommand .. "'" )
117                        
118                         os.remove( aCommand )
119                 else
120                         print( "(warning) clean: no module name found" )
121                         os.exit( 1 )
122                 end
123         end
124 end
125
126 local compileCommandWithName = function( aName )
127         if aName ~= nil then
128                 local aBuffer = {}
129
130                 table.insert( aBuffer, CompileCommand )
131                
132                 table.insert( aBuffer, aName .. ObjectSuffix )
133                 table.insert( aBuffer, aName .. SourceSuffix )
134
135                 return table.concat( aBuffer, " " )
136         else
137                 print( "(warning) compileCommandWithName: empty name" )
138                 os.exit( 1 )
139         end
140 end
141
142 local compile = function()
143         local someObjectNames = objectNames()
144        
145         if someObjectNames ~= nil then
146                 local count = table.getn( someObjectNames )
147
148                 for index = 1, count do
149                         local aName = someObjectNames[ index ]
150                         local aCommand = compileCommandWithName( aName )
151                        
152                         print ( "(info) compile: '" .. aCommand .. "'" )
153                         os.execute( aCommand )
154                 end
155         else
156                 print( "(warning) compile: no object names found" )
157                 os.exit( 1 )
158         end
159 end
160
161 local link = function()
162         local aModuleName = moduleName()
163        
164         if aModuleName ~= nil then
165                 local someObjectNames = objectNames()
166                
167                 if someObjectNames ~= nil then
168                         local aCommand = nil
169                         local aBuffer = {}
170                         local count = table.getn( someObjectNames )
171                        
172                         table.insert( aBuffer, LinkCommand )
173                         table.insert( aBuffer, aModuleName .. ModuleSuffix )
174        
175                         for index = 1, count do
176                                 local aName = someObjectNames[ index ]
177
178                                 table.insert( aBuffer, aName .. ObjectSuffix )
179                         end
180                        
181                         aCommand = table.concat( aBuffer, " " )
182
183                         print ( "(info) link: '" .. aCommand .. "'" )
184
185                         os.execute( aCommand )
186                 else
187                         print( "(warning) link: no object names found" )
188                         os.exit( 1 )
189                 end
190         else
191                 print( "(warning) link: no module name found" )
192                 os.exit( 1 )
193         end
194 end
195
196 clean()
197 compile()
198 link()
199 clean( false )
Note: See TracBrowser for help on using the browser.