gcc-uClibc.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <string.h>
  73. #include <unistd.h>
  74. #include "gcc-uClibc.h"
  75. static char *rpath_link[] = {
  76. "-Wl,-rpath-link,"UCLIBC_DEVEL_PREFIX"/lib",
  77. "-Wl,-rpath-link,"UCLIBC_BUILD_DIR"/lib"
  78. };
  79. static char *rpath[] = {
  80. "-Wl,-rpath,"UCLIBC_DEVEL_PREFIX"/lib",
  81. "-Wl,-rpath,"UCLIBC_BUILD_DIR"/lib"
  82. };
  83. static char *uClibc_inc[] = {
  84. "-I"UCLIBC_DEVEL_PREFIX"/include/",
  85. "-I"UCLIBC_BUILD_DIR"/include/"
  86. };
  87. static char *crt0_path[] = {
  88. UCLIBC_DEVEL_PREFIX"/lib/crt0.o",
  89. UCLIBC_BUILD_DIR"/lib/crt0.o"
  90. };
  91. static char *lib_path[] = {
  92. "-L"UCLIBC_DEVEL_PREFIX"/lib",
  93. "-L"UCLIBC_BUILD_DIR"/lib"
  94. };
  95. static char *usr_lib_path = "-L"UCLIBC_DEVEL_PREFIX"/lib";
  96. static char static_linking[] = "-static";
  97. static char nostdinc[] = "-nostdinc";
  98. static char nostartfiles[] = "-nostartfiles";
  99. static char nodefaultlibs[] = "-nodefaultlibs";
  100. static char nostdlib[] = "-nostdlib";
  101. int main(int argc, char **argv)
  102. {
  103. int use_build_dir = 0, linking = 1, use_static_linking = 0;
  104. int use_stdinc = 1, use_start = 1, use_stdlib = 1;
  105. int source_count = 0, use_rpath = 0, verbose = 0;
  106. int i, j;
  107. char ** gcc_argv;
  108. char *dlstr;
  109. char *build_dlstr;
  110. char *ep;
  111. build_dlstr = "-Wl,--dynamic-linker," BUILD_DYNAMIC_LINKER;
  112. dlstr = getenv("UCLIBC_GCC_DLOPT");
  113. if (!dlstr) {
  114. dlstr = "-Wl,--dynamic-linker," DYNAMIC_LINKER;
  115. }
  116. ep = getenv("UCLIBC_GCC");
  117. if (!ep) {
  118. ep = "";
  119. }
  120. if (strstr(ep,"build") != 0) {
  121. use_build_dir = 1;
  122. }
  123. if (strstr(ep,"rpath") != 0) {
  124. use_rpath = 1;
  125. }
  126. for ( i = 1 ; i < argc ; i++ ) {
  127. if (argv[i][0] == '-') { /* option */
  128. switch (argv[i][1]) {
  129. case 'c': /* compile or assemble */
  130. case 'S': /* generate assembler code */
  131. case 'E': /* preprocess only */
  132. case 'r': /* partial-link */
  133. case 'i': /* partial-link */
  134. case 'M': /* map file generation */
  135. if (argv[i][2] == 0) linking = 0;
  136. break;
  137. case 'v': /* verbose */
  138. if (argv[i][2] == 0) verbose = 1;
  139. break;
  140. case 'n':
  141. if (strcmp(nostdinc,argv[i]) == 0) {
  142. use_stdinc = 0;
  143. } else if (strcmp(nostartfiles,argv[i]) == 0) {
  144. use_start = 0;
  145. } else if (strcmp(nodefaultlibs,argv[i]) == 0) {
  146. use_stdlib = 0;
  147. } else if (strcmp(nostdlib,argv[i]) == 0) {
  148. use_start = 0;
  149. use_stdlib = 0;
  150. }
  151. break;
  152. case 's':
  153. if (strcmp(static_linking,argv[i]) == 0) {
  154. use_static_linking = 1;
  155. }
  156. break;
  157. case 'W': /* -static could be passed directly to ld */
  158. if (strncmp("-Wl,",argv[i],4) == 0) {
  159. if (strstr(argv[i],static_linking) != 0) {
  160. use_static_linking = 1;
  161. }
  162. if (strstr(argv[i],"--dynamic-linker") != 0) {
  163. dlstr = 0;
  164. }
  165. }
  166. break;
  167. case '-':
  168. if (strcmp(static_linking,argv[i]+1) == 0) {
  169. use_static_linking = 1;
  170. }
  171. break;
  172. }
  173. } else { /* assume it is an existing source file */
  174. ++source_count;
  175. }
  176. }
  177. #if 1
  178. gcc_argv = __builtin_alloca(sizeof(char*) * (argc + 20));
  179. #else
  180. if (!(gcc_argv = malloc(sizeof(char) * (argc + 20)))) {
  181. return EXIT_FAILURE;
  182. }
  183. #endif
  184. i = 0;
  185. gcc_argv[i++] = GCC_BIN;
  186. for ( j = 1 ; j < argc ; j++ ) {
  187. if (strcmp("--uclibc-use-build-dir",argv[j]) == 0) {
  188. use_build_dir = 1;
  189. } else if (strcmp("--uclibc-use-rpath",argv[j]) == 0) {
  190. use_rpath = 1;
  191. } else {
  192. gcc_argv[i++] = argv[j];
  193. }
  194. }
  195. if (use_stdinc) {
  196. gcc_argv[i++] = nostdinc;
  197. gcc_argv[i++] = uClibc_inc[use_build_dir];
  198. gcc_argv[i++] = GCC_INCDIR;
  199. }
  200. if (linking && source_count) {
  201. if (!use_static_linking) {
  202. if (dlstr && use_build_dir) {
  203. gcc_argv[i++] = build_dlstr;
  204. } else if (dlstr) {
  205. gcc_argv[i++] = dlstr;
  206. }
  207. if (use_rpath) {
  208. gcc_argv[i++] = rpath[use_build_dir];
  209. }
  210. }
  211. gcc_argv[i++] = rpath_link[use_build_dir]; /* just to be safe */
  212. gcc_argv[i++] = lib_path[use_build_dir];
  213. if (!use_build_dir) {
  214. gcc_argv[i++] = usr_lib_path;
  215. }
  216. if (use_start) {
  217. gcc_argv[i++] = crt0_path[use_build_dir];
  218. }
  219. if (use_stdlib) {
  220. gcc_argv[i++] = nostdlib;
  221. gcc_argv[i++] = "-lc";
  222. gcc_argv[i++] = GCC_LIB;
  223. }
  224. }
  225. gcc_argv[i++] = NULL;
  226. if (verbose) {
  227. printf("Invoked as %s\n", argv[0]);
  228. for ( j = 0 ; gcc_argv[j] ; j++ ) {
  229. printf("arg[%2i] = %s\n", j, gcc_argv[j]);
  230. }
  231. }
  232. return execvp(GCC_BIN, gcc_argv);
  233. }