cmake/Source/cmSourceFile.cxx

483 lines
15 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 "cmSourceFile.h"
2019-11-11 23:01:05 +01:00
#include <utility>
2017-04-14 19:02:05 +02:00
#include "cmGlobalGenerator.h"
2020-02-01 23:06:01 +01:00
#include "cmListFileCache.h"
#include "cmMakefile.h"
2019-11-11 23:01:05 +01:00
#include "cmMessageType.h"
2021-09-14 00:13:48 +02:00
#include "cmPolicies.h"
2016-10-30 18:24:19 +01:00
#include "cmProperty.h"
#include "cmState.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
#include "cmake.h"
2018-04-23 21:13:27 +02:00
cmSourceFile::cmSourceFile(cmMakefile* mf, const std::string& name,
2021-09-14 00:13:48 +02:00
bool generated, cmSourceFileLocationKind kind)
: Location(mf, name, (!generated) ? kind : cmSourceFileLocationKind::Known)
{
2021-09-14 00:13:48 +02:00
if (generated) {
this->MarkAsGenerated();
}
}
std::string const& cmSourceFile::GetExtension() const
{
return this->Extension;
}
2021-09-14 00:13:48 +02:00
const std::string propTRUE = "1";
const std::string propFALSE = "0";
2015-04-27 22:25:09 +02:00
const std::string cmSourceFile::propLANGUAGE = "LANGUAGE";
2019-11-11 23:01:05 +01:00
const std::string cmSourceFile::propLOCATION = "LOCATION";
const std::string cmSourceFile::propGENERATED = "GENERATED";
2020-02-01 23:06:01 +01:00
const std::string cmSourceFile::propCOMPILE_DEFINITIONS =
"COMPILE_DEFINITIONS";
const std::string cmSourceFile::propCOMPILE_OPTIONS = "COMPILE_OPTIONS";
const std::string cmSourceFile::propINCLUDE_DIRECTORIES =
"INCLUDE_DIRECTORIES";
2015-04-27 22:25:09 +02:00
void cmSourceFile::SetObjectLibrary(std::string const& objlib)
{
this->ObjectLibrary = objlib;
}
std::string cmSourceFile::GetObjectLibrary() const
{
return this->ObjectLibrary;
}
2020-02-01 23:06:01 +01:00
std::string const& cmSourceFile::GetOrDetermineLanguage()
{
// If the language was set explicitly by the user then use it.
2021-11-20 13:41:27 +01:00
if (cmValue lang = this->GetProperty(propLANGUAGE)) {
2020-02-01 23:06:01 +01:00
// Assign to member in order to return a reference.
2020-08-30 11:54:41 +02:00
this->Language = *lang;
2020-02-01 23:06:01 +01:00
return this->Language;
2016-07-09 11:21:54 +02:00
}
// Perform computation needed to get the language if necessary.
2021-09-14 00:13:48 +02:00
if (this->Language.empty()) {
// If a known extension is given or a known full path is given then trust
// that the current extension is sufficient to determine the language. This
// will fail only if the user specifies a full path to the source but
// leaves off the extension, which is kind of weird.
if (this->FullPath.empty() && this->Location.ExtensionIsAmbiguous() &&
2016-07-09 11:21:54 +02:00
this->Location.DirectoryIsAmbiguous()) {
2021-09-14 00:13:48 +02:00
// Finalize the file location to get the extension and set the language.
2020-02-01 23:06:01 +01:00
this->ResolveFullPath();
2016-07-09 11:21:54 +02:00
} else {
// Use the known extension to get the language if possible.
std::string ext =
cmSystemTools::GetFilenameLastExtension(this->Location.GetName());
this->CheckLanguage(ext);
}
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
// Use the language determined from the file extension.
return this->Language;
}
2015-04-27 22:25:09 +02:00
std::string cmSourceFile::GetLanguage() const
{
// If the language was set explicitly by the user then use it.
2021-11-20 13:41:27 +01:00
if (cmValue lang = this->GetProperty(propLANGUAGE)) {
2020-08-30 11:54:41 +02:00
return *lang;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
// Use the language determined from the file extension.
return this->Language;
}
cmSourceFileLocation const& cmSourceFile::GetLocation() const
{
2016-07-09 11:21:54 +02:00
return this->Location;
}
2021-09-14 00:13:48 +02:00
std::string const& cmSourceFile::ResolveFullPath(std::string* error,
std::string* cmp0115Warning)
{
2016-07-09 11:21:54 +02:00
if (this->FullPath.empty()) {
2021-09-14 00:13:48 +02:00
if (this->FindFullPath(error, cmp0115Warning)) {
this->CheckExtension();
}
2016-07-09 11:21:54 +02:00
}
return this->FullPath;
}
std::string const& cmSourceFile::GetFullPath() const
{
return this->FullPath;
}
2021-09-14 00:13:48 +02:00
bool cmSourceFile::FindFullPath(std::string* error,
std::string* cmp0115Warning)
{
2019-11-11 23:01:05 +01:00
// If the file is generated compute the location without checking on disk.
2021-09-14 00:13:48 +02:00
// Note: We also check for a locally set GENERATED property, because
// it might have been set before policy CMP0118 was set to NEW.
if (this->GetIsGenerated(CheckScope::GlobalAndLocal)) {
// The file is either already a full path or is relative to the
// build directory for the target.
this->Location.DirectoryUseBinary();
2019-11-11 23:01:05 +01:00
this->FullPath = this->Location.GetFullPath();
2021-09-14 00:13:48 +02:00
this->FindFullPathFailed = false;
return true;
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
// If this method has already failed once do not try again.
if (this->FindFullPathFailed) {
return false;
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
2019-11-11 23:01:05 +01:00
// The file is not generated. It must exist on disk.
cmMakefile const* makefile = this->Location.GetMakefile();
// Location path
2020-08-30 11:54:41 +02:00
std::string const& lPath = this->Location.GetFullPath();
2019-11-11 23:01:05 +01:00
// List of extension lists
2021-09-14 00:13:48 +02:00
std::vector<std::string> exts =
makefile->GetCMakeInstance()->GetAllExtensions();
auto cmp0115 = makefile->GetPolicyStatus(cmPolicies::CMP0115);
auto cmp0118 = makefile->GetPolicyStatus(cmPolicies::CMP0118);
bool const cmp0118new =
cmp0118 != cmPolicies::OLD && cmp0118 != cmPolicies::WARN;
2019-11-11 23:01:05 +01:00
// Tries to find the file in a given directory
2021-09-14 00:13:48 +02:00
auto findInDir = [this, &exts, &lPath, cmp0115, cmp0115Warning, cmp0118new,
makefile](std::string const& dir) -> bool {
2019-11-11 23:01:05 +01:00
// Compute full path
std::string const fullPath = cmSystemTools::CollapseFullPath(lPath, dir);
// Try full path
2021-09-14 00:13:48 +02:00
if (cmp0118new &&
makefile->GetGlobalGenerator()->IsGeneratedFile(fullPath)) {
this->IsGenerated = true;
}
if (this->IsGenerated || cmSystemTools::FileExists(fullPath)) {
2019-11-11 23:01:05 +01:00
this->FullPath = fullPath;
return true;
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
// This has to be an if statement due to a bug in Oracle Developer Studio.
// See https://community.oracle.com/tech/developers/discussion/4476246/
// for details.
if (cmp0115 == cmPolicies::OLD || cmp0115 == cmPolicies::WARN) {
// Try full path with extension
for (std::string const& ext : exts) {
2019-11-11 23:01:05 +01:00
if (!ext.empty()) {
2020-02-01 23:06:01 +01:00
std::string extPath = cmStrCat(fullPath, '.', ext);
2021-09-14 00:13:48 +02:00
if (cmp0118new &&
makefile->GetGlobalGenerator()->IsGeneratedFile(extPath)) {
this->IsGenerated = true;
}
if (this->IsGenerated || cmSystemTools::FileExists(extPath)) {
2019-11-11 23:01:05 +01:00
this->FullPath = extPath;
2021-09-14 00:13:48 +02:00
if (cmp0115 == cmPolicies::WARN) {
std::string warning =
cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0115),
"\nFile:\n ", extPath);
if (cmp0115Warning) {
*cmp0115Warning = std::move(warning);
} else {
makefile->GetCMakeInstance()->IssueMessage(
MessageType::AUTHOR_WARNING, warning);
}
}
2019-11-11 23:01:05 +01:00
return true;
}
}
}
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
// File not found
return false;
};
// Try to find the file in various directories
if (this->Location.DirectoryIsAmbiguous()) {
if (findInDir(makefile->GetCurrentSourceDirectory()) ||
findInDir(makefile->GetCurrentBinaryDirectory())) {
return true;
}
} else {
if (findInDir({})) {
return true;
}
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
// Compose error
2021-09-14 00:13:48 +02:00
std::string err = cmStrCat("Cannot find source file:\n ", lPath);
switch (cmp0115) {
case cmPolicies::OLD:
case cmPolicies::WARN:
err = cmStrCat(err, "\nTried extensions");
for (auto const& ext : exts) {
err = cmStrCat(err, " .", ext);
}
break;
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS:
case cmPolicies::NEW:
break;
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
if (error != nullptr) {
*error = std::move(err);
2016-07-09 11:21:54 +02:00
} else {
2019-11-11 23:01:05 +01:00
makefile->IssueMessage(MessageType::FATAL_ERROR, err);
2016-07-09 11:21:54 +02:00
}
this->FindFullPathFailed = true;
2019-11-11 23:01:05 +01:00
// File not found
return false;
}
void cmSourceFile::CheckExtension()
{
// Compute the extension.
std::string realExt =
cmSystemTools::GetFilenameLastExtension(this->FullPath);
2016-07-09 11:21:54 +02:00
if (!realExt.empty()) {
// Store the extension without the leading '.'.
this->Extension = realExt.substr(1);
2016-07-09 11:21:54 +02:00
}
// Look for object files.
2016-07-09 11:21:54 +02:00
if (this->Extension == "obj" || this->Extension == "o" ||
this->Extension == "lo") {
this->SetProperty("EXTERNAL_OBJECT", "1");
2016-07-09 11:21:54 +02:00
}
// Try to identify the source file language from the extension.
2016-07-09 11:21:54 +02:00
if (this->Language.empty()) {
this->CheckLanguage(this->Extension);
2016-07-09 11:21:54 +02:00
}
}
void cmSourceFile::CheckLanguage(std::string const& ext)
{
// Try to identify the source file language from the extension.
2014-08-03 19:52:23 +02:00
cmMakefile const* mf = this->Location.GetMakefile();
2015-08-17 11:37:30 +02:00
cmGlobalGenerator* gg = mf->GetGlobalGenerator();
2015-04-27 22:25:09 +02:00
std::string l = gg->GetLanguageFromExtension(ext.c_str());
2016-07-09 11:21:54 +02:00
if (!l.empty()) {
this->Language = l;
2016-07-09 11:21:54 +02:00
}
}
bool cmSourceFile::Matches(cmSourceFileLocation const& loc)
{
return this->Location.Matches(loc);
}
2021-11-20 13:41:27 +01:00
template <typename ValueType>
void cmSourceFile::StoreProperty(const std::string& prop, ValueType value)
{
2020-02-01 23:06:01 +01:00
if (prop == propINCLUDE_DIRECTORIES) {
this->IncludeDirectories.clear();
if (value) {
cmListFileBacktrace lfbt = this->Location.GetMakefile()->GetBacktrace();
this->IncludeDirectories.emplace_back(value, lfbt);
}
} else if (prop == propCOMPILE_OPTIONS) {
this->CompileOptions.clear();
if (value) {
cmListFileBacktrace lfbt = this->Location.GetMakefile()->GetBacktrace();
this->CompileOptions.emplace_back(value, lfbt);
}
} else if (prop == propCOMPILE_DEFINITIONS) {
this->CompileDefinitions.clear();
if (value) {
cmListFileBacktrace lfbt = this->Location.GetMakefile()->GetBacktrace();
this->CompileDefinitions.emplace_back(value, lfbt);
}
} else {
this->Properties.SetProperty(prop, value);
}
}
2021-11-20 13:41:27 +01:00
void cmSourceFile::SetProperty(const std::string& prop, const char* value)
{
this->StoreProperty(prop, value);
}
void cmSourceFile::SetProperty(const std::string& prop, cmValue value)
{
this->StoreProperty(prop, value);
}
2020-08-30 11:54:41 +02:00
void cmSourceFile::AppendProperty(const std::string& prop,
const std::string& value, bool asString)
{
2020-02-01 23:06:01 +01:00
if (prop == propINCLUDE_DIRECTORIES) {
2020-08-30 11:54:41 +02:00
if (!value.empty()) {
2020-02-01 23:06:01 +01:00
cmListFileBacktrace lfbt = this->Location.GetMakefile()->GetBacktrace();
this->IncludeDirectories.emplace_back(value, lfbt);
}
} else if (prop == propCOMPILE_OPTIONS) {
2020-08-30 11:54:41 +02:00
if (!value.empty()) {
2020-02-01 23:06:01 +01:00
cmListFileBacktrace lfbt = this->Location.GetMakefile()->GetBacktrace();
this->CompileOptions.emplace_back(value, lfbt);
}
} else if (prop == propCOMPILE_DEFINITIONS) {
2020-08-30 11:54:41 +02:00
if (!value.empty()) {
2020-02-01 23:06:01 +01:00
cmListFileBacktrace lfbt = this->Location.GetMakefile()->GetBacktrace();
this->CompileDefinitions.emplace_back(value, lfbt);
}
} else {
this->Properties.AppendProperty(prop, value, asString);
}
}
2021-11-20 13:41:27 +01:00
cmValue cmSourceFile::GetPropertyForUser(const std::string& prop)
{
// This method is a consequence of design history and backwards
// compatibility. GetProperty is (and should be) a const method.
// Computed properties should not be stored back in the property map
// but instead reference information already known. If they need to
// cache information in a mutable ivar to provide the return string
// safely then so be it.
//
// The LOCATION property is particularly problematic. The CMake
// language has very loose restrictions on the names that will match
// a given source file (for historical reasons). Implementing
// lookups correctly with such loose naming requires the
// cmSourceFileLocation class to commit to a particular full path to
// the source file as late as possible. If the users requests the
// LOCATION property we must commit now.
2019-11-11 23:01:05 +01:00
if (prop == propLOCATION) {
// Commit to a location.
2020-02-01 23:06:01 +01:00
this->ResolveFullPath();
}
// Similarly, LANGUAGE can be determined by the file extension
// if it is requested by the user.
if (prop == propLANGUAGE) {
2021-09-14 00:13:48 +02:00
// The pointer is valid until `this->Language` is modified.
2021-11-20 13:41:27 +01:00
return cmValue(this->GetOrDetermineLanguage());
2021-09-14 00:13:48 +02:00
}
// Special handling for GENERATED property.
if (prop == propGENERATED) {
// We need to check policy CMP0118 in order to determine if we need to
// possibly consider the value of a locally set GENERATED property, too.
auto policyStatus =
this->Location.GetMakefile()->GetPolicyStatus(cmPolicies::CMP0118);
if (this->GetIsGenerated(
(policyStatus == cmPolicies::WARN || policyStatus == cmPolicies::OLD)
? CheckScope::GlobalAndLocal
: CheckScope::Global)) {
2021-11-20 13:41:27 +01:00
return cmValue(propTRUE);
2021-09-14 00:13:48 +02:00
}
2021-11-20 13:41:27 +01:00
return cmValue(propFALSE);
2016-07-09 11:21:54 +02:00
}
// Perform the normal property lookup.
2021-09-14 00:13:48 +02:00
return this->GetProperty(prop);
}
2021-11-20 13:41:27 +01:00
cmValue cmSourceFile::GetProperty(const std::string& prop) const
{
// Check for computed properties.
2019-11-11 23:01:05 +01:00
if (prop == propLOCATION) {
2016-07-09 11:21:54 +02:00
if (this->FullPath.empty()) {
2018-01-26 17:06:56 +01:00
return nullptr;
}
2021-11-20 13:41:27 +01:00
return cmValue(this->FullPath);
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
// Check for the properties with backtraces.
if (prop == propINCLUDE_DIRECTORIES) {
if (this->IncludeDirectories.empty()) {
return nullptr;
}
static std::string output;
output = cmJoin(this->IncludeDirectories, ";");
2021-11-20 13:41:27 +01:00
return cmValue(output);
2020-02-01 23:06:01 +01:00
}
if (prop == propCOMPILE_OPTIONS) {
if (this->CompileOptions.empty()) {
return nullptr;
}
static std::string output;
output = cmJoin(this->CompileOptions, ";");
2021-11-20 13:41:27 +01:00
return cmValue(output);
2020-02-01 23:06:01 +01:00
}
if (prop == propCOMPILE_DEFINITIONS) {
if (this->CompileDefinitions.empty()) {
return nullptr;
}
static std::string output;
output = cmJoin(this->CompileDefinitions, ";");
2021-11-20 13:41:27 +01:00
return cmValue(output);
2020-02-01 23:06:01 +01:00
}
2021-11-20 13:41:27 +01:00
cmValue retVal = this->Properties.GetPropertyValue(prop);
2016-07-09 11:21:54 +02:00
if (!retVal) {
2014-08-03 19:52:23 +02:00
cmMakefile const* mf = this->Location.GetMakefile();
2016-07-09 11:21:54 +02:00
const bool chain =
mf->GetState()->IsPropertyChained(prop, cmProperty::SOURCE_FILE);
if (chain) {
2015-11-17 17:22:37 +01:00
return mf->GetProperty(prop, chain);
}
2020-08-30 11:54:41 +02:00
return nullptr;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
return retVal;
}
2021-09-14 00:13:48 +02:00
const std::string& cmSourceFile::GetSafeProperty(const std::string& prop) const
2018-10-28 12:09:07 +01:00
{
2021-11-20 13:41:27 +01:00
cmValue ret = this->GetProperty(prop);
2021-09-14 00:13:48 +02:00
if (ret) {
return *ret;
2018-10-28 12:09:07 +01:00
}
2021-09-14 00:13:48 +02:00
static std::string const s_empty;
return s_empty;
2018-10-28 12:09:07 +01:00
}
2015-04-27 22:25:09 +02:00
bool cmSourceFile::GetPropertyAsBool(const std::string& prop) const
{
2021-09-14 00:13:48 +02:00
return cmIsOn(this->GetProperty(prop));
}
void cmSourceFile::MarkAsGenerated()
{
this->IsGenerated = true;
const auto& mf = *this->Location.GetMakefile();
mf.GetGlobalGenerator()->MarkAsGeneratedFile(this->ResolveFullPath());
}
bool cmSourceFile::GetIsGenerated(CheckScope checkScope) const
{
if (this->IsGenerated) {
// Globally marked as generated!
return true;
}
if (checkScope == CheckScope::GlobalAndLocal) {
// Check locally stored properties.
return this->GetPropertyAsBool(propGENERATED);
}
return false;
}
2020-02-01 23:06:01 +01:00
void cmSourceFile::SetProperties(cmPropertyMap properties)
{
2020-02-01 23:06:01 +01:00
this->Properties = std::move(properties);
}
2020-02-01 23:06:01 +01:00
cmCustomCommand* cmSourceFile::GetCustomCommand() const
{
2020-02-01 23:06:01 +01:00
return this->CustomCommand.get();
}
2020-02-01 23:06:01 +01:00
void cmSourceFile::SetCustomCommand(std::unique_ptr<cmCustomCommand> cc)
{
2020-02-01 23:06:01 +01:00
this->CustomCommand = std::move(cc);
}