bin/stmkmf.rb
author Jan Vrany <jan.vrany@fit.cvut.cz>
Thu, 21 Feb 2019 20:42:12 +0000
changeset 261 9312db25c9f8
parent 257 c6a3ceed613c
child 334 eb15c224410b
permissions -rwxr-xr-x
Add `libffi` to a set of binary files for librun

#!/usr/bin/env ruby
DOCUMENTATION = <<DOCEND
Generates a `Makefile` for compiling a Smalltalk/X class library. 

DOCEND

#
# Generates a makefile for a Smalltalk/X package. Return status code
# (0 means success)
def stmkmf(cwd: '.', out: 'makefile', top: nil)   
  if not File.exist? cwd then
    STDERR.puts "error: no such directory: #{cwd}"
    return 1
  end
  if not File.directory? cwd then
    STDERR.puts "error: not a directory: #{cwd}"
    return 2
  end
  if not File.exist? File.join(cwd, 'Make.proto') then
    STDERR.puts "error: could not find Make.proto: #{File.join(cwd, 'Make.proto')}"    
    return 3
  end
  if top.nil? then 
    # Spuriously it happens the `Make.proto` is somehow corrupted and not properly
    # UTF8 encoded. This would result in error like
    #
    #    `match': invalid byte sequence in UTF-8 (ArgumentError)
    #
    # To prevent, replace all invalid character by $?. Not a safest
    # approach, though.
    make_proto_contents = File.read(File.join(cwd, 'Make.proto')).encode("UTF-8", :invalid=>:replace, :replace=>"?").encode('UTF-8')
    match = /^TOP=(.*)$/.match(make_proto_contents)
    if match.nil? then
      STDERR.puts "error: could not extract TOP from Make.proto (missing TOP=.. definition?)"    
      return 4
    end
    top = match[1].rstrip()
  end
  if not File.directory? File.join(cwd, top) then
    require 'pry'
    require 'pry-byebug'
    binding.pry
    STDERR.puts "error: TOP does not exist or not a directory: #{top}"    
    return 5
  end
  makelib = File.join(cwd, top, '..', '..' , 'makelib')
  if not File.exist? makelib then
    STDERR.puts "error: makefile include library does not exist or not a directory: #{makelib}"
    return 6
  end
  File.open(File.join(cwd, out), "w") do | f |
    f.puts <<-CONTENTS
#  
# Do not edit! Automatically generated by stmkmf.
#

MAKELIB ?= #{top}/../../makelib

include $(MAKELIB)/definitions.make

#{File.exists?(File.join(cwd, 'Make.spec')) ? 'include Make.spec' : ''}
include Make.proto

include $(MAKELIB)/rules.make
CONTENTS
  end
  return 0
end

if __FILE__ == $0
  require 'optparse'

  cwd = '.'
  out = 'makefile'


  optparser = OptionParser.new do | optparser |
    optparser.banner = "Usage: stmkmf.rb [options] [stx-top-directory]"
    optparser.on('-C', '--cd DIRECTORY', "Generates makefile in DIRECTORY. Optional, default is current working cwd.") do | value |
      cwd = value
    end
    optparser.on('-o', '--out FILE', "Write result to FILE. Optional, default is 'makefile'") do | value |
      out = value
    end    
    optparser.on(nil, '--help', "Prints this message") do
      puts DOCUMENTATION
      puts optparser.help()
      exit 0
    end
  end
  optparser.parse!
  top = ARGV[0] || nil


  exit stmkmf(cwd: cwd, out: out, top: top)
end