gcc-uClibc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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"lib"
  52. };
  53. static char *rpath[] = {
  54. "-Wl,-rpath,"UCLIBC_INSTALL_DIR"lib",
  55. "-Wl,-rpath,"UCLIBC_BUILD_DIR"lib"
  56. };
  57. static char *uClibc_inc[] = {
  58. "-I"UCLIBC_INSTALL_DIR"usr/include/",
  59. "-I"UCLIBC_BUILD_DIR"include/"
  60. };
  61. static char *crt0_path[] = {
  62. UCLIBC_INSTALL_DIR"lib/crt0.o",
  63. UCLIBC_BUILD_DIR"lib/crt0.o"
  64. };
  65. static char *lib_path[] = {
  66. "-L"UCLIBC_INSTALL_DIR"lib",
  67. "-L"UCLIBC_BUILD_DIR"lib"
  68. };
  69. static char *usr_lib_path = "-L"UCLIBC_INSTALL_DIR"usr/lib";
  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 use_build_dir = 0, 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],"build") != 0) || (strstr(ep,"build") != 0)) {
  88. use_build_dir = 1;
  89. }
  90. if ((strstr(argv[0],"rpath") != 0) || (strstr(ep,"rpath") != 0)) {
  91. use_rpath = 1;
  92. }
  93. for ( i = 1 ; i < argc ; i++ ) {
  94. if (argv[i][0] == '-') { /* option */
  95. switch (argv[i][1]) {
  96. case 'c': /* compile or assemble */
  97. case 'S': /* generate assembler code */
  98. case 'E': /* preprocess only */
  99. case 'r': /* partial-link */
  100. case 'i': /* partial-link */
  101. case 'M': /* map file generation */
  102. if (argv[i][2] == 0) linking = 0;
  103. break;
  104. case 'v': /* verbose */
  105. if (argv[i][2] == 0) verbose = 1;
  106. break;
  107. case 'n':
  108. if (strcmp(nostdinc,argv[i]) == 0) {
  109. use_stdinc = 0;
  110. } else if (strcmp(nostartfiles,argv[i]) == 0) {
  111. use_start = 0;
  112. } else if (strcmp(nodefaultlibs,argv[i]) == 0) {
  113. use_stdlib = 0;
  114. } else if (strcmp(nostdlib,argv[i]) == 0) {
  115. use_start = 0;
  116. use_stdlib = 0;
  117. }
  118. break;
  119. case 's':
  120. if (strcmp(static_linking,argv[i]) == 0) {
  121. use_static_linking = 1;
  122. }
  123. break;
  124. case 'W': /* -static could be passed directly to ld */
  125. if (strncmp("-Wl,",argv[i],4) == 0) {
  126. if (strstr(argv[i],static_linking) != 0) {
  127. use_static_linking = 1;
  128. }
  129. }
  130. break;
  131. case '-':
  132. if (strcmp(static_linking,argv[i]+1) == 0) {
  133. use_static_linking = 1;
  134. }
  135. break;
  136. }
  137. } else { /* assume it is an existing source file */
  138. ++source_count;
  139. }
  140. }
  141. #if 1
  142. gcc_argv = __builtin_alloca(sizeof(char*) * (argc + 20));
  143. #else
  144. if (!(gcc_argv = malloc(sizeof(char) * (argc + 20)))) {
  145. return EXIT_FAILURE;
  146. }
  147. #endif
  148. i = 0;
  149. gcc_argv[i++] = GCC_BIN;
  150. for ( j = 1 ; j < argc ; j++ ) {
  151. gcc_argv[i++] = argv[j];
  152. }
  153. if (use_stdinc) {
  154. gcc_argv[i++] = nostdinc;
  155. gcc_argv[i++] = uClibc_inc[use_build_dir];
  156. gcc_argv[i++] = GCC_INCDIR;
  157. }
  158. if (linking && source_count) {
  159. if (!use_static_linking) {
  160. if (DYNAMIC_LINKER[0]) { /* not empty string */
  161. #if 0
  162. gcc_argv[i++] = "-Wl,--dynamic-linker,"DYNAMIC_LINKER;
  163. if (strstr(DYNAMIC_LINKER,"uclibc") != 0) { /* custom linker */
  164. use_rpath = 0; /* so -rpath not needed for normal case */
  165. }
  166. #else
  167. char *dlstr = "-Wl,--dynamic-linker,"DYNAMIC_LINKER;
  168. if (strstr(DYNAMIC_LINKER,"uclibc") != 0) { /* custom linker */
  169. use_rpath = 0; /* so -rpath not needed for normal case */
  170. if (use_build_dir) {
  171. dlstr = "-Wl,--dynamic-linker," \
  172. UCLIBC_BUILD_DIR DYNAMIC_LINKER;
  173. }
  174. }
  175. gcc_argv[i++] = dlstr;
  176. #endif
  177. }
  178. if (use_build_dir || use_rpath) {
  179. gcc_argv[i++] = rpath[use_build_dir];
  180. }
  181. }
  182. gcc_argv[i++] = rpath_link[use_build_dir]; /* just to be safe */
  183. gcc_argv[i++] = lib_path[use_build_dir];
  184. if (!use_build_dir) {
  185. gcc_argv[i++] = usr_lib_path;
  186. }
  187. if (use_start) {
  188. gcc_argv[i++] = crt0_path[use_build_dir];
  189. }
  190. if (use_stdlib) {
  191. gcc_argv[i++] = nostdlib;
  192. gcc_argv[i++] = "-lc";
  193. gcc_argv[i++] = GCC_LIB;
  194. }
  195. }
  196. gcc_argv[i++] = NULL;
  197. if (verbose) {
  198. printf("Invoked as %s\n", argv[0]);
  199. for ( j = 0 ; gcc_argv[j] ; j++ ) {
  200. printf("arg[%2i] = %s\n", j, gcc_argv[j]);
  201. }
  202. }
  203. return execvp(GCC_BIN, gcc_argv);
  204. }