1# Generate the config.h to compile with specific intrinsics / libs.
2
3# Check for compiler options.
4include(CheckCSourceCompiles)
5check_c_source_compiles("
6    int main(void) {
7      (void)__builtin_bswap16(0);
8      return 0;
9    }
10  " HAVE_BUILTIN_BSWAP16)
11check_c_source_compiles("
12    int main(void) {
13      (void)__builtin_bswap32(0);
14      return 0;
15    }
16  " HAVE_BUILTIN_BSWAP32)
17check_c_source_compiles("
18    int main(void) {
19      (void)__builtin_bswap64(0);
20      return 0;
21    }
22  " HAVE_BUILTIN_BSWAP64)
23
24# Check for libraries.
25find_package(Threads)
26if(Threads_FOUND)
27  if(CMAKE_USE_PTHREADS_INIT)
28    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
29  endif()
30  foreach(PTHREAD_TEST HAVE_PTHREAD_PRIO_INHERIT PTHREAD_CREATE_UNDETACHED)
31    check_c_source_compiles("
32        #include <pthread.h>
33        int main (void) {
34          int attr = ${PTHREAD_TEST};
35          return attr;
36        }
37      " ${PTHREAD_TEST})
38  endforeach()
39  list(APPEND WEBP_DEP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
40endif()
41set(WEBP_USE_THREAD ${Threads_FOUND})
42
43# TODO: this seems unused, check with autotools.
44set(LT_OBJDIR ".libs/")
45
46# Only useful for vwebp, so useless for now.
47find_package(OpenGL)
48set(WEBP_HAVE_GL ${OPENGL_FOUND})
49
50# Check if we need to link to the C math library. We do not look for it as it is
51# not found when cross-compiling, while it is here.
52check_c_source_compiles("
53    #include <math.h>
54    int main(int argc, char** argv) {
55      return (int)pow(argc, 2.5);
56    }
57  " HAVE_MATH_LIBRARY)
58if(NOT HAVE_MATH_LIBRARY)
59  message(STATUS "Adding -lm flag.")
60  list(APPEND WEBP_DEP_LIBRARIES m)
61endif()
62
63# Find the standard image libraries.
64set(WEBP_DEP_IMG_LIBRARIES)
65set(WEBP_DEP_IMG_INCLUDE_DIRS)
66foreach(I_LIB PNG JPEG TIFF)
67  find_package(${I_LIB})
68  set(WEBP_HAVE_${I_LIB} ${${I_LIB}_FOUND})
69  if(${I_LIB}_FOUND)
70    list(APPEND WEBP_DEP_IMG_LIBRARIES ${${I_LIB}_LIBRARIES})
71    list(APPEND WEBP_DEP_IMG_INCLUDE_DIRS ${${I_LIB}_INCLUDE_DIR}
72                ${${I_LIB}_INCLUDE_DIRS})
73  endif()
74endforeach()
75if(WEBP_DEP_IMG_INCLUDE_DIRS)
76  list(REMOVE_DUPLICATES WEBP_DEP_IMG_INCLUDE_DIRS)
77endif()
78
79# GIF detection, gifdec isn't part of the imageio lib.
80include(CMakePushCheckState)
81set(WEBP_DEP_GIF_LIBRARIES)
82set(WEBP_DEP_GIF_INCLUDE_DIRS)
83find_package(GIF)
84set(WEBP_HAVE_GIF ${GIF_FOUND})
85if(GIF_FOUND)
86  # GIF find_package only locates the header and library, it doesn't fail
87  # compile tests when detecting the version, but falls back to 3 (as of at
88  # least cmake 3.7.2). Make sure the library links to avoid incorrect detection
89  # when cross compiling.
90  cmake_push_check_state()
91  set(CMAKE_REQUIRED_LIBRARIES ${GIF_LIBRARIES})
92  set(CMAKE_REQUIRED_INCLUDES ${GIF_INCLUDE_DIR})
93  check_c_source_compiles("
94      #include <gif_lib.h>
95      int main(void) {
96        (void)DGifOpenFileHandle;
97        return 0;
98      }
99      " GIF_COMPILES)
100  cmake_pop_check_state()
101  if(GIF_COMPILES)
102    list(APPEND WEBP_DEP_GIF_LIBRARIES ${GIF_LIBRARIES})
103    list(APPEND WEBP_DEP_GIF_INCLUDE_DIRS ${GIF_INCLUDE_DIR})
104  else()
105    unset(GIF_FOUND)
106  endif()
107endif()
108
109# Check for specific headers.
110include(CheckIncludeFiles)
111check_include_files("stdlib.h;stdarg.h;string.h;float.h" STDC_HEADERS)
112check_include_files(dlfcn.h HAVE_DLFCN_H)
113check_include_files(GLUT/glut.h HAVE_GLUT_GLUT_H)
114check_include_files(GL/glut.h HAVE_GL_GLUT_H)
115check_include_files(inttypes.h HAVE_INTTYPES_H)
116check_include_files(memory.h HAVE_MEMORY_H)
117check_include_files(OpenGL/glut.h HAVE_OPENGL_GLUT_H)
118check_include_files(shlwapi.h HAVE_SHLWAPI_H)
119check_include_files(stdint.h HAVE_STDINT_H)
120check_include_files(stdlib.h HAVE_STDLIB_H)
121check_include_files(strings.h HAVE_STRINGS_H)
122check_include_files(string.h HAVE_STRING_H)
123check_include_files(sys/stat.h HAVE_SYS_STAT_H)
124check_include_files(sys/types.h HAVE_SYS_TYPES_H)
125check_include_files(unistd.h HAVE_UNISTD_H)
126check_include_files(wincodec.h HAVE_WINCODEC_H)
127check_include_files(windows.h HAVE_WINDOWS_H)
128
129# Windows specifics
130if(HAVE_WINCODEC_H)
131  list(APPEND WEBP_DEP_LIBRARIES
132              shlwapi
133              ole32
134              windowscodecs)
135endif()
136
137# Check for SIMD extensions.
138include(${CMAKE_CURRENT_LIST_DIR}/cpu.cmake)
139
140# Define extra info.
141set(PACKAGE ${PROJECT_NAME})
142set(PACKAGE_NAME ${PROJECT_NAME})
143
144# Read from configure.ac.
145file(READ ${CMAKE_CURRENT_SOURCE_DIR}/configure.ac CONFIGURE_AC)
146string(REGEX MATCHALL
147             "\\[([0-9a-z\\.:/]*)\\]"
148             CONFIGURE_AC_PACKAGE_INFO
149             ${CONFIGURE_AC})
150function(strip_bracket VAR)
151  string(LENGTH ${${VAR}} TMP_LEN)
152  math(EXPR TMP_LEN ${TMP_LEN}-2)
153  string(SUBSTRING ${${VAR}}
154                   1
155                   ${TMP_LEN}
156                   TMP_SUB)
157  set(${VAR} ${TMP_SUB} PARENT_SCOPE)
158endfunction()
159
160list(GET CONFIGURE_AC_PACKAGE_INFO 1 PACKAGE_VERSION)
161strip_bracket(PACKAGE_VERSION)
162list(GET CONFIGURE_AC_PACKAGE_INFO 2 PACKAGE_BUGREPORT)
163strip_bracket(PACKAGE_BUGREPORT)
164list(GET CONFIGURE_AC_PACKAGE_INFO 3 PACKAGE_URL)
165strip_bracket(PACKAGE_URL)
166
167# Build more info.
168set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
169set(PACKAGE_TARNAME ${PACKAGE_NAME})
170set(VERSION ${PACKAGE_VERSION})
171