Back to index

lightning-sunbird  0.9+nobinonly
gencfg.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include <stdio.h>
00039 
00040 #if defined(sgi)
00041 #ifndef IRIX
00042        error - IRIX is not defined
00043 #endif
00044 #endif
00045 
00046 #if defined(__sun)
00047 #if defined(__svr4) || defined(__svr4__) || defined(__SVR4)
00048 #ifndef SOLARIS
00049        error - SOLARIS is not defined
00050 #endif
00051 #else
00052 #ifndef SUNOS4
00053        error - SUNOS4 is not defined
00054 #endif
00055 #endif
00056 #endif
00057 
00058 #if defined(__hpux)
00059 #ifndef HPUX
00060        error - HPUX is not defined
00061 #endif
00062 #endif
00063 
00064 #if defined(__alpha) 
00065 #if !(defined(_WIN32)) && !(defined(OSF1)) && !(defined(__linux)) && !(defined(__FreeBSD__))
00066        error - None of OSF1, _WIN32, __linux, or __FreeBSD__ is defined
00067 #endif
00068 #endif
00069 
00070 #if defined(_IBMR2)
00071 #ifndef AIX
00072        error - AIX is not defined
00073 #endif
00074 #endif
00075 
00076 #if defined(linux)
00077 #ifndef LINUX
00078        error - LINUX is not defined
00079 #endif
00080 #endif
00081 
00082 #if defined(bsdi)
00083 #ifndef BSDI
00084        error - BSDI is not defined
00085 #endif
00086 #endif
00087 
00088 #if defined(M_UNIX)
00089 #ifndef SCO
00090       error - SCO is not defined
00091 #endif
00092 #endif
00093 #if !defined(M_UNIX) && defined(_USLC_)
00094 #ifndef UNIXWARE
00095       error - UNIXWARE is not defined
00096 #endif
00097 #endif
00098 
00099 #if defined(__APPLE__)
00100 #ifndef DARWIN
00101       error - DARWIN is not defined
00102 #endif
00103 #endif
00104 
00105 #if defined(__NeXT__)
00106 #ifndef NEXTSTEP
00107       error - NEXTSTEP is not defined
00108 #endif
00109 #endif
00110 
00111 /************************************************************************/
00112 
00113 /* Generate cpucfg.h */
00114 
00115 #ifdef XP_PC
00116 #ifdef WIN32
00117 #define INT64 _PRInt64
00118 #else
00119 #define INT64 long
00120 #endif
00121 #else
00122 #if defined(HPUX) || defined(NECSVR4) || defined(SCO) || defined(UNIXWARE) || defined (NCR)
00123 #define INT64 long
00124 #else
00125 #define INT64 long long
00126 #endif
00127 #endif
00128 
00129 struct align_short {
00130     char c;
00131     short a;
00132 };
00133 struct align_int {
00134     char c;
00135     int a;
00136 };
00137 struct align_long {
00138     char c;
00139     long a;
00140 };
00141 struct align_PRInt64 {
00142     char c;
00143     INT64 a;
00144 };
00145 struct align_fakelonglong {
00146     char c;
00147     struct {
00148        long hi, lo;
00149     } a;
00150 };
00151 struct align_float {
00152     char c;
00153     float a;
00154 };
00155 struct align_double {
00156     char c;
00157     double a;
00158 };
00159 struct align_pointer {
00160     char c;
00161     void *a;
00162 };
00163 
00164 #define ALIGN_OF(type) \
00165     (((char*)&(((struct align_##type *)0)->a)) - ((char*)0))
00166 
00167 int bpb;
00168 
00169 /* Used if shell doesn't support redirection. By default, assume it does. */
00170 FILE *stream;
00171 
00172 static int Log2(int n)
00173 {
00174     int log2 = 0;
00175 
00176     if (n & (n-1))
00177        log2++;
00178     if (n >> 16)
00179        log2 += 16, n >>= 16;
00180     if (n >> 8)
00181        log2 += 8, n >>= 8;
00182     if (n >> 4)
00183        log2 += 4, n >>= 4;
00184     if (n >> 2)
00185        log2 += 2, n >>= 2;
00186     if (n >> 1)
00187        log2++;
00188     return log2;
00189 }
00190 
00191 /* We assume that int's are 32 bits */
00192 static void do64(void)
00193 {
00194     union {
00195        int i;
00196        char c[4];
00197     } u;
00198 
00199     u.i = 0x01020304;
00200     if (u.c[0] == 0x01) {
00201        fprintf(stream, "#undef  IS_LITTLE_ENDIAN\n");
00202        fprintf(stream, "#define IS_BIG_ENDIAN 1\n\n");
00203     } else {
00204        fprintf(stream, "#define IS_LITTLE_ENDIAN 1\n");
00205        fprintf(stream, "#undef  IS_BIG_ENDIAN\n\n");
00206     }
00207 }
00208 
00209 static void do32(void)
00210 {
00211     union {
00212        long i;
00213        char c[4];
00214     } u;
00215 
00216     u.i = 0x01020304;
00217     if (u.c[0] == 0x01) {
00218        fprintf(stream, "#undef  IS_LITTLE_ENDIAN\n");
00219        fprintf(stream, "#define IS_BIG_ENDIAN 1\n\n");
00220     } else {
00221        fprintf(stream, "#define IS_LITTLE_ENDIAN 1\n");
00222        fprintf(stream, "#undef  IS_BIG_ENDIAN\n\n");
00223     }
00224 }
00225 
00226 /*
00227 ** Concievably this could actually be used; but there is lots of code out
00228 ** there with and's and shift's in it that assumes a byte is 8 bits, so
00229 ** forget about porting THIS code to those non 8 bit byte machines.
00230 */
00231 static void BitsPerByte(void)
00232 {
00233     bpb = 8;
00234 }
00235 
00236 int main(int argc, char **argv)
00237 {
00238     BitsPerByte();
00239 
00240     /* If we got a command line argument, try to use it as the stream. */
00241     ++argv;
00242     if(*argv) {
00243         if(!(stream = fopen ( *argv, "wt" ))) {
00244             fprintf(stderr, "Could not write to output file %s.\n", *argv);
00245             return 1;
00246         }
00247     } else {
00248               stream = stdout;
00249        }
00250 
00251     fprintf(stream, "#ifndef nspr_cpucfg___\n");
00252     fprintf(stream, "#define nspr_cpucfg___\n\n");
00253 
00254     fprintf(stream, "/* AUTOMATICALLY GENERATED - DO NOT EDIT */\n\n");
00255 
00256     if (sizeof(long) == 8) {
00257        do64();
00258     } else {
00259        do32();
00260     }
00261     fprintf(stream, "#define PR_BYTES_PER_BYTE   %d\n", sizeof(char));
00262     fprintf(stream, "#define PR_BYTES_PER_SHORT  %d\n", sizeof(short));
00263     fprintf(stream, "#define PR_BYTES_PER_INT    %d\n", sizeof(int));
00264     fprintf(stream, "#define PR_BYTES_PER_INT64  %d\n", 8);
00265     fprintf(stream, "#define PR_BYTES_PER_LONG   %d\n", sizeof(long));
00266     fprintf(stream, "#define PR_BYTES_PER_FLOAT  %d\n", sizeof(float));
00267     fprintf(stream, "#define PR_BYTES_PER_DOUBLE %d\n\n", sizeof(double));
00268 
00269     fprintf(stream, "#define PR_BITS_PER_BYTE    %d\n", bpb);
00270     fprintf(stream, "#define PR_BITS_PER_SHORT   %d\n", bpb * sizeof(short));
00271     fprintf(stream, "#define PR_BITS_PER_INT     %d\n", bpb * sizeof(int));
00272     fprintf(stream, "#define PR_BITS_PER_INT64   %d\n", bpb * 8);
00273     fprintf(stream, "#define PR_BITS_PER_LONG    %d\n", bpb * sizeof(long));
00274     fprintf(stream, "#define PR_BITS_PER_FLOAT   %d\n", bpb * sizeof(float));
00275     fprintf(stream, "#define PR_BITS_PER_DOUBLE  %d\n\n", 
00276             bpb * sizeof(double));
00277 
00278     fprintf(stream, "#define PR_BITS_PER_BYTE_LOG2   %d\n", Log2(bpb));
00279     fprintf(stream, "#define PR_BITS_PER_SHORT_LOG2  %d\n", 
00280             Log2(bpb * sizeof(short)));
00281     fprintf(stream, "#define PR_BITS_PER_INT_LOG2    %d\n", 
00282             Log2(bpb * sizeof(int)));
00283     fprintf(stream, "#define PR_BITS_PER_INT64_LOG2  %d\n", 6);
00284     fprintf(stream, "#define PR_BITS_PER_LONG_LOG2   %d\n", 
00285             Log2(bpb * sizeof(long)));
00286     fprintf(stream, "#define PR_BITS_PER_FLOAT_LOG2  %d\n", 
00287             Log2(bpb * sizeof(float)));
00288     fprintf(stream, "#define PR_BITS_PER_DOUBLE_LOG2 %d\n\n", 
00289             Log2(bpb * sizeof(double)));
00290 
00291     fprintf(stream, "#define PR_ALIGN_OF_SHORT   %d\n", ALIGN_OF(short));
00292     fprintf(stream, "#define PR_ALIGN_OF_INT     %d\n", ALIGN_OF(int));
00293     fprintf(stream, "#define PR_ALIGN_OF_LONG    %d\n", ALIGN_OF(long));
00294     if (sizeof(INT64) < 8) {
00295        /* this machine doesn't actually support PRInt64's */
00296        fprintf(stream, "#define PR_ALIGN_OF_INT64   %d\n", 
00297                 ALIGN_OF(fakelonglong));
00298     } else {
00299        fprintf(stream, "#define PR_ALIGN_OF_INT64   %d\n", ALIGN_OF(PRInt64));
00300     }
00301     fprintf(stream, "#define PR_ALIGN_OF_FLOAT   %d\n", ALIGN_OF(float));
00302     fprintf(stream, "#define PR_ALIGN_OF_DOUBLE  %d\n", ALIGN_OF(double));
00303     fprintf(stream, "#define PR_ALIGN_OF_POINTER %d\n\n", ALIGN_OF(pointer));
00304 
00305     fprintf(stream, "#endif /* nspr_cpucfg___ */\n");
00306     fclose(stream);
00307 
00308     return 0;
00309 }