File: program

package info (click to toggle)
stealth 1.45-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 716 kB
  • ctags: 175
  • sloc: cpp: 1,612; makefile: 88; sh: 70
file content (181 lines) | stat: -rw-r--r-- 4,312 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

#define COPT               "-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 class;

    while (sizeof(class = fgets("CLASSES", (int)element(1, class))))
        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 initialize()
{
    echo(ECHO_REQUEST);
    g_sources = "*.cc";

    g_binary = "tmp/bin/stealth";

    g_cwd = chdir(".");

    setClasses();                           // remaining classes
}

void link(string library)
{
    exec(COMPILER, "-o", g_binary,
            "-l" + library, libs, "-L.", libpath , "-s");
}


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 + " -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 idx;
    string class;
    string fullLibname;

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

    for (idx = g_nClasses; idx--; )
        std_cpp(idx, element(idx, g_classes), "../" + fullLibname);
        
    std_cpp(g_nClasses, ".", fullLibname);  // compile all files in current dir

    static_library(fullLibname);            // make the library

    link(library);
    printf("\n"
            "ok: ", g_binary, "\n");
}

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 program()
{
    initialize();
    setlibs();
    
    md("tmp/bin tmp/o");
    
    special();

    cpp_make
    (
        "stealth.cc",      // program source
        "stealth"          // static program library base name
    );

    exit(0);
}