Keeping track of source code variants

后端 未结 6 1819
眼角桃花
眼角桃花 2021-02-06 02:03

I am soon starting to maintain a line of products containing variants of the same embedded software. Since I\'ve been playing with git for one year and appreciate it very much,

6条回答
  •  甜味超标
    2021-02-06 02:53

    I think that the appropriate answer depends in part on how radically different the variants are.

    If there are small portions that are different, using conditional compilation on a single source file is reasonable. If the variant implementations are only consistent at the call interface, then it may be better to use separate files. You can include radically variant implementations in a single file with conditional compilation; how messy that is depends on the volume of variant code. If it is, say, four variants of about 100 lines each, maybe one file is OK. If it is four variants of 100, 300, 500 and 900 lines, then one file is probably a bad idea.

    You don't necessarily need the variants on separate branches; indeed, you should only use branches when necessary (but do use them when they are necessary!). You can have the four files, say, all on a common branch, always visible. You can arrange for the compilation to pick up the correct variant. One possibility (there are many others) is compiling a single source file that knows which source variant to include given the current compilation environment:

    #include "config.h"
    #if defined(USE_VARIANT_A)
    #include "variant_a.c"
    #elif defined(USE_VARIANT_B)
    #include "variant_b.c"
    #else
    #include "basecase.c"
    #endif
    

提交回复
热议问题