Skip to content

Commit 485afc4

Browse files
committed
check validity of $PREFIX/SDK and set SDKROOT=xcrun --show-sdk-path if it's not
1 parent fab7422 commit 485afc4

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

build/stubs/gfortran.c

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,34 @@
2828

2929
static char fn[512];
3030

31+
#ifdef PREFIX
32+
/* prepend PREFIX so the prefix tools are found first */
33+
static int add_PATH(const char *prefix) {
34+
char *path = getenv("PATH");
35+
size_t len = strlen(path) + strlen(PREFIX) + 8;
36+
char *npath = (char*) malloc(len);
37+
int n;
38+
if (!npath) {
39+
fprintf(stderr, "Out of memory.\n");
40+
return 1;
41+
}
42+
n = snprintf(npath, len, "%s/bin:%s", PREFIX, path);
43+
if (n > 0 && n < len)
44+
setenv("PATH", npath, 1);
45+
free(npath);
46+
return 0;
47+
}
48+
49+
static int file_exists(const char *fn) {
50+
FILE *f = fopen(fn, "r");
51+
if (f) fclose(f);
52+
return f ? 1 : 0;
53+
}
54+
#endif
55+
3156
int main(int argc, char **argv) {
3257
int i = 1, j = 1, archs = 0;
33-
const char *arch = 0;
58+
const char *arch = 0, *sdk;
3459
while (i < argc) {
3560
if (!strcmp(argv[i], "-arch")) {
3661
if (i + 1 < argc) {
@@ -56,6 +81,31 @@ int main(int argc, char **argv) {
5681
if (!strcmp(arch, "arm64"))
5782
arch = "aarch64";
5883
#ifdef PREFIX
84+
sdk = getenv("SDKROOT");
85+
if (sdk && !*sdk) sdk = 0;
86+
if (!sdk) { /* if there is no SDKROOT, check if the SDK link is correct */
87+
snprintf(fn, sizeof(fn), "%s/SDK/SDKSettings.plist", PREFIX);
88+
if (!file_exists(fn)) {
89+
snprintf(fn, sizeof(fn), "%s/SDK/SDKSettings.json", PREFIX);
90+
if (!file_exists(fn)) { /* invalid, detect SDK */
91+
FILE *f = popen("/usr/bin/xcrun --show-sdk-path", "r");
92+
if (!f || !fgets(fn, sizeof(fn), f)) {
93+
fprintf(stderr, "** ERROR: %s/SDK is invalid and cannot determine SDK path!\n\n", PREFIX);
94+
if (f) fclose(f);
95+
return 1;
96+
} else {
97+
char *c = strchr(fn, '\n');
98+
fclose(f);
99+
if (c) *c = 0;
100+
if (*fn) {
101+
fprintf(stderr,"Warning: %s/SDK is invalid, setting SDKROOT=%s\n Consider running the following to fix (or set SDKROOT):\n ln -sfn %s %s/SDK\n", PREFIX, fn, fn, PREFIX);
102+
setenv("SDKROOT", fn, 1);
103+
}
104+
}
105+
}
106+
}
107+
}
108+
add_PATH(PREFIX);
59109
snprintf(fn, sizeof(fn), "%s/bin/%s-%s-gfortran", PREFIX, arch, BUILD);
60110
#else
61111
snprintf(fn, sizeof(fn), "%s-%s-gfortran", arch, BUILD);

0 commit comments

Comments
 (0)