|
| 1 | +# |
| 2 | +# Copyright (c) 2026, Oracle and/or its affiliates. |
| 3 | +# |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without modification, are |
| 7 | +# permitted provided that the following conditions are met: |
| 8 | +# |
| 9 | +# 1. Redistributions of source code must retain the above copyright notice, this list of |
| 10 | +# conditions and the following disclaimer. |
| 11 | +# |
| 12 | +# 2. Redistributions in binary form must reproduce the above copyright notice, this list of |
| 13 | +# conditions and the following disclaimer in the documentation and/or other materials provided |
| 14 | +# with the distribution. |
| 15 | +# 3. Neither the name of the copyright holder nor the names of its contributors may be used to |
| 16 | +# endorse or promote products derived from this software without specific prior written |
| 17 | +# permission. |
| 18 | +# |
| 19 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
| 20 | +# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | +# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 22 | +# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 24 | +# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 25 | +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 | +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 27 | +# OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | +# |
| 29 | +cmake_minimum_required(VERSION 3.22) |
| 30 | +project(graalpy-graalos-standalone-payload LANGUAGES NONE) |
| 31 | + |
| 32 | +foreach(required_var |
| 33 | + GRAALPY_LAUNCHER |
| 34 | + PYTHON_LAUNCHER |
| 35 | + PYTHON3_LAUNCHER |
| 36 | + GRAALPY_CONFIG_LAUNCHER |
| 37 | + GRAALPY_POLYGLOT_GET_LAUNCHER |
| 38 | + GRAALPY_SUITE_PARENT) |
| 39 | + if(NOT DEFINED ${required_var}) |
| 40 | + message(FATAL_ERROR "${required_var} needs to be set") |
| 41 | + endif() |
| 42 | +endforeach() |
| 43 | + |
| 44 | +set(PAYLOAD_DIR "${CMAKE_BINARY_DIR}") |
| 45 | +set(GRAALOS_DIR "${PAYLOAD_DIR}/lib/graalos") |
| 46 | +set(SYSROOT_LIB_DIR "${GRAALOS_DIR}/sysroot/lib") |
| 47 | + |
| 48 | +file(REMOVE_RECURSE |
| 49 | + "${PAYLOAD_DIR}/bin" |
| 50 | + "${PAYLOAD_DIR}/libexec" |
| 51 | + "${PAYLOAD_DIR}/lib" |
| 52 | + "${PAYLOAD_DIR}/config.json") |
| 53 | +file(MAKE_DIRECTORY |
| 54 | + "${PAYLOAD_DIR}/bin" |
| 55 | + "${PAYLOAD_DIR}/libexec" |
| 56 | + "${GRAALOS_DIR}" |
| 57 | + "${SYSROOT_LIB_DIR}") |
| 58 | + |
| 59 | +function(_append_env_path out_var env_name) |
| 60 | + if(DEFINED ENV{${env_name}} AND NOT "$ENV{${env_name}}" STREQUAL "") |
| 61 | + list(APPEND ${out_var} "$ENV{${env_name}}") |
| 62 | + set(${out_var} "${${out_var}}" PARENT_SCOPE) |
| 63 | + endif() |
| 64 | +endfunction() |
| 65 | + |
| 66 | +function(_find_graalos_runtime out_var) |
| 67 | + set(candidates) |
| 68 | + _append_env_path(candidates GRAALOS_RUNTIME_HOME) |
| 69 | + _append_env_path(candidates GRAALOS_HOME) |
| 70 | + _append_env_path(candidates GRAALOS_BUILD_HOME) |
| 71 | + list(APPEND candidates |
| 72 | + "${GRAALPY_SUITE_PARENT}/graalos/cmake-build-clang-pkeysoff" |
| 73 | + "${GRAALPY_SUITE_PARENT}/graalos/build") |
| 74 | + |
| 75 | + foreach(candidate IN LISTS candidates) |
| 76 | + if(EXISTS "${candidate}/graalhost/graalhost" |
| 77 | + AND EXISTS "${candidate}/graalhost/libc.so" |
| 78 | + AND EXISTS "${candidate}/graalhost/libbinsweep.so") |
| 79 | + set(${out_var} "${candidate}" PARENT_SCOPE) |
| 80 | + return() |
| 81 | + endif() |
| 82 | + endforeach() |
| 83 | + |
| 84 | + string(REPLACE ";" "\n" candidate_text "${candidates}") |
| 85 | + message(FATAL_ERROR |
| 86 | + "Could not find GraalOS graalhost, safe libc.so, and libbinsweep.so. Checked:\n${candidate_text}") |
| 87 | +endfunction() |
| 88 | + |
| 89 | +function(_find_graalos_sysroot_lib_dir out_var) |
| 90 | + set(candidates) |
| 91 | + if(DEFINED ENV{MUSL_TOOLCHAIN} AND NOT "$ENV{MUSL_TOOLCHAIN}" STREQUAL "") |
| 92 | + list(APPEND candidates "$ENV{MUSL_TOOLCHAIN}/lib") |
| 93 | + endif() |
| 94 | + if(DEFINED ENV{GRAALOS_TOOLCHAIN_PATH} AND NOT "$ENV{GRAALOS_TOOLCHAIN_PATH}" STREQUAL "") |
| 95 | + list(APPEND candidates "$ENV{GRAALOS_TOOLCHAIN_PATH}/lib") |
| 96 | + endif() |
| 97 | + if(DEFINED ENV{JAVA_HOME} AND NOT "$ENV{JAVA_HOME}" STREQUAL "") |
| 98 | + list(APPEND candidates |
| 99 | + "$ENV{JAVA_HOME}/lib/toolchains/musl-swcfi/lib" |
| 100 | + "$ENV{JAVA_HOME}/lib/toolchains/sysroot/x86_64-unknown-linux-musl_swcfi/lib") |
| 101 | + endif() |
| 102 | + file(GLOB graalos_prebuilt_musl_lib_dirs |
| 103 | + LIST_DIRECTORIES TRUE |
| 104 | + "${GRAALPY_SUITE_PARENT}/graalos/build-tools/prebuilt/graalvm-graalos-*/lib/toolchains/musl-swcfi/lib") |
| 105 | + file(GLOB graalos_prebuilt_sysroot_lib_dirs |
| 106 | + LIST_DIRECTORIES TRUE |
| 107 | + "${GRAALPY_SUITE_PARENT}/graalos/build-tools/prebuilt/graalvm-graalos-*/lib/toolchains/sysroot/x86_64-unknown-linux-musl_swcfi/lib") |
| 108 | + list(APPEND candidates |
| 109 | + ${graalos_prebuilt_musl_lib_dirs} |
| 110 | + ${graalos_prebuilt_sysroot_lib_dirs}) |
| 111 | + |
| 112 | + set(required_libs |
| 113 | + libc++.so |
| 114 | + libc++.so.1 |
| 115 | + libc++.so.1.0 |
| 116 | + libc++abi.so |
| 117 | + libc++abi.so.1 |
| 118 | + libc++abi.so.1.0 |
| 119 | + libunwind.so |
| 120 | + libunwind.so.1 |
| 121 | + libunwind.so.1.0) |
| 122 | + |
| 123 | + foreach(candidate IN LISTS candidates) |
| 124 | + set(found TRUE) |
| 125 | + foreach(lib IN LISTS required_libs) |
| 126 | + if(NOT EXISTS "${candidate}/${lib}") |
| 127 | + set(found FALSE) |
| 128 | + break() |
| 129 | + endif() |
| 130 | + endforeach() |
| 131 | + if(found) |
| 132 | + set(${out_var} "${candidate}" PARENT_SCOPE) |
| 133 | + return() |
| 134 | + endif() |
| 135 | + endforeach() |
| 136 | + |
| 137 | + string(REPLACE ";" "\n" candidate_text "${candidates}") |
| 138 | + message(FATAL_ERROR "Could not find required GraalOS musl-swcfi runtime libraries. Checked:\n${candidate_text}") |
| 139 | +endfunction() |
| 140 | + |
| 141 | +function(_copy_executable source target) |
| 142 | + configure_file("${source}" "${target}" COPYONLY) |
| 143 | + file(CHMOD "${target}" |
| 144 | + PERMISSIONS |
| 145 | + OWNER_READ OWNER_WRITE OWNER_EXECUTE |
| 146 | + GROUP_READ GROUP_EXECUTE |
| 147 | + WORLD_READ WORLD_EXECUTE) |
| 148 | +endfunction() |
| 149 | + |
| 150 | +function(_copy_file source target) |
| 151 | + configure_file("${source}" "${target}" COPYONLY) |
| 152 | +endfunction() |
| 153 | + |
| 154 | +function(_write_launcher target virtual_executable) |
| 155 | + file(READ "${CMAKE_CURRENT_LIST_DIR}/launcher-wrapper.sh" wrapper_content) |
| 156 | + string(REPLACE "@GRAALPY_SANDBOX_VIRTUAL_EXECUTABLE@" "${virtual_executable}" wrapper_content "${wrapper_content}") |
| 157 | + file(WRITE "${target}" "${wrapper_content}") |
| 158 | + file(CHMOD "${target}" |
| 159 | + PERMISSIONS |
| 160 | + OWNER_READ OWNER_WRITE OWNER_EXECUTE |
| 161 | + GROUP_READ GROUP_EXECUTE |
| 162 | + WORLD_READ WORLD_EXECUTE) |
| 163 | +endfunction() |
| 164 | + |
| 165 | +_write_launcher("${PAYLOAD_DIR}/bin/${GRAALPY_LAUNCHER}" "/bin/graalpy") |
| 166 | +_write_launcher("${PAYLOAD_DIR}/bin/${PYTHON_LAUNCHER}" "/bin/python") |
| 167 | +_write_launcher("${PAYLOAD_DIR}/bin/${PYTHON3_LAUNCHER}" "/bin/python3") |
| 168 | +_write_launcher("${PAYLOAD_DIR}/bin/${GRAALPY_CONFIG_LAUNCHER}" "/bin/graalpy-config") |
| 169 | +_write_launcher("${PAYLOAD_DIR}/libexec/${GRAALPY_POLYGLOT_GET_LAUNCHER}" "/libexec/graalpy-polyglot-get") |
| 170 | + |
| 171 | +_copy_file("${CMAKE_CURRENT_LIST_DIR}/config.json" "${PAYLOAD_DIR}/config.json") |
| 172 | +_copy_executable("${CMAKE_CURRENT_LIST_DIR}/graalpy-sandbox-launcher.sh" "${GRAALOS_DIR}/graalpy-sandbox-launcher") |
| 173 | +_copy_executable( |
| 174 | + "${CMAKE_CURRENT_LIST_DIR}/graalpy-sandbox-expand-config.sh" |
| 175 | + "${GRAALOS_DIR}/graalpy-sandbox-expand-config") |
| 176 | +_copy_executable("${CMAKE_CURRENT_LIST_DIR}/graalpy-sandbox-fsmappings.sh" "${GRAALOS_DIR}/graalpy-sandbox-fsmappings") |
| 177 | + |
| 178 | +_find_graalos_runtime(graalos_runtime) |
| 179 | +_copy_executable("${graalos_runtime}/graalhost/graalhost" "${GRAALOS_DIR}/graalhost") |
| 180 | +_copy_file("${graalos_runtime}/graalhost/libc.so" "${GRAALOS_DIR}/libc.so") |
| 181 | +_copy_file("${graalos_runtime}/graalhost/libbinsweep.so" "${GRAALOS_DIR}/libbinsweep.so") |
| 182 | + |
| 183 | +_find_graalos_sysroot_lib_dir(graalos_sysroot_lib_dir) |
| 184 | +foreach(lib |
| 185 | + libc++.so |
| 186 | + libc++.so.1 |
| 187 | + libc++.so.1.0 |
| 188 | + libc++abi.so |
| 189 | + libc++abi.so.1 |
| 190 | + libc++abi.so.1.0 |
| 191 | + libunwind.so |
| 192 | + libunwind.so.1 |
| 193 | + libunwind.so.1.0) |
| 194 | + file(COPY "${graalos_sysroot_lib_dir}/${lib}" DESTINATION "${SYSROOT_LIB_DIR}") |
| 195 | +endforeach() |
0 commit comments