Skip to content

Commit

Permalink
core: ffi types: strip preprocessor directives
Browse files Browse the repository at this point in the history
This is primarily to allow include guards for type declarations that
are used by other tools.

Signed-off-by: Markus Klotzbuecher <mk@mkio.de>
  • Loading branch information
kmarkus committed Dec 7, 2019
1 parent 3901b80 commit fe55558
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 4 additions & 0 deletions API_Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ This file tracks user visible API changes.

## v0.7.1

- core: ffi: types: strip preprocessor directives from types that are
fed to the ffi. This is to allow include guards in these header
files.

- config: add `min`/`max` specifiers for introducing constraints such
as optional or mandatory configs.

Expand Down
20 changes: 16 additions & 4 deletions lua/ubx.lua.source
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ local setup_enums
-- helpers
------------------------------------------------------------------------------

--- preprocess a C string
-- currently this means stripping out all preprocessor directives
-- @param str string to process
-- @return string
local function preproc(str)
local res = {}
for l in str:gmatch("[^\n]+") do
if not (string.match(l, "^%s*#%s*")) then res[#res+1] = l end
end
return table.concat(res, "\n")
end

--- Read the entire contents of a file.
-- @param file name of file
-- @return string contents
Expand Down Expand Up @@ -137,9 +149,9 @@ end

local ubx = nil
local function load_ubx_ffi(prefix)
ffi.cdef(read_file(prefix.."/include/ubx/ubx_uthash_ffi.h"))
ffi.cdef(read_file(prefix.."/include/ubx/ubx_types.h"))
ffi.cdef(read_file(prefix.."/include/ubx/ubx_proto.h"))
ffi.cdef(preproc(read_file(prefix.."/include/ubx/ubx_uthash_ffi.h")))
ffi.cdef(preproc(read_file(prefix.."/include/ubx/ubx_types.h")))
ffi.cdef(preproc(read_file(prefix.."/include/ubx/ubx_proto.h")))
ubx=ffi.load(prefix.."/lib/libubx.so.UBX_IF")
setmetatable(M, { __index=function(t,k) return ubx["ubx_"..k] end })
M.ubx=ubx
Expand Down Expand Up @@ -678,7 +690,7 @@ function M.ffi_load_types(ni)
local function ffi_load_no_ns(typref)
local t = typref.type_ptr
if t.type_class==ubx.TYPE_CLASS_STRUCT and t.private_data~=nil then
local struct_str = ffi.string(t.private_data)
local struct_str = preproc(ffi.string(t.private_data))
local ret, err = pcall(ffi.cdef, struct_str)
if ret==false then
error("ffi_load_types: failed to load the following struct:\n"..struct_str.."\n"..err)
Expand Down
5 changes: 5 additions & 0 deletions std_types/stattypes/types/tstat.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef TSTAT_H
#define TSTAT_H

struct ubx_tstat
{
char block_name[BLOCK_NAME_MAXLEN+1];
Expand All @@ -6,3 +9,5 @@ struct ubx_tstat
struct ubx_timespec total;
unsigned long cnt;
};

#endif /* TSTAT_H */

0 comments on commit fe55558

Please sign in to comment.