CMake minimum required lowered to 3.1
[flac.git] / CMakeLists.txt
1 cmake_minimum_required(VERSION 3.0)
2
3 project(FLAC VERSION 1.3.2) # HOMEPAGE_URL "https://www.xiph.org/flac/")
4
5 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
6
7 option(BUILD_CXXLIBS "Build libFLAC++" ON)
8 option(BUILD_EXAMPLES "Build and install examples" ON)
9 option(WITH_OGG "ogg support (default: test for libogg)" ON)
10
11 if(WITH_OGG)
12     find_package(OGG REQUIRED)
13 endif()
14
15 if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
16     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -Waggregate-return -Wcast-align -Wnested-externs -Wshadow -Wundef -Wmissing-declarations -Winline")
17
18     if(CMAKE_BULD_TYPE STREQUAL Release)
19         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -funroll-loops")
20     endif()
21
22     option(ENABLE_SSP "Enable GNU GCC stack smash protection" OFF)
23 endif()
24 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
25     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wcast-align -Wshadow -Wwrite-strings -Wctor-dtor-privacy -Wnon-virtual-dtor -Wreorder -Wsign-promo -Wundef")
26 endif()
27 if(CMAKE_C_COMPILER_ID MATCHES "GNU")
28     set(CMAKE_EXE_LINKER_FLAGS -no-pie)
29 endif()
30
31 include(CMakePackageConfigHelpers)
32 include(CPack)
33 include(CTest)
34 include(CheckCCompilerFlag)
35 include(CheckCXXCompilerFlag)
36 include(CheckSymbolExists)
37 include(CheckFunctionExists)
38 include(CheckIncludeFile)
39 include(CheckCSourceCompiles)
40 include(CheckCXXSourceCompiles)
41 include(GNUInstallDirs)
42 include(UseSystemExtensions)
43 include(TestBigEndian)
44
45 check_include_file("byteswap.h" HAVE_BYTESWAP_H)
46 check_include_file("inttypes.h" HAVE_INTTYPES_H)
47 check_include_file("stdint.h" HAVE_STDINT_H)
48 check_include_file("x86intrin.h" FLAC__HAS_X86INTRIN)
49
50 check_function_exists(fseeko HAVE_FSEEKO)
51
52 check_c_source_compiles("int main() { return __builtin_bswap16 (0) ; }" HAVE_BSWAP16)
53 check_c_source_compiles("int main() { return __builtin_bswap32 (0) ; }" HAVE_BSWAP32)
54
55 test_big_endian(CPU_IS_BIG_ENDIAN)
56
57 check_c_compiler_flag(-Werror HAVE_WERROR_FLAG)
58 check_c_compiler_flag(-Wdeclaration-after-statement HAVE_DECL_AFTER_STMT_FLAG)
59 check_c_compiler_flag("-fstack-protector --param ssp-buffer-size=4" HAVE_SSP_FLAG)
60 check_c_compiler_flag(-mstackrealign HAVE_STACKREALIGN_FLAG)
61 check_cxx_compiler_flag(-Weffc++ HAVE_WEFFCXX_FLAG)
62
63 if(HAVE_WERROR_FLAG)
64     option(ENABLE_WERROR "Enable -Werror in all Makefiles" OFF)
65 endif()
66
67 add_compile_options(
68     $<$<BOOL:${MSVC}>:/wd4267>
69     $<$<BOOL:${MSVC}>:/wd4996>
70     $<$<BOOL:${ENABLE_WERROR}>:-Werror>
71     $<$<AND:$<BOOL:${HAVE_SSP_FLAG}>,$<BOOL:${ENABLE_SSP}>>:-fstack-protector>
72     $<$<AND:$<BOOL:${HAVE_SSP_FLAG}>,$<BOOL:${ENABLE_SSP}>>:--param>
73     $<$<AND:$<BOOL:${HAVE_SSP_FLAG}>,$<BOOL:${ENABLE_SSP}>>:ssp-buffer-size=4>
74     $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:${HAVE_WEFFCXX_FLAG}>>:-Weffc++>
75     $<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:${HAVE_DECL_AFTER_STMT_FLAG}>>:-Wdeclaration-after-statement>)
76
77 if(CMAKE_SYSTEM_PROCESSOR STREQUAL "i686" AND HAVE_STACKREALIGN_FLAG)
78     add_compile_options(-mstackrealign)
79 endif()
80
81 include_directories("include")
82
83 include_directories("${CMAKE_CURRENT_BINARY_DIR}")
84 add_definitions(-DHAVE_CONFIG_H)
85
86 if(MSVC)
87     add_definitions(
88         -D_CRT_SECURE_NO_WARNINGS
89         -D_USE_MATH_DEFINES)
90 endif()
91 if(CMAKE_BUILD_TYPE STREQUAL Debug OR CMAKE_BUILD_TYPE STREQUAL RelWithDebInfo)
92     add_definitions(-DFLAC__OVERFLOW_DETECT)
93 endif()
94
95 add_subdirectory("doc")
96 add_subdirectory("src")
97 add_subdirectory("microbench")
98 if(BUILD_EXAMPLES)
99     add_subdirectory("examples")
100 endif()
101 if(BUILD_TESTING)
102     add_subdirectory("test")
103 endif()
104
105 configure_file(config.cmake.h.in config.h)
106
107 install(
108     EXPORT targets
109     DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake"
110     NAMESPACE FLAC::)
111
112 configure_package_config_file(
113     flac-config.cmake.in flac-config.cmake
114     INSTALL_DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake")
115 write_basic_package_version_file(
116     flac-config-version.cmake COMPATIBILITY AnyNewerVersion)
117
118 install(
119     FILES
120         "${CMAKE_CURRENT_BINARY_DIR}/flac-config.cmake"
121         "${CMAKE_CURRENT_BINARY_DIR}/flac-config-version.cmake"
122         "cmake/FindOGG.cmake"
123     DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake")
124
125 file(GLOB FLAC_HEADERS "include/FLAC/*.h")
126 file(GLOB FLAC++_HEADERS "include/FLAC++/*.h")
127 install(FILES ${FLAC_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/FLAC")
128 install(FILES ${FLAC++_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/FLAC++")
129 install(FILES "man/flac.1" "man/metaflac.1" DESTINATION "${CMAKE_INSTALL_MANDIR}")