This source file includes following definitions.
- GetTopleveOutputDir
- GetTargetOutputDir
- GetNinjaFileForTarget
- GetNinjaFileForToolchain
- GetOutputFileForSource
- GetTargetOutputFile
- GetRulePrefix
- GetRuleForSourceType
#include "tools/gn/ninja_helper.h"
#include "base/logging.h"
#include "tools/gn/filesystem_utils.h"
#include "tools/gn/string_utils.h"
#include "tools/gn/target.h"
namespace {
const char kLibDirWithSlash[] = "lib/";
const char kObjectDirNoSlash[] = "obj";
}
NinjaHelper::NinjaHelper(const BuildSettings* build_settings)
: build_settings_(build_settings) {
build_to_src_no_last_slash_ = build_settings->build_to_source_dir_string();
if (!build_to_src_no_last_slash_.empty() &&
build_to_src_no_last_slash_[build_to_src_no_last_slash_.size() - 1] ==
'/')
build_to_src_no_last_slash_.resize(build_to_src_no_last_slash_.size() - 1);
build_to_src_system_no_last_slash_ = build_to_src_no_last_slash_;
}
NinjaHelper::~NinjaHelper() {
}
std::string NinjaHelper::GetTopleveOutputDir() const {
return kObjectDirNoSlash;
}
std::string NinjaHelper::GetTargetOutputDir(const Target* target) const {
return kObjectDirNoSlash + target->label().dir().SourceAbsoluteWithOneSlash();
}
OutputFile NinjaHelper::GetNinjaFileForTarget(const Target* target) const {
OutputFile ret(target->settings()->toolchain_output_subdir());
ret.value().append(kObjectDirNoSlash);
AppendStringPiece(&ret.value(),
target->label().dir().SourceAbsoluteWithOneSlash());
ret.value().append(target->label().name());
ret.value().append(".ninja");
return ret;
}
OutputFile NinjaHelper::GetNinjaFileForToolchain(
const Settings* settings) const {
OutputFile ret;
ret.value().append(settings->toolchain_output_subdir().value());
ret.value().append("toolchain.ninja");
return ret;
}
OutputFile NinjaHelper::GetOutputFileForSource(
const Target* target,
const SourceFile& source,
SourceFileType type) const {
base::StringPiece filename = FindFilename(&source.value());
std::string name(filename.data(), filename.size());
size_t extension_offset = FindExtensionOffset(name);
CHECK(extension_offset != std::string::npos);
name.resize(extension_offset);
switch (type) {
case SOURCE_ASM:
case SOURCE_C:
case SOURCE_CC:
case SOURCE_M:
case SOURCE_MM:
case SOURCE_S:
name.append(target->settings()->IsWin() ? "obj" : "o");
break;
case SOURCE_RC:
name.append("res");
break;
case SOURCE_H:
case SOURCE_UNKNOWN:
NOTREACHED();
return OutputFile();
}
OutputFile ret(target->settings()->toolchain_output_subdir());
ret.value().append(kObjectDirNoSlash);
base::StringPiece dir = FindDir(&source.value());
CHECK(dir.size() >= 2 && dir[0] == '/' && dir[1] == '/')
<< "Source file isn't in the source repo: " << dir;
AppendStringPiece(&ret.value(), dir.substr(1));
ret.value().append(target->label().name());
ret.value().append(".");
ret.value().append(name);
return ret;
}
OutputFile NinjaHelper::GetTargetOutputFile(const Target* target) const {
OutputFile ret;
const std::string& name = target->output_name().empty() ?
target->label().name() : target->output_name();
const char* prefix;
if (!target->settings()->IsWin() &&
(target->output_type() == Target::SHARED_LIBRARY ||
target->output_type() == Target::STATIC_LIBRARY) &&
name.compare(0, 3, "lib") != 0)
prefix = "lib";
else
prefix = "";
const char* extension;
if (target->output_extension().empty()) {
if (target->output_type() == Target::GROUP ||
target->output_type() == Target::SOURCE_SET ||
target->output_type() == Target::COPY_FILES ||
target->output_type() == Target::ACTION ||
target->output_type() == Target::ACTION_FOREACH) {
extension = "stamp";
} else {
extension = GetExtensionForOutputType(target->output_type(),
target->settings()->target_os());
}
} else {
extension = target->output_extension().c_str();
}
ret.value().append(target->settings()->toolchain_output_subdir().value());
if (target->output_type() == Target::EXECUTABLE ||
(target->settings()->IsMac() &&
(target->output_type() == Target::SHARED_LIBRARY ||
target->output_type() == Target::STATIC_LIBRARY)) ||
(target->settings()->IsWin() &&
target->output_type() == Target::SHARED_LIBRARY)) {
ret.value().append(prefix);
ret.value().append(name);
if (extension[0]) {
ret.value().push_back('.');
ret.value().append(extension);
}
return ret;
}
if (target->output_type() == Target::SHARED_LIBRARY) {
ret.value().append(kLibDirWithSlash);
ret.value().append(prefix);
ret.value().append(name);
if (extension[0]) {
ret.value().push_back('.');
ret.value().append(extension);
}
return ret;
}
ret.value().append(kObjectDirNoSlash);
AppendStringPiece(&ret.value(),
target->label().dir().SourceAbsoluteWithOneSlash());
ret.value().append(prefix);
ret.value().append(name);
if (extension[0]) {
ret.value().push_back('.');
ret.value().append(extension);
}
return ret;
}
std::string NinjaHelper::GetRulePrefix(const Settings* settings) const {
if (settings->is_default())
return std::string();
return settings->toolchain_label().name() + "_";
}
std::string NinjaHelper::GetRuleForSourceType(const Settings* settings,
SourceFileType type) const {
std::string prefix = GetRulePrefix(settings);
if (type == SOURCE_C)
return prefix + "cc";
if (type == SOURCE_CC)
return prefix + "cxx";
if (settings->IsMac()) {
if (type == SOURCE_M)
return prefix + "objc";
if (type == SOURCE_MM)
return prefix + "objcxx";
}
if (settings->IsWin()) {
if (type == SOURCE_RC)
return prefix + "rc";
} else {
if (type == SOURCE_S)
return prefix + "cc";
}
return std::string();
}