вторник, 22 октября 2013 г.

Intel SPMD (ISPC) with CMake

####################
# add it to your CMakeLists.txt
#################### ISPC

# given an ispc file, append it and the generated .h file to 'outList'
macro( AddIspcSrc outList outListObj srcLocationsList )
  file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/CMakeFiles/ispc)

  foreach(ispc_file ${srcLocationsList})
    message("ISPC source file " ${ispc_file})
    set(srcLocation2 ${ispc_file})
    find_file(srcLocation2 NAMES ${ispc_file} PATHS ${CMAKE_CURRENT_SOURCE_DIR} )
    get_source_file_property(srcLocation2 ${srcLocation2} LOCATION)
    string(FIND "${ispc_file}" "/" lastSlash REVERSE) #just get the file name, strip the path
    if(NOT ${lastSlash} STREQUAL "-1" )
      math(EXPR lastSlash ${lastSlash}+1)
      string(SUBSTRING "${ispc_file}" ${lastSlash} -1 objLocation)
    else()
      set( objLocation ${ispc_file} )
    endif()
    string(REPLACE ".ispc" ".obj" objLocation ${objLocation})
    set(objLocation ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/CMakeFiles/ispc/${objLocation}) # put the obj in the intermediate dir
    string(REPLACE ".ispc" "_ispc.h" hdrLocation ${ispc_file})

    message("ISPC header " ${hdrLocation})
    message("ISPC object" ${objLocation})
    ADD_CUSTOM_COMMAND(
       OUTPUT ${objLocation}
       COMMAND ispc ${srcLocation2} -o ${objLocation} -h ${hdrLocation} --arch=x86-64 --target=avx1-i32x8
       IMPLICIT_DEPENDS C ${ispc_file}
       DEPENDS ${ispc_file}
       MAIN_DEPENDENCY  ${ispc_file}
       )
    set_source_files_properties( ${objLocation} PROPERTIES GENERATED TRUE EXTERNAL_OBJECT TRUE )
    set_source_files_properties( ${hdrLocation} PROPERTIES GENERATED TRUE EXTERNAL_OBJECT TRUE )
    list( APPEND ${outList} ${srcLocation} ${hdrLocation} )
    list( APPEND ${outListObj} ${objLocation})
  endforeach(ispc_file)
endmacro( AddIspcSrc )

# collect ispc files
file(GLOB_RECURSE ISPC_FILES  ${PROJECT_SOURCE_DIR}/*.ispc)

# call ispc preprocessor, collect *obj files in outListObj
AddIspcSrc( outList outListObj "${ISPC_FILES}")

add_custom_target(ISPC_OBJECTS ALL DEPENDS ${outListObj})


# your exe file
add_executable(${PROJECT_NAME} ${CPP_FILES})

# make a library of all ispc *obj file
add_library( ispc_objs STATIC EXCLUDE_FROM_ALL ${outListObj})
add_dependencies(ispc_objs ISPC_OBJECTS)
set_target_properties(ispc_objs PROPERTIES LINKER_LANGUAGE C)


# link ispc-ed library with your exe
target_link_libraries(${PROJECT_NAME} ispc_objs)

Комментариев нет:

Отправить комментарий