@@ -50,6 +50,7 @@ void _swift_initRuntimePath(void *);
50
50
void _swift_initRootPath (void *);
51
51
const char *_swift_getDefaultRootPath ();
52
52
char *_swift_getAuxExePathIn (const char *path, const char *name);
53
+ const char *_swift_tryAuxExePath (const char *name, const char *path, ...);
53
54
54
55
bool _swift_isPathSep (char ch) {
55
56
#ifdef _WIN32
@@ -134,22 +135,31 @@ _swift_getDefaultRootPath()
134
135
// +-------------- ptr - 10
135
136
ptr -= 10 ;
136
137
} else {
137
- // We *might* be in a <platform> directory, so scan backwards for that too
138
+ // We *might* be in a <platform> or <platform>/<arch> directory, so scan
139
+ // backwards for that too
140
+ bool found = false ;
138
141
const char *platform = ptr;
139
- while (platform > runtimePath && !_swift_isPathSep (*--platform));
140
142
141
- if (_swift_lookingAtLibSwift (platform, runtimePath)) {
143
+ for (unsigned n = 0 ; n < 2 ; ++n) {
144
+ while (platform > runtimePath && !_swift_isPathSep (*--platform));
142
145
143
- // When we get here, we have:
144
- //
145
- // /some/path/to/some/thing/lib/swift/macosx/libswiftCore.dylib
146
- // ^ ^ ^
147
- // | | +---- ptr
148
- // | +----------- platform
149
- // +--------------------- platform - 10
146
+ if (_swift_lookingAtLibSwift (platform, runtimePath)) {
150
147
151
- ptr = platform - 10 ;
152
- } else {
148
+ // When we get here, we have:
149
+ //
150
+ // /some/path/to/some/thing/lib/swift/macosx/libswiftCore.dylib
151
+ // ^ ^ ^
152
+ // | | +---- ptr
153
+ // | +----------- platform
154
+ // +--------------------- platform - 10
155
+
156
+ ptr = platform - 10 ;
157
+ found = true ;
158
+ break ;
159
+ }
160
+ }
161
+
162
+ if (!found) {
153
163
// We *might* also be in a bin directory, for instance on Windows, so
154
164
// check if we should remove that also.
155
165
if (_swift_lookingAtBin (ptr, runtimePath)) {
@@ -178,13 +188,15 @@ _swift_getDefaultRootPath()
178
188
179
189
// Join paths together
180
190
char *
181
- _swift_joinPaths (const char *path, ... )
191
+ _swift_joinPathsV (const char *path, va_list val )
182
192
{
183
- va_list val ;
193
+ va_list val2 ;
184
194
size_t baseLen = 0 ;
185
195
size_t totalLen = 0 ;
186
196
const char *pathSeg;
187
197
198
+ va_copy (val2, val);
199
+
188
200
baseLen = std::strlen (path);
189
201
while (baseLen && _swift_isPathSep (path[baseLen - 1 ]))
190
202
--baseLen;
@@ -194,15 +206,13 @@ _swift_joinPaths(const char *path, ...)
194
206
else
195
207
totalLen = baseLen;
196
208
197
- va_start (val, path);
198
- while ((pathSeg = va_arg (val, const char *))) {
209
+ while ((pathSeg = va_arg (val2, const char *))) {
199
210
size_t len = std::strlen (pathSeg);
200
211
while (len && _swift_isPathSep (pathSeg[len - 1 ]))
201
212
--len;
202
213
if (len)
203
214
totalLen += 1 + len;
204
215
}
205
- va_end (val);
206
216
207
217
char *buffer = static_cast <char *>(std::malloc (totalLen + 1 ));
208
218
char *ptr = buffer;
@@ -214,7 +224,6 @@ _swift_joinPaths(const char *path, ...)
214
224
ptr += baseLen;
215
225
}
216
226
217
- va_start (val, path);
218
227
while ((pathSeg = va_arg (val, const char *))) {
219
228
size_t len = std::strlen (pathSeg);
220
229
while (len && _swift_isPathSep (pathSeg[len - 1 ]))
@@ -230,6 +239,19 @@ _swift_joinPaths(const char *path, ...)
230
239
return buffer;
231
240
}
232
241
242
+ char *
243
+ _swift_joinPaths (const char *path, ...)
244
+ {
245
+ va_list val;
246
+ char *result;
247
+
248
+ va_start (val, path);
249
+ result = _swift_joinPathsV (path, val);
250
+ va_end (val);
251
+
252
+ return result;
253
+ }
254
+
233
255
void
234
256
_swift_initRootPath (void *)
235
257
{
@@ -340,81 +362,58 @@ swift_getAuxiliaryExecutablePath(const char *name)
340
362
const char *rootPath = swift_getRootPath ();
341
363
342
364
const char *platformName = SWIFT_LIB_SUBDIR;
365
+ const char *archName = SWIFT_ARCH;
343
366
344
367
// <rootPath>/libexec/swift/<platformName>
345
- {
346
- char *libexecPlatPath = _swift_joinPaths (rootPath,
347
- " libexec" PATHSEP_STR " swift" ,
348
- platformName, nullptr );
349
-
350
- // If it exists, look there
351
- if (_swift_exists (libexecPlatPath)) {
352
- char *result = _swift_getAuxExePathIn (libexecPlatPath, name);
353
-
354
- if (_swift_exists (result)) {
355
- std::free (libexecPlatPath);
356
-
357
- return result;
358
- }
359
-
360
- std::free (result);
361
- }
362
-
368
+ if (const char *result = _swift_tryAuxExePath (name,
369
+ rootPath,
370
+ " libexec" , " swift" ,
371
+ platformName, nullptr )) {
372
+ return result;
373
+ }
363
374
364
- std::free (libexecPlatPath);
375
+ // <rootPath>/libexec/swift/<platformName>/<arch>
376
+ if (const char *result = _swift_tryAuxExePath (name,
377
+ rootPath,
378
+ " libexec" , " swift" ,
379
+ platformName,
380
+ archName, nullptr )) {
381
+ return result;
365
382
}
366
383
367
384
// <rootPath>/libexec/swift
368
- {
369
- char *libexecPath = _swift_joinPaths (rootPath,
370
- " libexec" PATHSEP_STR " swift" ,
371
- nullptr );
372
-
373
- // If it exists, look there
374
- if (_swift_exists (libexecPath)) {
375
- char *result = _swift_getAuxExePathIn (libexecPath, name);
376
-
377
- if (_swift_exists (result)) {
378
- std::free (libexecPath);
379
-
380
- return result;
381
- }
382
-
383
- std::free (result);
384
- }
385
+ if (const char *result = _swift_tryAuxExePath (name,
386
+ rootPath,
387
+ " libexec" , " swift" ,
388
+ nullptr )) {
389
+ return result;
390
+ }
385
391
386
- std::free (libexecPath);
392
+ // <rootPath>/libexec/swift/<arch>
393
+ if (const char *result = _swift_tryAuxExePath (name,
394
+ rootPath,
395
+ " libexec" , " swift" ,
396
+ archName, nullptr )) {
397
+ return result;
387
398
}
388
399
389
400
// <rootPath>/bin
390
- {
391
- char *binPath = _swift_joinPaths (rootPath, " bin" , nullptr );
392
-
393
- // If bin exists, look there
394
- if (_swift_exists (binPath)) {
395
- char *result = _swift_getAuxExePathIn (binPath, name);
396
-
397
- if (_swift_exists (result)) {
398
- std::free (binPath);
399
-
400
- return result;
401
- }
402
-
403
- std::free (result);
404
- }
405
-
406
- std::free (binPath);
401
+ if (const char *result = _swift_tryAuxExePath (name,
402
+ rootPath,
403
+ " bin" , nullptr )) {
404
+ return result;
407
405
}
408
406
409
- // Otherwise, look in the root itself
410
- char *result = _swift_getAuxExePathIn (rootPath, name);
411
-
412
- if (_swift_exists (result))
407
+ // <rootPath>/bin/<arch>
408
+ if (const char *result = _swift_tryAuxExePath (name,
409
+ rootPath,
410
+ " bin" ,
411
+ archName, nullptr )) {
413
412
return result;
413
+ }
414
414
415
- std::free (result);
416
-
417
- return nullptr ;
415
+ // Otherwise, look in the root itself
416
+ return _swift_tryAuxExePath (name, rootPath, nullptr );
418
417
}
419
418
420
419
namespace {
@@ -444,6 +443,32 @@ _swift_getAuxExePathIn(const char *path, const char *name)
444
443
return fullPath;
445
444
}
446
445
446
+ const char *
447
+ _swift_tryAuxExePath (const char *name, const char *path, ...)
448
+ {
449
+ va_list val;
450
+ char *fullPath;
451
+ va_start (val, path);
452
+ fullPath = _swift_joinPathsV (path, val);
453
+ va_end (val);
454
+
455
+ if (_swift_exists (fullPath)) {
456
+ char *result = _swift_getAuxExePathIn (fullPath, name);
457
+
458
+ if (_swift_exists (result)) {
459
+ std::free (fullPath);
460
+
461
+ return result;
462
+ }
463
+
464
+ std::free (result);
465
+ }
466
+
467
+ std::free (fullPath);
468
+
469
+ return nullptr ;
470
+ }
471
+
447
472
#if !defined(_WIN32) || defined(__CYGWIN__)
448
473
void
449
474
_swift_initRuntimePath (void *) {
@@ -494,7 +519,7 @@ _swift_initRuntimePath(void *) {
494
519
495
520
std::free (lpFilename);
496
521
497
- runtimePath = swift::win32::copyUTF8FromWide (lpWin32Filename);
522
+ runtimePath = _swift_win32_copyUTF8FromWide (lpWin32Filename);
498
523
if (!runtimePath) {
499
524
swift::fatalError (/* flags = */ 0 ,
500
525
" Unable to convert Swift runtime path to UTF-8: %lx, %d\n " ,
@@ -519,7 +544,7 @@ bool _swift_exists(const char *path)
519
544
struct stat st;
520
545
return stat (path, &st) == 0 ;
521
546
#else
522
- wchar_t *wszPath = swift::win32::copyWideFromUTF8 (path);
547
+ wchar_t *wszPath = _swift_win32_copyWideFromUTF8 (path);
523
548
bool result = GetFileAttributesW (wszPath) != INVALID_FILE_ATTRIBUTES;
524
549
free (wszPath);
525
550
return result;
0 commit comments