CMakeLists.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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()
  5. cmake_minimum_required(VERSION 3.5)
  6. if(NOT (CMAKE_BUILD_TYPE OR CMAKE_CONFIGURATION_TYPES OR DEFINED ENV{CFLAGS} OR DEFINED ENV{CXXFLAGS}))
  7. set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo")
  8. endif()
  9. project(FLAC VERSION 1.4.3) # HOMEPAGE_URL "https://www.xiph.org/flac/")
  10. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
  11. option(BUILD_CXXLIBS "Build libFLAC++" ON)
  12. option(BUILD_PROGRAMS "Build and install programs" OFF)
  13. option(BUILD_EXAMPLES "Build and install examples" OFF)
  14. option(BUILD_TESTING "Build tests" OFF)
  15. option(BUILD_DOCS "Build and install doxygen documents" OFF)
  16. option(WITH_FORTIFY_SOURCE "Enable protection against buffer overflows" ON)
  17. option(WITH_STACK_PROTECTOR "Enable GNU GCC stack smash protection" ON)
  18. option(INSTALL_MANPAGES "Install MAN pages" OFF)
  19. option(INSTALL_PKGCONFIG_MODULES "Install PkgConfig modules" ON)
  20. option(INSTALL_CMAKE_CONFIG_MODULE "Install CMake package-config module" ON)
  21. option(WITH_OGG "ogg support (default: test for libogg)" OFF)
  22. option(BUILD_SHARED_LIBS "Build shared instead of static libraries" OFF)
  23. set(VERSION ${PROJECT_VERSION})
  24. if(NOT UNIX)
  25. # This is to make sure testing works when building with a DLL
  26. set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/objs)
  27. set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/objs)
  28. endif()
  29. if(WITH_OGG)
  30. if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/ogg")
  31. add_subdirectory("ogg")
  32. set(OGG_FOUND 1 CACHE INTERNAL "ogg has been added as subdirectory")
  33. set_target_properties(ogg PROPERTIES FOLDER Libraries)
  34. if(BUILD_TESTING)
  35. set_target_properties(test_bitwise test_framing PROPERTIES FOLDER Tests)
  36. endif()
  37. else()
  38. if(NOT TARGET Ogg::ogg)
  39. find_package(Ogg REQUIRED)
  40. else()
  41. set(OGG_FOUND 1 CACHE INTERNAL "ogg has already been built")
  42. endif()
  43. set(OGG_PACKAGE "ogg")
  44. endif()
  45. endif()
  46. find_program (HAVE_GIT git)
  47. if(HAVE_GIT)
  48. execute_process(
  49. COMMAND git --git-dir=.git describe --tags --exact-match
  50. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  51. OUTPUT_VARIABLE GIT_COMMIT_TAG
  52. OUTPUT_STRIP_TRAILING_WHITESPACE
  53. ERROR_QUIET
  54. )
  55. execute_process(
  56. COMMAND git --git-dir=.git log -1 --pretty=format:%h
  57. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  58. OUTPUT_VARIABLE GIT_COMMIT_HASH
  59. OUTPUT_STRIP_TRAILING_WHITESPACE
  60. ERROR_QUIET
  61. )
  62. execute_process(
  63. COMMAND git --git-dir=.git log -1 --pretty=format:%cd --date=format:%Y%m%d
  64. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  65. OUTPUT_VARIABLE GIT_COMMIT_DATE
  66. OUTPUT_STRIP_TRAILING_WHITESPACE
  67. ERROR_QUIET
  68. )
  69. endif()
  70. if(NOT WIN32)
  71. find_package(Iconv)
  72. set(HAVE_ICONV ${Iconv_FOUND})
  73. endif()
  74. if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
  75. set(CMAKE_C_FLAGS "-Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -Waggregate-return -Wcast-align -Wnested-externs -Wshadow -Wundef -Wmissing-declarations -Winline ${CMAKE_C_FLAGS}")
  76. set(CMAKE_C_FLAGS_RELEASE "-DNDEBUG ${CMAKE_C_FLAGS_RELEASE}")
  77. endif()
  78. if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
  79. set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wcast-align -Wshadow -Wwrite-strings -Wctor-dtor-privacy -Wnon-virtual-dtor -Wreorder -Wsign-promo -Wundef ${CMAKE_CXX_FLAGS}")
  80. set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG ${CMAKE_CXX_FLAGS_RELEASE}")
  81. endif()
  82. if(MSVC)
  83. set(CMAKE_C_FLAGS_RELEASE "/O2 /Ob2 /Oi /Ot /Oy /DNDEBUG ${CMAKE_C_FLAGS_RELEASE}")
  84. endif()
  85. include(CMakePackageConfigHelpers)
  86. include(CheckCCompilerFlag)
  87. include(CheckCXXCompilerFlag)
  88. include(CheckSymbolExists)
  89. include(CheckFunctionExists)
  90. include(CheckIncludeFile)
  91. include(CheckCSourceCompiles)
  92. include(CheckCXXSourceCompiles)
  93. include(CheckLibraryExists)
  94. include(GNUInstallDirs)
  95. include(UseSystemExtensions)
  96. include(TestBigEndian)
  97. enable_testing()
  98. check_include_file("byteswap.h" HAVE_BYTESWAP_H)
  99. check_include_file("inttypes.h" HAVE_INTTYPES_H)
  100. check_include_file("stdint.h" HAVE_STDINT_H)
  101. check_include_file("stdbool.h" HAVE_STDBOOL_H)
  102. check_include_file("arm_neon.h" FLAC__HAS_NEONINTRIN)
  103. if(NOT HAVE_STDINT_H OR NOT HAVE_STDBOOL_H)
  104. message(SEND_ERROR "Header stdint.h and/or stdbool.h not found")
  105. endif()
  106. if(MSVC)
  107. check_include_file("intrin.h" FLAC__HAS_X86INTRIN)
  108. else()
  109. check_include_file("x86intrin.h" FLAC__HAS_X86INTRIN)
  110. endif()
  111. check_function_exists(fseeko HAVE_FSEEKO)
  112. check_c_source_compiles("int main() { return __builtin_bswap16 (0) ; }" HAVE_BSWAP16)
  113. check_c_source_compiles("int main() { return __builtin_bswap32 (0) ; }" HAVE_BSWAP32)
  114. check_c_source_compiles("
  115. #include <langinfo.h>
  116. int main()
  117. {
  118. char* cs = nl_langinfo(CODESET);
  119. return !cs;
  120. }"
  121. HAVE_LANGINFO_CODESET)
  122. test_big_endian(CPU_IS_BIG_ENDIAN)
  123. check_c_compiler_flag(-Werror HAVE_WERROR_FLAG)
  124. check_c_compiler_flag(-Wdeclaration-after-statement HAVE_DECL_AFTER_STMT_FLAG)
  125. check_c_compiler_flag(-mstackrealign HAVE_STACKREALIGN_FLAG)
  126. check_cxx_compiler_flag(-Weffc++ HAVE_WEFFCXX_FLAG)
  127. if(MINGW AND (WITH_FORTIFY_SOURCE OR WITH_STACK_PROTECTOR))
  128. check_library_exists("ssp.a" __stack_chk_fail "" HAVE_LIBSSP)
  129. if(NOT HAVE_LIBSSP)
  130. message(WARNING "Could not find libssp in MinGW, stack protection and/or FORTIFY_SOURCE are unavailable")
  131. else()
  132. link_libraries("ssp.a")
  133. endif()
  134. elseif(NOT MSVC)
  135. set(HAVE_LIBSSP 1)
  136. endif()
  137. if(WITH_STACK_PROTECTOR)
  138. if(NOT MSVC)
  139. check_c_compiler_flag("-fstack-protector-strong" HAVE_STACK_PROTECTOR_FLAG)
  140. endif()
  141. endif()
  142. if(HAVE_WERROR_FLAG)
  143. option(ENABLE_WERROR "Enable -Werror in all Makefiles" OFF)
  144. endif()
  145. add_compile_options(
  146. $<$<BOOL:${MSVC}>:/wd4267>
  147. $<$<BOOL:${MSVC}>:/wd4996>
  148. $<$<BOOL:${ENABLE_WERROR}>:-Werror>
  149. $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:${HAVE_WEFFCXX_FLAG}>>:-Weffc++>
  150. $<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:${HAVE_DECL_AFTER_STMT_FLAG}>>:-Wdeclaration-after-statement>)
  151. if(WITH_FORTIFY_SOURCE AND HAVE_LIBSSP)
  152. add_definitions(-D_FORTIFY_SOURCE=2)
  153. endif()
  154. if(HAVE_STACK_PROTECTOR_FLAG AND HAVE_LIBSSP)
  155. add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fstack-protector-strong>)
  156. endif()
  157. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "i686" AND HAVE_STACKREALIGN_FLAG)
  158. add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-mstackrealign>)
  159. endif()
  160. include_directories("include")
  161. include_directories("${CMAKE_CURRENT_BINARY_DIR}")
  162. add_definitions(-DHAVE_CONFIG_H)
  163. if(MSVC)
  164. add_definitions(
  165. -D_CRT_SECURE_NO_WARNINGS
  166. -D_USE_MATH_DEFINES)
  167. endif()
  168. if(CMAKE_BUILD_TYPE STREQUAL Debug OR CMAKE_BUILD_TYPE STREQUAL RelWithDebInfo)
  169. add_definitions(-DFLAC__OVERFLOW_DETECT)
  170. endif()
  171. add_subdirectory("src")
  172. add_subdirectory("microbench")
  173. if(BUILD_DOCS)
  174. add_subdirectory("doc")
  175. endif()
  176. if(BUILD_EXAMPLES)
  177. add_subdirectory("examples")
  178. endif()
  179. if(BUILD_TESTING)
  180. add_subdirectory("test")
  181. endif()
  182. # The following folder layout is mostly for MSVC
  183. set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  184. set_target_properties(FLAC grabbag getopt replaygain_analysis replaygain_synthesis utf8 PROPERTIES FOLDER Libraries)
  185. if(BUILD_CXXLIBS)
  186. set_target_properties(FLAC++ PROPERTIES FOLDER Libraries)
  187. endif()
  188. if(BUILD_PROGRAMS)
  189. set_target_properties(flacapp metaflac PROPERTIES FOLDER Programs)
  190. endif()
  191. if(BUILD_TESTING)
  192. set_target_properties(test_libFLAC test_libs_common test_picture test_seeking test_streams test_cuesheet PROPERTIES FOLDER Tests)
  193. if(BUILD_CXXLIBS)
  194. set_target_properties(test_libFLAC++ PROPERTIES FOLDER Tests)
  195. endif()
  196. endif()
  197. if(BUILD_EXAMPLES)
  198. set_target_properties(decode_file encode_file PROPERTIES FOLDER Examples)
  199. if(BUILD_CXXLIBS)
  200. set_target_properties(decode_file_cxx encode_file_cxx PROPERTIES FOLDER Examples)
  201. endif()
  202. endif()
  203. if(BUILD_UTILS)
  204. set_target_properties(flacdiff flactimer PROPERTIES FOLDER Utils)
  205. endif()
  206. configure_file(config.cmake.h.in config.h)
  207. if(INSTALL_CMAKE_CONFIG_MODULE)
  208. install(
  209. EXPORT targets
  210. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
  211. NAMESPACE FLAC::)
  212. export(EXPORT targets NAMESPACE FLAC:: FILE FLACTargets.cmake)
  213. configure_package_config_file(
  214. ${PROJECT_SOURCE_DIR}/flac-config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/flac-config.cmake
  215. INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
  216. write_basic_package_version_file(
  217. ${CMAKE_CURRENT_BINARY_DIR}/flac-config-version.cmake COMPATIBILITY AnyNewerVersion)
  218. install(
  219. FILES ${CMAKE_CURRENT_BINARY_DIR}/flac-config.cmake ${CMAKE_CURRENT_BINARY_DIR}/flac-config-version.cmake
  220. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
  221. )
  222. install(
  223. FILES
  224. "${CMAKE_CURRENT_BINARY_DIR}/flac-config.cmake"
  225. "${CMAKE_CURRENT_BINARY_DIR}/flac-config-version.cmake"
  226. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
  227. endif()
  228. file(GLOB FLAC_HEADERS "include/FLAC/*.h")
  229. file(GLOB FLAC++_HEADERS "include/FLAC++/*.h")
  230. install(FILES ${FLAC_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/FLAC")
  231. install(FILES ${FLAC++_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/FLAC++")
  232. if(INSTALL_MANPAGES)
  233. find_program (HAVE_PANDOC pandoc)
  234. if(HAVE_PANDOC)
  235. file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/man")
  236. add_custom_command(
  237. OUTPUT man/flac.1
  238. COMMAND pandoc --standalone --to man "${CMAKE_SOURCE_DIR}/man/flac.md" > man/flac.1
  239. )
  240. add_custom_command(
  241. OUTPUT man/metaflac.1
  242. COMMAND pandoc --standalone --to man "${CMAKE_SOURCE_DIR}/man/metaflac.md" > man/metaflac.1
  243. )
  244. add_custom_target(man ALL DEPENDS man/flac.1 man/metaflac.1)
  245. install(FILES "${CMAKE_BINARY_DIR}/man/flac.1" "${CMAKE_BINARY_DIR}/man/metaflac.1" DESTINATION "${CMAKE_INSTALL_MANDIR}/man1")
  246. else()
  247. if(EXISTS "${CMAKE_SOURCE_DIR}/man/flac.1" AND EXISTS "${CMAKE_SOURCE_DIR}/man/metaflac.1")
  248. install(FILES "man/flac.1" "man/metaflac.1" DESTINATION "${CMAKE_INSTALL_MANDIR}/man1")
  249. else()
  250. message(SEND_ERROR "Pandoc nor prebuild manpages are found. Cannot install manpages. Set INSTALL_MANPAGES to OFF to build without man pages")
  251. endif()
  252. endif()
  253. endif()