1# Copyright 2020 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15FetchContent_Declare(libffi 16 URL https://github.com/libffi/libffi/releases/download/v3.3-rc2/libffi-3.3-rc2.tar.gz 17 URL_HASH SHA256=653ffdfc67fbb865f39c7e5df2a071c0beb17206ebfb0a9ecb18a18f63f6b263 18) 19FetchContent_GetProperties(libffi) 20if(NOT libffi_POPULATED) 21 FetchContent_Populate(libffi) 22 set(libffi_STATUS_FILE "${libffi_SOURCE_DIR}/config.status") 23 if(EXISTS "${libffi_STATUS_FILE}") 24 file(SHA256 "${libffi_STATUS_FILE}" _sapi_CONFIG_STATUS) 25 endif() 26 if(NOT _sapi_CONFIG_STATUS STREQUAL "${libffi_CONFIG_STATUS}") 27 message("-- Running ./configure for libffi...") 28 execute_process( 29 COMMAND ./configure --disable-dependency-tracking 30 --disable-builddir 31 --quiet 32 WORKING_DIRECTORY "${libffi_SOURCE_DIR}" 33 RESULT_VARIABLE _sapi_libffi_config_result 34 ) 35 if(NOT _sapi_libffi_config_result EQUAL "0") 36 message(FATAL_ERROR "Configuration of libffi dependency failed") 37 endif() 38 file(SHA256 "${libffi_SOURCE_DIR}/config.status" _sapi_CONFIG_STATUS) 39 set(libffi_CONFIG_STATUS "${_sapi_CONFIG_STATUS}" CACHE INTERNAL "") 40 endif() 41endif() 42 43set(libffi_INCLUDE_DIR ${libffi_SOURCE_DIR}/libffi/include) 44 45if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64") 46 list(APPEND _ffi_platform_srcs 47 ${libffi_SOURCE_DIR}/src/x86/asmnames.h 48 ${libffi_SOURCE_DIR}/src/x86/ffi.c 49 ${libffi_SOURCE_DIR}/src/x86/ffi64.c 50 ${libffi_SOURCE_DIR}/src/x86/ffiw64.c 51 ${libffi_SOURCE_DIR}/src/x86/internal.h 52 ${libffi_SOURCE_DIR}/src/x86/internal64.h 53 ${libffi_SOURCE_DIR}/src/x86/sysv.S 54 ${libffi_SOURCE_DIR}/src/x86/unix64.S 55 ${libffi_SOURCE_DIR}/src/x86/win64.S 56 ) 57elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "ppc64") 58 list(APPEND _ffi_platform_srcs 59 ${libffi_SOURCE_DIR}/src/powerpc/ffi.c 60 ${libffi_SOURCE_DIR}/src/powerpc/ffi_linux64.c 61 ${libffi_SOURCE_DIR}/src/powerpc/ffi_sysv.c 62 ${libffi_SOURCE_DIR}/src/powerpc/linux64.S 63 ${libffi_SOURCE_DIR}/src/powerpc/linux64_closure.S 64 ${libffi_SOURCE_DIR}/src/powerpc/ppc_closure.S 65 ${libffi_SOURCE_DIR}/src/powerpc/sysv.S 66 # Textual headers 67 ${libffi_SOURCE_DIR}/src/powerpc/ffi_powerpc.h 68 ${libffi_SOURCE_DIR}/src/powerpc/asm.h 69 ) 70elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64") 71 list(APPEND _ffi_platform_srcs 72 ${libffi_SOURCE_DIR}/src/aarch64/ffi.c 73 ${libffi_SOURCE_DIR}/src/aarch64/internal.h 74 ${libffi_SOURCE_DIR}/src/aarch64/sysv.S 75 ) 76endif() 77 78add_library(ffi STATIC 79 ${libffi_SOURCE_DIR}/fficonfig.h 80 ${libffi_SOURCE_DIR}/include/ffi.h 81 ${libffi_SOURCE_DIR}/include/ffi_cfi.h 82 ${libffi_SOURCE_DIR}/include/ffi_common.h 83 ${libffi_SOURCE_DIR}/include/ffitarget.h 84 ${libffi_SOURCE_DIR}/src/closures.c 85 ${libffi_SOURCE_DIR}/src/debug.c 86 ${libffi_SOURCE_DIR}/src/java_raw_api.c 87 ${libffi_SOURCE_DIR}/src/prep_cif.c 88 ${libffi_SOURCE_DIR}/src/raw_api.c 89 ${libffi_SOURCE_DIR}/src/types.c 90 ${_ffi_platform_srcs} 91) 92add_library(libffi::libffi ALIAS ffi) 93target_include_directories(ffi PUBLIC 94 ${libffi_SOURCE_DIR} 95 ${libffi_SOURCE_DIR}/include 96) 97target_compile_options(ffi PRIVATE 98 -Wno-vla 99 -Wno-unused-result 100) 101target_link_libraries(ffi PRIVATE 102 sapi::base 103) 104