gcc-uClibc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. for ( i = 1 ; i < argc ; i++ ) {
  43. if (argv[i][0] == '-') { /* option */
  44. switch (argv[i][1]) {
  45. case 'c':
  46. case 'S':
  47. case 'E':
  48. case 'r':
  49. if (argv[i][2] == 0) linking = 0;
  50. break;
  51. case 'g':
  52. if (argv[i][2] == 0) debugging = 1;
  53. break;
  54. case 'n':
  55. if (strcmp(nostdinc,argv[i]) == 0) {
  56. use_stdinc = 0;
  57. } else if (strcmp(nostartfiles,argv[i]) == 0) {
  58. use_start = 0;
  59. } else if (strcmp(nodefaultlibs,argv[i]) == 0) {
  60. use_stdlib = 0;
  61. } else if (strcmp(nostdlib,argv[i]) == 0) {
  62. use_start = 0;
  63. use_stdlib = 0;
  64. }
  65. break;
  66. case 's':
  67. if (strcmp(static_linking,argv[i]) == 0) {
  68. use_static_linking = 1;
  69. }
  70. break;
  71. case 'W':
  72. if (strncmp("-Wl,",argv[i],4) == 0) {
  73. if (strstr(argv[i],static_linking) != 0) {
  74. use_static_linking = 1;
  75. }
  76. }
  77. break;
  78. case '-':
  79. if (strcmp(static_linking,argv[i]+1) == 0) {
  80. use_static_linking = 1;
  81. }
  82. break;
  83. }
  84. } else { /* assume it is an existing source file */
  85. ++source_count;
  86. }
  87. }
  88. #if 1
  89. gcc_argv = __builtin_alloca(sizeof(char*) * (argc + 20));
  90. #else
  91. if (!(gcc_argv = malloc(sizeof(char) * (argc + 20)))) {
  92. return EXIT_FAILURE;
  93. }
  94. #endif
  95. i = 0;
  96. gcc_argv[i++] = GCC_BIN;
  97. for ( j = 1 ; j < argc ; j++ ) {
  98. gcc_argv[i++] = argv[j];
  99. }
  100. if (use_stdinc) {
  101. gcc_argv[i++] = nostdinc;
  102. gcc_argv[i++] = UCLIBC_INC;
  103. gcc_argv[i++] = GCC_INCDIR;
  104. }
  105. if (linking && source_count) {
  106. if (use_start) {
  107. if (debugging) {
  108. gcc_argv[i++] = UCLIBC_CRT0_G;
  109. } else {
  110. gcc_argv[i++] = UCLIBC_CRT0;
  111. }
  112. }
  113. if (use_stdlib) {
  114. gcc_argv[i++] = "-nostdlib";
  115. if (use_static_linking) {
  116. if (debugging) {
  117. gcc_argv[i++] = UCLIBC_LIB_G;
  118. } else {
  119. gcc_argv[i++] = UCLIBC_LIB;
  120. }
  121. } else {
  122. if (debugging) {
  123. gcc_argv[i++] = UCLIBC_SHAREDLIB_G;
  124. } else {
  125. gcc_argv[i++] = UCLIBC_SHAREDLIB;
  126. }
  127. }
  128. gcc_argv[i++] = GCC_LIB;
  129. }
  130. }
  131. gcc_argv[i++] = NULL;
  132. #ifdef DEBUG
  133. for ( j = 0 ; gcc_argv[j] ; j++ ) {
  134. printf("arg[%2i] = %s\n", j, gcc_argv[j]);
  135. }
  136. return EXIT_SUCCESS;
  137. #else
  138. return execvp(GCC_BIN, gcc_argv);
  139. #endif
  140. }