Skip to content

Commit

Permalink
Improvements to FindWine
Browse files Browse the repository at this point in the history
  • Loading branch information
tresf committed May 19, 2024
1 parent 5b5a3b0 commit 77547d7
Showing 1 changed file with 71 additions and 16 deletions.
87 changes: 71 additions & 16 deletions cmake/modules/FindWine.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
# Once done this will define
#
# WINE_FOUND - System has wine
# WINE_INCLUDE_DIRS - The wine include directories
# WINE_DEFINITIONS - Compiler switches required for using wine
#
# WINE_INCLUDE_DIR - Wine include directory
# WINE_BUILD - Path to winebuild
# WINE_CXX - Path to wineg++
# WINE_CPP - Path to winegcc
# WINE_32_LIBRARY_DIRS - Path(s) to 32-bit wine libs
# WINE_32_FLAGS - 32-bit linker flags
# WINE_64_LIBRARY_DIRS - Path to 64-bit wine libs
# WINE_64_FLAGS - 64-bit linker flags
#

MACRO(_findwine_find_flags output expression result)
Expand All @@ -23,28 +30,63 @@ MACRO(_regex_replace_foreach EXPRESSION REPLACEMENT RESULT INPUT)
ENDFOREACH()
ENDMACRO()

LIST(APPEND CMAKE_PREFIX_PATH /opt/wine-stable /opt/wine-devel /opt/wine-staging /usr/lib/wine/)
# Prefer newest wine first
list(APPEND WINE_LOCATIONS
/opt/wine-staging
/opt/wine-devel
/opt/wine-stable
/usr/lib/wine)

# Prepare bin search
foreach(_loc ${WINE_LOCATIONS})
if(_loc STREQUAL /usr/lib/wine)
# /usr/lib/wine doesn't have a "bin"
list(APPEND WINE_CXX_LOCATIONS "${_loc}")
else()
# expect "bin"
list(APPEND WINE_CXX_LOCATIONS "${_loc}/bin")
endif()
endforeach()
# Fallback
list(APPEND WINE_CXX_LOCATIONS "/usr/bin")

# Prefer most-common to least common
FIND_PROGRAM(WINE_CXX NAMES
wineg++
wineg++-stable
winecpp
winecpp-stable
PATHS
${WINE_CXX_LOCATIONS}
NO_DEFAULT_PATH
)

FIND_PROGRAM(WINE_CXX
NAMES wineg++ winegcc winegcc64 winegcc32 winegcc-stable
PATHS /usr/lib/wine /usr/bin/
FIND_PROGRAM(WINE_GCC NAMES
winegcc
winegcc-stable
PATHS
${WINE_CXX_LOCATIONS}
NO_DEFAULT_PATH
)
FIND_PROGRAM(WINE_BUILD NAMES winebuild)

FIND_PROGRAM(WINE_BUILD NAMES winebuild PATHS ${WINE_CXX_LOCATIONS} NO_DEFAULT_PATH)
# Detect wine paths and handle linking problems
IF(WINE_CXX)
execute_process(COMMAND ${WINE_CXX} -m32 -v /dev/zero OUTPUT_VARIABLE WINEBUILD_OUTPUT_32)
execute_process(COMMAND ${WINE_CXX} -m64 -v /dev/zero OUTPUT_VARIABLE WINEBUILD_OUTPUT_64)
message("Output of winebuild_32 : ${WINEBUILD_OUTPUT_32}")
message("Output of winebuild_64 : ${WINEBUILD_OUTPUT_64}")
# call winegcc to obtain implied includes and libs
execute_process(COMMAND ${WINE_CXX} -m32 -v /dev/null OUTPUT_VARIABLE WINEBUILD_OUTPUT_32 ERROR_VARIABLE WINEBUILD_OUTPUT_32B)
execute_process(COMMAND ${WINE_CXX} -m64 -v /dev/null OUTPUT_VARIABLE WINEBUILD_OUTPUT_64 ERROR_VARIABLE WINEBUILD_OUTPUT_64B)

_findwine_find_flags("${WINEBUILD_OUTPUT_32}" "^-isystem/usr/include$" BUGGED_WINEGCC)
_findwine_find_flags("${WINEBUILD_OUTPUT_32}" "^-isystem" WINEGCC_INCLUDE_DIR)
_findwine_find_flags("${WINEBUILD_OUTPUT_32}" "libwinecrt0\\.a.*" WINECRT_32)
_findwine_find_flags("${WINEBUILD_OUTPUT_64}" "libwinecrt0\\.a.*" WINECRT_64)
_regex_replace_foreach("^-isystem" "" WINE_INCLUDE_HINT "${WINEGCC_INCLUDE_DIR}")
_regex_replace_foreach("/wine/windows$" "" WINE_INCLUDE_HINT "${WINE_INCLUDE_HINT}")
MESSAGE("WINE HINT AFTER REPLACEMENT : ${WINE_INCLUDE_HINT}")
STRING(REGEX REPLACE "wine/libwinecrt0\\.a.*" "" WINE_32_LIBRARY_DIR "${WINECRT_32}")
STRING(REGEX REPLACE "wine/libwinecrt0\\.a.*" "" WINE_64_LIBRARY_DIR "${WINECRT_64}")
# Handle winehq
STRING(REGEX REPLACE "/libwinecrt0\\.a.*" "/" WINE_32_LIBRARY_DIR "${WINE_32_LIBRARY_DIR}")
STRING(REGEX REPLACE "/libwinecrt0\\.a.*" "/" WINE_64_LIBRARY_DIR "${WINE_64_LIBRARY_DIR}")

IF(BUGGED_WINEGCC)
MESSAGE(WARNING "Your winegcc is unusable due to https://bugs.winehq.org/show_bug.cgi?id=46293,\n
Expand Down Expand Up @@ -86,8 +128,7 @@ IF(WINE_CXX)
ENDIF()

FIND_PATH(WINE_INCLUDE_DIR wine/exception.h
HINTS ${WINE_INCLUDE_HINT}
/usr/include/wine/ # Fallback for not finding the header in variable
HINTS ${WINE_INCLUDE_HINT}
)

SET(_ARCHITECTURE ${CMAKE_LIBRARY_ARCHITECTURE})
Expand All @@ -102,7 +143,11 @@ find_package_handle_standard_args(Wine DEFAULT_MSG WINE_CXX WINE_INCLUDE_DIRS)
mark_as_advanced(WINE_INCLUDE_DIR WINE_LIBRARY WINE_CXX WINE_BUILD)

IF(WINE_32_LIBRARY_DIR)
IF(WINE_32_LIBRARY_DIR MATCHES "wine*/lib")
IF(WINE_32_LIBRARY_DIR MATCHES "^/opt/wine-.*")
# winehq uses a singular lib directory
SET(WINE_32_FLAGS "-L${WINE_32_LIBRARY_DIR}")
SET(WINE_32_LIBRARY_DIRS "${WINE_32_LIBRARY_DIR}")
ELSEIF(WINE_32_LIBRARY_DIR MATCHES "wine*/lib")
SET(WINE_32_FLAGS "-L${WINE_32_LIBRARY_DIR} -L${WINE_32_LIBRARY_DIR}../")
SET(WINE_32_LIBRARY_DIRS "${WINE_32_LIBRARY_DIR}:${WINE_32_LIBRARY_DIR}/..")
ELSE()
Expand All @@ -112,7 +157,11 @@ IF(WINE_32_LIBRARY_DIR)
ENDIF()

IF(WINE_64_LIBRARY_DIR)
IF(WINE_64_LIBRARY_DIR MATCHES "wine*/lib")
IF(WINE_32_LIBRARY_DIR MATCHES "^/opt/wine-.*")
# winehq uses a singular lib directory
SET(WINE_64_FLAGS "-L${WINE_64_LIBRARY_DIR}")
SET(WINE_64_LIBRARY_DIRS "${WINE_64_LIBRARY_DIR}")
ELSEIF(WINE_64_LIBRARY_DIR MATCHES "wine*/lib")
SET(WINE_64_FLAGS "-L${WINE_64_LIBRARY_DIR} -L${WINE_64_LIBRARY_DIR}../")
SET(WINE_64_LIBRARY_DIRS "${WINE_64_LIBRARY_DIR}:${WINE_64_LIBRARY_DIR}/..")
ELSE()
Expand All @@ -121,6 +170,12 @@ IF(WINE_64_LIBRARY_DIR)
ENDIF()
ENDIF()

message(STATUS " WINE_INCLUDE_DIR: ${WINE_INCLUDE_DIR}")
message(STATUS " WINE_CXX: ${WINE_CXX}")
message(STATUS " WINE_GCC: ${WINE_GCC}")
message(STATUS " WINE_32_FLAGS: ${WINE_32_FLAGS}")
message(STATUS " WINE_64_FLAGS: ${WINE_64_FLAGS}")

# Create winegcc wrapper
configure_file(${CMAKE_CURRENT_LIST_DIR}/winegcc_wrapper.in winegcc_wrapper @ONLY)
SET(WINEGCC "${CMAKE_CURRENT_BINARY_DIR}/winegcc_wrapper")

0 comments on commit 77547d7

Please sign in to comment.