gcc-uClibc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. */
  39. /*
  40. *
  41. * TODO:
  42. * Check/modify gcc-specific environment variables?
  43. */
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <unistd.h>
  48. #include <sys/stat.h>
  49. #include "gcc-uClibc.h"
  50. static char *rpath_link[] = {
  51. "-Wl,-rpath-link,"UCLIBC_INSTALL_DIR"lib",
  52. "-Wl,-rpath-link,"UCLIBC_BUILD_DIR"lib"
  53. };
  54. static char *rpath[] = {
  55. "-Wl,-rpath,"UCLIBC_INSTALL_DIR"lib",
  56. "-Wl,-rpath,"UCLIBC_BUILD_DIR"lib"
  57. };
  58. static char *uClibc_inc[] = {
  59. "-I"UCLIBC_INSTALL_DIR"usr/include/",
  60. "-I"UCLIBC_BUILD_DIR"include/"
  61. };
  62. static char *crt0_path[] = {
  63. UCLIBC_INSTALL_DIR"lib/crt0.o",
  64. UCLIBC_BUILD_DIR"lib/crt0.o"
  65. };
  66. static char *lib_path[] = {
  67. "-L"UCLIBC_INSTALL_DIR"lib",
  68. "-L"UCLIBC_INSTALL_DIR"usr/lib",
  69. "-L"UCLIBC_BUILD_DIR"lib"
  70. "-L"UCLIBC_BUILD_DIR"usr/lib"
  71. };
  72. static char static_linking[] = "-static";
  73. static char nostdinc[] = "-nostdinc";
  74. static char nostartfiles[] = "-nostartfiles";
  75. static char nodefaultlibs[] = "-nodefaultlibs";
  76. static char nostdlib[] = "-nostdlib";
  77. int main(int argc, char **argv)
  78. {
  79. int linking = 1, use_static_linking = 0;
  80. int use_stdinc = 1, use_start = 1, use_stdlib = 1;
  81. int source_count = 0, use_rpath = 0, verbose = 0;
  82. int i, j;
  83. char ** gcc_argv;
  84. char *ep;
  85. ep = getenv("UCLIBC_GCC");
  86. if (!ep) {
  87. ep = "";
  88. }
  89. if ((strstr(argv[0],"rpath") != 0) || (strstr(ep,"rpath") != 0)) {
  90. use_rpath = 1;
  91. }
  92. #if 0
  93. /* Erik added this stuff in. Disabled but kept in case the new changes */
  94. /* don't do what he needed. */
  95. /* FIXME: We need to work out the install vs use-in-built-dir
  96. * issue..*/
  97. /* Apparently gcc doesn't accept this stuff via the command line */
  98. setenv("COMPILER_PATH", UCLIBC_DIR"extra/gcc-uClibc/", 1);
  99. setenv("LIBRARY_PATH", UCLIBC_DIR"lib/", 1);
  100. /* The double '/' works around a gcc bug */
  101. setenv("GCC_EXEC_PREFIX", UCLIBC_DIR"extra/gcc-uClibc//", 1);
  102. #endif
  103. for ( i = 1 ; i < argc ; i++ ) {
  104. if (argv[i][0] == '-') { /* option */
  105. switch (argv[i][1]) {
  106. case 'c': /* compile or assemble */
  107. case 'S': /* generate assembler code */
  108. case 'E': /* preprocess only */
  109. case 'r': /* partial-link */
  110. case 'i': /* partial-link */
  111. case 'M': /* map file generation */
  112. if (argv[i][2] == 0) linking = 0;
  113. break;
  114. case 'v': /* verbose */
  115. if (argv[i][2] == 0) verbose = 1;
  116. break;
  117. case 'n':
  118. if (strcmp(nostdinc,argv[i]) == 0) {
  119. use_stdinc = 0;
  120. } else if (strcmp(nostartfiles,argv[i]) == 0) {
  121. use_start = 0;
  122. } else if (strcmp(nodefaultlibs,argv[i]) == 0) {
  123. use_stdlib = 0;
  124. } else if (strcmp(nostdlib,argv[i]) == 0) {
  125. use_start = 0;
  126. use_stdlib = 0;
  127. }
  128. break;
  129. case 's':
  130. if (strcmp(static_linking,argv[i]) == 0) {
  131. use_static_linking = 1;
  132. }
  133. break;
  134. case 'W': /* -static could be passed directly to ld */
  135. if (strncmp("-Wl,",argv[i],4) == 0) {
  136. if (strstr(argv[i],static_linking) != 0) {
  137. use_static_linking = 1;
  138. }
  139. }
  140. break;
  141. case '-':
  142. if (strcmp(static_linking,argv[i]+1) == 0) {
  143. use_static_linking = 1;
  144. }
  145. break;
  146. }
  147. } else { /* assume it is an existing source file */
  148. ++source_count;
  149. }
  150. }
  151. #if 1
  152. gcc_argv = __builtin_alloca(sizeof(char*) * (argc + 20));
  153. #else
  154. if (!(gcc_argv = malloc(sizeof(char) * (argc + 20)))) {
  155. return EXIT_FAILURE;
  156. }
  157. #endif
  158. i = 0;
  159. gcc_argv[i++] = GCC_BIN;
  160. for ( j = 1 ; j < argc ; j++ ) {
  161. gcc_argv[i++] = argv[j];
  162. }
  163. if (use_stdinc) {
  164. gcc_argv[i++] = nostdinc;
  165. gcc_argv[i++] = uClibc_inc[0];
  166. gcc_argv[i++] = uClibc_inc[1];
  167. gcc_argv[i++] = GCC_INCDIR;
  168. }
  169. if (linking && source_count) {
  170. if (!use_static_linking) {
  171. if (DYNAMIC_LINKER[0]) { /* not empty string */
  172. gcc_argv[i++] = "-Wl,--dynamic-linker,"DYNAMIC_LINKER;
  173. if (strstr(DYNAMIC_LINKER,"uclibc") != 0) { /* custom linker */
  174. use_rpath = 0; /* so -rpath not needed for normal case */
  175. }
  176. }
  177. if (use_rpath) {
  178. gcc_argv[i++] = rpath[0];
  179. gcc_argv[i++] = rpath[1];
  180. }
  181. }
  182. gcc_argv[i++] = rpath_link[0]; /* just to be safe */
  183. gcc_argv[i++] = rpath_link[1]; /* just to be safe */
  184. gcc_argv[i++] = lib_path[0];
  185. gcc_argv[i++] = lib_path[1];
  186. if (use_start) {
  187. struct stat buf;
  188. if (stat(crt0_path[0], &buf) >= 0) {
  189. gcc_argv[i++] = crt0_path[0];
  190. } else {
  191. gcc_argv[i++] = crt0_path[1];
  192. }
  193. }
  194. if (use_stdlib) {
  195. gcc_argv[i++] = nostdlib;
  196. gcc_argv[i++] = "-lc";
  197. gcc_argv[i++] = GCC_LIB;
  198. }
  199. }
  200. gcc_argv[i++] = NULL;
  201. if (verbose) {
  202. printf("Invoked as %s\n", argv[0]);
  203. for ( j = 0 ; gcc_argv[j] ; j++ ) {
  204. printf("arg[%2i] = %s\n", j, gcc_argv[j]);
  205. }
  206. }
  207. return execvp(GCC_BIN, gcc_argv);
  208. }