generalize ./installdeps + mac pkg mgrs support

Add a more general purpose ./installdeps script to replace
./installdeps-msys2 that handles: Debian Linux, Arch Linux, MSYS2+MinGW,
Mac Homebrew, MacPorts and Fink for Mac. More OSes and distributions
will be added in the future, and the script may be generalized for use
in other projects.

Move Mac package manager setup code into MacPackageManagers.cmake and
greatly improve it. Handles Homebrew, MacPorts and Fink correctly.

Minor tweaks for FindSDL2.cmake: better include dir searching that works
on cygwin, fix flags for MinGW, and quiet pkg-config program errors.

Add MinGW cross-compilation toolchain files, they can be specified on
the cmake command line with:

-DCMAKE_TOOLCHAIN_FILE=../CMakeScripts/Toolchain-cross-MinGW-w64-i686.cmake

or

-DCMAKE_TOOLCHAIN_FILE=../CMakeScripts/Toolchain-cross-MinGW-w64-x86_64.cmake

these work for Cygwin and should work for MinGW on Linux as well.
This commit is contained in:
Rafael Kitover 2016-12-10 23:28:28 -08:00
parent 0e9fb98393
commit c216528793
7 changed files with 313 additions and 87 deletions

View File

@ -109,44 +109,7 @@ ENDIF()
# Add support for Homebrew, MacPorts and Fink on OS X
IF(APPLE)
IF(EXISTS /usr/local/include)
SET(CMAKE_INCLUDE_PATH "${CMAKE_INCLUDE_PATH};/usr/local/include")
INCLUDE_DIRECTORIES("/usr/local/include")
ELSEIF(EXISTS /opt/local/include)
SET(CMAKE_INCLUDE_PATH "${CMAKE_INCLUDE_PATH};/opt/local/include")
INCLUDE_DIRECTORIES("/opt/local/include")
ELSEIF(EXISTS /sw/include)
SET(CMAKE_INCLUDE_PATH "${CMAKE_INCLUDE_PATH};/sw/include")
INCLUDE_DIRECTORIES("/sw/include")
ENDIF()
IF(EXISTS /usr/local/lib)
SET(CMAKE_LIBRARY_PATH "${CMAKE_LIBRARY_PATH};/usr/local/lib")
LINK_DIRECTORIES("/usr/local/lib")
ELSEIF(EXISTS /opt/local/lib)
SET(CMAKE_LIBRARY_PATH "${CMAKE_LIBRARY_PATH};/opt/local/lib")
LINK_DIRECTORIES("/opt/local/lib")
ELSEIF(EXISTS /sw/lib)
SET(CMAKE_LIBRARY_PATH "${CMAKE_LIBRARY_PATH};/sw/lib")
LINK_DIRECTORIES("/sw/lib")
ENDIF()
IF(EXISTS /usr/local/bin)
SET(CMAKE_PROGRAM_PATH "${CMAKE_PROGRAM_PATH};/usr/local/bin")
IF(EXISTS "/usr/local/bin/nasm")
SET(CMAKE_ASM_NASM_COMPILER "/usr/local/bin/nasm")
ENDIF()
ELSEIF(EXISTS /opt/local/bin)
SET(CMAKE_PROGRAM_PATH "${CMAKE_PROGRAM_PATH};/opt/local/bin")
IF(EXISTS "/opt/local/bin/nasm")
SET(CMAKE_ASM_NASM_COMPILER "/opt/local/bin/nasm")
ENDIF()
ELSEIF(EXISTS /sw/bin)
SET(CMAKE_PROGRAM_PATH "${CMAKE_PROGRAM_PATH};/sw/bin")
IF(EXISTS "/sw/bin/nasm")
SET(CMAKE_ASM_NASM_COMPILER "/sw/bin/nasm")
ENDIF()
ENDIF()
INCLUDE(MacPackageManagers)
ENDIF(APPLE)
# We do not support amd64 asm yet
@ -166,9 +129,9 @@ FIND_PACKAGE(OpenGL REQUIRED)
# TODO: make static on mac
FIND_PACKAGE(PNG REQUIRED)
IF(APPLE)
IF(APPLE AND NOT MACPORTS)
SET(SDL2_STATIC ON)
ENDIF(APPLE)
ENDIF()
FIND_PACKAGE(SDL2 REQUIRED)
ADD_DEFINITIONS(${SDL2_DEFINITIONS})

View File

@ -80,24 +80,21 @@
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)
message("<FindSDL2.cmake>")
SET(SDL2_SEARCH_PATHS
~/Library/Frameworks
/Library/Frameworks
/usr/local
/usr/local # Mac Homebrew and local installs
/usr
/sw # Fink
/opt/local # DarwinPorts
/opt/csw # Blastwave
/opt/local # MacPorts
/opt/csw # OpenCSW (Solaris)
/opt
${SDL2_PATH}
)
FIND_PATH(SDL2_INCLUDE_DIR SDL.h
HINTS
$ENV{SDL2DIR}
PATH_SUFFIXES include/SDL2 include
HINTS $ENV{SDL2DIR}
PATH_SUFFIXES SDL2
PATHS ${SDL2_SEARCH_PATHS}
)
@ -113,8 +110,7 @@ ENDIF(SDL2_STATIC)
FIND_LIBRARY(SDL2_LIBRARY_TEMP
NAMES SDL2
HINTS
$ENV{SDL2DIR}
HINTS $ENV{SDL2DIR}
PATH_SUFFIXES lib64 lib lib/x64 lib/x86
PATHS ${SDL2_SEARCH_PATHS}
)
@ -149,7 +145,7 @@ ENDIF(NOT APPLE)
# MinGW needs an additional link flag, -mwindows
# It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -mwindows
IF(MINGW)
SET(MINGW32_LIBRARY mingw32 "-mwindows" CACHE STRING "mwindows for MinGW")
SET(MINGW32_LIBRARY -lmingw32 -mwindows CACHE STRING "mwindows for MinGW")
ENDIF(MINGW)
IF(SDL2_LIBRARY_TEMP)
@ -189,18 +185,18 @@ IF(SDL2_LIBRARY_TEMP)
IF(PKG_CONFIG_EXECUTABLE)
# get any definitions
EXECUTE_PROCESS(COMMAND ${PKG_CONFIG_EXECUTABLE} --cflags-only-other sdl2 OUTPUT_VARIABLE SDL2_DEFINITIONS)
EXECUTE_PROCESS(COMMAND ${PKG_CONFIG_EXECUTABLE} --cflags-only-other sdl2 OUTPUT_VARIABLE SDL2_DEFINITIONS ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
SET(SDL2_DEFINITIONS ${SDL2_DEFINITIONS} CACHE STRING "Extra CFLAGS for SDL2 from pkg-config")
# get any extra stuff needed for linking
IF(NOT SDL2_STATIC)
EXECUTE_PROCESS(COMMAND ${PKG_CONFIG_EXECUTABLE} --libs-only-other sdl2 OUTPUT_VARIABLE SDL2_LINKER_FLAGS_RAW OUTPUT_STRIP_TRAILING_WHITESPACE)
EXECUTE_PROCESS(COMMAND ${PKG_CONFIG_EXECUTABLE} --libs-only-other sdl2 OUTPUT_VARIABLE SDL2_LINKER_FLAGS_RAW ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
EXECUTE_PROCESS(COMMAND ${PKG_CONFIG_EXECUTABLE} --libs-only-l sdl2 OUTPUT_VARIABLE SDL2_EXTRA_LIBS_RAW OUTPUT_STRIP_TRAILING_WHITESPACE)
EXECUTE_PROCESS(COMMAND ${PKG_CONFIG_EXECUTABLE} --libs-only-l sdl2 OUTPUT_VARIABLE SDL2_EXTRA_LIBS_RAW ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
ELSE(NOT SDL2_STATIC)
EXECUTE_PROCESS(COMMAND ${PKG_CONFIG_EXECUTABLE} --static --libs-only-other sdl2 OUTPUT_VARIABLE SDL2_LINKER_FLAGS_RAW OUTPUT_STRIP_TRAILING_WHITESPACE)
EXECUTE_PROCESS(COMMAND ${PKG_CONFIG_EXECUTABLE} --static --libs-only-l sdl2 OUTPUT_VARIABLE SDL2_EXTRA_LIBS_RAW OUTPUT_STRIP_TRAILING_WHITESPACE)
EXECUTE_PROCESS(COMMAND ${PKG_CONFIG_EXECUTABLE} --static --libs-only-other sdl2 OUTPUT_VARIABLE SDL2_LINKER_FLAGS_RAW ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
EXECUTE_PROCESS(COMMAND ${PKG_CONFIG_EXECUTABLE} --static --libs-only-l sdl2 OUTPUT_VARIABLE SDL2_EXTRA_LIBS_RAW ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
ENDIF(NOT SDL2_STATIC)
STRING(REGEX REPLACE "[^ ]+SDL2[^ ]*" "" SDL2_EXTRA_LIBS_RAW2 "${SDL2_EXTRA_LIBS_RAW}")
@ -219,8 +215,6 @@ IF(SDL2_LIBRARY_TEMP)
SET(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "")
ENDIF(SDL2_LIBRARY_TEMP)
message("</FindSDL2.cmake>")
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR)

View File

@ -0,0 +1,106 @@
# This module checks for Mac Homebrew, MacPorts, or Fink, in that order, on Mac
# OS X.
#
# It will prepend the active package manager's paths to:
#
# CMAKE_FRAMEWORK_PATH
# CMAKE_INCLUDE_PATH
# CMAKE_LIBRARY_PATH
# CMAKE_PROGRAM_PATH
#
# In addition, the following commands are called with the package manager's
# paths:
#
# INCLUDE_DIRECTORIES()
# LINK_DIRECTORIES()
#
# The paths of package managers not currently in $ENV{PATH} are added to
# CMAKE_IGNORE_PATH .
#
# Copyright (c) 2016, Rafael Kitover
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
IF(NOT APPLE)
RETURN()
ENDIF()
IF(EXISTS /usr/local/bin/brew AND $ENV{PATH} MATCHES "(^|:)/usr/local/bin/?(:|$)")
MESSAGE("-- Configuring for Mac Homebrew")
SET(MAC_HOMEBREW ON)
SET(CMAKE_IGNORE_PATH /opt/local /opt/local/bin /opt/local/include /opt/local/Library/Frameworks /opt/local/lib ${CMAKE_IGNORE_PATH})
SET(CMAKE_IGNORE_PATH /sw /sw/bin /sw/include /sw/Library/Frameworks /sw/lib ${CMAKE_IGNORE_PATH})
SET(CMAKE_FRAMEWORK_PATH /usr/local/Frameworks ${CMAKE_FRAMEWORK_PATH})
SET(CMAKE_INCLUDE_PATH /usr/local/include ${CMAKE_INCLUDE_PATH})
INCLUDE_DIRECTORIES("/usr/local/include")
SET(CMAKE_LIBRARY_PATH /usr/local/lib ${CMAKE_LIBRARY_PATH})
LINK_DIRECTORIES("/usr/local/lib")
SET(CMAKE_PROGRAM_PATH /usr/local/bin ${CMAKE_PROGRAM_PATH})
ELSEIF(EXISTS /opt/local/bin/port AND $ENV{PATH} MATCHES "(^|:)/opt/local/bin/?(:|$)")
MESSAGE("-- Configuring for MacPorts")
SET(MACPORTS ON)
SET(CMAKE_IGNORE_PATH /sw /sw/bin /sw/include /sw/Library/Frameworks /sw/lib ${CMAKE_IGNORE_PATH})
SET(CMAKE_FRAMEWORK_PATH /opt/local/Library/Frameworks ${CMAKE_FRAMEWORK_PATH})
SET(CMAKE_INCLUDE_PATH /opt/local/include ${CMAKE_INCLUDE_PATH})
INCLUDE_DIRECTORIES("/opt/local/include")
SET(CMAKE_LIBRARY_PATH /opt/local/lib ${CMAKE_LIBRARY_PATH})
LINK_DIRECTORIES("/opt/local/lib")
SET(CMAKE_PROGRAM_PATH /opt/local/bin ${CMAKE_PROGRAM_PATH})
ELSEIF(EXISTS /sw/bin/fink AND $ENV{PATH} MATCHES "(^|:)/sw/bin/?(:|$)")
MESSAGE("-- Configuring for Fink")
SET(FINK ON)
SET(CMAKE_IGNORE_PATH /opt/local /opt/local/bin /opt/local/include /opt/local/Library/Frameworks /opt/local/lib ${CMAKE_IGNORE_PATH})
SET(CMAKE_FRAMEWORK_PATH /sw/Library/Frameworks ${CMAKE_FRAMEWORK_PATH})
SET(CMAKE_INCLUDE_PATH /sw/include ${CMAKE_INCLUDE_PATH})
INCLUDE_DIRECTORIES("/sw/include")
SET(CMAKE_LIBRARY_PATH /sw/lib ${CMAKE_LIBRARY_PATH})
LINK_DIRECTORIES("/sw/lib")
SET(CMAKE_PROGRAM_PATH /sw/bin ${CMAKE_PROGRAM_PATH})
ELSE()
# no package manager found or active, do nothing
RETURN()
ENDIF()
# only ignore /usr/local if brew is installed and not in the PATH
# in other cases, it is the user's personal installations
IF(NOT MAC_HOMEBREW AND EXISTS /usr/local/bin/brew)
SET(CMAKE_IGNORE_PATH /usr/local /usr/local/bin /usr/local/include /usr/local/Library/Frameworks /usr/local/lib ${CMAKE_IGNORE_PATH})
ENDIF()

View File

@ -0,0 +1,20 @@
SET(CMAKE_SYSTEM_NAME Windows)
set(COMPILER_PREFIX "i686-w64-mingw32")
# which compilers to use for C and C++
find_program(CMAKE_RC_COMPILER NAMES ${COMPILER_PREFIX}-windres)
find_program(CMAKE_C_COMPILER NAMES ${COMPILER_PREFIX}-gcc)
find_program(CMAKE_CXX_COMPILER NAMES ${COMPILER_PREFIX}-g++)
SET(CMAKE_FIND_ROOT_PATH /usr/${COMPILER_PREFIX} /usr/${COMPILER_PREFIX}/sys-root/mingw)
# adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search
# programs in the host environment too
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
# pkg-config may be under the prefix
find_program(PKG_CONFIG_EXECUTABLE NAMES ${COMPILER_PREFIX}-pkg-config)

View File

@ -0,0 +1,20 @@
SET(CMAKE_SYSTEM_NAME Windows)
set(COMPILER_PREFIX "x86_64-w64-mingw32")
# which compilers to use for C and C++
find_program(CMAKE_RC_COMPILER NAMES ${COMPILER_PREFIX}-windres)
find_program(CMAKE_C_COMPILER NAMES ${COMPILER_PREFIX}-gcc)
find_program(CMAKE_CXX_COMPILER NAMES ${COMPILER_PREFIX}-g++)
SET(CMAKE_FIND_ROOT_PATH /usr/${COMPILER_PREFIX} /usr/${COMPILER_PREFIX}/sys-root/mingw)
# adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search
# programs in the host environment too
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
# pkg-config may be under the prefix
find_program(PKG_CONFIG_EXECUTABLE NAMES ${COMPILER_PREFIX}-pkg-config)

152
installdeps Executable file
View File

@ -0,0 +1,152 @@
#!/bin/sh
main() {
cd "$(dirname $0)"
case "$(uname -s)" in
Linux)
linux_installdeps
;;
Darwin)
osx_installdeps
;;
MINGW*|MSYS*)
msys2_installdeps
;;
*)
echo "Don't know how to install deps on your OS." >&2
exit 1
;;
esac
exit 0
}
linux_installdeps() {
if [ -f /etc/debian_version ]; then
debian_installdeps
elif [ -f /etc/arch-release ]; then
archlinux_installdeps
else
echo "Don't know how to install deps on your version of Linux." >&2
exit 1
fi
}
debian_installdeps() {
echo 'Installing deps....'
echo
sudo apt-get -y update || exit 1
sudo apt-get -y install build-essential g++ zlib1g-dev libgl1-mesa-dev cmake libavcodec-dev libavformat-dev libswscale-dev libavutil-dev libgettextpo-dev libjpeg-dev libpng16-dev libtiff5-dev libsdl2-dev libsfml-dev libopenal-dev libwxgtk3.0-dev || exit 1
generic_build_instructions
}
archlinux_installdeps() {
echo 'Installing deps....'
echo
pacman="sudo pacman"
command -v pacaur >/dev/null && pacman=pacaur
$pacman --noconfirm --needed -Syuu base-devel gcc zlib mesa cairo cmake ffmpeg gettext libpng libtiff pkg-config sdl2 sfml openal wxgtk || exit 1
generic_build_instructions
}
msys2_installdeps() {
MINGW_DEPS='SDL2 cairo ffmpeg openal sfml wxWidgets zlib binutils cmake crt-git extra-cmake-modules gcc gcc-libs gdb headers-git make pkg-config tools-git windows-default-manifest libmangle-git nasm'
MINGW64_DEPS=
MINGW32_DEPS=
for dep in $MINGW_DEPS; do
MINGW64_DEPS="$MINGW64_DEPS mingw-w64-x86_64-$dep"
MINGW32_DEPS="$MINGW32_DEPS mingw-w64-i686-$dep"
done
echo 'Installing deps....'
echo
pacman --noconfirm --needed -Syuu git make zip $MINGW64_DEPS $MINGW32_DEPS || exit 1
git submodule update --init --recursive
cmake_flags="-G 'MSYS Makefiles'"
generic_build_instructions
}
osx_installdeps() {
if ! xcode-select -p >/dev/null 2>&1 && \
! pkgutil --pkg-info=com.apple.pkg.CLTools_Executables >/dev/null 2>&1 && \
! pkgutil --pkg-info=com.apple.pkg.DeveloperToolsCLI >/dev/null 2>&1; then
echo 'Please install XCode and the XCode Command Line Tools, then run this script again.' >&2
echo 'On newer systems this can be done with: xcode-select --install' >&2
exit 1
fi
if command -v brew >/dev/null; then
brew_installdeps
elif command -v port >/dev/null; then
macports_installdeps
elif command -v fink >/dev/null; then
fink_installdeps
else
echo 'You have no package manager, please install homebrew, macports or fink.' >&2
echo 'Homebrew is recommended.' >&2
fi
}
brew_installdeps() {
echo 'Installing deps....'
echo
brew -v update || exit 1
brew -v install cairo cmake ffmpeg gettext jpeg libpng libtiff pkg-config sdl2 sfml wxmac || exit 1
brew -v cleanup
generic_build_instructions
}
macports_installdeps() {
echo 'Installing deps....'
echo
sudo port -v selfupdate || exit 1
sudo port -v install cairo cmake ffmpeg gettext jpeg libpng tiff pkgconfig libsdl2 sfml wxWidgets-3.0 libiconv || exit 1
sudo port select wxWidgets wxWidgets-3.0
generic_build_instructions
}
fink_installdeps() {
echo 'Installing deps....'
echo
sudo fink -vy selfupdate || exit 1
sudo fink -vy install cairo cmake ffmpeg libgettext8-dev libjpeg9 libpng16 libtiff5 pkgconfig sdl2 wxwidgets300-osxcocoa libiconv-dev || exit 1
echo
echo 'WARNING: SFML is required for LINK support, there is currently no SFML package for Fink, if you want LINK support you will need to install it manually.'
cmake_flags='-DENABLE_LINK=NO'
generic_build_instructions
}
generic_build_instructions() {
cat <<EOF
Done! To build do:
mkdir build
cd build
cmake .. $cmake_flags
make -j10
EOF
}
main "$@"

View File

@ -1,29 +0,0 @@
#!/bin/sh
cd "$(dirname $0)"
MINGW_DEPS='SDL2 cairo ffmpeg openal sfml wxWidgets zlib binutils cmake crt-git extra-cmake-modules gcc gcc-libs gdb headers-git make pkg-config tools-git windows-default-manifest libmangle-git nasm'
MINGW64_DEPS=
MINGW32_DEPS=
for dep in $MINGW_DEPS; do
MINGW64_DEPS="$MINGW64_DEPS mingw-w64-x86_64-$dep"
MINGW32_DEPS="$MINGW32_DEPS mingw-w64-i686-$dep"
done
echo 'Installing deps....'
echo
pacman --noconfirm --needed -Syuu git make zip $MINGW64_DEPS $MINGW32_DEPS
git submodule update --init --recursive
cat <<'EOF'
Done! To build do:
mkdir build
cd build
cmake .. -G 'MSYS Makefiles'
make -j10
EOF