No strncasecmp with MSVC
[platform/upstream/openblas.git] / CMakeLists.txt
1 ##
2 ## Author: Hank Anderson <hank@statease.com>
3 ##
4
5 cmake_minimum_required(VERSION 2.8.5)
6 project(OpenBLAS)
7 set(OpenBLAS_MAJOR_VERSION 0)
8 set(OpenBLAS_MINOR_VERSION 3)
9 set(OpenBLAS_PATCH_VERSION 0.dev)
10 set(OpenBLAS_VERSION "${OpenBLAS_MAJOR_VERSION}.${OpenBLAS_MINOR_VERSION}.${OpenBLAS_PATCH_VERSION}")
11
12 enable_language(ASM)
13 enable_language(C)
14
15 # Adhere to GNU filesystem layout conventions
16 include(GNUInstallDirs)
17
18 if(MSVC)
19 set(OpenBLAS_LIBNAME libopenblas)
20 else()
21 set(OpenBLAS_LIBNAME openblas)
22 endif()
23
24 #######
25 if(MSVC)
26 option(BUILD_WITHOUT_LAPACK "Without LAPACK and LAPACKE (Only BLAS or CBLAS)" ON)
27 endif()
28 option(BUILD_WITHOUT_CBLAS "Without CBLAS" OFF)
29 option(BUILD_DEBUG "Build Debug Version" OFF)
30 option(DYNAMIC_ARCH "Build with DYNAMIC_ARCH" OFF)
31 #######
32 if(BUILD_WITHOUT_LAPACK)
33 set(NO_LAPACK 1)
34 set(NO_LAPACKE 1)
35 endif()
36
37 if(CMAKE_CONFIGURATION_TYPES) # multiconfig generator?
38         set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "" FORCE)
39         set(CMAKE_BUILD_TYPE
40                 Debug Debug
41                 Release Release
42         )
43 else()
44         if( NOT CMAKE_BUILD_TYPE )
45                 if(BUILD_DEBUG)
46                         set(CMAKE_BUILD_TYPE Debug)
47                 else()
48                         set(CMAKE_BUILD_TYPE Release)
49                 endif()
50         endif()
51 endif()
52
53 if(BUILD_WITHOUT_CBLAS)
54 set(NO_CBLAS 1)
55 endif()
56
57 #######
58
59
60 message(WARNING "CMake support is experimental. This will not produce the same Makefiles that OpenBLAS ships with. Only x86 support is currently available.")
61
62 include("${PROJECT_SOURCE_DIR}/cmake/utils.cmake")
63 include("${PROJECT_SOURCE_DIR}/cmake/system.cmake")
64
65 set(BLASDIRS interface driver/level2 driver/level3 driver/others)
66
67 if (NOT DYNAMIC_ARCH)
68   list(APPEND BLASDIRS kernel)
69 endif ()
70
71 if (DEFINED SANITY_CHECK)
72   list(APPEND BLASDIRS reference)
73 endif ()
74
75 set(SUBDIRS     ${BLASDIRS})
76 if (NOT NO_LAPACK)
77   list(APPEND SUBDIRS lapack)
78 endif ()
79
80 # set which float types we want to build for
81 if (NOT DEFINED BUILD_SINGLE AND NOT DEFINED BUILD_DOUBLE AND NOT DEFINED BUILD_COMPLEX AND NOT DEFINED BUILD_COMPLEX16)
82   # if none are defined, build for all
83   set(BUILD_SINGLE true)
84   set(BUILD_DOUBLE true)
85   set(BUILD_COMPLEX true)
86   set(BUILD_COMPLEX16 true)
87 endif ()
88
89 set(FLOAT_TYPES "")
90 if (BUILD_SINGLE)
91   message(STATUS "Building Single Precision")
92   list(APPEND FLOAT_TYPES "SINGLE") # defines nothing
93 endif ()
94
95 if (BUILD_DOUBLE)
96   message(STATUS "Building Double Precision")
97   list(APPEND FLOAT_TYPES "DOUBLE") # defines DOUBLE
98 endif ()
99
100 if (BUILD_COMPLEX)
101   message(STATUS "Building Complex Precision")
102   list(APPEND FLOAT_TYPES "COMPLEX") # defines COMPLEX
103 endif ()
104
105 if (BUILD_COMPLEX16)
106   message(STATUS "Building Double Complex Precision")
107   list(APPEND FLOAT_TYPES "ZCOMPLEX") # defines COMPLEX and DOUBLE
108 endif ()
109
110 set(SUBDIRS_ALL ${SUBDIRS} test ctest utest exports benchmark ../laswp ../bench)
111
112 # all :: libs netlib tests shared
113
114 # libs :
115 if (NOT DEFINED CORE OR "${CORE}" STREQUAL "UNKNOWN")
116   message(FATAL_ERROR "Detecting CPU failed. Please set TARGET explicitly, e.g. make TARGET=your_cpu_target. Please read README for details.")
117 endif ()
118
119 if (${NO_STATIC} AND ${NO_SHARED})
120   message(FATAL_ERROR "Neither static nor shared are enabled.")
121 endif ()
122
123 #Set default output directory
124 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
125 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
126 if(MSVC)
127 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BINARY_DIR}/lib/Debug)
128 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BINARY_DIR}/lib/Release)
129 endif ()
130 # get obj vars into format that add_library likes: $<TARGET_OBJS:objlib> (see http://www.cmake.org/cmake/help/v3.0/command/add_library.html)
131 set(TARGET_OBJS "")
132 foreach (SUBDIR ${SUBDIRS})
133   add_subdirectory(${SUBDIR})
134   string(REPLACE "/" "_" subdir_obj ${SUBDIR})
135   list(APPEND TARGET_OBJS "$<TARGET_OBJECTS:${subdir_obj}>")
136 endforeach ()
137
138 # netlib:
139
140 # Can't just use lapack-netlib's CMake files, since they are set up to search for BLAS, build and install a binary. We just want to build a couple of lib files out of lapack and lapacke.
141 # Not using add_subdirectory here because lapack-netlib already has its own CMakeLists.txt. Instead include a cmake script with the sources we want.
142 if (NOT NOFORTRAN AND NOT NO_LAPACK)
143   include("${PROJECT_SOURCE_DIR}/cmake/lapack.cmake")
144 if (NOT NO_LAPACKE)
145   include("${PROJECT_SOURCE_DIR}/cmake/lapacke.cmake")
146 endif ()
147 endif ()
148
149 # Only generate .def for dll on MSVC and always produce pdb files for debug and release
150 if(MSVC)
151 set(OpenBLAS_DEF_FILE "${PROJECT_BINARY_DIR}/openblas.def")
152 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
153 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /Zi")
154 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
155 endif()
156
157 #ifeq ($(DYNAMIC_ARCH), 1)
158 #       @$(MAKE) -C kernel commonlibs || exit 1
159 #       @for d in $(DYNAMIC_CORE) ; \
160 #       do  $(MAKE) GOTOBLAS_MAKEFILE= -C kernel TARGET_CORE=$$d kernel || exit 1 ;\
161 #       done
162 #       @echo DYNAMIC_ARCH=1 >> Makefile.conf_last
163 #endif
164 if (${DYNAMIC_ARCH})
165   add_subdirectory(kernel)
166   foreach(TARGET_CORE ${DYNAMIC_CORE})
167     message("${TARGET_CORE}")
168     list(APPEND TARGET_OBJS "$<TARGET_OBJECTS:kernel_${TARGET_CORE}>")
169   endforeach()
170 endif ()
171
172
173 # add objects to the openblas lib
174 add_library(${OpenBLAS_LIBNAME} SHARED ${LA_SOURCES} ${LAPACKE_SOURCES} ${TARGET_OBJS} ${OpenBLAS_DEF_FILE})
175
176 include("${PROJECT_SOURCE_DIR}/cmake/export.cmake")
177
178 # Set output for libopenblas
179 set_target_properties( ${OpenBLAS_LIBNAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
180 set_target_properties( ${OpenBLAS_LIBNAME} PROPERTIES LIBRARY_OUTPUT_NAME_DEBUG "${OpenBLAS_LIBNAME}_d")
181
182 foreach (OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES})
183   string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
184   
185   set_target_properties( ${OpenBLAS_LIBNAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${PROJECT_BINARY_DIR}/lib/${OUTPUTCONFIG} )
186   set_target_properties( ${OpenBLAS_LIBNAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${PROJECT_BINARY_DIR}/lib/${OUTPUTCONFIG} )
187   set_target_properties( ${OpenBLAS_LIBNAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${PROJECT_BINARY_DIR}/lib/${OUTPUTCONFIG} )
188 endforeach()
189
190 enable_testing()
191 add_subdirectory(utest)
192
193 if (NOT MSVC)
194         #only build shared library for MSVC
195
196         add_library(${OpenBLAS_LIBNAME}_static STATIC ${LA_SOURCES} ${LAPACKE_SOURCES} ${TARGET_OBJS})
197         set_target_properties(${OpenBLAS_LIBNAME}_static PROPERTIES OUTPUT_NAME ${OpenBLAS_LIBNAME})
198         set_target_properties(${OpenBLAS_LIBNAME}_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
199
200         if(SMP)
201         target_link_libraries(${OpenBLAS_LIBNAME} pthread)
202         target_link_libraries(${OpenBLAS_LIBNAME}_static pthread)
203 endif()
204
205 #build test and ctest
206 add_subdirectory(test)
207 if(NOT NO_CBLAS)
208 add_subdirectory(ctest)
209 endif()
210 endif()
211
212 set_target_properties(${OpenBLAS_LIBNAME} PROPERTIES 
213   VERSION ${OpenBLAS_MAJOR_VERSION}.${OpenBLAS_MINOR_VERSION}
214   SOVERSION ${OpenBLAS_MAJOR_VERSION}
215 )
216
217
218 # TODO: Why is the config saved here? Is this necessary with CMake?
219 #Save the config files for installation
220 #       @cp Makefile.conf Makefile.conf_last
221 #       @cp config.h config_last.h
222 #ifdef QUAD_PRECISION
223 #       @echo "#define QUAD_PRECISION">> config_last.h
224 #endif
225 #ifeq ($(EXPRECISION), 1)
226 #       @echo "#define EXPRECISION">> config_last.h
227 #endif
228 ###
229
230 #ifdef USE_THREAD
231 #       @echo USE_THREAD=$(USE_THREAD) >>  Makefile.conf_last
232 #endif
233 #       @touch lib.grd
234
235 # Install project
236
237 # Install libraries
238 install(TARGETS ${OpenBLAS_LIBNAME}
239         RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
240         ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
241         LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} )
242
243 # Install include files
244         set (GENCONFIG_BIN ${CMAKE_BINARY_DIR}/gen_config_h${CMAKE_EXECUTABLE_SUFFIX})
245
246         execute_process(COMMAND ${GENCONFIG_BIN}
247                                 ${CMAKE_CURRENT_SOURCE_DIR}/config.h
248                                 ${CMAKE_CURRENT_SOURCE_DIR}/openblas_config_template.h
249                         OUTPUT_VARIABLE OPENBLAS_CONFIG_H_CONTENTS)
250
251         file(WRITE ${CMAKE_BINARY_DIR}/openblas_config.tmp "${OPENBLAS_CONFIG_H_CONTENTS}")
252         configure_file(${CMAKE_BINARY_DIR}/openblas_config.tmp ${CMAKE_BINARY_DIR}/openblas_config.h COPYONLY)
253
254         install (FILES ${CMAKE_BINARY_DIR}/openblas_config.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
255
256         message(STATUS "Generating f77blas.h in ${CMAKE_INSTALL_INCLUDEDIR}")
257
258         file(WRITE ${CMAKE_BINARY_DIR}/f77blas.h "")
259         file(APPEND ${CMAKE_BINARY_DIR}/f77blas.h "#ifndef OPENBLAS_F77BLAS_H\n#define OPENBLAS_F77BLAS_H\n#include \"openblas_config.h\"\n")
260         file(READ ${CMAKE_CURRENT_SOURCE_DIR}/common_interface.h COMMON_INTERFACE_H_CONTENTS)
261         file(APPEND ${CMAKE_BINARY_DIR}/f77blas.h "${COMMON_INTERFACE_H_CONTENTS}")
262         file(APPEND ${CMAKE_BINARY_DIR}/f77blas.h "#endif")
263         install (FILES ${CMAKE_BINARY_DIR}/f77blas.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
264
265 if(NOT NO_CBLAS)
266         message (STATUS "Generating cblas.h in ${CMAKE_INSTALL_INCLUDEDIR}")
267
268         file(READ ${CMAKE_CURRENT_SOURCE_DIR}/cblas.h CBLAS_H_CONTENTS)
269         string(REPLACE "common" "openblas_config" CBLAS_H_CONTENTS_NEW "${CBLAS_H_CONTENTS}")
270         file(WRITE ${CMAKE_BINARY_DIR}/cblas.tmp "${CBLAS_H_CONTENTS_NEW}")
271         install (FILES ${CMAKE_BINARY_DIR}/cblas.tmp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} RENAME cblas.h)
272 endif()
273
274 if(NOT NO_LAPACKE)
275         message (STATUS "Copying LAPACKE header files to ${CMAKE_INSTALL_INCLUDEDIR}")
276         add_dependencies( ${OpenBLAS_LIBNAME} genlapacke)
277         FILE(GLOB_RECURSE INCLUDE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/lapack-netlib/LAPACKE/*.h")
278         install (FILES ${INCLUDE_FILES} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
279  
280         ADD_CUSTOM_TARGET(genlapacke
281         COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/lapack-netlib/LAPACKE/include/lapacke_mangling_with_flags.h.in "${CMAKE_BINARY_DIR}/lapacke_mangling.h"
282         )
283         install (FILES ${CMAKE_BINARY_DIR}/lapacke_mangling.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
284 endif()
285  
286 if(NOT MSVC)
287         install (TARGETS ${OpenBLAS_LIBNAME}_static DESTINATION ${CMAKE_INSTALL_LIBDIR})
288 endif()
289
290 include(FindPkgConfig QUIET)
291 if(PKG_CONFIG_FOUND)
292         configure_file(${PROJECT_SOURCE_DIR}/cmake/openblas.pc.in ${PROJECT_BINARY_DIR}/openblas.pc @ONLY)
293         install (FILES ${PROJECT_BINARY_DIR}/openblas.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig/)
294 endif()