File: library

package info (click to toggle)
xd 3.11.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 304 kB
  • ctags: 102
  • sloc: cpp: 703; makefile: 139
file content (212 lines) | stat: -rw-r--r-- 5,090 bytes parent folder | download
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

#define COPT               "--std=c++0x -Wall -O3"

#define ECHO_REQUEST       1

#define LIBS               "bobcat"
#define LIBPATH            ""


string                  // contain options for
    g_cwd,              // current WD
    libs,               // extra libs, e.g., "-lrss -licce"
    libpath,            // extra lib-paths, eg, "-L../rss"
    g_sources,          // sources to be used
    g_binary;           // the name of the program to create
int
    g_nClasses;         // number of classes/subdirectories
list
    g_classes;          // list of classes/directories

void setClasses()
{
    list candidate;
    list class;

    while (sizeof(class = fgets("CLASSES", (int)element(1, class))))
    {
        candidate = strtok(element(0, class), " \t\n");

        // if the line contains info not starting with #, add the class
        if (sizeof(candidate) && element(0, element(0, candidate)) != "#")
            g_classes += (list)element(0, strtok(element(0, class), " \t\n"));
    }

    g_nClasses = sizeof(g_classes);
}

void static_lib(string ofiles, string library)
{
    if (sizeof(makelist(ofiles)))
    {
        run("ar cru " + library + " " + ofiles);
        run("ranlib " + library);
        run("rm " + ofiles);
    }
}

void static_library(string library)
{
    static_lib("*/o/*.o", library);
    static_lib("o/*.o", library);
}

void scanner()
{
    chdir("scanner");
    if
    (                                           // new lexer needed
        exists("lexer")
        &&
        "lexer" younger "yylex.cc"
    )
        exec("flex", "lexer");

    chdir("..");
}

/*
                                I N I T I A L . I M
*/
void initialize()
{
    echo(ECHO_REQUEST);
    g_sources = "*.cc";

    g_binary = "tmp/bin/xd" EXTENSION;

    g_cwd = chdir(".");

    if (exists("scanner"))                  // subdir scanner exists
        scanner(); 
    
    setClasses();                           // remaining classes
}

/*
                        L I N K . I M
*/

list inspect(int prefix, list srcList, string library)
{
    int idx;
    string ofile;
    string oprefix;
    string file;

    oprefix = "./o/" + (string)prefix;

    for (idx = sizeof(srcList); idx--; )
    {
        file  = element(idx, srcList);   

        ofile   = oprefix + change_ext(file, "o");    // make o-filename

        // A file s must be recompiled if it's newer than its object
        // file o or newer than its target library l, or if neither o nor l
        // exist. 
        // Since `a newer b' is true if a is newer than b, or if a exists and
        // b doesn't exist s must be compiled if s newer o and s newer l.
        // So, it doesn't have to be compiled if s older o or s older l.
                                            // redo if file has changed
        if (file older ofile || file older library)
            srcList -= (list)file;
    }
    return srcList;
}


void c_compile(int prefix, string srcDir, list cfiles)
{
    int idx;
    string compiler;
    string file;

    compiler = COMPILER;

    #ifdef PROFILE
        compiler += " " PROFILE " ";
    #endif

    compiler +=  " -c -o " + srcDir + "/o/" + (string)prefix;

    md(srcDir + "/o");

    for (idx = sizeof(cfiles); idx--; )
    {
        file = element(idx, cfiles);
        
        run(compiler + change_ext(file, "o") + " " + 
                COPT + " " + srcDir + "/" + file);
    }
}

void std_cpp(int prefix, string srcDir, string library)
{
    list files;

    chdir(srcDir);
                                                      // make list of all files
    files = inspect(prefix, makelist(g_sources), library);  
    chdir(g_cwd);

    if (sizeof(files))
        c_compile(prefix, srcDir, files);             // compile files
}

void cpp_make(string mainfile, string library, int lib_only)
{
    int idx;
    string class;
    string fullLibname;

    fullLibname = "lib" + library + ".a";

    for (idx = g_nClasses; idx--; )
        std_cpp(idx, element(idx, g_classes), "../" + fullLibname);

    if (!lib_only)                          // compile all files in current
                                            // dir
        std_cpp(g_nClasses, ".", fullLibname);  

    static_library(fullLibname);            // make the library
}

void setlibs()
{
        int
            n,
            index;
        list
            cut;
            
        cut = strtok(LIBS, " ");        // cut op libraries
        n = sizeof(cut);
        for (index = 0; index < n; index++)
            libs += " -l" + element(index, cut);

        cut = strtok(LIBPATH, " ");     // cut up the paths
        n = sizeof(cut);
        for (index = 0; index < n; index++)
            libpath += " -L" + element(index, cut);
}

void library(int lib_only)
{
    initialize();
    setlibs();
    
    md("tmp/bin tmp/o");

    special();

    cpp_make
    (
        "xd.cc",      // program source
        "xd",         // static program library base name
        lib_only            // don't compile maindir for lib construction
    );

    if (lib_only)
        exit(0);
}