forked from pykaldi/pykaldi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
299 lines (258 loc) · 10.6 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
cmake_minimum_required(VERSION 3.5)
project(PyKaldi)
# Prevent in-source builds
if( ${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR} )
message( FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there. You may need to remove CMakeCache.txt." )
endif()
####################################################################################################
# Find dependencies
####################################################################################################
# Find Python
find_package(PythonInterp REQUIRED)
find_package(PythonLibs REQUIRED)
get_filename_component(PYTHON_LIB_DIR "${PYTHON_LIBRARIES}" PATH)
# Find Numpy
if(NOT NUMPY_INC_DIR)
message(FATAL_ERROR "You need to specify Numpy include directory. Set -DNUMPY_INC_DIR option.")
endif(NOT NUMPY_INC_DIR)
# Find CLIF
if(NOT PYCLIF)
set(PYCLIF $ENV{PYCLIF})
if (NOT PYCLIF)
find_program(PYCLIF pyclif)
if(NOT PYCLIF)
message(FATAL_ERROR "Could not find 'pyclif'. Set PYCLIF environment variable or -DPYCLIF option.")
endif(NOT PYCLIF)
endif(NOT PYCLIF)
endif(NOT PYCLIF)
message("Using PYCLIF: ${PYCLIF}")
if(NOT CLIF_MATCHER)
set(CLIF_MATCHER $ENV{CLIF_MATCHER})
if (NOT CLIF_MATCHER)
get_filename_component(_Python_PREFIX "${PYTHON_LIB_DIR}" PATH)
find_program(CLIF_MATCHER clif-matcher PATH ${_Python_PREFIX}/clang/bin)
unset(_Python_PREFIX)
if(NOT CLIF_MATCHER)
message(FATAL_ERROR "Could not find 'clif-matcher'. Set CLIF_MATCHER environment variable or -DCLIF_MATCHER option.")
endif(NOT CLIF_MATCHER)
endif(NOT CLIF_MATCHER)
endif(NOT CLIF_MATCHER)
message("Using CLIF_MATCHER: ${CLIF_MATCHER}")
# Find Kaldi
if(NOT KALDI_DIR)
set(KALDI_DIR $ENV{KALDI_DIR})
if(NOT KALDI_DIR)
message(FATAL_ERROR "You need to specify Kaldi directory. Set KALDI_DIR environment variable or -DKALDI_DIR option.")
endif(NOT KALDI_DIR)
endif(NOT KALDI_DIR)
set(KALDI_SRC_DIR "${KALDI_DIR}/src")
set(KALDI_TOOLS_DIR "${KALDI_DIR}/tools")
set(KALDI_LIBRARIES_DIR "${KALDI_SRC_DIR}/lib")
set(OPENFST_LIB_DIR "${KALDI_TOOLS_DIR}/openfst/lib")
####################################################################################################
# Set compiler and linker flags
####################################################################################################
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined -Wl,--as-needed ${LD_FLAGS}")
if(CUDA)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CUDA_LD_FLAGS}")
set(LD_LIBS "${LD_LIBS} ${CUDA_LD_LIBS}")
endif(CUDA)
separate_arguments(LD_LIBS)
set(CMAKE_INSTALL_RPATH "${PYTHON_LIB_DIR};${KALDI_LIBRARIES_DIR};${OPENFST_LIB_DIR}")
file(GLOB children RELATIVE ${CMAKE_SOURCE_DIR}/kaldi ${CMAKE_SOURCE_DIR}/kaldi/*)
foreach(child ${children})
if(IS_DIRECTORY ${CMAKE_SOURCE_DIR}/kaldi/${child})
LIST(APPEND CMAKE_INSTALL_RPATH "$ORIGIN/../${child}")
endif()
endforeach()
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
include_directories(
${CMAKE_SOURCE_DIR}/kaldi/lib
${CMAKE_SOURCE_DIR}/kaldi
${CMAKE_BINARY_DIR}/kaldi
${KALDI_SRC_DIR}
${PYTHON_INCLUDE_DIRS}
${NUMPY_INC_DIR}
)
link_directories(
${KALDI_LIBRARIES_DIR}
${OPENFST_LIB_DIR}
)
####################################################################################################
# Function to set up rules to invoke pyclif on a .clif file
# and generate the wrapper .cc and .h files.
# It also adds a custom target for building the python extension.
#
# Arguments:
# name - name of the extension
# pyclif_file - pyclif file to compile into a python extension
#
# Options:
# LIBRARIES - (multi-value) libraries (e.g. kaldi-matrix, kaldi-base) to link against
# NAMESPACES - (multi-value) additional namespaces to make visible in the generated .cc files
# RPATH - colon separated list of additional runtime paths
# CLIF_DEPS - (multi-value) other clif targets that this target depends on
####################################################################################################
function(add_pyclif_library name pyclif_file)
set(oneValueArgs RPATH)
set(multiValueArgs CLIF_DEPS NAMESPACES LIBRARIES)
cmake_parse_arguments(PYCLIF_LIBRARY "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
# PYCLIF configuration
string(REPLACE ".clif" "" pyclif_file_basename ${pyclif_file})
set(gen_cc "${CMAKE_CURRENT_BINARY_DIR}/${pyclif_file_basename}-clifwrap.cc")
set(gen_h "${CMAKE_CURRENT_BINARY_DIR}/${pyclif_file_basename}-clifwrap.h")
set(gen_init "${CMAKE_CURRENT_BINARY_DIR}/${pyclif_file_basename}-clifwrap-init.cc")
string(REPLACE "-" "_" module_name ${name})
if(PYTHON_VERSION_MAJOR EQUAL 3)
set(PYOUTPUT "--py3output")
else()
set(PYOUTPUT "--py2output")
endif()
# This defines the call to pyclif
add_custom_command(
OUTPUT ${gen_cc} ${gen_h} ${gen_init}
COMMAND
${PYCLIF}
${PYOUTPUT}
--matcher_bin=${CLIF_MATCHER}
--ccdeps_out ${gen_cc} --header_out ${gen_h} --ccinit_out ${gen_init}
--modname=${module_name}
--prepend=clif/python/types.h
-I${CMAKE_SOURCE_DIR}/kaldi/lib
-I${CMAKE_SOURCE_DIR}/kaldi
-I${CMAKE_BINARY_DIR}/kaldi
-I${KALDI_SRC_DIR}
"-f-I${PYTHON_INCLUDE_DIRS} \
-I${CMAKE_SOURCE_DIR}/kaldi/lib \
-I${CMAKE_SOURCE_DIR}/kaldi \
-I${CMAKE_BINARY_DIR}/kaldi \
-I${KALDI_SRC_DIR} \
${CLIF_CXX_FLAGS} \
${CMAKE_CXX_FLAGS}"
${CMAKE_CURRENT_SOURCE_DIR}/${pyclif_file}
COMMAND
${CMAKE_SOURCE_DIR}/tools/use_namespace.sh ${gen_cc} ${PYCLIF_LIBRARY_NAMESPACES}
VERBATIM
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${pyclif_file}
)
# Create a custom target to run pyclif whenever the file changes
add_custom_target(
${module_name}_pyclif
ALL
DEPENDS ${gen_cc} ${gen_h} ${gen_init}
VERBATIM
)
# Add dependencies on the other pyclif targets
foreach(DEP ${PYCLIF_LIBRARY_CLIF_DEPS})
add_dependencies(${module_name}_pyclif ${DEP})
endforeach(DEP)
# Call the custom function for building the extension module
# PYCLIFDEP flag adds a dependency on ${module_name}_pyclif target
add_CC_library(${module_name}
"${gen_cc};${gen_init}"
PYCLIFDEP
RPATH ${PYCLIF_LIBRARY_RPATH}
CLIF_DEPS ${PYCLIF_LIBRARY_CLIF_DEPS}
LIBRARIES ${PYCLIF_LIBRARY_LIBRARIES})
endfunction(add_pyclif_library)
####################################################################################################
# This function handles the compilation of python extension modules.
#
# arguments:
# module_name - name of the python extension module
# SOURCES - (multi-value) list of source files to compile into a shared library
#
# options:
# OUTPUTDIR - Overrides the output directory for shared libraries
# PYCLIFDEP - (flag) add a dependency on ${module_name}-pyclif target. Should be set for files generated with pyclif
# LIBRARIES - (multi-value) libraries (e.g. kaldi-matrix, kaldi-base) to link against
# RPATH - colon separated list of additional runtime paths
# CLIF_DEPS - (multi-value) other clif targets that this target depends on
####################################################################################################
function(add_CC_library module_name SOURCES)
set(options PYCLIFDEP)
set(multiValueArgs LIBRARIES CLIF_DEPS)
set(oneValueArgs RPATH OUTPUTDIR)
cmake_parse_arguments(PYCLIF_LIBRARY "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
string(REPLACE "-" "_" module_name ${module_name})
if("${PYCLIF_LIBRARY_OUTPUTDIR}" STREQUAL "")
string(REPLACE "/build" "/build/lib" PYCLIF_LIBRARY_OUTPUTDIR ${CMAKE_CURRENT_BINARY_DIR})
endif()
if(DEBUG)
message("Module: ${module_name}")
message("- Sources:")
foreach(source ${SOURCES})
message(" ${source}")
endforeach()
endif(DEBUG)
# Create extension module
add_library(${module_name} SHARED ${SOURCES})
if(DEBUG)
message("- Dependencies:")
endif(DEBUG)
# If PYCLIFDEP is set, add a dependency on ${module_name}_pyclif
if(PYCLIF_LIBRARY_PYCLIFDEP)
if(DEBUG)
message(" ${module_name}_pyclif")
endif(DEBUG)
add_dependencies(${module_name} ${module_name}_pyclif)
endif(PYCLIF_LIBRARY_PYCLIFDEP)
# Link with other extension modules
foreach(LIB ${PYCLIF_LIBRARY_CLIF_DEPS})
if(DEBUG)
message(" ${LIB}")
endif(DEBUG)
target_link_libraries(${module_name} ${LIB})
endforeach(LIB)
if(DEBUG)
message("- Libraries:")
endif(DEBUG)
# Link with clif library
if(NOT "${module_name}" STREQUAL "${CLIF_LIBRARY}")
if(DEBUG)
message(" ${CLIF_LIBRARY}")
endif(DEBUG)
target_link_libraries(${module_name} ${CLIF_LIBRARY})
endif()
# Link with kaldi libraries
foreach(LIB ${PYCLIF_LIBRARY_LIBRARIES})
if(DEBUG)
message(" ${LIB}")
endif(DEBUG)
target_link_libraries(${module_name} ${LIB})
endforeach(LIB)
# Link with other libraries
foreach(LIB ${LD_LIBS})
target_link_libraries(${module_name} ${LIB})
endforeach(LIB)
# Do not prepend extension modules with "lib"
set_target_properties(${module_name} PROPERTIES PREFIX "")
# Set the output directory for the extension module
set_target_properties(${module_name}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${PYCLIF_LIBRARY_OUTPUTDIR}
LIBRARY_OUTPUT_DIRECTORY ${PYCLIF_LIBRARY_OUTPUTDIR}
RUNTIME_OUTPUT_DIRECTORY ${PYCLIF_LIBRARY_OUTPUTDIR})
if(DEBUG)
message("- Runtime path: ${PYCLIF_LIBRARY_RPATH}")
message("- Output directory: ${PYCLIF_LIBRARY_OUTPUTDIR}")
message("")
endif(DEBUG)
endfunction(add_CC_library)
####################################################################################################
# Add target for clif library
####################################################################################################
set(CLIF_LIBRARY "_clif")
string(REPLACE "/build" "/build/lib/kaldi/lib" CLIF_LIB_OUTPUTDIR ${CMAKE_CURRENT_BINARY_DIR})
set(CLIF_SRC_DIR "${CMAKE_SOURCE_DIR}/kaldi/lib")
add_CC_library(${CLIF_LIBRARY}
"${CLIF_SRC_DIR}/clif/python/optional.cc;${CLIF_SRC_DIR}/clif/python/runtime.cc;${CLIF_SRC_DIR}/clif/python/slots.cc;${CLIF_SRC_DIR}/clif/python/types.cc"
OUTPUTDIR ${CLIF_LIB_OUTPUTDIR}
LIBRARIES ${PYTHON_LIBRARIES}
)
####################################################################################################
# Add targets for pykaldi libraries
####################################################################################################
add_subdirectory("kaldi")
add_subdirectory("docs")