-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
153 lines (130 loc) · 4.98 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
cmake_minimum_required(VERSION 3.15)
cmake_policy(SET CMP0091 NEW)
cmake_policy(SET CMP0042 NEW)
project(flac-bindings)
include(CheckCCompilerFlag)
include(CheckIPOSupported)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(COVERAGE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS}")
message("-- Enabled code coverage")
endif()
if(SANITIZE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize=undefined")
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -fsanitize=address -fsanitize=undefined -static-libasan")
message("-- Enabled sanitizers")
endif()
if(NOT MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "11.0.0")
# GCC does not warn about invalid target so we need to disabled it manually
set(COMPILER_SUPPORTS_x86_64_V2_MICRO_ARCH NO)
else()
check_c_compiler_flag("-march=x86-64-v2" COMPILER_SUPPORTS_x86_64_V2_MICRO_ARCH)
endif()
check_c_compiler_flag("-march=nehalem" COMPILER_SUPPORTS_NEHALEM_MICRO_ARCH)
if (COMPILER_SUPPORTS_x86_64_V2_MICRO_ARCH)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=x86-64-v2")
elseif(COMPILER_SUPPORTS_NEHALEM_MICRO_ARCH)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=nehalem")
endif()
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
check_c_compiler_flag("-march=armv8-a" COMPILER_SUPPORTS_ARM_V8_MICRO_ARCH)
if (COMPILER_SUPPORTS_ARM_V8_MICRO_ARCH)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a")
endif()
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm")
check_c_compiler_flag("-march=armv7-a" COMPILER_SUPPORTS_ARM_V7_MICRO_ARCH)
if (COMPILER_SUPPORTS_ARM_V7_MICRO_ARCH)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv7-a")
endif()
endif()
endif()
check_ipo_supported(RESULT LTO_SUPPORTED)
if(LTO_SUPPORTED AND CMAKE_BUILD_TYPE STREQUAL "Release")
message("-- LTO/IPO supported and enabled")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()
option(USE_FLAC_SOURCES OFF)
set(FLAC_TARGET FLAC)
if(NOT USE_FLAC_SOURCES)
if(MSVC)
find_package(FLAC 1.3 QUIET)
else()
find_package(FLAC 1.3 QUIET)
if(NOT FLAC_FOUND)
include(FindPkgConfig)
pkg_check_modules(FLAC QUIET IMPORTED_TARGET flac>=1.3)
set(FLAC_TARGET PkgConfig::FLAC)
endif()
endif()
endif()
if(USE_FLAC_SOURCES OR NOT FLAC_FOUND)
if(NOT USE_FLAC_SOURCES)
message("-- No libflac detected on system that can be used, fall back to compile from sources")
endif()
include(FetchContent)
option(BUILD_TESTING OFF)
# fetch libogg (v1.3.5)
FetchContent_Declare(
Ogg
GIT_REPOSITORY https://github.com/xiph/ogg
GIT_TAG v1.3.5
)
# polyfill for FetchContent_MakeAvailable
FetchContent_GetProperties(Ogg)
if(NOT ogg_POPULATED)
FetchContent_Populate(Ogg)
add_subdirectory(${ogg_SOURCE_DIR} ${ogg_BINARY_DIR})
endif()
# tell CMake ogg already exists
set(OGG_FOUND YES)
# expected target is different in FLAC, so creating an alias to be able to use it
add_library(Ogg::ogg ALIAS ogg)
# fetch flac (v1.4.1)
FetchContent_Declare(
FLAC
GIT_REPOSITORY https://github.com/xiph/flac
GIT_TAG 1.4.3
)
option(WITH_OGG YES)
option(BUILD_PROGRAMS OFF)
option(BUILD_EXAMPLES OFF)
option(BUILD_CXXLIBS OFF)
option(BUILD_TESTING OFF)
option(BUILD_DOCS OFF)
option(INSTALL_MANPAGES OFF)
FetchContent_GetProperties(FLAC)
if(NOT flac_POPULATED)
FetchContent_Populate(FLAC)
add_subdirectory(${flac_SOURCE_DIR} ${flac_BINARY_DIR})
endif()
set(FLAC_TARGET FLAC)
endif()
file(GLOB_RECURSE SOURCE_FILES "src/**/*.cpp" "src/**/*.hpp" "src/**/*.h" "src/*.cpp")
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
set(napi_build_version 8 CACHE STRING "N-API Version")
message("-- Using N-API ${napi_build_version}")
add_compile_definitions(
NAPI_VERSION=${napi_build_version}
NAPI_EXPERIMENTAL
NODE_ADDON_API_DISABLE_DEPRECATED
NODE_API_EXPERIMENTAL_NOGC_ENV_OPT_OUT
)
# NODE_API_EXPERIMENTAL_NOGC_ENV_OPT_OUT node-addon-api does not support nogc versions of the API
# see https://github.com/nodejs/node-addon-api/issues/1508
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB} ${FLAC_TARGET})
if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET)
execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
endif()
if(USE_FLAC_SOURCES OR NOT FLAC_FOUND)
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux" OR "${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties("${FLAC_TARGET}" PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()
endif()