3

I am wondering under what license the code generated by a code generator that generates code from conditional code templates would fall under.

Would it be possible to release the generator's source code (generator logic + templates) under say GPLv3 without having the generated code to be GPLv3 licensed too?

Or would a custom license or exception be needed for this to work and if so, would adding such exception to the GPLv3 license of the generator's source code break the compatibility with GPLv3 and/or other opensource licenses?

Or should I release template files under another license (any recommendations if this is a case?) that would allow the final user to license generated code under any open or closed source license and only keep the generator logic licensed under say GPLv3?

I found this similar question: Does "the GPL doesn't cover the ouput of a program" also apply if the output is source code? From what I understood from here, generated files would fall under GPLv3 too if an exception is not provided. Please correct me if I am wrong.

What the question linked in the paragraph above does not cover are my additional questions about how and if to add an exception to GPLv3 or if the template files should be released under another license?

EDIT: The generator output does not contain any part of the generator (except some Java keywords and tiny fractions of code without added value), all code is defined in the templates.

I would prefer it if the templates could not be reused in a closed source non-free situation by a third-party as one of the commenters asked.

4
  • Does your code generator read a template file, apply some mechanical changes to it and output the result, or does the output also contain actual portions of the generator itself? May 14, 2020 at 12:55
  • Would it be acceptable if someone took a template, modified that and distributed the modified template under a non-free license? May 14, 2020 at 12:56
  • "Would it be acceptable if someone took a template, modified that, and distributed the modified template under a non-free license?" - I would prefer the template could only be used in the context of generator, but I am not sure if this legally contradicts the fact that I want the final result to be up to the user to license
    – Klemen
    May 14, 2020 at 14:02
  • "Does your code generator read a template file, apply some mechanical changes to it and output the result, or does the output also contain actual portions of the generator itself?" - No, the result does not contain any part of the generator (except some Java keywords), all code is defined in the templates.
    – Klemen
    May 14, 2020 at 14:03

2 Answers 2

3

As no (copyrightable) part of the generator makes it into the output, the license on the generator code does not affect the license of the output in any way.

The license on the templates is a different matter. As much of the content of the template does make it into the output, the output is legally a derived work of the template used to generate it. That means that any licensing restrictions of the template carry over into the output.

Thus, if the templates are licensed under the GPL, the output of the generator must also be licensed under the GPL. But as you are the copyright holder of the templates, you can grant an exception to the GPL requirements or even choose to put the templates under a completely different license.

If you don't really care what happens to the templates themselves (for example, you are just providing them as a convenience to people using your generator; they could just as easily write them themselves), then you could choose to use a short permissive license, like the MIT or 0BSD. Those licenses put very few requirements on the people that use the template to start a larger work.

If you want the templates to remain open-source always, but allow the use of the code generated from them in a non-free application, you can add an additional permission to the GPL license, for example something like this (adapted from the exception used by GNU Bison):

As a special exception, you may create a larger work that contains part or all of the [Generator] template and distribute that work under terms of your choice, so long as that work isn't itself a template for code generation. Alternatively, if you modify or redistribute the template itself, you may (at your option) remove this special exception, which will cause the template and the resulting [Generator] output files to be licensed under the GNU General Public License without this special exception.

2
  • Using MIT or say Apache 2.0 on the templates would make it possible for users to use the generated code under their own terms and license?
    – Klemen
    May 16, 2020 at 12:32
  • 1
    @Klemen, yes. A permissive license like MIT or Apache allows people to create derived works with a completely different license. A usual requirement is that the copyright and license statements must be preserved. But a derived work could also be another template. May 16, 2020 at 12:37
2

Would it be possible to release the generator's source code (generator logic + templates) under say GPLv3 without having the generated code to be GPLv3 licensed too?

Yes of course. But this is a legal question and I am not a lawyer

Typical examples include:

  • C preprocessors : GNU cpp (part of GCC) is GPLv3+ licensed, but not its output (which is valid C code)

  • other preprocessors, such as GNU m4 (also used by GNU autoconf) or gpp.

  • parser generators such as perhaps ANTLR or GNU bison or GNU flex.

  • more generally, transpilers and source code obfuscators. You could write your own one for C -and publish it under GPLv3+ license- as a GCC plugin. The obfusc obfuscator is GPLv3+ and claims to work on C code. I leave you to test it. You could also adapt the tinycc compiler and make an obfuscator out of it.

  • any Quine program. You certainly can write your own under an MIT license, and you probably could already find one with other licenses.

See also my old GCC MELT (a transpiler for a Lisp dialect generating C++ code for GCC plugins), my Bismon project, the RefPerSys project

I would prefer it if the templates could not be reused in a closed source non-free situation by a third-party as one of the commenters asked.

Look into the licenses of various popular parser generators, such as lemon and GNU bison or GNU flex

But if someone (in a different continent) would -illegally- reuse your code in a closed source situation, what is your concrete way to retaliate? Intercontinental law suits are expensive!

Perhaps you legally could fork some bootstrapped transpiler like s48 or bigloo or Chicken-Scheme as GPLV3+ projects. That is a question for your lawyer.

please consult your lawyer

Details could be dependent of your legal system (not the same in the USA and in the European Union).

And the definitive answer will happen in court.

PS. Writing an obfuscator for C or Java code (or even C++) is really a simple exercise, and could be an internship project for any student having followed courses on compilation. The idea is simple: you collect all the symbols program-wide (e.g. in some sqlite database, perhaps with the help of nm(1)), e.g. a_symb, other_name and you generate a header file like

#define a_symb x12345
#define other_name x12346

etc.... Then you invoke your preprocessor on that header file before every your translation unit. In other terms, a C source obfuscator is a C to C transpiler.

For Java, you could get the bytecode of your entire application and transform it in C or Java using transpiler techniques.

I would refuse to use any obfuscated source code (all my computers run Linux, because it is free software), because in that case I cannot evaluate its software quality (and cannot delegate my trust): it could contain a software virus.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.