Skip to content

Commit

Permalink
[common,Pal/Linux] Compile with Meson
Browse files Browse the repository at this point in the history
common/ is mesonified and used in Pal/Linux only for now.

Signed-off-by: Wojtek Porczyk <woju@invisiblethingslab.com>
  • Loading branch information
woju committed Sep 30, 2021
1 parent 3e778e5 commit 7e59c4d
Show file tree
Hide file tree
Showing 14 changed files with 387 additions and 8 deletions.
5 changes: 1 addition & 4 deletions Pal/src/host/Linux/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ pal_direct_map = custom_target('pal.map[direct]',
output: 'pal.map',
)

libgramine_pal_direct = cc.find_library('gramine-lib',
dirs: join_paths(meson.current_source_dir(), '.lib'))

# TODO after meson>=0.46 change this to both_libraries
foreach target_type : ['shared_library', 'static_library']
target = build_target('pal',
Expand Down Expand Up @@ -74,7 +71,7 @@ foreach target_type : ['shared_library', 'static_library']
],

dependencies: [
libgramine_pal_direct,
common_dep,
],

install: true,
Expand Down
11 changes: 7 additions & 4 deletions common/include/meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
common_inc = include_directories(
'.',
join_paths('arch', host_machine.cpu_family()),
)
common_inc = [
include_directories(
'.',
join_paths('arch', host_machine.cpu_family()),
),
tomlc99_inc, # TODO this is because there's #include "toml.h" in api.h
]
1 change: 1 addition & 0 deletions common/meson.build
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
subdir('include')
subdir('src')
37 changes: 37 additions & 0 deletions common/src/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
common_src = files(
'avl_tree.c',
'network/hton.c',
'network/inet_pton.c',
'path.c',
'stack_protector.c',
'stdlib/printfmt.c',
'string/atoi.c',
'string/ctype.c',
'string/memcmp.c',
'string/memcpy.c',
'string/memset.c',
'string/strchr.c',
'string/strcmp.c',
'string/strlen.c',
'string/strspn.c',
'string/strstr.c',
'string/toml_utils.c',
'string/utils.c',
)
if asan
common_src += files('asan.c')
endif
if ubsan
common_src += files('ubsan.c')
endif

common_dep = declare_dependency(
sources: common_src,

include_directories: common_inc,

dependencies: [
uthash_dep,
tomlc99_dep,
],
)
8 changes: 8 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ endif
# Dependencies
#

tomlc99_proj = subproject('tomlc99-208203af46bdbdb29ba199660ed78d09c220b6c5')
tomlc99_dep = tomlc99_proj.get_variable('tomlc99_dep')
tomlc99_inc = tomlc99_proj.get_variable('tomlc99_inc')
tomlc99_incpath = tomlc99_proj.get_variable('tomlc99_incpath')
tomlc99_src = tomlc99_proj.get_variable('tomlc99_src')

uthash_dep = subproject('uthash-2.1.0').get_variable('uthash_dep')

if sgx
threads_dep = dependency('threads')

Expand Down
1 change: 1 addition & 0 deletions subprojects/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/cJSON-*/
/glibc-*/
/mbedtls-*/
/tomlc99-*/
/uthash-*/
30 changes: 30 additions & 0 deletions subprojects/packagefiles/tomlc99/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
project('tomlc99', 'c')

patch_prog = find_program('patch')

tomlc99_src = []
foreach src : ['toml.c', 'toml.h']
src_patch = '@0@.patch'.format(src)
tomlc99_src += custom_target(src,
command: [
patch_prog,
'-p1',
'--quiet',
'--directory=@CURRENT_SOURCE_DIR@',
'--input=@0@'.format(src_patch),
'--output=-',
],
input: [src_patch, src],
output: src,
capture: true,
)
endforeach

tomlc99_inc = include_directories('.')
tomlc99_incpath = meson.current_build_dir()

tomlc99_dep = declare_dependency(
sources: tomlc99_src,
include_directories: tomlc99_inc,
)

191 changes: 191 additions & 0 deletions subprojects/packagefiles/tomlc99/toml.c.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
diff --git a/toml.c b/toml.c
index 8ec554e88ec772931aebcff615a6d9624ffacbe6..382fdeda573b6b9aa8402c66e960b4edcb2c7d65 100644
--- a/toml.c
+++ b/toml.c
@@ -24,15 +24,7 @@
SOFTWARE.

*/
-#define _POSIX_C_SOURCE 200809L
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-#include <errno.h>
-#include <stdint.h>
-#include <ctype.h>
-#include <string.h>
-#include <stdbool.h>
+#include "api.h"
#include "toml.h"


@@ -1447,61 +1439,6 @@ fail:
}


-toml_table_t* toml_parse_file(FILE* fp,
- char* errbuf,
- int errbufsz)
-{
- int bufsz = 0;
- char* buf = 0;
- int off = 0;
-
- /* read from fp into buf */
- while (! feof(fp)) {
-
- if (off == bufsz) {
- int xsz = bufsz + 1000;
- char* x = expand(buf, bufsz, xsz);
- if (!x) {
- snprintf(errbuf, errbufsz, "out of memory");
- xfree(buf);
- return 0;
- }
- buf = x;
- bufsz = xsz;
- }
-
- errno = 0;
- int n = fread(buf + off, 1, bufsz - off, fp);
- if (ferror(fp)) {
- snprintf(errbuf, errbufsz, "%s",
- errno ? strerror(errno) : "Error reading file");
- xfree(buf);
- return 0;
- }
- off += n;
- }
-
- /* tag on a NUL to cap the string */
- if (off == bufsz) {
- int xsz = bufsz + 1;
- char* x = expand(buf, bufsz, xsz);
- if (!x) {
- snprintf(errbuf, errbufsz, "out of memory");
- xfree(buf);
- return 0;
- }
- buf = x;
- bufsz = xsz;
- }
- buf[off] = 0;
-
- /* parse it, cleanup and finish */
- toml_table_t* ret = toml_parse(buf, errbuf, errbufsz);
- xfree(buf);
- return ret;
-}
-
-
static void xfree_kval(toml_keyval_t* p)
{
if (!p) return;
@@ -2062,66 +1999,19 @@ int toml_rtoi(toml_raw_t src, int64_t* ret_)

/* Run strtoll on buf to get the integer */
char* endp;
- errno = 0;
*ret = strtoll(buf, &endp, base);
- return (errno || *endp) ? -1 : 0;
+ return (*endp) ? -1 : 0;
}


int toml_rtod_ex(toml_raw_t src, double* ret_, char* buf, int buflen)
{
- if (!src) return -1;
-
- char* p = buf;
- char* q = p + buflen;
- const char* s = src;
- double dummy;
- double* ret = ret_ ? ret_ : &dummy;
-
-
- /* allow +/- */
- if (s[0] == '+' || s[0] == '-')
- *p++ = *s++;
-
- /* disallow +_1.00 */
- if (s[0] == '_')
- return -1;
-
- /* decimal point, if used, must be surrounded by at least one digit on each side */
- {
- char* dot = strchr(s, '.');
- if (dot) {
- if (dot == s || !isdigit(dot[-1]) || !isdigit(dot[1]))
- return -1;
- }
- }
-
- /* zero must be followed by . or 'e', or NUL */
- if (s[0] == '0' && s[1] && !strchr("eE.", s[1]))
- return -1;
-
- /* just strip underscores and pass to strtod */
- while (*s && p < q) {
- int ch = *s++;
- if (ch == '_') {
- // disallow '__'
- if (s[0] == '_') return -1;
- // disallow last char '_'
- if (s[0] == 0) return -1;
- continue; /* skip _ */
- }
- *p++ = ch;
- }
- if (*s || p == q) return -1; /* reached end of string or buffer is full? */
-
- /* cap with NUL */
- *p = 0;
-
- /* Run strtod on buf to get the value */
- char* endp;
- errno = 0;
- *ret = strtod(buf, &endp);
- return (errno || *endp) ? -1 : 0;
+ /* FIXME: strtod() is not supported by Gramine */
+ (void)src;
+ (void)ret_;
+ (void)buf;
+ (void)buflen;
+ return -1;
}

int toml_rtod(toml_raw_t src, double* ret_)
@@ -2240,33 +2130,6 @@ toml_datum_t toml_timestamp_at(const toml_array_t* arr, int idx)
return ret;
}

-toml_datum_t toml_string_in(const toml_table_t* arr, const char* key)
-{
- toml_datum_t ret;
- memset(&ret, 0, sizeof(ret));
- toml_raw_t raw = toml_raw_in(arr, key);
- if (raw) {
- ret.ok = (0 == toml_rtos(raw, &ret.u.s));
- }
- return ret;
-}
-
-toml_datum_t toml_bool_in(const toml_table_t* arr, const char* key)
-{
- toml_datum_t ret;
- memset(&ret, 0, sizeof(ret));
- ret.ok = (0 == toml_rtob(toml_raw_in(arr, key), &ret.u.b));
- return ret;
-}
-
-toml_datum_t toml_int_in(const toml_table_t* arr, const char* key)
-{
- toml_datum_t ret;
- memset(&ret, 0, sizeof(ret));
- ret.ok = (0 == toml_rtoi(toml_raw_in(arr, key), &ret.u.i));
- return ret;
-}
-
toml_datum_t toml_double_in(const toml_table_t* arr, const char* key)
{
toml_datum_t ret;
36 changes: 36 additions & 0 deletions subprojects/packagefiles/tomlc99/toml.h.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
diff --git a/toml.h b/toml.h
index b91ef890303238e734f06bbfc95ab39b7851e031..735518d25d411d78ad417efb333683014c6d4c45 100644
--- a/toml.h
+++ b/toml.h
@@ -26,7 +26,6 @@
#define TOML_H


-#include <stdio.h>
#include <stdint.h>


@@ -41,13 +40,6 @@ typedef struct toml_table_t toml_table_t;
typedef struct toml_array_t toml_array_t;
typedef struct toml_datum_t toml_datum_t;

-/* Parse a file. Return a table on success, or 0 otherwise.
- * Caller must toml_free(the-return-value) after use.
- */
-TOML_EXTERN toml_table_t* toml_parse_file(FILE* fp,
- char* errbuf,
- int errbufsz);
-
/* Parse a string containing the full config.
* Return a table on success, or 0 otherwise.
* Caller must toml_free(the-return-value) after use.
@@ -110,9 +102,6 @@ TOML_EXTERN toml_table_t* toml_table_at(const toml_array_t* arr, int idx);
/* ... retrieve the key in table at keyidx. Return 0 if out of range. */
TOML_EXTERN const char* toml_key_in(const toml_table_t* tab, int keyidx);
/* ... retrieve values using key. */
-TOML_EXTERN toml_datum_t toml_string_in(const toml_table_t* arr, const char* key);
-TOML_EXTERN toml_datum_t toml_bool_in(const toml_table_t* arr, const char* key);
-TOML_EXTERN toml_datum_t toml_int_in(const toml_table_t* arr, const char* key);
TOML_EXTERN toml_datum_t toml_double_in(const toml_table_t* arr, const char* key);
TOML_EXTERN toml_datum_t toml_timestamp_in(const toml_table_t* arr, const char* key);
/* .. retrieve array or table using key. */
10 changes: 10 additions & 0 deletions subprojects/packagefiles/uthash/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
project('uthash', 'c')

patch_prog = find_program('patch')

subdir('src')

uthash_dep = declare_dependency(
sources: uthash_h,
include_directories: include_directories('src/'),
)
13 changes: 13 additions & 0 deletions subprojects/packagefiles/uthash/src/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
uthash_h = custom_target('uthash.h',
command: [
patch_prog,
'-p1',
'--quiet',
'--directory=@CURRENT_SOURCE_DIR@',
'--input=uthash.h.patch',
'--output=-',
],
input: ['uthash.h.patch', 'uthash.h'],
output: 'uthash.h',
capture: true,
)

0 comments on commit 7e59c4d

Please sign in to comment.