GetCpuInfos.cmake 1.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
###########################################################################################
# Berenger Bramas Inria
# This goes with the getCpuInfos.cpp
# This will create one CMAKE value per output option from the cpp file.
# For example the output of the CPP file can be:
# SSE3=TRUE;AVX=FALSE
# Then it will create:
# CPUOPTION_SSE3 = TRUE
# CPUOPTION_AVX = FALSE
#
# The binary should return 0 on success.
###########################################################################################
macro(GetCpuInfos)
# The original CPP file
15
set(GetCpuInfosFile "${PROJECT_SOURCE_DIR}/CMakeModules/getCpuInfos.cpp")
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

# Fatal error if the file does not exist
if(NOT EXISTS ${GetCpuInfosFile})
	message(FATAL_ERROR "The GetCpuInfosFile does not exist (${GetCpuInfosFile})")
endif()

# Compile and execute the file
try_run(RUN_RESULT_VAR COMPILE_RESULT_VAR
          ${CMAKE_BINARY_DIR} ${GetCpuInfosFile}  # [CMAKE_FLAGS <Flags>] [COMPILE_DEFINITIONS <flags>]
          COMPILE_OUTPUT_VARIABLE comp
          RUN_OUTPUT_VARIABLE run)

# If it has successfuly compiled an run
if(COMPILE_RESULT_VAR AND (RUN_RESULT_VAR EQUAL 0) )
	set( CPU_OPTIONS ${run} )
	# For each value
	foreach(optionNode ${run})
		# Get name and value
		string(REPLACE "=" ";" optionNameAndValue ${optionNode})
		list(LENGTH optionNameAndValue optionLength)
		# If we get both
		if(optionLength EQUAL 2)
			list(GET optionNameAndValue 0 optionName)
			list(GET optionNameAndValue 1 optionValue)
			# create cmake variable
			set(CPUOPTION_${optionName} ${optionValue})
		else()
			message(WARNING "GetCpuInfosFile wrong format for ${optionNode}.")
		endif()
	endforeach()
	# output the sentence from the binrary
	message(STATUS "CPUOPTION : ${CPU_OPTIONS}")
else()
	message(WARNING "GetCpuInfosFile did not return correctly.")
endif()

endmacro(GetCpuInfos)