Browse Source

OpenCL bin2h script correction

- The script to turn the source into a bytearray header is no longer a
  function but is instead the body of a script so that it's callable as an
  external cmake command

- Spaces -> Tabs in the touched cmake files
cl-refactor
Lefteris Karapetsas 10 years ago
parent
commit
5390c358b7
  1. 27
      libethash-cl/CMakeLists.txt
  2. 100
      libethash-cl/bin2h.cmake

27
libethash-cl/CMakeLists.txt

@ -1,22 +1,23 @@
set(EXECUTABLE ethash-cl) set(EXECUTABLE ethash-cl)
include(bin2h.cmake) # A custom command and target to turn the OpenCL kernel into a byte array header
bin2h(SOURCE_FILE ethash_cl_miner_kernel.cl # The normal build depends on it properly and if the kernel file is changed, then
VARIABLE_NAME ethash_cl_miner_kernel # a rebuild of libethash-cl should be triggered
HEADER_FILE ${CMAKE_CURRENT_BINARY_DIR}/ethash_cl_miner_kernel.h) add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/ethash_cl_miner_kernel.h
# Add a custom command so that the user can regenerate the header file containing the kernel COMMAND ${CMAKE_COMMAND} ARGS
# code's bytearray by running "make clbin2h" -DBIN2H_SOURCE_FILE="${CMAKE_CURRENT_SOURCE_DIR}/ethash_cl_miner_kernel.cl"
add_custom_target(clbin2h)
add_custom_command(TARGET clbin2h
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -DBIN2H_SOURCE_FILE="${CMAKE_CURRENT_SOURCE_DIR}/ethash_cl_miner_kernel.cl"
-DBIN2H_VARIABLE_NAME=ethash_cl_miner_kernel -DBIN2H_VARIABLE_NAME=ethash_cl_miner_kernel
-DBIN2H_HEADER_FILE="${CMAKE_CURRENT_BINARY_DIR}/ethash_cl_miner_kernel.h" -DBIN2H_HEADER_FILE="${CMAKE_CURRENT_BINARY_DIR}/ethash_cl_miner_kernel.h"
-P "${CMAKE_CURRENT_SOURCE_DIR}/bin2h.cmake") -P "${CMAKE_CURRENT_SOURCE_DIR}/bin2h.cmake"
COMMENT "Generating OpenCL Kernel Byte Array"
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/ethash_cl_miner_kernel.cl
)
add_custom_target(clbin2h DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/ethash_cl_miner_kernel.h ${CMAKE_CURRENT_SOURCE_DIR}/ethash_cl_miner_kernel.cl)
aux_source_directory(. SRC_LIST) aux_source_directory(. SRC_LIST)
file(GLOB HEADERS "*.h") file(GLOB OUR_HEADERS "*.h")
set(HEADERS ${OUR_HEADERS} ${CMAKE_CURRENT_BINARY_DIR}/ethash_cl_miner_kernel.h)
include_directories(${CMAKE_CURRENT_BINARY_DIR}) include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${OpenCL_INCLUDE_DIRS}) include_directories(${OpenCL_INCLUDE_DIRS})

100
libethash-cl/bin2h.cmake

@ -6,31 +6,31 @@ include(CMakeParseArguments)
# VARIABLE - The name of the CMake variable holding the string. # VARIABLE - The name of the CMake variable holding the string.
# AT_COLUMN - The column position at which string will be wrapped. # AT_COLUMN - The column position at which string will be wrapped.
function(WRAP_STRING) function(WRAP_STRING)
set(oneValueArgs VARIABLE AT_COLUMN) set(oneValueArgs VARIABLE AT_COLUMN)
cmake_parse_arguments(WRAP_STRING "${options}" "${oneValueArgs}" "" ${ARGN}) cmake_parse_arguments(WRAP_STRING "${options}" "${oneValueArgs}" "" ${ARGN})
string(LENGTH ${${WRAP_STRING_VARIABLE}} stringLength) string(LENGTH ${${WRAP_STRING_VARIABLE}} stringLength)
math(EXPR offset "0") math(EXPR offset "0")
while(stringLength GREATER 0) while(stringLength GREATER 0)
if(stringLength GREATER ${WRAP_STRING_AT_COLUMN}) if(stringLength GREATER ${WRAP_STRING_AT_COLUMN})
math(EXPR length "${WRAP_STRING_AT_COLUMN}") math(EXPR length "${WRAP_STRING_AT_COLUMN}")
else() else()
math(EXPR length "${stringLength}") math(EXPR length "${stringLength}")
endif() endif()
string(SUBSTRING ${${WRAP_STRING_VARIABLE}} ${offset} ${length} line) string(SUBSTRING ${${WRAP_STRING_VARIABLE}} ${offset} ${length} line)
set(lines "${lines}\n${line}") set(lines "${lines}\n${line}")
math(EXPR stringLength "${stringLength} - ${length}") math(EXPR stringLength "${stringLength} - ${length}")
math(EXPR offset "${offset} + ${length}") math(EXPR offset "${offset} + ${length}")
endwhile() endwhile()
set(${WRAP_STRING_VARIABLE} "${lines}" PARENT_SCOPE) set(${WRAP_STRING_VARIABLE} "${lines}" PARENT_SCOPE)
endfunction() endfunction()
# Function to embed contents of a file as byte array in C/C++ header file(.h). The header file # Script to embed contents of a file as byte array in C/C++ header file(.h). The header file
# will contain a byte array and integer variable holding the size of the array. # will contain a byte array and integer variable holding the size of the array.
# Parameters # Parameters
# SOURCE_FILE - The path of source file whose contents will be embedded in the header file. # SOURCE_FILE - The path of source file whose contents will be embedded in the header file.
@ -42,45 +42,41 @@ endfunction()
# useful if the source file is a text file and we want to use the file contents # useful if the source file is a text file and we want to use the file contents
# as string. But the size variable holds size of the byte array without this # as string. But the size variable holds size of the byte array without this
# null byte. # null byte.
# Usage: set(options APPEND NULL_TERMINATE)
# bin2h(SOURCE_FILE "Logo.png" HEADER_FILE "Logo.h" VARIABLE_NAME "LOGO_PNG") set(oneValueArgs SOURCE_FILE VARIABLE_NAME HEADER_FILE)
function(BIN2H) # cmake_parse_arguments(BIN2H "${options}" "${oneValueArgs}" "" ${ARGN})
set(options APPEND NULL_TERMINATE)
set(oneValueArgs SOURCE_FILE VARIABLE_NAME HEADER_FILE)
cmake_parse_arguments(BIN2H "${options}" "${oneValueArgs}" "" ${ARGN})
# reads source file contents as hex string # reads source file contents as hex string
file(READ ${BIN2H_SOURCE_FILE} hexString HEX) file(READ ${BIN2H_SOURCE_FILE} hexString HEX)
string(LENGTH ${hexString} hexStringLength) string(LENGTH ${hexString} hexStringLength)
# appends null byte if asked # appends null byte if asked
if(BIN2H_NULL_TERMINATE) if(BIN2H_NULL_TERMINATE)
set(hexString "${hexString}00") set(hexString "${hexString}00")
endif() endif()
# wraps the hex string into multiple lines at column 32(i.e. 16 bytes per line) # wraps the hex string into multiple lines at column 32(i.e. 16 bytes per line)
wrap_string(VARIABLE hexString AT_COLUMN 32) wrap_string(VARIABLE hexString AT_COLUMN 32)
math(EXPR arraySize "${hexStringLength} / 2") math(EXPR arraySize "${hexStringLength} / 2")
# adds '0x' prefix and comma suffix before and after every byte respectively # adds '0x' prefix and comma suffix before and after every byte respectively
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1, " arrayValues ${hexString}) string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1, " arrayValues ${hexString})
# removes trailing comma # removes trailing comma
string(REGEX REPLACE ", $" "" arrayValues ${arrayValues}) string(REGEX REPLACE ", $" "" arrayValues ${arrayValues})
# converts the variable name into proper C identifier # converts the variable name into proper C identifier
IF (${CMAKE_VERSION} GREATER 2.8.10) # fix for legacy cmake IF (${CMAKE_VERSION} GREATER 2.8.10) # fix for legacy cmake
string(MAKE_C_IDENTIFIER "${BIN2H_VARIABLE_NAME}" BIN2H_VARIABLE_NAME) string(MAKE_C_IDENTIFIER "${BIN2H_VARIABLE_NAME}" BIN2H_VARIABLE_NAME)
ENDIF() ENDIF()
string(TOUPPER "${BIN2H_VARIABLE_NAME}" BIN2H_VARIABLE_NAME) string(TOUPPER "${BIN2H_VARIABLE_NAME}" BIN2H_VARIABLE_NAME)
# declares byte array and the length variables # declares byte array and the length variables
set(arrayDefinition "const unsigned char ${BIN2H_VARIABLE_NAME}[] = { ${arrayValues} };") set(arrayDefinition "const unsigned char ${BIN2H_VARIABLE_NAME}[] = { ${arrayValues} };")
set(arraySizeDefinition "const size_t ${BIN2H_VARIABLE_NAME}_SIZE = ${arraySize};") set(arraySizeDefinition "const size_t ${BIN2H_VARIABLE_NAME}_SIZE = ${arraySize};")
set(declarations "${arrayDefinition}\n\n${arraySizeDefinition}\n\n") set(declarations "${arrayDefinition}\n\n${arraySizeDefinition}\n\n")
if(BIN2H_APPEND) if(BIN2H_APPEND)
file(APPEND ${BIN2H_HEADER_FILE} "${declarations}") file(APPEND ${BIN2H_HEADER_FILE} "${declarations}")
else() else()
file(WRITE ${BIN2H_HEADER_FILE} "${declarations}") file(WRITE ${BIN2H_HEADER_FILE} "${declarations}")
endif() endif()
endfunction()

Loading…
Cancel
Save