|
| 1 | +# Invoked only after the shipped executable has been installed. |
| 2 | +set(_pf_sa_root "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}") |
| 3 | +set(_pf_sa_binary "${_pf_sa_root}/${PF_SA_HELPER_PATH}") |
| 4 | +if(NOT EXISTS "${_pf_sa_binary}" OR IS_DIRECTORY "${_pf_sa_binary}") |
| 5 | + message(FATAL_ERROR "Installed pineforge-selected-admit is missing: ${_pf_sa_binary}") |
| 6 | +endif() |
| 7 | +file(SIZE "${_pf_sa_binary}" _pf_sa_size) |
| 8 | +file(SHA256 "${_pf_sa_binary}" _pf_sa_sha) |
| 9 | + |
| 10 | +# Byte lookup and UTF-8 decoding keep the JSON identical to the C++ ASCII |
| 11 | +# encoder without depending on a newer CMake JSON implementation. |
| 12 | +function(_pf_sa_octet text index result) |
| 13 | + string(SUBSTRING "${text}" ${index} 1 _pf_sa_byte) |
| 14 | + foreach(_pf_sa_number RANGE 1 255) |
| 15 | + string(ASCII ${_pf_sa_number} _pf_sa_character) |
| 16 | + string(COMPARE EQUAL "${_pf_sa_character}" "${_pf_sa_byte}" _pf_sa_same) |
| 17 | + if(_pf_sa_same) |
| 18 | + set(${result} ${_pf_sa_number} PARENT_SCOPE) |
| 19 | + return() |
| 20 | + endif() |
| 21 | + endforeach() |
| 22 | + message(FATAL_ERROR "Invalid UTF-8 in helper manifest metadata") |
| 23 | +endfunction() |
| 24 | + |
| 25 | +function(_pf_sa_unit point result) |
| 26 | + set(_pf_sa_hex "0123456789abcdef") |
| 27 | + set(_pf_sa_text "\\u") |
| 28 | + foreach(_pf_sa_shift 12 8 4 0) |
| 29 | + math(EXPR _pf_sa_digit "(${point} >> ${_pf_sa_shift}) & 15") |
| 30 | + string(SUBSTRING "${_pf_sa_hex}" ${_pf_sa_digit} 1 _pf_sa_character) |
| 31 | + string(APPEND _pf_sa_text "${_pf_sa_character}") |
| 32 | + endforeach() |
| 33 | + set(${result} "${_pf_sa_text}" PARENT_SCOPE) |
| 34 | +endfunction() |
| 35 | + |
| 36 | +function(_pf_sa_quote result text) |
| 37 | + string(LENGTH "${text}" _pf_sa_length) |
| 38 | + set(_pf_sa_at 0) |
| 39 | + set(_pf_sa_json "\"") |
| 40 | + while(_pf_sa_at LESS _pf_sa_length) |
| 41 | + _pf_sa_octet("${text}" ${_pf_sa_at} _pf_sa_point) |
| 42 | + string(SUBSTRING "${text}" ${_pf_sa_at} 1 _pf_sa_character) |
| 43 | + math(EXPR _pf_sa_at "${_pf_sa_at} + 1") |
| 44 | + if(_pf_sa_point EQUAL 34 OR _pf_sa_point EQUAL 92) |
| 45 | + string(APPEND _pf_sa_json "\\${_pf_sa_character}") |
| 46 | + elseif(_pf_sa_point GREATER_EQUAL 32 AND _pf_sa_point LESS 127) |
| 47 | + string(APPEND _pf_sa_json "${_pf_sa_character}") |
| 48 | + else() |
| 49 | + if(_pf_sa_point GREATER_EQUAL 128) |
| 50 | + if(_pf_sa_point GREATER_EQUAL 194 AND _pf_sa_point LESS 224) |
| 51 | + set(_pf_sa_tails 1) |
| 52 | + set(_pf_sa_minimum 128) |
| 53 | + elseif(_pf_sa_point GREATER_EQUAL 224 AND _pf_sa_point LESS 240) |
| 54 | + set(_pf_sa_tails 2) |
| 55 | + set(_pf_sa_minimum 2048) |
| 56 | + elseif(_pf_sa_point GREATER_EQUAL 240 AND _pf_sa_point LESS_EQUAL 244) |
| 57 | + set(_pf_sa_tails 3) |
| 58 | + set(_pf_sa_minimum 65536) |
| 59 | + else() |
| 60 | + message(FATAL_ERROR "Invalid UTF-8 in helper manifest metadata") |
| 61 | + endif() |
| 62 | + math(EXPR _pf_sa_point "${_pf_sa_point} & ((1 << (6 - ${_pf_sa_tails})) - 1)") |
| 63 | + foreach(_pf_sa_tail RANGE 1 ${_pf_sa_tails}) |
| 64 | + if(NOT _pf_sa_at LESS _pf_sa_length) |
| 65 | + message(FATAL_ERROR "Truncated UTF-8 in helper manifest metadata") |
| 66 | + endif() |
| 67 | + _pf_sa_octet("${text}" ${_pf_sa_at} _pf_sa_next) |
| 68 | + if(_pf_sa_next LESS 128 OR _pf_sa_next GREATER 191) |
| 69 | + message(FATAL_ERROR "Invalid UTF-8 in helper manifest metadata") |
| 70 | + endif() |
| 71 | + math(EXPR _pf_sa_point "(${_pf_sa_point} << 6) | (${_pf_sa_next} & 63)") |
| 72 | + math(EXPR _pf_sa_at "${_pf_sa_at} + 1") |
| 73 | + endforeach() |
| 74 | + if(_pf_sa_point LESS _pf_sa_minimum OR _pf_sa_point GREATER 1114111 |
| 75 | + OR (_pf_sa_point GREATER_EQUAL 55296 AND _pf_sa_point LESS_EQUAL 57343)) |
| 76 | + message(FATAL_ERROR "Invalid UTF-8 in helper manifest metadata") |
| 77 | + endif() |
| 78 | + endif() |
| 79 | + if(_pf_sa_point GREATER 65535) |
| 80 | + math(EXPR _pf_sa_point "${_pf_sa_point} - 65536") |
| 81 | + math(EXPR _pf_sa_high "55296 + (${_pf_sa_point} >> 10)") |
| 82 | + math(EXPR _pf_sa_low "56320 + (${_pf_sa_point} & 1023)") |
| 83 | + _pf_sa_unit(${_pf_sa_high} _pf_sa_encoded_high) |
| 84 | + _pf_sa_unit(${_pf_sa_low} _pf_sa_encoded_low) |
| 85 | + string(APPEND _pf_sa_json "${_pf_sa_encoded_high}${_pf_sa_encoded_low}") |
| 86 | + else() |
| 87 | + _pf_sa_unit(${_pf_sa_point} _pf_sa_encoded) |
| 88 | + string(APPEND _pf_sa_json "${_pf_sa_encoded}") |
| 89 | + endif() |
| 90 | + endif() |
| 91 | + endwhile() |
| 92 | + string(APPEND _pf_sa_json "\"") |
| 93 | + set(${result} "${_pf_sa_json}" PARENT_SCOPE) |
| 94 | +endfunction() |
| 95 | + |
| 96 | +_pf_sa_quote(_pf_sa_path_json "${PF_SA_HELPER_PATH}") |
| 97 | +_pf_sa_quote(_pf_sa_full_json "${PF_SA_VERSION_FULL}") |
| 98 | +_pf_sa_quote(_pf_sa_version_json "${PF_SA_VERSION}") |
| 99 | +_pf_sa_quote(_pf_sa_git_json "${PF_SA_GIT_SHA}") |
| 100 | +if(PF_SA_DIRTY) |
| 101 | + set(_pf_sa_dirty_json true) |
| 102 | +else() |
| 103 | + set(_pf_sa_dirty_json false) |
| 104 | +endif() |
| 105 | +set(_pf_sa_manifest "{\"schema\":\"pineforge-selected-capacity-helper-manifest/v1\",") |
| 106 | +string(APPEND _pf_sa_manifest "\"helper\":{\"name\":\"pineforge-selected-admit\",\"path\":${_pf_sa_path_json},\"bytes\":\"${_pf_sa_size}\",\"sha256\":\"${_pf_sa_sha}\"},") |
| 107 | +string(APPEND _pf_sa_manifest "\"engine\":{\"version_full\":${_pf_sa_full_json},\"version\":${_pf_sa_version_json},\"git_sha\":${_pf_sa_git_json},\"dirty\":${_pf_sa_dirty_json}},") |
| 108 | +string(APPEND _pf_sa_manifest "\"descriptor\":{\"request_schema\":\"pineforge-selected-capacity-request/v1\",\"result_schema\":\"pineforge-selected-capacity-result/v1\",\"spec_sha256\":\"${PF_SA_SPEC_SHA256}\"},") |
| 109 | +string(APPEND _pf_sa_manifest "\"failure_catalog\":{\"codes\":[\"selected_auxiliary_rows_limit_exceeded\",\"selected_bytes_limit_exceeded\",\"selected_primary_rows_limit_exceeded\",\"selected_script_bars_limit_exceeded\",\"selected_source_rows_limit_exceeded\"],\"catalog_sha256\":\"${PF_SA_CATALOG_SHA256}\"},") |
| 110 | +string(APPEND _pf_sa_manifest "\"stages\":${PF_SA_STAGES_JSON}}\n") |
| 111 | +set(_pf_sa_manifest_file "${_pf_sa_root}/${PF_SA_MANIFEST_PATH}") |
| 112 | +get_filename_component(_pf_sa_manifest_directory "${_pf_sa_manifest_file}" DIRECTORY) |
| 113 | +file(MAKE_DIRECTORY "${_pf_sa_manifest_directory}") |
| 114 | +file(WRITE "${_pf_sa_manifest_file}" "${_pf_sa_manifest}") |
0 commit comments