All Projects → biojppm → regen

biojppm / regen

Licence: MIT license
Easy C++ reflection and code generation

Programming Languages

python
139335 projects - #7 most used programming language
C++
36643 projects - #6 most used programming language
CMake
9771 projects
shell
77523 projects

Projects that are alternatives of or similar to regen

Scala Db Codegen
Scala code/boilerplate generator from a db schema
Stars: ✭ 49 (+68.97%)
Mutual labels:  code-generator, code-generation
Toolkit
Collection of useful patterns
Stars: ✭ 137 (+372.41%)
Mutual labels:  code-generator, code-generation
Mid
mid is a generic domain-specific language for generating code and documentation
Stars: ✭ 68 (+134.48%)
Mutual labels:  code-generator, code-generation
Laravel Code Generator
An intelligent code generator for Laravel framework that will save you time! This awesome tool will help you generate resources like views, controllers, routes, migrations, languages and/or form-requests! It is extremely flexible and customizable to cover many on the use cases. It is shipped with cross-browsers compatible template, along with a client-side validation to modernize your application.
Stars: ✭ 485 (+1572.41%)
Mutual labels:  code-generator, code-generation
Xcassetpacker
A command line tool for converting a folder of images into an .xcasset package for Xcode
Stars: ✭ 150 (+417.24%)
Mutual labels:  code-generator, code-generation
Colfer
binary serialization format
Stars: ✭ 597 (+1958.62%)
Mutual labels:  code-generator, code-generation
Geco
Simple code generator based on a console project, running on .Net core and using C# interpolated strings
Stars: ✭ 97 (+234.48%)
Mutual labels:  code-generator, code-generation
Fpp
Functional PHP Preprocessor - Generate Immutable Data Types
Stars: ✭ 282 (+872.41%)
Mutual labels:  code-generator, code-generation
evon
Fast and versatile event dispatcher code generator for Golang
Stars: ✭ 15 (-48.28%)
Mutual labels:  code-generator, code-generation
Dbcc
CAN DBC to C (and CSV, JSON and XML) compiler using the mpc parser combinator library
Stars: ✭ 142 (+389.66%)
Mutual labels:  code-generator, code-generation
Codemaker
A idea-plugin for Java/Scala, support custom code template.
Stars: ✭ 440 (+1417.24%)
Mutual labels:  code-generator, code-generation
Jennifer
Jennifer is a code generator for Go
Stars: ✭ 2,257 (+7682.76%)
Mutual labels:  code-generator, code-generation
Javaparser
Java 1-15 Parser and Abstract Syntax Tree for Java, including preview features to Java 13
Stars: ✭ 3,972 (+13596.55%)
Mutual labels:  code-generator, code-generation
Dogen
Reference implementation of the MASD Code Generator.
Stars: ✭ 44 (+51.72%)
Mutual labels:  code-generator, code-generation
Loopy
A code generator for array-based code on CPUs and GPUs
Stars: ✭ 367 (+1165.52%)
Mutual labels:  code-generator, code-generation
Goreuse
Generic Code for Go
Stars: ✭ 93 (+220.69%)
Mutual labels:  code-generator, code-generation
Efdesigner
Entity Framework visual design surface and code-first code generation for EF6, Core and beyond
Stars: ✭ 256 (+782.76%)
Mutual labels:  code-generator, code-generation
Xmlschemaclassgenerator
Generate C# classes from XML Schema files
Stars: ✭ 277 (+855.17%)
Mutual labels:  code-generator, code-generation
Php Code Generator
PHP code generator library
Stars: ✭ 141 (+386.21%)
Mutual labels:  code-generator, code-generation
Swiftcolorgen
A tool that generate code for Swift projects, designed to improve the maintainability of UIColors
Stars: ✭ 152 (+424.14%)
Mutual labels:  code-generator, code-generation
Version License: MIT

regen

regen is a python3 package providing C/C++ reflection and source-code generation. You provide your own code generation templates , and have full control over where the generated code goes. A flexible system of annotations is used, so that you can pass meta-values to your code generation templates.

regen is ideal to be used as a pre-build step for your C/C++ project, but it can also be used separately on a file by file basis. Examples of application are object-tree iteration utilities, or property systems with arbitrary per-property annotations, or maintenance-free enum strings.

This is a very fresh pre-alpha project. It is buggy and its interface will change.

How it works

regen receives two inputs: your C/C++ source code and the code generation templates. The code generation templates are written for Python's jinja2 template engine. regen uses libclang to parse your C/C++ code and produce an Abstract Syntax Tree (AST). This tree is processed to extract the features, which are then passed to each generator.

You can see regen being used in the project's examples folder.

Quick examples

Enum stringification

Consider a C++ header, let's name it myenum.h:

#pragma once

C4_ENUM()
typedef enum {
    FOO,
    BAR,
    BAZ
} MyEnum;

Now use the following python code for parsing and generating, saved as regen.py:

import c4.regen as regen

egen = regen.EnumGenerator(
    # extract enums tagged with this macro
    tag="C4_ENUM",
    # header preamble
    hdrp="""\
#include "enum_pairs.h"
"""
    # template for code in header files
    hdr="""\
template<> const EnumPairs< {{enum.type}} > enum_pairs();
""",
    # template for code in source files
    src="""\
template<> const EnumPairs< {{enum.type}} > enum_pairs()
{
    static const EnumAndName< {{enum.type}} > vals[] = {
        {% for e in enum.symbols %}
        { {{e.name}}, "{{e.name}}"},
        {% endfor %}
    };
    EnumPairs< {{enum.type}} > r(vals);
    return r;
}
"""
)
writer = regen.ChunkWriterGenFile()

#------------------------------------------------------------------------------
if __name__ == "__main__":
    regen.run(writer, egen, [])

Now run regen to parse the source code and generate your code:

python regen.py myenum.h

The command above generates myenum.gen.h:

#ifndef _MYENUM_GEN_H_
#define _MYENUM_GEN_H_

#include "enum_pairs.h"
#include "myenum.h"

template<> const EnumPairs< MyEnum > enum_pairs();
#endif // _MYENUM_GEN_H_

and also myenum.gen.cpp:

#include "myenum.gen.h"

template<> const EnumPairs< MyEnum > enum_pairs()
{
    static const EnumAndName< MyEnum > vals[] = {
        { FOO, "FOO"},
        { BAR, "BAR"},
        { BAZ, "BAZ"},
    };
    EnumPairs< MyEnum > r(vals);
    return r;
}

Running

Finding libclang

regen uses libclang-py3, which is a python wrapper for the libclang library. The current version of libclang-py3 requires libclang 3.8. regen tries to find libclang 3.8 by querying llvm-config --libdir (if llvm-config --version reports 3.8) or llvm-config-3.8 --libdir if the first fails. If this also fails, then you can still use the option --clang-libdir.

(This version dependency needs to be fixed; this will probably be done by using different branches).

libclang on windows

libclang is hard to use on windows, but it is useable. While its rough edges are rounded out by the clang developers, we need to deal with its windows problems:

  • The official installer for version 3.8.1 on the LLVM site is broken with VS2015 Update 3, so it won't work out of the box when the C++ library is used. It needs to be compiled from source and patched (AFAIK there's no 3.8.2 release).
  • clang 3.9.1 needs to be run with the Visual Studio developer environment, or it will cause a linker error (no kernel32).
  • Use of the flag -fms-compatibility-version=19 is required (even after compiling).

For this and other reasons it is sometimes good to compile clang from source. To make this task easier, regen has a clang build project, which downloads the source code from llvm, clang and extra tools, patches it as needed, compiles and installs. You can use it like this:

cd regen/tools/clang-build
mkdir build
cd build
cmake -DCLANG_VERSION=3.8.1 ..
cmake --build --config Release .

You can compile several versions at once. For example, to compile versions 3.8.1, 3.9.1 and 4.0.0 in a single swoop, you can configure with this command instead:

cmake -DCLANG_VERSION="3.8.1;3.9.1;4.0.0" ..

Installing

From PyPi

regen installation is easy with the Python package repository. This will install regen along with its dependencies:

pip install regen

From source

git clone https://github.com/biojppm/regen.git
cd regen
pip install .

For development

Setting up regen for development is easy:

git clone https://github.com/biojppm/regen.git
cd regen
pip install -r requirements_dev.txt
pip install -e .

*Windows notes*. The examples rely extensively on symbolic link files. This works as expected in Unix and Mac, but symbolic links were only recently introduced in Windows. Git already allows you to use symbolic links in Windows, but the process is convoluted. Before cloning the repo, you must first enable symlinks in windows. Then you need to pass an option to git clone to ensure that the files are really symbolic links. The clone command thus needs to be:

git clone -c core.symlinks=true https://github.com/biojppm/regen.git

License

cmany is permissively licensed under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].