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