diff options
Diffstat (limited to 'src/luarocks/build/make.tl')
-rw-r--r-- | src/luarocks/build/make.tl | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/luarocks/build/make.tl b/src/luarocks/build/make.tl new file mode 100644 index 00000000..512357f7 --- /dev/null +++ b/src/luarocks/build/make.tl | |||
@@ -0,0 +1,107 @@ | |||
1 | |||
2 | local type Build = require("luarocks.core.types.build").Build | ||
3 | |||
4 | --- Build back-end for using Makefile-based packages. | ||
5 | local record make | ||
6 | record MakeBuild | ||
7 | is Build where self.type == "make" | ||
8 | |||
9 | makefile: string | ||
10 | build_target: string | ||
11 | build_pass: boolean | ||
12 | install_target: string | ||
13 | install_pass: boolean | ||
14 | build_variables: {string: string} | ||
15 | install_variables: {string: string} | ||
16 | variables: {string: string} | ||
17 | end | ||
18 | end | ||
19 | |||
20 | local fs = require("luarocks.fs") | ||
21 | local util = require("luarocks.util") | ||
22 | local cfg = require("luarocks.core.cfg") | ||
23 | |||
24 | local type Rockspec = require("luarocks.core.types.rockspec").Rockspec | ||
25 | |||
26 | --- Call "make" with given target and variables | ||
27 | -- @param make_cmd string: the make command to be used (typically | ||
28 | -- configured through variables.MAKE in the config files, or | ||
29 | -- the appropriate platform-specific default). | ||
30 | -- @param pass boolean: If true, run make; if false, do nothing. | ||
31 | -- @param target string: The make target; an empty string indicates | ||
32 | -- the default target. | ||
33 | -- @param variables table: A table containing string-string key-value | ||
34 | -- pairs representing variable assignments to be passed to make. | ||
35 | -- @return boolean: false if any errors occurred, true otherwise. | ||
36 | local function make_pass(make_cmd: string, pass: boolean, target: string, variables: {string: string}): boolean, string, string | ||
37 | local assignments: {string} = {} | ||
38 | for k,v in pairs(variables) do | ||
39 | table.insert(assignments, k.."="..v) | ||
40 | end | ||
41 | if pass then | ||
42 | return fs.execute(make_cmd.." "..target, table.unpack(assignments)) | ||
43 | else | ||
44 | return true | ||
45 | end | ||
46 | end | ||
47 | |||
48 | --- Driver function for the "make" build back-end. | ||
49 | -- @param rockspec table: the loaded rockspec. | ||
50 | -- @return boolean or (nil, string): true if no errors occurred, | ||
51 | -- nil and an error message otherwise. | ||
52 | function make.run(rockspec: Rockspec, not_install: boolean): boolean, string, string | ||
53 | |||
54 | local build = rockspec.build as make.MakeBuild | ||
55 | |||
56 | if build.build_pass == nil then build.build_pass = true end | ||
57 | if build.install_pass == nil then build.install_pass = true end | ||
58 | build.build_variables = build.build_variables or {} | ||
59 | build.install_variables = build.install_variables or {} | ||
60 | build.build_target = build.build_target or "" | ||
61 | build.install_target = build.install_target or "install" | ||
62 | local makefile = build.makefile or cfg.makefile | ||
63 | if makefile then | ||
64 | -- Assumes all make's accept -f. True for POSIX make, GNU make and Microsoft nmake. | ||
65 | build.build_target = "-f "..makefile.." "..build.build_target | ||
66 | build.install_target = "-f "..makefile.." "..build.install_target | ||
67 | end | ||
68 | |||
69 | if build.variables then | ||
70 | for var, val in pairs(build.variables) do | ||
71 | build.build_variables[var] = val | ||
72 | build.install_variables[var] = val | ||
73 | end | ||
74 | end | ||
75 | |||
76 | util.warn_if_not_used(build.build_variables, { CFLAGS=true }, "variable %s was not passed in build_variables") | ||
77 | util.variable_substitutions(build.build_variables, rockspec.variables) | ||
78 | util.variable_substitutions(build.install_variables, rockspec.variables) | ||
79 | |||
80 | local auto_variables = { "CC" } | ||
81 | |||
82 | for _, variable in ipairs(auto_variables) do | ||
83 | if not build.build_variables[variable] then | ||
84 | build.build_variables[variable] = rockspec.variables[variable] | ||
85 | end | ||
86 | if not build.install_variables[variable] then | ||
87 | build.install_variables[variable] = rockspec.variables[variable] | ||
88 | end | ||
89 | end | ||
90 | |||
91 | -- backwards compatibility | ||
92 | local make_cmd = cfg.make or rockspec.variables.MAKE | ||
93 | |||
94 | local ok = make_pass(make_cmd, build.build_pass, build.build_target, build.build_variables) | ||
95 | if not ok then | ||
96 | return nil, "Failed building." | ||
97 | end | ||
98 | if not not_install then | ||
99 | ok = make_pass(make_cmd, build.install_pass, build.install_target, build.install_variables) | ||
100 | if not ok then | ||
101 | return nil, "Failed installing." | ||
102 | end | ||
103 | end | ||
104 | return true | ||
105 | end | ||
106 | |||
107 | return make | ||