aboutsummaryrefslogtreecommitdiff
path: root/autoconfig.cpp
blob: 9d09412aa5aed860997ef5808b3c0e680801b936 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

FILE *fOut = NULL;

bool testCpp( const char *prog )
{
    FILE *pop = popen("g++ -x c++ - -o /dev/null", "w");
    fwrite( prog, 1, strlen( prog ), pop );
    return pclose(pop) == 0;
}

bool testLib( const char *lib, const char *symname, const char *humname )
{
    printf("Detecting library %s (-l%s)...", humname, lib );
    fflush( stdout );
    static const char *prog = "int main () { return 0; }";
    char *cmdline = (char *)malloc( 29+strlen(lib) );
    strcpy( cmdline, "g++ -x c++ -l");
    strcat( cmdline, lib );
    strcat( cmdline, " - -o /dev/null");

    FILE *pop = popen( cmdline, "w");
    fwrite( prog, 1, strlen( prog ), pop );
    free( cmdline );
    if( pclose(pop) == 0 )
    {
        printf("found.\n");
        fprintf( fOut, "#define BU_FEATURE_%s 1\n", symname );
        fprintf( fOut, "#define BU_HAS_%s\n", symname );
        return false;
    }
    else
    {
        printf("missing.\n");
        fprintf( fOut, "#define BU_FEATURE_%s 0\n", symname );
        fprintf( fOut, "#define BU_MISSING_%s\n", symname );
        return true;
    }
}

void detectEndianness()
{
    printf("Detecting endian support...");
    fflush( stdout );
    if( testCpp("#include <endian.h>\n\nint main() { return BYTE_ORDER; }\n") )
    {
        fprintf( fOut, "#include <endian.h>\n\n");
        printf("header file endian.h found, using that.\n");
    }
    else
    {
        uint16_t x=0x0100;
        fprintf( fOut,
            "#define LITTLE_ENDIAN 0\n"
            "#define BIG_ENDIAN 1\n"
            "#define BYTE_ORDER %d\n\n",
            ((uint8_t *)&x)[0]
            );
        printf("no header file found, faking it...\n"
            "\tArchetecture is: %s Endian\n",
            ((uint8_t *)&x)[0]?"Big":"Little"
            );
    }
}

int main( int argc, char *argv[] )
{
    if( argc == 1 )
    {
        fprintf( stderr,
            "Invalid usage: specify a file to generate:\n"
            "    src/autoconfig.h\n"
            "    src/version.h\n"
            "\n"
            );
        return 127;
    }
    if( strcmp( argv[1], "src/autoconfig.h" ) == 0 )
    {
        fOut = fopen( argv[1], "w" );
        fprintf( fOut,
            "#ifndef BU_AUTO_CONFIG_H\n"
            "#define BU_AUTO_CONFIG_H\n\n"
            );

        detectEndianness();
        testLib("z", "DEFLATE", "Deflate");
        testLib("bz2", "BZIP2", "BZip2");
        testLib("lzma", "LZMA", "Lzma");

        fprintf( fOut, "#endif\n");
    }
    else if( strcmp( argv[1], "src/version.h" ) == 0 )
    {
        FILE *fVer = fopen("version", "rt");
        char buf[1024];
        buf[fread( buf, 1, 1024, fVer )] = '\0';
        for( int j = 0; buf[j]; j++ )
            if( buf[j] == '\n' )
                buf[j] = '\0';
        fclose( fVer );

        fOut = fopen( argv[1], "w" );
        fprintf( fOut,
            "#ifndef BU_VERSION_H\n"
            "#define BU_VERSION_H\n\n"
            "#define LIBBU_VERSION		0\n"
            "#define LIBBU_REVISION		1\n"
            "#define LIBBU_VERSION_STR	\"%s\"\n"
            "#define LIBBU_API		0\n"
            "#define LIBBU_VC_ID		\"",
			buf
			);
		FILE *psub = popen("svnversion -n", "r");
		fwrite( buf, fread( buf, 1, 1024, psub ), 1, fOut );
		pclose( psub );
		fprintf( fOut, "\"\n\n#endif\n");
    }

    return 0;
}