From 6ad699358b6b421da6c49cf540b30eb8b0a3a996 Mon Sep 17 00:00:00 2001 From: Fred Baksik Date: Mon, 22 Apr 2019 11:01:24 -0400 Subject: cmake: --build -j should not accept 0. Fixes #19059 --- Help/manual/cmake.1.rst | 3 +++ Source/cmakemain.cxx | 24 +++++++++++++++++++--- .../BuildDir--build--parallel-large-result.txt | 1 + .../BuildDir--build--parallel-large-stderr.txt | 3 +++ .../BuildDir--build--parallel-zero-result.txt | 1 + .../BuildDir--build--parallel-zero-stderr.txt | 3 +++ .../BuildDir--build-jobs-large-result.txt | 1 + .../BuildDir--build-jobs-large-stderr.txt | 3 +++ .../BuildDir--build-jobs-zero-result.txt | 1 + .../BuildDir--build-jobs-zero-stderr.txt | 3 +++ Tests/RunCMake/CommandLine/RunCMakeTest.cmake | 8 ++++++++ 11 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 Tests/RunCMake/CommandLine/BuildDir--build--parallel-large-result.txt create mode 100644 Tests/RunCMake/CommandLine/BuildDir--build--parallel-large-stderr.txt create mode 100644 Tests/RunCMake/CommandLine/BuildDir--build--parallel-zero-result.txt create mode 100644 Tests/RunCMake/CommandLine/BuildDir--build--parallel-zero-stderr.txt create mode 100644 Tests/RunCMake/CommandLine/BuildDir--build-jobs-large-result.txt create mode 100644 Tests/RunCMake/CommandLine/BuildDir--build-jobs-large-stderr.txt create mode 100644 Tests/RunCMake/CommandLine/BuildDir--build-jobs-zero-result.txt create mode 100644 Tests/RunCMake/CommandLine/BuildDir--build-jobs-zero-stderr.txt diff --git a/Help/manual/cmake.1.rst b/Help/manual/cmake.1.rst index 5b88694..ba47885 100644 --- a/Help/manual/cmake.1.rst +++ b/Help/manual/cmake.1.rst @@ -280,6 +280,9 @@ following options: The :envvar:`CMAKE_BUILD_PARALLEL_LEVEL` environment variable, if set, specifies a default parallel level when this option is not given. + Some native build tools always build in parallel. The use of ```` + value of ``1`` can be used to limit to a single job. + ``--target ..., -t ...`` Build ```` instead of default targets. May be specified multiple times. diff --git a/Source/cmakemain.cxx b/Source/cmakemain.cxx index d70c9d9..d96d956 100644 --- a/Source/cmakemain.cxx +++ b/Source/cmakemain.cxx @@ -24,6 +24,7 @@ #endif #include +#include #include #include #include @@ -69,7 +70,7 @@ static const char* cmDocumentationUsageNote[][2] = { " --config = For multi-configuration tools, choose .\n" \ " --clean-first = Build target 'clean' first, then build.\n" \ " (To clean only, use --target 'clean'.)\n" \ - " --verbose, -v = Enable verbose output - if supported - including\n" \ + " --verbose, -v = Enable verbose output - if supported - including\n" \ " the build commands to be executed. \n" \ " -- = Pass remaining options to the native tool.\n" @@ -393,7 +394,14 @@ int extract_job_number(int& index, char const* current, char const* next, if (jobString.empty()) { jobs = cmake::DEFAULT_BUILD_PARALLEL_LEVEL; } else if (cmSystemTools::StringToULong(jobString.c_str(), &numJobs)) { - jobs = int(numJobs); + if (numJobs == 0) { + std::cerr + << "The value requires a positive integer argument.\n\n"; + } else if (numJobs > INT_MAX) { + std::cerr << "The value is too large.\n\n"; + } else { + jobs = int(numJobs); + } } else { std::cerr << "'" << command.substr(0, len_of_flag) << "' invalid number '" << jobString << "' given.\n\n"; @@ -505,7 +513,17 @@ static int do_build(int ac, char const* const* av) } else { unsigned long numJobs = 0; if (cmSystemTools::StringToULong(parallel.c_str(), &numJobs)) { - jobs = int(numJobs); + if (numJobs == 0) { + std::cerr << "The CMAKE_BUILD_PARALLEL_LEVEL environment variable " + "requires a positive integer argument.\n\n"; + dir.clear(); + } else if (numJobs > INT_MAX) { + std::cerr << "The CMAKE_BUILD_PARALLEL_LEVEL environment variable " + "is too large.\n\n"; + dir.clear(); + } else { + jobs = int(numJobs); + } } else { std::cerr << "'CMAKE_BUILD_PARALLEL_LEVEL' environment variable\n" << "invalid number '" << parallel << "' given.\n\n"; diff --git a/Tests/RunCMake/CommandLine/BuildDir--build--parallel-large-result.txt b/Tests/RunCMake/CommandLine/BuildDir--build--parallel-large-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/CommandLine/BuildDir--build--parallel-large-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CommandLine/BuildDir--build--parallel-large-stderr.txt b/Tests/RunCMake/CommandLine/BuildDir--build--parallel-large-stderr.txt new file mode 100644 index 0000000..94fc157 --- /dev/null +++ b/Tests/RunCMake/CommandLine/BuildDir--build--parallel-large-stderr.txt @@ -0,0 +1,3 @@ +^The value is too large\. ++ +Usage: cmake --build \[options\] \[-- \[native-options\]\] diff --git a/Tests/RunCMake/CommandLine/BuildDir--build--parallel-zero-result.txt b/Tests/RunCMake/CommandLine/BuildDir--build--parallel-zero-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/CommandLine/BuildDir--build--parallel-zero-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CommandLine/BuildDir--build--parallel-zero-stderr.txt b/Tests/RunCMake/CommandLine/BuildDir--build--parallel-zero-stderr.txt new file mode 100644 index 0000000..8ed4fee --- /dev/null +++ b/Tests/RunCMake/CommandLine/BuildDir--build--parallel-zero-stderr.txt @@ -0,0 +1,3 @@ +^The value requires a positive integer argument\. ++ +Usage: cmake --build \[options\] \[-- \[native-options\]\] diff --git a/Tests/RunCMake/CommandLine/BuildDir--build-jobs-large-result.txt b/Tests/RunCMake/CommandLine/BuildDir--build-jobs-large-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/CommandLine/BuildDir--build-jobs-large-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CommandLine/BuildDir--build-jobs-large-stderr.txt b/Tests/RunCMake/CommandLine/BuildDir--build-jobs-large-stderr.txt new file mode 100644 index 0000000..94fc157 --- /dev/null +++ b/Tests/RunCMake/CommandLine/BuildDir--build-jobs-large-stderr.txt @@ -0,0 +1,3 @@ +^The value is too large\. ++ +Usage: cmake --build \[options\] \[-- \[native-options\]\] diff --git a/Tests/RunCMake/CommandLine/BuildDir--build-jobs-zero-result.txt b/Tests/RunCMake/CommandLine/BuildDir--build-jobs-zero-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/CommandLine/BuildDir--build-jobs-zero-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CommandLine/BuildDir--build-jobs-zero-stderr.txt b/Tests/RunCMake/CommandLine/BuildDir--build-jobs-zero-stderr.txt new file mode 100644 index 0000000..8ed4fee --- /dev/null +++ b/Tests/RunCMake/CommandLine/BuildDir--build-jobs-zero-stderr.txt @@ -0,0 +1,3 @@ +^The value requires a positive integer argument\. ++ +Usage: cmake --build \[options\] \[-- \[native-options\]\] diff --git a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake index ea749ea..49d3608 100644 --- a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake +++ b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake @@ -149,6 +149,14 @@ function(run_BuildDir) ${CMAKE_COMMAND} --build BuildDir-build --parallel2) run_cmake_command(BuildDir--build--parallel-no-space-good-number-trailing--target ${CMAKE_COMMAND} -E chdir .. ${CMAKE_COMMAND} --build BuildDir-build --parallel2 --target CustomTarget) + run_cmake_command(BuildDir--build-jobs-zero ${CMAKE_COMMAND} -E chdir .. + ${CMAKE_COMMAND} --build BuildDir-build -j 0) + run_cmake_command(BuildDir--build--parallel-zero ${CMAKE_COMMAND} -E chdir .. + ${CMAKE_COMMAND} --build BuildDir-build --parallel 0) + run_cmake_command(BuildDir--build-jobs-large ${CMAKE_COMMAND} -E chdir .. + ${CMAKE_COMMAND} --build BuildDir-build -j 4294967293) + run_cmake_command(BuildDir--build--parallel-large ${CMAKE_COMMAND} -E chdir .. + ${CMAKE_COMMAND} --build BuildDir-build --parallel 4294967293) # No default jobs for Xcode and FreeBSD build command if(NOT RunCMake_GENERATOR MATCHES "Xcode" AND NOT CMAKE_SYSTEM_NAME MATCHES "FreeBSD") -- cgit v0.12