Debian Codegraph @

ansible_collections/community/general/plugins/modules/make.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2015, Linus Unnebäck <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: make
11 short_description: Run targets in a Makefile
12 requirements:
13 - make
14 author: Linus Unnebäck (@LinusU) <[email protected]>
15 description:
16 - Run targets in a Makefile.
17 extends_documentation_fragment:
18 - community.general._attributes
19 attributes:
20 check_mode:
21 support: full
22 diff_mode:
23 support: none
24 options:
25 chdir:
26 description:
27 - Change to this directory before running make.
28 type: path
29 required: true
30 file:
31 description:
32 - Use a custom Makefile.
33 type: path
34 jobs:
35 description:
36 - Set the number of make jobs to run concurrently.
37 - Typically if set, this would be the number of processors and/or threads available to the machine.
38 - This is not supported by all make implementations.
39 type: int
40 version_added: 2.0.0
41 make:
42 description:
43 - Use a specific make binary.
44 type: path
45 version_added: '0.2.0'
46 params:
47 description:
48 - Any extra parameters to pass to make.
49 - If the value is empty, only the key is used. For example, V(FOO:) produces V(FOO), not V(FOO=).
50 type: dict
51 target:
52 description:
53 - The target to run.
54 - Typically this would be something like V(install), V(test), or V(all).
55 - O(target) and O(targets) are mutually exclusive.
56 type: str
57 targets:
58 description:
59 - The list of targets to run.
60 - Typically this would be something like V(install), V(test), or V(all).
61 - O(target) and O(targets) are mutually exclusive.
62 type: list
63 elements: str
64 version_added: 7.2.0
65 """
66
67 EXAMPLES = r"""
68 - name: Build the default target
69 community.general.make:
70 chdir: /home/ubuntu/cool-project
71
72 - name: Run 'install' target as root
73 community.general.make:
74 chdir: /home/ubuntu/cool-project
75 target: install
76 become: true
77
78 - name: Build 'all' target with extra arguments
79 community.general.make:
80 chdir: /home/ubuntu/cool-project
81 target: all
82 params:
83 NUM_THREADS: 4
84 BACKEND: lapack
85
86 - name: Build 'all' target with a custom Makefile
87 community.general.make:
88 chdir: /home/ubuntu/cool-project
89 target: all
90 file: /some-project/Makefile
91
92 - name: build arm64 kernel on FreeBSD, with 16 parallel jobs
93 community.general.make:
94 chdir: /usr/src
95 jobs: 16
96 target: buildkernel
97 params:
98 # This adds -DWITH_FDT to the command line:
99 -DWITH_FDT:
100 # The following adds TARGET=arm64 TARGET_ARCH=aarch64 to the command line:
101 TARGET: arm64
102 TARGET_ARCH: aarch64
103 """
104
105 RETURN = r"""
106 chdir:
107 description:
108 - The value of the module parameter O(chdir).
109 type: str
110 returned: success
111 command:
112 description:
113 - The command built and executed by the module.
114 type: str
115 returned: success
116 version_added: 6.5.0
117 file:
118 description:
119 - The value of the module parameter O(file).
120 type: str
121 returned: success
122 jobs:
123 description:
124 - The value of the module parameter O(jobs).
125 type: int
126 returned: success
127 params:
128 description:
129 - The value of the module parameter O(params).
130 type: dict
131 returned: success
132 target:
133 description:
134 - The value of the module parameter O(target).
135 type: str
136 returned: success
137 targets:
138 description:
139 - The value of the module parameter O(targets).
140 type: str
141 returned: success
142 version_added: 7.2.0
143 """
144
145 from shlex import quote as shlex_quote
146
147 from ansible.module_utils.basic import AnsibleModule
148
149
150 def run_command(command, module, check_rc=True):
151 """
152 Run a command using the module, return
153 the result code and std{err,out} content.
154
155 :param command: list of command arguments
156 :param module: Ansible make module instance
157 :return: return code, stdout content, stderr content
158 """
159 rc, out, err = module.run_command(command, check_rc=check_rc, cwd=module.params["chdir"])
160 return rc, sanitize_output(out), sanitize_output(err)
161
162
163 def sanitize_output(output):
164 """
165 Sanitize the output string before we
166 pass it to module.fail_json. Defaults
167 the string to empty if it is None, else
168 strips trailing newlines.
169
170 :param output: output to sanitize
171 :return: sanitized output
172 """
173 if output is None:
174 return ""
175 else:
176 return output.rstrip("\r\n")
177
178
179 def main():
180 module = AnsibleModule(
181 argument_spec=dict(
182 target=dict(type="str"),
183 targets=dict(type="list", elements="str"),
184 params=dict(type="dict"),
185 chdir=dict(type="path", required=True),
186 file=dict(type="path"),
187 make=dict(type="path"),
188 jobs=dict(type="int"),
189 ),
190 mutually_exclusive=[("target", "targets")],
191 supports_check_mode=True,
192 )
193
194 make_path = module.params["make"]
195 if make_path is None:
196 # Build up the invocation of `make` we are going to use
197 # For non-Linux OSes, prefer gmake (GNU make) over make
198 make_path = module.get_bin_path("gmake", required=False)
199 if not make_path:
200 # Fall back to system make
201 make_path = module.get_bin_path("make", required=True)
202 if module.params["params"] is not None:
203 make_parameters = [k + (f"={v!s}" if v is not None else "") for k, v in module.params["params"].items()]
204 else:
205 make_parameters = []
206
207 # build command:
208 # handle any make specific arguments included in params
209 base_command = [make_path]
210 if module.params["jobs"] is not None:
211 jobs = str(module.params["jobs"])
212 base_command.extend(["-j", jobs])
213 if module.params["file"] is not None:
214 base_command.extend(["-f", module.params["file"]])
215
216 # add make target
217 if module.params["target"]:
218 base_command.append(module.params["target"])
219 elif module.params["targets"]:
220 base_command.extend(module.params["targets"])
221
222 # add makefile parameters
223 base_command.extend(make_parameters)
224
225 # Check if the target is already up to date
226 rc, out, err = run_command(base_command + ["-q"], module, check_rc=False)
227 if module.check_mode:
228 # If we've been asked to do a dry run, we only need
229 # to report whether or not the target is up to date
230 changed = rc != 0
231 else:
232 if rc == 0:
233 # The target is up to date, so we don't have to
234 # do anything
235 changed = False
236 else:
237 # The target isn't up to date, so we need to run it
238 rc, out, err = run_command(base_command, module, check_rc=True)
239 changed = True
240
241 # We don't report the return code, as if this module failed
242 # we would be calling fail_json from run_command, so even if
243 # we had a non-zero return code, we did not fail. However, if
244 # we report a non-zero return code here, we will be marked as
245 # failed regardless of what we signal using the failed= kwarg.
246 module.exit_json(
247 changed=changed,
248 failed=False,
249 stdout=out,
250 stderr=err,
251 target=module.params["target"],
252 targets=module.params["targets"],
253 params=module.params["params"],
254 chdir=module.params["chdir"],
255 file=module.params["file"],
256 jobs=module.params["jobs"],
257 command=" ".join([shlex_quote(part) for part in base_command]),
258 )
259
260
261 if __name__ == "__main__":
262 main()