gcc-uClibc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "gcc-uClibc.h"
  49. static char *rpath_link[] = {
  50. "-Wl,-rpath-link,"UCLIBC_INSTALL_DIR"lib",
  51. "-Wl,-rpath-link,"UCLIBC_BUILD_DIR
  52. };
  53. static char *rpath[] = {
  54. "-Wl,-rpath,"UCLIBC_INSTALL_DIR"lib",
  55. "-Wl,-rpath,"UCLIBC_BUILD_DIR
  56. };
  57. static char *uClibc_inc[] = {
  58. "-I"UCLIBC_INSTALL_DIR"include/",
  59. "-I"UCLIBC_BUILD_DIR"include/"
  60. };
  61. static char *crt0_path[] = {
  62. UCLIBC_INSTALL_DIR"lib/crt0.o",
  63. UCLIBC_BUILD_DIR"crt0.o"
  64. };
  65. static char *lib_path[] = {
  66. "-L"UCLIBC_INSTALL_DIR"lib",
  67. "-L"UCLIBC_BUILD_DIR
  68. };
  69. static char static_linking[] = "-static";
  70. static char nostdinc[] = "-nostdinc";
  71. static char nostartfiles[] = "-nostartfiles";
  72. static char nodefaultlibs[] = "-nodefaultlibs";
  73. static char nostdlib[] = "-nostdlib";
  74. int main(int argc, char **argv)
  75. {
  76. int use_build_dir = 0, linking = 1, use_static_linking = 0;
  77. int use_stdinc = 1, use_start = 1, use_stdlib = 1;
  78. int source_count = 0, use_rpath = 0, verbose = 0;
  79. int i, j;
  80. char ** gcc_argv;
  81. char *ep;
  82. ep = getenv("UCLIBC_GCC");
  83. if (!ep) {
  84. ep = "";
  85. }
  86. if ((strstr(argv[0],"build") != 0) || (strstr(ep,"build") != 0)) {
  87. use_build_dir = 1;
  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[use_build_dir];
  166. gcc_argv[i++] = GCC_INCDIR;
  167. }
  168. if (linking && source_count) {
  169. if (!use_static_linking) {
  170. if (DYNAMIC_LINKER[0]) { /* not empty string */
  171. gcc_argv[i++] = "-Wl,--dynamic-linker,"DYNAMIC_LINKER;
  172. if (strstr(DYNAMIC_LINKER,"uclibc") != 0) { /* custom linker */
  173. use_rpath = 0; /* so -rpath not needed for normal case */
  174. }
  175. }
  176. if (use_build_dir || use_rpath) {
  177. gcc_argv[i++] = rpath[use_build_dir];
  178. }
  179. }
  180. gcc_argv[i++] = rpath_link[use_build_dir]; /* just to be safe */
  181. gcc_argv[i++] = lib_path[use_build_dir];
  182. if (use_start) {
  183. gcc_argv[i++] = crt0_path[use_build_dir];
  184. }
  185. if (use_stdlib) {
  186. gcc_argv[i++] = nostdlib;
  187. gcc_argv[i++] = "-lc";
  188. gcc_argv[i++] = GCC_LIB;
  189. }
  190. }
  191. gcc_argv[i++] = NULL;
  192. if (verbose) {
  193. printf("Invoked as %s\n", argv[0]);
  194. for ( j = 0 ; gcc_argv[j] ; j++ ) {
  195. printf("arg[%2i] = %s\n", j, gcc_argv[j]);
  196. }
  197. }
  198. return execvp(GCC_BIN, gcc_argv);
  199. }