2 This and the shell builder was originally written by
3 https://github.com/tpoechtrager but I had to modify both so that
4 they played nicely and were reproducible with nixpkgs. Much thanks
5 to MixRank for letting me work on this.
6 Edgar Aroutiounian <edgar.factorial@gmail.com>
10 #define TARGET_CPU "armv7"
14 #define OS_VER_MIN "4.2"
17 #ifndef NIX_APPLE_HDRS
18 #define NIX_APPLE_HDRS ""
21 #ifndef NIX_APPLE_FRAMEWORKS
22 #define NIX_APPLE_FRAMEWORKS ""
25 #ifndef NIX_APPLE_PRIV_FRAMEWORKS
26 #define NIX_APPLE_PRIV_FRAMEWORKS ""
39 #include <mach-o/dyld.h>
42 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
43 #include <sys/sysctl.h>
47 #include <sys/types.h>
52 char *get_executable_path(char *epath
, size_t buflen
)
56 unsigned int l
= buflen
;
57 if (_NSGetExecutablePath(epath
, &l
) != 0) return NULL
;
58 #elif defined(__FreeBSD__) || defined(__DragonFly__)
59 int mib
[4] = { CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1 };
61 if (sysctl(mib
, 4, epath
, &l
, NULL
, 0) != 0) return NULL
;
62 #elif defined(__OpenBSD__)
70 mib
[1] = KERN_PROC_ARGS
;
72 mib
[3] = KERN_PROC_ARGV
;
73 if (sysctl(mib
, 4, NULL
, &len
, NULL
, 0) < 0)
75 if (!(argv
= malloc(len
)))
77 if (sysctl(mib
, 4, argv
, &len
, NULL
, 0) < 0)
80 if (*comm
== '/' || *comm
== '.') {
82 if ((rpath
= realpath(comm
, NULL
))) {
83 strlcpy(epath
, rpath
, buflen
);
89 char *xpath
= strdup(getenv("PATH"));
90 char *path
= strtok_r(xpath
, ":", &sp
);
95 snprintf(epath
, buflen
, "%s/%s", path
, comm
);
96 if (!stat(epath
, &st
) && (st
.st_mode
& S_IXUSR
)) {
100 path
= strtok_r(NULL
, ":", &sp
);
105 if (!ok
) return NULL
;
108 ssize_t l
= readlink("/proc/self/exe", epath
, buflen
);
110 if (l
<= 0) return NULL
;
111 epath
[buflen
- 1] = '\0';
112 p
= strrchr(epath
, '/');
117 char *get_filename(char *str
)
119 char *p
= strrchr(str
, '/');
120 return p
? &p
[1] : str
;
123 void target_info(char *argv
[], char **triple
, char **compiler
)
125 char *p
= get_filename(argv
[0]);
126 char *x
= strrchr(p
, '-');
133 void env(char **p
, const char *name
, char *fallback
)
135 char *ev
= getenv(name
);
136 if (ev
) { *p
= ev
; return; }
140 int main(int argc
, char *argv
[])
142 char **args
= alloca(sizeof(char*) * (argc
+ 17));
145 char execpath
[PATH_MAX
+1];
146 char sdkpath
[PATH_MAX
+1];
147 char codesign_allocate
[64];
150 char *compiler
, *target
, *sdk
, *cpu
, *osmin
;
152 target_info(argv
, &target
, &compiler
);
154 if (!get_executable_path(execpath
, sizeof(execpath
))) abort();
155 snprintf(sdkpath
, sizeof(sdkpath
), "%s/../SDK", execpath
);
157 snprintf(codesign_allocate
, sizeof(codesign_allocate
),
158 "%s-codesign_allocate", target
);
160 setenv("CODESIGN_ALLOCATE", codesign_allocate
, 1);
161 setenv("IOS_FAKE_CODE_SIGN", "1", 1);
163 env(&sdk
, "IOS_SDK_SYSROOT", sdkpath
);
164 env(&cpu
, "IOS_TARGET_CPU", TARGET_CPU
);
166 env(&osmin
, "IPHONEOS_DEPLOYMENT_TARGET", OS_VER_MIN
);
167 unsetenv("IPHONEOS_DEPLOYMENT_TARGET");
169 snprintf(osvermin
, sizeof(osvermin
), "-miphoneos-version-min=%s", osmin
);
171 for (i
= 1; i
< argc
; ++i
) {
172 if (!strcmp(argv
[i
], "-arch")) {
180 args
[i
++] = compiler
;
181 args
[i
++] = "-target";
183 args
[i
++] = "-isysroot";
185 args
[i
++] = NIX_APPLE_HDRS
;
187 args
[i
++] = NIX_APPLE_FRAMEWORKS
;
189 args
[i
++] = NIX_APPLE_PRIV_FRAMEWORKS
;
196 args
[i
++] = osvermin
;
197 args
[i
++] = "-mlinker-version=253.3";
199 for (j
= 1; j
< argc
; ++i
, ++j
) args
[i
] = argv
[j
];
203 setenv("COMPILER_PATH", execpath
, 1);
205 /* for (k = 0; k < i; k++) */
206 /* printf("Compiler option: %s\n", args[k]); */
207 /* printf("End of Compiler args\n"); */
208 execvp(compiler
, args
);
210 fprintf(stderr
, "cannot invoke compiler, this is a serious bug\n");