diff mbox series

[1/5] luarocks: add buildroot addon

Message ID 20181110140832.9700-2-francois.perrad@gadz.org
State Changes Requested
Headers show
Series luarocks addon | expand

Commit Message

Francois Perrad Nov. 10, 2018, 2:08 p.m. UTC
Signed-off-by: Francois Perrad <francois.perrad@gadz.org>
---
 docs/manual/adding-packages-luarocks.txt    |   9 +
 package/luarocks/0002-Buildroot-addon.patch | 348 ++++++++++++++++++++
 2 files changed, 357 insertions(+)
 create mode 100644 package/luarocks/0002-Buildroot-addon.patch

Comments

Arnout Vandecappelle Nov. 11, 2018, 11:17 p.m. UTC | #1
Hi Francois,

On 10/11/2018 15:08, Francois Perrad wrote:
> Signed-off-by: Francois Perrad <francois.perrad@gadz.org>
> ---
>  docs/manual/adding-packages-luarocks.txt    |   9 +
>  package/luarocks/0002-Buildroot-addon.patch | 348 ++++++++++++++++++++
>  2 files changed, 357 insertions(+)
>  create mode 100644 package/luarocks/0002-Buildroot-addon.patch
> 
> diff --git a/docs/manual/adding-packages-luarocks.txt b/docs/manual/adding-packages-luarocks.txt
> index ec3d4e7cc..b35a33206 100644
> --- a/docs/manual/adding-packages-luarocks.txt
> +++ b/docs/manual/adding-packages-luarocks.txt
> @@ -50,6 +50,15 @@ Finally, on line 16, we invoke the +luarocks-package+
>  macro that generates all the Makefile rules that actually allows the
>  package to be built.
>  
> +Most of these data can be retrieved in the +rock+ and +rockspec+.
> +So, this file and the Config.in can be generated by running
> +the command +luarocks buildroot foo lua-foo+ in the Buildroot directory

 It's not that simple: you first have to build Buildroot's host-luarocks, and
then you have to make sure that it can be found in your path!

> +which runs a specific Buildroot addon of +luarocks+.
> +You should still manually edit the result.
> +
> +* The +package/Config.in+ file has to be updated manually to include the
> +  generated Config.in files.
> +
>  [[luarocks-package-reference]]
>  
>  ==== +luarocks-package+ reference
> diff --git a/package/luarocks/0002-Buildroot-addon.patch b/package/luarocks/0002-Buildroot-addon.patch
> new file mode 100644
> index 000000000..24e22b3d7
> --- /dev/null
> +++ b/package/luarocks/0002-Buildroot-addon.patch
> @@ -0,0 +1,348 @@
> +From 76b7ec7af0e3b46178fa6d3cfac3810b60e88e16 Mon Sep 17 00:00:00 2001
> +From: Francois Perrad <francois.perrad@gadz.org>
> +Date: Sat, 27 Oct 2018 17:15:50 +0200
> +Subject: [PATCH 2/2] Buildroot addon
> +
> +Signed-off-by: Francois Perrad <francois.perrad@gadz.org>
> +---
> + src/bin/luarocks                 |   1 +
> + src/luarocks/addon/buildroot.lua | 315 +++++++++++++++++++++++++++++++

 Why do you do this by patching luarocks? Can't you just add a lua script like
we have with scancpan and scanpypi? That way, you could run the script directly
from util/ without building host-luarocks first (assuming luarocks is installed
in your system).

 Hang on, that's actually what you sent originally. So why did you change it?
You don't mention that v1 at all, and it's not clear if you addressed my
comments on v1 or not.

[snip]

> ++local function get_native_dependencies (rockspec)

 Clearly, you either didn't see my review, or you chose to ignore my suggestion
to change this name into get_external_dependencies. In the latter case, please
mention explicitly that you did that (and preferably also say why).

 But you did take my comment to heart that the load(rockspec) wasn't very clear.
Much better now, with this argument!


> ++   local t = {}
> ++   local d = rockspec.external_dependencies
> ++   if d then
> ++      if d.platforms and d.platforms.unix then

 I wrote:

 I don't think this is correct. I think the platform overrides don't replace the
generic settings but they add to them. Only if a key exists both in d and in
d.platforms.unix, it will be the unix one that is used.

 Also, shouldn't it be linux instead of unix? Or both?


> ++         d = d.platforms.unix
> ++      end
> ++      for k in pairs(d) do
> ++         if k ~= 'platforms' then
> ++            k = k:lower()
> ++            if fs.is_dir('package/' .. k) then
> ++               t[#t+1] = k
> ++            else
> ++               t[#t+1] = 'lib' .. k

 You should still check that this package exists. Except that this would create
a problem with BR2_EXTERNAL, as I mentioned.

> ++            end
> ++         end
> ++      end
> ++      table.sort(t)
> ++   end
> ++   return t
> ++end
> ++
> ++local function get_dependencies (rockspec)
> ++   local t = {}
> ++   for i = 1, #rockspec.dependencies do
> ++      local dep = tostring(rockspec.dependencies[i]):match('^(%S+)')
> ++      if dep ~= 'lua' then
> ++         dep = dep:gsub('_', '-')
> ++         if fs.is_dir('package/lua-' .. dep) then
> ++            t[#t+1] = 'lua-' .. dep
> ++         else
> ++            t[#t+1] = dep

 Again, you should check that it exists.

> ++         end
> ++      end
> ++   end
> ++   table.sort(t)
> ++   return t
> ++end
> ++
[snip]
> ++local function generate_mk (rockspec, lcname, licenses)
> ++   local function escape (s)
> ++      return s:gsub('-', '%%-'):gsub('%.', '%%.')
> ++   end
> ++
> ++   local ucname = brname(lcname)
> ++   local need_name_upstream = false
> ++   local need_version_upstream = false
> ++   local name_upstream = rockspec.package
> ++   local version = rockspec.version
> ++   local version_upstream = version:match('^([^-]+)-')
> ++   local revision = version:match('-(%d+)$')
> ++   local license = rockspec.description.license
> ++   local subdir = rockspec.source.dir
> ++   if subdir then
> ++      local root = subdir:match('^(.-)-' .. escape(version) .. '$')
> ++      if root then
> ++         subdir = root .. '-$(' .. ucname .. '_VERSION)'
> ++      end
> ++      root = subdir:match('^(.--[Vv])' .. escape(version_upstream) .. '$')
> ++      if root then
> ++         need_version_upstream = true

 I hadn't noticed in my review of v1, but:

 I actually like how we currently always have a _VERSION_UPSTREAM even though it
is not strictly needed. It makes the .mk files more unified. And it would also
(slightly) simplify this script.

> ++         subdir = root .. '$(' .. ucname .. '_VERSION_UPSTREAM)'
> ++      end
[snip]
> ++local function generate_hash (rockspec, lcname, rock_file, licenses, md5)
> ++   local subdir = rockspec.source.dir
> ++   local fname = 'package/' .. lcname .. '/' .. lcname .. '.hash'
> ++   local f = assert(io.open(fname, 'w'))
> ++   util.printout('write ' .. fname)
> ++   f:write('# computed by luarocks\n')
> ++   f:write('md5 ' .. md5[rock_file] .. '  ' .. rock_file .. '\n')
> ++   for i = 1, #licenses do
> ++      local file = licenses[i]
> ++      f:write('md5 ' .. md5[file] .. '  ' .. subdir .. '/' .. file .. '\n')

 md5? Since when do we use md5? This should be sha256! In fact, I already acked
the patch that did the correct thing...

> ++   end
> ++   f:close()
> ++end
> ++
> ++--- Driver function for the "buildroot" command.
> ++-- @param rockname string: the name of a rock to be fetched and unpacked.
> ++-- @param brname string: the name used by Buildroot (optional)
> ++-- @return boolean: true if successful
> ++function buildroot.command(flags, rockname, fsname)

 Good idea to pass the fsname explicitly instead of adding lua- automatically.

[snip]
> ++   local build_type = rockspec.build.type
> ++   if build_type ~= 'none' and build_type ~= 'builtin' and build_type ~= 'module' then

 Doesn't sound logical to me that "builtin" would be supported.

 Regards,
 Arnout

> ++      util.printout('[' .. rockspec.package .. '] ' .. build_type .. " not supported")
> ++   end

[snip]
diff mbox series

Patch

diff --git a/docs/manual/adding-packages-luarocks.txt b/docs/manual/adding-packages-luarocks.txt
index ec3d4e7cc..b35a33206 100644
--- a/docs/manual/adding-packages-luarocks.txt
+++ b/docs/manual/adding-packages-luarocks.txt
@@ -50,6 +50,15 @@  Finally, on line 16, we invoke the +luarocks-package+
 macro that generates all the Makefile rules that actually allows the
 package to be built.
 
+Most of these data can be retrieved in the +rock+ and +rockspec+.
+So, this file and the Config.in can be generated by running
+the command +luarocks buildroot foo lua-foo+ in the Buildroot directory
+which runs a specific Buildroot addon of +luarocks+.
+You should still manually edit the result.
+
+* The +package/Config.in+ file has to be updated manually to include the
+  generated Config.in files.
+
 [[luarocks-package-reference]]
 
 ==== +luarocks-package+ reference
diff --git a/package/luarocks/0002-Buildroot-addon.patch b/package/luarocks/0002-Buildroot-addon.patch
new file mode 100644
index 000000000..24e22b3d7
--- /dev/null
+++ b/package/luarocks/0002-Buildroot-addon.patch
@@ -0,0 +1,348 @@ 
+From 76b7ec7af0e3b46178fa6d3cfac3810b60e88e16 Mon Sep 17 00:00:00 2001
+From: Francois Perrad <francois.perrad@gadz.org>
+Date: Sat, 27 Oct 2018 17:15:50 +0200
+Subject: [PATCH 2/2] Buildroot addon
+
+Signed-off-by: Francois Perrad <francois.perrad@gadz.org>
+---
+ src/bin/luarocks                 |   1 +
+ src/luarocks/addon/buildroot.lua | 315 +++++++++++++++++++++++++++++++
+ 2 files changed, 316 insertions(+)
+ create mode 100644 src/luarocks/addon/buildroot.lua
+
+diff --git a/src/bin/luarocks b/src/bin/luarocks
+index d982530..53c7b0b 100755
+--- a/src/bin/luarocks
++++ b/src/bin/luarocks
+@@ -31,6 +31,7 @@ local commands = {
+    config = "luarocks.cmd.config",
+    which = "luarocks.cmd.which",
+    test = "luarocks.cmd.test",
++   buildroot = "luarocks.addon.buildroot",
+ }
+ 
+ cmd.run_command(description, commands, "luarocks.cmd.external", ...)
+diff --git a/src/luarocks/addon/buildroot.lua b/src/luarocks/addon/buildroot.lua
+new file mode 100644
+index 0000000..69d2222
+--- /dev/null
++++ b/src/luarocks/addon/buildroot.lua
+@@ -0,0 +1,315 @@
++
++--- Module implementing the LuaRocks "buildroot" command.
++local buildroot = {}
++
++local dir = require("luarocks.dir")
++local fs = require("luarocks.fs")
++local util = require("luarocks.util")
++local queries = require("luarocks.queries")
++local search = require("luarocks.search")
++local download = require("luarocks.download")
++local fetch = require("luarocks.fetch")
++
++buildroot.help_summary = "generate buildroot package files of a rock."
++buildroot.help_arguments = "rockname [brname]"
++buildroot.help = [[
++This addon generates Buildroot package files of a rock.
++First argument is the name of a rock, the second argument is optional
++and needed when Buildroot uses another name (usually prefixed by lua-).
++Files are generated with the source content of the rock and more
++especially the rockspec. So, the rock is downloaded and unpacked.
++]]
++
++local function brname (name)
++   return name:upper():gsub('-', '_')
++end
++
++local function brlicense (license)
++   if license:match('MIT/X') then
++      return 'MIT'
++   end
++   return license
++end
++
++local function wrap (txt, max)
++   local lines = {}
++   local line = ''
++   for word in txt:gmatch('(%S+)') do
++      if line:len() + word:len() > max - 1 then
++          lines[#lines+1] = line
++          line = ''
++      end
++      if line == '' then
++         line  = word
++      else
++         line = line .. ' ' .. word
++      end
++   end
++   lines[#lines+1] = line
++   return lines
++end
++
++local function get_native_dependencies (rockspec)
++   local t = {}
++   local d = rockspec.external_dependencies
++   if d then
++      if d.platforms and d.platforms.unix then
++         d = d.platforms.unix
++      end
++      for k in pairs(d) do
++         if k ~= 'platforms' then
++            k = k:lower()
++            if fs.is_dir('package/' .. k) then
++               t[#t+1] = k
++            else
++               t[#t+1] = 'lib' .. k
++            end
++         end
++      end
++      table.sort(t)
++   end
++   return t
++end
++
++local function get_dependencies (rockspec)
++   local t = {}
++   for i = 1, #rockspec.dependencies do
++      local dep = tostring(rockspec.dependencies[i]):match('^(%S+)')
++      if dep ~= 'lua' then
++         dep = dep:gsub('_', '-')
++         if fs.is_dir('package/lua-' .. dep) then
++            t[#t+1] = 'lua-' .. dep
++         else
++            t[#t+1] = dep
++         end
++      end
++   end
++   table.sort(t)
++   return t
++end
++
++local function generate_config (rockspec, lcname)
++   local ucname = brname(lcname)
++   local only_luajit = rockspec.package:match('^lj')
++   local summary = rockspec.description.summary
++   if not summary then
++      summary = '???'
++   elseif not summary:match('%.%s*$') then
++      summary = summary:gsub('%s*$', '.')
++   end
++   local homepage = rockspec.description.homepage or '???'
++   local native_dependencies = get_native_dependencies(rockspec)
++   local dependencies = get_dependencies(rockspec)
++   local fname = 'package/' .. lcname .. '/Config.in'
++   local f = assert(io.open(fname, 'w'))
++   util.printout('write ' .. fname)
++   f:write('config BR2_PACKAGE_' .. ucname .. '\n')
++   f:write('\tbool "' .. lcname .. '"\n')
++   if only_luajit then
++      f:write('\tdepends on BR2_PACKAGE_LUAJIT\n')
++   end
++   for i = 1, #native_dependencies do
++      f:write('\tselect BR2_PACKAGE_' .. brname(native_dependencies[i]) .. '\n')
++   end
++   for i = 1, #dependencies do
++      f:write('\tselect BR2_PACKAGE_' .. brname(dependencies[i]) .. ' # runtime\n')
++   end
++   f:write('\thelp\n')
++   f:write('\t  ' .. table.concat(wrap(summary, 62), '\n\t  ') .. '\n')
++   f:write('\n\t  ' .. homepage .. '\n')
++   if only_luajit then
++      f:write('\ncomment "' .. lcname .. ' needs LuaJIT"\n')
++      f:write('\tdepends on !BR2_PACKAGE_LUAJIT\n')
++   end
++   f:close()
++end
++
++local function generate_mk (rockspec, lcname, licenses)
++   local function escape (s)
++      return s:gsub('-', '%%-'):gsub('%.', '%%.')
++   end
++
++   local ucname = brname(lcname)
++   local need_name_upstream = false
++   local need_version_upstream = false
++   local name_upstream = rockspec.package
++   local version = rockspec.version
++   local version_upstream = version:match('^([^-]+)-')
++   local revision = version:match('-(%d+)$')
++   local license = rockspec.description.license
++   local subdir = rockspec.source.dir
++   if subdir then
++      local root = subdir:match('^(.-)-' .. escape(version) .. '$')
++      if root then
++         subdir = root .. '-$(' .. ucname .. '_VERSION)'
++      end
++      root = subdir:match('^(.--[Vv])' .. escape(version_upstream) .. '$')
++      if root then
++         need_version_upstream = true
++         subdir = root .. '$(' .. ucname .. '_VERSION_UPSTREAM)'
++      end
++      root = subdir:match('^(.-)-' .. escape(version_upstream) .. '$')
++      if root then
++         if root == lcname then
++            subdir = nil
++         elseif root == name_upstream then
++            subdir = nil
++            need_name_upstream = true
++         else
++            need_version_upstream = true
++            subdir = root .. '-$(' .. ucname .. '_VERSION_UPSTREAM)'
++         end
++      end
++   end
++   local native_dependencies = get_native_dependencies(rockspec)
++   local fname = 'package/' .. lcname .. '/' .. lcname .. '.mk'
++   local f = assert(io.open(fname, 'w'))
++   util.printout('write ' .. fname)
++   f:write('################################################################################\n')
++   f:write('#\n')
++   f:write('# ' .. lcname .. '\n')
++   f:write('#\n')
++   f:write('################################################################################\n')
++   f:write('\n')
++   if need_version_upstream then
++      f:write(ucname .. '_VERSION_UPSTREAM = ' .. version_upstream .. '\n')
++      f:write(ucname .. '_VERSION = $(' .. ucname .. '_VERSION_UPSTREAM)-' .. revision .. '\n')
++   else
++      f:write(ucname .. '_VERSION = ' .. version .. '\n')
++   end
++   if lcname ~= name_upstream:lower() or need_name_upstream then
++      f:write(ucname .. '_NAME_UPSTREAM = ' .. name_upstream .. '\n')
++   end
++   if subdir then
++      f:write(ucname .. '_SUBDIR = ' .. subdir .. '\n')
++   end
++   if license then
++      f:write(ucname .. '_LICENSE = ' .. brlicense(license) .. '\n')
++   end
++   if #licenses == 1 then
++      f:write(ucname .. '_LICENSE_FILES = $(' .. ucname .. '_SUBDIR)/' .. licenses[1] .. '\n')
++   elseif #licenses > 1 then
++      f:write(ucname .. '_LICENSE_FILES =')
++      for i = 1, #licenses do
++         local file = licenses[i]
++         f:write(' \\\n    $(' .. ucname .. '_SUBDIR)/' .. file)
++      end
++      f:write('\n')
++   end
++   if #native_dependencies > 0 then
++      f:write(ucname .. '_DEPENDENCIES = ' .. table.concat(native_dependencies, ' ') .. '\n')
++   end
++   f:write('\n$(eval $(luarocks-package))\n')
++   f:close()
++end
++
++local function generate_hash (rockspec, lcname, rock_file, licenses, md5)
++   local subdir = rockspec.source.dir
++   local fname = 'package/' .. lcname .. '/' .. lcname .. '.hash'
++   local f = assert(io.open(fname, 'w'))
++   util.printout('write ' .. fname)
++   f:write('# computed by luarocks\n')
++   f:write('md5 ' .. md5[rock_file] .. '  ' .. rock_file .. '\n')
++   for i = 1, #licenses do
++      local file = licenses[i]
++      f:write('md5 ' .. md5[file] .. '  ' .. subdir .. '/' .. file .. '\n')
++   end
++   f:close()
++end
++
++--- Driver function for the "buildroot" command.
++-- @param rockname string: the name of a rock to be fetched and unpacked.
++-- @param brname string: the name used by Buildroot (optional)
++-- @return boolean: true if successful
++function buildroot.command(flags, rockname, fsname)
++   if type(rockname) ~= 'string' then
++      return nil, "Argument missing. "..util.see_help('buildroot')
++   end
++   fsname = fsname or rockname
++   assert(type(fsname) == 'string')
++
++   local query = queries.new(rockname:lower(), nil, false, 'src')
++   local url, err = search.find_suitable_rock(query)
++   if not url then
++      return nil, "Could not find a result named " .. tostring(query) .. ": " .. err
++   end
++   local rock_file = dir.base_name(url)
++
++   local temp_dir, err = fs.make_temp_dir(rockname)
++   if not temp_dir then
++      return nil, "Failed creating temporary dir: " .. err
++   end
++   local ok, err = fs.change_dir(temp_dir)
++   if not ok then return nil, err end
++
++   ok = fs.download(url, rock_file, true)
++   if not ok then
++      return nil, "Failed downloading " .. url
++   end
++
++   local md5 = {}
++   md5[rock_file], err = fs.get_md5(rock_file)
++   if not md5[rock_file] then return nil, err end
++   ok, err = fs.unzip(rock_file)
++   if not ok then return nil, err end
++
++   local rockspec_file = rock_file:gsub('%.src%.rock$', '.rockspec')
++   local rockspec, err = fetch.load_rockspec(rockspec_file)
++   if not rockspec then
++      return nil, "Error loading rockspec: " .. err
++   end
++   if rockspec.source.file then
++      ok, err = fs.unpack_archive(rockspec.source.file)
++      if not ok then return nil, err end
++   end
++
++   if rockspec.source.dir ~= '.' then
++      fs.copy(rockspec.local_abs_filename, rockspec.source.dir, 'read')
++   end
++
++   local build_type = rockspec.build.type
++   if build_type ~= 'none' and build_type ~= 'builtin' and build_type ~= 'module' then
++      util.printout('[' .. rockspec.package .. '] ' .. build_type .. " not supported")
++   end
++
++   local licenses = {}
++   ok, err = fs.change_dir(rockspec.source.dir)
++   if not ok then return nil, err end
++   local files = fs.find()
++   for i = 1, #files do
++      local v = files[i]
++      if v == 'COPYING'
++         or v == 'COPYRIGHT'
++         or v:match('^LICENSE') then
++         licenses[#licenses+1] = v
++         md5[v], err = fs.get_md5(v)
++         if not md5[v] then return nil, err end
++      end
++   end
++   if #licenses == 0 then
++      for i = 1, #files do
++         local v = files[i]
++         if v:match('^doc/LICENSE')
++            or v:match('^doc/license')
++            or v:match('^doc/us/license') then
++            licenses[#licenses+1] = v
++            md5[v], err = fs.get_md5(v)
++            if not md5[v] then return nil, err end
++         end
++      end
++   end
++   fs.pop_dir()
++   table.sort(licenses)
++
++   fs.pop_dir()
++   ok, err = fs.make_dir('package/' .. fsname:lower())
++   if not ok then return nil, err end
++
++   generate_config(rockspec, fsname:lower())
++   generate_mk(rockspec, fsname:lower(), licenses)
++   generate_hash(rockspec, fsname:lower(), rock_file, licenses, md5)
++
++   return true
++end
++
++return buildroot
+-- 
+2.17.1
+