cmake/Source/cmQTWrapCPPCommand.cxx

91 lines
2.8 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmQTWrapCPPCommand.h"
2022-03-29 21:10:50 +02:00
#include <utility>
#include <cm/memory>
#include "cmCustomCommand.h"
2017-04-14 19:02:05 +02:00
#include "cmCustomCommandLines.h"
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
2019-11-11 23:01:05 +01:00
#include "cmRange.h"
2017-04-14 19:02:05 +02:00
#include "cmSourceFile.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
2020-02-01 23:06:01 +01:00
bool cmQTWrapCPPCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2016-07-09 11:21:54 +02:00
if (args.size() < 3) {
2020-02-01 23:06:01 +01:00
status.SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
cmMakefile& mf = status.GetMakefile();
// Get the moc executable to run in the custom command.
2020-02-01 23:06:01 +01:00
std::string const& moc_exe = mf.GetRequiredDefinition("QT_MOC_EXECUTABLE");
// Get the variable holding the list of sources.
std::string const& sourceList = args[1];
2020-02-01 23:06:01 +01:00
std::string sourceListValue = mf.GetSafeDefinition(sourceList);
// Create a rule for all sources listed.
2019-11-11 23:01:05 +01:00
for (std::string const& arg : cmMakeRange(args).advance(2)) {
2020-02-01 23:06:01 +01:00
cmSourceFile* curr = mf.GetSource(arg);
// if we should wrap the class
2016-07-09 11:21:54 +02:00
if (!(curr && curr->GetPropertyAsBool("WRAP_EXCLUDE"))) {
// Compute the name of the file to generate.
2019-11-11 23:01:05 +01:00
std::string srcName =
cmSystemTools::GetFilenameWithoutLastExtension(arg);
2020-02-01 23:06:01 +01:00
std::string newName =
cmStrCat(mf.GetCurrentBinaryDirectory(), "/moc_", srcName, ".cxx");
cmSourceFile* sf = mf.GetOrCreateSource(newName, true);
2016-07-09 11:21:54 +02:00
if (curr) {
2021-11-20 13:41:27 +01:00
sf->SetProperty("ABSTRACT", curr->GetProperty("ABSTRACT"));
2016-07-09 11:21:54 +02:00
}
// Compute the name of the header from which to generate the file.
std::string hname;
2019-11-11 23:01:05 +01:00
if (cmSystemTools::FileIsFullPath(arg)) {
hname = arg;
2016-07-09 11:21:54 +02:00
} else {
2019-11-11 23:01:05 +01:00
if (curr && curr->GetIsGenerated()) {
2020-02-01 23:06:01 +01:00
hname = mf.GetCurrentBinaryDirectory();
2016-07-09 11:21:54 +02:00
} else {
2020-02-01 23:06:01 +01:00
hname = mf.GetCurrentSourceDirectory();
2016-07-09 11:21:54 +02:00
}
hname += "/";
2019-11-11 23:01:05 +01:00
hname += arg;
2016-07-09 11:21:54 +02:00
}
// Append the generated source file to the list.
2016-07-09 11:21:54 +02:00
if (!sourceListValue.empty()) {
sourceListValue += ";";
2016-07-09 11:21:54 +02:00
}
sourceListValue += newName;
// Create the custom command to generate the file.
2020-02-01 23:06:01 +01:00
cmCustomCommandLines commandLines =
cmMakeSingleCommandLine({ moc_exe, "-o", newName, hname });
std::vector<std::string> depends;
depends.push_back(moc_exe);
depends.push_back(hname);
2022-03-29 21:10:50 +02:00
auto cc = cm::make_unique<cmCustomCommand>();
cc->SetOutputs(newName);
cc->SetDepends(depends);
cc->SetCommandLines(commandLines);
cc->SetComment("Qt Wrapped File");
mf.AddCustomCommandToOutput(std::move(cc));
}
2016-07-09 11:21:54 +02:00
}
// Store the final list of source files.
2020-02-01 23:06:01 +01:00
mf.AddDefinition(sourceList, sourceListValue);
return true;
}