Skip to content

Commit 2121b9e

Browse files
committed
[PBCKP-245] fu_utils: compilable under MinGW
1 parent 585a069 commit 2121b9e

File tree

13 files changed

+178
-104
lines changed

13 files changed

+178
-104
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ BORROWED_C_SRC := \
5151
src/bin/pg_basebackup/streamutil.c \
5252
src/bin/pg_basebackup/walmethods.c
5353

54-
OBJS += src/fu_util/impl/ft_impl.o src/fu_util/impl/fo_impl.o
55-
5654
BORROW_DIR := src/borrowed
5755
BORROWED_H := $(addprefix $(BORROW_DIR)/, $(notdir $(BORROWED_H_SRC)))
5856
BORROWED_C := $(addprefix $(BORROW_DIR)/, $(notdir $(BORROWED_C_SRC)))
5957
OBJS += $(patsubst %.c, %.o, $(BORROWED_C))
6058
EXTRA_CLEAN := $(BORROWED_H) $(BORROWED_C) $(BORROW_DIR) borrowed.mk
6159

60+
OBJS += src/fu_util/impl/ft_impl.o src/fu_util/impl/fo_impl.o
61+
6262
# off-source build support
6363
ifneq ($(abspath $(CURDIR))/, $(top_pbk_srcdir))
6464
VPATH := $(top_pbk_srcdir)

src/fu_util/CMakeLists.txt

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,29 @@ set(CMAKE_C_STANDARD 99)
55
set(CMAKE_C_EXTENSIONS true)
66

77
include(CheckCSourceCompiles)
8+
include(CheckFunctionExists)
89

910
add_library(fu_utils impl/ft_impl.c impl/fo_impl.c)
1011

1112
set(THREADS_PREFER_PTHREAD_FLAG ON)
1213
find_package(Threads REQUIRED)
1314
target_link_libraries(fu_utils PRIVATE Threads::Threads)
1415

16+
if(CMAKE_USE_PTHREADS_INIT)
17+
target_compile_definitions(fu_utils PRIVATE USE_PTHREADS)
18+
else()
19+
message(FATAL_ERROR "Need pthread support to build")
20+
endif()
21+
22+
CHECK_FUNCTION_EXISTS(strerror_r HAVE_STRERROR_R)
23+
1524
# Detect for installed beautiful https://github.com/ianlancetaylor/libbacktrace
1625
include_directories(.)
1726
if(NOT CMAKE_C_COMPILER MATCHES tcc)
1827
find_library(LIBBACKTRACE backtrace)
1928
if(LIBBACKTRACE)
2029
set(CMAKE_REQUIRED_LIBRARIES backtrace)
30+
target_link_libraries(fu_utils PRIVATE backtrace)
2131
check_c_source_compiles("
2232
#include <backtrace.h>
2333
int main(void) {
@@ -30,11 +40,21 @@ if(NOT CMAKE_C_COMPILER MATCHES tcc)
3040
endif()
3141
endif()
3242
endif()
43+
check_include_file(execinfo.h HAVE_EXECINFO_H)
44+
45+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fexceptions")
46+
47+
if(HAVE_EXECINFO_H)
48+
target_compile_definitions(fu_utils PRIVATE HAVE_EXECINFO_H)
49+
endif()
50+
if(HAVE_STRERROR_R)
51+
target_compile_definitions(fu_utils PRIVATE HAVE_STRERROR_R)
52+
endif()
53+
3354

3455
configure_file(fu_utils_cfg.h.in fu_utils_cfg.h)
3556
target_include_directories(fu_utils INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}")
3657
target_include_directories(fu_utils PRIVATE "${PROJECT_BINARY_DIR}")
37-
target_link_libraries(fu_utils PUBLIC backtrace)
3858

3959
install(TARGETS fu_utils DESTINATION lib)
4060
install(FILES fm_util.h ft_util.h fo_obj.h
@@ -43,4 +63,4 @@ install(FILES fm_util.h ft_util.h fo_obj.h
4363
DESTINATION include/fu_utils)
4464
install(FILES impl/ft_impl.h impl/fo_impl.h DESTINATION include/fu_utils/impl)
4565

46-
add_subdirectory(test)
66+
add_subdirectory(test)

src/fu_util/fm_util.h

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
/****************************************/
2525
// LOGIC
2626

27+
#define fm_true 1
28+
#define fm_false 0
29+
2730
#define fm_compl(v) fm_cat(fm_compl_, v)
2831
#define fm_compl_0 1
2932
#define fm_compl_1 0
@@ -81,38 +84,56 @@
8184
#define fm_tail(...) fm__tail(__VA_ARGS__)
8285
#define fm__tail(x, ...) __VA_ARGS__
8386

84-
#define fm_or_default(...) \
85-
fm_iif(fm_va_01(__VA_ARGS__))(__VA_ARGS__)
8687
#define fm_va_single(...) fm__va_single(__VA_ARGS__, fm__comma)
8788
#define fm_va_many(...) fm__va_many(__VA_ARGS__, fm__comma)
8889
#define fm__va_single(x, y, ...) fm__va_result(y, 1, 0)
8990
#define fm__va_many(x, y, ...) fm__va_result(y, 0, 1)
90-
#define fm__va_result(x, y, res, ...) res
91+
#define fm__va_result(...) fm__va_result_fin(__VA_ARGS__)
92+
#define fm__va_result_fin(x, y, res, ...) res
9193

9294
#define fm_no_va fm_is_empty
9395
#define fm_va_01 fm_isnt_empty
94-
#define fm_va_01n(...) fm_cat3(fm__va_01n_, fm__isnt_empty(__VA_ARGS__), fm_va_many(__VA_ARGS__))
95-
#define fm__va_01n_00 0
96-
#define fm__va_01n_10 1
97-
#define fm__va_01n_11 n
9896

99-
#if !__STRICT_ANSI__
97+
#ifndef FM_USE_STRICT
98+
#if defined(__STRICT_ANSI__) || defined(_MSC_VER) /* well, clang-cl doesn't allow to distinguish std mode */
99+
#define FM_USE_STRICT
100+
#endif
101+
#endif
102+
103+
#ifndef FM_USE_STRICT
100104
#define fm_is_empty(...) fm__is_empty(__VA_ARGS__)
101105
#define fm__is_empty(...) fm_va_single(~, ##__VA_ARGS__)
102106
#define fm_isnt_empty(...) fm__isnt_empty(__VA_ARGS__)
103107
#define fm__isnt_empty(...) fm_va_many(~, ##__VA_ARGS__)
108+
109+
#define fm_va_01n(...) fm_cat3(fm__va_01n_, fm__isnt_empty(__VA_ARGS__), fm_va_many(__VA_ARGS__))
110+
#define fm__va_01n_00 0
111+
#define fm__va_01n_10 1
112+
#define fm__va_01n_11 n
113+
114+
#define fm_when_isnt_empty(...) fm_cat(fm__when_, fm__isnt_empty(__VA_ARGS__))
104115
#else
105116
#define fm_is_empty(...) fm_and(fm__is_emptyfirst(__VA_ARGS__), fm_va_single(__VA_ARGS__))
106117
#define fm_isnt_empty(...) fm_nand(fm__is_emptyfirst(__VA_ARGS__), fm_va_single(__VA_ARGS__))
107118

108119
#define fm__is_emptyfirst(x, ...) fm_iif(fm_is_tuple(x))(0)(fm__is_emptyfirst_impl(x))
109-
#define fm__is_emptyfirst_impl(x,...) fm_tuple_2((\
110-
fm__is_emptyfirst_do1 x (fm__is_emptyfirst_do2), 1, 0))
120+
#define fm__is_emptyfirst_impl(x,...) fm__va_result(\
121+
fm__is_emptyfirst_do1 x (fm__is_emptyfirst_do2), 1, 0)
111122
#define fm__is_emptyfirst_do1(F) F()
112123
#define fm__is_emptyfirst_do2(...) ,
124+
125+
#define fm_when_isnt_empty(...) fm_cat(fm__when_, fm_isnt_empty(__VA_ARGS__))
126+
127+
#define fm_va_01n(...) fm_cat3(fm__va_01n_, fm__is_emptyfirst(__VA_ARGS__), fm_va_many(__VA_ARGS__))
128+
#define fm__va_01n_10 0
129+
#define fm__va_01n_00 1
130+
#define fm__va_01n_01 n
131+
#define fm__va_01n_11 n
113132
#endif
114133

115-
#define fm_when_isnt_empty(...) fm_cat(fm__when_, fm__isnt_empty(__VA_ARGS__))
134+
#define fm_or_default(...) \
135+
fm_iif(fm_va_01(__VA_ARGS__))(__VA_ARGS__)
136+
116137
#define fm_va_comma(...) \
117138
fm_when_isnt_empty(__VA_ARGS__)(fm__comma)
118139
#define fm_va_comma_fun(...) \
@@ -127,23 +148,6 @@
127148
#define fm__is_tuple_help(...) ,
128149
#define fm__is_tuple_(...) fm__is_tuple_choose(__VA_ARGS__)
129150

130-
#define fm_tuple_expand(x) fm_expand x
131-
#define fm_tuple_tag(x) fm_head x
132-
#define fm_tuple_data(x) fm_tail x
133-
#define fm_tuple_0(x) fm_head x
134-
#define fm_tuple_1(x) fm__tuple_1 x
135-
#define fm__tuple_1(_0, _1, ...) _1
136-
#define fm_tuple_2(x) fm__tuple_2 x
137-
#define fm__tuple_2(_0, _1, _2, ...) _2
138-
139-
#define fm_tuple_tag_or_0(x) fm__tuple_tag_or_0_(fm__tuple_tag_or_0_help x, 0)
140-
#define fm__tuple_tag_or_0_(...) fm__tuple_tag_or_0_choose(__VA_ARGS__)
141-
#define fm__tuple_tag_or_0_choose(a,x,...) x
142-
#define fm__tuple_tag_or_0_help(tag, ...) , tag
143-
144-
#define fm_dispatch_tag_or_0(prefix, x) \
145-
fm_cat(prefix, fm_tuple_tag_or_0(x))
146-
147151
/****************************************/
148152
// Iteration
149153

@@ -160,71 +164,63 @@
160164

161165
// recursion handle : delay macro expansion to next recursion iteration
162166
#define fm_recurs(id) id fm_empty fm_empty() ()
163-
#define fm_recurs2(a,b) fm_cat fm_empty fm_empty() () (a,b)
167+
#define fm_recurs2(a,b) fm_cat fm_empty() (a,b)
164168
#define fm_defer(id) id fm_empty()
165169

166170
#define fm_foreach_join(join, macro, ...) \
167-
fm_foreach_join_(fm_empty, join, macro, __VA_ARGS__)
168-
#define fm_foreach_join_(join1, join2, macro, ...) \
169-
fm_cat(fm_foreach_join_, fm_va_01n(__VA_ARGS__))(join1, join2, macro, __VA_ARGS__)
171+
fm_cat(fm_foreach_join_, fm_va_01n(__VA_ARGS__))(fm_empty, join, macro, __VA_ARGS__)
170172
#define fm_foreach_join_0(join1, join2, macro, ...)
171173
#define fm_foreach_join_1(join1, join2, macro, x) \
172174
join1() macro(x)
173175
#define fm_foreach_join_n(join1, join2, macro, x, y, ...) \
174176
join1() macro(x) \
175177
join2() macro(y) \
176-
fm_recurs2(fm_, foreach_join_) (join2, join2, macro, __VA_ARGS__)
178+
fm_recurs2(fm_foreach_join_, fm_va_01n(__VA_ARGS__))(join2, join2, macro, __VA_ARGS__)
177179

178180
#define fm_foreach(macro, ...) \
179181
fm_foreach_join(fm_empty, macro, __VA_ARGS__)
180182
#define fm_foreach_comma(macro, ...) \
181183
fm_foreach_join(fm_comma, macro, __VA_ARGS__)
182184

183185
#define fm_foreach_arg_join(join, macro, arg, ...) \
184-
fm_foreach_arg_join_(fm_empty, join, macro, arg, __VA_ARGS__)
185-
#define fm_foreach_arg_join_(join1, join2, macro, arg, ...) \
186-
fm_cat(fm_foreach_arg_join_, fm_va_01n(__VA_ARGS__))(join1, join2, macro, arg, __VA_ARGS__)
186+
fm_cat(fm_foreach_arg_join_, fm_va_01n(__VA_ARGS__))(fm_empty, join, macro, arg, __VA_ARGS__)
187187
#define fm_foreach_arg_join_0(join1, join2, macro, ...)
188188
#define fm_foreach_arg_join_1(join1, join2, macro, arg, x) \
189189
join1() macro(arg, x)
190190
#define fm_foreach_arg_join_n(join1, join2, macro, arg, x, y, ...) \
191191
join1() macro(arg, x) \
192192
join2() macro(arg, y) \
193-
fm_recurs2(fm_, foreach_arg_join_) (join2, join2, macro, arg, __VA_ARGS__)
193+
fm_recurs2(fm_foreach_arg_join_, fm_va_01n(__VA_ARGS__))(join1, join2, macro, arg, __VA_ARGS__)
194194

195195
#define fm_foreach_arg(macro, arg, ...) \
196196
fm_foreach_arg_join(fm_empty, macro, arg, __VA_ARGS__)
197197
#define fm_foreach_arg_comma(macro, arg, ...) \
198198
fm_foreach_arg_join(fm_comma, macro, arg, __VA_ARGS__)
199199

200200
#define fm_foreach_tuple_join(join, macro, ...) \
201-
fm_foreach_tuple_join_(fm_empty, join, macro, __VA_ARGS__)
202-
#define fm_foreach_tuple_join_(join1, join2, macro, ...) \
203-
fm_cat(fm_foreach_tuple_join_, fm_va_01n(__VA_ARGS__))(join1, join2, macro, __VA_ARGS__)
201+
fm_cat(fm_foreach_tuple_join_, fm_va_01n(__VA_ARGS__))(fm_empty, join, macro, __VA_ARGS__)
204202
#define fm_foreach_tuple_join_0(join1, join2, macro, ...)
205203
#define fm_foreach_tuple_join_1(join1, join2, macro, x) \
206204
join1() macro x
207205
#define fm_foreach_tuple_join_n(join1, join2, macro, x, y, ...) \
208206
join1() macro x \
209207
join2() macro y \
210-
fm_recurs2(fm_, foreach_tuple_join_) (join2, join2, macro, __VA_ARGS__)
208+
fm_recurs2(fm_foreach_tuple_join_, fm_va_01n(__VA_ARGS__))(join2, join2, macro, __VA_ARGS__)
211209

212210
#define fm_foreach_tuple(macro, ...) \
213211
fm_foreach_tuple_join(fm_empty, macro, __VA_ARGS__)
214212
#define fm_foreach_tuple_comma(macro, ...) \
215213
fm_foreach_tuple_join(fm_comma, macro, __VA_ARGS__)
216214

217215
#define fm_foreach_tuple_arg_join(join, macro, arg, ...) \
218-
fm_foreach_tuple_arg_join_(fm_empty, join, macro, arg, __VA_ARGS__)
219-
#define fm_foreach_tuple_arg_join_(join1, join2, macro, arg, ...) \
220-
fm_cat(fm_foreach_tuple_arg_join_, fm_va_01n(__VA_ARGS__))(join1, join2, macro, arg, __VA_ARGS__)
216+
fm_cat(fm_foreach_tuple_arg_join_, fm_va_01n(__VA_ARGS__))(fm_empty, join, macro, arg, __VA_ARGS__)
221217
#define fm_foreach_tuple_arg_join_0(join1, join2, macro, ...)
222218
#define fm_foreach_tuple_arg_join_1(join1, join2, macro, arg, x) \
223219
join1() fm_apply(macro, arg, fm_expand x)
224220
#define fm_foreach_tuple_arg_join_n(join1, join2, macro, arg, x, y, ...) \
225221
join1() fm_apply(macro, arg, fm_expand x) \
226222
join2() fm_apply(macro, arg, fm_expand y) \
227-
fm_recurs2(fm_, foreach_tuple_arg_join_) (join2, join2, macro, arg, __VA_ARGS__)
223+
fm_recurs2(fm_foreach_tuple_arg_join_, fm_va_01n(__VA_ARGS__))(join1, join2, macro, arg, __VA_ARGS__)
228224

229225
#define fm_foreach_tuple_arg(macro, arg, ...) \
230226
fm_foreach_tuple_arg_join(fm_empty, macro, arg, __VA_ARGS__)

src/fu_util/fo_obj.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
typedef void* fobj_t;
88

99
#include <ft_util.h>
10-
#include <ft_ar_examples.h>
1110

1211
/*
1312
* Pointer to "object*.

src/fu_util/ft_array.inc.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* vim: set expandtab autoindent cindent ts=4 sw=4 sts=4 */
2-
#include <ft_util.h>
2+
#ifndef FU_UTIL_H
3+
#error "ft_util.h should be included"
4+
#endif
35

46
/*
57
* Accepts 2 macroses:
@@ -176,7 +178,12 @@
176178
#define ft_array_walk fm_cat(ft_array_pref, _walk)
177179
#define ft_array_walk_r fm_cat(ft_array_pref, _walk_r)
178180

179-
#define HUGE_SIZE ((uint64_t)UINT_MAX << 16)
181+
#if __SIZEOF_SIZE_T__ < 8
182+
#define HUGE_SIZE ((size_t)UINT_MAX >> 2)
183+
#else
184+
#define HUGE_SIZE ((size_t)UINT_MAX << 16)
185+
#endif
186+
180187
#ifndef NDEBUG
181188
/* try to catch uninitialized vars */
182189
#define ft_slice_invariants(slc) \

src/fu_util/ft_search.inc.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/* vim: set expandtab autoindent cindent ts=4 sw=4 sts=4 */
2+
#ifndef FU_UTIL_H
3+
#error "ft_util.h should be included"
4+
#endif
5+
16
/*
27
* Sort template.
38
* Accepts four macrosses:
@@ -39,8 +44,6 @@
3944
*
4045
*/
4146

42-
#include <ft_util.h>
43-
4447
#define ft_func_bsearch fm_cat(ft_bsearch_, FT_SEARCH)
4548
#define ft_func_bsearch_r fm_cat3(ft_bsearch_, FT_SEARCH, _r)
4649
#define ft_func_search fm_cat(ft_search_, FT_SEARCH)

src/fu_util/ft_util.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
/* vim: set expandtab autoindent cindent ts=4 sw=4 sts=4 */
22
#ifndef FU_UTIL_H
3-
#define FU_UTIL_H
3+
#define FU_UTIL_H 1
44

55
#include <stdlib.h>
66
#include <stdint.h>
7+
#include <string.h>
78
#include <inttypes.h>
89
#include <stdbool.h>
910
#include <stddef.h>
11+
#include <stdarg.h>
12+
#include <sys/types.h>
1013
/* trick to find ssize_t even on windows and strict ansi mode */
1114
#if defined(_MSC_VER)
1215
#include <BaseTsd.h>
1316
typedef SSIZE_T ssize_t;
14-
#else
15-
#include <sys/types.h>
17+
#define SSIZE_MAX ((ssize_t)((SIZE_MAX) >> 1))
18+
19+
#if !defined(WIN32) && defined(_WIN32)
20+
#define WIN32 _WIN32
21+
#endif
22+
1623
#endif
1724
#include <memory.h>
1825
#include <limits.h>
1926
#include <fm_util.h>
20-
#include <stdarg.h>
21-
2227

2328
#ifdef __GNUC__
2429
#define ft_gcc_const __attribute__((const))
@@ -103,6 +108,7 @@ typedef void ft_gnu_printf(4, 0) (*ft_log_hook_t)(enum FT_LOG_LEVEL,
103108
/*
104109
* Initialize logging in main executable file.
105110
* Pass custom hook or NULL.
111+
* In MinGW if built with libbacktrace, pass executable path (argv[0]).
106112
*/
107113
#define ft_init_log(hook) ft__init_log(hook, __FILE__)
108114

@@ -135,7 +141,7 @@ const char* ft__truncate_log_filename(const char *file);
135141

136142
#define ft_dbg_enabled() ft__dbg_enabled()
137143
#define ft_dbg_assert(x, ...) ft__dbg_assert(x, #x, __VA_ARGS__)
138-
#define ft_assert(x, ...) ft__assert(x, #x, __VA_ARGS__)
144+
#define ft_assert(x, ...) ft__assert(x, #x, ##__VA_ARGS__)
139145
#define ft_assyscall(syscall, ...) ft__assyscall(syscall, fm_uniq(res), __VA_ARGS__)
140146

141147
/* threadsafe strerror */
@@ -305,13 +311,14 @@ typedef struct ft_bytes_t {
305311
} ft_bytes_t;
306312

307313
ft_inline ft_bytes_t ft_bytes(void* ptr, size_t len) {
308-
return (ft_bytes_t){.ptr = ptr, .len = len};
314+
return (ft_bytes_t){.ptr = (char*)ptr, .len = len};
309315
}
310316

311317
ft_inline void ft_bytes_consume(ft_bytes_t *bytes, size_t cut);
312318
ft_inline void ft_bytes_move(ft_bytes_t *dest, ft_bytes_t *src);
313319

314320
// String utils
321+
extern size_t ft_strlcpy(char *dest, const char* src, size_t dest_size);
315322
/*
316323
* Concat strings regarding destination buffer size.
317324
* Note: if dest already full and doesn't contain \0n character, then fatal log is issued.
@@ -411,7 +418,7 @@ extern bool ft_strbuf_vcatf (ft_strbuf_t *buf, const char *fmt, va_list
411418
* Use it if format string comes from user.
412419
*/
413420
ft_gnu_printf(3, 0)
414-
extern bool ft_strbuf_vcatf_err (ft_strbuf_t *buf, bool err[static 1],
421+
extern bool ft_strbuf_vcatf_err (ft_strbuf_t *buf, bool err[1],
415422
const char *fmt, va_list args);
416423
/*
417424
* Returns string which points into the buffer.

0 commit comments

Comments
 (0)