gcc-uClibc.c 7.1 KB

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