poplar.cmake 3.7 KB
Newer Older
J
jianghaicheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
A
Allen Guo 已提交
14

A
Allen Guo 已提交
15 16
macro(find_popart_version popart_version_file)
  file(READ ${popart_version_file} popart_version_file_content)
17 18 19 20 21 22
  string(
    REGEX
      MATCH
      "(POPART_VERSION_STRING)[ \t\r\n](\")([0-9]+\.[0-9]+\.[0-9]+)(\\+)([A-Za-z0-9_]*)(\")"
      POPART_VERSION
      ${popart_version_file_content})
A
Allen Guo 已提交
23 24 25 26 27 28 29 30 31 32
  string(REPLACE "POPART_VERSION_STRING" "" POPART_VERSION "${POPART_VERSION}")
  string(REPLACE "\"" "" POPART_VERSION "${POPART_VERSION}")
  string(REPLACE " " "" POPART_VERSION "${POPART_VERSION}")
  if(NOT POPART_VERSION)
    set(POPART_VERSION "Unknown version")
  else()
    message(STATUS "Current PopART version is ${POPART_VERSION}")
  endif()
endmacro()

J
jianghaicheng 已提交
33 34 35
if(WITH_IPU)
  set(POPLAR_DIR CACHE PATH "Path to a Poplar install")
  set(POPART_DIR CACHE PATH "Path to a Popart install")
36 37 38 39 40
  set(POPLAR_SDK_DIR
      CACHE
        PATH
        "Path to an extracted SDK archive or to a Poplar & Popart install directory (Will populate POPLAR_DIR and POPART_DIR)"
  )
J
jianghaicheng 已提交
41

A
Allen Guo 已提交
42 43
  # support setting SDK both from environment variable or command line arguments

J
jianghaicheng 已提交
44 45
  if(DEFINED ENV{POPLAR_SDK_DIR})
    set(POPLAR_SDK_DIR $ENV{POPLAR_SDK_DIR})
A
Allen Guo 已提交
46 47
  endif()
  if(EXISTS ${POPLAR_SDK_DIR})
48 49 50 51 52 53 54 55 56
    execute_process(
      COMMAND find ${POPLAR_SDK_DIR}/ -maxdepth 1 -type d -name "popart*"
      OUTPUT_VARIABLE POPART_DIR
      OUTPUT_STRIP_TRAILING_WHITESPACE)
    execute_process(
      COMMAND find ${POPLAR_SDK_DIR}/ -maxdepth 1 -type d -name "poplar-*" -o
              -name "poplar"
      OUTPUT_VARIABLE POPLAR_DIR
      OUTPUT_STRIP_TRAILING_WHITESPACE)
J
jianghaicheng 已提交
57
  endif()
A
Allen Guo 已提交
58 59 60 61 62 63
  if(DEFINED ENV{POPLAR_DIR})
    set(POPLAR_DIR $ENV{POPLAR_DIR})
  endif()
  if(DEFINED ENV{POPART_DIR})
    set(POPART_DIR $ENV{POPART_DIR})
  endif()
J
jianghaicheng 已提交
64 65

  if(EXISTS ${POPLAR_DIR})
A
Allen Guo 已提交
66 67
    message("POPLAR_DIR is ${POPLAR_DIR}")
    if(NOT IS_DIRECTORY "${POPLAR_DIR}")
68 69 70 71
      message(
        FATAL_ERROR
          "Couldn't find a \"poplar\" or \"poplar-*\" folder in '${POPLAR_SDK_DIR}'"
      )
A
Allen Guo 已提交
72
    endif()
J
jianghaicheng 已提交
73 74 75 76 77 78 79
    list(APPEND CMAKE_PREFIX_PATH ${POPLAR_DIR})
    set(ENABLE_POPLAR_CMD "source ${POPLAR_DIR}/enable.sh")
    find_package(poplar REQUIRED)
    include_directories("${POPLAR_DIR}/include")
    link_directories("${POPLAR_DIR}/lib")
  endif()
  if(NOT poplar_FOUND)
80 81 82 83
    message(
      FATAL_ERROR
        "You must provide a path to a Poplar install using -DPOPLAR_DIR=/path/to/popart/build/install"
    )
J
jianghaicheng 已提交
84 85
  endif()
  if(EXISTS ${POPART_DIR})
A
Allen Guo 已提交
86 87
    message("POPART_DIR is ${POPART_DIR}")
    if(NOT IS_DIRECTORY "${POPART_DIR}")
88 89
      message(
        FATAL_ERROR "Couldn't find a \"popart*\" folder in '${POPLAR_SDK_DIR}'")
A
Allen Guo 已提交
90
    endif()
J
jianghaicheng 已提交
91 92 93 94 95 96 97
    list(APPEND CMAKE_PREFIX_PATH ${POPART_DIR})
    set(ENABLE_POPART_CMD "source ${POPART_DIR}/enable.sh")
    find_package(popart REQUIRED COMPONENTS popart-only)
    include_directories("${POPART_DIR}/include")
    link_directories("${POPART_DIR}/lib")
  endif()
  if(NOT popart_FOUND)
98 99 100 101
    message(
      FATAL_ERROR
        "You must provide a path to a Popart build using -DPOPART_DIR=/path/to/popart/build"
    )
J
jianghaicheng 已提交
102
  endif()
A
Allen Guo 已提交
103

A
Allen Guo 已提交
104 105
  find_popart_version("${POPART_DIR}/include/popart/version.hpp")

J
jianghaicheng 已提交
106 107 108
  add_definitions(-DONNX_NAMESPACE=onnx)
  add_custom_target(extern_poplar DEPENDS poplar popart-only)
endif()