Statistics
| Branch: | Tag: | Revision:

root / volk / lib / CMakeLists.txt @ bb61c325

History | View | Annotate | Download (12.6 kB)

1
#
2
# Copyright 2011-2012 Free Software Foundation, Inc.
3
#
4
# This program is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
#
17
18
########################################################################
19
# header file detection
20
########################################################################
21
include(CheckIncludeFile)
22
CHECK_INCLUDE_FILE(cpuid.h HAVE_CPUID_H)
23
if(HAVE_CPUID_H)
24
    add_definitions(-DHAVE_CPUID_H)
25
endif()
26
27
CHECK_INCLUDE_FILE(intrin.h HAVE_INTRIN_H)
28
if(HAVE_INTRIN_H)
29
    add_definitions(-DHAVE_INTRIN_H)
30
endif()
31
32
CHECK_INCLUDE_FILE(fenv.h HAVE_FENV_H)
33
if(HAVE_FENV_H)
34
    add_definitions(-DHAVE_FENV_H)
35
endif()
36
37
CHECK_INCLUDE_FILE(dlfcn.h HAVE_DLFCN_H)
38
if(HAVE_DLFCN_H)
39
    add_definitions(-DHAVE_DLFCN_H)
40
    list(APPEND volk_libraries ${CMAKE_DL_LIBS})
41
endif()
42
43
########################################################################
44
# Setup the compiler name
45
########################################################################
46
set(COMPILER_NAME ${CMAKE_C_COMPILER_ID})
47
if(MSVC) #its not set otherwise
48
    set(COMPILER_NAME MSVC)
49
endif()
50
51
message(STATUS "Compiler name: ${COMPILER_NAME}")
52
53
if(NOT DEFINED COMPILER_NAME)
54
    message(FATAL_ERROR "COMPILER_NAME undefined. Volk build may not support this compiler.")
55
endif()
56
57
########################################################################
58
# detect x86 flavor of CPU
59
########################################################################
60
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(i.86|x86|x86_64|amd64)$")
61
    message(STATUS "x86* CPU detected")
62
    set(CPU_IS_x86 TRUE)
63
endif()
64
65
########################################################################
66
# determine passing architectures based on compile flag tests
67
########################################################################
68
execute_process(
69
    COMMAND ${PYTHON_EXECUTABLE} ${PYTHON_DASH_B}
70
    ${CMAKE_SOURCE_DIR}/gen/volk_compile_utils.py
71
    --mode "arch_flags" --compiler "${COMPILER_NAME}"
72
    OUTPUT_VARIABLE arch_flag_lines OUTPUT_STRIP_TRAILING_WHITESPACE
73
)
74
75
macro(check_arch arch_name)
76
    set(flags ${ARGN})
77
    set(have_${arch_name} TRUE)
78
    foreach(flag ${flags})
79
        include(CheckCXXCompilerFlag)
80
        set(have_flag have${flag})
81
        execute_process( #make the have_flag have nice alphanum chars (just for looks/not necessary)
82
            COMMAND ${PYTHON_EXECUTABLE} -c "import re; print(re.sub('\\W', '_', '${have_flag}'))"
83
            OUTPUT_VARIABLE have_flag OUTPUT_STRIP_TRAILING_WHITESPACE
84
        )
85
        CHECK_CXX_COMPILER_FLAG(${flag} ${have_flag})
86
        if (NOT ${have_flag})
87
            set(have_${arch_name} FALSE)
88
        endif()
89
    endforeach(flag)
90
    if (have_${arch_name})
91
        list(APPEND available_archs ${arch_name})
92
    endif()
93
endmacro(check_arch)
94
95
foreach(line ${arch_flag_lines})
96
    string(REGEX REPLACE "," ";" arch_flags ${line})
97
    check_arch(${arch_flags})
98
endforeach(line)
99
100
macro(OVERRULE_ARCH arch reason)
101
    message(STATUS "${reason}, Overruled arch ${arch}")
102
    list(REMOVE_ITEM available_archs ${arch})
103
endmacro(OVERRULE_ARCH)
104
105
########################################################################
106
# eliminate AVX on GCC < 4.4
107
# even though it accepts -mavx, as won't assemble xgetbv, which we need
108
########################################################################
109
if(CPU_IS_x86 AND COMPILER_NAME MATCHES "GNU")
110
    execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
111
        OUTPUT_VARIABLE GCC_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE)
112
    if(GCC_VERSION VERSION_LESS "4.4")
113
        OVERRULE_ARCH(avx "GCC missing xgetbv")
114
    endif()
115
endif()
116
117
########################################################################
118
# implement overruling in the ORC case,
119
# since ORC always passes flag detection
120
########################################################################
121
if(NOT ORC_FOUND)
122
    OVERRULE_ARCH(orc "ORC support not found")
123
endif()
124
125
########################################################################
126
# implement overruling in the non-multilib case
127
# this makes things work when both -m32 and -m64 pass
128
########################################################################
129
if(NOT CROSSCOMPILE_MULTILIB AND CPU_IS_x86)
130
    include(CheckTypeSize)
131
    check_type_size("void*[8]" SIZEOF_CPU BUILTIN_TYPES_ONLY)
132
    if (${SIZEOF_CPU} EQUAL 64)
133
        OVERRULE_ARCH(32 "CPU width is 64 bits")
134
    endif()
135
    if (${SIZEOF_CPU} EQUAL 32)
136
        OVERRULE_ARCH(64 "CPU width is 32 bits")
137
    endif()
138
endif()
139
140
########################################################################
141
# done overrules! print the result
142
########################################################################
143
message(STATUS "Available architectures: ${available_archs}")
144
145
########################################################################
146
# determine available machines given the available architectures
147
########################################################################
148
execute_process(
149
    COMMAND ${PYTHON_EXECUTABLE} ${PYTHON_DASH_B}
150
    ${CMAKE_SOURCE_DIR}/gen/volk_compile_utils.py
151
    --mode "machines" --archs "${available_archs}"
152
    OUTPUT_VARIABLE available_machines OUTPUT_STRIP_TRAILING_WHITESPACE
153
)
154
155
########################################################################
156
# Implement machine overruling for redundant machines:
157
# A machine is redundant when expansion rules occur,
158
# and the arch superset passes configuration checks.
159
# When this occurs, eliminate the redundant machines
160
# to avoid unnecessary compilation of subset machines.
161
########################################################################
162
foreach(arch orc 64 32)
163
    foreach(machine_name ${available_machines})
164
        string(REPLACE "_${arch}" "" machine_name_no_arch ${machine_name})
165
        if (${machine_name} STREQUAL ${machine_name_no_arch})
166
        else()
167
            list(REMOVE_ITEM available_machines ${machine_name_no_arch})
168
        endif()
169
    endforeach(machine_name)
170
endforeach(arch)
171
172
########################################################################
173
# done overrules! print the result
174
########################################################################
175
message(STATUS "Available machines: ${available_machines}")
176
177
########################################################################
178
# Create rules to run the volk generator
179
########################################################################
180
181
#dependencies are all python, xml, and header implementation files
182
file(GLOB xml_files ${CMAKE_SOURCE_DIR}/gen/*.xml)
183
file(GLOB py_files ${CMAKE_SOURCE_DIR}/gen/*.py)
184
file(GLOB h_files ${CMAKE_SOURCE_DIR}/include/volk/*.h)
185
186
macro(gen_template tmpl output)
187
    list(APPEND volk_gen_sources ${output})
188
    add_custom_command(
189
        OUTPUT ${output}
190
        DEPENDS ${xml_files} ${py_files} ${h_files} ${tmpl}
191
        COMMAND ${PYTHON_EXECUTABLE} ${PYTHON_DASH_B}
192
        ${CMAKE_SOURCE_DIR}/gen/volk_tmpl_utils.py
193
        --input ${tmpl} --output ${output} ${ARGN}
194
    )
195
endmacro(gen_template)
196
197
make_directory(${CMAKE_BINARY_DIR}/include/volk)
198
199
gen_template(${CMAKE_SOURCE_DIR}/tmpl/volk.tmpl.h              ${CMAKE_BINARY_DIR}/include/volk/volk.h)
200
gen_template(${CMAKE_SOURCE_DIR}/tmpl/volk.tmpl.c              ${CMAKE_BINARY_DIR}/lib/volk.c)
201
gen_template(${CMAKE_SOURCE_DIR}/tmpl/volk_typedefs.tmpl.h     ${CMAKE_BINARY_DIR}/include/volk/volk_typedefs.h)
202
gen_template(${CMAKE_SOURCE_DIR}/tmpl/volk_cpu.tmpl.h          ${CMAKE_BINARY_DIR}/include/volk/volk_cpu.h)
203
gen_template(${CMAKE_SOURCE_DIR}/tmpl/volk_cpu.tmpl.c          ${CMAKE_BINARY_DIR}/lib/volk_cpu.c)
204
gen_template(${CMAKE_SOURCE_DIR}/tmpl/volk_config_fixed.tmpl.h ${CMAKE_BINARY_DIR}/include/volk/volk_config_fixed.h)
205
gen_template(${CMAKE_SOURCE_DIR}/tmpl/volk_machines.tmpl.h     ${CMAKE_BINARY_DIR}/lib/volk_machines.h)
206
gen_template(${CMAKE_SOURCE_DIR}/tmpl/volk_machines.tmpl.c     ${CMAKE_BINARY_DIR}/lib/volk_machines.c)
207
208
foreach(machine_name ${available_machines})
209
    #generate machine source
210
    set(machine_source ${CMAKE_CURRENT_BINARY_DIR}/volk_machine_${machine_name}.c)
211
    gen_template(${CMAKE_SOURCE_DIR}/tmpl/volk_machine_xxx.tmpl.c ${machine_source} ${machine_name})
212
213
    #determine machine flags
214
    execute_process(
215
        COMMAND ${PYTHON_EXECUTABLE} ${PYTHON_DASH_B}
216
        ${CMAKE_SOURCE_DIR}/gen/volk_compile_utils.py
217
        --mode "machine_flags" --machine "${machine_name}" --compiler "${COMPILER_NAME}"
218
        OUTPUT_VARIABLE ${machine_name}_flags OUTPUT_STRIP_TRAILING_WHITESPACE
219
    )
220
    if(${machine_name}_flags)
221
        set_source_files_properties(${machine_source} PROPERTIES COMPILE_FLAGS "${${machine_name}_flags}")
222
    endif()
223
224
    #add to available machine defs
225
    string(TOUPPER LV_MACHINE_${machine_name} machine_def)
226
    list(APPEND machine_defs ${machine_def})
227
endforeach(machine_name)
228
229
########################################################################
230
# Set local include directories first
231
########################################################################
232
include_directories(
233
    ${CMAKE_BINARY_DIR}/include
234
    ${CMAKE_SOURCE_DIR}/include
235
    ${CMAKE_CURRENT_BINARY_DIR}
236
    ${CMAKE_CURRENT_SOURCE_DIR}
237
)
238
239
########################################################################
240
# Handle orc support
241
########################################################################
242
if(ORC_FOUND)
243
    #setup orc library usage
244
    include_directories(${ORC_INCLUDE_DIRS})
245
    link_directories(${ORC_LIBRARY_DIRS})
246
    list(APPEND volk_libraries ${ORC_LIBRARIES})
247
248
    #setup orc functions
249
    file(GLOB orc_files ${CMAKE_SOURCE_DIR}/orc/*.orc)
250
    foreach(orc_file ${orc_files})
251
252
        #extract the name for the generated c source from the orc file
253
        get_filename_component(orc_file_name_we ${orc_file} NAME_WE)
254
        set(orcc_gen ${CMAKE_CURRENT_BINARY_DIR}/${orc_file_name_we}.c)
255
256
        #create a rule to generate the source and add to the list of sources
257
        add_custom_command(
258
            COMMAND ${ORCC_EXECUTABLE} --include math.h --implementation -o ${orcc_gen} ${orc_file}
259
            DEPENDS ${orc_file} OUTPUT ${orcc_gen}
260
        )
261
        list(APPEND volk_sources ${orcc_gen})
262
263
    endforeach(orc_file)
264
else()
265
    message(STATUS "Did not find liborc and orcc, disabling orc support...")
266
endif()
267
268
########################################################################
269
# Setup the volk sources list and library
270
########################################################################
271
if(NOT WIN32)
272
    add_definitions(-fvisibility=hidden)
273
endif()
274
275
list(APPEND volk_sources
276
    ${CMAKE_CURRENT_SOURCE_DIR}/volk_prefs.c
277
    ${CMAKE_CURRENT_SOURCE_DIR}/volk_rank_archs.c
278
    ${volk_gen_sources}
279
)
280
281
#set the machine definitions where applicable
282
set_source_files_properties(
283
    ${CMAKE_CURRENT_BINARY_DIR}/volk.c
284
    ${CMAKE_CURRENT_BINARY_DIR}/volk_machines.c
285
PROPERTIES COMPILE_DEFINITIONS "${machine_defs}")
286
287
if(MSVC)
288
    #add compatibility includes for stdint types
289
    include_directories(${CMAKE_SOURCE_DIR}/cmake/msvc)
290
    add_definitions(-DHAVE_CONFIG_H)
291
    #compile the sources as C++ due to the lack of complex.h under MSVC
292
    set_source_files_properties(${volk_sources} PROPERTIES LANGUAGE CXX)
293
endif()
294
295
#create the volk runtime library
296
add_library(volk SHARED ${volk_sources})
297
target_link_libraries(volk ${volk_libraries})
298
set_target_properties(volk PROPERTIES SOVERSION ${LIBVER})
299
set_target_properties(volk PROPERTIES DEFINE_SYMBOL "volk_EXPORTS")
300
301
install(TARGETS volk
302
    LIBRARY DESTINATION lib${LIB_SUFFIX} COMPONENT "volk_runtime" # .so file
303
    ARCHIVE DESTINATION lib${LIB_SUFFIX} COMPONENT "volk_devel"   # .lib file
304
    RUNTIME DESTINATION bin              COMPONENT "volk_runtime" # .dll file
305
)
306
307
########################################################################
308
# Build the QA test application
309
########################################################################
310
311
312
if(Boost_FOUND)
313
314
    set_source_files_properties(
315
        ${CMAKE_CURRENT_SOURCE_DIR}/testqa.cc PROPERTIES
316
        COMPILE_DEFINITIONS "BOOST_TEST_DYN_LINK;BOOST_TEST_MAIN"
317
    )
318
319
    include_directories(${Boost_INCLUDE_DIRS})
320
    link_directories(${Boost_LIBRARY_DIRS})
321
322
    add_executable(test_all
323
        ${CMAKE_CURRENT_SOURCE_DIR}/testqa.cc
324
        ${CMAKE_CURRENT_SOURCE_DIR}/qa_utils.cc
325
    )
326
    target_link_libraries(test_all volk ${Boost_LIBRARIES})
327
    add_test(qa_volk_test_all test_all)
328
329
endif(Boost_FOUND)
330