gcc-uClibc.c 6.8 KB

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