cmake/Source/cmAuxSourceDirectoryCommand.cxx

75 lines
2.3 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 "cmAuxSourceDirectoryCommand.h"
2016-07-09 11:21:54 +02:00
2017-04-14 19:02:05 +02:00
#include <algorithm>
2020-02-01 23:06:01 +01:00
#include <cstddef>
2019-11-11 23:01:05 +01:00
#include <utility>
2017-04-14 19:02:05 +02:00
2020-08-30 11:54:41 +02:00
#include <cm/string_view>
2020-02-01 23:06:01 +01:00
#include "cmsys/Directory.hxx"
#include "cmExecutionStatus.h"
2023-12-07 09:12:54 +01:00
#include "cmList.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
#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"
#include "cmake.h"
2020-02-01 23:06:01 +01:00
bool cmAuxSourceDirectoryCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2017-07-20 19:35:53 +02:00
if (args.size() != 2) {
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
}
2013-03-16 19:13:01 +02:00
2020-02-01 23:06:01 +01:00
cmMakefile& mf = status.GetMakefile();
std::string sourceListValue;
2017-07-20 19:35:53 +02:00
std::string const& templateDirectory = args[0];
std::string tdir;
2018-04-23 21:13:27 +02:00
if (!cmSystemTools::FileIsFullPath(templateDirectory)) {
2020-02-01 23:06:01 +01:00
tdir = cmStrCat(mf.GetCurrentSourceDirectory(), '/', templateDirectory);
2016-07-09 11:21:54 +02:00
} else {
tdir = templateDirectory;
2016-07-09 11:21:54 +02:00
}
// was the list already populated
2021-09-14 00:13:48 +02:00
sourceListValue = mf.GetSafeDefinition(args[1]);
2013-03-16 19:13:01 +02:00
2016-10-30 18:24:19 +01:00
std::vector<std::string> files;
// Load all the files in the directory
cmsys::Directory dir;
2017-04-14 19:02:05 +02:00
if (dir.Load(tdir)) {
size_t numfiles = dir.GetNumberOfFiles();
2016-07-09 11:21:54 +02:00
for (size_t i = 0; i < numfiles; ++i) {
std::string file = dir.GetFile(static_cast<unsigned long>(i));
// Split the filename into base and extension
2017-04-14 19:02:05 +02:00
std::string::size_type dotpos = file.rfind('.');
2016-07-09 11:21:54 +02:00
if (dotpos != std::string::npos) {
2020-08-30 11:54:41 +02:00
auto ext = cm::string_view(file).substr(dotpos + 1);
// Process only source files
2021-09-14 00:13:48 +02:00
auto* cm = mf.GetCMakeInstance();
if (dotpos > 0 && cm->IsACLikeSourceExtension(ext)) {
2020-02-01 23:06:01 +01:00
std::string fullname = cmStrCat(templateDirectory, '/', file);
2013-03-16 19:13:01 +02:00
// add the file as a class file so
// depends can be done
2020-02-01 23:06:01 +01:00
cmSourceFile* sf = mf.GetOrCreateSource(fullname);
2016-07-09 11:21:54 +02:00
sf->SetProperty("ABSTRACT", "0");
2018-04-23 21:13:27 +02:00
files.push_back(std::move(fullname));
}
}
}
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
std::sort(files.begin(), files.end());
if (!sourceListValue.empty()) {
sourceListValue += ";";
}
2023-12-07 09:12:54 +01:00
sourceListValue += cmList::to_string(files);
2020-02-01 23:06:01 +01:00
mf.AddDefinition(args[1], sourceListValue);
return true;
}