-
Notifications
You must be signed in to change notification settings - Fork 23
/
CMakeLists.txt
127 lines (109 loc) · 3.94 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Copyright (C) 2017 Franklin "Snaipe" Mathieu.
# Redistribution and use of this file is allowed according to the terms of the MIT license.
# For details see the LICENSE file distributed with Mimick.
cmake_minimum_required (VERSION 2.8)
project (fmem C)
list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/.cmake")
include (CheckSymbolExists)
include (CheckCSourceCompiles)
include (GNUInstallDirs)
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall -Wextra -pedantic")
endif ()
list (APPEND CMAKE_REQUIRED_DEFINITIONS
-D_GNU_SOURCE
-D_CRT_RAND_S
-DVC_EXTRALEAN
-DWIN32_LEAN_AND_MEAN)
check_symbol_exists (open_memstream stdio.h HAVE_OPEN_MEMSTREAM)
check_symbol_exists (fopencookie stdio.h HAVE_FOPENCOOKIE)
check_symbol_exists (funopen stdio.h HAVE_FUNOPEN)
check_symbol_exists (tmpfile stdio.h HAVE_TMPFILE)
check_symbol_exists (rand_s stdlib.h HAVE_WINAPI_RAND_S)
check_symbol_exists (CreateFile windows.h HAVE_WINAPI_CREATEFILE)
check_symbol_exists (CloseHandle windows.h HAVE_WINAPI_CLOSEHANDLE)
check_symbol_exists (GetFileSize windows.h HAVE_WINAPI_GETFILESIZE)
check_symbol_exists (CreateFileMapping windows.h HAVE_WINAPI_CREATEFILEMAPPING)
check_symbol_exists (MapViewOfFile windows.h HAVE_WINAPI_MAPVIEWOFFILE)
check_symbol_exists (UnmapViewOfFile windows.h HAVE_WINAPI_UNMAPVIEWOFFILE)
check_symbol_exists (GetTempPath windows.h HAVE_WINAPI_GETTEMPPATH)
check_symbol_exists (_open_osfhandle io.h HAVE_WINAPI_OPEN_OSFHANDLE)
check_symbol_exists (_get_osfhandle io.h HAVE_WINAPI_GET_OSFHANDLE)
check_symbol_exists (_fdopen stdio.h HAVE_WINAPI_FDOPEN)
check_symbol_exists (_fileno stdio.h HAVE_WINAPI_FILENO)
check_symbol_exists (_close io.h HAVE_WINAPI_CLOSE)
set (SOURCES)
if (HAVE_OPEN_MEMSTREAM)
list (APPEND SOURCES src/fmem-open_memstream.c)
elseif (HAVE_FOPENCOOKIE)
list (APPEND SOURCES
src/alloc.c
src/alloc.h
src/fmem-fopencookie.c)
elseif (HAVE_FUNOPEN)
list (APPEND SOURCES
src/alloc.c
src/alloc.h
src/fmem-funopen.c)
elseif (HAVE_WINAPI_CREATEFILE
AND HAVE_WINAPI_CLOSEHANDLE
AND HAVE_WINAPI_GETFILESIZE
AND HAVE_WINAPI_CREATEFILEMAPPING
AND HAVE_WINAPI_MAPVIEWOFFILE
AND HAVE_WINAPI_UNMAPVIEWOFFILE
AND HAVE_WINAPI_GETTEMPPATH
AND HAVE_WINAPI_FDOPEN
AND HAVE_WINAPI_FILENO
AND HAVE_WINAPI_CLOSE
AND HAVE_WINAPI_OPEN_OSFHANDLE
AND HAVE_WINAPI_GET_OSFHANDLE
AND HAVE_WINAPI_RAND_S)
list (APPEND SOURCES src/fmem-winapi-tmpfile.c)
elseif (HAVE_TMPFILE)
list (APPEND SOURCES src/fmem-tmpfile.c)
else ()
message (FATAL_ERROR "No memory stream implementation found")
endif ()
include_directories (include src ${PROJECT_BINARY_DIR}/gen)
add_library (fmem ${SOURCES})
get_property (FMEM_LIBTYPE
TARGET fmem
PROPERTY TYPE)
set (CMAKE_REQUIRED_DEFINITIONS -fvisibility=hidden)
check_c_source_compiles (
"__attribute__((visibility(\"default\"))) int main(void) { return 0; }"
CC_HAVE_VISIBILITY)
set (CMAKE_REQUIRED_DEFINITIONS)
if ("${FMEM_LIBTYPE}" MATCHES "SHARED_LIBRARY")
if (WIN32)
set (EXPORT_MACROS
"#ifdef FMEM_BUILD_LIBRARY
# define FMEM_API __declspec(dllexport)
#else /* !FMEM_BUILD_LIBRARY */
# define FMEM_API __declspec(dllimport)
#endif /* !FMEM_BUILD_LIBRARY */")
add_definitions (-DFMEM_BUILD_LIBRARY)
elseif (CC_HAVE_VISIBILITY)
set (EXPORT_MACROS "#define FMEM_API __attribute__((visibility(\"default\")))")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
endif ()
else ()
set (EXPORT_MACROS "#define FMEM_API")
endif ()
configure_file (
${PROJECT_SOURCE_DIR}/include/fmem.h.in
${PROJECT_BINARY_DIR}/gen/fmem.h
@ONLY)
install(TARGETS fmem
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES
fmem.h
${PROJECT_BINARY_DIR}/gen/fmem-export.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
include (CTest)
if (BUILD_TESTING)
find_package (Criterion REQUIRED)
add_subdirectory (test)
endif ()