summaryrefslogtreecommitdiffstats
path: root/Modules/Platform/Android-Determine.cmake
blob: ef97c230484a7801a240fb3872fe089f52c2c8e7 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

# When CMAKE_SYSTEM_NAME is "Android", CMakeDetermineSystem loads this module.
# This module detects platform-wide information about the Android target
# in order to store it in "CMakeSystem.cmake".

# Support for NVIDIA Nsight Tegra Visual Studio Edition was previously
# implemented in the CMake VS IDE generators.  Avoid interfering with
# that functionality for now.
if(CMAKE_GENERATOR_PLATFORM STREQUAL "Tegra-Android")
  return()
endif()

# Commonly used Android toolchain files that pre-date CMake upstream support
# set CMAKE_SYSTEM_VERSION to 1.  Avoid interfering with them.
if(CMAKE_SYSTEM_VERSION EQUAL 1)
  return()
endif()

# Natively compiling on an Android host doesn't use the NDK cross-compilation
# tools.
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Android")
  return()
endif()

cmake_policy(PUSH)
cmake_policy(SET CMP0057 NEW) # if IN_LIST

# If using Android tools for Visual Studio, compile a sample project to get the
# sysroot.
if(CMAKE_GENERATOR MATCHES "Visual Studio")
  if(NOT CMAKE_SYSROOT)
    set(vcx_platform ${CMAKE_GENERATOR_PLATFORM})
    if(CMAKE_GENERATOR MATCHES "Visual Studio 1[45]")
      set(vcx_sysroot_var "Sysroot")
    else()
      set(vcx_sysroot_var "SysrootLink")
    endif()
    if(CMAKE_GENERATOR MATCHES "Visual Studio 14")
      set(vcx_revision "2.0")
    elseif(CMAKE_GENERATOR MATCHES "Visual Studio 1[56]")
      set(vcx_revision "3.0")
    else()
      set(vcx_revision "")
    endif()
    configure_file(${CMAKE_ROOT}/Modules/Platform/Android/VCXProjInspect.vcxproj.in
      ${CMAKE_PLATFORM_INFO_DIR}/VCXProjInspect.vcxproj @ONLY)
    execute_process(
      COMMAND "${CMAKE_VS_MSBUILD_COMMAND}" "VCXProjInspect.vcxproj"
        "/p:Configuration=Debug" "/p:Platform=${vcx_platform}"
      WORKING_DIRECTORY ${CMAKE_PLATFORM_INFO_DIR}
      OUTPUT_VARIABLE VCXPROJ_INSPECT_OUTPUT
      ERROR_VARIABLE VCXPROJ_INSPECT_OUTPUT
      RESULT_VARIABLE VCXPROJ_INSPECT_RESULT
      )
    if(NOT CMAKE_SYSROOT AND VCXPROJ_INSPECT_OUTPUT MATCHES "CMAKE_SYSROOT=([^%\r\n]+)[\r\n]")
      # Strip VS diagnostic output from the end of the line.
      string(REGEX REPLACE " \\(TaskId:[0-9]*\\)$" "" _sysroot "${CMAKE_MATCH_1}")
      if(EXISTS "${_sysroot}")
        file(TO_CMAKE_PATH "${_sysroot}" CMAKE_SYSROOT)
      endif()
    endif()
    if(VCXPROJ_INSPECT_RESULT)
      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
        "Determining the sysroot for the Android NDK failed.
The output was:
${VCXPROJ_INSPECT_RESULT}
${VCXPROJ_INSPECT_OUTPUT}

")
    else()
      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
        "Determining the sysroot for the Android NDK succeeded.
The output was:
${VCXPROJ_INSPECT_RESULT}
${VCXPROJ_INSPECT_OUTPUT}

")
    endif()
  endif()
  if(NOT CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION)
    set(CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION "clang")
  endif()
endif()

# If the user provided CMAKE_SYSROOT for us, extract information from it.
set(_ANDROID_SYSROOT_NDK "")
set(_ANDROID_SYSROOT_API "")
set(_ANDROID_SYSROOT_ARCH "")
set(_ANDROID_SYSROOT_STANDALONE_TOOLCHAIN "")
if(CMAKE_SYSROOT)
  if(NOT IS_DIRECTORY "${CMAKE_SYSROOT}")
    message(FATAL_ERROR
      "Android: The specified CMAKE_SYSROOT:\n"
      "  ${CMAKE_SYSROOT}\n"
      "is not an existing directory."
      )
  endif()
  if(CMAKE_SYSROOT MATCHES "^([^\\\n]*)/platforms/android-([0-9]+)/arch-([a-z0-9_]+)$")
    set(_ANDROID_SYSROOT_NDK "${CMAKE_MATCH_1}")
    set(_ANDROID_SYSROOT_API "${CMAKE_MATCH_2}")
    set(_ANDROID_SYSROOT_ARCH "${CMAKE_MATCH_3}")
  elseif(CMAKE_SYSROOT MATCHES "^([^\\\n]*)/sysroot$")
    set(_ANDROID_SYSROOT_STANDALONE_TOOLCHAIN "${CMAKE_MATCH_1}")
  else()
    message(FATAL_ERROR
      "The value of CMAKE_SYSROOT:\n"
      "  ${CMAKE_SYSROOT}\n"
      "does not match any of the forms:\n"
      "  <ndk>/platforms/android-<api>/arch-<arch>\n"
      "  <standalone-toolchain>/sysroot\n"
      "where:\n"
      "  <ndk>  = Android NDK directory (with forward slashes)\n"
      "  <api>  = Android API version number (decimal digits)\n"
      "  <arch> = Android ARCH name (lower case)\n"
      "  <standalone-toolchain> = Path to standalone toolchain prefix\n"
      )
  endif()
endif()

# Find the Android NDK.
if(CMAKE_ANDROID_NDK)
  if(NOT IS_DIRECTORY "${CMAKE_ANDROID_NDK}")
    message(FATAL_ERROR
      "Android: The NDK root directory specified by CMAKE_ANDROID_NDK:\n"
      "  ${CMAKE_ANDROID_NDK}\n"
      "does not exist."
      )
  endif()
elseif(CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
  if(NOT IS_DIRECTORY "${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}")
    message(FATAL_ERROR
      "Android: The standalone toolchain directory specified by CMAKE_ANDROID_STANDALONE_TOOLCHAIN:\n"
      "  ${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}\n"
      "does not exist."
      )
  endif()
  if(NOT EXISTS "${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/sysroot/usr/include/android/api-level.h")
    message(FATAL_ERROR
      "Android: The standalone toolchain directory specified by CMAKE_ANDROID_STANDALONE_TOOLCHAIN:\n"
      "  ${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}\n"
      "does not contain a sysroot with a known layout.  The file:\n"
      "  ${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/sysroot/usr/include/android/api-level.h\n"
      "does not exist."
      )
  endif()
else()
  if(IS_DIRECTORY "${_ANDROID_SYSROOT_NDK}")
    set(CMAKE_ANDROID_NDK "${_ANDROID_SYSROOT_NDK}")
  elseif(IS_DIRECTORY "${_ANDROID_SYSROOT_STANDALONE_TOOLCHAIN}")
    set(CMAKE_ANDROID_STANDALONE_TOOLCHAIN "${_ANDROID_SYSROOT_STANDALONE_TOOLCHAIN}")
  elseif(IS_DIRECTORY "${ANDROID_NDK}")
    file(TO_CMAKE_PATH "${ANDROID_NDK}" CMAKE_ANDROID_NDK)
  elseif(IS_DIRECTORY "${ANDROID_STANDALONE_TOOLCHAIN}")
    file(TO_CMAKE_PATH "${ANDROID_STANDALONE_TOOLCHAIN}" CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
  elseif(IS_DIRECTORY "$ENV{ANDROID_NDK_ROOT}")
    file(TO_CMAKE_PATH "$ENV{ANDROID_NDK_ROOT}" CMAKE_ANDROID_NDK)
  elseif(IS_DIRECTORY "$ENV{ANDROID_NDK}")
    file(TO_CMAKE_PATH "$ENV{ANDROID_NDK}" CMAKE_ANDROID_NDK)
  elseif(IS_DIRECTORY "$ENV{ANDROID_STANDALONE_TOOLCHAIN}")
    file(TO_CMAKE_PATH "$ENV{ANDROID_STANDALONE_TOOLCHAIN}" CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
  endif()
  # TODO: Search harder for the NDK or standalone toolchain.
endif()

set(_ANDROID_STANDALONE_TOOLCHAIN_API "")
if(CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
  # Try to read the API level from the toolchain launcher.
  if(EXISTS "${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/bin/clang")
    set(_ANDROID_API_LEVEL_CLANG_REGEX "__ANDROID_API__=([0-9]+)")
    file(STRINGS "${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/bin/clang" _ANDROID_STANDALONE_TOOLCHAIN_BIN_CLANG
      REGEX "${_ANDROID_API_LEVEL_CLANG_REGEX}" LIMIT_COUNT 1 LIMIT_INPUT 65536)
    if(_ANDROID_STANDALONE_TOOLCHAIN_BIN_CLANG MATCHES "${_ANDROID_API_LEVEL_CLANG_REGEX}")
      set(_ANDROID_STANDALONE_TOOLCHAIN_API "${CMAKE_MATCH_1}")
    endif()
    unset(_ANDROID_STANDALONE_TOOLCHAIN_BIN_CLANG)
    unset(_ANDROID_API_LEVEL_CLANG_REGEX)
  endif()
  if(NOT _ANDROID_STANDALONE_TOOLCHAIN_API)
    # The compiler launcher does not know __ANDROID_API__.  Assume this
    # is not unified headers and look for it in the api-level.h header.
    set(_ANDROID_API_LEVEL_H_REGEX "^[\t ]*#[\t ]*define[\t ]+__ANDROID_API__[\t ]+([0-9]+)")
    file(STRINGS "${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/sysroot/usr/include/android/api-level.h"
      _ANDROID_API_LEVEL_H_CONTENT REGEX "${_ANDROID_API_LEVEL_H_REGEX}")
    if(_ANDROID_API_LEVEL_H_CONTENT MATCHES "${_ANDROID_API_LEVEL_H_REGEX}")
      set(_ANDROID_STANDALONE_TOOLCHAIN_API "${CMAKE_MATCH_1}")
    endif()
  endif()
  if(NOT _ANDROID_STANDALONE_TOOLCHAIN_API)
    message(WARNING
      "Android: Did not detect API level from\n"
      "  ${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/bin/clang\n"
      "or\n"
      "  ${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/sysroot/usr/include/android/api-level.h\n"
      )
  endif()
endif()

if(NOT CMAKE_ANDROID_NDK AND NOT CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
  message(FATAL_ERROR "Android: Neither the NDK or a standalone toolchain was found.")
endif()

if(CMAKE_ANDROID_NDK)
  # Identify the host platform.
  if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
    if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
      set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "darwin-x86_64")
    else()
      set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "darwin-x86")
    endif()
  elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
    if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
      set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "linux-x86_64")
    else()
      set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "linux-x86")
    endif()
  elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
    if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "AMD64")
      set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "windows-x86_64")
    else()
      set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "windows")
    endif()
  else()
    message(FATAL_ERROR "Android: Builds hosted on '${CMAKE_HOST_SYSTEM_NAME}' not supported.")
  endif()

  # Look for a unified toolchain/sysroot provided with the NDK.
  set(CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED "${CMAKE_ANDROID_NDK}/toolchains/llvm/prebuilt/${CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG}")
  if(NOT IS_DIRECTORY "${CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED}/sysroot")
    set(CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED "")
  endif()
else()
  set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "")
  set(CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED "")
endif()

# https://developer.android.com/ndk/guides/abis.html

set(_ANDROID_ABI_arm64-v8a_PROC     "aarch64")
set(_ANDROID_ABI_arm64-v8a_ARCH     "arm64")
set(_ANDROID_ABI_arm64-v8a_TRIPLE   "aarch64-linux-android")
set(_ANDROID_ABI_armeabi-v7a_PROC   "armv7-a")
set(_ANDROID_ABI_armeabi-v7a_ARCH   "arm")
set(_ANDROID_ABI_armeabi-v7a_TRIPLE "arm-linux-androideabi")
set(_ANDROID_ABI_armeabi-v6_PROC    "armv6")
set(_ANDROID_ABI_armeabi-v6_ARCH    "arm")
set(_ANDROID_ABI_armeabi-v6_TRIPLE  "arm-linux-androideabi")
set(_ANDROID_ABI_armeabi_PROC       "armv5te")
set(_ANDROID_ABI_armeabi_ARCH       "arm")
set(_ANDROID_ABI_armeabi_TRIPLE     "arm-linux-androideabi")
set(_ANDROID_ABI_mips_PROC          "mips")
set(_ANDROID_ABI_mips_ARCH          "mips")
set(_ANDROID_ABI_mips_TRIPLE        "mipsel-linux-android")
set(_ANDROID_ABI_mips64_PROC        "mips64")
set(_ANDROID_ABI_mips64_ARCH        "mips64")
set(_ANDROID_ABI_mips64_TRIPLE      "mips64el-linux-android")
set(_ANDROID_ABI_x86_PROC           "i686")
set(_ANDROID_ABI_x86_ARCH           "x86")
set(_ANDROID_ABI_x86_TRIPLE         "i686-linux-android")
set(_ANDROID_ABI_x86_64_PROC        "x86_64")
set(_ANDROID_ABI_x86_64_ARCH        "x86_64")
set(_ANDROID_ABI_x86_64_TRIPLE      "x86_64-linux-android")

set(_ANDROID_PROC_aarch64_ARCH_ABI "arm64-v8a")
set(_ANDROID_PROC_armv7-a_ARCH_ABI "armeabi-v7a")
set(_ANDROID_PROC_armv6_ARCH_ABI   "armeabi-v6")
set(_ANDROID_PROC_armv5te_ARCH_ABI "armeabi")
set(_ANDROID_PROC_i686_ARCH_ABI    "x86")
set(_ANDROID_PROC_mips_ARCH_ABI    "mips")
set(_ANDROID_PROC_mips64_ARCH_ABI  "mips64")
set(_ANDROID_PROC_x86_64_ARCH_ABI  "x86_64")

set(_ANDROID_ARCH_arm64_ABI  "arm64-v8a")
set(_ANDROID_ARCH_arm_ABI    "armeabi")
set(_ANDROID_ARCH_mips_ABI   "mips")
set(_ANDROID_ARCH_mips64_ABI "mips64")
set(_ANDROID_ARCH_x86_ABI    "x86")
set(_ANDROID_ARCH_x86_64_ABI "x86_64")

# Validate inputs.
if(CMAKE_ANDROID_ARCH_ABI AND NOT DEFINED "_ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_PROC")
  message(FATAL_ERROR "Android: Unknown ABI CMAKE_ANDROID_ARCH_ABI='${CMAKE_ANDROID_ARCH_ABI}'.")
endif()
if(CMAKE_SYSTEM_PROCESSOR AND NOT DEFINED "_ANDROID_PROC_${CMAKE_SYSTEM_PROCESSOR}_ARCH_ABI")
  message(FATAL_ERROR "Android: Unknown processor CMAKE_SYSTEM_PROCESSOR='${CMAKE_SYSTEM_PROCESSOR}'.")
endif()
if(_ANDROID_SYSROOT_ARCH AND NOT DEFINED "_ANDROID_ARCH_${_ANDROID_SYSROOT_ARCH}_ABI")
  message(FATAL_ERROR
    "Android: Unknown architecture '${_ANDROID_SYSROOT_ARCH}' specified in CMAKE_SYSROOT:\n"
    "  ${CMAKE_SYSROOT}"
    )
endif()

# Select an ABI.
if(NOT CMAKE_ANDROID_ARCH_ABI)
  if(CMAKE_SYSTEM_PROCESSOR)
    set(CMAKE_ANDROID_ARCH_ABI "${_ANDROID_PROC_${CMAKE_SYSTEM_PROCESSOR}_ARCH_ABI}")
  elseif(_ANDROID_SYSROOT_ARCH)
    set(CMAKE_ANDROID_ARCH_ABI "${_ANDROID_ARCH_${_ANDROID_SYSROOT_ARCH}_ABI}")
  else()
    # https://developer.android.com/ndk/guides/application_mk.html
    # Default is the oldest ARM ABI.

    # Lookup the available ABIs among all toolchains.
    set(_ANDROID_ABIS "")
    file(GLOB _ANDROID_CONFIG_MKS
      "${CMAKE_ANDROID_NDK}/build/core/toolchains/*/config.mk"
      "${CMAKE_ANDROID_NDK}/toolchains/*/config.mk"
      )
    foreach(config_mk IN LISTS _ANDROID_CONFIG_MKS)
      file(STRINGS "${config_mk}" _ANDROID_TOOL_ABIS REGEX "^TOOLCHAIN_ABIS :=")
      string(REPLACE "TOOLCHAIN_ABIS :=" "" _ANDROID_TOOL_ABIS "${_ANDROID_TOOL_ABIS}")
      separate_arguments(_ANDROID_TOOL_ABIS UNIX_COMMAND "${_ANDROID_TOOL_ABIS}")
      list(APPEND _ANDROID_ABIS ${_ANDROID_TOOL_ABIS})
      unset(_ANDROID_TOOL_ABIS)
    endforeach()
    unset(_ANDROID_CONFIG_MKS)

    # Choose the oldest among the available arm ABIs.
    if(_ANDROID_ABIS)
      list(REMOVE_DUPLICATES _ANDROID_ABIS)
      foreach(abi armeabi armeabi-v7a arm64-v8a)
        if("${abi}" IN_LIST _ANDROID_ABIS)
          set(CMAKE_ANDROID_ARCH_ABI "${abi}")
          break()
        endif()
      endforeach()
    endif()
    unset(_ANDROID_ABIS)

    if(NOT CMAKE_ANDROID_ARCH_ABI)
      set(CMAKE_ANDROID_ARCH_ABI "armeabi")
    endif()
  endif()
endif()
set(CMAKE_ANDROID_ARCH "${_ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_ARCH}")
if(_ANDROID_SYSROOT_ARCH AND NOT "x${_ANDROID_SYSROOT_ARCH}" STREQUAL "x${CMAKE_ANDROID_ARCH}")
  message(FATAL_ERROR
    "Android: Architecture '${_ANDROID_SYSROOT_ARCH}' specified in CMAKE_SYSROOT:\n"
    "  ${CMAKE_SYSROOT}\n"
    "does not match architecture '${CMAKE_ANDROID_ARCH}' for the ABI '${CMAKE_ANDROID_ARCH_ABI}'."
    )
endif()
set(CMAKE_ANDROID_ARCH_TRIPLE "${_ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_TRIPLE}")

# Select a processor.
if(NOT CMAKE_SYSTEM_PROCESSOR)
  set(CMAKE_SYSTEM_PROCESSOR "${_ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_PROC}")
endif()

# If the user specified both an ABI and a processor then they might not match.
if(NOT _ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_PROC STREQUAL CMAKE_SYSTEM_PROCESSOR)
  message(FATAL_ERROR "Android: The specified CMAKE_ANDROID_ARCH_ABI='${CMAKE_ANDROID_ARCH_ABI}' and CMAKE_SYSTEM_PROCESSOR='${CMAKE_SYSTEM_PROCESSOR}' is not a valid combination.")
endif()

# Select an API.
if(CMAKE_SYSTEM_VERSION)
  set(_ANDROID_API_VAR CMAKE_SYSTEM_VERSION)
elseif(CMAKE_ANDROID_API)
  set(CMAKE_SYSTEM_VERSION "${CMAKE_ANDROID_API}")
  set(_ANDROID_API_VAR CMAKE_ANDROID_API)
elseif(_ANDROID_SYSROOT_API)
  set(CMAKE_SYSTEM_VERSION "${_ANDROID_SYSROOT_API}")
  set(_ANDROID_API_VAR CMAKE_SYSROOT)
elseif(_ANDROID_STANDALONE_TOOLCHAIN_API)
  set(CMAKE_SYSTEM_VERSION "${_ANDROID_STANDALONE_TOOLCHAIN_API}")
endif()
if(CMAKE_SYSTEM_VERSION)
  if(CMAKE_ANDROID_API AND NOT "x${CMAKE_ANDROID_API}" STREQUAL "x${CMAKE_SYSTEM_VERSION}")
    message(FATAL_ERROR
      "Android: The API specified by CMAKE_ANDROID_API='${CMAKE_ANDROID_API}' is not consistent with CMAKE_SYSTEM_VERSION='${CMAKE_SYSTEM_VERSION}'."
      )
  endif()
  if(_ANDROID_SYSROOT_API)
    foreach(v CMAKE_ANDROID_API CMAKE_SYSTEM_VERSION)
      if(${v} AND NOT "x${_ANDROID_SYSROOT_API}" STREQUAL "x${${v}}")
        message(FATAL_ERROR
          "Android: The API specified by ${v}='${${v}}' is not consistent with CMAKE_SYSROOT:\n"
          "  ${CMAKE_SYSROOT}"
          )
      endif()
    endforeach()
  endif()
  if(CMAKE_ANDROID_NDK AND NOT IS_DIRECTORY "${CMAKE_ANDROID_NDK}/platforms/android-${CMAKE_SYSTEM_VERSION}")
    message(FATAL_ERROR
      "Android: The API specified by ${_ANDROID_API_VAR}='${${_ANDROID_API_VAR}}' does not exist in the NDK.  "
      "The directory:\n"
      "  ${CMAKE_ANDROID_NDK}/platforms/android-${CMAKE_SYSTEM_VERSION}\n"
      "does not exist."
      )
  endif()
elseif(CMAKE_ANDROID_NDK)
  file(GLOB _ANDROID_APIS_1 RELATIVE "${CMAKE_ANDROID_NDK}/platforms" "${CMAKE_ANDROID_NDK}/platforms/android-[0-9]")
  file(GLOB _ANDROID_APIS_2 RELATIVE "${CMAKE_ANDROID_NDK}/platforms" "${CMAKE_ANDROID_NDK}/platforms/android-[0-9][0-9]")
  list(SORT _ANDROID_APIS_1)
  list(SORT _ANDROID_APIS_2)
  set(_ANDROID_APIS ${_ANDROID_APIS_1} ${_ANDROID_APIS_2})
  unset(_ANDROID_APIS_1)
  unset(_ANDROID_APIS_2)
  if(_ANDROID_APIS STREQUAL "")
    message(FATAL_ERROR
      "Android: No APIs found in the NDK.  No\n"
      "  ${CMAKE_ANDROID_NDK}/platforms/android-*\n"
      "directories exist."
      )
  endif()
  string(REPLACE "android-" "" _ANDROID_APIS "${_ANDROID_APIS}")
  list(REVERSE _ANDROID_APIS)
  list(GET _ANDROID_APIS 0 CMAKE_SYSTEM_VERSION)
  unset(_ANDROID_APIS)
endif()
if(NOT CMAKE_SYSTEM_VERSION MATCHES "^[0-9]+$")
  message(FATAL_ERROR "Android: The API specified by CMAKE_SYSTEM_VERSION='${CMAKE_SYSTEM_VERSION}' is not an integer.")
endif()

if(CMAKE_ANDROID_NDK AND NOT DEFINED CMAKE_ANDROID_NDK_DEPRECATED_HEADERS)
  if(IS_DIRECTORY "${CMAKE_ANDROID_NDK}/sysroot/usr/include/${CMAKE_ANDROID_ARCH_TRIPLE}")
    # Unified headers exist so we use them by default.
    set(CMAKE_ANDROID_NDK_DEPRECATED_HEADERS 0)
  else()
    # Unified headers do not exist so use the deprecated headers.
    set(CMAKE_ANDROID_NDK_DEPRECATED_HEADERS 1)
  endif()
endif()

# Save the Android-specific information in CMakeSystem.cmake.
set(CMAKE_SYSTEM_CUSTOM_CODE "
set(CMAKE_ANDROID_NDK \"${CMAKE_ANDROID_NDK}\")
set(CMAKE_ANDROID_STANDALONE_TOOLCHAIN \"${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}\")
set(CMAKE_ANDROID_ARCH \"${CMAKE_ANDROID_ARCH}\")
set(CMAKE_ANDROID_ARCH_ABI \"${CMAKE_ANDROID_ARCH_ABI}\")
")

if(CMAKE_ANDROID_NDK)
  string(APPEND CMAKE_SYSTEM_CUSTOM_CODE
    "set(CMAKE_ANDROID_ARCH_TRIPLE \"${CMAKE_ANDROID_ARCH_TRIPLE}\")\n"
    "set(CMAKE_ANDROID_NDK_DEPRECATED_HEADERS \"${CMAKE_ANDROID_NDK_DEPRECATED_HEADERS}\")\n"
    "set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG \"${CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG}\")\n"
    "set(CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED \"${CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED}\")\n"
    )
endif()

# Select an ARM variant.
if(CMAKE_ANDROID_ARCH_ABI MATCHES "^armeabi")
  if(CMAKE_ANDROID_ARM_MODE)
    set(CMAKE_ANDROID_ARM_MODE 1)
  else()
    set(CMAKE_ANDROID_ARM_MODE 0)
  endif()
  string(APPEND CMAKE_SYSTEM_CUSTOM_CODE
    "set(CMAKE_ANDROID_ARM_MODE \"${CMAKE_ANDROID_ARM_MODE}\")\n"
    )
elseif(DEFINED CMAKE_ANDROID_ARM_MODE)
  message(FATAL_ERROR "Android: CMAKE_ANDROID_ARM_MODE is set but is valid only for 'armeabi' architectures.")
endif()

if(CMAKE_ANDROID_ARCH_ABI STREQUAL "armeabi-v7a")
  if(CMAKE_ANDROID_ARM_NEON)
    set(CMAKE_ANDROID_ARM_NEON 1)
  else()
    set(CMAKE_ANDROID_ARM_NEON 0)
  endif()
  string(APPEND CMAKE_SYSTEM_CUSTOM_CODE
    "set(CMAKE_ANDROID_ARM_NEON \"${CMAKE_ANDROID_ARM_NEON}\")\n"
    )
elseif(DEFINED CMAKE_ANDROID_ARM_NEON)
  message(FATAL_ERROR "Android: CMAKE_ANDROID_ARM_NEON is set but is valid only for 'armeabi-v7a' architecture.")
endif()

# Report the chosen architecture.
message(STATUS "Android: Targeting API '${CMAKE_SYSTEM_VERSION}' with architecture '${CMAKE_ANDROID_ARCH}', ABI '${CMAKE_ANDROID_ARCH_ABI}', and processor '${CMAKE_SYSTEM_PROCESSOR}'")

cmake_policy(POP)
-DExternalData_ACTION=fetch -DExternalData_CONFIG=${config} -P ${_ExternalData_SELF} # Update whenever the object hash changes. MAIN_DEPENDENCY "${name}${ext}" ) list(APPEND files "${file}${stamp}") endif() endforeach() # Custom target to drive all update commands. add_custom_target(${target} ALL DEPENDS ${files}) endfunction() function(ExternalData_expand_arguments target outArgsVar) # Replace DATA{} references with real arguments. set(data_regex "DATA{([^;{}\r\n]*)}") set(other_regex "([^D]|D[^A]|DA[^T]|DAT[^A]|DATA[^{])+|.") set(outArgs "") # This list expansion un-escapes semicolons in list element values so we # must re-escape them below anywhere a new list expansion will occur. foreach(arg IN LISTS ARGN) if("x${arg}" MATCHES "${data_regex}") # Re-escape in-value semicolons before expansion in foreach below. string(REPLACE ";" "\\;" tmp "${arg}") # Split argument into DATA{}-pieces and other pieces. string(REGEX MATCHALL "${data_regex}|${other_regex}" pieces "${tmp}") # Compose output argument with DATA{}-pieces replaced. set(outArg "") foreach(piece IN LISTS pieces) if("x${piece}" MATCHES "^x${data_regex}$") # Replace this DATA{}-piece with a file path. string(REGEX REPLACE "${data_regex}" "\\1" data "${piece}") _ExternalData_arg("${target}" "${piece}" "${data}" file) set(outArg "${outArg}${file}") else() # No replacement needed for this piece. set(outArg "${outArg}${piece}") endif() endforeach() else() # No replacements needed in this argument. set(outArg "${arg}") endif() # Re-escape in-value semicolons in resulting list. string(REPLACE ";" "\\;" outArg "${outArg}") list(APPEND outArgs "${outArg}") endforeach() set("${outArgsVar}" "${outArgs}" PARENT_SCOPE) endfunction() #----------------------------------------------------------------------------- # Private helper interface set(_ExternalData_REGEX_ALGO "MD5|SHA1|SHA224|SHA256|SHA384|SHA512") set(_ExternalData_REGEX_EXT "md5|sha1|sha224|sha256|sha384|sha512") set(_ExternalData_SELF "${CMAKE_CURRENT_LIST_FILE}") get_filename_component(_ExternalData_SELF_DIR "${_ExternalData_SELF}" PATH) function(_ExternalData_compute_hash var_hash algo file) if("${algo}" MATCHES "^${_ExternalData_REGEX_ALGO}$") file("${algo}" "${file}" hash) set("${var_hash}" "${hash}" PARENT_SCOPE) else() message(FATAL_ERROR "Hash algorithm ${algo} unimplemented.") endif() endfunction() function(_ExternalData_random var) string(RANDOM LENGTH 6 random) set("${var}" "${random}" PARENT_SCOPE) endfunction() function(_ExternalData_exact_regex regex_var string) string(REGEX REPLACE "([][+.*()^])" "\\\\\\1" regex "${string}") set("${regex_var}" "${regex}" PARENT_SCOPE) endfunction() function(_ExternalData_atomic_write file content) _ExternalData_random(random) set(tmp "${file}.tmp${random}") file(WRITE "${tmp}" "${content}") file(RENAME "${tmp}" "${file}") endfunction() function(_ExternalData_link_content name var_ext) if("${ExternalData_LINK_CONTENT}" MATCHES "^(${_ExternalData_REGEX_ALGO})$") set(algo "${ExternalData_LINK_CONTENT}") else() message(FATAL_ERROR "Unknown hash algorithm specified by ExternalData_LINK_CONTENT:\n" " ${ExternalData_LINK_CONTENT}") endif() _ExternalData_compute_hash(hash "${algo}" "${name}") get_filename_component(dir "${name}" PATH) set(staged "${dir}/.ExternalData_${algo}_${hash}") string(TOLOWER ".${algo}" ext) _ExternalData_atomic_write("${name}${ext}" "${hash}\n") file(RENAME "${name}" "${staged}") set("${var_ext}" "${ext}" PARENT_SCOPE) file(RELATIVE_PATH relname "${ExternalData_SOURCE_ROOT}" "${name}${ext}") message(STATUS "Linked ${relname} to ExternalData ${algo}/${hash}") endfunction() function(_ExternalData_arg target arg options var_file) # Separate data path from the options. string(REPLACE "," ";" options "${options}") list(GET options 0 data) list(REMOVE_AT options 0) # Interpret trailing slashes as directories. set(data_is_directory 0) if("x${data}" MATCHES "^x(.*)([/\\])$") set(data_is_directory 1) set(data "${CMAKE_MATCH_1}") endif() # Convert to full path. if(IS_ABSOLUTE "${data}") set(absdata "${data}") else() set(absdata "${CMAKE_CURRENT_SOURCE_DIR}/${data}") endif() get_filename_component(absdata "${absdata}" ABSOLUTE) # Convert to relative path under the source tree. if(NOT ExternalData_SOURCE_ROOT) set(ExternalData_SOURCE_ROOT "${CMAKE_SOURCE_DIR}") endif() set(top_src "${ExternalData_SOURCE_ROOT}") file(RELATIVE_PATH reldata "${top_src}" "${absdata}") if(IS_ABSOLUTE "${reldata}" OR "${reldata}" MATCHES "^\\.\\./") message(FATAL_ERROR "Data file referenced by argument\n" " ${arg}\n" "does not lie under the top-level source directory\n" " ${top_src}\n") endif() if(data_is_directory AND NOT IS_DIRECTORY "${top_src}/${reldata}") message(FATAL_ERROR "Data directory referenced by argument\n" " ${arg}\n" "corresponds to source tree path\n" " ${reldata}\n" "that does not exist as a directory!") endif() if(NOT ExternalData_BINARY_ROOT) set(ExternalData_BINARY_ROOT "${CMAKE_BINARY_DIR}") endif() set(top_bin "${ExternalData_BINARY_ROOT}") # Handle in-source builds gracefully. if("${top_src}" STREQUAL "${top_bin}") if(ExternalData_LINK_CONTENT) message(WARNING "ExternalData_LINK_CONTENT cannot be used in-source") set(ExternalData_LINK_CONTENT 0) endif() set(top_same 1) endif() set(external "") # Entries external to the source tree. set(internal "") # Entries internal to the source tree. set(have_original ${data_is_directory}) # Process options. set(series_option "") set(associated_files "") set(associated_regex "") foreach(opt ${options}) if("x${opt}" MATCHES "^xREGEX:[^:/]+$") # Regular expression to match associated files. string(REGEX REPLACE "^REGEX:" "" regex "${opt}") list(APPEND associated_regex "${regex}") elseif(opt STREQUAL ":") # Activate series matching. set(series_option "${opt}") elseif("x${opt}" MATCHES "^[^][:/*?]+$") # Specific associated file. list(APPEND associated_files "${opt}") else() message(FATAL_ERROR "Unknown option \"${opt}\" in argument\n" " ${arg}\n") endif() endforeach() if(series_option) if(data_is_directory) message(FATAL_ERROR "Series option \"${series_option}\" not allowed with directories.") endif() if(associated_files OR associated_regex) message(FATAL_ERROR "Series option \"${series_option}\" not allowed with associated files.") endif() # Load a whole file series. _ExternalData_arg_series() elseif(data_is_directory) if(associated_files OR associated_regex) # Load listed/matching associated files in the directory. _ExternalData_arg_associated() else() message(FATAL_ERROR "Data directory referenced by argument\n" " ${arg}\n" "must list associated files.") endif() else() # Load the named data file. _ExternalData_arg_single() if(associated_files OR associated_regex) # Load listed/matching associated files. _ExternalData_arg_associated() endif() endif() if(NOT have_original) message(FATAL_ERROR "Data file referenced by argument\n" " ${arg}\n" "corresponds to source tree path\n" " ${reldata}\n" "that does not exist as a file (with or without an extension)!") endif() if(external) # Make the series available in the build tree. set_property(GLOBAL APPEND PROPERTY _ExternalData_${target}_FETCH "${external}") set_property(GLOBAL APPEND PROPERTY _ExternalData_${target}_LOCAL "${internal}") set("${var_file}" "${top_bin}/${reldata}" PARENT_SCOPE) else() # The whole series is in the source tree. set("${var_file}" "${top_src}/${reldata}" PARENT_SCOPE) endif() endfunction() macro(_ExternalData_arg_associated) # Associated files lie in the same directory. if(data_is_directory) set(reldir "${reldata}") else() get_filename_component(reldir "${reldata}" PATH) endif() if(reldir) set(reldir "${reldir}/") endif() _ExternalData_exact_regex(reldir_regex "${reldir}") # Find files named explicitly. foreach(file ${associated_files}) _ExternalData_exact_regex(file_regex "${file}") _ExternalData_arg_find_files("${reldir}${file}" "${reldir_regex}${file_regex}") endforeach() # Find files matching the given regular expressions. set(all "") set(sep "") foreach(regex ${associated_regex}) set(all "${all}${sep}${reldir_regex}${regex}") set(sep "|") endforeach() _ExternalData_arg_find_files("${reldir}" "${all}") endmacro() macro(_ExternalData_arg_single) # Match only the named data by itself. _ExternalData_exact_regex(data_regex "${reldata}") _ExternalData_arg_find_files("${reldata}" "${data_regex}") endmacro() macro(_ExternalData_arg_series) # Configure series parsing and matching. set(series_parse_prefix "") set(series_parse_number "\\1") set(series_parse_suffix "\\2") if(ExternalData_SERIES_PARSE) if(ExternalData_SERIES_PARSE_NUMBER AND ExternalData_SERIES_PARSE_SUFFIX) if(ExternalData_SERIES_PARSE_PREFIX) set(series_parse_prefix "\\${ExternalData_SERIES_PARSE_PREFIX}") endif() set(series_parse_number "\\${ExternalData_SERIES_PARSE_NUMBER}") set(series_parse_suffix "\\${ExternalData_SERIES_PARSE_SUFFIX}") elseif(NOT "x${ExternalData_SERIES_PARSE}" MATCHES "^x\\([^()]*\\)\\([^()]*\\)\\$$") message(FATAL_ERROR "ExternalData_SERIES_PARSE is set to\n" " ${ExternalData_SERIES_PARSE}\n" "which is not of the form\n" " (<number>)(<suffix>)$\n" "Fix the regular expression or set variables\n" " ExternalData_SERIES_PARSE_PREFIX = <prefix> regex group number, if any\n" " ExternalData_SERIES_PARSE_NUMBER = <number> regex group number\n" " ExternalData_SERIES_PARSE_SUFFIX = <suffix> regex group number\n" ) endif() set(series_parse "${ExternalData_SERIES_PARSE}") else() set(series_parse "([0-9]*)(\\.[^./]*)$") endif() if(ExternalData_SERIES_MATCH) set(series_match "${ExternalData_SERIES_MATCH}") else() set(series_match "[_.-]?[0-9]*") endif() # Parse the base, number, and extension components of the series. string(REGEX REPLACE "${series_parse}" "${series_parse_prefix};${series_parse_number};${series_parse_suffix}" tuple "${reldata}") list(LENGTH tuple len) if(NOT "${len}" EQUAL 3) message(FATAL_ERROR "Data file referenced by argument\n" " ${arg}\n" "corresponds to path\n" " ${reldata}\n" "that does not match regular expression\n" " ${series_parse}") endif() list(GET tuple 0 relbase) list(GET tuple 2 ext) # Glob files that might match the series. # Then match base, number, and extension. _ExternalData_exact_regex(series_base "${relbase}") _ExternalData_exact_regex(series_ext "${ext}") _ExternalData_arg_find_files("${relbase}*${ext}" "${series_base}${series_match}${series_ext}") endmacro() function(_ExternalData_arg_find_files pattern regex) file(GLOB globbed RELATIVE "${top_src}" "${top_src}/${pattern}*") foreach(entry IN LISTS globbed) if("x${entry}" MATCHES "^x(.*)(\\.(${_ExternalData_REGEX_EXT}))$") set(relname "${CMAKE_MATCH_1}") set(alg "${CMAKE_MATCH_2}") else() set(relname "${entry}") set(alg "") endif() if("x${relname}" MATCHES "^x${regex}$" # matches AND NOT IS_DIRECTORY "${top_src}/${entry}" # not a directory AND NOT "x${relname}" MATCHES "(^x|/)\\.ExternalData_" # not staged obj ) set(name "${top_src}/${relname}") set(file "${top_bin}/${relname}") if(alg) list(APPEND external "${file}|${name}|${alg}") elseif(ExternalData_LINK_CONTENT) _ExternalData_link_content("${name}" alg) list(APPEND external "${file}|${name}|${alg}") elseif(NOT top_same) list(APPEND internal "${file}|${name}") endif() if("${relname}" STREQUAL "${reldata}") set(have_original 1) endif() endif() endforeach() set(external "${external}" PARENT_SCOPE) set(internal "${internal}" PARENT_SCOPE) set(have_original "${have_original}" PARENT_SCOPE) endfunction() #----------------------------------------------------------------------------- # Private script mode interface if(CMAKE_GENERATOR OR NOT ExternalData_ACTION) return() endif() if(ExternalData_CONFIG) include(${ExternalData_CONFIG}) endif() if(NOT ExternalData_URL_TEMPLATES) message(FATAL_ERROR "No ExternalData_URL_TEMPLATES set!") endif() function(_ExternalData_link_or_copy src dst) # Create a temporary file first. get_filename_component(dst_dir "${dst}" PATH) file(MAKE_DIRECTORY "${dst_dir}") _ExternalData_random(random) set(tmp "${dst}.tmp${random}") if(UNIX) # Create a symbolic link. set(tgt "${src}") if(relative_top) # Use relative path if files are close enough. file(RELATIVE_PATH relsrc "${relative_top}" "${src}") file(RELATIVE_PATH relfile "${relative_top}" "${dst}") if(NOT IS_ABSOLUTE "${relsrc}" AND NOT "${relsrc}" MATCHES "^\\.\\./" AND NOT IS_ABSOLUTE "${reldst}" AND NOT "${reldst}" MATCHES "^\\.\\./") file(RELATIVE_PATH tgt "${dst_dir}" "${src}") endif() endif() execute_process(COMMAND "${CMAKE_COMMAND}" -E create_symlink "${tgt}" "${tmp}" RESULT_VARIABLE result) else() # Create a copy. execute_process(COMMAND "${CMAKE_COMMAND}" -E copy "${src}" "${tmp}" RESULT_VARIABLE result) endif() if(result) file(REMOVE "${tmp}") message(FATAL_ERROR "Failed to create\n ${tmp}\nfrom\n ${obj}") endif() # Atomically create/replace the real destination. file(RENAME "${tmp}" "${dst}") endfunction() function(_ExternalData_download_file url file err_var msg_var) set(retry 3) while(retry) math(EXPR retry "${retry} - 1") if(ExternalData_TIMEOUT_INACTIVITY) set(inactivity_timeout INACTIVITY_TIMEOUT ${ExternalData_TIMEOUT_INACTIVITY}) elseif(NOT "${ExternalData_TIMEOUT_INACTIVITY}" EQUAL 0) set(inactivity_timeout INACTIVITY_TIMEOUT 60) else() set(inactivity_timeout "") endif() if(ExternalData_TIMEOUT_ABSOLUTE) set(absolute_timeout TIMEOUT ${ExternalData_TIMEOUT_ABSOLUTE}) elseif(NOT "${ExternalData_TIMEOUT_ABSOLUTE}" EQUAL 0) set(absolute_timeout TIMEOUT 300) else() set(absolute_timeout "") endif() file(DOWNLOAD "${url}" "${file}" STATUS status LOG log ${inactivity_timeout} ${absolute_timeout} SHOW_PROGRESS) list(GET status 0 err) list(GET status 1 msg) if(err) if("${msg}" MATCHES "HTTP response code said error" AND "${log}" MATCHES "error: 503") set(msg "temporarily unavailable") endif() elseif("${log}" MATCHES "\nHTTP[^\n]* 503") set(err TRUE) set(msg "temporarily unavailable") endif() if(NOT err OR NOT "${msg}" MATCHES "partial|timeout|temporarily") break() elseif(retry) message(STATUS "[download terminated: ${msg}, retries left: ${retry}]") endif() endwhile() set("${err_var}" "${err}" PARENT_SCOPE) set("${msg_var}" "${msg}" PARENT_SCOPE) endfunction() function(_ExternalData_download_object name hash algo var_obj) # Search all object stores for an existing object. foreach(dir ${ExternalData_OBJECT_STORES}) set(obj "${dir}/${algo}/${hash}") if(EXISTS "${obj}") message(STATUS "Found object: \"${obj}\"") set("${var_obj}" "${obj}" PARENT_SCOPE) return() endif() endforeach() # Download object to the first store. list(GET ExternalData_OBJECT_STORES 0 store) set(obj "${store}/${algo}/${hash}") _ExternalData_random(random) set(tmp "${obj}.tmp${random}") set(found 0) set(tried "") foreach(url_template IN LISTS ExternalData_URL_TEMPLATES) string(REPLACE "%(hash)" "${hash}" url_tmp "${url_template}") string(REPLACE "%(algo)" "${algo}" url "${url_tmp}") message(STATUS "Fetching \"${url}\"") _ExternalData_download_file("${url}" "${tmp}" err errMsg) set(tried "${tried}\n ${url}") if(err) set(tried "${tried} (${errMsg})") else() # Verify downloaded object. _ExternalData_compute_hash(dl_hash "${algo}" "${tmp}") if("${dl_hash}" STREQUAL "${hash}") set(found 1) break() else() set(tried "${tried} (wrong hash ${algo}=${dl_hash})") if("$ENV{ExternalData_DEBUG_DOWNLOAD}" MATCHES ".") file(RENAME "${tmp}" "${store}/${algo}/${dl_hash}") endif() endif() endif() file(REMOVE "${tmp}") endforeach() get_filename_component(dir "${name}" PATH) set(staged "${dir}/.ExternalData_${algo}_${hash}") if(found) file(RENAME "${tmp}" "${obj}") message(STATUS "Downloaded object: \"${obj}\"") elseif(EXISTS "${staged}") set(obj "${staged}") message(STATUS "Staged object: \"${obj}\"") else() message(FATAL_ERROR "Object ${algo}=${hash} not found at:${tried}") endif() set("${var_obj}" "${obj}" PARENT_SCOPE) endfunction() if("${ExternalData_ACTION}" STREQUAL "fetch") foreach(v ExternalData_OBJECT_STORES file name ext) if(NOT DEFINED "${v}") message(FATAL_ERROR "No \"-D${v}=\" value provided!") endif() endforeach() file(READ "${name}${ext}" hash) string(STRIP "${hash}" hash) if("${ext}" MATCHES "^\\.(${_ExternalData_REGEX_EXT})$") string(TOUPPER "${CMAKE_MATCH_1}" algo) else() message(FATAL_ERROR "Unknown hash algorithm extension \"${ext}\"") endif() _ExternalData_download_object("${name}" "${hash}" "${algo}" obj) # Check if file already corresponds to the object. set(stamp "${ext}-stamp") set(file_up_to_date 0) if(EXISTS "${file}" AND EXISTS "${file}${stamp}") file(READ "${file}${stamp}" f_hash) string(STRIP "${f_hash}" f_hash) if("${f_hash}" STREQUAL "${hash}") #message(STATUS "File already corresponds to object") set(file_up_to_date 1) endif() endif() if(file_up_to_date) # Touch the file to convince the build system it is up to date. execute_process(COMMAND "${CMAKE_COMMAND}" -E touch "${file}") else() _ExternalData_link_or_copy("${obj}" "${file}") endif() # Atomically update the hash/timestamp file to record the object referenced. _ExternalData_atomic_write("${file}${stamp}" "${hash}\n") elseif("${ExternalData_ACTION}" STREQUAL "local") foreach(v file name) if(NOT DEFINED "${v}") message(FATAL_ERROR "No \"-D${v}=\" value provided!") endif() endforeach() _ExternalData_link_or_copy("${name}" "${file}") else() message(FATAL_ERROR "Unknown ExternalData_ACTION=[${ExternalData_ACTION}]") endif()