Skip to content

Commit 87e2a7a

Browse files
committed
Add Bison download for Windows
1 parent 35aafe6 commit 87e2a7a

File tree

1 file changed

+90
-28
lines changed

1 file changed

+90
-28
lines changed

cmake/cmake/modules/PHP/Bison.cmake

Lines changed: 90 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,15 @@ These variables can be set before using this module to configure behavior:
117117
* `PHP_BISON_VERSION` - The version constraint, when looking for BISON package
118118
with `find_package(BISON <version-constraint> ...)` in this module.
119119

120-
* `PHP_BISON_VERSION_DOWNLOAD` - When Bison cannot be found on the system or the
121-
found version is not suitable, this module can also download and build it from
122-
its release archive sources as part of the project build. Set which version
123-
should be downloaded.
120+
* `PHP_BISON_GNU_VERSION_DOWNLOAD` - When Bison cannot be found on the system or
121+
the found version is not suitable, this module can also download and build it
122+
from its release archive sources as part of the project build. Set which
123+
version should be downloaded.
124+
125+
* `PHP_BISON_WIN_VERSION_DOWNLOAD` - When Bison cannot be found on the Windows
126+
host system or the found version is not suitable, this module can also
127+
download [`win_bison.exe`](https://github.com/lexxmark/winflexbison). This
128+
variable specifies which `winflexbison` version should be downloaded.
124129

125130
* `PHP_BISON_WORKING_DIRECTORY` - Set the default global working directory
126131
for all `php_bison()` invocations in the directory scope where the
@@ -206,8 +211,14 @@ macro(_php_bison_config)
206211
endif()
207212

208213
# If Bison is not found on the system, set which version to download.
209-
if(NOT PHP_BISON_VERSION_DOWNLOAD)
210-
set(PHP_BISON_VERSION_DOWNLOAD 3.8.2)
214+
if(NOT PHP_BISON_GNU_VERSION_DOWNLOAD)
215+
set(PHP_BISON_GNU_VERSION_DOWNLOAD 3.8.2)
216+
endif()
217+
218+
# If Bison is not found on the Windows host system, set which winflexbison
219+
# version to download.
220+
if(NOT PHP_BISON_WIN_VERSION_DOWNLOAD)
221+
set(PHP_BISON_WIN_VERSION_DOWNLOAD 2.5.25)
211222
endif()
212223
endmacro()
213224

@@ -311,11 +322,10 @@ function(php_bison name input output)
311322

312323
if(
313324
NOT BISON_FOUND
314-
AND PHP_BISON_VERSION_DOWNLOAD
325+
AND PHP_BISON_GNU_VERSION_DOWNLOAD
326+
AND PHP_BISON_WIN_VERSION_DOWNLOAD
315327
AND packageType STREQUAL "REQUIRED"
316328
AND role STREQUAL "PROJECT"
317-
# TODO: Support for other platforms.
318-
AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux"
319329
)
320330
_php_bison_download()
321331
endif()
@@ -616,14 +626,58 @@ endfunction()
616626

617627
# Download and build Bison if not found.
618628
function(_php_bison_download)
619-
set(BISON_VERSION ${PHP_BISON_VERSION_DOWNLOAD})
629+
set(BISON_VERSION ${PHP_BISON_GNU_VERSION_DOWNLOAD})
620630
set(BISON_FOUND TRUE)
621631

622632
if(TARGET Bison::Bison)
623633
return(PROPAGATE BISON_FOUND BISON_VERSION)
624634
endif()
625635

626-
message(STATUS "Bison ${BISON_VERSION} will be downloaded at build phase")
636+
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
637+
_php_bison_download_windows()
638+
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
639+
_php_bison_download_gnu()
640+
else()
641+
# TODO: Add support for more platforms.
642+
message(
643+
WARNING
644+
"Bison couldn't be downloaded. The current platform ${CMAKE_SYSTEM_NAME} "
645+
"is not yet supported by PHP/Bison module. Please install Bison manually."
646+
)
647+
return()
648+
endif()
649+
650+
add_executable(Bison::Bison IMPORTED GLOBAL)
651+
set_target_properties(
652+
Bison::Bison
653+
PROPERTIES IMPORTED_LOCATION ${BISON_EXECUTABLE}
654+
)
655+
656+
# Target created by ExternalProject:
657+
if(TARGET bison)
658+
add_dependencies(Bison::Bison bison)
659+
endif()
660+
661+
# Move dependency to PACKAGES_FOUND.
662+
get_property(packagesNotFound GLOBAL PROPERTY PACKAGES_NOT_FOUND)
663+
list(REMOVE_ITEM packagesNotFound BISON)
664+
set_property(GLOBAL PROPERTY PACKAGES_NOT_FOUND ${packagesNotFound})
665+
get_property(packagesFound GLOBAL PROPERTY PACKAGES_FOUND)
666+
set_property(GLOBAL APPEND PROPERTY PACKAGES_FOUND BISON)
667+
668+
set(
669+
_PHP_BISON_DOWNLOAD
670+
TRUE
671+
CACHE INTERNAL
672+
"Internal marker whether the Bison will be downloaded."
673+
)
674+
675+
return(PROPAGATE BISON_FOUND BISON_VERSION)
676+
endfunction()
677+
678+
# Downloads GNU Bison.
679+
function(_php_bison_download_gnu)
680+
message(STATUS "GNU Bison ${BISON_VERSION} will be downloaded at build phase")
627681

628682
include(ExternalProject)
629683

@@ -641,28 +695,36 @@ function(_php_bison_download)
641695
)
642696

643697
ExternalProject_Get_Property(bison INSTALL_DIR)
698+
644699
set_property(CACHE BISON_EXECUTABLE PROPERTY VALUE ${INSTALL_DIR}/bin/bison)
700+
endfunction()
645701

646-
add_executable(Bison::Bison IMPORTED GLOBAL)
647-
set_target_properties(
648-
Bison::Bison
649-
PROPERTIES IMPORTED_LOCATION ${BISON_EXECUTABLE}
702+
# Downloads https://github.com/lexxmark/winflexbison.
703+
function(_php_bison_download_windows)
704+
message(
705+
STATUS
706+
"Downloading win_bison ${BISON_VERSION} (${PHP_BISON_WIN_VERSION_DOWNLOAD})"
650707
)
651-
add_dependencies(Bison::Bison bison)
652708

653-
# Move dependency to PACKAGES_FOUND.
654-
get_property(packagesNotFound GLOBAL PROPERTY PACKAGES_NOT_FOUND)
655-
list(REMOVE_ITEM packagesNotFound BISON)
656-
set_property(GLOBAL PROPERTY PACKAGES_NOT_FOUND ${packagesNotFound})
657-
get_property(packagesFound GLOBAL PROPERTY PACKAGES_FOUND)
658-
set_property(GLOBAL APPEND PROPERTY PACKAGES_FOUND BISON)
709+
get_directory_property(dir EP_BASE)
710+
if(NOT dir)
711+
set(dir "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles")
712+
endif()
659713

660-
set(
661-
_PHP_BISON_DOWNLOAD
662-
TRUE
663-
CACHE INTERNAL
664-
"Internal marker whether the Bison will be downloaded."
714+
set(file "${dir}/win_flex_bison.zip")
715+
716+
file(
717+
DOWNLOAD
718+
"https://github.com/lexxmark/winflexbison/releases/download/v${PHP_BISON_WIN_VERSION_DOWNLOAD}/win_flex_bison-${PHP_BISON_WIN_VERSION_DOWNLOAD}.zip"
719+
${file}
720+
SHOW_PROGRESS
665721
)
666722

667-
return(PROPAGATE BISON_FOUND BISON_VERSION)
723+
file(ARCHIVE_EXTRACT INPUT "${file}" DESTINATION "${dir}/win_flex_bison")
724+
725+
set_property(
726+
CACHE
727+
BISON_EXECUTABLE
728+
PROPERTY VALUE "${dir}/win_flex_bison/win_bison.exe"
729+
)
668730
endfunction()

0 commit comments

Comments
 (0)