BUG: Fix find_* search order with path suffixes
[cmake.git] / Source / cmLocalVisualStudioGenerator.cxx
blob3191a1fecfb75948615346e81b5f36d5449e054f
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmLocalVisualStudioGenerator.cxx,v $
5 Language: C++
6 Date: $Date: 2008-01-15 19:00:52 $
7 Version: $Revision: 1.16 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmLocalVisualStudioGenerator.h"
18 #include "cmGlobalGenerator.h"
19 #include "cmMakefile.h"
20 #include "cmSourceFile.h"
21 #include "cmSystemTools.h"
22 #include "windows.h"
24 //----------------------------------------------------------------------------
25 cmLocalVisualStudioGenerator::cmLocalVisualStudioGenerator()
27 this->WindowsShell = true;
28 this->WindowsVSIDE = true;
31 //----------------------------------------------------------------------------
32 cmLocalVisualStudioGenerator::~cmLocalVisualStudioGenerator()
36 //----------------------------------------------------------------------------
37 bool cmLocalVisualStudioGenerator::SourceFileCompiles(const cmSourceFile* sf)
39 // Identify the language of the source file.
40 if(const char* lang = this->GetSourceFileLanguage(*sf))
42 // Check whether this source will actually be compiled.
43 return (!sf->GetCustomCommand() &&
44 !sf->GetPropertyAsBool("HEADER_FILE_ONLY") &&
45 !sf->GetPropertyAsBool("EXTERNAL_OBJECT"));
47 else
49 // Unknown source file language. Assume it will not be compiled.
50 return false;
54 //----------------------------------------------------------------------------
55 void cmLocalVisualStudioGenerator::CountObjectNames(
56 const std::vector<cmSourceGroup>& groups,
57 std::map<cmStdString, int>& counts)
59 for(unsigned int i = 0; i < groups.size(); ++i)
61 cmSourceGroup sg = groups[i];
62 std::vector<const cmSourceFile*> const& srcs = sg.GetSourceFiles();
63 for(std::vector<const cmSourceFile*>::const_iterator s = srcs.begin();
64 s != srcs.end(); ++s)
66 const cmSourceFile* sf = *s;
67 if(this->SourceFileCompiles(sf))
69 std::string objectName = cmSystemTools::LowerCase(
70 cmSystemTools::GetFilenameWithoutLastExtension(
71 sf->GetFullPath()));
72 objectName += ".obj";
73 counts[objectName] += 1;
76 this->CountObjectNames(sg.GetGroupChildren(), counts);
80 //----------------------------------------------------------------------------
81 void cmLocalVisualStudioGenerator::InsertNeedObjectNames(
82 const std::vector<cmSourceGroup>& groups,
83 std::map<cmStdString, int>& count)
85 for(unsigned int i = 0; i < groups.size(); ++i)
87 cmSourceGroup sg = groups[i];
88 std::vector<const cmSourceFile*> const& srcs = sg.GetSourceFiles();
89 for(std::vector<const cmSourceFile*>::const_iterator s = srcs.begin();
90 s != srcs.end(); ++s)
92 const cmSourceFile* sf = *s;
93 if(this->SourceFileCompiles(sf))
95 std::string objectName = cmSystemTools::LowerCase(
96 cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath()));
97 objectName += ".obj";
98 if(count[objectName] > 1)
100 this->NeedObjectName.insert(sf);
104 this->InsertNeedObjectNames(sg.GetGroupChildren(), count);
109 //----------------------------------------------------------------------------
110 void cmLocalVisualStudioGenerator::ComputeObjectNameRequirements
111 (std::vector<cmSourceGroup> const& sourceGroups)
113 // Clear the current set of requirements.
114 this->NeedObjectName.clear();
116 // Count the number of object files with each name. Note that
117 // windows file names are not case sensitive.
118 std::map<cmStdString, int> objectNameCounts;
119 this->CountObjectNames(sourceGroups, objectNameCounts);
121 // For all source files producing duplicate names we need unique
122 // object name computation.
123 this->InsertNeedObjectNames(sourceGroups, objectNameCounts);
126 //----------------------------------------------------------------------------
127 std::string
128 cmLocalVisualStudioGenerator
129 ::ConstructScript(const cmCustomCommandLines& commandLines,
130 const char* workingDirectory,
131 const char* configName,
132 bool escapeOldStyle,
133 bool escapeAllowMakeVars,
134 const char* newline_text)
136 // Avoid leading or trailing newlines.
137 const char* newline = "";
139 // Store the script in a string.
140 std::string script;
141 if(workingDirectory)
143 // Change the working directory.
144 script += newline;
145 newline = newline_text;
146 script += "cd ";
147 script += this->Convert(workingDirectory, START_OUTPUT, SHELL);
149 // Change the working drive.
150 if(workingDirectory[0] && workingDirectory[1] == ':')
152 script += newline;
153 newline = newline_text;
154 script += workingDirectory[0];
155 script += workingDirectory[1];
158 // for visual studio IDE add extra stuff to the PATH
159 // if CMAKE_MSVCIDE_RUN_PATH is set.
160 if(this->Makefile->GetDefinition("MSVC_IDE"))
162 const char* extraPath =
163 this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
164 if(extraPath)
166 script += newline;
167 newline = newline_text;
168 script += "set PATH=";
169 script += extraPath;
170 script += ";%PATH%";
173 // Write each command on a single line.
174 for(cmCustomCommandLines::const_iterator cl = commandLines.begin();
175 cl != commandLines.end(); ++cl)
177 // Start a new line.
178 script += newline;
179 newline = newline_text;
181 // Start with the command name.
182 const cmCustomCommandLine& commandLine = *cl;
183 std::string commandName = this->GetRealLocation(commandLine[0].c_str(),
184 configName);
185 if(!workingDirectory)
187 script += this->Convert(commandName.c_str(),START_OUTPUT,SHELL);
189 else
191 script += this->Convert(commandName.c_str(),NONE,SHELL);
194 // Add the arguments.
195 for(unsigned int j=1;j < commandLine.size(); ++j)
197 script += " ";
198 if(escapeOldStyle)
200 script += this->EscapeForShellOldStyle(commandLine[j].c_str());
202 else
204 script += this->EscapeForShell(commandLine[j].c_str(),
205 escapeAllowMakeVars);
209 return script;