Skip to content

Commit b6d2057

Browse files
committed
Use cmake --build and cmake --install for MO2 tasks.
1 parent e31d5fa commit b6d2057

File tree

3 files changed

+117
-47
lines changed

3 files changed

+117
-47
lines changed

src/tasks/modorganizer.cpp

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ namespace mob::tasks {
9797

9898
fs::path modorganizer::super_path()
9999
{
100-
return conf().path().build() / "modorganizer_super";
100+
return conf().path().build();
101101
}
102102

103103
url modorganizer::git_url() const
@@ -128,10 +128,6 @@ namespace mob::tasks {
128128
// cmake clean
129129
if (is_set(c, clean::reconfigure))
130130
run_tool(create_cmake_tool(cmake::clean));
131-
132-
// msbuild clean
133-
if (is_set(c, clean::rebuild))
134-
run_tool(create_msbuild_tool(msbuild::clean));
135131
}
136132

137133
void modorganizer::do_fetch()
@@ -155,7 +151,7 @@ namespace mob::tasks {
155151

156152
void modorganizer::do_build_and_install()
157153
{
158-
// adds a git submodule in modorganizer_super for this project; note that
154+
// adds a git submodule in build for this project; note that
159155
// git_submodule_adder runs a thread because adding submodules is slow, but
160156
// can happen while stuff is building
161157
git_submodule_adder::instance().queue(
@@ -177,8 +173,12 @@ namespace mob::tasks {
177173
// run cmake
178174
run_tool(create_cmake_tool());
179175

180-
// run msbuild
181-
run_tool(create_msbuild_tool());
176+
// run cmake --build with default target
177+
// TODO: handle rebuild by adding `--clean-first`
178+
run_tool(cmake(cmake::build).configuration(mob::config::relwithdebinfo));
179+
180+
// run cmake --install
181+
run_tool(cmake(cmake::install).configuration(mob::config::relwithdebinfo));
182182
}
183183

184184
cmake modorganizer::create_cmake_tool(cmake::ops o)
@@ -188,48 +188,23 @@ namespace mob::tasks {
188188

189189
cmake modorganizer::create_cmake_tool(const fs::path& root, cmake::ops o, config c)
190190
{
191+
192+
if (!exists(root / "CMakePresets.json")) {
193+
gcx().bail_out(context::generic, "missing CMakePresets.json in {}",
194+
root.string());
195+
}
196+
191197
cmake g(o);
192198

193199
g.generator(cmake::vs)
194200
.def("CMAKE_INSTALL_PREFIX:PATH", conf().path().install())
195-
.def("CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD", "1")
196-
.def("CMAKE_PREFIX_PATH", conf().path().install() / "lib" / "cmake")
201+
.def("CMAKE_PREFIX_PATH",
202+
conf().path().qt_install().string() + ";" +
203+
(conf().path().install() / "lib" / "cmake").string())
204+
.preset("vs2022-windows")
197205
.root(root);
198206

199-
if (exists(root / "CMakePresets.json")) {
200-
g.preset("vs2022-windows");
201-
}
202-
else {
203-
g.def("DEPENDENCIES_DIR", conf().path().build())
204-
.def("MO2_CMAKE_DEPRECATED_UIBASE_INCLUDE", "ON")
205-
.def("BOOST_ROOT", boost::source_path())
206-
.def("BOOST_LIBRARYDIR", boost::lib_path(arch::x64))
207-
.def("SPDLOG_ROOT", spdlog::source_path())
208-
.def("LOOT_PATH", libloot::source_path())
209-
.def("LZ4_ROOT", lz4::source_path())
210-
.def("QT_ROOT", qt::installation_path())
211-
.def("ZLIB_ROOT", zlib::source_path())
212-
.def("PYTHON_ROOT", python::source_path())
213-
.def("SEVENZ_ROOT", sevenz::source_path())
214-
.def("LIBBSARCH_ROOT", libbsarch::source_path())
215-
.def("BOOST_DI_ROOT", boost_di::source_path())
216-
// gtest has no RelWithDebInfo, so simply use Debug/Release
217-
.def("GTEST_ROOT",
218-
gtest::build_path(arch::x64, c == config::debug ? config::debug
219-
: config::release))
220-
.def("OPENSSL_ROOT_DIR", openssl::source_path())
221-
.def("DIRECTXTEX_ROOT", directxtex::source_path());
222-
}
223-
224207
return std::move(g);
225208
}
226209

227-
msbuild modorganizer::create_msbuild_tool(msbuild::ops o)
228-
{
229-
return std::move(msbuild(o)
230-
.solution(project_file_path())
231-
.configuration(task_conf().configuration())
232-
.architecture(arch::x64));
233-
}
234-
235210
} // namespace mob::tasks

src/tools/cmake.cpp

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,23 @@
44

55
namespace 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

@@ -81,6 +96,24 @@ namespace mob {
8196
return *this;
8297
}
8398

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+
84117
cmake& cmake::cmd(const std::string& s)
85118
{
86119
cmd_ = s;
@@ -116,6 +149,16 @@ namespace mob {
116149
break;
117150
}
118151

152+
case build: {
153+
do_build();
154+
break;
155+
}
156+
157+
case install: {
158+
do_install();
159+
break;
160+
}
161+
119162
default: {
120163
cx().bail_out(context::generic, "bad cmake op {}", op_);
121164
}
@@ -138,9 +181,8 @@ namespace mob {
138181
p = p.arg("--preset").arg(preset_);
139182
}
140183

141-
p = p.arg("-DCMAKE_BUILD_TYPE=Release")
142-
.arg("-DCMAKE_INSTALL_MESSAGE=" +
143-
conf_cmake::to_string(conf().cmake().install_message()))
184+
p = p.arg("-DCMAKE_INSTALL_MESSAGE=" +
185+
conf_cmake::to_string(conf().cmake().install_message()))
144186
.arg("--log-level=ERROR")
145187
.arg("--no-warn-unused-cli");
146188

@@ -180,6 +222,36 @@ namespace mob {
180222
execute_and_join(p);
181223
}
182224

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_));
235+
236+
for (auto& target : targets_) {
237+
p = p.arg("--target").arg(target);
238+
}
239+
240+
execute_and_join(p);
241+
}
242+
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+
183255
void cmake::do_clean()
184256
{
185257
cx().trace(context::rebuild, "deleting all generator directories");

src/tools/cmake.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ namespace mob {
2929
// generates the build files
3030
generate = 1,
3131

32+
// build
33+
build,
34+
35+
// install
36+
install,
37+
3238
// cleans the build files so they're regenerated from scratch
3339
clean
3440
};
@@ -54,6 +60,15 @@ namespace mob {
5460
//
5561
cmake& root(const fs::path& p);
5662

63+
// set the targets for build
64+
//
65+
cmake& targets(const std::string& target);
66+
cmake& targets(const std::vector<std::string>& target);
67+
68+
// set the configuration to build or install
69+
//
70+
cmake& configuration(mob::config config);
71+
5772
// overrides the directory in which cmake will write build files
5873
//
5974
// by default, this is a directory inside what was given in root() with a
@@ -163,6 +178,12 @@ namespace mob {
163178
// passed as -DCMAKE_INSTALL_PREFIX
164179
fs::path prefix_;
165180

181+
// targets
182+
std::vector<std::string> targets_;
183+
184+
// configuration
185+
mob::config config_{mob::config::relwithdebinfo};
186+
166187
// passed verbatim
167188
std::vector<std::string> args_;
168189

@@ -182,6 +203,8 @@ namespace mob {
182203
// runs cmake
183204
//
184205
void do_generate();
206+
void do_build();
207+
void do_install();
185208

186209
// returns a list of generators handled by this tool, same ones as in the
187210
// `generators` enum on top

0 commit comments

Comments
 (0)