diff options
author | Florian Maushart <FloriansGit@online.ms> | 2018-04-14 20:50:19 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2018-05-25 13:42:20 (GMT) |
commit | 1ab3881ec9e809ac5f6cad5cd84048310b8683e2 (patch) | |
tree | 2b5c7c5a9ad2e38a584de50b5f019056883c3877 /Source/cmakemain.cxx | |
parent | dfc692342831186053bd385605c8a21239e2e77d (diff) | |
download | CMake-1ab3881ec9e809ac5f6cad5cd84048310b8683e2.zip CMake-1ab3881ec9e809ac5f6cad5cd84048310b8683e2.tar.gz CMake-1ab3881ec9e809ac5f6cad5cd84048310b8683e2.tar.bz2 |
cmake: Add options for parallel builds to --build mode
While we already support `cmake --build . -- -j`, the options after `--`
are specific to the native build tool. Add new options `--parallel
[<N>]` and `-j [<N>]` to abstract this and map to the proper option
for the native build tool.
Diffstat (limited to 'Source/cmakemain.cxx')
-rw-r--r-- | Source/cmakemain.cxx | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/Source/cmakemain.cxx b/Source/cmakemain.cxx index b185a1b..9c9f65c 100644 --- a/Source/cmakemain.cxx +++ b/Source/cmakemain.cxx @@ -22,6 +22,8 @@ #if defined(_WIN32) && defined(CMAKE_BUILD_WITH_CMAKE) #include "cmsys/ConsoleBuf.hxx" #endif + +#include <ctype.h> #include <iostream> #include <string.h> #include <string> @@ -49,6 +51,12 @@ static const char* cmDocumentationUsageNote[][2] = { #define CMAKE_BUILD_OPTIONS \ " <dir> = Project binary directory to be built.\n" \ + " -j [<jobs>] --parallel [<jobs>] = Build in parallel using\n" \ + " the given number of jobs. If <jobs> is omitted\n" \ + " the native build tool's default number is used.\n" \ + " The CMAKE_BUILD_PARALLEL_LEVEL environment variable\n" \ + " specifies a default parallel level when this option\n" \ + " is not given.\n" \ " --target <tgt> = Build <tgt> instead of default targets.\n" \ " May only be specified once.\n" \ " --config <cfg> = For multi-configuration tools, choose <cfg>.\n" \ @@ -338,6 +346,7 @@ static int do_build(int ac, char const* const* av) std::cerr << "This cmake does not support --build\n"; return -1; #else + int jobs = cmake::NO_BUILD_PARALLEL_LEVEL; std::string target; std::string config = "Debug"; std::string dir; @@ -348,6 +357,7 @@ static int do_build(int ac, char const* const* av) enum Doing { DoingNone, + DoingJobs, DoingDir, DoingTarget, DoingConfig, @@ -357,6 +367,13 @@ static int do_build(int ac, char const* const* av) for (int i = 2; i < ac; ++i) { if (doing == DoingNative) { nativeOptions.push_back(av[i]); + } else if ((strcmp(av[i], "-j") == 0) || + (strcmp(av[i], "--parallel") == 0)) { + jobs = cmake::DEFAULT_BUILD_PARALLEL_LEVEL; + /* does the next argument start with a number? */ + if ((i + 1 < ac) && (isdigit(*av[i + 1]))) { + doing = DoingJobs; + } } else if (strcmp(av[i], "--target") == 0) { if (!hasTarget) { doing = DoingTarget; @@ -377,6 +394,18 @@ static int do_build(int ac, char const* const* av) doing = DoingNative; } else { switch (doing) { + case DoingJobs: { + unsigned long numJobs = 0; + if (cmSystemTools::StringToULong(av[i], &numJobs)) { + jobs = int(numJobs); + doing = DoingNone; + } else { + std::cerr << "'" << av[i - 1] << "' invalid number '" << av[i] + << "' given.\n\n"; + dir.clear(); + break; + } + } break; case DoingDir: dir = cmSystemTools::CollapseFullPath(av[i]); doing = DoingNone; @@ -396,6 +425,25 @@ static int do_build(int ac, char const* const* av) } } } + + if (jobs == cmake::NO_BUILD_PARALLEL_LEVEL) { + std::string parallel; + if (cmSystemTools::GetEnv("CMAKE_BUILD_PARALLEL_LEVEL", parallel)) { + if (parallel.empty()) { + jobs = cmake::DEFAULT_BUILD_PARALLEL_LEVEL; + } else { + unsigned long numJobs = 0; + if (cmSystemTools::StringToULong(parallel.c_str(), &numJobs)) { + jobs = int(numJobs); + } else { + std::cerr << "'CMAKE_BUILD_PARALLEL_LEVEL' environment variable\n" + << "invalid number '" << parallel << "' given.\n\n"; + dir.clear(); + } + } + } + } + if (dir.empty()) { /* clang-format off */ std::cerr << @@ -410,7 +458,7 @@ static int do_build(int ac, char const* const* av) cmake cm(cmake::RoleInternal); cmSystemTools::SetMessageCallback(cmakemainMessageCallback, &cm); cm.SetProgressCallback(cmakemainProgressCallback, &cm); - return cm.Build(dir, target, config, nativeOptions, clean); + return cm.Build(jobs, dir, target, config, nativeOptions, clean); #endif } |