gcc-uClibc.c 6.1 KB

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