cmake/Source/cmSourceFile.cxx

319 lines
8.7 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"
2017-04-14 19:02:05 +02:00
#include <sstream>
2016-10-30 18:24:19 +01:00
#include "cmCustomCommand.h"
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
2016-10-30 18:24:19 +01:00
#include "cmProperty.h"
#include "cmState.h"
#include "cmSystemTools.h"
#include "cmake.h"
2016-07-09 11:21:54 +02:00
cmSourceFile::cmSourceFile(cmMakefile* mf, const std::string& name)
: Location(mf, name)
{
2018-01-26 17:06:56 +01:00
this->CustomCommand = nullptr;
this->FindFullPathFailed = false;
}
cmSourceFile::~cmSourceFile()
{
2018-01-26 17:06:56 +01:00
this->SetCustomCommand(nullptr);
}
std::string const& cmSourceFile::GetExtension() const
{
return this->Extension;
}
2015-04-27 22:25:09 +02:00
const std::string cmSourceFile::propLANGUAGE = "LANGUAGE";
void cmSourceFile::SetObjectLibrary(std::string const& objlib)
{
this->ObjectLibrary = objlib;
}
std::string cmSourceFile::GetObjectLibrary() const
{
return this->ObjectLibrary;
}
std::string cmSourceFile::GetLanguage()
{
// If the language was set explicitly by the user then use it.
2016-07-09 11:21:54 +02:00
if (const char* lang = this->GetProperty(propLANGUAGE)) {
return lang;
2016-07-09 11:21:54 +02:00
}
// Perform computation needed to get the language if necessary.
2016-07-09 11:21:54 +02:00
if (this->FullPath.empty() && 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.
2016-07-09 11:21:54 +02:00
if (this->Location.ExtensionIsAmbiguous() &&
this->Location.DirectoryIsAmbiguous()) {
// Finalize the file location to get the extension and set the
// language.
this->GetFullPath();
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
}
// Now try to determine the language.
return static_cast<cmSourceFile const*>(this)->GetLanguage();
}
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.
2016-07-09 11:21:54 +02:00
if (const char* lang = this->GetProperty(propLANGUAGE)) {
return lang;
2016-07-09 11:21:54 +02:00
}
// If the language was determined from the source file extension use it.
2016-07-09 11:21:54 +02:00
if (!this->Language.empty()) {
2015-04-27 22:25:09 +02:00
return this->Language;
2016-07-09 11:21:54 +02:00
}
// The language is not known.
2015-04-27 22:25:09 +02:00
return "";
}
cmSourceFileLocation const& cmSourceFile::GetLocation() const
{
2016-07-09 11:21:54 +02:00
return this->Location;
}
2010-11-13 01:00:53 +02:00
std::string const& cmSourceFile::GetFullPath(std::string* error)
{
2016-07-09 11:21:54 +02:00
if (this->FullPath.empty()) {
if (this->FindFullPath(error)) {
this->CheckExtension();
}
2016-07-09 11:21:54 +02:00
}
return this->FullPath;
}
std::string const& cmSourceFile::GetFullPath() const
{
return this->FullPath;
}
2010-11-13 01:00:53 +02:00
bool cmSourceFile::FindFullPath(std::string* error)
{
// If thie method has already failed once do not try again.
2016-07-09 11:21:54 +02:00
if (this->FindFullPathFailed) {
return false;
2016-07-09 11:21:54 +02:00
}
// If the file is generated compute the location without checking on
// disk.
2016-07-09 11:21:54 +02:00
if (this->GetPropertyAsBool("GENERATED")) {
// The file is either already a full path or is relative to the
// build directory for the target.
this->Location.DirectoryUseBinary();
this->FullPath = this->Location.GetDirectory();
this->FullPath += "/";
this->FullPath += this->Location.GetName();
return true;
2016-07-09 11:21:54 +02:00
}
// The file is not generated. It must exist on disk.
2014-08-03 19:52:23 +02:00
cmMakefile const* mf = this->Location.GetMakefile();
2018-01-26 17:06:56 +01:00
const char* tryDirs[3] = { nullptr, nullptr, nullptr };
2016-07-09 11:21:54 +02:00
if (this->Location.DirectoryIsAmbiguous()) {
2015-08-17 11:37:30 +02:00
tryDirs[0] = mf->GetCurrentSourceDirectory();
tryDirs[1] = mf->GetCurrentBinaryDirectory();
2016-07-09 11:21:54 +02:00
} else {
tryDirs[0] = "";
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
cmake const* const cmakeInst = mf->GetCMakeInstance();
std::vector<std::string> const& srcExts = cmakeInst->GetSourceExtensions();
std::vector<std::string> const& hdrExts = cmakeInst->GetHeaderExtensions();
2016-07-09 11:21:54 +02:00
for (const char* const* di = tryDirs; *di; ++di) {
std::string tryPath = this->Location.GetDirectory();
2016-07-09 11:21:54 +02:00
if (!tryPath.empty()) {
tryPath += "/";
2016-07-09 11:21:54 +02:00
}
tryPath += this->Location.GetName();
2015-04-27 22:25:09 +02:00
tryPath = cmSystemTools::CollapseFullPath(tryPath, *di);
2016-07-09 11:21:54 +02:00
if (this->TryFullPath(tryPath, "")) {
return true;
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
for (std::string const& ext : srcExts) {
if (this->TryFullPath(tryPath, ext)) {
return true;
}
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
for (std::string const& ext : hdrExts) {
if (this->TryFullPath(tryPath, ext)) {
return true;
}
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2011-02-07 16:37:25 +01:00
std::string missing = this->Location.GetDirectory();
2016-07-09 11:21:54 +02:00
if (!missing.empty()) {
2011-02-07 16:37:25 +01:00
missing += "/";
2016-07-09 11:21:54 +02:00
}
2011-02-07 16:37:25 +01:00
missing += this->Location.GetName();
e << "Cannot find source file:\n " << missing << "\nTried extensions";
2018-01-26 17:06:56 +01:00
for (std::string const& srcExt : srcExts) {
e << " ." << srcExt;
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
for (std::string const& ext : hdrExts) {
e << " ." << ext;
2016-07-09 11:21:54 +02:00
}
if (error) {
2010-11-13 01:00:53 +02:00
*error = e.str();
2016-07-09 11:21:54 +02:00
} else {
2010-11-13 01:00:53 +02:00
this->Location.GetMakefile()->IssueMessage(cmake::FATAL_ERROR, e.str());
2016-07-09 11:21:54 +02:00
}
this->FindFullPathFailed = true;
return false;
}
2016-07-09 11:21:54 +02:00
bool cmSourceFile::TryFullPath(const std::string& path, const std::string& ext)
{
2015-04-27 22:25:09 +02:00
std::string tryPath = path;
2016-07-09 11:21:54 +02:00
if (!ext.empty()) {
tryPath += ".";
tryPath += ext;
2016-07-09 11:21:54 +02:00
}
if (cmSystemTools::FileExists(tryPath.c_str())) {
this->FullPath = tryPath;
return true;
2016-07-09 11:21:54 +02:00
}
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);
}
2015-04-27 22:25:09 +02:00
void cmSourceFile::SetProperty(const std::string& prop, const char* value)
{
2015-11-17 17:22:37 +01:00
this->Properties.SetProperty(prop, value);
}
2015-04-27 22:25:09 +02:00
void cmSourceFile::AppendProperty(const std::string& prop, const char* value,
2012-02-18 12:40:36 +02:00
bool asString)
{
2015-11-17 17:22:37 +01:00
this->Properties.AppendProperty(prop, value, asString);
}
2015-04-27 22:25:09 +02:00
const char* 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.
2016-07-09 11:21:54 +02:00
if (prop == "LOCATION") {
// Commit to a location.
this->GetFullPath();
2016-07-09 11:21:54 +02:00
}
// Perform the normal property lookup.
return this->GetProperty(prop);
}
2015-04-27 22:25:09 +02:00
const char* cmSourceFile::GetProperty(const std::string& prop) const
{
// Check for computed properties.
2016-07-09 11:21:54 +02:00
if (prop == "LOCATION") {
if (this->FullPath.empty()) {
2018-01-26 17:06:56 +01:00
return nullptr;
}
2016-10-30 18:24:19 +01:00
return this->FullPath.c_str();
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
const char* retVal = this->Properties.GetPropertyValue(prop);
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);
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
return retVal;
}
2015-04-27 22:25:09 +02:00
bool cmSourceFile::GetPropertyAsBool(const std::string& prop) const
{
return cmSystemTools::IsOn(this->GetProperty(prop));
}
cmCustomCommand* cmSourceFile::GetCustomCommand()
{
return this->CustomCommand;
}
cmCustomCommand const* cmSourceFile::GetCustomCommand() const
{
return this->CustomCommand;
}
void cmSourceFile::SetCustomCommand(cmCustomCommand* cc)
{
cmCustomCommand* old = this->CustomCommand;
this->CustomCommand = cc;
delete old;
}