-- © 2007 David Given.
-- WordGrinder is licensed under the MIT open source license. See the COPYING
-- file in this distribution for the full text.
--
-- $Id: pmfile 160 2009-12-13 13:44:52Z dtrg $
-- $URL: https://wordgrinder.svn.sf.net/svnroot/wordgrinder/wordgrinder/pmfile $

include "tools/c.pm"
include "tools/lua.pm"

HOME = os.getenv("HOME")
DATE = "13 December 2009"
VERSION = "0.3.3"
FILEFORMAT = 3

-----------------------------------------------------------------------------
-- User configurable settings start here!

-- Where do you want WordGrinder installed? By default, it goes into your
-- home directory.

PREFIX = HOME
-- PREFIX = "/usr/local"

-- What build flags do you want to use? (Not including -g or -Os, which are
-- added later automatically.)

CBUILDFLAGS = {
	'-Wall',
	'--std=c99'
}

-- Any other build options go in these three sections. Note the lack of -l, -D
-- or -I. We need the XOPEN stuff to make the wide-character curses library
-- work. 

CLIBRARIES = {
	'm',
}

CDEFINES = {
	'VERSION="%VERSION%"',
	'FILEFORMAT=%FILEFORMAT%',
	'PREFIX="%PREFIX%"'
}
	
CINCLUDES = {
	"/usr/include/ncursesw",
	"/usr/include/lua5.1",
	"src/c"
}

-- When doing a Windows build, what compiler should we use?

if posix.stat("c:/") then
	-- Attempt to autodetect cygwin, which is rather strange.
	WINCCOMPILER = "gcc"
	WINCBUILDFLAGS = {"-mno-cygwin"}
	WINRESCOMPILER = "windres"
else
	-- Otherwise, change this to something appropriate.
	WINCCOMPILER = "i586-mingw32msvc-gcc"
	WINCBUILDFLAGS = EMPTY
	WINRESCOMPILER = "i586-mingw32msvc-windres"
end

-----------------------------------------------------------------------------
-- Build rules
--
-- This pmfile will build a large number of different things. If you don't
-- specify a build rule you'll get the standard Unix release.
--
-- install
--   Compiles the release version of the standard Unix executable and
--   installs it into %PREFIX%.
-- 
-- wordgrinder_release_exe
-- wordgrinder_debug_exe
--   Builds the debugging and release versions of the standard Unix
--   executable.
--
-- wordgrinder_static_exe
--   Builds the static version of the release executable.
-- 
-- wordgrinder_w32_release_exe
-- wordgrinder_w32_debug_exe
--   Builds the debugging and release versions of the Windows
--   executable.
--
-- windows_release_zip 
-- windows_debug_zip
--   Builds the debugging and release versions of the Windows binary
--   package (containing the executable, the font, the READMEs, etc.)
--
-- all
--   Builds everything. 
  
-- There is nothing editable beyond this point.
-----------------------------------------------------------------------------

-- Utility.

rename = simple {
	class = "rename",
	command = {
		"cp %in% %out%"
	},
	outputs = {"%U%-%I%/%name%"}
}

-- Build the man page.

wordgrinder_man = simple {
	command = {
		"sed -e 's/@@@DATE@@@/%DATE%/g; s/@@@VERSION@@@/%VERSION%/g' %in% > %out%"
	},
	outputs = {"%U%-%I%.man"},
	file "wordgrinder.man"
}

-- The Lua runtime (only used on some platforms).

luacore = group {
	cfile "src/c/luacore/lapi.c",
	cfile "src/c/luacore/lauxlib.c",
	cfile "src/c/luacore/lbaselib.c",
	cfile "src/c/luacore/lcode.c",
	cfile "src/c/luacore/ldblib.c",
	cfile "src/c/luacore/ldebug.c",
	cfile "src/c/luacore/ldo.c",
	cfile "src/c/luacore/ldump.c",
	cfile "src/c/luacore/lfunc.c",
	cfile "src/c/luacore/lgc.c",
	cfile "src/c/luacore/linit.c",
	cfile "src/c/luacore/liolib.c",
	cfile "src/c/luacore/llex.c",
	cfile "src/c/luacore/lmathlib.c",
	cfile "src/c/luacore/lmem.c",
	cfile "src/c/luacore/loadlib.c",
	cfile "src/c/luacore/lobject.c",
	cfile "src/c/luacore/lopcodes.c",
	cfile "src/c/luacore/loslib.c",
	cfile "src/c/luacore/lparser.c",
	cfile "src/c/luacore/lstate.c",
	cfile "src/c/luacore/lstring.c",
	cfile "src/c/luacore/lstrlib.c",
	cfile "src/c/luacore/ltable.c",
	cfile "src/c/luacore/ltablib.c",
	cfile "src/c/luacore/ltm.c",
	cfile "src/c/luacore/lundump.c",
	cfile "src/c/luacore/lvm.c",
	cfile "src/c/luacore/lzio.c",
}

-- LuaFileSystem (only used on some platforms)

lualfs = group {
	cfile "src/c/lfs/lfs.c"
}

-- Build the Lua part of the program into a C file.

wordgrinder_lua = simple {
	outputs = {"%U%-%I%.c"},
	command = "lua tools/multibin2c.lua script_table %in% > %out%",
	
	luafile "src/lua/_prologue.lua",
	luafile "src/lua/utils.lua",
	luafile "src/lua/events.lua",
	luafile "src/lua/redraw.lua",
	luafile "src/lua/document.lua",
	luafile "src/lua/forms.lua",
	luafile "src/lua/ui.lua",
	luafile "src/lua/browser.lua",
	luafile "src/lua/html.lua",
	luafile "src/lua/margin.lua",
	luafile "src/lua/fileio.lua",
	luafile "src/lua/export.lua",
	luafile "src/lua/export/text.lua",
	luafile "src/lua/export/html.lua",
	luafile "src/lua/export/latex.lua",
	luafile "src/lua/export/troff.lua",
	luafile "src/lua/import.lua",
	luafile "src/lua/navigate.lua",
	luafile "src/lua/addons/goto.lua",
	luafile "src/lua/addons/autosave.lua",
	luafile "src/lua/addons/docsetman.lua",
	luafile "src/lua/addons/scrapbook.lua",
	luafile "src/lua/menu.lua",
	luafile "src/lua/main.lua",
}

-- Build the main program.

wordgrinder_core = group {
	cfile "src/c/utils.c",
	cfile "src/c/main.c",
	cfile "src/c/lua.c",
	cfile "src/c/word.c",
	cfile "src/c/bit.c",
	cfile "src/c/screen.c",
	cfile { wordgrinder_lua }
}

wordgrinder_curses_exe = cprogram {
	wordgrinder_core,
	cfile "src/c/arch/unix/cursesw/dpy.c",

	CDEFINES = {
		PARENT,
		'_XOPEN_SOURCE_EXTENDED',
		'_XOPEN_SOURCE',
	},
	
	CLIBRARIES = {
		PARENT,
		'ncursesw',
		'lua5.1'
	}
}

wordgrinder_static_exe = cprogram {
	luacore,
	lualfs,
	wordgrinder_core,
	cfile "src/c/emu/wcwidth.c",
	cfile "src/c/arch/unix/cursesw/dpy.c",

	CBUILDFLAGS = {PARENT, '-Os'},
	LUABUILDFLAGS = {PARENT, '-s'},
	CDEFINES = {PARENT, 'NDEBUG'},
	CLINKFLAGS = {PARENT, '-s', '-static'},
	
	CDEFINES = {
		PARENT,
		'_XOPEN_SOURCE_EXTENDED',
		'_XOPEN_SOURCE',
		"EMULATED_WCWIDTH",
		"BUILTIN_LFS",
	},
	
	CINCLUDES = {
		PARENT,
		"src/c/luacore"
	},
	
	CLIBRARIES = {
		PARENT,
		'ncursesw',
	},

	install = pm.install("bin/wordgrinder-static")
}

rcfile = simple_with_clike_dependencies {
	class = "rcfile",
	command = "%WINRESCOMPILER% %in% %out%",
	outputs = {"%U%-%I%.o"}
}

wordgrinder_w32_exe = cprogram {
	luacore,
	lualfs,
	wordgrinder_core,
	cfile "src/c/emu/wcwidth.c",
	cfile "src/c/arch/win32/console/dpy.c",
	rcfile "src/c/arch/win32/wordgrinder.rc",

	CCOMPILER = "%WINCCOMPILER% %WINCBUILDFLAGS%",
	
	CDEFINES = {
		PARENT,
		"EMULATED_WCWIDTH",
		"BUILTIN_LFS",
		"WIN32"
	},

	CINCLUDES = {
		PARENT,
		"src/c/luacore"
	},
}

wordgrinder_w32_release_exe = group {
	wordgrinder_w32_exe,
	CBUILDFLAGS = {PARENT, '-Os'},
	LUABUILDFLAGS = {PARENT, '-s'},
	CDEFINES = {PARENT, 'NDEBUG'},
	CLINKFLAGS = {PARENT, '-s'},
	
	install = pm.install("bin/wordgrinder.exe")
}

wordgrinder_w32_debug_exe = group {
	wordgrinder_w32_exe,
	CBUILDFLAGS = {PARENT, '-g'},
	
	install = pm.install("bin/wordgrinder-debug.exe")
}

wordgrinder_release_exe = group {
	wordgrinder_curses_exe,
	CBUILDFLAGS = {PARENT, '-Os'},
	LUABUILDFLAGS = {PARENT, '-s'},
	CDEFINES = {PARENT, 'NDEBUG'},
	CLINKFLAGS = {PARENT, '-s'},
	
	install = pm.install("bin/wordgrinder")
}

wordgrinder_debug_exe = group {
	wordgrinder_curses_exe,
	CBUILDFLAGS = {PARENT, '-g'},
	
	install = pm.install("bin/wordgrinder-debug")
}

default = group {
	wordgrinder_debug_exe,
	wordgrinder_release_exe,
}

windows = group {
	wordgrinder_w32_debug_exe,
	wordgrinder_w32_release_exe,
}	

windows_package = simple {
	class = "windows_package",
	command = {
		"rm -f %out%",
		"zip -9j %out% %in%"
	},
	outputs = {"%U%-%I%.zip"},

	file "COPYING",	
	file "README.wg",
	file "README.Windows.txt",
	file "extras/DejaVuSansMono-license.txt",
	file "extras/DejaVuSansMono.reg",
	file "extras/DejaVuSansMono.ttf",
}
	
windows_debug_zip = windows_package {
	rename {
		name = "wordgrinder_debug.exe",
		wordgrinder_w32_debug_exe,
	},
	install = pm.install("bin/wordgrinder-for-windows-%VERSION%-debug.zip")	
}
	
windows_release_zip = windows_package {
	rename {
		name = "wordgrinder.exe",
		wordgrinder_w32_release_exe,
	},
	install = pm.install("bin/wordgrinder-for-windows-%VERSION%.zip")	
}
 
all = group {
	wordgrinder_debug_exe,
	wordgrinder_release_exe,
	windows_debug_zip,
	windows_release_zip
}

-- Once built, do the installation, rather crudely. FIXME.

install = simple {
	outputs = {"dummy"},
	command = {
		"install -D -m 755 %in[1]% %PREFIX%/bin/wordgrinder",
		"install -D -m 644 %in[2]% %PREFIX%/man/man1/wordgrinder.1",
		"install -D -m 644 %in[3]% %PREFIX%/share/doc/wordgrinder/README.wg"
	},
	
	wordgrinder_release_exe,
	wordgrinder_man,
	file "README.wg"
}
