From fc98095b18995c8744601c21c94e31be565d4c4b Mon Sep 17 00:00:00 2001 From: Tom Black Date: Mon, 15 Apr 2019 23:44:05 -0500 Subject: Bundle Linux dependencies Also clean up `extconf.rb` generally and add new `rake dev` task to use user-installed libraries. --- ext/ruby2d/extconf.rb | 139 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 110 insertions(+), 29 deletions(-) (limited to 'ext') diff --git a/ext/ruby2d/extconf.rb b/ext/ruby2d/extconf.rb index 46f42df..023daaf 100644 --- a/ext/ruby2d/extconf.rb +++ b/ext/ruby2d/extconf.rb @@ -2,7 +2,25 @@ require 'mkmf' require_relative '../../lib/ruby2d/colorize' S2D_VERSION = '1.1.0' # Simple 2D minimum version required -$errors = [] # Array to capture errors +$errors = [] # Holds errors + +# Set the OS platform +case RUBY_PLATFORM +when /darwin/ + $platform = :macos +when /linux/ + $platform = :linux + if `cat /etc/os-release` =~ /raspbian/ + $platform = :linux_rpi + end +when /mingw/ + $platform = :windows +else + $platform = nil +end + + +# Helper functions ############################################################# # Print installation errors def print_errors @@ -12,15 +30,15 @@ def print_errors #{"======================================================================"}" end + # Check that Simple 2D is installed and meets minimum version requirements def check_s2d # Simple 2D not installed if `which simple2d`.empty? $errors << "Ruby 2D uses a native library called Simple 2D, which was not found." << - "To install, follow the instructions at #{"ruby2d.com/learn".bold}" - print_errors - exit + "To install, follow the instructions at #{"ruby2d.com".bold}" + print_errors; exit # Simple 2D installed, checking version else @@ -28,26 +46,79 @@ def check_s2d $errors << "Simple 2D needs to be updated for this version of Ruby 2D." << "Run the following, then try reinstalling this gem:\n" << " simple2d update".bold - print_errors - exit + print_errors; exit end end end + +# Add compiler and linker flags +def add_flags(type, flags) + case type + when :c + $CFLAGS << " #{flags} " + when :ld + $LDFLAGS << " #{flags} " + end +end + + +# Check SDL libraries on Linux +def check_sdl_linux + unless have_library('SDL2') && have_library('SDL2_image') && have_library('SDL2_mixer') && have_library('SDL2_ttf') + + $errors << "Couldn't find packages needed by Ruby 2D." + + # Fedora and CentOS + if system('which yum') + $errors << "Install the following packages using `yum` (or `dnf`) and try again:\n" << + " SDL2-devel SDL2_image-devel SDL2_mixer-devel SDL2_ttf-devel".bold + + # Arch + elsif system('which pacman') + $errors << "Install the following packages using `pacman` and try again:\n" << + " sdl2 sdl2_image sdl2_mixer sdl2_ttf".bold + + # openSUSE + elsif system('which zypper') + $errors << "Install the following packages using `zypper` and try again:\n" << + " libSDL2-devel libSDL2_image-devel libSDL2_mixer-devel libSDL2_ttf-devel".bold + + # Ubuntu, Debian, and Mint + # `apt` must be last because openSUSE has it aliased to `zypper` + elsif system('which apt') + $errors << "Install the following packages using `apt` and try again:\n" << + " libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev".bold + end + + $errors << "" << "See #{"ruby2d.com".bold} for additional help." + print_errors; exit + end +end + + +# Set Raspberry Pi flags +def set_rpi_flags + if $platform == :linux_rpi + add_flags(:c, '-I/opt/vc/include') + add_flags(:ld, '-L/opt/vc/lib -lbrcmGLESv2') + end +end + + # Use the Simple 2D, SDL, and other libraries installed by the user (not those bundled with the gem) def use_usr_libs check_s2d # Add flags - $CFLAGS << ' -std=c11 -I/usr/local/include' - if `cat /etc/os-release` =~ /raspbian/ # Raspberry Pi - $CFLAGS << ' -I/opt/vc/include' - end - $LDFLAGS << ' ' << `bash simple2d --libs` - $LDFLAGS.gsub!(/\n/, ' ') # remove newlines in flags, they cause problems + set_rpi_flags + add_flags(:c, '-I/usr/local/include') + add_flags(:ld, `bash simple2d --libs`) end +# Configure native extension ################################################### + # Build Ruby 2D native extention using libraries installed by user # To use install flag: `gem install ruby2d -- libs` if ARGV.include? 'libs' @@ -55,27 +126,36 @@ if ARGV.include? 'libs' # Use libraries provided by the gem (default) else - $CFLAGS << ' -std=c11 -I../../assets/include' - case RUBY_PLATFORM + add_flags(:c, '-std=c11') - # macOS - when /darwin/ - # $LDFLAGS << " -L../../assets/macos/lib -lsimple2d -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf -ljpeg -lpng16 -ltiff -lwebp -lmpg123 -logg -lflac -lvorbis -lvorbisfile -lfreetype -Wl,-framework,Cocoa -Wl,-framework,ForceFeedback" + case $platform + when :macos + add_flags(:c, '-I../../assets/include') ldir = "#{Dir.pwd}/../../assets/macos/lib" - $LDFLAGS << " #{ldir}/libsimple2d.a #{ldir}/libSDL2.a #{ldir}/libSDL2_image.a #{ldir}/libSDL2_mixer.a #{ldir}/libSDL2_ttf.a \ - #{ldir}/libjpeg.a #{ldir}/libpng16.a #{ldir}/libtiff.a #{ldir}/libwebp.a \ - #{ldir}/libmpg123.a #{ldir}/libogg.a #{ldir}/libflac.a #{ldir}/libvorbis.a #{ldir}/libvorbisfile.a \ - #{ldir}/libfreetype.a -Wl,-framework,Cocoa -Wl,-framework,ForceFeedback" - - # Linux - when /linux/ - # TODO: Implement static compilation for Linux - use_usr_libs - # Windows / MinGW - when /mingw/ - $LDFLAGS << " -L../../assets/mingw/lib -lsimple2d -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf -lmingw32 -lopengl32 -lglew32" + add_flags(:ld, "#{ldir}/libsimple2d.a") + add_flags(:ld, "#{ldir}/libSDL2.a #{ldir}/libSDL2_image.a #{ldir}/libSDL2_mixer.a #{ldir}/libSDL2_ttf.a") + add_flags(:ld, "#{ldir}/libjpeg.a #{ldir}/libpng16.a #{ldir}/libtiff.a #{ldir}/libwebp.a") + add_flags(:ld, "#{ldir}/libmpg123.a #{ldir}/libogg.a #{ldir}/libflac.a #{ldir}/libvorbis.a #{ldir}/libvorbisfile.a") + add_flags(:ld, "#{ldir}/libfreetype.a") + add_flags(:ld, "-Wl,-framework,Cocoa -Wl,-framework,ForceFeedback") + + when :linux, :linux_rpi + check_sdl_linux + simple2d_dir = "#{Dir.pwd}/../../assets/linux/simple2d" + + `(cd #{simple2d_dir} && make)` + + set_rpi_flags + add_flags(:c, "-I#{simple2d_dir}/include") + add_flags(:ld, "#{simple2d_dir}/build/libsimple2d.a -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf -lm") + if $platform == :linux then add_flags(:ld, '-lGL') end + + when :windows + add_flags(:c, '-I../../assets/include') + add_flags(:ld, '-L../../assets/mingw/lib -lsimple2d -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf') + add_flags(:ld, '-lmingw32 -lopengl32 -lglew32') # If can't detect the platform, use libraries installed by the user else @@ -83,6 +163,7 @@ else end end +$LDFLAGS.gsub!(/\n/, ' ') # remove newlines in flags, they can cause problems # Create the Makefile create_makefile('ruby2d/ruby2d') -- cgit v1.2.3