summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/misc/build_dcss_release.rb
blob: 92e8750d215124cad6def9ccb75da6f79d9abb8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# build_dcss_release.rb
#   Builds DOS and Windows binaries for a Stone Soup release.
# Needs rubyzip to be installed.

require 'fileutils'
require 'zip/zipfilesystem'

SVN_BASE_URL    = 'https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/'
SVN_BRANCH      = 'branches/stone_soup-0.3'
SVN_URL         = SVN_BASE_URL + SVN_BRANCH + '/crawl-ref'

# If empty, nothing is done. Useful to sync svk mirrors.
SVN_PRESYNC     = ''

SVN             = 'svn'

BUILDS          = [ Proc.new { build_win32 }, 
                    Proc.new { build_dos } ].reverse

W32MAKE         = 'mingw32-make'
DOSMAKE         = 'make'

UPX             = 'upx'

SVN_CO_DIR      = 'stone_soup'

DEV_NULL        = 'nul'

CLEANPATH       = ENV['PATH']
# Leave empty if svn is already in the path
SVN_PATH_PREFIX = 'C:\etc\misc\svk\bin;C:\etc\misc\svn\bin;'

W32MAKE_PATH_PREFIX = 'C:\etc\MinGW\bin;C:\etc\misc\yacc;'
DOSMAKE_PATH_PREFIX = 'C:\etc\djgpp\bin;'

BUILD_HOME      = File.join( FileUtils.pwd(), 'release' )

# Relative to source directory
CRAWL_NDB_PATH  = 'rel/crawl.exe'
CRAWL_DBG_PATH  = 'dbg/crawl.exe'
CRAWL_DOS_PATH  = 'crawl.exe'

PACKAGE_PATH    = BUILD_HOME

def build_soup
  checkout
  make
  package
end

def setup_svn_env
  ENV['PATH'] = SVN_PATH_PREFIX + CLEANPATH
end

def setup_build_home
  FileUtils.mkdir_p(BUILD_HOME)
  Dir.chdir(BUILD_HOME)
end

def full_checkout
  puts "#{SVN} co #{SVN_URL} #{SVN_CO_DIR}"
  system "#{SVN} co #{SVN_URL} #{SVN_CO_DIR}" or 
          raise "#{SVN} co failed: #$?"
  Dir.chdir SVN_CO_DIR
end

def checkout
  setup_svn_env

  if SVN_PRESYNC and not SVN_PRESYNC.empty?
    puts "Running presync: '#{SVN_PRESYNC}'"
    system(SVN_PRESYNC) or raise "#{SVN_PRESYNC} failed: #$?!"
  end

  setup_build_home

  clean_directory SVN_CO_DIR if File.directory? SVN_CO_DIR
  full_checkout
end

def clean_directory(*dirs)
  dirs.each do |dir|
    currdir = FileUtils.pwd
    if currdir != BUILD_HOME
      raise "In #{currdir}, need to be in #{BUILD_HOME}!"
    end
    raise "Evil directory name: #{dir}" if dir =~ /\.\./

    puts "Trying to remove #{dir}"
    FileUtils.rm_r dir, :verbose => true
  end
end

def path_prefix(prefix)
  ENV['PATH'] = prefix + CLEANPATH unless ENV['PATH'].index(prefix)
end

def clean_objects
  [ '.', 'rel', 'dbg', 'util', 'contrib/lua/src', 'contrib/sqlite' ].each do |dir|
    if File.directory? dir
      FileUtils.rm( Dir[dir + '/*.o'], :force => true )
      FileUtils.rm( Dir[dir + '/*.a'], :force => true )
    end
  end
end

def clean_w32build_area
  clean_objects
  system "#{W32MAKE} -f makefile.mgw clean"
end

def clean_dosbuild_area
  clean_objects
  system "#{DOSMAKE} -f makefile.dos clean"
end

def make
  BUILDS.each do |builder|
    Dir.chdir( File.join(BUILD_HOME, SVN_CO_DIR) )
    builder.call
  end
end

def setup_w32make_env
  Dir.chdir 'source'
  path_prefix W32MAKE_PATH_PREFIX
  clean_w32build_area
end

def setup_dosmake_env
  Dir.chdir 'source'
  path_prefix DOSMAKE_PATH_PREFIX
  clean_dosbuild_area
end

$release_version = nil
def release_version
  if not $release_version
    raise "Can't find version.h" unless File.file? 'version.h'
    IO.readlines('version.h').each do |line|
      if line =~ /VER_NUM\s+"(\d\.\d(?:\.\d)?)/
        $release_version = $1
      end
      if line =~ /VER_QUAL\s+"(.*)"/
        if $1 =~ /-svn/
          raise "Version number has -svn suffix! Remove it to proceed"
        end
      end
    end
  end
  $release_version or raise "Unable to read version at #{FileUtils.pwd}"
end

def build_dos
  setup_dosmake_env

  puts "\nBuilding stone_soup (ndebug) for #{release_version} DOS"
  ENV['LIB'] = "-static -Lcontrib\\lua\\src -llua -lpcre -Lcontrib\\sqlite -lsql3"
  ENV['EXTRA_FLAGS'] = "-O2 -DCLUA_BINDINGS -DREGEX_PCRE -DDEBUG -DWIZARD"

  puts %{ #{DOSMAKE} -e -f makefile.dos DOYACC=y }
  system( %{ #{DOSMAKE} -e -f makefile.dos DOYACC=y } ) or
            raise "#{DOSMAKE} failed: #$?"

  upx CRAWL_DOS_PATH
end

def build_win32
  setup_w32make_env
  
  puts "\nBuilding stone_soup (non-debug) for #{release_version} release!"
  system( %{#{W32MAKE} -f makefile.mgw DOYACC=y "EXTRA_FLAGS=-O2 } +
         %{-DCLUA_BINDINGS -DREGEX_PCRE -DDEBUG -DWIZARD" } +
         %{"LIB=-lwinmm -static -Lcontrib/lua/src -llua -lpcre -Lcontrib/sqlite -lsqlite3"} ) or
            raise "#{W32MAKE} failed: #$?"

  clean_w32build_area

  #puts "\nBuilding stone_soup (debug) for #{release_version}!"
  #system( %{#{W32MAKE} -f makefile.mgw debug DEBUG_CRAWL=y "EXTRA_FLAGS=-O2 } +
  #       %{-DCLUA_BINDINGS -DREGEX_PCRE -DFULLDEBUG -DWIZARD" } +
  #       %{"LIB=-lwinmm -static -llua -lpcre" } +
  #       %{DOYACC=y} ) or
  #          raise "#{W32MAKE} failed: #$?"

  #upx CRAWL_NDB_PATH, CRAWL_DBG_PATH
  upx CRAWL_NDB_PATH
end

def upx(*files)
  return unless UPX and not UPX.empty?
  files.each do |file|
    system "#{UPX} #{file}" or raise "#{UPX} failed: #$?"
  end
end

def makezip(path, name, exe)
  zipname = File.join(path, name) + '.zip'

  puts "Creating zip archive at #{zipname}"
  if File.exists? zipname
    FileUtils.rm(zipname)
  end
  Zip::ZipFile.open( zipname, Zip::ZipFile::CREATE ) do |zip|
    zip.dir.mkdir(name)
    
    zip.dir.chdir name

    # The exe itself
    zip.add 'source/' + exe

    # Add base documentation
    zip.add [ 'CREDITS', 'licence.txt', 'readme.txt' ] + Dir['README*']
    
    # Add base config
    zip.add [ 'init.txt', 'macro.txt' ]

    zip.add( Dir['docs/*'].find_all { |f| not File.directory?(f) },
            :keep_paths => true )

    [ 'lua', 'dat', 'dat/clua', 'dat/descript' ].each do |dir|
      zip.add( Dir['source/' + dir + '/*'], :prefix => dir )
    end
  end
end

def package
  Dir.chdir( File.join(BUILD_HOME, SVN_CO_DIR) )

  # Wipe all existing zips!
  FileUtils.rm( Dir[ File.join(PACKAGE_PATH, '*.zip') ] )

  [ [ "stone_soup-#{release_version}-win32", CRAWL_NDB_PATH ],
    # [ "stone_soup-#{release_version}-win32-debug", CRAWL_DBG_PATH ],
    [ "ss#{release_version.tr '.', ''}dos", CRAWL_DOS_PATH ]
  ].
    each do |pkg, exe|

    makezip(PACKAGE_PATH, pkg, exe)
  end
end

##########################################################################
# Zip extensions

class Zip::ZipFile
  def mkdir_p(dirpath)
    dirpath.tr! '\\', '/'
    segments = File.split(dirpath)
    fullpath = nil
    segments.each do |dir|
      next if dir == '.'
      fullpath = fullpath ? File.join(fullpath, dir) : dir
      self.dir.mkdir fullpath unless self.file.directory? fullpath
    end
  end

  def add(files, options = {})
    files = [ files ] unless files.respond_to? :to_ary
    files.each do |f|
      entryname = options[:keep_paths]? f : File.basename(f)
      entryname = File.join(options[:prefix], entryname) if options[:prefix]

      if File.directory? f
        # We DON'T add the contents of the directory automagically
        self.mkdir_p entryname
      else
        dirname = File.dirname(entryname)
        self.mkdir_p(dirname) unless dirname == '.'

        self.file.open(entryname, 'w') do |outf|
          File.open(f, 'rb') do |inf|
            outf.write( inf.read )
          end
        end
      end
    end
  end
end

build_soup