Imported Upstream version 2.8.9
[platform/upstream/cmake.git] / Source / cmUtilitySourceCommand.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #include "cmUtilitySourceCommand.h"
13
14 // cmUtilitySourceCommand
15 bool cmUtilitySourceCommand
16 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
17 {
18   if(args.size() < 3)
19     {
20     this->SetError("called with incorrect number of arguments");
21     return false;
22     }
23
24   std::vector<std::string>::const_iterator arg = args.begin();
25   
26   // The first argument is the cache entry name.
27   std::string cacheEntry = *arg++;
28   const char* cacheValue =
29     this->Makefile->GetDefinition(cacheEntry.c_str());
30   // If it exists already and appears up to date then we are done.  If
31   // the string contains "(IntDir)" but that is not the
32   // CMAKE_CFG_INTDIR setting then the value is out of date.
33   const char* intDir = 
34     this->Makefile->GetRequiredDefinition("CMAKE_CFG_INTDIR");
35
36   bool haveCacheValue = false;
37   if (this->Makefile->IsOn("CMAKE_CROSSCOMPILING"))
38     {
39     haveCacheValue = (cacheValue != 0);
40     if (!haveCacheValue)
41       {
42       std::string msg = "UTILITY_SOURCE is used in cross compiling mode for ";
43       msg += cacheEntry;
44       msg += ". If your intention is to run this executable, you need to "
45             "preload the cache with the full path to a version of that "
46             "program, which runs on this build machine.";
47       cmSystemTools::Message(msg.c_str() ,"Warning");
48       }
49     }
50   else
51     {
52     haveCacheValue = (cacheValue &&
53      (strstr(cacheValue, "(IntDir)") == 0 ||
54       (intDir && strcmp(intDir, "$(IntDir)") == 0)) &&
55      (this->Makefile->GetCacheMajorVersion() != 0 &&
56       this->Makefile->GetCacheMinorVersion() != 0 ));
57     }
58
59   if(haveCacheValue)
60     {
61     return true;
62     }
63   
64   // The second argument is the utility's executable name, which will be
65   // needed later.
66   std::string utilityName = *arg++;
67   
68   // The third argument specifies the relative directory of the source
69   // of the utility.
70   std::string relativeSource = *arg++;
71   std::string utilitySource = this->Makefile->GetCurrentDirectory();
72   utilitySource = utilitySource+"/"+relativeSource;
73   
74   // If the directory doesn't exist, the source has not been included.
75   if(!cmSystemTools::FileExists(utilitySource.c_str()))
76     { return true; }
77   
78   // Make sure all the files exist in the source directory.
79   while(arg != args.end())
80     {
81     std::string file = utilitySource+"/"+*arg++;
82     if(!cmSystemTools::FileExists(file.c_str()))
83       { return true; }
84     }
85   
86   // The source exists.
87   std::string cmakeCFGout = 
88     this->Makefile->GetRequiredDefinition("CMAKE_CFG_INTDIR");
89   std::string utilityDirectory = this->Makefile->GetCurrentOutputDirectory();
90   std::string exePath;
91   if (this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
92     {
93     exePath = this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
94     }
95   if(exePath.size())
96     {
97     utilityDirectory = exePath;
98     }
99   else
100     {
101     utilityDirectory += "/"+relativeSource;
102     }
103   
104   // Construct the cache entry for the executable's location.
105   std::string utilityExecutable =
106     utilityDirectory+"/"+cmakeCFGout+"/"
107     +utilityName+this->Makefile->GetDefinition("CMAKE_EXECUTABLE_SUFFIX");
108
109   // make sure we remove any /./ in the name
110   cmSystemTools::ReplaceString(utilityExecutable, "/./", "/");
111   
112   // Enter the value into the cache.
113   this->Makefile->AddCacheDefinition(cacheEntry.c_str(),
114                                  utilityExecutable.c_str(),
115                                  "Path to an internal program.",
116                                  cmCacheManager::FILEPATH);
117   // add a value into the cache that maps from the
118   // full path to the name of the project
119   cmSystemTools::ConvertToUnixSlashes(utilityExecutable);
120   this->Makefile->AddCacheDefinition(utilityExecutable.c_str(),
121                                  utilityName.c_str(),
122                                  "Executable to project name.",
123                                  cmCacheManager::INTERNAL);
124   
125   return true;
126 }
127