/src/CMake/Source/cmMakefileUtilityTargetGenerator.cxx
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | #include "cmMakefileUtilityTargetGenerator.h" |
4 | | |
5 | | #include <ostream> |
6 | | #include <string> |
7 | | #include <utility> |
8 | | #include <vector> |
9 | | |
10 | | #include <cm/memory> |
11 | | |
12 | | #include "cmGeneratedFileStream.h" |
13 | | #include "cmGeneratorTarget.h" |
14 | | #include "cmGlobalUnixMakefileGenerator3.h" |
15 | | #include "cmLocalUnixMakefileGenerator3.h" |
16 | | #include "cmMakefile.h" |
17 | | #include "cmOSXBundleGenerator.h" |
18 | | #include "cmStringAlgorithms.h" |
19 | | #include "cmSystemTools.h" |
20 | | |
21 | | cmMakefileUtilityTargetGenerator::cmMakefileUtilityTargetGenerator( |
22 | | cmGeneratorTarget* target) |
23 | 0 | : cmMakefileTargetGenerator(target) |
24 | 0 | { |
25 | 0 | this->CustomCommandDriver = OnUtility; |
26 | 0 | this->OSXBundleGenerator = cm::make_unique<cmOSXBundleGenerator>(target); |
27 | 0 | this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders); |
28 | 0 | } |
29 | | |
30 | 0 | cmMakefileUtilityTargetGenerator::~cmMakefileUtilityTargetGenerator() = |
31 | | default; |
32 | | |
33 | | void cmMakefileUtilityTargetGenerator::WriteRuleFiles() |
34 | 0 | { |
35 | 0 | this->CreateRuleFile(); |
36 | |
|
37 | 0 | *this->BuildFileStream << "# Utility rule file for " |
38 | 0 | << this->GeneratorTarget->GetName() << ".\n\n"; |
39 | |
|
40 | 0 | char const* root = (this->Makefile->IsOn("CMAKE_MAKE_INCLUDE_FROM_ROOT") |
41 | 0 | ? "$(CMAKE_BINARY_DIR)/" |
42 | 0 | : ""); |
43 | | |
44 | | // Include the dependencies for the target. |
45 | 0 | std::string dependFile = |
46 | 0 | cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.make"); |
47 | 0 | *this->BuildFileStream |
48 | 0 | << "# Include any custom commands dependencies for this target.\n" |
49 | 0 | << this->GlobalGenerator->IncludeDirective << " " << root |
50 | 0 | << cmSystemTools::ConvertToOutputPath( |
51 | 0 | this->LocalGenerator->MaybeRelativeToTopBinDir(dependFile)) |
52 | 0 | << "\n\n"; |
53 | 0 | if (!cmSystemTools::FileExists(dependFile)) { |
54 | | // Write an empty dependency file. |
55 | 0 | cmGeneratedFileStream depFileStream( |
56 | 0 | dependFile, false, this->GlobalGenerator->GetMakefileEncoding()); |
57 | 0 | depFileStream << "# Empty custom commands generated dependencies file for " |
58 | 0 | << this->GeneratorTarget->GetName() |
59 | 0 | << ".\n" |
60 | 0 | "# This may be replaced when dependencies are built.\n"; |
61 | 0 | } |
62 | |
|
63 | 0 | std::string dependTimestamp = |
64 | 0 | cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.ts"); |
65 | 0 | if (!cmSystemTools::FileExists(dependTimestamp)) { |
66 | | // Write a dependency timestamp file. |
67 | 0 | cmGeneratedFileStream depFileStream( |
68 | 0 | dependTimestamp, false, this->GlobalGenerator->GetMakefileEncoding()); |
69 | 0 | depFileStream << "# CMAKE generated file: DO NOT EDIT!\n" |
70 | 0 | "# Timestamp file for custom commands dependencies " |
71 | 0 | "management for " |
72 | 0 | << this->GeneratorTarget->GetName() << ".\n"; |
73 | 0 | } |
74 | |
|
75 | 0 | if (!this->NoRuleMessages) { |
76 | | // Include the progress variables for the target. |
77 | 0 | *this->BuildFileStream |
78 | 0 | << "# Include the progress variables for this target.\n" |
79 | 0 | << this->GlobalGenerator->IncludeDirective << " " << root |
80 | 0 | << cmSystemTools::ConvertToOutputPath( |
81 | 0 | this->LocalGenerator->MaybeRelativeToTopBinDir( |
82 | 0 | this->ProgressFileNameFull)) |
83 | 0 | << "\n\n"; |
84 | 0 | } |
85 | | |
86 | | // write the custom commands for this target |
87 | 0 | this->WriteTargetBuildRules(); |
88 | | |
89 | | // Collect the commands and dependencies. |
90 | 0 | std::vector<std::string> commands; |
91 | 0 | std::vector<std::string> depends; |
92 | | |
93 | | // Utility targets store their rules in pre- and post-build commands. |
94 | 0 | this->LocalGenerator->AppendCustomDepends( |
95 | 0 | depends, this->GeneratorTarget->GetPreBuildCommands()); |
96 | |
|
97 | 0 | this->LocalGenerator->AppendCustomDepends( |
98 | 0 | depends, this->GeneratorTarget->GetPostBuildCommands()); |
99 | |
|
100 | 0 | this->LocalGenerator->AppendCustomCommands( |
101 | 0 | commands, this->GeneratorTarget->GetPreBuildCommands(), |
102 | 0 | this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory()); |
103 | | |
104 | | // Depend on all custom command outputs for sources |
105 | 0 | this->DriveCustomCommands(depends); |
106 | |
|
107 | 0 | this->LocalGenerator->AppendCustomCommands( |
108 | 0 | commands, this->GeneratorTarget->GetPostBuildCommands(), |
109 | 0 | this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory()); |
110 | | |
111 | | // Add dependencies on targets that must be built first. |
112 | 0 | this->AppendTargetDepends(depends); |
113 | | |
114 | | // Add a dependency on the rule file itself. |
115 | 0 | this->LocalGenerator->AppendRuleDepend(depends, |
116 | 0 | this->BuildFileNameFull.c_str()); |
117 | | |
118 | | // If the rule is empty add the special empty rule dependency needed |
119 | | // by some make tools. |
120 | 0 | if (depends.empty() && commands.empty()) { |
121 | 0 | std::string hack = this->GlobalGenerator->GetEmptyRuleHackDepends(); |
122 | 0 | if (!hack.empty()) { |
123 | 0 | depends.push_back(std::move(hack)); |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | | // Write the rule. |
128 | 0 | this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr, |
129 | 0 | this->GeneratorTarget->GetName(), |
130 | 0 | depends, commands, true); |
131 | | |
132 | | // Write the main driver rule to build everything in this target. |
133 | 0 | this->WriteTargetDriverRule(this->GeneratorTarget->GetName(), false); |
134 | | |
135 | | // Write clean target |
136 | 0 | this->WriteTargetCleanRules(); |
137 | | |
138 | | // Write the dependency generation rule. This must be done last so |
139 | | // that multiple output pair information is available. |
140 | 0 | this->WriteTargetDependRules(); |
141 | | |
142 | | // close the streams |
143 | 0 | this->CloseFileStreams(); |
144 | 0 | } |