Skip to content

Commit 6383d17

Browse files
committed
[acquire/acquire_{crc32c,librhash,string_extras}.h] Refactor to one LIBACQUIRE_IMPLEMENTATION #ifdef per header ; [cmake/generate_header_and_source_from_header_only.cmake] Fix implementation to work consistently across codebase (no matter if multiline comment on #endif, or #ifdef or `#if <unrelated stuff \
defined(header_guard)` syntax used ; [acquire/CMakeLists.txt] Cleanup ordering and inclusion of associated header in generated code ; [acquire/tests/CMakeLists.txt] Minor cleanup in `Header_Files` to use relative paths ; comment out `"${CHECKSUM_LIBRARIES}"` library linkage
1 parent 37efc07 commit 6383d17

File tree

6 files changed

+155
-189
lines changed

6 files changed

+155
-189
lines changed

acquire/CMakeLists.txt

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,30 @@ else ()
9191
)
9292

9393
if (USE_LIBCURL)
94-
list(APPEND header_impls "acquire_libcurl.h")
94+
set(NETWORK_LIB "acquire_libcurl.h")
9595
elseif (USE_LIBFETCH)
96-
list(APPEND header_impls "acquire_libfetch.h")
96+
set(NETWORK_LIB "acquire_libfetch.h")
9797
elseif (USE_WININET)
98-
list(APPEND header_impls "acquire_wininet.h")
98+
set(NETWORK_LIB "acquire_wininet.h")
9999
elseif (USE_OPENBSD_FTP)
100-
list(APPEND header_impls "acquire_openbsd_ftp.h")
100+
set(NETWORK_LIB "acquire_openbsd_ftp.h")
101101
endif ()
102+
list(APPEND header_impls "${NETWORK_LIB}")
102103

103104

104105
foreach (header_file IN LISTS header_impls)
105106
get_filename_component(name_no_ext "${header_file}" NAME_WE)
106107
set(gen_header_file "${CMAKE_BINARY_DIR}/gen/gen_${name_no_ext}.h")
107108
set(gen_source_file "${CMAKE_BINARY_DIR}/gen/gen_${name_no_ext}.c")
108109

110+
set(associated_header "")
111+
if(header_file STREQUAL NETWORK_LIB)
112+
set(associated_header "gen_acquire_download.h")
113+
endif (header_file STREQUAL NETWORK_LIB)
114+
109115
generate_header_and_source_from_header_only(
110116
"LIBACQUIRE_IMPLEMENTATION"
117+
"${associated_header}"
111118
"${CMAKE_CURRENT_SOURCE_DIR}/${header_file}"
112119
"${gen_header_file}"
113120
"${gen_source_file}"
@@ -116,23 +123,20 @@ else ()
116123
if (EXISTS "${gen_header_file}")
117124
list(APPEND gen_header_files "${gen_header_file}")
118125
string(REGEX REPLACE "^${CMAKE_BINARY_DIR}." "" gen_header_file_rel "${gen_header_file}")
119-
message(STATUS "Generated header: ${gen_header_file_rel}")
126+
message(STATUS "Generated header: ${gen_header_file_rel}")
120127
else ()
121-
message(WARNING "Missing generated header: ${gen_header_file}")
128+
message(WARNING "Missing generated header: ${gen_header_file}")
122129
endif ()
123130

124131
if (EXISTS "${gen_source_file}")
125132
list(APPEND gen_source_files "${gen_source_file}")
126133
string(REGEX REPLACE "^${CMAKE_BINARY_DIR}." "" gen_source_file_rel "${gen_source_file}")
127-
message(STATUS "Generated source: ${gen_source_file_rel}")
134+
message(STATUS "Generated source: ${gen_source_file_rel}")
128135
else ()
129136
message(WARNING "Missing generated source: ${gen_source_file_rel}")
130137
endif ()
131138
endforeach (header_file IN LISTS header_impls)
132139

133-
# Implement the internal implementation library as STATIC
134-
add_library("${LIBRARY_NAME}_impl" STATIC ${gen_header_files} ${gen_source_files})
135-
136140
# Set compile definitions per source file to avoid multiple definitions
137141
foreach (src IN LISTS gen_source_files)
138142
if (src MATCHES "/gen_acquire_librhash.c$")
@@ -181,6 +185,9 @@ else ()
181185
endif ()
182186
endforeach (src IN LISTS gen_source_files)
183187

188+
# Implement the internal implementation library as STATIC
189+
add_library("${LIBRARY_NAME}_impl" STATIC ${gen_header_files} ${gen_source_files})
190+
184191
if (DEFINED CHECKSUM_LIBRARIES)
185192
message(STATUS "[libacquire] Using var ${CHECKSUM_LIBRARIES} for checksums")
186193
target_link_libraries("${LIBRARY_NAME}_impl" PUBLIC "${CHECKSUM_LIBRARIES}")

acquire/acquire_crc32c.h

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ extern "C" {
1919

2020
#include <stdio.h>
2121

22+
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
23+
#else
24+
#include <unistd.h>
25+
#endif /* defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || \
26+
defined(__NT__) */
27+
2228
/**
2329
* @brief Verify the CRC32C checksum of a given file against an expected hash
2430
* string.
@@ -43,17 +49,9 @@ extern bool crc32c(const char *filename, const char *hash);
4349
*
4450
* Alternatives are to use `zlib`'s CRC32C impl conditionally or RHash or miniz
4551
* */
46-
52+
#ifndef CHUNK_SIZE
4753
#define CHUNK_SIZE 4096
48-
49-
#include <stdio.h>
50-
51-
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
52-
53-
#else
54-
#include <unistd.h>
55-
#endif /* defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || \
56-
defined(__NT__) */
54+
#endif /* !CHUNK_SIZE */
5755

5856
#ifndef O_BINARY
5957
#define O_BINARY 0
@@ -108,6 +106,10 @@ static const unsigned int crctable[256] = {
108106
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
109107
};
110108

109+
#if defined(LIBACQUIRE_IMPLEMENTATION) && \
110+
!defined(LIBACQUIRE_ACQUIRE_CRC32C_IMPL)
111+
#define LIBACQUIRE_ACQUIRE_CRC32C_IMPL
112+
111113
unsigned int crc32_algo(unsigned int iv, unsigned char *buf, long long len) {
112114
unsigned int crc = iv ^ ~0;
113115
for (; len; len--)
@@ -120,19 +122,11 @@ unsigned int crc32_file(FILE *file) {
120122
unsigned crc = 0;
121123
size_t len;
122124
while ((len = fread(&buf[0], sizeof(char), CHUNK_SIZE, file)) > 0) {
123-
crc = crc32_algo(crc, buf, len);
125+
crc = crc32_algo(crc, buf, (long long)len);
124126
}
125127
return crc;
126128
}
127129

128-
#ifdef LIBACQUIRE_IMPLEMENTATION
129-
#ifndef LIBACQUIRE_ACQUIRE_CRC32C_IMPL
130-
#define LIBACQUIRE_ACQUIRE_CRC32C_IMPL 1
131-
#ifndef USE_CRC32C
132-
#define USE_CRC32C 1
133-
#endif /* !USE_CRC32C */
134-
#ifdef USE_CRC32C
135-
136130
/**
137131
* @brief Implementation of CRC32C checksum verification.
138132
*
@@ -176,9 +170,8 @@ bool crc32c(const char *filename, const char *hash) {
176170
return true;
177171
}
178172

179-
#endif /* USE_CRC32C */
180-
#endif /* !LIBACQUIRE_ACQUIRE_CRC32C_IMPL */
181-
#endif /* LIBACQUIRE_IMPLEMENTATION */
173+
#endif /* defined(LIBACQUIRE_IMPLEMENTATION) && \
174+
!defined(LIBACQUIRE_ACQUIRE_CRC32C_IMPL) */
182175

183176
#ifdef __cplusplus
184177
}

acquire/acquire_librhash.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
* librhash implementation of libacquire's checksum API
33
* */
44

5-
#if !defined(LIBACQUIRE_ACQUIRE_LIBRHASH_H) && \
6-
defined(LIBACQUIRE_IMPLEMENTATION) && defined(USE_LIBRHASH)
5+
#ifndef LIBACQUIRE_ACQUIRE_LIBRHASH_H
76
#define LIBACQUIRE_ACQUIRE_LIBRHASH_H
87

9-
#include <errno.h>
8+
#if defined(LIBACQUIRE_IMPLEMENTATION) && defined(USE_LIBRHASH)
109

1110
#ifdef __cplusplus
1211
extern "C" {
@@ -16,6 +15,8 @@ extern "C" {
1615
#include "acquire_stdbool.h"
1716
#endif /* __cplusplus */
1817

18+
#include <errno.h>
19+
1920
#include <rhash.h>
2021

2122
#include <acquire_string_extras.h>
@@ -112,5 +113,6 @@ HASHES
112113
}
113114
#endif /* __cplusplus */
114115

115-
#endif /* !defined(LIBACQUIRE_ACQUIRE_LIBRHASH_H) && \
116-
defined(LIBACQUIRE_IMPLEMENTATION) && defined(USE_LIBRHASH) */
116+
#endif /* defined(LIBACQUIRE_IMPLEMENTATION) && defined(USE_LIBRHASH) */
117+
118+
#endif /* !LIBACQUIRE_ACQUIRE_LIBRHASH_H */

acquire/acquire_string_extras.h

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,48 @@ typedef int errno_t;
9898
#if defined(ANY_BSD) || defined(__APPLE__) && defined(__MACH__) || \
9999
defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
100100
#define HAVE_ASPRINTF
101+
#define HAVE_SNPRINTF
101102
#endif /* defined(ANY_BSD) || defined(__APPLE__) && defined(__MACH__) || \
102103
defined(_GNU_SOURCE) || defined(_BSD_SOURCE) */
103104

104105
#ifdef HAVE_STRINGS_H
105106
#include <strings.h>
106107
#endif /* HAVE_STRINGS_H */
107108

108-
#if !defined(HAVE_SNPRINTF_H) && defined(LIBACQUIRE_IMPLEMENTATION) && \
109-
!defined(SNPRINTF_IMPL)
109+
#ifndef HAVE_STRNCASECMP
110+
111+
extern LIBACQUIRE_EXPORT int strncasecmp(const char *, const char *, size_t);
112+
113+
extern LIBACQUIRE_EXPORT int strcasecmp(const char *, const char *);
114+
115+
#endif /* !HAVE_STRNCASECMP */
116+
117+
#ifndef HAVE_STRNSTR
118+
119+
extern LIBACQUIRE_EXPORT char *strnstr(const char *, const char *, size_t);
120+
121+
#endif /* ! HAVE_STRNSTR */
122+
123+
#ifndef HAVE_STRCASESTR_H
124+
125+
extern LIBACQUIRE_EXPORT char *strcasestr(const char *, const char *);
126+
127+
#endif /* ! HAVE_STRCASESTR_H */
128+
129+
#ifndef HAVE_STRERRORLEN_S
130+
131+
extern size_t LIBACQUIRE_EXPORT strerrorlen_s(errno_t);
132+
133+
#endif /* !HAVE_STRERRORLEN_S */
134+
135+
#ifndef HAVE_SNPRINTF
136+
extern LIBACQUIRE_IMPLEMENTATION int snprintf(char *buffer, size_t count,
137+
const char *format, ...);
138+
#endif /* !HAVE_SNPRINTF */
139+
140+
#ifdef LIBACQUIRE_IMPLEMENTATION
141+
142+
#if !defined(HAVE_SNPRINTF) && !defined(SNPRINTF_IMPL)
110143
#define SNPRINTF_IMPL
111144

112145
/*
@@ -120,7 +153,7 @@ typedef int errno_t;
120153
* SPDX-License-Identifier: BSD-2-Clause
121154
*/
122155

123-
inline int snprintf(char *buffer, size_t count, const char *format, ...) {
156+
int snprintf(char *buffer, size_t count, const char *format, ...) {
124157
int result;
125158
va_list args;
126159
va_start(args, format);
@@ -149,16 +182,9 @@ inline double wtf_vsnprintf(char *buffer, size_t count, const char *format,
149182
#define vsnprintf(buffer, count, format, args) \
150183
wtf_vsnprintf(buffer, count, format, args)
151184

152-
#endif /* !defined(HAVE_SNPRINTF_H) && defined(LIBACQUIRE_IMPLEMENTATION) && \
153-
!defined(SNPRINTF_IMPL) */
154-
155-
#ifndef HAVE_STRNCASECMP
185+
#endif /* !defined(HAVE_SNPRINTF) && !defined(SNPRINTF_IMPL) */
156186

157-
extern LIBACQUIRE_EXPORT int strncasecmp(const char *, const char *, size_t);
158-
159-
extern LIBACQUIRE_EXPORT int strcasecmp(const char *, const char *);
160-
161-
#if defined(LIBACQUIRE_IMPLEMENTATION) && !defined(STRNCASECMP_IMPL)
187+
#if !defined(HAVE_STRNCASECMP) && !defined(STRNCASECMP_IMPL)
162188
#define STRNCASECMP_IMPL
163189

164190
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
@@ -178,15 +204,9 @@ int strncasecmp(const char *_l, const char *_r, size_t n) {
178204
}
179205
#endif /* defined(_MSC_VER) && !defined(__INTEL_COMPILER) */
180206

181-
#endif /* defined(LIBACQUIRE_IMPLEMENTATION) && !defined(STRNCASECMP_IMPL) */
182-
183-
#endif /* !HAVE_STRNCASECMP */
184-
185-
#ifndef HAVE_STRNSTR
186-
187-
extern LIBACQUIRE_EXPORT char *strnstr(const char *, const char *, size_t);
207+
#endif /* !defined(HAVE_STRNCASECMP) && !defined(STRNCASECMP_IMPL) */
188208

189-
#if defined(LIBACQUIRE_IMPLEMENTATION) && !defined(STRNSTR_IMPL)
209+
#if !defined(HAVE_STRNSTR) && !defined(STRNSTR_IMPL)
190210
#define STRNSTR_IMPL
191211
char *strnstr(const char *buffer, const char *target, size_t bufferLength) {
192212
/*
@@ -218,14 +238,9 @@ char *strnstr(const char *buffer, const char *target, size_t bufferLength) {
218238
}
219239
return 0;
220240
}
221-
#endif /* defined(LIBACQUIRE_IMPLEMENTATION) && !defined(STRNSTR_IMPL) */
222-
223-
#endif /* ! HAVE_STRNSTR */
241+
#endif /* !defined(HAVE_STRNSTR) && !defined(STRNSTR_IMPL) */
224242

225-
#ifndef HAVE_STRCASESTR_H
226-
extern LIBACQUIRE_EXPORT char *strcasestr(const char *, const char *);
227-
228-
#if defined(LIBACQUIRE_IMPLEMENTATION) && defined(STRCASESTR_IMPL)
243+
#ifdef STRCASESTR_IMPL
229244
/* `strcasestr` from MUSL */
230245

231246
char *strcasestr(const char *h, const char *n) {
@@ -236,15 +251,9 @@ char *strcasestr(const char *h, const char *n) {
236251
return 0;
237252
}
238253

239-
#endif /* defined(LIBACQUIRE_IMPLEMENTATION) && defined(STRCASESTR_IMPL) */
254+
#endif /* STRCASESTR_IMPL */
240255

241-
#endif /* ! HAVE_STRCASESTR_H */
242-
243-
#ifndef HAVE_STRERRORLEN_S
244-
245-
extern size_t LIBACQUIRE_EXPORT strerrorlen_s(errno_t);
246-
247-
#if defined(LIBACQUIRE_IMPLEMENTATION) && defined(STRERRORLEN_IMPL)
256+
#if !defined(HAVE_STRERRORLEN_S) && defined(STRERRORLEN_IMPL)
248257
/* MIT licensed function from Safe C Library */
249258

250259
size_t strerrorlen_s(errno_t errnum) {
@@ -289,9 +298,9 @@ size_t strerrorlen_s(errno_t errnum) {
289298
}
290299
}
291300

292-
#endif /* defined(LIBACQUIRE_IMPLEMENTATION) && defined(STRERRORLEN_IMPL) */
301+
#endif /* !defined(HAVE_STRERRORLEN_S) && defined(STRERRORLEN_IMPL) */
293302

294-
#endif /* !HAVE_STRERRORLEN_S */
303+
#endif /* ! LIBACQUIRE_IMPLEMENTATION */
295304

296305
#ifdef __cplusplus
297306
}

acquire/tests/CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ configure_file(config_for_tests.h.in "${PROJECT_BINARY_DIR}/src/config_for_tests
103103

104104
foreach (EXEC_NAME ${_targets})
105105
set(Header_Files
106-
"${CMAKE_SOURCE_DIR}/acquire/tests/test_checksum.h"
107-
"${CMAKE_SOURCE_DIR}/acquire/tests/test_download.h"
108-
"${CMAKE_SOURCE_DIR}/acquire/tests/test_fileutils.h"
109-
"${CMAKE_SOURCE_DIR}/acquire/tests/test_string_extras.h"
110-
"${CMAKE_SOURCE_DIR}/acquire/tests/test_url_utils.h"
106+
"test_checksum.h"
107+
"test_download.h"
108+
"test_fileutils.h"
109+
"test_string_extras.h"
110+
"test_url_utils.h"
111111
)
112112
source_group("Header Files" FILES "${Header_Files}")
113113

@@ -123,7 +123,7 @@ foreach (EXEC_NAME ${_targets})
123123
PRIVATE
124124
"${PROJECT_NAME}"
125125
"${PROJECT_NAME}_compiler_flags"
126-
"${CHECKSUM_LIBRARIES}"
126+
#"${CHECKSUM_LIBRARIES}"
127127
)
128128

129129
target_include_directories(

0 commit comments

Comments
 (0)