1# - Additional help finding search paths on MinGW distributions, including MSYS2.
2#
3# Much of this is really more in the purview of CMake or the packages of CMake for
4# those distributions, but if I can centralize/simplify the pain here, it's worth doing.
5#
6# Variables: (all are internal cache variables)
7#  MINGWSEARCH_INCLUDE_DIRS - use under PATHS in your find_path() commands
8#  MINGWSEARCH_LIBRARY_DIRS - use under PATHS in your find_library() commands
9#  MINGWSEARCH_PREFIXES - suitable for temporary use in CMAKE_FIND_ROOT_PATH or CMAKE_PREFIX_PATH.
10#  MINGWSEARCH_TARGET_TRIPLE - something like x86_64-w64-mingw32 or i686-w64-mingw32, use as you see fit.
11#
12# Original Author:
13# 2016 Ryan Pavlik <ryan@sensics.com> <ryan.pavlik@gmail.com>
14#
15# Copyright Sensics, Inc. 2016.
16# Distributed under the Boost Software License, Version 1.0.
17# (See accompanying file LICENSE_1_0.txt or copy at
18# http://www.boost.org/LICENSE_1_0.txt)
19
20if(MINGW AND NOT MINGWSEARCH_COMPLETED)
21    ###
22    # Helper function
23    ###
24    function(_mingwsearch_conditional_add _var _path)
25        #message(STATUS "conditional add to ${_var}: ${_path}")
26        if(("${_path}" MATCHES "registry") OR (NOT IS_DIRECTORY "${_path}"))
27            # Path invalid - do not add
28            return()
29        endif()
30        list(FIND ${_var} "${_path}" _idx)
31        if(_idx GREATER -1)
32            # Path already in list - do not add
33            return()
34        endif()
35        # Not yet in the list, so we'll add it
36        list(APPEND ${_var} "${_path}")
37        set(${_var} ${${_var}} PARENT_SCOPE)
38    endfunction()
39
40    # Clear the working variables.
41    set(MINGWSEARCH_INCLUDE_DIRS_WORK)
42    set(MINGWSEARCH_LIBRARY_DIRS_WORK)
43    set(MINGWSEARCH_PREFIXES_WORK)
44    set(_mingw_target_triple)
45
46    # Try to find the string like x86_64-w64-mingw32 by parsing the implicit link directories...
47    # TODO this is a hack that either should be resolved in CMake or in MSYS2's package of CMake.
48    foreach(_link_dir ${CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES})
49        _mingwsearch_conditional_add(MINGWSEARCH_LIBRARY_DIRS_WORK "${_link_dir}")
50        if("${_link_dir}" MATCHES "/([^/]*-mingw32)/lib")
51            set(_mingw_target_triple ${CMAKE_MATCH_1})
52            get_filename_component(_mingw_internal_basedir "${_link_dir}" PATH)
53            # Try adding the parallel include dir
54            if(IS_DIRECTORY "${_mingw_internal_basedir}/include")
55                _mingwsearch_conditional_add(MINGWSEARCH_INCLUDE_DIRS_WORK "${_mingw_internal_basedir}/include")
56                _mingwsearch_conditional_add(MINGWSEARCH_PREFIXES_WORK "${_mingw_internal_basedir}")
57            endif()
58            if(NOT CMAKE_CROSSCOMPILING)
59                # Try going up a level, since the directory with the target is usually a sibling to the main prefix.
60                get_filename_component(_mingw_main_basedir_candidate "${_mingw_internal_basedir}/.." ABSOLUTE)
61                if(IS_DIRECTORY "${_mingw_main_basedir_candidate}/include" AND NOT ("${_mingw_main_basedir_candidate}" STREQUAL "${_mingw_internal_basedir}"))
62                    # If we could go up a level, add that include dir too.
63                    _mingwsearch_conditional_add(MINGWSEARCH_INCLUDE_DIRS_WORK "${_mingw_main_basedir_candidate}/include")
64                    _mingwsearch_conditional_add(MINGWSEARCH_PREFIXES_WORK "${_mingw_main_basedir_candidate}")
65                endif()
66            endif()
67        endif()
68    endforeach()
69
70    ###
71    # Output results.
72    ###
73    if(MINGWSEARCH_INCLUDE_DIRS_WORK)
74        set(MINGWSEARCH_INCLUDE_DIRS "${MINGWSEARCH_INCLUDE_DIRS_WORK}" CACHE INTERNAL "" FORCE)
75        #message(STATUS "MINGWSEARCH_INCLUDE_DIRS ${MINGWSEARCH_INCLUDE_DIRS}")
76    endif()
77
78    if(MINGWSEARCH_LIBRARY_DIRS_WORK)
79        set(MINGWSEARCH_LIBRARY_DIRS "${MINGWSEARCH_LIBRARY_DIRS_WORK}" CACHE INTERNAL "" FORCE)
80        #message(STATUS "MINGWSEARCH_LIBRARY_DIRS ${MINGWSEARCH_LIBRARY_DIRS}")
81    endif()
82
83    if(MINGWSEARCH_PREFIXES_WORK)
84        set(MINGWSEARCH_PREFIXES "${MINGWSEARCH_PREFIXES_WORK}" CACHE INTERNAL "" FORCE)
85        #message(STATUS "MINGWSEARCH_PREFIXES ${MINGWSEARCH_PREFIXES}")
86    endif()
87
88    if(_mingw_target_triple)
89        set(MINGWSEARCH_TARGET_TRIPLE ${_mingw_target_triple} CACHE INTERNAL "" FORCE)
90        #message(STATUS "MINGWSEARCH_TARGET_TRIPLE ${MINGWSEARCH_TARGET_TRIPLE}")
91    endif()
92
93    set(MINGWSEARCH_COMPLETED TRUE CACHE INTERNAL "" FORCE)
94endif()
95