c3fa486552c01df4bad5e474acf37e13d9a53709
[platform/upstream/openblas.git] / cmake / prebuild.cmake
1 ##
2 ## Author: Hank Anderson <hank@statease.com>
3 ## Description: Ported from OpenBLAS/Makefile.prebuild
4 ##              This is triggered by system.cmake and runs before any of the code is built.
5 ##              Creates config.h and Makefile.conf by first running the c_check perl script (which creates those files).
6 ##              Next it runs f_check and appends some fortran information to the files.
7 ##              Finally it runs getarch and getarch_2nd for even more environment information.
8
9 # CMake vars set by this file:
10 # CORE
11 # LIBCORE
12 # NUM_CORES
13 # HAVE_MMX
14 # HAVE_SSE
15 # HAVE_SSE2
16 # HAVE_SSE3
17 # MAKE
18 # SGEMM_UNROLL_M
19 # SGEMM_UNROLL_N
20 # DGEMM_UNROLL_M
21 # DGEMM_UNROLL_M
22 # QGEMM_UNROLL_N
23 # QGEMM_UNROLL_N
24 # CGEMM_UNROLL_M
25 # CGEMM_UNROLL_M
26 # ZGEMM_UNROLL_N
27 # ZGEMM_UNROLL_N
28 # XGEMM_UNROLL_M
29 # XGEMM_UNROLL_N
30 # CGEMM3M_UNROLL_M
31 # CGEMM3M_UNROLL_N
32 # ZGEMM3M_UNROLL_M
33 # ZGEMM3M_UNROLL_M
34 # XGEMM3M_UNROLL_N
35 # XGEMM3M_UNROLL_N
36
37 # CPUIDEMU = ../../cpuid/table.o
38
39 if (DEFINED CPUIDEMU)
40   set(EXFLAGS "-DCPUIDEMU -DVENDOR=99")
41 endif ()
42
43 if (DEFINED TARGET_CORE)
44   # set the C flags for just this file
45   set(GETARCH2_FLAGS "-DBUILD_KERNEL")
46   set(TARGET_MAKE "Makefile_kernel.conf")
47   set(TARGET_CONF "config_kernel.h")
48 else()
49   set(TARGET_MAKE "Makefile.conf")
50   set(TARGET_CONF "config.h")
51 endif ()
52
53 include("${CMAKE_SOURCE_DIR}/cmake/c_check.cmake")
54
55 if (NOT NOFORTRAN)
56   include("${CMAKE_SOURCE_DIR}/cmake/f_check.cmake")
57 endif ()
58
59 # compile getarch
60 set(GETARCH_SRC
61   ${CMAKE_SOURCE_DIR}/getarch.c
62   ${CPUIDEMO}
63 )
64
65 if (NOT MSVC)
66   list(APPEND GETARCH_SRC ${CMAKE_SOURCE_DIR}/cpuid.S)
67 endif ()
68
69 if (MSVC)
70 #Use generic for MSVC now
71 set(GETARCH_FLAGS ${GETARCH_FLAGS} -DFORCE_GENERIC)
72 endif()
73
74 set(GETARCH_DIR "${PROJECT_BINARY_DIR}/getarch_build")
75 set(GETARCH_BIN "getarch${CMAKE_EXECUTABLE_SUFFIX}")
76 file(MAKE_DIRECTORY ${GETARCH_DIR})
77 try_compile(GETARCH_RESULT ${GETARCH_DIR}
78   SOURCES ${GETARCH_SRC}
79   COMPILE_DEFINITIONS ${EXFLAGS} ${GETARCH_FLAGS} -I${CMAKE_SOURCE_DIR}
80   OUTPUT_VARIABLE GETARCH_LOG
81   COPY_FILE ${PROJECT_BINARY_DIR}/${GETARCH_BIN}
82 )
83
84 message(STATUS "Running getarch")
85
86 # use the cmake binary w/ the -E param to run a shell command in a cross-platform way
87 execute_process(COMMAND ${PROJECT_BINARY_DIR}/${GETARCH_BIN} 0 OUTPUT_VARIABLE GETARCH_MAKE_OUT)
88 execute_process(COMMAND ${PROJECT_BINARY_DIR}/${GETARCH_BIN} 1 OUTPUT_VARIABLE GETARCH_CONF_OUT)
89
90 message(STATUS "GETARCH results:\n${GETARCH_MAKE_OUT}")
91
92 # append config data from getarch to the TARGET file and read in CMake vars
93 file(APPEND ${TARGET_CONF} ${GETARCH_CONF_OUT})
94 ParseGetArchVars(${GETARCH_MAKE_OUT})
95
96 set(GETARCH2_DIR "${PROJECT_BINARY_DIR}/getarch2_build")
97 set(GETARCH2_BIN "getarch_2nd${CMAKE_EXECUTABLE_SUFFIX}")
98 file(MAKE_DIRECTORY ${GETARCH2_DIR})
99 try_compile(GETARCH2_RESULT ${GETARCH2_DIR}
100   SOURCES ${CMAKE_SOURCE_DIR}/getarch_2nd.c
101   COMPILE_DEFINITIONS ${EXFLAGS} ${GETARCH_FLAGS} ${GETARCH2_FLAGS} -I${CMAKE_SOURCE_DIR}
102   OUTPUT_VARIABLE GETARCH2_LOG
103   COPY_FILE ${PROJECT_BINARY_DIR}/${GETARCH2_BIN}
104 )
105
106 # use the cmake binary w/ the -E param to run a shell command in a cross-platform way
107 execute_process(COMMAND ${PROJECT_BINARY_DIR}/${GETARCH2_BIN} 0 OUTPUT_VARIABLE GETARCH2_MAKE_OUT)
108 execute_process(COMMAND ${PROJECT_BINARY_DIR}/${GETARCH2_BIN} 1 OUTPUT_VARIABLE GETARCH2_CONF_OUT)
109
110 # append config data from getarch_2nd to the TARGET file and read in CMake vars
111 file(APPEND ${TARGET_CONF} ${GETARCH2_CONF_OUT})
112 ParseGetArchVars(${GETARCH2_MAKE_OUT})
113