-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathApplicationPathHelpers.cxx.in
373 lines (317 loc) · 12.9 KB
/
ApplicationPathHelpers.cxx.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/***********************************************************************************************************************
* OpenStudio(R), Copyright (c) 2008-2017, Alliance for Sustainable Energy, LLC. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
*
* (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other materials provided with the distribution.
*
* (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
* products derived from this software without specific prior written permission from the respective party.
*
* (4) Other than as required in clauses (1) and (2), distributions in any form of modifications or other derivative
* works may not use the "OpenStudio" trademark, "OS", "os", or any other confusingly similar designation without
* specific prior written permission from Alliance for Sustainable Energy, LLC.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**********************************************************************************************************************/
#include <utilities/core/ApplicationPathHelpers.hpp>
#include <utilities/core/PathHelpers.hpp>
#include <OpenStudio.hxx>
#include <sstream>
#include <boost/regex.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include <iostream>
#include <QtGlobal>
#if defined _WIN32
#include <windows.h>
#else
#include <stdio.h>
#include <dlfcn.h>
#endif
bool pathBeginsWith(const openstudio::path &t_shorter, const openstudio::path &t_longer)
{
openstudio::path::const_iterator begin1 = t_shorter.begin();
openstudio::path::const_iterator end1 = t_shorter.end();
openstudio::path::const_iterator begin2 = t_longer.begin();
openstudio::path::const_iterator end2 = t_longer.end();
if (std::distance(begin1, end1) > std::distance(begin2, end2))
{
return false; // the run dir has fewer elements than the build dir - cannot be running from builddir
}
// if the rundir begins with the builddir, we know it's running from the builddir
return std::equal(begin1, end1, begin2);
}
namespace openstudio {
openstudio::path getApplicationSourceDirectory() {
return openstudio::toPath("${CMAKE_SOURCE_DIR}");
}
openstudio::path getApplicationBuildDirectory() {
return openstudio::toPath("${CMAKE_BINARY_DIR}");
}
openstudio::path getApplicationPath() {
#if defined _WIN32
TCHAR szPath[MAX_PATH];
if( GetModuleFileName( nullptr, szPath, MAX_PATH ) ) {
return completeAndNormalize(toPath(szPath));
}
#else
Dl_info info;
if (dladdr("main", &info)) {
return completeAndNormalize(toPath(info.dli_fname));
}
#endif
return openstudio::path();
}
openstudio::path getApplicationDirectory() {
openstudio::path p = getApplicationPath();
if (!p.empty()){
return p.parent_path();
}
return openstudio::path();
}
bool applicationIsRunningFromBuildDirectory()
{
#ifdef _WIN32
openstudio::path buildDir = openstudio::toPath(boost::algorithm::to_upper_copy(openstudio::toString(getApplicationBuildDirectory())));
openstudio::path runDir = openstudio::toPath(boost::algorithm::to_upper_copy(openstudio::toString(getApplicationDirectory())));
#else
openstudio::path buildDir = getApplicationBuildDirectory();
openstudio::path runDir = getApplicationDirectory();
#endif
return pathBeginsWith(buildDir, runDir);
}
openstudio::path getEnergyPlusDirectory() {
return getEnergyPlusExecutable().parent_path();
}
// define the function GetCurrentModule so we can get its address
#if defined _WIN32
HMODULE GetCurrentModule()
{
HMODULE hModule = NULL;
// hModule is NULL if GetModuleHandleEx fails.
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
| GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCTSTR)GetCurrentModule, &hModule);
return hModule;
}
#else
bool GetCurrentModule()
{
return true;
}
#endif
openstudio::path getOpenStudioModule()
{
openstudio::path openstudioDirPath;
#if defined _WIN32
TCHAR szPath[MAX_PATH];
if( GetModuleFileName( GetCurrentModule(), szPath, MAX_PATH ) ) {
openstudioDirPath = completeAndNormalize(toPath(szPath));
}
#else
Dl_info info;
if (dladdr("GetCurrentModule", &info)) {
openstudioDirPath = completeAndNormalize(toPath(info.dli_fname));
}
#endif
return openstudioDirPath;
}
openstudio::path getOpenStudioModuleDirectory()
{
return getOpenStudioModule().parent_path();
}
bool moduleIsRunningFromBuildDirectory()
{
#ifdef _WIN32
openstudio::path buildDir = openstudio::toPath(boost::algorithm::to_upper_copy(openstudio::toString(getApplicationBuildDirectory())));
openstudio::path runDir = openstudio::toPath(boost::algorithm::to_upper_copy(openstudio::toString(getOpenStudioModuleDirectory())));
#else
openstudio::path buildDir = getApplicationBuildDirectory();
openstudio::path runDir = getOpenStudioModuleDirectory();
#endif
return pathBeginsWith(buildDir, runDir);
}
openstudio::path getOpenStudioCLI(){
openstudio::path cliPath;
#if defined _WIN32
cliPath = getOpenStudioModuleDirectory() / toPath("openstudio.exe");
if (!openstudio::filesystem::exists(cliPath)){
#if _DEBUG
cliPath = getOpenStudioModuleDirectory() / toPath("../../Debug/openstudio.exe");
#else
cliPath = getOpenStudioModuleDirectory() / toPath("../../Release/openstudio.exe");
#endif
}
#else
cliPath = getOpenStudioModuleDirectory() / toPath("openstudio");
if (!openstudio::filesystem::exists(cliPath)){
cliPath = getOpenStudioModuleDirectory() / toPath("../openstudio");
}
#endif
if( openstudio::filesystem::exists(cliPath) ){
return cliPath;
}
#if defined _WIN32
cliPath = getOpenStudioModuleDirectory() / toPath("../bin/openstudio.exe");
#else
cliPath = getOpenStudioModuleDirectory() / toPath("../bin/openstudio");
#endif
if( openstudio::filesystem::exists(cliPath) ){
return cliPath;
}
return openstudio::path();
}
openstudio::path getEnergyPlusExecutable() {
openstudio::path cmakeEnergyPlusPath = openstudio::toPath("${ENERGYPLUS_EXE}");
openstudio::path cmakeEnergyPlusDirectory = cmakeEnergyPlusPath.parent_path();
// handle developer case first
if (moduleIsRunningFromBuildDirectory()){
if( openstudio::filesystem::exists(cmakeEnergyPlusPath) ){
return cmakeEnergyPlusPath;
}
}
// next check environment variable
auto envPathString = std::getenv("ENERGYPLUS_EXE_PATH");
if( envPathString ) {
openstudio::path envPath = openstudio::toPath(envPathString);
if( openstudio::filesystem::exists(envPath) ) {
return envPath;
}
}
// next check for installed energyplus
#ifdef _WIN32
openstudio::path installPath = getOpenStudioModuleDirectory() / toPath("/../EnergyPlus/energyplus.exe");
#else
openstudio::path installPath = getOpenStudioModuleDirectory() / toPath("/../EnergyPlus/energyplus");
#endif
if( openstudio::filesystem::exists(installPath) ) {
return installPath;
}
// next check default location
openstudio::path energyPlusExeName = cmakeEnergyPlusPath.filename();
openstudio::path energyPlusDirectoryName = cmakeEnergyPlusDirectory.filename();
#ifdef Q_OS_LINUX
auto systemPath = openstudio::toPath("/usr/local") / energyPlusDirectoryName / energyPlusExeName;
#elif defined(Q_OS_MAC)
auto systemPath = openstudio::toPath("/Applications") / energyPlusDirectoryName / energyPlusExeName;
#else
auto systemPath = openstudio::toPath("C:/") / energyPlusDirectoryName / energyPlusExeName;
#endif
if( openstudio::filesystem::exists(systemPath) ) {
return systemPath;
}
std::string energyPlusDirectoryNameStr = toString(energyPlusDirectoryName);
std::string energyPlusDirectoryNameStr2 = boost::replace_first_copy(energyPlusDirectoryNameStr,"EnergyPlus-","EnergyPlusV");
if (energyPlusDirectoryNameStr != energyPlusDirectoryNameStr2){
#ifdef Q_OS_LINUX
systemPath = openstudio::toPath("/usr/local") / toPath(energyPlusDirectoryNameStr2) / energyPlusExeName;
#elif defined(Q_OS_MAC)
systemPath = openstudio::toPath("/Applications") / toPath(energyPlusDirectoryNameStr2) / energyPlusExeName;
#else
systemPath = openstudio::toPath("C:/") / toPath(energyPlusDirectoryNameStr2) / energyPlusExeName;
#endif
if( openstudio::filesystem::exists(systemPath) ) {
return systemPath;
}
}
// next check user path
//QString userPath = QStandardPaths::findExecutable("energyplus");
//if (!userPath.isEmpty()){
// openstudio::path path = openstudio::toPath(userPath);
// if( openstudio::filesystem::exists(path) ) {
// return path;
// }
//}
return openstudio::path();
}
openstudio::path getRadianceDirectory() {
// handle developer case first
if (moduleIsRunningFromBuildDirectory()){
openstudio::path cmakeRadianceDirectory = openstudio::toPath("${RADIANCE_LOCATION}");
if( openstudio::filesystem::exists(cmakeRadianceDirectory) ){
return cmakeRadianceDirectory;
}
}
// next check environment variable
auto envPathString = std::getenv("OS_RAYPATH");
if( envPathString ) {
openstudio::path envPath = openstudio::toPath(envPathString);
if( openstudio::filesystem::exists(envPath) ) {
return envPath;
}
}
// next check for installed energyplus
openstudio::path installPath = getOpenStudioModuleDirectory() / toPath("/../Radiance");
if( openstudio::filesystem::exists(installPath) ) {
return installPath;
}
// next check default location
#ifdef Q_OS_LINUX
auto systemPath = openstudio::toPath("/usr/local/radiance");
#elif defined(Q_OS_MAC)
auto systemPath = openstudio::toPath("/usr/local/radiance");
#else
auto systemPath = openstudio::toPath("C:/Program Files/radiance");
#endif
if( openstudio::filesystem::exists(systemPath) ) {
return systemPath;
}
return openstudio::path();
}
openstudio::path getPerlExecutable() {
#if defined (Q_OS_LINUX) || defined (Q_OS_MAC)
// check environment variable
auto envPathString = std::getenv("PERL_EXE_PATH");
if( envPathString ) {
openstudio::path envPath = openstudio::toPath(envPathString);
if( openstudio::filesystem::exists(envPath) ) {
return envPath;
}
}
// next check for system perl
openstudio::path systemPath = toPath("/usr/bin/perl");
if( openstudio::filesystem::exists(systemPath) ) {
return systemPath;
}
#else
// handle developer case first
if (moduleIsRunningFromBuildDirectory()){
openstudio::path cmakePerlPath = openstudio::toPath("${PERL_EXE}");
if( openstudio::filesystem::exists(cmakePerlPath) ){
return cmakePerlPath;
}
}
// next check environment variable
auto envPathString = std::getenv("PERL_EXE_PATH");
if( envPathString ) {
openstudio::path envPath = openstudio::toPath(envPathString);
if( openstudio::filesystem::exists(envPath) ) {
return envPath;
}
}
// next check for installed perl
openstudio::path installPath = getOpenStudioModuleDirectory() / toPath("/../Perl/bin/perl.exe");
if( openstudio::filesystem::exists(installPath) ) {
return installPath;
}
// alternate install location (to be removed)
installPath = getOpenStudioModuleDirectory() / toPath("/../Perl/perl/bin/perl.exe");
if( openstudio::filesystem::exists(installPath) ) {
return installPath;
}
#endif
return openstudio::path();
}
} // openstudio