1 | /* Copyright © 2005-2007 Roger Leigh <rleigh@debian.org> |
---|
2 | * |
---|
3 | * schroot is free software: you can redistribute it and/or modify it |
---|
4 | * under the terms of the GNU General Public License as published by |
---|
5 | * the Free Software Foundation, either version 3 of the License, or |
---|
6 | * (at your option) any later version. |
---|
7 | * |
---|
8 | * schroot is distributed in the hope that it will be useful, but |
---|
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
11 | * General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU General Public License |
---|
14 | * along with this program. If not, see |
---|
15 | * <http://www.gnu.org/licenses/>. |
---|
16 | * |
---|
17 | *********************************************************************/ |
---|
18 | |
---|
19 | #include <config.h> |
---|
20 | |
---|
21 | #include "sbuild-log.h" |
---|
22 | |
---|
23 | #include "csbuild-main.h" |
---|
24 | |
---|
25 | #include <cerrno> |
---|
26 | #include <cstdlib> |
---|
27 | #include <ctime> |
---|
28 | #include <iostream> |
---|
29 | #include <locale> |
---|
30 | #include <sstream> |
---|
31 | |
---|
32 | #include <sys/types.h> |
---|
33 | #include <sys/stat.h> |
---|
34 | #include <unistd.h> |
---|
35 | |
---|
36 | #include <boost/format.hpp> |
---|
37 | |
---|
38 | using std::endl; |
---|
39 | using boost::format; |
---|
40 | using sbuild::_; |
---|
41 | using sbuild::N_; |
---|
42 | using namespace csbuild; |
---|
43 | |
---|
44 | namespace |
---|
45 | { |
---|
46 | |
---|
47 | typedef std::pair<main::error_code,const char *> emap; |
---|
48 | |
---|
49 | /** |
---|
50 | * This is a list of the supported error codes. It's used to |
---|
51 | * construct the real error codes map. |
---|
52 | */ |
---|
53 | emap init_errors[] = |
---|
54 | { |
---|
55 | emap(main::DEVICE_NOTBLOCK, N_("File is not a block device")), |
---|
56 | // TRANSLATORS: %4% = integer process ID |
---|
57 | emap(main::DEVICE_OWNED, N_("Failed to release device lock (lock held by PID %4%)")), |
---|
58 | emap(main::DEVICE_RELEASE, N_("Failed to release device lock")), |
---|
59 | emap(main::DEVICE_STAT, N_("Failed to stat device")) |
---|
60 | }; |
---|
61 | |
---|
62 | } |
---|
63 | |
---|
64 | template<> |
---|
65 | sbuild::error<main::error_code>::map_type |
---|
66 | sbuild::error<main::error_code>::error_strings |
---|
67 | (init_errors, |
---|
68 | init_errors + (sizeof(init_errors) / sizeof(init_errors[0]))); |
---|
69 | |
---|
70 | main::main (options::ptr& options): |
---|
71 | schroot_base::main("csbuild", |
---|
72 | // TRANSLATORS: '...' is an ellipsis e.g. U+2026, |
---|
73 | // and '-' is an em-dash. |
---|
74 | _("[OPTION...] - build Debian packages from source"), |
---|
75 | options, |
---|
76 | false), |
---|
77 | opts(options) |
---|
78 | { |
---|
79 | } |
---|
80 | |
---|
81 | main::~main () |
---|
82 | { |
---|
83 | } |
---|
84 | |
---|
85 | void |
---|
86 | main::action_build () |
---|
87 | { |
---|
88 | sbuild::string_list command; |
---|
89 | sbuild::environment env(environ); |
---|
90 | |
---|
91 | /// @todo Make sbuild binary configurable. |
---|
92 | command.push_back("/usr/bin/sbuild"); |
---|
93 | |
---|
94 | /// @todo Handle incrementing debug level. |
---|
95 | if (sbuild::debug_log_level != sbuild::DEBUG_NONE) |
---|
96 | command.push_back("--debug"); |
---|
97 | |
---|
98 | if (opts->nolog) |
---|
99 | command.push_back("--nolog"); |
---|
100 | |
---|
101 | if (opts->batch_mode) |
---|
102 | command.push_back("--batch"); |
---|
103 | |
---|
104 | for (sbuild::string_list::const_iterator pos = opts->deb_build_options.begin(); |
---|
105 | pos != opts->deb_build_options.end(); |
---|
106 | ++pos) |
---|
107 | { |
---|
108 | std::string bopt("--debbuildopt="); |
---|
109 | bopt += *pos; |
---|
110 | command.push_back(bopt); |
---|
111 | } |
---|
112 | |
---|
113 | if (!opts->distribution.empty()) |
---|
114 | { |
---|
115 | std::string dist("--dist="); |
---|
116 | dist += opts->distribution; |
---|
117 | command.push_back(dist); |
---|
118 | } |
---|
119 | |
---|
120 | if (!opts->archive.empty()) |
---|
121 | { |
---|
122 | std::string archive("--archive="); |
---|
123 | archive += opts->archive; |
---|
124 | command.push_back(archive); |
---|
125 | } |
---|
126 | |
---|
127 | if (!opts->build_arch.empty()) |
---|
128 | { |
---|
129 | std::string arch("--arch="); |
---|
130 | arch += opts->build_arch; |
---|
131 | command.push_back(arch); |
---|
132 | } |
---|
133 | |
---|
134 | if (opts->build_arch_all) |
---|
135 | command.push_back("--arch-all"); |
---|
136 | |
---|
137 | if (opts->build_source) |
---|
138 | command.push_back("--source"); |
---|
139 | |
---|
140 | if (opts->force_orig_source) |
---|
141 | command.push_back("--force-orig-source"); |
---|
142 | |
---|
143 | if (opts->bin_nmu) |
---|
144 | { |
---|
145 | std::string binnmu1("--make-binNMU="); |
---|
146 | binnmu1 += opts->bin_nmu_changelog; |
---|
147 | command.push_back(binnmu1); |
---|
148 | |
---|
149 | std::ostringstream binnmu2; |
---|
150 | binnmu2.imbue(std::locale::classic()); |
---|
151 | binnmu2 << "--binNMU="; |
---|
152 | binnmu2 << opts->bin_nmu_version; |
---|
153 | command.push_back(binnmu2.str()); |
---|
154 | } |
---|
155 | |
---|
156 | if (!opts->append_version.empty()) |
---|
157 | { |
---|
158 | std::string append("--append-to-version="); |
---|
159 | append += opts->append_version; |
---|
160 | command.push_back(append); |
---|
161 | } |
---|
162 | |
---|
163 | if (opts->apt_update) |
---|
164 | command.push_back("apt-update"); |
---|
165 | |
---|
166 | if (!opts->chroot.empty()) |
---|
167 | { |
---|
168 | std::string chroot("--chroot="); |
---|
169 | chroot += opts->chroot; |
---|
170 | command.push_back(chroot); |
---|
171 | } |
---|
172 | |
---|
173 | if (!opts->purge_string.empty()) |
---|
174 | { |
---|
175 | std::string purge("--purge="); |
---|
176 | purge += opts->purge_string; |
---|
177 | command.push_back(purge); |
---|
178 | } |
---|
179 | |
---|
180 | if (!opts->purge_deps_string.empty()) |
---|
181 | { |
---|
182 | std::string purge_deps("--purge-deps="); |
---|
183 | purge_deps += opts->purge_deps_string; |
---|
184 | command.push_back(purge_deps); |
---|
185 | } |
---|
186 | |
---|
187 | if (!opts->setup_hook_script.empty()) |
---|
188 | { |
---|
189 | std::string setup_hook("--setup-hook="); |
---|
190 | setup_hook += opts->setup_hook_script; |
---|
191 | command.push_back(setup_hook); |
---|
192 | } |
---|
193 | |
---|
194 | if (!opts->keyid.empty()) |
---|
195 | { |
---|
196 | std::string keyid("--keyid="); |
---|
197 | keyid += opts->keyid; |
---|
198 | command.push_back(keyid); |
---|
199 | } |
---|
200 | |
---|
201 | if (!opts->maintainer.empty()) |
---|
202 | { |
---|
203 | std::string maintainer("--maintainer="); |
---|
204 | maintainer += opts->maintainer; |
---|
205 | command.push_back(maintainer); |
---|
206 | } |
---|
207 | |
---|
208 | if (!opts->uploader.empty()) |
---|
209 | { |
---|
210 | std::string uploader("--uploader="); |
---|
211 | uploader += opts->uploader; |
---|
212 | command.push_back(uploader); |
---|
213 | } |
---|
214 | |
---|
215 | for (sbuild::string_list::const_iterator bd = opts->build_depends.begin(); |
---|
216 | bd != opts->build_depends.end(); |
---|
217 | ++bd) |
---|
218 | { |
---|
219 | std::string dep("--add-depends="); |
---|
220 | dep += *bd; |
---|
221 | command.push_back(dep); |
---|
222 | } |
---|
223 | |
---|
224 | for (sbuild::string_list::const_iterator bc = opts->build_conflicts.begin(); |
---|
225 | bc != opts->build_conflicts.end(); |
---|
226 | ++bc) |
---|
227 | { |
---|
228 | std::string dep("--add-conflicts="); |
---|
229 | dep += *bc; |
---|
230 | command.push_back(dep); |
---|
231 | } |
---|
232 | |
---|
233 | for (sbuild::string_list::const_iterator bdi = opts->build_depends_indep.begin(); |
---|
234 | bdi != opts->build_depends_indep.end(); |
---|
235 | ++bdi) |
---|
236 | { |
---|
237 | std::string dep("--add-depends="); |
---|
238 | dep += *bdi; |
---|
239 | command.push_back(dep); |
---|
240 | } |
---|
241 | |
---|
242 | for (sbuild::string_list::const_iterator bci = opts->build_conflicts_indep.begin(); |
---|
243 | bci != opts->build_conflicts_indep.end(); |
---|
244 | ++bci) |
---|
245 | { |
---|
246 | std::string dep("--add-conflicts="); |
---|
247 | dep += *bci; |
---|
248 | command.push_back(dep); |
---|
249 | } |
---|
250 | |
---|
251 | if (!opts->depends_algorithm.empty()) |
---|
252 | { |
---|
253 | std::string algo("check-depends-algorithm="); |
---|
254 | algo += opts->depends_algorithm; |
---|
255 | command.push_back(algo); |
---|
256 | } |
---|
257 | |
---|
258 | if (opts->gcc_snapshot) |
---|
259 | command.push_back("--use-snapshot"); |
---|
260 | |
---|
261 | std::copy(opts->packages.begin(), opts->packages.end(), |
---|
262 | std::back_inserter(command)); |
---|
263 | |
---|
264 | sbuild::log_debug(sbuild::DEBUG_NOTICE) |
---|
265 | << "command=" |
---|
266 | << sbuild::string_list_to_string(command, ", ") |
---|
267 | << std::endl; |
---|
268 | |
---|
269 | exec(command[0], command, env); |
---|
270 | |
---|
271 | // This should never be reached. |
---|
272 | exit(EXIT_FAILURE); |
---|
273 | } |
---|
274 | |
---|
275 | int |
---|
276 | main::run_impl () |
---|
277 | { |
---|
278 | if (this->opts->action == options::ACTION_HELP) |
---|
279 | action_help(std::cerr); |
---|
280 | else if (this->opts->action == options::ACTION_VERSION) |
---|
281 | action_version(std::cerr); |
---|
282 | else if (this->opts->action == options::ACTION_BUILD) |
---|
283 | action_build(); |
---|
284 | else |
---|
285 | assert(0); // Invalid action. |
---|
286 | |
---|
287 | return EXIT_SUCCESS; |
---|
288 | } |
---|