parsav  Check-in [e18c9de34d]

Overview
Comment:initial commit
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e18c9de34dde4fe2a832cbedf871c72c91901ef0d40d784fbe305ee291a07fe7
User & Date: lexi on 2020-12-10 05:28:52
Other Links: manifest | tags
Context
2020-12-14
14:40
more boilerplate, add template framework check-in: 6f17de4767 user: lexi tags: trunk
2020-12-10
05:28
initial commit check-in: e18c9de34d user: lexi tags: trunk
05:25
initial empty check-in check-in: 1f9b1ddd5b user: lexi tags: trunk
Changes

Added common.lua version [9dc63595c4].

            1  +local map = function(lst,fn)
            2  +	local new = {} for k,v in pairs(lst) do
            3  +		local nv, nk = fn(v,k)
            4  +		new[nk or k] = nv
            5  +	end
            6  +	return new
            7  +end
            8  +
            9  +local chomp = function(str)
           10  +	while string.sub(str,#str) == '\n' do
           11  +		str = string.sub(str,1,#str - 1)
           12  +	end
           13  +	return str
           14  +end
           15  +
           16  +local exec = function(args, val)
           17  +	local cmd = table.concat(map(args, function(a)
           18  +		return string.format('%q', a)
           19  +	end), ' ')
           20  +	if val then return os.execute(cmd) else
           21  +		local fd = io.popen(cmd,'r')
           22  +		local t = fd:read('*a')
           23  +		return chomp(t), fd:close()
           24  +	end
           25  +end
           26  +
           27  +local function dump(v,pfx)
           28  +	pfx = pfx or ''
           29  +	local np = pfx .. '  '
           30  +	if type(v) == 'string' then
           31  +		return string.format('%q', v)
           32  +	elseif type(v) == 'table' then
           33  +		local str = ''
           34  +		for k,v in pairs(v) do
           35  +			str = str .. string.format('%s[%s] = %s\n', np, dump(k,np), dump(v,np))
           36  +		end
           37  +		return '{\n' .. str .. pfx .. '}\n'
           38  +	else
           39  +		return string.format('%s', v)
           40  +	end
           41  +end
           42  +local ping = function(path)
           43  +	local f = io.open(path)
           44  +	if f then f:close() return true end
           45  +	return false
           46  +end
           47  +local tobool = function(s)
           48  +	if s == true then return true
           49  +	elseif s == false then return false end
           50  +	s = ({
           51  +		yes = true, ['true'] = true,   on = true,   ['1'] = true;
           52  +		no = false, ['false'] = false, off = false, ['0'] = false;
           53  +	})[string.lower(s)]
           54  +	if not s then return false end
           55  +	return true
           56  +end
           57  +return {
           58  +	exec = exec;
           59  +	dump = dump;
           60  +	ping = ping;
           61  +	chomp = chomp;
           62  +	map = map;
           63  +	tobool = tobool;
           64  +	append = function(a,b)
           65  +		for _, v in pairs(b) do a[#a+1] = v end
           66  +	end;
           67  +	has = function(haystack,needle,eq)
           68  +		eq = eq or function(a,b) return a == b end
           69  +		for k,v in pairs(haystack) do
           70  +			if eq(needle,v) then return k end
           71  +		end
           72  +	end;
           73  +	parseargs = function(a)
           74  +		local raw = false
           75  +		local opts, args = {}, {}
           76  +		for i,v in ipairs(a) do
           77  +			if v == '--' then
           78  +				raw = true 
           79  +			else
           80  +				if (not raw) and string.sub(v,1,2) == '--' then -- longopt
           81  +					opts[#opts+1] = string.sub(v,3)
           82  +				elseif (not raw) and string.sub(v,1,1) == '-' then -- shortopts
           83  +					for c in string.gmatch(string.sub(v,2), '.') do
           84  +						opts[#opts+1] = c
           85  +					end
           86  +				else args[#args+1]=v end
           87  +			end
           88  +		end
           89  +		return opts, args
           90  +	end;
           91  +}

Added config.lua version [f14c1e8df4].

            1  +local u = dofile('common.lua')
            2  +local default = function(var,def)
            3  +	local v = os.getenv(var)
            4  +	if v then return v else return def end
            5  +end
            6  +local posixes = {
            7  +	linux = true; osx = true;
            8  +	android = true; haiku = true;
            9  +}
           10  +local conf = {
           11  +	pkg = {};
           12  +	os        = default('parsav_target_os', 'linux');
           13  +	dist      = default('parsav_dist', os.getenv('NIX_PATH') and 'nixos');
           14  +	tgttrip   = default('parsav_arch_triple'); -- target triple, used in xcomp
           15  +	tgtcpu    = default('parsav_arch_cpu'); -- target cpu, used in xcomp
           16  +	tgthf     = u.tobool(default('parsav_arch_armhf',true)); 
           17  +}
           18  +conf.posix = posixes[conf.os]
           19  +conf.exe   = u.tobool(default('parsav_link',not conf.tgttrip)); -- turn off for partial builds
           20  +
           21  +local fallback = dofile('pkgdata.lua')
           22  +local libfind do local mkf = function(p)
           23  +		return function(l) return string.format(p,l) end
           24  +	end
           25  +	local unx = function(p)
           26  +		return function(l)
           27  +			if string.sub(l,1,3) == 'lib'
           28  +				then return string.format('%s.%s',l,p)
           29  +				else return string.format('lib%s.%s',l,p)
           30  +			end
           31  +		end
           32  +	end
           33  +
           34  +	libfind = {
           35  +		linux = {
           36  +			static = unx 'a';
           37  +			dynamic = unx 'so';
           38  +		};
           39  +		osx = { dynamic = mkf '%s.dylib'; };
           40  +		win = { dynamic = mkf '%s.dll'; };
           41  +	}
           42  +end
           43  +local pkg = function(name)
           44  +	local fbo = fallback[name] and fallback[name].osvars
           45  +		and fallback[name].osvars[conf.os .. '_' .. conf.dist]
           46  +	local fbv = fallback[name] and fallback[name].vars
           47  +	local pkgenv = function(e)
           48  +		return os.getenv(string.format('parsav_pkg_%s_%s',name,e))
           49  +	end
           50  +	name = pkgenv('override') or name
           51  +	local nul = function() end
           52  +	local pkc, pkv = nul, nul
           53  +	local cnfvar = function(v,e)
           54  +		local eval = function(e)
           55  +			if type(e) == 'function'
           56  +				then return e(conf)
           57  +				else return e
           58  +			end
           59  +		end
           60  +		return pkgenv(v) or pkv(e or v) or (fbo and eval(fbo[v])) or (fbv and eval(fbv[v]))
           61  +	end
           62  +	if conf.posix then
           63  +		pkc  = function(...) return u.exec { 'pkg-config'; name; ...  } end
           64  +		local pkcv = function(...) return u.exec({ 'pkg-config'; name; ...  }, true) end
           65  +		if pkcv('--exists') == 0 then
           66  +			 pkv = function(v)
           67  +				return pkc('--variable', v)
           68  +			end
           69  +		else pkc = nul end
           70  +	else
           71  +		print '(warn) configuring on non-POSIX OS, all relevant paths must be specified manually in environment variables or build will fail!'
           72  +	end
           73  +	local locdep = u.ping('./lib/' .. name)
           74  +	local prefix
           75  +	if locdep then
           76  +		prefix = './lib/' .. name
           77  +	end
           78  +	prefix = prefix or cnfvar('prefix')
           79  +	local libdir = cnfvar('libdir')
           80  +	if not libdir then
           81  +		if locdep
           82  +			then libdir = prefix .. (cnfvar('builddir') or cnfvar('libbuilddir')) or ''
           83  +			else libdir = prefix .. '/lib'
           84  +		end
           85  +	end
           86  +	local incdir = cnfvar('incdir','includedir') or (prefix .. '/include')
           87  +	local libstr = pkc '--libs-only-l' -- (--static is not reliable)
           88  +	local libs = fallback[name] and fallback[name].libs or {}
           89  +	local linkstatic = locdep
           90  +	if (not locdep) and libstr then
           91  +		libs = {}
           92  +		for m in string.gmatch(libstr, '-l(%g+)') do
           93  +			libs[#libs + 1] = m
           94  +		end
           95  +	else
           96  +		if #libs == 0 then libs = { name } end
           97  +	end
           98  +
           99  +	conf.pkg[name] = {
          100  +		prefix = prefix;
          101  +		libdir = libdir;
          102  +		incdir = incdir;
          103  +		dylibs = {}, statlibs = {};
          104  +	}
          105  +	local me = conf.pkg[name]
          106  +	
          107  +	local lf = libfind[conf.os]
          108  +	if lf.dynamic then me.dylibs   = u.map(libs, lf.dynamic) end
          109  +	if lf.static  then me.statlibs = u.map(libs, lf.static)  end
          110  +
          111  +	if linkstatic then
          112  +		local ldf = {}
          113  +		for _,v in pairs(me.statlibs) do
          114  +			local fl = libdir .. '/' .. v
          115  +			if u.ping(fl) then ldf[#ldf+1] = fl end
          116  +		end
          117  +		me.linkargs = ldf
          118  +	else
          119  +		local arg = name
          120  +		if string.sub(arg,1,3) == 'lib' then arg = string.sub(arg,4) end
          121  +		local linkline = libstr or string.format('-l%s', arg)
          122  +		me.linkargs = {
          123  +			string.format('-L%s', me.libdir);
          124  +		}
          125  +		for m in string.gmatch(linkline, '(%g+)') do
          126  +			me.linkargs[#me.linkargs+1] = m
          127  +		end
          128  +
          129  +		-- siiiiigh
          130  +		for _,l in pairs(libs) do
          131  +			local sw = string.format('-l%s', l) 
          132  +			local found = false
          133  +			for _,a in pairs(me.linkargs) do
          134  +				if a == sw then found = true break end
          135  +			end
          136  +			if not found then
          137  +				me.linkargs[#me.linkargs+1] = sw
          138  +			end
          139  +		end
          140  +	end
          141  +end
          142  +
          143  +pkg('mbedtls')
          144  +pkg('libhttp')
          145  +pkg('json-c')
          146  +
          147  +return conf

Added makefile version [b49975ddb0].

            1  +dl = git
            2  +dbg-flags = $(if $(dbg),-g)
            3  +
            4  +parsav: parsav.t config.lua pkgdata.lua
            5  +	terra $(dbg-flags) $<
            6  +parsav.o: parsav.t config.lua pkgdata.lua
            7  +	env parsav_link=no terra $(dbg-flags) $<
            8  +
            9  +clean:
           10  +	rm parsav parsav.o
           11  +
           12  +dep: dep.mbedtls dep.libhttp dep.json-c
           13  +dep.mbedtls: lib/mbedtls/library/libmbedtls.a \
           14  +	lib/mbedtls/library/libmbedcrypto.a \
           15  +	lib/mbedtls/library/libmbedx509.a
           16  +dep.libhttp: lib/libhttp/lib/libhttp.a
           17  +dep.json-c: lib/libhttp/json-c.a
           18  +
           19  +lib:
           20  +	mkdir $@
           21  +# parsav is designed to be fronted by a real web
           22  +# server like nginx if SSL is to be used
           23  +# caveat: libhttp is a mess. the docs are completely
           24  +# full of shit. there is no lua support as far as i
           25  +# can tell.
           26  +lib/libhttp/lib/libhttp.a: lib/libhttp
           27  +	$(MAKE) -C $< lib/libhttp.a \
           28  +		RM='rm -f' \
           29  +		CC="$(CC) -Wno-unused-result" \
           30  +		DFLAGS="-DNO_SSL -DNO_FILES -DNO_CGI -DUSE_STACK_SIZE=102400 -DUSE_IPV6"
           31  +
           32  +lib/json-c/Makefile: lib/json-c lib/json-c/CMakeLists.txt
           33  +	cd lib/json-c && cmake .
           34  +lib/json-c/libjson-c.a: lib/json-c/Makefile
           35  +	$(MAKE) -C lib/json-c
           36  +lib/mbedtls/library/%.a: lib/mbedtls 
           37  +	$(MAKE) -C lib/mbedtls/library $*.a
           38  +
           39  +ifeq ($(dl), git)
           40  +lib/libhttp: lib
           41  +	cd lib && git clone https://github.com/lammertb/libhttp.git
           42  +lib/mbedtls: lib
           43  +	cd lib && git clone https://github.com/ARMmbed/mbedtls.git
           44  +lib/json-c: lib
           45  +	cd lib && git clone https://github.com/json-c/json-c.git
           46  +else
           47  +lib/%: lib/%.tar.gz
           48  +	cd lib && tar zxf $*.tar.gz
           49  +	mv lib/$$(tar tf $< | head -n1) $@
           50  +
           51  +ifeq ($(dl), wget)
           52  +    dlfile = wget "$(1)" -O "$(2)"
           53  +endif
           54  +
           55  +ifeq ($(dl), curl)
           56  +    dlfile = curl "$(1)" -o "$(2)"
           57  +endif
           58  +
           59  +lib/libhttp.tar.gz: lib
           60  +	$(call dlfile,https://api.github.com/repos/lammertb/libhttp/tarball/master,$@)
           61  +lib/mbedtls.tar.gz: lib
           62  +	$(call dlfile,https://api.github.com/repos/ARMmbed/mbedtls/tarball/master,$@)
           63  +lib/json-c.tar.gz: lib
           64  +	$(call dlfile,https://api.github.com/repos/json-c/json-c/tarball/master,$@)
           65  +endif

Added parsav.md version [d4a5d691bd].

            1  +# parsav
            2  +
            3  +**parsav** is a lightweight fediverse server
            4  +
            5  +## dependencies
            6  +
            7  +* libhttp
            8  +* json-c
            9  +* mbedtls
           10  +* postgresql-libs
           11  +
           12  +## building
           13  +
           14  +first, either install any missing dependencies as shared libraries, or build them as static libraries as described below:
           15  +
           16  +* libhttp: run `$ make lib/libhttp/lib/libhttp.a`
           17  +* json-c (deps: `cmake`): run `$ make lib/json-c/libjson-c.a`
           18  +* mbedtls: run `$ make lib/mbedtls/lib/mbed{crypto,tls,x509}.a`
           19  +
           20  +you can install static libraries for all dependencies with `$ make dep`, but this is recommended only if you have none of the above

Added parsav.t version [d2f35b212c].

            1  +-- vim: ft=terra
            2  +
            3  +local util = dofile('common.lua')
            4  +local buildopts, buildargs = util.parseargs{...}
            5  +
            6  +local config = dofile('config.lua')
            7  +
            8  +local lib = {
            9  +	loadlib = function(name,hdr)
           10  +		local p = config.pkg[name]
           11  +		-- for _,v in pairs(p.dylibs) do
           12  +		-- 	terralib.linklibrary(p.libdir .. '/' .. v)
           13  +		-- end
           14  +		return terralib.includec(p.incdir .. '/' .. hdr)
           15  +	end;
           16  +	randomize = terralib.externfunction('getrandom', {&opaque, intptr, uint} -> ptrdiff);
           17  +}
           18  +lib.rsa = lib.loadlib('mbedtls','mbedtls/rsa.h')
           19  +lib.pk = lib.loadlib('mbedtls','mbedtls/pk.h')
           20  +
           21  +local callbacks = {
           22  +	randomize = terra(ctx: &opaque, dest: &uint8, sz: intptr): int
           23  +		return lib.randomize(dest, sz, 0)
           24  +	end;
           25  +}
           26  +
           27  +terra entry(): int
           28  +	var pk: lib.pk.mbedtls_pk_context
           29  +	lib.pk.mbedtls_pk_init(&pk)
           30  +	lib.pk.mbedtls_pk_setup(&pk, lib.pk.mbedtls_pk_info_from_type(lib.pk.MBEDTLS_PK_RSA))
           31  +	var rsa = [&lib.rsa.mbedtls_rsa_context](pk.pk_ctx)
           32  +	lib.rsa.mbedtls_rsa_gen_key(rsa, callbacks.randomize, nil, 2048, 65537)
           33  +	return 0
           34  +end
           35  +
           36  +local bflag = function(long,short)
           37  +	if short and util.has(buildopts, short) then return true end
           38  +	if long and util.has(buildopts, long) then return true end
           39  +	return false
           40  +end
           41  +
           42  +if bflag('dump-config','C') then
           43  +	print(util.dump(config))
           44  +	os.exit(0)
           45  +end
           46  +
           47  +local emit = print
           48  +if bflag('quiet','q') then emit = function() end end
           49  +
           50  +local out = config.exe and 'parsav' or 'parsav.o'
           51  +local linkargs = {}
           52  +for _,p in pairs(config.pkg) do util.append(linkargs, p.linkargs) end
           53  +emit('linking with args',util.dump(linkargs))
           54  +terralib.saveobj(out, {
           55  +		main = entry
           56  +	},
           57  +	linkargs,
           58  +	config.tgttrip and terralib.newtarget {
           59  +		Triple = config.tgttrip;
           60  +		CPU = config.tgtcpu;
           61  +		FloatABIHard = config.tgthf;
           62  +	} or nil)

Added pkgdata.lua version [b4c0388029].

            1  +local util = dofile('common.lua')
            2  +local sthunk = function(...) local a = {...} return function() return util.exec(a) end end
            3  +return {
            4  +	mbedtls = { 
            5  +		libs = {'mbedtls', 'mbedcrypto', 'mbedx509'};
            6  +		osvars = {
            7  +			linux_nixos = { -- lacks a *.pc on nixos systems
            8  +				prefix = sthunk('nix', 'path-info', 'nixos.mbedtls');
            9  +			}
           10  +		};
           11  +		vars = { builddir = '/library' };
           12  +	};
           13  +	libhttp = { vars = { builddir = '/lib' }; };
           14  +}