44
55namespace mob {
66
7+ namespace {
8+ std::string config_to_string (config c)
9+ {
10+ switch (c) {
11+ case config::debug:
12+ return " Debug" ;
13+ case config::release:
14+ return " Release" ;
15+ case config::relwithdebinfo:
16+ return " RelWithDebInfo" ;
17+ }
18+ gcx ().bail_out (context::generic, " unknow configuration type {}" , c);
19+ }
20+ } // namespace
21+
722 cmake::cmake (ops o)
8- : basic_process_runner(" cmake" ), op_(o), gen_(jom ), arch_(arch::def)
23+ : basic_process_runner(" cmake" ), op_(o), gen_(vs ), arch_(arch::def)
924 {
1025 }
1126
@@ -62,6 +77,12 @@ namespace mob {
6277 return *this ;
6378 }
6479
80+ cmake& cmake::preset (const std::string& s)
81+ {
82+ preset_ = s;
83+ return *this ;
84+ }
85+
6586 cmake& cmake::arg (std::string s)
6687 {
6788 std::replace (s.begin (), s.end (), ' \\ ' , ' /' );
@@ -75,6 +96,24 @@ namespace mob {
7596 return *this ;
7697 }
7798
99+ cmake& cmake::targets (const std::string& target)
100+ {
101+ targets_ = {target};
102+ return *this ;
103+ }
104+
105+ cmake& cmake::targets (const std::vector<std::string>& targets)
106+ {
107+ targets_ = targets;
108+ return *this ;
109+ }
110+
111+ cmake& cmake::configuration (mob::config config)
112+ {
113+ config_ = config;
114+ return *this ;
115+ }
116+
78117 cmake& cmake::cmd (const std::string& s)
79118 {
80119 cmd_ = s;
@@ -110,6 +149,16 @@ namespace mob {
110149 break ;
111150 }
112151
152+ case build: {
153+ do_build ();
154+ break ;
155+ }
156+
157+ case install: {
158+ do_install ();
159+ break ;
160+ }
161+
113162 default : {
114163 cx ().bail_out (context::generic, " bad cmake op {}" , op_);
115164 }
@@ -126,43 +175,83 @@ namespace mob {
126175 auto p = process ()
127176 .stdout_encoding (encodings::utf8)
128177 .stderr_encoding (encodings::utf8)
129- .binary (binary ())
130- .arg (" -DCMAKE_BUILD_TYPE=Release" )
131- .arg (" -DCMAKE_INSTALL_MESSAGE=" +
132- conf_cmake::to_string (conf ().cmake ().install_message ()))
133- .arg (" --log-level=ERROR" )
134- .arg (" --no-warn-unused-cli" );
135-
136- if (genstring_.empty ()) {
137- // there's always a generator name, but some generators don't need
138- // an architecture flag, like jom, so get_arch() might return an empty
139- // string
140- p.arg (" -G" , " \" " + g.name + " \" " )
141- .arg (g.get_arch (arch_))
142- .arg (g.get_host (conf ().cmake ().host ()));
143- }
144- else {
145- // verbatim generator string
146- p.arg (" -G" , " \" " + genstring_ + " \" " );
178+ .binary (binary ());
179+
180+ if (!preset_.empty ()) {
181+ p = p.arg (" --preset" ).arg (preset_);
147182 }
148183
184+ p = p.arg (" -DCMAKE_INSTALL_MESSAGE=" +
185+ conf_cmake::to_string (conf ().cmake ().install_message ()))
186+ .arg (" --log-level=ERROR" )
187+ .arg (" --no-warn-unused-cli" );
188+
149189 // prefix
150190 if (!prefix_.empty ())
151191 p.arg (" -DCMAKE_INSTALL_PREFIX=" , prefix_);
152192
153193 p.args (args_);
154194
155- // `..` by default, overriden by cmd()
156- if (cmd_.empty ())
157- p.arg (" .." );
158- else
159- p.arg (cmd_);
195+ if (preset_.empty ()) {
196+
197+ if (genstring_.empty ()) {
198+ // there's always a generator name, but some generators don't need
199+ // an architecture flag, like jom, so get_arch() might return an empty
200+ // string
201+ p.arg (" -G" , " \" " + g.name + " \" " )
202+ .arg (g.get_arch (arch_))
203+ .arg (g.get_host (conf ().cmake ().host ()));
204+ }
205+ else {
206+ // verbatim generator string
207+ p.arg (" -G" , " \" " + genstring_ + " \" " );
208+ }
209+
210+ // `..` by default, overriden by cmd()
211+ if (cmd_.empty ())
212+ p.arg (" .." );
213+ else
214+ p.arg (cmd_);
215+ }
216+
217+ p.env (env::vs (arch_)
218+ .set (" CXXFLAGS" , " /wd4566" )
219+ .set (" VCPKG_ROOT" , absolute (conf ().path ().vcpkg ()).string ()))
220+ .cwd (preset_.empty () ? build_path () : root_);
221+
222+ execute_and_join (p);
223+ }
224+
225+ void cmake::do_build ()
226+ {
227+ auto p = process ()
228+ .stdout_encoding (encodings::utf8)
229+ .stderr_encoding (encodings::utf8)
230+ .binary (binary ())
231+ .arg (" --build" )
232+ .arg (build_path ())
233+ .arg (" --config" )
234+ .arg (config_to_string (config_));
160235
161- p.env (env::vs (arch_).set (" CXXFLAGS" , " /wd4566" )).cwd (build_path ());
236+ for (auto & target : targets_) {
237+ p = p.arg (" --target" ).arg (target);
238+ }
162239
163240 execute_and_join (p);
164241 }
165242
243+ void cmake::do_install ()
244+ {
245+ execute_and_join (process ()
246+ .stdout_encoding (encodings::utf8)
247+ .stderr_encoding (encodings::utf8)
248+ .binary (binary ())
249+ .arg (" --install" )
250+ .arg (build_path ())
251+ .arg (" --config" )
252+ .arg (config_to_string (config_)));
253+ }
254+
166255 void cmake::do_clean ()
167256 {
168257 cx ().trace (context::rebuild, " deleting all generator directories" );
0 commit comments