前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >eRPC:修改erpcgen代码生成模板增加#if#endif宏定义,解决多个eRPC服务共用时类型重复定义问题

eRPC:修改erpcgen代码生成模板增加#if#endif宏定义,解决多个eRPC服务共用时类型重复定义问题

作者头像
10km
发布2020-10-29 10:43:28
7430
发布2020-10-29 10:43:28
举报
文章被收录于专栏:10km的专栏10km的专栏

最近在用eRPC(https://github.com/EmbeddedRPC/erpc)实现通信时,发现一个问题, 当有两个以上eRPC服务共用时会存在类型重定义问题,比如binary_t会在每个eRPC服务的头文件中定义一次。 解决这个问题只能修改erpcgen的模板,还好,eRPC模板代码结构比较清晰,很快就找到生成eRPC服务接口头文件的位置(${ERPC_ROOT}/erpcgen/src/templates/c_common_header.template) 只要添加类似如下的宏定义就可以解决问题

代码语言:javascript
复制
#if !define(${typename}_DEFINE)
#define ${typename}_DEFINE
#endif /** ${typename}_DEFINE */

修改后的模板文件如下: c_common_header.template

代码语言:javascript
复制
{% if mlComment != ""%}
{$mlComment}

{% endif %}
{$commonHeader()}

#if !defined({$commonGuardMacro})
#define {$commonGuardMacro}
{% if usedUnionType %}

#if defined(__CC_ARM) || defined(__ARMCC_VERSION)
#pragma anon_unions
#endif
{% endif -- usedUnionType %}

{% if not commonTypesFile == "" %}
// Common types header file
#include "{$commonTypesFile}"
{% else %}
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "erpc_version.h"
{% if empty(crc16) == false %}
#include "erpc_crc16.h"
{% endif  -- empty(crc16) == false %}
{% for inc in includes %}
#include "{$inc}"
{% endfor -- includes %}

{$checkVersion()}
{$>checkCrc()}
{% endif -- not commonTypesFile %}
{% if commonTypesFile == "" %}
#if !defined(ERPC_TYPE_DEFINITIONS)
#define ERPC_TYPE_DEFINITIONS
{% if not empty(enums) %}

// Enumerators data types declarations
{%  for enum in enums %}
{% if enum.name != "" %}
{% endif %}
{$> enum.mlComment}
{%   if enum.name %}typedef {% endif --enum.name  %}enum{$addIndent(" ", enum.name)}
{
{%   for enumMember in enum.members %}
{$> addIndent("    ", enumMember.mlComment)}
    {$enumMember.memberDeclaration}{$enumMember.ilComment}
{%   endfor -- enumsMembers %}
}{$addIndent(" ", enum.name)};{$enum.ilComment}{$loop.addNewLineIfNotLast}
{%  endfor -- enums %}
{% endif -- enums %}
{% if not empty(aliases) %}

// Aliases data types declarations
{%  for alias in aliases %}
#if !defined({$alias.name}_TYPEDEF)
#define {$alias.name}_TYPEDEF
{$> alias.mlComment}
{%   if alias.typenameName == "" %}
typedef {$alias.unnamedType}
{
{%    for mem in alias.unnamed.members %}
    {$mem.memberDeclaration}
{%    endfor -- alias.unnamed.members %}
} {$alias.unnamedName};
{%   else -- alias.typenameName %}
typedef {$alias.typenameName};{$alias.ilComment}
{%   endif -- alias.typenameName %}
#endif /** {$alias.name}_TYPEDEF */
{%  endfor -- aliases %}
{% endif -- aliases %}
{% if nonExternalStructUnion %}

// Structures/unions data types declarations
{%  for us in symbols %}
{%   if !us.isExternal %}
#if !defined({$us.name}_DEFINED)
#define {$us.name}_DEFINED
{%    if us.type == "struct" %}
{$> us.mlComment}
struct {$us.name}
{
{%     for mem in us.members %}
{$> addIndent("    ", mem.mlComment)}
    {$mem.memberDeclaration}{$mem.ilComment}
{$> addIndent("    ", mem.elementsCount)}
{%     endfor %}
};{$us.ilComment}{$loop.addNewLineIfNotLast}
{%    else -- us.type == "union" %}
{$> us.mlComment}
union {$us.name}
{
{$ addIndent("    ", unionMembersDeclaration(us))}
};{$us.ilComment}{$loop.addNewLineIfNotLast}
{%    endif -- us.type == "union/struct" %}
#endif /** {$us.name}_DEFINED */
{%   endif -- !us.isExternal %}
{%  endfor -- symbols %}

{% endif -- nonExternalStruct || nonExternalUnion %}
{% if not empty(consts) %}

// Constant variable declarations
{%  for c in consts %}
{$> c.mlComment}
extern const {$c.typeAndName};{$c.ilComment}{$loop.addNewLineIfNotLast}
{%  endfor -- consts %}
{% endif -- consts %}

#endif // ERPC_TYPE_DEFINITIONS
{% endif -- commonTypesFile %}

{% if not genCommonTypesFile %}
{% for iface in group.interfaces %}
/*! @brief {$iface.name} identifiers */
enum _{$iface.name}_ids
{
    k{$iface.name}_service_id = {$iface.id},
{%  for fn in iface.functions %}
    k{$iface.name}_{$fn.name}_id = {$fn.id},
{%  endfor %}
};

{% endfor %}
#if defined(__cplusplus)
extern "C" {
#endif
{% for iface in group.interfaces if iface.isNonExternalInterface == true %}

{$> iface.mlComment}
//! @name {$iface.name}
//@{
{%  for fn in iface.functions if fn.isNonExternalFunction == true %}
{$> fn.mlComment}
{$fn.prototype};{$fn.ilComment}{$loop.addNewLineIfNotLast}
{%  endfor -- functions %}
//@} {$iface.ilComment}
{% endfor -- iface %}

#if defined(__cplusplus)
}
#endif
{% endif -- genCommonTypesFile %}

#endif // {$commonGuardMacro}

以上模板文件在码云仓库的位置

https://gitee.com/l0km/facelog/blob/erpc/facelog-client-cpp/dependencies/erpc/erpc/erpcgen/src/templates/c_common_header.template

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-10-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档