blob: 415a1244721a16e21c00e65f2478637adc70b8d8 [file] [log] [blame]
Brad King86578ec2016-09-27 15:01:08 -04001/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
Bill Hoffman8a214932001-05-04 11:34:59 -04003#include "cmBuildCommand.h"
4
Regina Pfeiferf0ecb122019-09-12 10:04:07 +02005#include "cmExecutionStatus.h"
Andy Cedilnik1fff4182005-04-29 12:50:29 -04006#include "cmGlobalGenerator.h"
Daniel Pfeifere81c3232016-10-25 20:35:04 +02007#include "cmMakefile.h"
Bruno Manganellicc2a5262018-11-22 03:36:50 +00008#include "cmMessageType.h"
Daniel Pfeifere81c3232016-10-25 20:35:04 +02009#include "cmStateTypes.h"
Regina Pfeiferf0ecb122019-09-12 10:04:07 +020010#include "cmStringAlgorithms.h"
Daniel Pfeifere81c3232016-10-25 20:35:04 +020011#include "cmSystemTools.h"
Marc Chevriercc56dc72021-09-21 17:12:35 +020012#include "cmValue.h"
Daniel Pfeifere81c3232016-10-25 20:35:04 +020013
Regina Pfeiferf0ecb122019-09-12 10:04:07 +020014namespace {
Andy Cedilnik1fff4182005-04-29 12:50:29 -040015
Regina Pfeiferf0ecb122019-09-12 10:04:07 +020016bool MainSignature(std::vector<std::string> const& args,
17 cmExecutionStatus& status)
David Cole0b38bb42009-12-04 12:09:01 -050018{
Daniel Pfeifer73f648f2016-09-15 23:59:29 +020019 if (args.empty()) {
Regina Pfeiferf0ecb122019-09-12 10:04:07 +020020 status.SetError("requires at least one argument naming a CMake variable");
Bill Hoffman8a214932001-05-04 11:34:59 -040021 return false;
Kitware Robotd9fd2f52016-05-16 10:34:04 -040022 }
David Cole0b38bb42009-12-04 12:09:01 -050023
24 // The cmake variable in which to store the result.
Pavel Solodovnikov86dc86d2017-05-24 23:18:28 +030025 std::string const& variable = args[0];
David Cole0b38bb42009-12-04 12:09:01 -050026
27 // Parse remaining arguments.
Dāvis Mosānsb1f87a52016-07-08 00:54:05 +030028 std::string configuration;
29 std::string project_name;
Ben Boeckelfabf1fb2014-02-06 17:31:47 -050030 std::string target;
friendlyanonfc2ac462021-05-01 16:10:21 +020031 std::string parallel;
Kitware Robotd9fd2f52016-05-16 10:34:04 -040032 enum Doing
33 {
34 DoingNone,
35 DoingConfiguration,
36 DoingProjectName,
friendlyanonfc2ac462021-05-01 16:10:21 +020037 DoingTarget,
38 DoingParallel
Kitware Robotd9fd2f52016-05-16 10:34:04 -040039 };
David Cole0b38bb42009-12-04 12:09:01 -050040 Doing doing = DoingNone;
Kitware Robotd9fd2f52016-05-16 10:34:04 -040041 for (unsigned int i = 1; i < args.size(); ++i) {
42 if (args[i] == "CONFIGURATION") {
David Cole0b38bb42009-12-04 12:09:01 -050043 doing = DoingConfiguration;
Kitware Robotd9fd2f52016-05-16 10:34:04 -040044 } else if (args[i] == "PROJECT_NAME") {
David Cole0b38bb42009-12-04 12:09:01 -050045 doing = DoingProjectName;
Kitware Robotd9fd2f52016-05-16 10:34:04 -040046 } else if (args[i] == "TARGET") {
David Cole0b38bb42009-12-04 12:09:01 -050047 doing = DoingTarget;
friendlyanonfc2ac462021-05-01 16:10:21 +020048 } else if (args[i] == "PARALLEL_LEVEL") {
49 doing = DoingParallel;
Kitware Robotd9fd2f52016-05-16 10:34:04 -040050 } else if (doing == DoingConfiguration) {
David Cole0b38bb42009-12-04 12:09:01 -050051 doing = DoingNone;
Dāvis Mosānsb1f87a52016-07-08 00:54:05 +030052 configuration = args[i];
Kitware Robotd9fd2f52016-05-16 10:34:04 -040053 } else if (doing == DoingProjectName) {
David Cole0b38bb42009-12-04 12:09:01 -050054 doing = DoingNone;
Dāvis Mosānsb1f87a52016-07-08 00:54:05 +030055 project_name = args[i];
Kitware Robotd9fd2f52016-05-16 10:34:04 -040056 } else if (doing == DoingTarget) {
David Cole0b38bb42009-12-04 12:09:01 -050057 doing = DoingNone;
Ben Boeckelfabf1fb2014-02-06 17:31:47 -050058 target = args[i];
friendlyanonfc2ac462021-05-01 16:10:21 +020059 } else if (doing == DoingParallel) {
60 doing = DoingNone;
61 parallel = args[i];
Kitware Robotd9fd2f52016-05-16 10:34:04 -040062 } else {
Regina Pfeiferf0ecb122019-09-12 10:04:07 +020063 status.SetError(cmStrCat("unknown argument \"", args[i], "\""));
David Cole0b38bb42009-12-04 12:09:01 -050064 return false;
David Cole0b38bb42009-12-04 12:09:01 -050065 }
Kitware Robotd9fd2f52016-05-16 10:34:04 -040066 }
David Cole0b38bb42009-12-04 12:09:01 -050067
Brad Kingbca67c72013-11-13 16:01:20 -050068 // If null/empty CONFIGURATION argument, cmake --build uses 'Debug'
David Cole0b38bb42009-12-04 12:09:01 -050069 // in the currently implemented multi-configuration global generators...
70 // so we put this code here to end up with the same default configuration
71 // as the original 2-arg build_command signature:
72 //
Dāvis Mosānsb1f87a52016-07-08 00:54:05 +030073 if (configuration.empty()) {
74 cmSystemTools::GetEnv("CMAKE_CONFIG_TYPE", configuration);
Kitware Robotd9fd2f52016-05-16 10:34:04 -040075 }
Dāvis Mosānsb1f87a52016-07-08 00:54:05 +030076 if (configuration.empty()) {
David Cole0b38bb42009-12-04 12:09:01 -050077 configuration = "Release";
Kitware Robotd9fd2f52016-05-16 10:34:04 -040078 }
David Cole0b38bb42009-12-04 12:09:01 -050079
Regina Pfeiferf0ecb122019-09-12 10:04:07 +020080 cmMakefile& mf = status.GetMakefile();
Dāvis Mosānsb1f87a52016-07-08 00:54:05 +030081 if (!project_name.empty()) {
Regina Pfeiferf0ecb122019-09-12 10:04:07 +020082 mf.IssueMessage(MessageType::AUTHOR_WARNING,
83 "Ignoring PROJECT_NAME option because it has no effect.");
Kitware Robotd9fd2f52016-05-16 10:34:04 -040084 }
David Cole0b38bb42009-12-04 12:09:01 -050085
Regina Pfeiferf0ecb122019-09-12 10:04:07 +020086 std::string makecommand = mf.GetGlobalGenerator()->GenerateCMakeBuildCommand(
friendlyanonfc2ac462021-05-01 16:10:21 +020087 target, configuration, parallel, "", mf.IgnoreErrorsCMP0061());
David Cole0b38bb42009-12-04 12:09:01 -050088
Regina Pfeiferf0ecb122019-09-12 10:04:07 +020089 mf.AddDefinition(variable, makecommand);
David Cole0b38bb42009-12-04 12:09:01 -050090
91 return true;
92}
93
Regina Pfeiferf0ecb122019-09-12 10:04:07 +020094bool TwoArgsSignature(std::vector<std::string> const& args,
95 cmExecutionStatus& status)
David Cole0b38bb42009-12-04 12:09:01 -050096{
Kitware Robotd9fd2f52016-05-16 10:34:04 -040097 if (args.size() < 2) {
Regina Pfeiferf0ecb122019-09-12 10:04:07 +020098 status.SetError("called with less than two arguments");
David Cole0b38bb42009-12-04 12:09:01 -050099 return false;
Kitware Robotd9fd2f52016-05-16 10:34:04 -0400100 }
David Cole0b38bb42009-12-04 12:09:01 -0500101
Regina Pfeiferf0ecb122019-09-12 10:04:07 +0200102 cmMakefile& mf = status.GetMakefile();
103
Pavel Solodovnikov86dc86d2017-05-24 23:18:28 +0300104 std::string const& define = args[0];
Marc Chevriercc56dc72021-09-21 17:12:35 +0200105 cmValue cacheValue = mf.GetDefinition(define);
David Cole0b38bb42009-12-04 12:09:01 -0500106
Dāvis Mosānsb1f87a52016-07-08 00:54:05 +0300107 std::string configType;
108 if (!cmSystemTools::GetEnv("CMAKE_CONFIG_TYPE", configType) ||
109 configType.empty()) {
110 configType = "Release";
Kitware Robotd9fd2f52016-05-16 10:34:04 -0400111 }
David Cole0b38bb42009-12-04 12:09:01 -0500112
Regina Pfeiferf0ecb122019-09-12 10:04:07 +0200113 std::string makecommand = mf.GetGlobalGenerator()->GenerateCMakeBuildCommand(
friendlyanon4dd4e9d2021-05-01 16:05:19 +0200114 "", configType, "", "", mf.IgnoreErrorsCMP0061());
Andy Cedilnik782bef72005-12-01 11:41:00 -0500115
Kitware Robotd9fd2f52016-05-16 10:34:04 -0400116 if (cacheValue) {
Andy Cedilnik1fff4182005-04-29 12:50:29 -0400117 return true;
Kitware Robotd9fd2f52016-05-16 10:34:04 -0400118 }
Vitaly Stakhovsky36a5b3d2020-03-11 09:40:00 -0400119 mf.AddCacheDefinition(define, makecommand,
Regina Pfeiferf0ecb122019-09-12 10:04:07 +0200120 "Command used to build entire project "
121 "from the command line.",
122 cmStateEnums::STRING);
Bill Hoffman8a214932001-05-04 11:34:59 -0400123 return true;
124}
Regina Pfeiferf0ecb122019-09-12 10:04:07 +0200125
126} // namespace
127
128bool cmBuildCommand(std::vector<std::string> const& args,
129 cmExecutionStatus& status)
130{
131 // Support the legacy signature of the command:
132 if (args.size() == 2) {
133 return TwoArgsSignature(args, status);
134 }
135
136 return MainSignature(args, status);
137}