gcc-uClibc.c 8.5 KB

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