Skip to content

Commit

Permalink
Use cmake --build and cmake --install for MO2 tasks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Holt59 committed Jul 12, 2024
1 parent e31d5fa commit b6d2057
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 47 deletions.
61 changes: 18 additions & 43 deletions src/tasks/modorganizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ namespace mob::tasks {

fs::path modorganizer::super_path()
{
return conf().path().build() / "modorganizer_super";
return conf().path().build();
}

url modorganizer::git_url() const
Expand Down Expand Up @@ -128,10 +128,6 @@ namespace mob::tasks {
// cmake clean
if (is_set(c, clean::reconfigure))
run_tool(create_cmake_tool(cmake::clean));

// msbuild clean
if (is_set(c, clean::rebuild))
run_tool(create_msbuild_tool(msbuild::clean));
}

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

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

// run msbuild
run_tool(create_msbuild_tool());
// run cmake --build with default target
// TODO: handle rebuild by adding `--clean-first`
run_tool(cmake(cmake::build).configuration(mob::config::relwithdebinfo));

// run cmake --install
run_tool(cmake(cmake::install).configuration(mob::config::relwithdebinfo));
}

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

cmake modorganizer::create_cmake_tool(const fs::path& root, cmake::ops o, config c)
{

if (!exists(root / "CMakePresets.json")) {
gcx().bail_out(context::generic, "missing CMakePresets.json in {}",
root.string());
}

cmake g(o);

g.generator(cmake::vs)
.def("CMAKE_INSTALL_PREFIX:PATH", conf().path().install())
.def("CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD", "1")
.def("CMAKE_PREFIX_PATH", conf().path().install() / "lib" / "cmake")
.def("CMAKE_PREFIX_PATH",
conf().path().qt_install().string() + ";" +
(conf().path().install() / "lib" / "cmake").string())
.preset("vs2022-windows")
.root(root);

if (exists(root / "CMakePresets.json")) {
g.preset("vs2022-windows");
}
else {
g.def("DEPENDENCIES_DIR", conf().path().build())
.def("MO2_CMAKE_DEPRECATED_UIBASE_INCLUDE", "ON")
.def("BOOST_ROOT", boost::source_path())
.def("BOOST_LIBRARYDIR", boost::lib_path(arch::x64))
.def("SPDLOG_ROOT", spdlog::source_path())
.def("LOOT_PATH", libloot::source_path())
.def("LZ4_ROOT", lz4::source_path())
.def("QT_ROOT", qt::installation_path())
.def("ZLIB_ROOT", zlib::source_path())
.def("PYTHON_ROOT", python::source_path())
.def("SEVENZ_ROOT", sevenz::source_path())
.def("LIBBSARCH_ROOT", libbsarch::source_path())
.def("BOOST_DI_ROOT", boost_di::source_path())
// gtest has no RelWithDebInfo, so simply use Debug/Release
.def("GTEST_ROOT",
gtest::build_path(arch::x64, c == config::debug ? config::debug
: config::release))
.def("OPENSSL_ROOT_DIR", openssl::source_path())
.def("DIRECTXTEX_ROOT", directxtex::source_path());
}

return std::move(g);
}

msbuild modorganizer::create_msbuild_tool(msbuild::ops o)
{
return std::move(msbuild(o)
.solution(project_file_path())
.configuration(task_conf().configuration())
.architecture(arch::x64));
}

} // namespace mob::tasks
80 changes: 76 additions & 4 deletions src/tools/cmake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,23 @@

namespace mob {

namespace {
std::string config_to_string(config c)
{
switch (c) {
case config::debug:
return "Debug";
case config::release:
return "Release";
case config::relwithdebinfo:
return "RelWithDebInfo";
}
gcx().bail_out(context::generic, "unknow configuration type {}", c);
}
} // namespace

cmake::cmake(ops o)
: basic_process_runner("cmake"), op_(o), gen_(jom), arch_(arch::def)
: basic_process_runner("cmake"), op_(o), gen_(vs), arch_(arch::def)
{
}

Expand Down Expand Up @@ -81,6 +96,24 @@ namespace mob {
return *this;
}

cmake& cmake::targets(const std::string& target)
{
targets_ = {target};
return *this;
}

cmake& cmake::targets(const std::vector<std::string>& targets)
{
targets_ = targets;
return *this;
}

cmake& cmake::configuration(mob::config config)
{
config_ = config;
return *this;
}

cmake& cmake::cmd(const std::string& s)
{
cmd_ = s;
Expand Down Expand Up @@ -116,6 +149,16 @@ namespace mob {
break;
}

case build: {
do_build();
break;
}

case install: {
do_install();
break;
}

default: {
cx().bail_out(context::generic, "bad cmake op {}", op_);
}
Expand All @@ -138,9 +181,8 @@ namespace mob {
p = p.arg("--preset").arg(preset_);
}

p = p.arg("-DCMAKE_BUILD_TYPE=Release")
.arg("-DCMAKE_INSTALL_MESSAGE=" +
conf_cmake::to_string(conf().cmake().install_message()))
p = p.arg("-DCMAKE_INSTALL_MESSAGE=" +
conf_cmake::to_string(conf().cmake().install_message()))
.arg("--log-level=ERROR")
.arg("--no-warn-unused-cli");

Expand Down Expand Up @@ -180,6 +222,36 @@ namespace mob {
execute_and_join(p);
}

void cmake::do_build()
{
auto p = process()
.stdout_encoding(encodings::utf8)
.stderr_encoding(encodings::utf8)
.binary(binary())
.arg("--build")
.arg(build_path())
.arg("--config")
.arg(config_to_string(config_));

for (auto& target : targets_) {
p = p.arg("--target").arg(target);
}

execute_and_join(p);
}

void cmake::do_install()
{
execute_and_join(process()
.stdout_encoding(encodings::utf8)
.stderr_encoding(encodings::utf8)
.binary(binary())
.arg("--install")
.arg(build_path())
.arg("--config")
.arg(config_to_string(config_)));
}

void cmake::do_clean()
{
cx().trace(context::rebuild, "deleting all generator directories");
Expand Down
23 changes: 23 additions & 0 deletions src/tools/cmake.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ namespace mob {
// generates the build files
generate = 1,

// build
build,

// install
install,

// cleans the build files so they're regenerated from scratch
clean
};
Expand All @@ -54,6 +60,15 @@ namespace mob {
//
cmake& root(const fs::path& p);

// set the targets for build
//
cmake& targets(const std::string& target);
cmake& targets(const std::vector<std::string>& target);

// set the configuration to build or install
//
cmake& configuration(mob::config config);

// overrides the directory in which cmake will write build files
//
// by default, this is a directory inside what was given in root() with a
Expand Down Expand Up @@ -163,6 +178,12 @@ namespace mob {
// passed as -DCMAKE_INSTALL_PREFIX
fs::path prefix_;

// targets
std::vector<std::string> targets_;

// configuration
mob::config config_{mob::config::relwithdebinfo};

// passed verbatim
std::vector<std::string> args_;

Expand All @@ -182,6 +203,8 @@ namespace mob {
// runs cmake
//
void do_generate();
void do_build();
void do_install();

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

0 comments on commit b6d2057

Please sign in to comment.