| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- /*============================================================================
- CMake - Cross Platform Makefile Generator
- Copyright 2011 Peter Collingbourne <[email protected]>
- Copyright 2011 Nicolas Despres <[email protected]>
- Distributed under the OSI-approved BSD License (the "License");
- see accompanying file Copyright.txt for details.
- This software is distributed WITHOUT ANY WARRANTY; without even the
- implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- See the License for more information.
- ============================================================================*/
- #include "cmNinjaNormalTargetGenerator.h"
- #include "cmLocalNinjaGenerator.h"
- #include "cmGlobalNinjaGenerator.h"
- #include "cmSourceFile.h"
- #include "cmGeneratedFileStream.h"
- #include "cmMakefile.h"
- #include <assert.h>
- cmNinjaNormalTargetGenerator::
- cmNinjaNormalTargetGenerator(cmTarget* target)
- : cmNinjaTargetGenerator(target)
- , TargetNameOut()
- , TargetNameSO()
- , TargetNameReal()
- , TargetNameImport()
- , TargetNamePDB()
- {
- this->TargetLinkLanguage = target->GetLinkerLanguage(this->GetConfigName());
- if (target->GetType() == cmTarget::EXECUTABLE)
- target->GetExecutableNames(this->TargetNameOut,
- this->TargetNameReal,
- this->TargetNameImport,
- this->TargetNamePDB,
- GetLocalGenerator()->GetConfigName());
- else
- target->GetLibraryNames(this->TargetNameOut,
- this->TargetNameSO,
- this->TargetNameReal,
- this->TargetNameImport,
- this->TargetNamePDB,
- GetLocalGenerator()->GetConfigName());
- if(target->GetType() != cmTarget::OBJECT_LIBRARY)
- {
- // on Windows the output dir is already needed at compile time
- // ensure the directory exists (OutDir test)
- EnsureDirectoryExists(target->GetDirectory(this->GetConfigName()));
- }
- }
- cmNinjaNormalTargetGenerator::~cmNinjaNormalTargetGenerator()
- {
- }
- void
- cmNinjaNormalTargetGenerator
- ::EnsureDirectoryExists(const std::string& dir)
- {
- cmSystemTools::MakeDirectory(dir.c_str());
- }
- void
- cmNinjaNormalTargetGenerator
- ::EnsureParentDirectoryExists(const std::string& path)
- {
- EnsureDirectoryExists(cmSystemTools::GetParentDirectory(path.c_str()));
- }
- void cmNinjaNormalTargetGenerator::Generate()
- {
- if (!this->TargetLinkLanguage) {
- cmSystemTools::Error("CMake can not determine linker language for target:",
- this->GetTarget()->GetName());
- return;
- }
- // Write the rules for each language.
- this->WriteLanguagesRules();
- // Write the build statements
- this->WriteObjectBuildStatements();
- if(this->GetTarget()->GetType() == cmTarget::OBJECT_LIBRARY)
- {
- this->WriteObjectLibStatement();
- }
- else
- {
- this->WriteLinkRule();
- this->WriteLinkStatement();
- }
- this->GetBuildFileStream() << "\n";
- this->GetRulesFileStream() << "\n";
- }
- void cmNinjaNormalTargetGenerator::WriteLanguagesRules()
- {
- cmGlobalNinjaGenerator::WriteDivider(this->GetRulesFileStream());
- this->GetRulesFileStream()
- << "# Rules for each languages for "
- << cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
- << " target "
- << this->GetTargetName()
- << "\n\n";
- std::set<cmStdString> languages;
- this->GetTarget()->GetLanguages(languages);
- for(std::set<cmStdString>::const_iterator l = languages.begin();
- l != languages.end();
- ++l)
- this->WriteLanguageRules(*l);
- }
- const char *cmNinjaNormalTargetGenerator::GetVisibleTypeName() const
- {
- switch (this->GetTarget()->GetType()) {
- case cmTarget::STATIC_LIBRARY:
- return "static library";
- case cmTarget::SHARED_LIBRARY:
- return "shared library";
- case cmTarget::MODULE_LIBRARY:
- return "shared module";
- case cmTarget::EXECUTABLE:
- return "executable";
- default:
- return 0;
- }
- }
- std::string
- cmNinjaNormalTargetGenerator
- ::LanguageLinkerRule() const
- {
- return std::string(this->TargetLinkLanguage)
- + "_"
- + cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
- + "_LINKER";
- }
- void
- cmNinjaNormalTargetGenerator
- ::WriteLinkRule()
- {
- cmTarget::TargetType targetType = this->GetTarget()->GetType();
- std::string ruleName = this->LanguageLinkerRule();
- if (!this->GetGlobalGenerator()->HasRule(ruleName)) {
- cmLocalGenerator::RuleVariables vars;
- vars.RuleLauncher = "RULE_LAUNCH_LINK";
- vars.CMTarget = this->GetTarget();
- vars.Language = this->TargetLinkLanguage;
- vars.Objects = "$in";
- std::string objdir =
- this->GetLocalGenerator()->GetHomeRelativeOutputPath();
- objdir += objdir.empty() ? "" : "/";
- objdir += cmake::GetCMakeFilesDirectoryPostSlash();
- objdir += this->GetTargetName();
- objdir += ".dir";
- objdir = this->GetLocalGenerator()->Convert(objdir.c_str(),
- cmLocalGenerator::START_OUTPUT,
- cmLocalGenerator::SHELL);
- vars.ObjectDir = objdir.c_str();
- vars.Target = "$out";
- vars.TargetSOName = "$SONAME";
- vars.TargetInstallNameDir = "$INSTALLNAME_DIR";
- vars.TargetPDB = "$TARGET_PDB";
- // Setup the target version.
- std::string targetVersionMajor;
- std::string targetVersionMinor;
- {
- cmOStringStream majorStream;
- cmOStringStream minorStream;
- int major;
- int minor;
- this->GetTarget()->GetTargetVersion(major, minor);
- majorStream << major;
- minorStream << minor;
- targetVersionMajor = majorStream.str();
- targetVersionMinor = minorStream.str();
- }
- vars.TargetVersionMajor = targetVersionMajor.c_str();
- vars.TargetVersionMinor = targetVersionMinor.c_str();
- vars.LinkLibraries = "$LINK_LIBRARIES";
- vars.Flags = "$FLAGS";
- vars.LinkFlags = "$LINK_FLAGS";
- std::string langFlags;
- this->GetLocalGenerator()->AddLanguageFlags(langFlags,
- this->TargetLinkLanguage,
- this->GetConfigName());
- if (targetType != cmTarget::EXECUTABLE)
- langFlags += " $ARCH_FLAGS";
- vars.LanguageCompileFlags = langFlags.c_str();
- // Rule for linking library.
- std::vector<std::string> linkCmds = this->ComputeLinkCmd();
- for(std::vector<std::string>::iterator i = linkCmds.begin();
- i != linkCmds.end();
- ++i)
- {
- this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
- }
- linkCmds.insert(linkCmds.begin(), "$PRE_LINK");
- linkCmds.push_back("$POST_BUILD");
- std::string linkCmd =
- this->GetLocalGenerator()->BuildCommandLine(linkCmds);
- // Write the linker rule.
- std::ostringstream comment;
- comment << "Rule for linking " << this->TargetLinkLanguage << " "
- << this->GetVisibleTypeName() << ".";
- std::ostringstream description;
- description << "Linking " << this->TargetLinkLanguage << " "
- << this->GetVisibleTypeName() << " $out";
- this->GetGlobalGenerator()->AddRule(ruleName,
- linkCmd,
- description.str(),
- comment.str());
- }
- if (this->TargetNameOut != this->TargetNameReal) {
- std::string cmakeCommand =
- this->GetLocalGenerator()->ConvertToOutputFormat(
- this->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND"),
- cmLocalGenerator::SHELL);
- if (targetType == cmTarget::EXECUTABLE)
- this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_EXECUTABLE",
- cmakeCommand +
- " -E cmake_symlink_executable"
- " $in $out && $POST_BUILD",
- "Creating executable symlink $out",
- "Rule for creating executable symlink.");
- else
- this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_LIBRARY",
- cmakeCommand +
- " -E cmake_symlink_library"
- " $in $SONAME $out && $POST_BUILD",
- "Creating library symlink $out",
- "Rule for creating library symlink.");
- }
- }
- std::vector<std::string>
- cmNinjaNormalTargetGenerator
- ::ComputeLinkCmd()
- {
- std::vector<std::string> linkCmds;
- cmTarget::TargetType targetType = this->GetTarget()->GetType();
- switch (targetType) {
- case cmTarget::STATIC_LIBRARY: {
- // Check if you have a non archive way to create the static library.
- {
- std::string linkCmdVar = "CMAKE_";
- linkCmdVar += this->TargetLinkLanguage;
- linkCmdVar += "_CREATE_STATIC_LIBRARY";
- if (const char *linkCmd =
- this->GetMakefile()->GetDefinition(linkCmdVar.c_str()))
- {
- cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
- return linkCmds;
- }
- }
- // We have archive link commands set. First, delete the existing archive.
- std::string cmakeCommand =
- this->GetLocalGenerator()->ConvertToOutputFormat(
- this->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND"),
- cmLocalGenerator::SHELL);
- linkCmds.push_back(cmakeCommand + " -E remove $out");
- // TODO: Use ARCHIVE_APPEND for archives over a certain size.
- {
- std::string linkCmdVar = "CMAKE_";
- linkCmdVar += this->TargetLinkLanguage;
- linkCmdVar += "_ARCHIVE_CREATE";
- const char *linkCmd =
- this->GetMakefile()->GetRequiredDefinition(linkCmdVar.c_str());
- cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
- }
- {
- std::string linkCmdVar = "CMAKE_";
- linkCmdVar += this->TargetLinkLanguage;
- linkCmdVar += "_ARCHIVE_FINISH";
- const char *linkCmd =
- this->GetMakefile()->GetRequiredDefinition(linkCmdVar.c_str());
- cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
- }
- return linkCmds;
- }
- case cmTarget::SHARED_LIBRARY:
- case cmTarget::MODULE_LIBRARY:
- case cmTarget::EXECUTABLE: {
- std::string linkCmdVar = "CMAKE_";
- linkCmdVar += this->TargetLinkLanguage;
- switch (targetType) {
- case cmTarget::SHARED_LIBRARY:
- linkCmdVar += "_CREATE_SHARED_LIBRARY";
- break;
- case cmTarget::MODULE_LIBRARY:
- linkCmdVar += "_CREATE_SHARED_MODULE";
- break;
- case cmTarget::EXECUTABLE:
- linkCmdVar += "_LINK_EXECUTABLE";
- break;
- default:
- assert(0 && "Unexpected target type");
- }
- const char *linkCmd =
- this->GetMakefile()->GetRequiredDefinition(linkCmdVar.c_str());
- cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
- return linkCmds;
- }
- default:
- assert(0 && "Unexpected target type");
- }
- return std::vector<std::string>();
- }
- void cmNinjaNormalTargetGenerator::WriteLinkStatement()
- {
- cmTarget::TargetType targetType = this->GetTarget()->GetType();
- // Write comments.
- cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
- this->GetBuildFileStream()
- << "# Link build statements for "
- << cmTarget::GetTargetTypeName(targetType)
- << " target "
- << this->GetTargetName()
- << "\n\n";
- cmNinjaDeps emptyDeps;
- cmNinjaVars vars;
- std::string targetOutput = ConvertToNinjaPath(
- this->GetTarget()->GetFullPath(this->GetConfigName()).c_str());
- std::string targetOutputReal = ConvertToNinjaPath(
- this->GetTarget()->GetFullPath(this->GetConfigName(),
- /*implib=*/false,
- /*realpath=*/true).c_str());
- std::string targetOutputImplib = ConvertToNinjaPath(
- this->GetTarget()->GetFullPath(this->GetConfigName(),
- /*implib=*/true).c_str());
- // Compute the comment.
- std::ostringstream comment;
- comment << "Link the " << this->GetVisibleTypeName() << " "
- << targetOutputReal;
- // Compute outputs.
- cmNinjaDeps outputs;
- outputs.push_back(targetOutputReal);
- // Compute specific libraries to link with.
- cmNinjaDeps explicitDeps = this->GetObjects(),
- implicitDeps = this->ComputeLinkDeps();
- this->GetLocalGenerator()->GetTargetFlags(vars["LINK_LIBRARIES"],
- vars["FLAGS"],
- vars["LINK_FLAGS"],
- *this->GetTarget());
- this->AddModuleDefinitionFlag(vars["LINK_FLAGS"]);
- // Compute architecture specific link flags. Yes, these go into a different
- // variable for executables, probably due to a mistake made when duplicating
- // code between the Makefile executable and library generators.
- this->GetLocalGenerator()
- ->AddArchitectureFlags(targetType == cmTarget::EXECUTABLE
- ? vars["FLAGS"]
- : vars["ARCH_FLAGS"],
- this->GetTarget(),
- this->TargetLinkLanguage,
- this->GetConfigName());
- vars["SONAME"] = this->TargetNameSO;
- if (targetType == cmTarget::SHARED_LIBRARY) {
- std::string install_name_dir =
- this->GetTarget()->GetInstallNameDirForBuildTree(this->GetConfigName());
- if (!install_name_dir.empty()) {
- vars["INSTALLNAME_DIR"] =
- this->GetLocalGenerator()->Convert(install_name_dir.c_str(),
- cmLocalGenerator::NONE,
- cmLocalGenerator::SHELL, false);
- }
- }
- std::string path;
- if (!this->TargetNameImport.empty()) {
- path = this->GetLocalGenerator()->ConvertToOutputFormat(
- targetOutputImplib.c_str(), cmLocalGenerator::SHELL);
- vars["TARGET_IMPLIB"] = path;
- EnsureParentDirectoryExists(path);
- }
- path = this->GetLocalGenerator()->ConvertToOutputFormat(
- this->GetTargetPDB().c_str(), cmLocalGenerator::SHELL);
- vars["TARGET_PDB"] = path;
- EnsureParentDirectoryExists(path);
- std::vector<cmCustomCommand> *cmdLists[3] = {
- &this->GetTarget()->GetPreBuildCommands(),
- &this->GetTarget()->GetPreLinkCommands(),
- &this->GetTarget()->GetPostBuildCommands()
- };
- std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
- std::vector<std::string> *cmdLineLists[3] = {
- &preLinkCmdLines,
- &preLinkCmdLines,
- &postBuildCmdLines
- };
- for (unsigned i = 0; i != 3; ++i) {
- for (std::vector<cmCustomCommand>::const_iterator
- ci = cmdLists[i]->begin();
- ci != cmdLists[i]->end(); ++ci) {
- this->GetLocalGenerator()->AppendCustomCommandLines(&*ci,
- *cmdLineLists[i]);
- }
- }
- // If we have any PRE_LINK commands, we need to go back to HOME_OUTPUT for
- // the link commands.
- if (!preLinkCmdLines.empty()) {
- path = this->GetLocalGenerator()->ConvertToOutputFormat(
- this->GetMakefile()->GetHomeOutputDirectory(),
- cmLocalGenerator::SHELL);
- preLinkCmdLines.push_back("cd " + path);
- }
- vars["PRE_LINK"] =
- this->GetLocalGenerator()->BuildCommandLine(preLinkCmdLines);
- std::string postBuildCmdLine =
- this->GetLocalGenerator()->BuildCommandLine(postBuildCmdLines);
- cmNinjaVars symlinkVars;
- if (targetOutput == targetOutputReal) {
- vars["POST_BUILD"] = postBuildCmdLine;
- } else {
- vars["POST_BUILD"] = ":";
- symlinkVars["POST_BUILD"] = postBuildCmdLine;
- }
- // Write the build statement for this target.
- cmGlobalNinjaGenerator::WriteBuild(this->GetBuildFileStream(),
- comment.str(),
- this->LanguageLinkerRule(),
- outputs,
- explicitDeps,
- implicitDeps,
- emptyDeps,
- vars);
- if (targetOutput != targetOutputReal) {
- if (targetType == cmTarget::EXECUTABLE) {
- cmGlobalNinjaGenerator::WriteBuild(this->GetBuildFileStream(),
- "Create executable symlink " + targetOutput,
- "CMAKE_SYMLINK_EXECUTABLE",
- cmNinjaDeps(1, targetOutput),
- cmNinjaDeps(1, targetOutputReal),
- emptyDeps,
- emptyDeps,
- symlinkVars);
- } else {
- symlinkVars["SONAME"] = this->GetTargetFilePath(this->TargetNameSO);
- cmGlobalNinjaGenerator::WriteBuild(this->GetBuildFileStream(),
- "Create library symlink " + targetOutput,
- "CMAKE_SYMLINK_LIBRARY",
- cmNinjaDeps(1, targetOutput),
- cmNinjaDeps(1, targetOutputReal),
- emptyDeps,
- emptyDeps,
- symlinkVars);
- }
- }
- if (!this->TargetNameImport.empty()) {
- // Since using multiple outputs would mess up the $out variable, use an
- // alias for the import library.
- cmGlobalNinjaGenerator::WritePhonyBuild(this->GetBuildFileStream(),
- "Alias for import library.",
- cmNinjaDeps(1, targetOutputImplib),
- cmNinjaDeps(1, targetOutputReal));
- }
- // Add aliases for the file name and the target name.
- this->GetGlobalGenerator()->AddTargetAlias(this->TargetNameOut,
- this->GetTarget());
- this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
- this->GetTarget());
- }
- //----------------------------------------------------------------------------
- void cmNinjaNormalTargetGenerator::WriteObjectLibStatement()
- {
- // Write a phony output that depends on all object files.
- cmNinjaDeps outputs;
- this->GetLocalGenerator()->AppendTargetOutputs(this->GetTarget(), outputs);
- cmNinjaDeps depends = this->GetObjects();
- cmGlobalNinjaGenerator::WritePhonyBuild(this->GetBuildFileStream(),
- "Object library "
- + this->GetTargetName(),
- outputs,
- depends);
- // Add aliases for the target name.
- this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
- this->GetTarget());
- }
|