aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/cmake/external/tensorboard.cmake
blob: 457868c72b74a7a20a91d27372e9b128dfc7cf61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Copyright 2017 The TensorFlow 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.
# ==============================================================================
include (ExternalProject)

set(tensorboard_dependencies)
add_custom_target(tensorboard_copy_dependencies)

function(tb_new_http_archive)
  cmake_parse_arguments(_TB "" "NAME;URL" "FILES" ${ARGN})
  ExternalProject_Add(${_TB_NAME}
    PREFIX ${_TB_NAME}
    URL ${_TB_URL}
    DOWNLOAD_DIR "${DOWNLOAD_LOCATION}/${_TB_NAME}"
    CONFIGURE_COMMAND ""
    BUILD_COMMAND ""
    INSTALL_COMMAND ""
  )

  set(src_dir "${CMAKE_CURRENT_BINARY_DIR}/${_TB_NAME}/src/${_TB_NAME}")
  set(dst_dir "${CMAKE_CURRENT_BINARY_DIR}/tensorboard_external/${_TB_NAME}")

  foreach(src_file ${_TB_FILES})
    add_custom_command(
      TARGET tensorboard_copy_dependencies PRE_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_dir}/${src_file} ${dst_dir}/${src_file}
    )
  endforeach()
  
  set(tensorboard_dependencies ${tensorboard_dependencies} ${_TB_NAME} PARENT_SCOPE)
endfunction()

function(tb_http_file)
  cmake_parse_arguments(_TB "" "NAME;URL" "" ${ARGN})
  get_filename_component(src_file ${_TB_URL} NAME)
  file(DOWNLOAD ${_TB_URL} "${DOWNLOAD_LOCATION}/${_TB_NAME}/${src_file}")
  
  set(src_dir "${DOWNLOAD_LOCATION}/${_TB_NAME}")
  set(dst_dir "${CMAKE_CURRENT_BINARY_DIR}/tensorboard_external/${_TB_NAME}/file")
  
  add_custom_command(
    TARGET tensorboard_copy_dependencies PRE_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_dir}/${src_file} ${dst_dir}/${src_file}
  )
  
  add_custom_target(${_TB_NAME} DEPENDS ${src_dir}/${src_file})
  set(tensorboard_dependencies ${tensorboard_dependencies} ${_TB_NAME} PARENT_SCOPE)
endfunction()

# Parse TensorBoard dependency names and URLs from Bazel's WORKSPACE file.
set(tb_dep_names)
file(STRINGS ${PROJECT_SOURCE_DIR}/../../../WORKSPACE workspace_contents)
foreach(line ${workspace_contents})
  if(line MATCHES "# TENSORBOARD_BOWER_AUTOGENERATED_BELOW_THIS_LINE_DO_NOT_EDIT")
    set(tb_deps_started 1)
  endif()

  if(NOT tb_deps_started)
    continue()
  endif()

  if(line MATCHES "new_http_archive\\(")
    set(tb_dep_is_archive 1)
    continue()
  elseif(line MATCHES "http_file\\(")
    set(tb_dep_is_archive 0)
    continue()
  endif()

  string(REGEX MATCH "name.*=.*\"(.*)\"" has_name ${line})
  if(has_name)
    set(tb_dep_name ${CMAKE_MATCH_1})
    continue()
  endif()

  string(REGEX MATCH "url.*=.*\"(.*)\"" has_url ${line})
  if(has_url)
    list(APPEND tb_dep_names ${tb_dep_name})
    set(${tb_dep_name}_is_archive ${tb_dep_is_archive})
    set(${tb_dep_name}_url ${CMAKE_MATCH_1})
  endif()
endforeach()

# Parse the files needed for each TensorBoard dependency from Bazel's bower.BUILD file.
# Due to CMAKE quirkiness, cannot use file(strings) with files that contain '[' and ']'.
file(READ ${PROJECT_SOURCE_DIR}/../../../bower.BUILD bower_build_contents)
string(REPLACE "\[" "OB" bower_build_contents "${bower_build_contents}")
string(REPLACE "\]" "CB" bower_build_contents "${bower_build_contents}")
string(REPLACE ";" "\\\\;" bower_build_contents "${bower_build_contents}")
string(REPLACE "\n" "E;" bower_build_contents "${bower_build_contents}")
foreach(line ${bower_build_contents})
  string(REGEX MATCH "name.*=.*\"(.*)\"" has_name ${line})
  if(has_name)
    set(tb_dep_name ${CMAKE_MATCH_1})
    set(${tb_dep_name}_files)
    continue()
  endif()

  string(REGEX MATCH "srcs.*=.*\"(.*)\"CB" has_single_line_src ${line})
  if(has_single_line_src)
    list(APPEND ${tb_dep_name}_files ${CMAKE_MATCH_1})
    continue()
  endif()

  if(line MATCHES "srcs.*=.*OB")
    set(inside_files_def 1)
    continue()
  elseif(line MATCHES "CB,")
    set(inside_files_def 0)
    continue()
  endif()

  if(inside_files_def)
   string(REGEX MATCH "\"(.*)\"," has_file ${line})
   if(has_file)
     list(APPEND ${tb_dep_name}_files ${CMAKE_MATCH_1})
   endif()
  endif()
endforeach()

# Generate a target for each dependency.
foreach(tb_dep_name ${tb_dep_names})
  if (${tb_dep_name}_is_archive)
    tb_new_http_archive(
      NAME ${tb_dep_name}
      URL ${${tb_dep_name}_url}
      FILES ${${tb_dep_name}_files}
    )
  else()
    tb_http_file(
      NAME ${tb_dep_name}
      URL ${${tb_dep_name}_url}
    )
  endif()
endforeach()

add_dependencies(tensorboard_copy_dependencies ${tensorboard_dependencies})