]> Dogcows Code - chaz/yoink/blob - tools/compile.lua
improved build tools
[chaz/yoink] / tools / compile.lua
1 #!/usr/bin/env lua
2
3 --
4 -- Yoink
5 -- This script wraps around the compiler command to handle the creation of
6 -- dependency files. It works with GCC. The script is based off of Emile
7 -- van Bergen's `ccdeps-gcc' script.
8 --
9
10
11 -- Get the next argument passed to the script.
12 function shift()
13 var = arg[1]
14 table.remove(arg, 1)
15 arguments = string.format("%s %q", tostring(arguments), tostring(var))
16 return var
17 end
18
19
20 -- The compiler command is the first argument.
21 compiler = shift()
22 arguments = ""
23 paths = {}
24
25
26 -- Consume each compiler argument, appending it to the arguments variable.
27 while 0 < #arg do
28 local v = shift()
29 if v == "-o" then
30 product = shift()
31 elseif v:match("%-[xubV]$") then
32 shift() -- Shift an extra one for the option's payload.
33 else
34 path = v:match("([^-].*)/")
35 if path then paths[path] = true end
36 end
37 end
38 if not product then print("No output file specified.") os.exit(1) end
39
40
41 -- Now try the actual compile WITH dependency generation.
42 if os.execute(compiler.." -MD"..arguments) ~= 0 then
43 -- Try it without dependency generation.
44 if os.execute(compiler..arguments) ~= 0 then
45 print("The compile failed. :-(")
46 os.exit(1)
47 end
48 print("The compile succeeded without dependency generation.")
49 os.exit(0)
50 end
51
52
53 -- Remove the old dependency file.
54 dep = product..".d"
55 os.remove(dep)
56
57
58 -- GCC outputs file.d for a product named file.o, either in the current
59 -- directory or in the directory where the source is.
60 depname = product:gsub("^.*/(.*)%.o$", "%1.d")
61 depfiles = {}
62 table.insert(depfiles, depname)
63 for path in pairs(paths) do table.insert(depfiles, path.."/"..depname) end
64
65 for i,gccdep in ipairs(depfiles) do
66 tmpname = gccdep..".tmp"
67 -- Fix up the dependency file with correct paths.
68 if os.execute("test -f "..gccdep) == 0 and os.rename(gccdep, tmpname) then
69 os.execute(string.format("sed -e 's|.*:|%s %s:|' <%s >>%s", product, dep, tmpname, dep))
70 os.execute(string.format("sed -e 's/^.*://' -e 's/^ *//' -e 's/ *\\\\$//' -e 's/$/:/' <%s >>%s", tmpname, dep))
71 os.remove(tmpname)
72 os.exit()
73 end
74 end
75
76 print("Couldn't find the dependency file.")
77 os.exit(1)
78
This page took 0.041466 seconds and 4 git commands to generate.