void cpHeaders()
{
    int idx;
    string file;
    string class;

    for (idx = g_nClasses; idx--; )
    {
        class = element(idx, g_classes);
        file = class + "/" + class;
    
        if (file newer g_tmphdr + class)
            run("cp " + file + " " + g_tmphdr);
    }
}

void static_library(string library)
{
    if (sizeof(makelist("*/oa/*.o")))
    {
        printf("\n"
               "Creating static library ", library, "\n");

        run("ar cru " + library + " */oa/*.o");
        run("ranlib " + library);
        run("rm */oa/*.o");
    }
}

void shared_library(string destDir, string libso, string libsoshared, 
                    int strip)
{
    string libsomajor;
    string gxx;

    gxx = g_cxx + " " + g_copt;
    
    if (sizeof(makelist("*/os/*.o")))
    {
        printf("\n"
               "Creating shared library ", libso, "\n");

        libsomajor  = libso + "." + element(0, strtok(g_version, "."));

        run(gxx +  
                    " " + setOpt(LDFLAGS, "LDFLAGS") +
                    " -shared -Wl,--as-needed,"
                    "-z,defs,-soname," + 
                    libsomajor +
                    " -o " + destDir + libsoshared +
                    " */os/*.o " +
                    g_sharedLibReq);
    
        chdir(destDir);
    
        if (strip)
            run("strip --strip-unneeded " + libsoshared);

        run("chmod -x " + libsoshared);
        
        run("ln -sf " + libsoshared + " " + libsomajor);
        run("ln -sf " + libsomajor  + " " + libso);
    
//        run("rm -f */os/*");

        chdir(g_cwd);
    }
}

void libraries(string libname, int all, int strip)
{
    string libso;
    string libsoshared;
    string staticLib;

    staticLib = g_tmplib + "lib" LIBRARY ".a";

    addClasses("CLASSES");
    special(1, all);

    md(g_tmplib + " " + g_tmphdr);

    cpHeaders();

    g_copt += " -isystem tmp";


    library("oa", staticLib);               // compile for static lib.
    static_library(staticLib);              // build static library

#ifndef STATIC
    libso = "libbobcat.so";
    libsoshared = libso + "." + g_version;

    g_copt += " -fPIC";
                                          // adds option for shared lib

    library("os", g_tmplib + libsoshared);      // compile the shared lib

    shared_library(g_tmplib, libso, libsoshared, strip);
#endif

    exit(0);
}

