Skip to content

Commit ff81b54

Browse files
committed
Enable downloading Bison on Windows
1 parent 08f4f74 commit ff81b54

File tree

2 files changed

+96
-33
lines changed

2 files changed

+96
-33
lines changed

cmake/cmake/modules/PHP/Bison.cmake

Lines changed: 94 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#[=============================================================================[
22
# PHP/Bison
33
4-
Finds the Bison command-line parser generator and provides a command to generate
5-
parser files with Bison:
4+
This module finds the Bison command-line parser generator and provides a command
5+
to generate parser files with Bison:
66
77
```cmake
88
include(PHP/Bison)
@@ -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. This variable
123+
specifies which GNU Bison 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
@@ -205,9 +210,16 @@ macro(_php_bison_config)
205210
set(PHP_BISON_VERSION 3.0.0)
206211
endif()
207212

208-
# 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)
213+
# If Bison is not found on the system, set which version to download for
214+
# POSIX platforms that might support GNU Bison.
215+
if(NOT PHP_BISON_GNU_VERSION_DOWNLOAD)
216+
set(PHP_BISON_GNU_VERSION_DOWNLOAD 3.8.2)
217+
endif()
218+
219+
# If Bison is not found on the Windows host system, set which winflexbison
220+
# version to download.
221+
if(NOT PHP_BISON_WIN_VERSION_DOWNLOAD)
222+
set(PHP_BISON_WIN_VERSION_DOWNLOAD 2.5.25)
211223
endif()
212224
endmacro()
213225

@@ -311,11 +323,10 @@ function(php_bison name input output)
311323

312324
if(
313325
NOT BISON_FOUND
314-
AND PHP_BISON_VERSION_DOWNLOAD
326+
AND PHP_BISON_GNU_VERSION_DOWNLOAD
327+
AND PHP_BISON_WIN_VERSION_DOWNLOAD
315328
AND packageType STREQUAL "REQUIRED"
316329
AND role STREQUAL "PROJECT"
317-
# TODO: Support for other platforms.
318-
AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux"
319330
)
320331
_php_bison_download()
321332
endif()
@@ -616,14 +627,58 @@ endfunction()
616627

617628
# Download and build Bison if not found.
618629
function(_php_bison_download)
619-
set(BISON_VERSION ${PHP_BISON_VERSION_DOWNLOAD})
630+
set(BISON_VERSION ${PHP_BISON_GNU_VERSION_DOWNLOAD})
620631
set(BISON_FOUND TRUE)
621632

622633
if(TARGET Bison::Bison)
623634
return(PROPAGATE BISON_FOUND BISON_VERSION)
624635
endif()
625636

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

628683
include(ExternalProject)
629684

@@ -641,28 +696,36 @@ function(_php_bison_download)
641696
)
642697

643698
ExternalProject_Get_Property(bison INSTALL_DIR)
699+
644700
set_property(CACHE BISON_EXECUTABLE PROPERTY VALUE ${INSTALL_DIR}/bin/bison)
701+
endfunction()
645702

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

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)
710+
get_directory_property(dir EP_BASE)
711+
if(NOT dir)
712+
set(dir "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles")
713+
endif()
659714

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

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

cmake/cmake/modules/PHP/Re2c.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#[=============================================================================[
22
# PHP/Re2c
33
4-
Finds the re2c command-line lexer generator and provides command to generate
5-
lexer files with re2c:
4+
This module finds the re2c command-line lexer generator and provides command to
5+
generate lexer files with re2c:
66
77
```cmake
88
include(PHP/Re2c)

0 commit comments

Comments
 (0)