gcc-uClibc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * TODO:
  9. * Check/modify gcc-specific environment variables?
  10. */
  11. #ifdef DEBUG
  12. #include <stdio.h>
  13. #endif
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include "gcc-uClibc.h"
  18. #define UCLIBC_CRT0 UCLIBC_DIR"crt0.o"
  19. #define UCLIBC_CRT0_G UCLIBC_CRT0
  20. #define UCLIBC_LIB UCLIBC_DIR"libc.a"
  21. #define UCLIBC_SHAREDLIB "-luClibc"
  22. #if 1
  23. #define UCLIBC_LIB_G UCLIBC_LIB
  24. #define UCLIBC_SHAREDLIB_G UCLIBC_SHAREDLIB
  25. #else
  26. #define UCLIBC_LIB_G UCLIBC_DIR"libc.a-debug"
  27. #endif
  28. #define UCLIBC_INC "-I"UCLIBC_DIR"include/"
  29. static char static_linking[] = "-static";
  30. static char nostdinc[] = "-nostdinc";
  31. static char nostartfiles[] = "-nostartfiles";
  32. static char nodefaultlibs[] = "-nodefaultlibs";
  33. static char nostdlib[] = "-nostdlib";
  34. int main(int argc, char **argv)
  35. {
  36. int debugging = 0, linking = 1, use_static_linking = 0;
  37. int use_stdinc = 1, use_start = 1, use_stdlib = 1;
  38. int i, j;
  39. int source_count;
  40. char ** gcc_argv;
  41. source_count = 0;
  42. /* FIXME: We need to work out the install vs use-in-built-dir
  43. * issue..*/
  44. /* Apparently gcc doesn't accept this stuff via the command line */
  45. setenv("COMPILER_PATH", UCLIBC_DIR"extra/gcc-uClibc/", 1);
  46. setenv("LIBRARY_PATH", UCLIBC_DIR"lib/", 1);
  47. /* The double '/' works around a gcc bug */
  48. setenv("GCC_EXEC_PREFIX", UCLIBC_DIR"extra/gcc-uClibc//", 1);
  49. for ( i = 1 ; i < argc ; i++ ) {
  50. if (argv[i][0] == '-') { /* option */
  51. switch (argv[i][1]) {
  52. case 'c':
  53. case 'S':
  54. case 'E':
  55. case 'r':
  56. if (argv[i][2] == 0) linking = 0;
  57. break;
  58. case 'g':
  59. if (argv[i][2] == 0) debugging = 1;
  60. break;
  61. case 'n':
  62. if (strcmp(nostdinc,argv[i]) == 0) {
  63. use_stdinc = 0;
  64. } else if (strcmp(nostartfiles,argv[i]) == 0) {
  65. use_start = 0;
  66. } else if (strcmp(nodefaultlibs,argv[i]) == 0) {
  67. use_stdlib = 0;
  68. } else if (strcmp(nostdlib,argv[i]) == 0) {
  69. use_start = 0;
  70. use_stdlib = 0;
  71. }
  72. break;
  73. case 's':
  74. if (strcmp(static_linking,argv[i]) == 0) {
  75. use_static_linking = 1;
  76. }
  77. break;
  78. case 'W':
  79. if (strncmp("-Wl,",argv[i],4) == 0) {
  80. if (strstr(argv[i],static_linking) != 0) {
  81. use_static_linking = 1;
  82. }
  83. }
  84. break;
  85. case '-':
  86. if (strcmp(static_linking,argv[i]+1) == 0) {
  87. use_static_linking = 1;
  88. }
  89. break;
  90. }
  91. } else { /* assume it is an existing source file */
  92. ++source_count;
  93. }
  94. }
  95. #if 1
  96. gcc_argv = __builtin_alloca(sizeof(char*) * (argc + 20));
  97. #else
  98. if (!(gcc_argv = malloc(sizeof(char) * (argc + 20)))) {
  99. return EXIT_FAILURE;
  100. }
  101. #endif
  102. i = 0;
  103. gcc_argv[i++] = GCC_BIN;
  104. for ( j = 1 ; j < argc ; j++ ) {
  105. gcc_argv[i++] = argv[j];
  106. }
  107. if (use_stdinc) {
  108. gcc_argv[i++] = nostdinc;
  109. gcc_argv[i++] = UCLIBC_INC;
  110. gcc_argv[i++] = GCC_INCDIR;
  111. }
  112. if (linking && source_count) {
  113. if (use_start) {
  114. if (debugging) {
  115. gcc_argv[i++] = UCLIBC_CRT0_G;
  116. } else {
  117. gcc_argv[i++] = UCLIBC_CRT0;
  118. }
  119. }
  120. if (use_stdlib) {
  121. gcc_argv[i++] = nostdlib;
  122. if (use_static_linking) {
  123. if (debugging) {
  124. gcc_argv[i++] = UCLIBC_LIB_G;
  125. } else {
  126. gcc_argv[i++] = UCLIBC_LIB;
  127. }
  128. } else {
  129. if (DYNAMIC_LINKER[0]) { /* not empty string */
  130. gcc_argv[i++] = "-Wl,--dynamic-linker";
  131. gcc_argv[i++] = DYNAMIC_LINKER;
  132. }
  133. if (debugging) {
  134. gcc_argv[i++] = UCLIBC_SHAREDLIB_G;
  135. } else {
  136. gcc_argv[i++] = UCLIBC_SHAREDLIB;
  137. }
  138. }
  139. gcc_argv[i++] = GCC_LIB;
  140. if (!use_static_linking && DYNAMIC_LINKER[0]) {
  141. gcc_argv[i++] = DYNAMIC_LINKER;
  142. }
  143. }
  144. }
  145. gcc_argv[i++] = NULL;
  146. #ifdef DEBUG
  147. for ( j = 0 ; gcc_argv[j] ; j++ ) {
  148. printf("arg[%2i] = %s\n", j, gcc_argv[j]);
  149. }
  150. return EXIT_SUCCESS;
  151. #else
  152. return execvp(GCC_BIN, gcc_argv);
  153. #endif
  154. }