home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / xloadimg.zip / xloadimage.4.1 / libconfig.c < prev    next >
C/C++ Source or Header  |  1993-11-08  |  6KB  |  212 lines

  1. /* libconfig.c:
  2.  *
  3.  * this performs tests to configure external libraries.  most of these
  4.  * tests are used to determine a compiler's configuration.  output
  5.  * files are written into <libname>.conf for inclusion by a Makefile.
  6.  *
  7.  * jim frost 09.15.93
  8.  */
  9. #include <stdio.h>
  10.  
  11. /* things determined by compilation switches
  12.  */
  13. #ifdef __STDC__
  14. /* ANSI versus non-ANSI.  Note that some compilers define __STDC__
  15.  * as zero when they are not fully ANSI-compliant.  We don't care,
  16.  * it's close enough.
  17.  */
  18. int is_ansi = 1;
  19. #else
  20. int is_ansi = 0;
  21. #endif
  22. #if defined(IS_BSD)
  23. int is_bsd = 1;
  24. #else
  25. int is_bsd = 0;
  26. #endif
  27.  
  28. /* things determined on-the-fly
  29.  */
  30. int is_big_endian;
  31. int is_signed_char;
  32. int is_broken_right_shift;
  33.  
  34. #ifdef HAS_TIFF
  35. void writeTIFFConfig()
  36. {
  37.   FILE *f;
  38.  
  39.   f = fopen("tiff.conf", "w");
  40.   if (f == NULL) {
  41.     perror("tiff.conf");
  42.     fprintf(stderr, "libconfig: Couldn't write TIFF library config, sorry.\n");
  43.     return;
  44.   }
  45.   printf("Writing TIFF library configuration file....\n");
  46.   fprintf(f, "\
  47. # TIFF distribution configuration file; this is automagically configured by\n\
  48. # 'libconfig.'  If you must edit it, beware that reconfiguring the\n\
  49. # distribution with rewrite this file.\n\n");
  50.  
  51.   fprintf(f, "# Get system-configuration stuff\n");
  52.   fprintf(f, "include ../Make.conf\n\n");
  53.  
  54.   fprintf(f, "# ANSI compatibility flags\n");
  55.   fprintf(f, "%sPROTOTYPES=-DUSE_PROTOTYPES\n", (is_ansi ? "" : "#"));
  56.   fprintf(f, "%sVARARGS=-DUSE_VARARGS\n", (is_ansi ? "#" : ""));
  57.   fprintf(f, "%sCONST=-DUSE_CONST\n", (is_ansi ? "" : "#"));
  58.   fprintf(f, "\n");
  59.  
  60.   fprintf(f, "# If not a BSD system, need -DSYSV to fix bcopy et al.\n");
  61.   fprintf(f, "%sSYSV=-DSYSV\n\n", (is_bsd ? "#" : ""));
  62.  
  63.   fprintf(f, "# Conglomerate of all TIFF flags\n");
  64.   fprintf(f, "TIFF_CC_FLAGS=$(PROTOTYPES) $(VARARGS) $(CONST) $(SYSV) -Dunix\n");
  65.   fclose(f);
  66. }
  67. #endif /* HAS_TIFF */
  68.  
  69. #ifdef HAS_JPEG
  70. void writeJPEGConfig()
  71. {
  72.   FILE *f;
  73.  
  74.   f = fopen("jpeg.conf", "w");
  75.   if (f == NULL) {
  76.     perror("jpeg.conf");
  77.     fprintf(stderr, "libconfig: Couldn't write JPEG library config, sorry.\n");
  78.     return;
  79.   }
  80.   printf("Writing JPEG library configuration file....\n");
  81.   fprintf(f, "\
  82. # JPEG distribution configuration file; this is automagically configured by\n\
  83. # 'libconfig.'  If you must edit it, beware that reconfiguring the\n\
  84. # distribution with rewrite this file and that changes made here must be\n\
  85. # reflected in jpeg.conf.h.\n\n");
  86.  
  87.   fprintf(f, "# Get system-configuration stuff\n");
  88.   fprintf(f, "include ../Make.conf\n\n");
  89.  
  90.   fprintf(f, "# SysV or not.\n");
  91.   fprintf(f, "%sSYSV=-DSYSV\n\n", (is_bsd ? "#" : ""));
  92.  
  93.   fprintf(f, "# ANSI compatibility flags\n");
  94.   fprintf(f, "%sANSI=-DHAS_STDC\n\n", (is_ansi ? "" : "#"));
  95.  
  96.   fprintf(f, "# Default signed-ness of char type\n");
  97.   fprintf(f, "%sUNSIGNED_CHAR=-DCHAR_IS_UNSIGNED\n\n", (is_signed_char ? "#" : ""));
  98.  
  99.   fprintf(f, "# We need to know if the compiler has a broken right-shift operator\n");
  100.   fprintf(f, "%sBROKEN_SHIFT=-DRIGHT_SHIFT_IS_UNSIGNED\n\n", (is_broken_right_shift ? "" : "#"));
  101.  
  102.   fprintf(f, "# Extra flags we need\n");
  103.   fprintf(f, "OTHER=%s\n\n", (is_ansi ? "" : "-DHAVE_UNSIGNED_CHAR"));
  104.  
  105.   fprintf(f, "# Conglomerate of all JPEG flags\n\n");
  106.   fprintf(f, "JPEG_CC_FLAGS=$(OPT_FLAGS) $(SYSV) $(ANSI) $(UNSIGNED_CHAR) $(BROKEN_SHIFT) $(OTHER)\n");
  107.  
  108.   /* JPEG stuff is all ANSI; we need to write out a .c.o target that
  109.    * does the compilation correctly if either ANSI or K&R
  110.    */
  111.   fprintf(f, "# .c to .o target; special work needs to be done if not an ANSI compiler.\n");
  112.   if (is_ansi) {
  113.     fprintf(f, ".c.o:\n");
  114.     fprintf(f, "\t$(CC) $(JPEG_CC_FLAGS) -c $*.c\n");
  115.   }
  116.   else {
  117.     fprintf(f, ".c.o: ./ansi2knr\n");
  118.     fprintf(f, "\t./ansi2knr $*.c tmpansi.c\n");
  119.     fprintf(f, "\t$(CC) $(JPEG_CC_FLAGS) -c tmpansi.c\n");
  120.     fprintf(f, "\t$(MV) tmpansi.o $*.o\n");
  121.     fprintf(f, "\t$(RM) tmpansi.c\n");
  122.   }
  123.   fclose(f);
  124.  
  125.   /* write jpeg.conf.h, used by jpeg.c to get the same defines as used by
  126.    * the library.
  127.    */
  128.   f = fopen("jpeg.conf.h", "w");
  129.   if (f == NULL) {
  130.     perror("jpeg.conf");
  131.     fprintf(stderr, "libconfig: Couldn't write JPEG configuration header, sorry.\n");
  132.     return;
  133.   }
  134.   /* -DSYSV already done in Make.conf */
  135.   fprintf(f, "\
  136. /* jpeg.conf.h:\n\
  137.  *\n\
  138.  * Configuration file that matches the Makefile configuration file jpeg.conf\n\
  139.  * for building jpeg.c.  This file is automatically generated.\n\
  140.  */\n\n");
  141.   fprintf(f, "#define HAVE_UNSIGNED_CHAR 1\n");
  142.   if (is_ansi) {
  143.     fprintf(f, "#define PROTO 1\n");
  144.     fprintf(f, "#define HAS_STDC 1\n");
  145.   }
  146.   if (!is_signed_char)
  147.     fprintf(f, "#define CHAR_IS_UNSIGNED 1\n");
  148.   if (is_broken_right_shift)
  149.     fprintf(f, "#define RIGHT_SHIFT_IS_UNSIGNED 1\n");
  150.   fclose(f);
  151. }
  152. #endif /* HAS_JPEG */
  153.  
  154. main()
  155. { union {
  156.     unsigned short s;
  157.     char c[2];
  158.   } byte_test;
  159.   char c = 0;
  160.   int i;
  161.  
  162.   if (is_ansi)
  163.     printf("C compiler recognizes ANSI constructs.\n");
  164.   else
  165.     printf("C compiler is not ANSI-compatible.\n");
  166.  
  167.   /* determine machine byte order
  168.    */
  169.   byte_test.s = 0x00ff;
  170.   if (byte_test.c[0] == 0) {
  171.     printf("Machine has big-endian byte order.\n");
  172.     is_big_endian = 1;
  173.   }
  174.   else {
  175.     printf("Machine has little-endian byte order.\n");
  176.     is_big_endian = 0;
  177.   }
  178.  
  179.   /* determine default signed-ness of characters.
  180.    */
  181.   if (--c > 0) { /* will wrap to 255 if unsigned */
  182.     printf("Compiler assumes unsigned characters by default.\n");
  183.     is_signed_char = 0;
  184.   }
  185.   else {
  186.     printf("Compiler assumes signed characters by default.\n");
  187.     is_signed_char = 1;
  188.   }
  189.  
  190.   /* determine if the compiler stupidly handles right-shift as a logical
  191.    * shift rather than an arithmetic shift.  this would be broken
  192.    * behavior but JPEG wants to know....
  193.    */
  194.   i = -1;
  195.   if ((i >> 1) > 0) {
  196.     printf("\
  197. Compiler stupidly treats a signed right-shift as a logical operation.\n");
  198.     is_broken_right_shift = 1;
  199.   }
  200.   else
  201.     is_broken_right_shift = 0;
  202.  
  203. #ifdef HAS_TIFF
  204.   writeTIFFConfig();
  205. #endif
  206. #ifdef HAS_JPEG
  207.   writeJPEGConfig();
  208. #endif
  209.  
  210.   exit(0);
  211. }
  212.