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