gcc-uClibc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright (C) 2000 Manuel Novoa III
  3. * Copyright (C) 2002 Erik Andersen
  4. *
  5. * This is a crude wrapper to use uClibc with gcc.
  6. * It was originally written to work around ./configure for ext2fs-utils.
  7. * It certainly can be improved, but it works for me in the normal cases.
  8. *
  9. * April 7, 2001
  10. *
  11. * A bug was fixed in building the gcc command line when dynamic linking.
  12. * The functions dlopen, etc. now work. At this time, you must make sure
  13. * the correct libdl.so is included however. It is safest to, for example,
  14. * add /lib/libdl.so.1 if using ld-linux.so.1 rather than adding -ldl to the
  15. * command line.
  16. *
  17. * Note: This is only a problem if devel and target archs are the same. To
  18. * avoid the problem, you can use a customized dynamic linker.
  19. *
  20. *
  21. * April 18, 2001
  22. *
  23. * The wrapper now works with either installed and uninstalled uClibc versions.
  24. * If you want to use the uninstalled header files and libs, either include
  25. * the string "build" in the invocation name such as
  26. * 'ln -s <ARCH>-uclibc-gcc <ARCH>-uclibc-gcc-build'
  27. * or include it in the environment variable setting of UCLIBC_GCC.
  28. * Note: This automatically enables the "rpath" behavior described below.
  29. *
  30. * The wrapper will now pass the location of the uClibc shared libs used to
  31. * the linker with the "-rpath" option if the invocation name includes the
  32. * string "rpath" or if the environment variable UCLIBC_GCC include it (as
  33. * with "build" above). This is primarily intended to be used on devel
  34. * platforms of the same arch as the target. A good place to use this feature
  35. * would be in the uClibc/test directory.
  36. *
  37. * The wrapper now displays the command line passed to gcc when '-v' is used.
  38. *
  39. * May 31, 2001
  40. *
  41. * "rpath" and "build" behavior are now decoupled. You can of course get
  42. * the old "build" behavior by setting UCLIBC_GCC="rpath-build". Order
  43. * isn't important here, as only the substrings are searched for.
  44. *
  45. * Added environment variable check for UCLIBC_GCC_DLOPT to let user specify
  46. * an alternative dynamic linker at runtime without using command line args.
  47. * Since this wouldn't commonly be used, I made it easy on myself. You have
  48. * to match the option you would have passed to the gcc wrapper. As an
  49. * example,
  50. *
  51. * export UCLIBC_GCC_DLOPT="-Wl,--dynamic-linker,/lib/ld-alt-linker.so.3"
  52. *
  53. * This is really only useful if target arch == devel arch and DEVEL_PREFIX
  54. * isn't empty. It involves a recompile, but you can at least test apps
  55. * on your devel system if combined with the "rpath" behavor if by using
  56. * LD_LIBRARY_PATH, etc.
  57. *
  58. * Also added check for "-Wl,--dynamic-linker" on the command line. The
  59. * use default dynamic linker or the envirnment-specified dynamic linker
  60. * is disabled in that case.
  61. *
  62. * Added options --uclibc-use-build-dir and --uclibc-use-rpath so that those
  63. * behaviors can be invoked from the command line.
  64. *
  65. */
  66. /*
  67. *
  68. * TODO:
  69. * Check/modify gcc-specific environment variables?
  70. */
  71. #include <stdio.h>
  72. #include <stdlib.h>
  73. #include <stdarg.h>
  74. #include <string.h>
  75. #include <unistd.h>
  76. #include "gcc-uClibc.h"
  77. static char *usr_lib_path = "-L"UCLIBC_DEVEL_PREFIX"/lib";
  78. static char static_linking[] = "-static";
  79. static char nostdinc[] = "-nostdinc";
  80. static char nostartfiles[] = "-nostartfiles";
  81. static char nodefaultlibs[] = "-nodefaultlibs";
  82. static char nostdlib[] = "-nostdlib";
  83. extern void *xmalloc(size_t size)
  84. {
  85. void *ptr = malloc(size);
  86. if (!ptr) {
  87. fprintf(stderr, "memory exhausted");
  88. exit(EXIT_FAILURE);
  89. }
  90. return ptr;
  91. }
  92. void xstrcat(char **string, ...)
  93. {
  94. const char *c;
  95. va_list p;
  96. #if 0
  97. int len = 0;
  98. /* Calculate how big exerything will be */
  99. va_start(p, string);
  100. while(1) {
  101. if (!(c = va_arg(p, const char *)))
  102. break;
  103. len+=strlen(c);
  104. }
  105. va_end(p);
  106. va_start(p, string);
  107. *string = xmalloc(len * sizeof(const char) + 2);
  108. #else
  109. /* This is faster. */
  110. /* Don't bother to calculate how big exerything
  111. * will be, just be careful to not overflow... */
  112. va_start(p, string);
  113. *string = xmalloc(BUFSIZ);
  114. #endif
  115. **string = '\0';
  116. while(1) {
  117. if (!(c = va_arg(p, const char *)))
  118. break;
  119. strcat(*string, c);
  120. }
  121. va_end(p);
  122. }
  123. int main(int argc, char **argv)
  124. {
  125. int use_build_dir = 0, linking = 1, use_static_linking = 0;
  126. int use_stdinc = 1, use_start = 1, use_stdlib = 1;
  127. int source_count = 0, use_rpath = 0, verbose = 0;
  128. int ctor_dtor = 0, cplusplus = 0;
  129. int i, j, k, l, m;
  130. char ** gcc_argv;
  131. char ** gcc_argument;
  132. char ** libraries;
  133. char *dlstr;
  134. char *incstr;
  135. char *devprefix;
  136. char *builddir;
  137. char *libstr;
  138. char *build_dlstr;
  139. char *ep;
  140. char *rpath_link[2];
  141. char *rpath[2];
  142. char *uClibc_inc[2];
  143. char *lib_path[2];
  144. char *crt0_path[2];
  145. const char *s, *application_name = argv[0];
  146. char *crti_path[2];
  147. char *crtn_path[2];
  148. char *GPLUSPLUS_BIN = NULL;
  149. if (application_name[0] == '-')
  150. application_name++;
  151. for (s = application_name; *s != '\0';) {
  152. if (*s++ == '/')
  153. application_name = s;
  154. }
  155. /* We must use strstr since g++ might be named like a
  156. * cross compiler (i.e. arm-linux-g++) . */
  157. if (strstr(application_name, "g++")!=0 || strstr(application_name, "c++")!=0) {
  158. if ((s=strstr(GCC_BIN,"gcc")) != 0) {
  159. GPLUSPLUS_BIN = strdup(GCC_BIN);
  160. GPLUSPLUS_BIN[1+s-GCC_BIN]='+';
  161. GPLUSPLUS_BIN[2+s-GCC_BIN]='+';
  162. }
  163. ctor_dtor = 1;
  164. cplusplus = 1;
  165. }
  166. devprefix = getenv("UCLIBC_DEVEL_PREFIX");
  167. if (!devprefix) {
  168. devprefix = UCLIBC_DEVEL_PREFIX;
  169. }
  170. builddir = getenv("UCLIBC_BUILD_DIR");
  171. if (!builddir) {
  172. builddir = UCLIBC_BUILD_DIR;
  173. }
  174. incstr = getenv("UCLIBC_GCC_INC");
  175. libstr = getenv("UCLIBC_GCC_LIB");
  176. xstrcat(&(rpath_link[0]), "-Wl,-rpath-link,", devprefix, "/lib", NULL);
  177. xstrcat(&(rpath_link[1]), "-Wl,-rpath-link,", builddir, "/lib", NULL);
  178. xstrcat(&(rpath[0]), "-Wl,-rpath,", devprefix, "/lib", NULL);
  179. xstrcat(&(rpath[1]), "-Wl,-rpath,", builddir, "/lib", NULL);
  180. xstrcat(&(uClibc_inc[0]), devprefix, "/include/", NULL);
  181. xstrcat(&(uClibc_inc[1]), builddir, "/include/", NULL);
  182. xstrcat(&(crt0_path[0]), devprefix, "/lib/crt0.o", NULL);
  183. xstrcat(&(crt0_path[1]), builddir, "/lib/crt0.o", NULL);
  184. xstrcat(&(crti_path[0]), devprefix, "/lib/crti.o", NULL);
  185. xstrcat(&(crti_path[1]), builddir, "/lib/crti.o", NULL);
  186. xstrcat(&(crtn_path[0]), devprefix, "/lib/crtn.o", NULL);
  187. xstrcat(&(crtn_path[1]), builddir, "/lib/crtn.o", NULL);
  188. xstrcat(&(lib_path[0]), "-L", devprefix, "/lib", NULL);
  189. xstrcat(&(lib_path[1]), "-L", builddir, "/lib", NULL);
  190. build_dlstr = "-Wl,--dynamic-linker," BUILD_DYNAMIC_LINKER;
  191. dlstr = getenv("UCLIBC_GCC_DLOPT");
  192. if (!dlstr) {
  193. dlstr = "-Wl,--dynamic-linker," DYNAMIC_LINKER;
  194. }
  195. ep = getenv("UCLIBC_GCC");
  196. if (!ep) {
  197. ep = "";
  198. }
  199. if (strstr(ep,"build") != 0) {
  200. use_build_dir = 1;
  201. }
  202. if (strstr(ep,"rpath") != 0) {
  203. use_rpath = 1;
  204. }
  205. #if 1
  206. m = 0;
  207. libraries = __builtin_alloca(sizeof(char*) * (argc));
  208. #else
  209. if (!(libraries = malloc(sizeof(char) * (argc)))) {
  210. return EXIT_FAILURE;
  211. }
  212. #endif
  213. libraries[m] = '\0';
  214. for ( i = 1 ; i < argc ; i++ ) {
  215. if (argv[i][0] == '-') { /* option */
  216. switch (argv[i][1]) {
  217. case 'c': /* compile or assemble */
  218. case 'S': /* generate assembler code */
  219. case 'E': /* preprocess only */
  220. case 'r': /* partial-link */
  221. case 'i': /* partial-link */
  222. case 'M': /* generate dependencies */
  223. linking = 0;
  224. break;
  225. case 'l': /* library */
  226. libraries[m++] = argv[i];
  227. libraries[m] = '\0';
  228. break;
  229. case 'v': /* verbose */
  230. if (argv[i][2] == 0) verbose = 1;
  231. printf("Invoked as %s\n", argv[0]);
  232. break;
  233. case 'n':
  234. if (strcmp(nostdinc,argv[i]) == 0) {
  235. use_stdinc = 0;
  236. } else if (strcmp(nostartfiles,argv[i]) == 0) {
  237. use_start = 0;
  238. } else if (strcmp(nodefaultlibs,argv[i]) == 0) {
  239. use_stdlib = 0;
  240. } else if (strcmp(nostdlib,argv[i]) == 0) {
  241. use_start = 0;
  242. use_stdlib = 0;
  243. }
  244. break;
  245. case 's':
  246. if (strstr(argv[i],static_linking) != NULL) {
  247. use_static_linking = 1;
  248. }
  249. break;
  250. case 'W': /* -static could be passed directly to ld */
  251. if (strncmp("-Wl,",argv[i],4) == 0) {
  252. if (strstr(argv[i],static_linking) != 0) {
  253. use_static_linking = 1;
  254. }
  255. if (strstr(argv[i],"--dynamic-linker") != 0) {
  256. dlstr = 0;
  257. }
  258. }
  259. break;
  260. case '-':
  261. if (strstr(argv[i]+1,static_linking) != NULL) {
  262. use_static_linking = 1;
  263. }
  264. break;
  265. }
  266. } else { /* assume it is an existing source file */
  267. ++source_count;
  268. }
  269. }
  270. #if 1
  271. gcc_argv = __builtin_alloca(sizeof(char*) * (argc + 20));
  272. gcc_argument = __builtin_alloca(sizeof(char*) * (argc + 20));
  273. #else
  274. if (!(gcc_argv = malloc(sizeof(char) * (argc + 20)))) {
  275. return EXIT_FAILURE;
  276. }
  277. if (!(gcc_argument = malloc(sizeof(char) * (argc + 20)))) {
  278. return EXIT_FAILURE;
  279. }
  280. #endif
  281. i = 0; k = 0;
  282. if (cplusplus && GPLUSPLUS_BIN)
  283. gcc_argv[i++] = GPLUSPLUS_BIN;
  284. else
  285. gcc_argv[i++] = GCC_BIN;
  286. for ( j = 1 ; j < argc ; j++ ) {
  287. if (strcmp("--uclibc-use-build-dir",argv[j]) == 0) {
  288. use_build_dir = 1;
  289. } else if (strcmp("--uclibc-use-rpath",argv[j]) == 0) {
  290. use_rpath = 1;
  291. } else if (strncmp("-l",argv[j], 2) == 0) {
  292. continue;
  293. } else if (strstr(argv[j],static_linking) != NULL) {
  294. continue;
  295. #if 0
  296. } else if (strncmp("-v",argv[j], 2) == 0) {
  297. continue;
  298. #endif
  299. } else {
  300. gcc_argument[k++] = argv[j];
  301. gcc_argument[k] = '\0';
  302. }
  303. }
  304. if (linking && source_count) {
  305. if (use_stdlib) {
  306. gcc_argv[i++] = nostdlib;
  307. }
  308. if (use_static_linking) {
  309. gcc_argv[i++] = static_linking;
  310. }
  311. if (!use_static_linking) {
  312. if (dlstr && use_build_dir) {
  313. gcc_argv[i++] = build_dlstr;
  314. } else if (dlstr) {
  315. gcc_argv[i++] = dlstr;
  316. }
  317. if (use_rpath) {
  318. gcc_argv[i++] = rpath[use_build_dir];
  319. }
  320. }
  321. gcc_argv[i++] = rpath_link[use_build_dir]; /* just to be safe */
  322. if( libstr )
  323. gcc_argv[i++] = libstr;
  324. gcc_argv[i++] = lib_path[use_build_dir];
  325. if (!use_build_dir) {
  326. gcc_argv[i++] = usr_lib_path;
  327. }
  328. }
  329. if (use_stdinc && source_count) {
  330. gcc_argv[i++] = nostdinc;
  331. gcc_argv[i++] = "-isystem";
  332. gcc_argv[i++] = uClibc_inc[use_build_dir];
  333. gcc_argv[i++] = "-isystem";
  334. gcc_argv[i++] = GCC_INCDIR;
  335. if( incstr )
  336. gcc_argv[i++] = incstr;
  337. }
  338. if (linking && source_count) {
  339. if (use_start) {
  340. if (ctor_dtor) {
  341. gcc_argv[i++] = crti_path[use_build_dir];
  342. gcc_argv[i++] = GCC_LIB_DIR "crtbegin.o" ;
  343. }
  344. gcc_argv[i++] = crt0_path[use_build_dir];
  345. }
  346. for ( l = 0 ; l < k ; l++ ) {
  347. if (gcc_argument[l]) gcc_argv[i++] = gcc_argument[l];
  348. }
  349. if (use_stdlib) {
  350. //gcc_argv[i++] = "-Wl,--start-group";
  351. gcc_argv[i++] = "-lgcc";
  352. for ( l = m ; l >= 0 ; l-- ) {
  353. if (libraries[l]) gcc_argv[i++] = libraries[l];
  354. }
  355. gcc_argv[i++] = "-lc";
  356. gcc_argv[i++] = "-lgcc";
  357. //gcc_argv[i++] = "-Wl,--end-group";
  358. }
  359. if (ctor_dtor) {
  360. gcc_argv[i++] = GCC_LIB_DIR "crtend.o" ;
  361. gcc_argv[i++] = crtn_path[use_build_dir];
  362. }
  363. } else {
  364. for ( l = 0 ; l < k ; l++ ) {
  365. if (gcc_argument[l]) gcc_argv[i++] = gcc_argument[l];
  366. }
  367. }
  368. gcc_argv[i++] = NULL;
  369. if (verbose) {
  370. for ( j = 0 ; gcc_argv[j] ; j++ ) {
  371. printf("arg[%2i] = %s\n", j, gcc_argv[j]);
  372. }
  373. fflush(stdout);
  374. }
  375. //no need to free memory from xstrcat because we never return...
  376. if (cplusplus && GPLUSPLUS_BIN)
  377. return execvp(GPLUSPLUS_BIN, gcc_argv);
  378. else
  379. return execvp(GCC_BIN, gcc_argv);
  380. }