Skip to content

Commit

Permalink
Move per-platform compiling functions out of builtin.run
Browse files Browse the repository at this point in the history
  • Loading branch information
mpeterv committed Dec 19, 2015
1 parent fe8d86f commit 8092d07
Showing 1 changed file with 128 additions and 118 deletions.
246 changes: 128 additions & 118 deletions src/luarocks/build/builtin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,129 +56,139 @@ local function normalize_info_table(info, variables)
end
end

local function add_flags(extras, flag, flags)
for _, v in ipairs(flags) do
table.insert(extras, flag:format(v))
end
end

local mingw32 = {}

function mingw32.compile_object(variables, opts, object, source)
local extras = {}
add_flags(extras, "-D%s", opts.defines)
add_flags(extras, "-I%s", opts.incdirs)
return execute(variables.CC.." "..variables.CFLAGS, "-c", "-o", object, "-I"..variables.LUA_INCDIR, source, unpack(extras))
end

function mingw32.compile_library(variables, opts, library, objects)
local extras = { unpack(objects) }
add_flags(extras, "-L%s", opts.libdirs)
add_flags(extras, "-l%s", opts.libraries)
extras[#extras+1] = dir.path(variables.LUA_LIBDIR, variables.LUALIB)
extras[#extras+1] = "-l" .. (variables.MSVCRT or "m")
local ok = execute(variables.LD.." "..variables.LIBFLAG, "-o", library, unpack(extras))
return ok
end

function mingw32.compile_wrapper_binary(variables, fullname, name)
--TODO EXEWRAPPER
local fullbasename = fullname:gsub("%.lua$", ""):gsub("/", "\\")
local basename = name:gsub("%.lua$", ""):gsub("/", "\\")
local rcname = basename..".rc"
local resname = basename..".o"
local wrapname = basename..".exe"
make_rc(fullname, fullbasename..".rc")
local ok = execute(variables.RC, "-o", resname, rcname)
if not ok then return ok end
ok = execute(variables.CC.." "..variables.CFLAGS, "-I"..variables.LUA_INCDIR,
"-o", wrapname, resname, variables.WRAPPER,
dir.path(variables.LUA_LIBDIR, variables.LUALIB),
"-l" .. (variables.MSVCRT or "m"), "-luser32")
return ok, wrapname
end

local win32 = {}

function win32.compile_object(variables, opts, object, source)
local extras = {}
add_flags(extras, "-D%s", opts.defines)
add_flags(extras, "-I%s", opts.incdirs)
return execute(variables.CC.." "..variables.CFLAGS, "-c", "-Fo"..object, "-I"..variables.LUA_INCDIR, source, unpack(extras))
end

function win32.compile_library(variables, opts, library, objects, name)
local extras = { unpack(objects) }
add_flags(extras, "-libpath:%s", opts.libdirs)
add_flags(extras, "%s.lib", opts.libraries)
local basename = dir.base_name(library):gsub(".[^.]*$", "")
local deffile = basename .. ".def"
local def = io.open(dir.path(fs.current_dir(), deffile), "w+")
def:write("EXPORTS\n")
def:write("luaopen_"..name:gsub("%.", "_").."\n")
def:close()
local ok = execute(variables.LD, "-dll", "-def:"..deffile, "-out:"..library, dir.path(variables.LUA_LIBDIR, variables.LUALIB), unpack(extras))
local basedir = ""
if name:find("%.") ~= nil then
basedir = name:gsub("%.%w+$", "\\")
basedir = basedir:gsub("%.", "\\")
end
local manifestfile = basedir .. basename..".dll.manifest"

if ok and fs.exists(manifestfile) then
ok = execute(variables.MT, "-manifest", manifestfile, "-outputresource:"..basedir..basename..".dll;2")
end
return ok
end

function win32.compile_wrapper_binary(variables, fullname, name)
--TODO EXEWRAPPER
local fullbasename = fullname:gsub("%.lua$", ""):gsub("/", "\\")
local basename = name:gsub("%.lua$", ""):gsub("/", "\\")
local object = basename..".obj"
local rcname = basename..".rc"
local resname = basename..".res"
local wrapname = basename..".exe"
make_rc(fullname, fullbasename..".rc")
local ok = execute(variables.RC, "-nologo", "-r", "-fo"..resname, rcname)
if not ok then return ok end
ok = execute(variables.CC.." "..variables.CFLAGS, "-c", "-Fo"..object,
"-I"..variables.LUA_INCDIR, variables.WRAPPER)
if not ok then return ok end
ok = execute(variables.LD, "-out:"..wrapname, resname, object,
dir.path(variables.LUA_LIBDIR, variables.LUALIB), "user32.lib")
local manifestfile = wrapname..".manifest"
if ok and fs.exists(manifestfile) then
ok = execute(variables.MT, "-manifest", manifestfile, "-outputresource:"..wrapname..";1")
end
return ok, wrapname
end

local default = {}

function default.compile_object(variables, opts, object, source)
local extras = {}
add_flags(extras, "-D%s", opts.defines)
add_flags(extras, "-I%s", opts.incdirs)
return execute(variables.CC.." "..variables.CFLAGS, "-I"..variables.LUA_INCDIR, "-c", source, "-o", object, unpack(extras))
end

function default.compile_library(variables, opts, library, objects)
local extras = { unpack(objects) }
add_flags(extras, "-L%s", opts.libdirs)
if cfg.gcc_rpath then
add_flags(extras, "-Wl,-rpath,%s:", opts.libdirs)
end
add_flags(extras, "-l%s", opts.libraries)
if cfg.is_platform("cygwin") then
add_flags(extras, "-l%s", {"lua"})
end
return execute(variables.LD.." "..variables.LIBFLAG, "-o", library, "-L"..variables.LUA_LIBDIR, unpack(extras))
end

function default.compile_wrapper_binary(_, _, name)
return true, name
end

--- Driver function for the builtin build back-end.
-- @param rockspec table: the loaded rockspec.
-- @return boolean or (nil, string): true if no errors ocurred,
-- nil and an error message otherwise.
function builtin.run(rockspec)
assert(type(rockspec) == "table")
local compile_object, compile_library, compile_wrapper_binary --TODO EXEWRAPPER

local build = rockspec.build
local variables = rockspec.variables

local function add_flags(extras, flag, flags)
for _, v in ipairs(flags) do
table.insert(extras, flag:format(v))
end
end

if cfg.is_platform("mingw32") then
compile_object = function(object, source, defines, incdirs)
local extras = {}
add_flags(extras, "-D%s", defines)
add_flags(extras, "-I%s", incdirs)
return execute(variables.CC.." "..variables.CFLAGS, "-c", "-o", object, "-I"..variables.LUA_INCDIR, source, unpack(extras))
end
compile_library = function(library, objects, libraries, libdirs)
local extras = { unpack(objects) }
add_flags(extras, "-L%s", libdirs)
add_flags(extras, "-l%s", libraries)
extras[#extras+1] = dir.path(variables.LUA_LIBDIR, variables.LUALIB)
extras[#extras+1] = "-l" .. (variables.MSVCRT or "m")
local ok = execute(variables.LD.." "..variables.LIBFLAG, "-o", library, unpack(extras))
return ok
end
compile_wrapper_binary = function(fullname, name)
--TODO EXEWRAPPER
local fullbasename = fullname:gsub("%.lua$", ""):gsub("/", "\\")
local basename = name:gsub("%.lua$", ""):gsub("/", "\\")
local rcname = basename..".rc"
local resname = basename..".o"
local wrapname = basename..".exe"
make_rc(fullname, fullbasename..".rc")
local ok = execute(variables.RC, "-o", resname, rcname)
if not ok then return ok end
ok = execute(variables.CC.." "..variables.CFLAGS, "-I"..variables.LUA_INCDIR,
"-o", wrapname, resname, variables.WRAPPER,
dir.path(variables.LUA_LIBDIR, variables.LUALIB),
"-l" .. (variables.MSVCRT or "m"), "-luser32")
return ok, wrapname
end
elseif cfg.is_platform("win32") then
compile_object = function(object, source, defines, incdirs)
local extras = {}
add_flags(extras, "-D%s", defines)
add_flags(extras, "-I%s", incdirs)
return execute(variables.CC.." "..variables.CFLAGS, "-c", "-Fo"..object, "-I"..variables.LUA_INCDIR, source, unpack(extras))
end
compile_library = function(library, objects, libraries, libdirs, name)
local extras = { unpack(objects) }
add_flags(extras, "-libpath:%s", libdirs)
add_flags(extras, "%s.lib", libraries)
local basename = dir.base_name(library):gsub(".[^.]*$", "")
local deffile = basename .. ".def"
local def = io.open(dir.path(fs.current_dir(), deffile), "w+")
def:write("EXPORTS\n")
def:write("luaopen_"..name:gsub("%.", "_").."\n")
def:close()
local ok = execute(variables.LD, "-dll", "-def:"..deffile, "-out:"..library, dir.path(variables.LUA_LIBDIR, variables.LUALIB), unpack(extras))
local basedir = ""
if name:find("%.") ~= nil then
basedir = name:gsub("%.%w+$", "\\")
basedir = basedir:gsub("%.", "\\")
end
local manifestfile = basedir .. basename..".dll.manifest"

if ok and fs.exists(manifestfile) then
ok = execute(variables.MT, "-manifest", manifestfile, "-outputresource:"..basedir..basename..".dll;2")
end
return ok
end
compile_wrapper_binary = function(fullname, name)
--TODO EXEWRAPPER
local fullbasename = fullname:gsub("%.lua$", ""):gsub("/", "\\")
local basename = name:gsub("%.lua$", ""):gsub("/", "\\")
local object = basename..".obj"
local rcname = basename..".rc"
local resname = basename..".res"
local wrapname = basename..".exe"
make_rc(fullname, fullbasename..".rc")
local ok = execute(variables.RC, "-nologo", "-r", "-fo"..resname, rcname)
if not ok then return ok end
ok = execute(variables.CC.." "..variables.CFLAGS, "-c", "-Fo"..object,
"-I"..variables.LUA_INCDIR, variables.WRAPPER)
if not ok then return ok end
ok = execute(variables.LD, "-out:"..wrapname, resname, object,
dir.path(variables.LUA_LIBDIR, variables.LUALIB), "user32.lib")
local manifestfile = wrapname..".manifest"
if ok and fs.exists(manifestfile) then
ok = execute(variables.MT, "-manifest", manifestfile, "-outputresource:"..wrapname..";1")
end
return ok, wrapname
end
else
compile_object = function(object, source, defines, incdirs)
local extras = {}
add_flags(extras, "-D%s", defines)
add_flags(extras, "-I%s", incdirs)
return execute(variables.CC.." "..variables.CFLAGS, "-I"..variables.LUA_INCDIR, "-c", source, "-o", object, unpack(extras))
end
compile_library = function (library, objects, libraries, libdirs)
local extras = { unpack(objects) }
add_flags(extras, "-L%s", libdirs)
if cfg.gcc_rpath then
add_flags(extras, "-Wl,-rpath,%s:", libdirs)
end
add_flags(extras, "-l%s", libraries)
if cfg.is_platform("cygwin") then
add_flags(extras, "-l%s", {"lua"})
end
return execute(variables.LD.." "..variables.LIBFLAG, "-o", library, "-L"..variables.LUA_LIBDIR, unpack(extras))
end
compile_wrapper_binary = function(_, name) return true, name end
--TODO EXEWRAPPER
end
local toolchain = cfg.is_platform("mingw32") and mingw32 or (cfg.is_platform("win32") and win32 or default)

local ok = true
local built_modules = {}
Expand All @@ -192,7 +202,7 @@ function builtin.run(rockspec)
for key, name in pairs(build.install.bin) do
local fullname = dir.path(fs.current_dir(), name)
if cfg.exewrapper and fs.is_lua(fullname) then
ok, name = compile_wrapper_binary(fullname, name)
ok, name = toolchain.compile_wrapper_binary(variables, fullname, name)
if ok then
build.install.bin[key] = name
else
Expand Down Expand Up @@ -237,7 +247,7 @@ function builtin.run(rockspec)
if not object then
object = source.."."..cfg.obj_extension
end
ok = compile_object(object, source, info.defines, info.incdirs)
ok = toolchain.compile_object(variables, info, object, source)
if not ok then
return nil, "Failed compiling object "..object
end
Expand All @@ -251,7 +261,7 @@ function builtin.run(rockspec)
if not ok then return nil, err end
end
built_modules[module_name] = dir.path(libdir, module_name)
ok = compile_library(module_name, objects, info.libraries, info.libdirs, name)
ok = toolchain.compile_library(variables, info, module_name, objects, name)
if not ok then
return nil, "Failed compiling module "..module_name
end
Expand Down

0 comments on commit 8092d07

Please sign in to comment.