Links

Categories

Tags


« | Main | »

Making a C library monolithic

By Jewe | March 13, 2016

This is the script I am using to make the “monolithic” source code version of the JewelScript library. Feel free to rip the code from this site and change it to your needs, if you would like to try to make a monolithic version of your project, too.

I think the code is pretty straightforward and self-explaining. Just change the output and input directories to whatever you need them to be and build a list of your include / C files like I have done in the main-function.

To run the script, you can download the JILRun Runtime Environment for Windows.

Code

//------------------------------------------------------------------------------
// make_mono.jc
//
// builds a monolithic C and H file from the JewelScript sources.
//------------------------------------------------------------------------------

import stdlib;
import System::IO::File;
import System::Text::Regex;

class MakeMono
{
    using stdlib;
    using System::IO;
    using System::Text::Regex;
    
    //------------------------------------------------------------------------------
    // constants
    //------------------------------------------------------------------------------

    const string RepoInputDir  = "E:/Source/SVN/jewe.org/sourceforge/jewelscript/jilruntime";
    const string RepoOutputDir = "E:/Source/SVN/jewe.org/sourceforge/jewelscript/jilruntime-mono";

    const Regex ReInclude = new Regex(/"#include\s+["][a-z0-9/.\-_]+["]"/, "i");
    
    const Regex ReComment = new Regex(/"^\s*//.*$"/, "m");
    //const Regex ReComment = null; // Uncomment if you don't want to remove single-line comments
    
    const Regex ReEmpty = new Regex(/"\n\n+"/);
    //const Regex ReEmpty = null; // Uncomment if you don't want to collapse empty lines

    //------------------------------------------------------------------------------
    // represents a source file reference
    //------------------------------------------------------------------------------

    class Ref
    {
        const int Default = 1, Verbatim = 2;
        string FileName;
        int Flags;
        method Ref(const string s, int f)
        {
            FileName = s;
            Flags = f;
        }
    }

    //------------------------------------------------------------------------------
    // Main
    //------------------------------------------------------------------------------

    function Main()
    {
        // these tables define which file is to append to which output file in what order
        Ref[] HppRefs = {
              new Ref("src/jilstdinc.h", Default)
            , new Ref("include/jilversion.h", Default)
            , new Ref("include/jilplatform.h", Default)
            , new Ref("include/jilapitypes.h", Default)
            , new Ref("include/jilnativetypeex.h", Default)
            , new Ref("include/jilnativetype.h", Default)
            , new Ref("include/jilexception.h", Default)
            , new Ref("include/jilapi.h", Default)
            , new Ref("include/jilcompilerapi.h", Default)
            , new Ref("src/jilstring.h", Default)
            , new Ref("src/jilarray.h", Default)
            , new Ref("src/jilcodelist.h", Default)
            , new Ref("src/jildebug.h", Default)
        };

        Ref[] CppRefs = {
            // add more include statements here if your library includes external headers
              new Ref("#include \"jilruntime.h\"\n", Verbatim)
            // runtime includes
            , new Ref("src/jiltypes.h", Default)
            , new Ref("src/jiltools.h", Default)
            , new Ref("src/jilallocators.h", Default)
            , new Ref("src/jilcallntl.h", Default)
            , new Ref("src/jilchunk.h", Default)
            , new Ref("src/jilcstrsegment.h", Default)
            , new Ref("src/jilfixmem.h", Default)
            , new Ref("src/jilfragmentedarray.h", Default)
            , new Ref("src/jilhandle.h", Default)
            , new Ref("src/jilarraylist.h", Default)
            , new Ref("src/jillist.h", Default)
            , new Ref("src/jiltable.h", Default)
            , new Ref("src/jilmachine.h", Default)
            , new Ref("src/jilopcodes.h", Default)
            , new Ref("src/jilopmacros.h", Default)
            , new Ref("src/jilprogramming.h", Default)
            , new Ref("src/jilsymboltable.h", Default)
            , new Ref("src/jiltypeinfo.h", Default)
            , new Ref("src/jiltypelist.h", Default)
            // compiler includes
            , new Ref("src/jcltools.h", Default)
            , new Ref("src/jclarray.h", Default)
            , new Ref("src/jclstring.h", Default)
            , new Ref("src/jclpair.h", Default)
            , new Ref("src/jcloption.h", Default)
            , new Ref("src/jclfile.h", Default)
            , new Ref("src/jclvar.h", Default)
            , new Ref("src/jclclause.h", Default)
            , new Ref("src/jclfunc.h", Default)
            , new Ref("src/jclclass.h", Default)
            , new Ref("src/jclstate.h", Default)
            // runtime sources
            , new Ref("src/bind_arraylist.c", Default)
            , new Ref("src/bind_runtime.c", Default)
            , new Ref("src/bind_runtime_exception.c", Default)
            , new Ref("src/bind_stringMatch.c", Default)
            , new Ref("src/jilallocators.c", Default)
            , new Ref("src/jilarray.c", Default)
            , new Ref("src/jilarraylist.c", Default)
            , new Ref("src/jilchunk.c", Default)
            , new Ref("src/jilcodelist.c", Default)
            , new Ref("src/jilcompiler.c", Default)
            , new Ref("src/jilcstrsegment.c", Default)
            , new Ref("src/jildebug.c", Default)
            , new Ref("src/jilexception.c", Default)
            , new Ref("src/jilexecbytecode.c", Default)
            , new Ref("src/jilfixmem.c", Default)
            , new Ref("src/jilfragmentedarray.c", Default)
            , new Ref("src/jilhandle.c", Default)
            , new Ref("src/jiliterator.c", Default)
            , new Ref("src/jillist.c", Default)
            , new Ref("src/jilmachine.c", Default)
            , new Ref("src/jilnativetype.c", Default)
            , new Ref("src/jiloptables.c", Default)
            , new Ref("src/jilprogramming.c", Default)
            , new Ref("src/jilruntime.c", Default)
            , new Ref("src/jilstring.c", Default)
            , new Ref("src/jilsymboltable.c", Default)
            , new Ref("src/jiltable.c", Default)
            , new Ref("src/jiltools.c", Default)
            , new Ref("src/jiltypeinfo.c", Default)
            , new Ref("src/jiltypelist.c", Default)
            // compiler sources
            , new Ref("src/jclarray.c", Default)
            , new Ref("src/jclclass.c", Default)
            , new Ref("src/jclclause.c", Default)
            , new Ref("src/jclerrors.c", Default)
            , new Ref("src/jclfile.c", Default)
            , new Ref("src/jclfunc.c", Default)
            , new Ref("src/jclgendoc.c", Default)
            , new Ref("src/jcllinker.c", Default)
            , new Ref("src/jclnative.c", Default)
            , new Ref("src/jcloption.c", Default)
            , new Ref("src/jclpair.c", Default)
            , new Ref("src/jclstate.c", Default)
            , new Ref("src/jclstring.c", Default)
            , new Ref("src/jclvar.c", Default)
        };

        Run(HppRefs, "jilruntime.h");
        Run(CppRefs, "jilruntime.c");
    }

    //------------------------------------------------------------------------------
    // Run
    //------------------------------------------------------------------------------

    function Run(Ref[] refs, const string outputFile)
    {
        println("Generating monolithic file " + outputFile);
        clause (string msg)
        {
            File inFile, outFile;
            string outpath = RepoOutputDir + "/" + outputFile;
            if (outFile.Open(outpath, Write))
                goto Error("Could not open " + outpath);
            for (int i = 0; i < refs.length; i++)
            {
                Ref ref = refs[i];
                string contents;
                if (ref.Flags == Ref::Default)
                {
                    string inpath = RepoInputDir + "/" + ref.FileName;
                    if (inFile.Open(inpath, Read))
                        goto Error("Could not open " + inpath);
                    contents = inFile.ReadText();
                    contents = ReInclude.Replace(contents, "");
                    if (ReComment != null)
                        contents = ReComment.Replace(contents, "");
                    if (ReEmpty != null)
                        contents = ReEmpty.Replace(contents, "\n\n");
                    inFile.Close();
                }
                else if (ref.Flags == Ref::Verbatim)
                {
                    contents = ref.FileName;
                }
                outFile.WriteText(contents);
                outFile.WriteLine("");
            }
            outFile.Close();
        }
        clause Error
        {
            println("ERROR: " + msg);
        }
    }
}

//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------

function string main(const string[] args)
{
    MakeMono::Main();
    return null;
}

Topics: code examples, docs, news | Comments Off on Making a C library monolithic

Comments are closed.