Skip to content

Commit 7d44ef7

Browse files
committed
Support for MSVC 2005
1 parent bbf0e2e commit 7d44ef7

File tree

9 files changed

+113
-24
lines changed

9 files changed

+113
-24
lines changed

CMakeLists.txt

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
1-
cmake_minimum_required(VERSION 3.12)
1+
cmake_minimum_required(VERSION 3.11)
22
project(c89stringutils VERSION 0.0.2 LANGUAGES C)
33

44
set(CMAKE_C_VISIBILITY_PRESET hidden)
55
set(CMAKE_VISIBILITY_INLINES_HIDDEN YES)
66
set(CMAKE_C_STANDARD 90)
77

88
add_library("${PROJECT_NAME}_compiler_flags" INTERFACE)
9-
target_compile_features("${PROJECT_NAME}_compiler_flags" INTERFACE "c_std_${CMAKE_C_STANDARD}")
10-
11-
# add compiler warning flags just when building this project via
12-
# the BUILD_INTERFACE genex
13-
set(gcc_like "$<COMPILE_LANG_AND_ID:C,CXX,ARMClang,AppleClang,Clang,GNU,LCC>")
14-
set(msvc "$<COMPILE_LANG_AND_ID:C,CXX,MSVC>")
15-
target_compile_options(
16-
"${PROJECT_NAME}_compiler_flags"
17-
INTERFACE
18-
"$<${gcc_like}:$<BUILD_INTERFACE:-Wshadow;-Wformat=2;-Wall;-pedantic>>"
19-
"$<${msvc}:$<BUILD_INTERFACE:-W3;-WX;-Zi;-permissive->>"
20-
)
21-
9+
message(STATUS "MSVC_VERSION = ${MSVC_VERSION}")
10+
if (NOT DEFINED MSVC_VERSION OR MSVC_VERSION STRGREATER "1900") # 2015
11+
target_compile_features("${PROJECT_NAME}_compiler_flags" INTERFACE "c_std_${CMAKE_C_STANDARD}")
12+
elseif (MSVC_VERSION STRLESS_EQUAL "1900" AND NOT EXISTS "${CMAKE_BINARY_DIR}/c89stringutils_export.h")
13+
file(COPY "${CMAKE_SOURCE_DIR}/c89stringutils/c89stringutils_export_pregen.h"
14+
DESTINATION "${CMAKE_BINARY_DIR}")
15+
file(RENAME "${CMAKE_BINARY_DIR}/c89stringutils_export_pregen.h"
16+
"${CMAKE_BINARY_DIR}/c89stringutils_export.h")
17+
endif ()
18+
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.15")
19+
# add compiler warning flags just when building this project via
20+
# the BUILD_INTERFACE genex
21+
set(gcc_like "$<COMPILE_LANG_AND_ID:C,CXX,ARMClang,AppleClang,Clang,GNU,LCC>")
22+
set(msvc "$<COMPILE_LANG_AND_ID:C,CXX,MSVC>")
23+
target_compile_options(
24+
"${PROJECT_NAME}_compiler_flags"
25+
INTERFACE
26+
"$<${gcc_like}:$<BUILD_INTERFACE:-Wshadow;-Wformat=2;-Wall;-pedantic>>"
27+
"$<${msvc}:$<BUILD_INTERFACE:-W3;-WX;-Zi;-permissive->>"
28+
)
29+
endif (CMAKE_VERSION VERSION_GREATER_EQUAL "3.15")
2230
# Set the build directories
23-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")
24-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
25-
31+
if (MSVC)
32+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
33+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
34+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
35+
else ()
36+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
37+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")
38+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
39+
endif()
2640
# configure a header file to pass the version number only
2741
configure_file(
2842
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/config.h.in"
@@ -44,7 +58,7 @@ include(GNUInstallDirs)
4458

4559
install(
4660
FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.h"
47-
TYPE "INCLUDE"
61+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
4862
)
4963
include(InstallRequiredSystemLibraries)
5064
set(CPACK_BUNDLE_NAME "${PROJECT_NAME}")

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Additionally `jasprintf`, a version of `asprintf` that concatenates on successiv
2929

3030
### Dependencies
3131

32-
- [CMake](https://cmake.org) (3.19 or later)
32+
- [CMake](https://cmake.org) (3.11 or later)
3333
- C compiler (any that work with CMake, and were released within the last 30 years)
3434

3535
### Build

c89stringutils/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ set_property(TARGET "${LIBRARY_NAME}" PROPERTY VERSION "${${PROJECT_NAME}_VERSIO
3939
set_property(TARGET "${LIBRARY_NAME}" PROPERTY SOVERSION "${${PROJECT_NAME}_VERSION_MAJOR}")
4040

4141
set(installable_libs # "${EXEC_NAME}"
42-
"${LIBRARY_NAME}" "${PROJECT_NAME}_compiler_flags")
42+
"${LIBRARY_NAME}")
4343
if (TARGET "${DEPENDANT_LIBRARY}")
4444
list(APPEND installable_libs "${DEPENDANT_LIBRARY}")
4545
endif ()
46+
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.19")
47+
list(APPEND installable_libs "${PROJECT_NAME}_compiler_flags")
48+
endif ()
4649
install(TARGETS ${installable_libs}
4750
EXPORT "${LIBRARY_NAME}Targets"
4851
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
@@ -51,4 +54,4 @@ install(TARGETS ${installable_libs}
5154
install(EXPORT "${LIBRARY_NAME}Targets" DESTINATION "${CMAKE_INSTALL_DATADIR}/${LIBRARY_NAME}")
5255

5356
install(FILES ${Header_Files} "${_export_file}"
54-
TYPE "INCLUDE")
57+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* for older targets and non CMake using build systems on MSVC */
2+
#ifndef C89STRINGUTILS_EXPORT_H
3+
#define C89STRINGUTILS_EXPORT_H
4+
5+
#ifdef C89STRINGUTILS_STATIC_DEFINE
6+
# define C89STRINGUTILS_EXPORT
7+
# define C89STRINGUTILS_NO_EXPORT
8+
#else
9+
# ifndef C89STRINGUTILS_EXPORT
10+
# ifdef c89stringutils_EXPORTS
11+
/* We are building this library */
12+
# if defined(BUILD_SHARED)
13+
# define C89STRINGUTILS_EXPORT __declspec(dllexport)
14+
# else
15+
# define C89STRINGUTILS_EXPORT
16+
# endif
17+
# endif
18+
# else
19+
/* We are using this library */
20+
# if defined(BUILD_SHARED)
21+
# define C89STRINGUTILS_EXPORT __declspec(dllimport)
22+
# else
23+
# define C89STRINGUTILS_EXPORT
24+
# endif
25+
# endif
26+
27+
# ifndef C89STRINGUTILS_NO_EXPORT
28+
# define C89STRINGUTILS_NO_EXPORT
29+
# endif
30+
#endif
31+
32+
#ifndef C89STRINGUTILS_DEPRECATED
33+
# if defined(BUILD_SHARED)
34+
# define C89STRINGUTILS_DEPRECATED __declspec(deprecated)
35+
# else
36+
# define C89STRINGUTILS_DEPRECATED
37+
# endif
38+
#endif
39+
40+
#ifndef C89STRINGUTILS_DEPRECATED_EXPORT
41+
# define C89STRINGUTILS_DEPRECATED_EXPORT C89STRINGUTILS_EXPORT C89STRINGUTILS_DEPRECATED
42+
#endif
43+
44+
#ifndef C89STRINGUTILS_DEPRECATED_NO_EXPORT
45+
# define C89STRINGUTILS_DEPRECATED_NO_EXPORT C89STRINGUTILS_NO_EXPORT C89STRINGUTILS_DEPRECATED
46+
#endif
47+
48+
#if 0 /* DEFINE_NO_DEPRECATED */
49+
# ifndef C89STRINGUTILS_NO_DEPRECATED
50+
# define C89STRINGUTILS_NO_DEPRECATED
51+
# endif
52+
#endif
53+
54+
#endif /* C89STRINGUTILS_EXPORT_H */

c89stringutils/c89stringutils_string_extras.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
*
2323
* SPDX-License-Identifier: BSD-2-Clause
2424
*/
25+
#if OLD_MSVC
26+
27+
#define snprintf _snprintf
28+
#define vsnprintf _vsnprintf
29+
30+
31+
32+
33+
#else
34+
2535

2636
inline int snprintf(char *buffer, size_t count, const char *format, ...) {
2737
int result;
@@ -52,6 +62,8 @@ inline double wtf_vsnprintf(char *buffer, size_t count, const char *format,
5262
#define vsnprintf(buffer, count, format, args) \
5363
wtf_vsnprintf(buffer, count, format, args)
5464

65+
#endif /* !OLD_MSVC */
66+
5567
#endif /* !defined(HAVE_SNPRINTF_H) */
5668

5769
#ifndef HAVE_STRNCASECMP_H

c89stringutils/c89stringutils_string_extras.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626

2727
#define HAVE_SNPRINTF_H
2828

29+
#else
30+
31+
#define OLD_MSVC
32+
/* !defined(MSC_VER) || MSC_VER > 1400 *//* 1400 is MSVC 2005 */
33+
2934
#endif /* _MSC_VER >= 1900 */
3035

3136
#else
@@ -109,6 +114,7 @@ extern C89STRINGUTILS_EXPORT char *strnstr(const char *, const char *, size_t);
109114
#endif /* !HAVE_STRNSTR */
110115

111116
#ifndef HAVE_STRCASESTR_H
117+
112118
extern C89STRINGUTILS_EXPORT char *strcasestr(const char *, const char *);
113119

114120
#endif /* !HAVE_STRCASESTR_H */

c89stringutils_tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.15)
1+
cmake_minimum_required(VERSION 3.11)
22
project(c89stringutils_tests
33
VERSION "0.0.1"
44
LANGUAGES C)

c89stringutils_tests/test_c89stringutils/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ file(MAKE_DIRECTORY "${DOWNLOAD_DIR}")
1010

1111
set(GREATEST_SHA256 "1292593d95c35eeccc89ffa1c91d6fe53b49f81cbf2c2b7758842b7f3186fcc2")
1212
if (NOT CMAKE_SYSTEM_NAME STREQUAL "SunOS")
13-
file(DOWNLOAD "https://raw.githubusercontent.com/silentbicycle/greatest/11a6af1/greatest.h"
13+
file(DOWNLOAD "https://raw.githubusercontent.com/silentbicycle/greatest/fbbf981/greatest.h"
1414
"${DOWNLOAD_DIR}/greatest.h")
1515
file(SHA256 "${DOWNLOAD_DIR}/greatest.h" GREATEST_SHA256_FOUND)
1616
if (NOT GREATEST_SHA256_FOUND STREQUAL GREATEST_SHA256)

c89stringutils_tests/test_c89stringutils/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifdef _MSC_VER
2-
#define _CRT_SECURE_NO_WARNINGS 1
32
#endif /* _MSC_VER */
3+
#define _CRT_SECURE_NO_WARNINGS 1
44

55
#include <greatest.h>
66

0 commit comments

Comments
 (0)