|
| 1 | +# CMake-based PHP build system wrapper |
| 2 | +# |
| 3 | +# It downloads PHP tarball, adds CMake files from the cmake development |
| 4 | +# directory in this repository and applies patches to downloaded PHP source code |
| 5 | +# for running CMake commands. This file is not part of the CMake-based build |
| 6 | +# system itself but is only a wrapper to simplify using CMake files in this |
| 7 | +# repository and upstream PHP sources. |
| 8 | + |
| 9 | +cmake_minimum_required(VERSION 3.25...3.31) |
| 10 | + |
| 11 | +if(CMAKE_SOURCE_DIR PATH_EQUAL CMAKE_BINARY_DIR) |
| 12 | + message( |
| 13 | + FATAL_ERROR |
| 14 | + "In-source builds are disabled. Please set build directory. For example:\n" |
| 15 | + " cmake -B build\n" |
| 16 | + " cmake --build build -j" |
| 17 | + ) |
| 18 | +endif() |
| 19 | + |
| 20 | +project( |
| 21 | + PhpBuildSystem |
| 22 | + VERSION 8.3 |
| 23 | + DESCRIPTION "CMake-based PHP build system" |
| 24 | + HOMEPAGE_URL "https://github.com/petk/php-build-system" |
| 25 | + LANGUAGES NONE |
| 26 | +) |
| 27 | + |
| 28 | +set(phpVersion "${PhpBuildSystem_VERSION}-dev") |
| 29 | +set(phpBranch "PHP-${PhpBuildSystem_VERSION}") |
| 30 | + |
| 31 | +include(FetchContent) |
| 32 | + |
| 33 | +cmake_path( |
| 34 | + RELATIVE_PATH |
| 35 | + CMAKE_CURRENT_BINARY_DIR |
| 36 | + BASE_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 37 | + OUTPUT_VARIABLE relativeDir |
| 38 | +) |
| 39 | + |
| 40 | +# Create script for the FetchContent_Declare(PATCH_COMMAND) option below. |
| 41 | +file( |
| 42 | + CONFIGURE |
| 43 | + OUTPUT CMakeFiles/PhpBuildSystem/patch.cmake |
| 44 | + CONTENT [=[ |
| 45 | + set(relativeDir "@relativeDir@/_deps/php-src") |
| 46 | + set(destination "@CMAKE_CURRENT_BINARY_DIR@/_deps/php-src") |
| 47 | + |
| 48 | + message(STATUS "Adding CMake files to ${relativeDir}") |
| 49 | + file(COPY "@CMAKE_CURRENT_SOURCE_DIR@/cmake/" DESTINATION "${destination}") |
| 50 | + |
| 51 | + find_package(Git QUIET) |
| 52 | + if(NOT GIT_FOUND) |
| 53 | + message(WARNING "Git not found. Patches are not be applied to ${relativeDir}.") |
| 54 | + return() |
| 55 | + endif() |
| 56 | + |
| 57 | + string(REGEX MATCH [[^([0-9]+\.[0-9]+)]] _ "@PhpBuildSystem_VERSION@") |
| 58 | + file(GLOB patches "@CMAKE_CURRENT_SOURCE_DIR@/patches/${CMAKE_MATCH_1}/*.patch") |
| 59 | + |
| 60 | + if(NOT patches) |
| 61 | + return() |
| 62 | + endif() |
| 63 | + |
| 64 | + message(STATUS "Applying patches to ${relativeDir}\n") |
| 65 | + |
| 66 | + # Add .git directory to be able to apply patches. |
| 67 | + execute_process( |
| 68 | + COMMAND ${GIT_EXECUTABLE} init |
| 69 | + WORKING_DIRECTORY ${destination} |
| 70 | + RESULT_VARIABLE result |
| 71 | + OUTPUT_VARIABLE output |
| 72 | + ERROR_VARIABLE error |
| 73 | + ERROR_STRIP_TRAILING_WHITESPACE |
| 74 | + OUTPUT_QUIET |
| 75 | + ) |
| 76 | + if(NOT result EQUAL 0) |
| 77 | + message(FATAL_ERROR "Adding .git directory failed:\n${output}\n\n${error}") |
| 78 | + endif() |
| 79 | + |
| 80 | + execute_process( |
| 81 | + COMMAND ${GIT_EXECUTABLE} apply --ignore-whitespace ${patches} |
| 82 | + WORKING_DIRECTORY ${destination} |
| 83 | + RESULT_VARIABLE result |
| 84 | + OUTPUT_VARIABLE output |
| 85 | + ERROR_VARIABLE error |
| 86 | + ERROR_STRIP_TRAILING_WHITESPACE |
| 87 | + OUTPUT_QUIET |
| 88 | + ) |
| 89 | + |
| 90 | + if(NOT result EQUAL 0) |
| 91 | + message(WARNING "Failed to apply patches:\n${output}\n\n${error}.") |
| 92 | + endif() |
| 93 | + |
| 94 | + # Clean temporary .git directory. Checks are done as safeguards. |
| 95 | + if( |
| 96 | + IS_DIRECTORY ${destination}/.git |
| 97 | + AND EXISTS ${destination}/php.ini-development |
| 98 | + AND EXISTS ${destination}/main/php_version.h |
| 99 | + AND EXISTS ${destination}/CMakeLists.txt |
| 100 | + ) |
| 101 | + file(REMOVE_RECURSE ${destination}/.git/) |
| 102 | + endif() |
| 103 | + ]=] |
| 104 | + @ONLY |
| 105 | +) |
| 106 | + |
| 107 | +FetchContent_Declare( |
| 108 | + php |
| 109 | + URL https://github.com/php/php-src/archive/refs/heads/${phpBranch}.tar.gz |
| 110 | + PATCH_COMMAND |
| 111 | + ${CMAKE_COMMAND} |
| 112 | + -P |
| 113 | + ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/PhpBuildSystem/patch.cmake |
| 114 | +) |
| 115 | + |
| 116 | +message(STATUS "Downloading PHP ${phpVersion}") |
| 117 | +FetchContent_MakeAvailable(php) |
0 commit comments