resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmInstallFilesCommand.cxx
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #include "cmInstallFilesCommand.h"
4
5 #include <cm/memory>
6
7 #include "cmExecutionStatus.h"
8 #include "cmGeneratorExpression.h"
9 #include "cmGlobalGenerator.h"
10 #include "cmInstallFilesGenerator.h"
11 #include "cmInstallGenerator.h"
12 #include "cmLocalGenerator.h"
13 #include "cmMakefile.h"
14 #include "cmRange.h"
15 #include "cmStringAlgorithms.h"
16 #include "cmSystemTools.h"
17
18 class cmListFileBacktrace;
19
20 static std::string FindInstallSource(cmMakefile& makefile, const char* name);
21 static void CreateInstallGenerator(cmMakefile& makefile,
22                                    std::string const& dest,
23                                    std::vector<std::string> const& files);
24 static void FinalAction(cmMakefile& makefile, std::string const& dest,
25                         std::vector<std::string> const& args);
26
27 bool cmInstallFilesCommand(std::vector<std::string> const& args,
28                            cmExecutionStatus& status)
29 {
30   if (args.size() < 2) {
31     status.SetError("called with incorrect number of arguments");
32     return false;
33   }
34
35   cmMakefile& mf = status.GetMakefile();
36
37   // Enable the install target.
38   mf.GetGlobalGenerator()->EnableInstallTarget();
39
40   std::string const& dest = args[0];
41
42   if ((args.size() > 1) && (args[1] == "FILES")) {
43     std::vector<std::string> files;
44     for (std::string const& arg : cmMakeRange(args).advance(2)) {
45       // Find the source location for each file listed.
46       files.push_back(FindInstallSource(mf, arg.c_str()));
47     }
48     CreateInstallGenerator(mf, dest, files);
49   } else {
50     std::vector<std::string> finalArgs(args.begin() + 1, args.end());
51     mf.AddGeneratorAction(
52       [dest, finalArgs](cmLocalGenerator& lg, const cmListFileBacktrace&) {
53         FinalAction(*lg.GetMakefile(), dest, finalArgs);
54       });
55   }
56
57   mf.GetGlobalGenerator()->AddInstallComponent(
58     mf.GetSafeDefinition("CMAKE_INSTALL_DEFAULT_COMPONENT_NAME"));
59
60   return true;
61 }
62
63 static void FinalAction(cmMakefile& makefile, std::string const& dest,
64                         std::vector<std::string> const& args)
65 {
66   std::string testf;
67   std::string const& ext = args[0];
68   std::vector<std::string> installFiles;
69
70   // two different options
71   if (args.size() > 1) {
72     // now put the files into the list
73     auto s = args.begin();
74     ++s;
75     // for each argument, get the files
76     for (; s != args.end(); ++s) {
77       // replace any variables
78       std::string const& temps = *s;
79       if (!cmSystemTools::GetFilenamePath(temps).empty()) {
80         testf = cmSystemTools::GetFilenamePath(temps) + "/" +
81           cmSystemTools::GetFilenameWithoutLastExtension(temps) + ext;
82       } else {
83         testf = cmSystemTools::GetFilenameWithoutLastExtension(temps) + ext;
84       }
85
86       // add to the result
87       installFiles.push_back(FindInstallSource(makefile, testf.c_str()));
88     }
89   } else // reg exp list
90   {
91     std::vector<std::string> files;
92     std::string const& regex = args[0];
93     cmSystemTools::Glob(makefile.GetCurrentSourceDirectory(), regex, files);
94
95     auto s = files.begin();
96     // for each argument, get the files
97     for (; s != files.end(); ++s) {
98       installFiles.push_back(FindInstallSource(makefile, s->c_str()));
99     }
100   }
101
102   CreateInstallGenerator(makefile, dest, installFiles);
103 }
104
105 static void CreateInstallGenerator(cmMakefile& makefile,
106                                    std::string const& dest,
107                                    std::vector<std::string> const& files)
108 {
109   // Construct the destination.  This command always installs under
110   // the prefix.  We skip the leading slash given by the user.
111   std::string destination = dest.substr(1);
112   cmSystemTools::ConvertToUnixSlashes(destination);
113   if (destination.empty()) {
114     destination = ".";
115   }
116
117   // Use a file install generator.
118   const std::string no_permissions;
119   const std::string no_rename;
120   bool no_exclude_from_all = false;
121   std::string no_component =
122     makefile.GetSafeDefinition("CMAKE_INSTALL_DEFAULT_COMPONENT_NAME");
123   std::vector<std::string> no_configurations;
124   cmInstallGenerator::MessageLevel message =
125     cmInstallGenerator::SelectMessageLevel(&makefile);
126   makefile.AddInstallGenerator(cm::make_unique<cmInstallFilesGenerator>(
127     files, destination, false, no_permissions, no_configurations, no_component,
128     message, no_exclude_from_all, no_rename, false, makefile.GetBacktrace()));
129 }
130
131 /**
132  * Find a file in the build or source tree for installation given a
133  * relative path from the CMakeLists.txt file.  This will favor files
134  * present in the build tree.  If a full path is given, it is just
135  * returned.
136  */
137 static std::string FindInstallSource(cmMakefile& makefile, const char* name)
138 {
139   if (cmSystemTools::FileIsFullPath(name) ||
140       cmGeneratorExpression::Find(name) == 0) {
141     // This is a full path.
142     return name;
143   }
144
145   // This is a relative path.
146   std::string tb = cmStrCat(makefile.GetCurrentBinaryDirectory(), '/', name);
147   std::string ts = cmStrCat(makefile.GetCurrentSourceDirectory(), '/', name);
148
149   if (cmSystemTools::FileExists(tb)) {
150     // The file exists in the binary tree.  Use it.
151     return tb;
152   }
153   if (cmSystemTools::FileExists(ts)) {
154     // The file exists in the source tree.  Use it.
155     return ts;
156   }
157   // The file doesn't exist.  Assume it will be present in the
158   // binary tree when the install occurs.
159   return tb;
160 }