Skip to content

Commit 9e9e129

Browse files
committed
CMake fixes and standardisation
1 parent 26b7278 commit 9e9e129

9 files changed

Lines changed: 81 additions & 72 deletions

‎CMakeLists.txt‎

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
cmake_minimum_required(VERSION 3.11...3.28)
22
project(c89stringutils VERSION 0.0.2 LANGUAGES C)
33

4+
string(TOUPPER "${PROJECT_NAME}" PROJECT_PREFIX)
5+
string(REPLACE "-" "_" PROJECT_PREFIX "${PROJECT_PREFIX}")
6+
47
set(CMAKE_C_VISIBILITY_PRESET hidden)
58
set(CMAKE_VISIBILITY_INLINES_HIDDEN YES)
69
set(CMAKE_C_STANDARD 90)
@@ -25,7 +28,7 @@ if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.15")
2528
target_compile_options(
2629
"${PROJECT_NAME}_compiler_flags"
2730
INTERFACE
28-
"$<$<AND:${gcc_like},$<CONFIG:Debug>>:$<BUILD_INTERFACE:-Wshadow;-Wformat=2;-Wall;-Wno-missing-braces;-Wno-long-long;-pedantic;>>"
31+
"$<$<AND:${gcc_like},$<CONFIG:Debug>>:$<BUILD_INTERFACE:-Wshadow;-Wformat=2;-Wall;-pedantic;>>"
2932
"$<${msvc}:$<BUILD_INTERFACE:-W4;-WX;-Zi;-permissive->>"
3033
)
3134
target_link_options(
@@ -268,7 +271,6 @@ check_c_source_compiles("
268271

269272
check_c_source_compiles("
270273
#pragma GCC diagnostic push
271-
#pragma GCC diagnostic ignored \"-Wunused-parameter\"
272274
int main(void) { return 0; }
273275
" C89STRINGUTILS_HAVE_PRAGMA_GCC_DIAGNOSTIC)
274276

@@ -301,47 +303,47 @@ configure_file(
301303
add_subdirectory("${PROJECT_NAME}")
302304

303305
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
304-
option(BUILD_TESTING_${PROJECT_NAME} "Build the tests" ON)
306+
option(BUILD_TESTING_${PROJECT_PREFIX} "Build the tests" ON)
305307
else()
306-
option(BUILD_TESTING_${PROJECT_NAME} "Build the tests" OFF)
308+
option(BUILD_TESTING_${PROJECT_PREFIX} "Build the tests" OFF)
307309
endif()
308310

309-
set(CDD_CHARSET "Unicode" CACHE STRING "Charset to use: Unicode or ANSI")
310-
set(CDD_THREADING "Multi-threaded" CACHE STRING "Threading: Multi-threaded or Single-threaded")
311-
set(CDD_MSVC_RTC "" CACHE STRING "MSVC Runtime Checks: RTC1, RTCs, RTCu, or empty")
312-
set(CDD_LTO OFF CACHE BOOL "Enable Link-Time Optimization (LTO)")
313-
set(CDD_DEPS "System" CACHE STRING "Dependency resolution strategy")
311+
set(${PROJECT_PREFIX}_CHARSET "Unicode" CACHE STRING "Charset to use: Unicode or ANSI")
312+
set(${PROJECT_PREFIX}_THREADING "Multi-threaded" CACHE STRING "Threading: Multi-threaded or Single-threaded")
313+
set(${PROJECT_PREFIX}_MSVC_RTC "" CACHE STRING "MSVC Runtime Checks: RTC1, RTCs, RTCu, or empty")
314+
set(${PROJECT_PREFIX}_LTO OFF CACHE BOOL "Enable Link-Time Optimization (LTO)")
315+
set(${PROJECT_PREFIX}_DEPS "System" CACHE STRING "Dependency resolution strategy")
314316

315-
set(CDD_CRT_LINKAGE "Shared" CACHE STRING "MSVC CRT Linkage: Static or Shared")
317+
set(${PROJECT_PREFIX}_CRT_LINKAGE "Shared" CACHE STRING "MSVC CRT Linkage: Static or Shared")
316318
if (MSVC)
317-
if (CDD_CRT_LINKAGE STREQUAL "Static")
319+
if (${PROJECT_PREFIX}_CRT_LINKAGE STREQUAL "Static")
318320
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
319321
else()
320322
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
321323
endif()
322324
endif()
323325

324-
325-
if (CDD_CHARSET STREQUAL "Unicode")
326+
if (${PROJECT_PREFIX}_CHARSET STREQUAL "Unicode")
326327
add_compile_definitions(UNICODE _UNICODE)
327328
endif()
328-
if (MSVC AND CDD_MSVC_RTC)
329-
add_compile_options("/${CDD_MSVC_RTC}")
329+
if (MSVC AND ${PROJECT_PREFIX}_MSVC_RTC)
330+
add_compile_options("/${${PROJECT_PREFIX}_MSVC_RTC}")
330331
endif()
331-
if (CDD_LTO)
332+
if (${PROJECT_PREFIX}_LTO)
332333
include(CheckIPOSupported)
333334
check_ipo_supported(RESULT ipo_supported)
334335
if(ipo_supported)
335336
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
336337
endif()
337338
endif()
338339

339-
if (BUILD_TESTING_${PROJECT_NAME})
340+
if (BUILD_TESTING_${PROJECT_PREFIX})
341+
enable_testing()
340342
include(CTest)
341343
if (BUILD_TESTING)
342344
add_subdirectory("${PROJECT_NAME}/tests")
343-
endif (BUILD_TESTING)
344-
endif (BUILD_TESTING_${PROJECT_NAME})
345+
endif()
346+
endif()
345347

346348
###########
347349
# Install #

‎c89stringutils/c89stringutils_safecrt.c‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
/* clang-format on */
1212

1313
#if defined(_MSC_VER)
14-
#pragma warning(push)
15-
#pragma warning(disable : 4996) /* disable secure warnings for fallbacks */
1614
#endif
1715

1816
#ifdef C89STRINGUTILS_TEST_MOCKS
@@ -154,6 +152,8 @@ static int minimal_vsscanf(const char *buffer, const char *format,
154152
* @param args The va_list of arguments.
155153
* @return The number of items scanned, or -1 on error.
156154
*/
155+
static int minimal_vfscanf(FILE *stream, const char *format, va_list args)
156+
C89STRINGUTILS_FORMAT_SCANF(2, 0);
157157
static int minimal_vfscanf(FILE *stream, const char *format, va_list args) {
158158
int count = 0;
159159
const char *p = format;
@@ -209,14 +209,10 @@ static int minimal_vfscanf(FILE *stream, const char *format, va_list args) {
209209
strncpy(token, start, len);
210210
token[len] = '\0';
211211
#if defined(__GNUC__) || defined(__clang__)
212-
#pragma GCC diagnostic push
213-
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
214-
#pragma GCC diagnostic ignored "-Wformat-security"
215212
#endif
216213
if (fscanf(stream, token) < 0)
217214
return count;
218215
#if defined(__GNUC__) || defined(__clang__)
219-
#pragma GCC diagnostic pop
220216
#endif
221217
continue;
222218
}
@@ -307,12 +303,9 @@ C89STRINGUTILS_EXPORT int c89stringutils_sscanf_s(const char *buffer,
307303
*/
308304
#if defined(C89STRINGUTILS_HAVE_VSSCANF)
309305
#if defined(__clang__) || defined(__GNUC__)
310-
#pragma GCC diagnostic push
311-
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
312306
#endif
313307
ret = vsscanf(buffer, format, args);
314308
#if defined(__clang__) || defined(__GNUC__)
315-
#pragma GCC diagnostic pop
316309
#endif
317310
#else
318311
ret = minimal_vsscanf(buffer, format, args);
@@ -337,12 +330,9 @@ C89STRINGUTILS_EXPORT int c89stringutils_fscanf_s(FILE *stream,
337330
#else
338331
#if defined(C89STRINGUTILS_HAVE_VFSCANF)
339332
#if defined(__clang__) || defined(__GNUC__)
340-
#pragma GCC diagnostic push
341-
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
342333
#endif
343334
ret = vfscanf(stream, format, args);
344335
#if defined(__clang__) || defined(__GNUC__)
345-
#pragma GCC diagnostic pop
346336
#endif
347337
#else
348338
ret = minimal_vfscanf(stream, format, args);

‎c89stringutils/c89stringutils_safecrt.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ c89stringutils_invoke_constraint_handler_s(const char *msg, void *ptr,
8686
* @return The number of items scanned.
8787
*/
8888
C89STRINGUTILS_EXPORT int c89stringutils_sscanf_s(const char *buffer,
89-
const char *format, ...);
89+
const char *format, ...)
90+
C89STRINGUTILS_FORMAT_SCANF(2, 3);
9091

9192
/**
9293
* @brief Safe fscanf wrapper.
@@ -96,7 +97,8 @@ C89STRINGUTILS_EXPORT int c89stringutils_sscanf_s(const char *buffer,
9697
* @return The number of items scanned.
9798
*/
9899
C89STRINGUTILS_EXPORT int c89stringutils_fscanf_s(FILE *stream,
99-
const char *format, ...);
100+
const char *format, ...)
101+
C89STRINGUTILS_FORMAT_SCANF(2, 3);
100102

101103
/**
102104
* @brief Safe fopen wrapper.

‎c89stringutils/c89stringutils_string_extras.c‎

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ extern void *mock_reallocarray(void *ptr, size_t nmemb, size_t size);
144144
/** @brief Mock macro for strerror */
145145
#define strerror mock_strerror
146146
/** @brief Mock macro for vsnprintf */
147+
#undef vsnprintf
147148
#define vsnprintf mock_vsnprintf
148149
/** @brief Mock macro for _vsnprintf */
149150
#define _vsnprintf mock_vsnprintf
@@ -152,6 +153,7 @@ extern void *mock_reallocarray(void *ptr, size_t nmemb, size_t size);
152153
/** @brief Mock macro for _vsnprintf_s */
153154
#define _vsnprintf_s mock_vsnprintf_s
154155
/** @brief Mock macro for vsprintf */
156+
#undef vsprintf
155157
#define vsprintf mock_vsprintf
156158
/** @brief Mock macro for vfprintf */
157159
#define vfprintf mock_vfprintf
@@ -160,21 +162,8 @@ extern void *mock_reallocarray(void *ptr, size_t nmemb, size_t size);
160162
#endif
161163

162164
#if defined(_MSC_VER) && _MSC_VER >= 1300
163-
#pragma warning(push)
164-
#pragma warning(disable : 4127) /* conditional expression is constant */
165-
#pragma warning(disable : 4100) /* unreferenced formal parameter */
166165
#elif defined(__clang__)
167-
#pragma clang diagnostic push
168-
#if defined(__has_warning)
169-
#if __has_warning("-Wnonnull-compare")
170-
#pragma clang diagnostic ignored "-Wnonnull-compare"
171-
#endif
172-
#endif
173-
#pragma clang diagnostic ignored "-Wunused-parameter"
174166
#elif defined(__GNUC__) && __GNUC__ >= 7
175-
#pragma GCC diagnostic push
176-
#pragma GCC diagnostic ignored "-Wnonnull-compare"
177-
#pragma GCC diagnostic ignored "-Wunused-parameter"
178167
#elif defined(__SUNPRO_C)
179168
#pragma error_messages(off, E_STATEMENT_NOT_REACHED)
180169
#endif
@@ -249,14 +238,10 @@ C89STRINGUTILS_EXPORT void c89stringutils_log_debug(const char *fmt, ...) {
249238
* if available */
250239
#undef STB_SPRINTF_NOFLOAT
251240
#if defined(_MSC_VER)
252-
#pragma warning(push)
253-
#pragma warning( \
254-
disable : 4244 4702) /* conversion from int to char, unreachable code */
255241
#endif
256242
#include "stb_sprintf.h"
257243
/* clang-format on */
258244
#if defined(_MSC_VER) && _MSC_VER >= 1300
259-
#pragma warning(pop)
260245
#endif
261246
#define C89STRINGUTILS_USE_STB_SPRINTF 1
262247
#endif
@@ -271,9 +256,9 @@ C89STRINGUTILS_EXPORT void c89stringutils_log_debug(const char *fmt, ...) {
271256
* @return The number of characters written, or a negative value on error.
272257
*/
273258
#if defined(_MSC_VER)
274-
#pragma warning(push)
275-
#pragma warning(disable : 4996)
276259
#endif
260+
static int fallback_vsnprintf(char *buffer, size_t count, const char *format,
261+
va_list args) C89STRINGUTILS_FORMAT_PRINTF(3, 0);
277262
static int fallback_vsnprintf(char *buffer, size_t count, const char *format,
278263
va_list args) {
279264
#ifdef C89STRINGUTILS_TEST_MOCKS
@@ -334,7 +319,6 @@ static int fallback_vsnprintf(char *buffer, size_t count, const char *format,
334319
#endif
335320
}
336321
#if defined(_MSC_VER)
337-
#pragma warning(pop)
338322
#endif
339323

340324
#if !defined(C89STRINGUTILS_HAVE_STRERROR_S)
@@ -638,8 +622,6 @@ C89STRINGUTILS_EXPORT int c89stringutils_vsnprintf_s(char *s, rsize_t n,
638622
#endif
639623

640624
#if defined(_MSC_VER)
641-
#pragma warning(push)
642-
#pragma warning(disable : 4996) /* disable secure warnings for fallbacks */
643625
#endif
644626
C89STRINGUTILS_EXPORT int
645627
c89stringutils_vsnprintf(char *s, size_t n, const char *format, va_list arg) {
@@ -655,7 +637,6 @@ c89stringutils_vsnprintf(char *s, size_t n, const char *format, va_list arg) {
655637
#endif
656638
}
657639
#if defined(_MSC_VER)
658-
#pragma warning(pop)
659640
#endif
660641

661642
#if !defined(C89STRINGUTILS_HAVE_SNPRINTF_S) || defined(_WIN32)
@@ -1180,11 +1161,8 @@ C89STRINGUTILS_EXPORT int c89stringutils_jasprintf(char **unto, const char *fmt,
11801161
}
11811162

11821163
#if defined(_MSC_VER) && _MSC_VER >= 1300
1183-
#pragma warning(pop)
11841164
#elif defined(__clang__)
1185-
#pragma clang diagnostic pop
11861165
#elif defined(C89STRINGUTILS_HAVE_PRAGMA_GCC_DIAGNOSTIC)
1187-
#pragma GCC diagnostic pop
11881166
#endif
11891167

11901168
#if !defined(C89STRINGUTILS_HAVE_VASPRINTF) || \

‎c89stringutils/c89stringutils_string_extras.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,11 @@ typedef unsigned long c89stringutils_uint64_t;
234234
#if defined(C89STRINGUTILS_HAVE_ATTR_FORMAT)
235235
#define C89STRINGUTILS_FORMAT_PRINTF(fmt_idx, arg_idx) \
236236
__attribute__((format(printf, fmt_idx, arg_idx)))
237+
#define C89STRINGUTILS_FORMAT_SCANF(fmt_idx, arg_idx) \
238+
__attribute__((format(scanf, fmt_idx, arg_idx)))
237239
#else
238240
#define C89STRINGUTILS_FORMAT_PRINTF(fmt_idx, arg_idx)
241+
#define C89STRINGUTILS_FORMAT_SCANF(fmt_idx, arg_idx)
239242
#endif
240243

241244
#if defined(C89STRINGUTILS_HAVE_ATTR_NONNULL)

‎c89stringutils/stb_sprintf.h‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ extern "C" {
33
#endif /* __cplusplus */
44
/* clang-format off */
55
#if defined(__GNUC__) || defined(__clang__)
6-
#pragma GCC diagnostic push
7-
#pragma GCC diagnostic ignored "-Wlong-long"
8-
#pragma GCC diagnostic ignored "-Wsign-compare"
96
#endif
107

118
/* stb_sprintf - v1.10 - public domain snprintf() implementation */
@@ -1920,7 +1917,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19201917
------------------------------------------------------------------------------
19211918
*/
19221919
#if defined(__GNUC__) || defined(__clang__)
1923-
#pragma GCC diagnostic pop
19241920
#endif
19251921
/* clang-format on */
19261922

‎c89stringutils/tests/CMakeLists.txt‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,23 @@ file(MAKE_DIRECTORY "${DOWNLOAD_DIR}")
1010

1111
if (NOT CMAKE_SYSTEM_NAME STREQUAL "SunOS")
1212
include(FetchContent)
13-
FetchContent_Declare(
14-
greatest
15-
URL "https://raw.githubusercontent.com/SamuelMarks/greatest/master/greatest.h"
16-
DOWNLOAD_NO_EXTRACT TRUE
17-
)
18-
FetchContent_MakeAvailable(greatest)
19-
file(COPY "${greatest_SOURCE_DIR}/greatest.h" DESTINATION "${DOWNLOAD_DIR}")
13+
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../../../greatest")
14+
message(STATUS "Fetching greatest from local directory")
15+
FetchContent_Declare(
16+
greatest
17+
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../greatest"
18+
)
19+
FetchContent_MakeAvailable(greatest)
20+
file(COPY "${greatest_SOURCE_DIR}/greatest.h" DESTINATION "${DOWNLOAD_DIR}")
21+
else()
22+
FetchContent_Declare(
23+
greatest
24+
URL "https://raw.githubusercontent.com/SamuelMarks/greatest/master/greatest.h"
25+
DOWNLOAD_NO_EXTRACT TRUE
26+
)
27+
FetchContent_MakeAvailable(greatest)
28+
file(COPY "${greatest_SOURCE_DIR}/greatest.h" DESTINATION "${DOWNLOAD_DIR}")
29+
endif()
2030
endif (NOT CMAKE_SYSTEM_NAME STREQUAL "SunOS")
2131

2232
#########
@@ -47,7 +57,7 @@ target_include_directories(
4757

4858
add_test(NAME "${EXEC_NAME}" COMMAND "${EXEC_NAME}")
4959

50-
if (C89STRINGUTILS_BUILD_AMALGAMATION)
60+
if (${PROJECT_PREFIX}_BUILD_AMALGAMATION)
5161
add_executable("test_amalg" "${CMAKE_CURRENT_SOURCE_DIR}/test_amalg.c")
5262
target_include_directories("test_amalg" PRIVATE "${PROJECT_BINARY_DIR}")
5363
add_test(NAME "test_amalg" COMMAND "test_amalg")

‎c89stringutils/tests/test.c‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* @brief Main entry point for greatest tests.
44
*/
55
#if defined(_MSC_VER)
6-
#pragma warning(disable : 4127) /* conditional expression is constant */
76
#ifndef _CRT_SECURE_NO_WARNINGS
87
#define _CRT_SECURE_NO_WARNINGS
98
#endif
@@ -113,6 +112,29 @@ static int do_mock_vsnprintf(size_t size, int ret) {
113112
* @param ap the va_list
114113
* @return the written length
115114
*/
115+
int mock_vsnprintf(char *str, size_t size, const char *format, va_list ap)
116+
C89STRINGUTILS_FORMAT_PRINTF(3, 0);
117+
int mock_vscprintf(const char *format, va_list ap)
118+
C89STRINGUTILS_FORMAT_PRINTF(1, 0);
119+
int mock_vsnprintf_s(char *buffer, size_t sizeOfBuffer, size_t count,
120+
const char *format, va_list ap)
121+
C89STRINGUTILS_FORMAT_PRINTF(4, 0);
122+
int mock_vsprintf(char *buffer, const char *format, va_list ap)
123+
C89STRINGUTILS_FORMAT_PRINTF(2, 0);
124+
int mock_vfprintf(FILE *stream, const char *format, va_list ap)
125+
C89STRINGUTILS_FORMAT_PRINTF(2, 0);
126+
int mock_fprintf(FILE *stream, const char *format, ...)
127+
C89STRINGUTILS_FORMAT_PRINTF(2, 3);
128+
int mock_vfprintf_s(FILE *stream, const char *format, va_list ap)
129+
C89STRINGUTILS_FORMAT_PRINTF(2, 0);
130+
int mock_fprintf_s(FILE *stream, const char *format, ...)
131+
C89STRINGUTILS_FORMAT_PRINTF(2, 3);
132+
133+
#undef vsnprintf
134+
#undef vsprintf
135+
#undef vfprintf
136+
#undef vscprintf
137+
116138
int mock_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
117139
int ret = -1;
118140
if (str == NULL) {

0 commit comments

Comments
 (0)