• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 3.1 is OK for most parts. However:
2# 3.3 is needed in src/libFLAC
3# 3.5 is needed in src/libFLAC/ia32
4# 3.9 is needed in 'doc' because of doxygen_add_docs()
5cmake_minimum_required(VERSION 3.5)
6
7if(NOT (CMAKE_BUILD_TYPE OR CMAKE_CONFIGURATION_TYPES OR DEFINED ENV{CFLAGS} OR DEFINED ENV{CXXFLAGS}))
8    set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo")
9endif()
10
11project(FLAC VERSION 1.3.3) # HOMEPAGE_URL "https://www.xiph.org/flac/")
12
13list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
14
15option(BUILD_CXXLIBS "Build libFLAC++" ON)
16option(BUILD_PROGRAMS "Build and install programs" ON)
17option(BUILD_EXAMPLES "Build and install examples" ON)
18option(BUILD_DOCS "Build and install doxygen documents" ON)
19option(WITH_STACK_PROTECTOR "Enable GNU GCC stack smash protection" ON)
20option(INSTALL_MANPAGES "Install MAN pages" ON)
21option(INSTALL_PKGCONFIG_MODULES "Install PkgConfig modules" ON)
22option(INSTALL_CMAKE_CONFIG_MODULE "Install CMake package-config module" ON)
23option(WITH_OGG "ogg support (default: test for libogg)" ON)
24
25if(WITH_OGG)
26    find_package(Ogg REQUIRED)
27endif()
28
29find_package(Iconv)
30set(HAVE_ICONV ${Iconv_FOUND})
31
32if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
33    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -Waggregate-return -Wcast-align -Wnested-externs -Wshadow -Wundef -Wmissing-declarations -Winline")
34    set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -funroll-loops")
35endif()
36if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
37    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wcast-align -Wshadow -Wwrite-strings -Wctor-dtor-privacy -Wnon-virtual-dtor -Wreorder -Wsign-promo -Wundef")
38endif()
39
40include(CMakePackageConfigHelpers)
41include(CPack)
42include(CTest)
43include(CheckCCompilerFlag)
44include(CheckCXXCompilerFlag)
45include(CheckSymbolExists)
46include(CheckFunctionExists)
47include(CheckIncludeFile)
48include(CheckCSourceCompiles)
49include(CheckCXXSourceCompiles)
50include(GNUInstallDirs)
51include(UseSystemExtensions)
52include(TestBigEndian)
53
54check_include_file("byteswap.h" HAVE_BYTESWAP_H)
55check_include_file("inttypes.h" HAVE_INTTYPES_H)
56check_include_file("stdint.h" HAVE_STDINT_H)
57if(MSVC)
58    check_include_file("intrin.h" FLAC__HAS_X86INTRIN)
59else()
60    check_include_file("x86intrin.h" FLAC__HAS_X86INTRIN)
61endif()
62
63check_function_exists(fseeko HAVE_FSEEKO)
64
65check_c_source_compiles("int main() { return __builtin_bswap16 (0) ; }" HAVE_BSWAP16)
66check_c_source_compiles("int main() { return __builtin_bswap32 (0) ; }" HAVE_BSWAP32)
67
68test_big_endian(CPU_IS_BIG_ENDIAN)
69
70check_c_compiler_flag(-Werror HAVE_WERROR_FLAG)
71check_c_compiler_flag(-Wdeclaration-after-statement HAVE_DECL_AFTER_STMT_FLAG)
72check_c_compiler_flag(-mstackrealign HAVE_STACKREALIGN_FLAG)
73check_cxx_compiler_flag(-Weffc++ HAVE_WEFFCXX_FLAG)
74
75if(WITH_STACK_PROTECTOR)
76  if(NOT MSVC)
77    check_c_compiler_flag("-fstack-protector-strong" HAVE_STACK_PROTECTOR_FLAG)
78  endif()
79endif()
80
81if(HAVE_WERROR_FLAG)
82    option(ENABLE_WERROR "Enable -Werror in all Makefiles" OFF)
83endif()
84
85add_compile_options(
86    $<$<BOOL:${MSVC}>:/wd4267>
87    $<$<BOOL:${MSVC}>:/wd4996>
88    $<$<BOOL:${ENABLE_WERROR}>:-Werror>
89    $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:${HAVE_WEFFCXX_FLAG}>>:-Weffc++>
90    $<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:${HAVE_DECL_AFTER_STMT_FLAG}>>:-Wdeclaration-after-statement>)
91
92if(HAVE_STACK_PROTECTOR_FLAG)
93    add_compile_options(-fstack-protector-strong)
94endif()
95
96if(CMAKE_SYSTEM_PROCESSOR STREQUAL "i686" AND HAVE_STACKREALIGN_FLAG)
97    add_compile_options(-mstackrealign)
98endif()
99
100include_directories("include")
101
102include_directories("${CMAKE_CURRENT_BINARY_DIR}")
103add_definitions(-DHAVE_CONFIG_H)
104
105if(MSVC)
106    add_definitions(
107        -D_CRT_SECURE_NO_WARNINGS
108        -D_USE_MATH_DEFINES)
109endif()
110if(CMAKE_BUILD_TYPE STREQUAL Debug OR CMAKE_BUILD_TYPE STREQUAL RelWithDebInfo)
111    add_definitions(-DFLAC__OVERFLOW_DETECT)
112endif()
113
114add_subdirectory("src")
115add_subdirectory("microbench")
116if(BUILD_DOCS)
117    add_subdirectory("doc")
118endif()
119if(BUILD_EXAMPLES)
120    add_subdirectory("examples")
121endif()
122if(BUILD_TESTING)
123    add_subdirectory("test")
124endif()
125
126configure_file(config.cmake.h.in config.h)
127
128if(INSTALL_CMAKE_CONFIG_MODULE)
129    install(
130        EXPORT targets
131        DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake"
132        NAMESPACE FLAC::)
133
134    configure_package_config_file(
135        flac-config.cmake.in flac-config.cmake
136        INSTALL_DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake")
137    write_basic_package_version_file(
138        flac-config-version.cmake COMPATIBILITY AnyNewerVersion)
139
140    install(
141        FILES
142            "${CMAKE_CURRENT_BINARY_DIR}/flac-config.cmake"
143            "${CMAKE_CURRENT_BINARY_DIR}/flac-config-version.cmake"
144        DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake")
145endif()
146
147file(GLOB FLAC_HEADERS "include/FLAC/*.h")
148file(GLOB FLAC++_HEADERS "include/FLAC++/*.h")
149install(FILES ${FLAC_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/FLAC")
150install(FILES ${FLAC++_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/FLAC++")
151if(INSTALL_MANPAGES)
152    install(FILES "man/flac.1" "man/metaflac.1" DESTINATION "${CMAKE_INSTALL_MANDIR}")
153endif()
154