Skip to content

Commit

Permalink
Cleanup global env
Browse files Browse the repository at this point in the history
Remove the most of the environment config from env.sh and also global
Jagen object. Migrating to the rule-based config. Rename some variables
for consistency while at it. Refresh works but running stage will break
because not all references are adjusted yet.
  • Loading branch information
bazurbat committed Sep 30, 2021
1 parent 974b5de commit 1f42a93
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 114 deletions.
3 changes: 3 additions & 0 deletions bin/jagen-stage
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pkg_args="$@"

. "${jagen_dir:?}/src/stages.sh" || exit

. "${jagen_include_dir:?}/jagen.config.sh" || exit
. "${jagen_include_dir:?}/root.config.sh" || exit

pkg__target="${pkg_name}:${pkg_stage}${pkg_config:+:${pkg_config}}"
pkg__stamp_file="${jagen_build_dir:?}/${pkg__target}"
pkg__log_file="${jagen_log_dir:?}/${pkg__target}.log"
Expand Down
2 changes: 0 additions & 2 deletions doc/Clean.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ SYNOPSIS
jagen_build_dir
jagen_include_dir
jagen_log_dir
jagen_host_dir
jagen_target_dir
Actual paths depend on configuration. After the deletion regenerates the
build system using the 'jagen refresh' command.
Expand Down
21 changes: 0 additions & 21 deletions env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,25 @@ jagen_IFS="$(printf '\n\t')"
jagen_dir="${jagen_dir:?}"
jagen_root_dir="$jagen_root_dir"

jagen_shell=""
jagen_lua="${jagen_lua-}"
: ${jagen_lua:=$(command -v luajit)}
: ${jagen_lua:=$(command -v lua)}

jagen_debug="${jagen_debug-}"
jagen_flags=""

jagen_lib_dir="$jagen_dir/lib"
jagen__src_dir="$jagen_dir/src"
jagen_root_lib_dir="$jagen_root_dir/lib"

jagen_bin_dir="$jagen_root_dir/bin"
jagen_src_dir="$jagen_root_dir/src"
jagen_dist_dir="$jagen_root_dir/dist"
jagen_build_dir="$jagen_root_dir/build"
jagen_include_dir="$jagen_root_dir/include"
jagen_log_dir="$jagen_root_dir/log"

jagen_build_verbose=${jagen_build_verbose-}

. "$jagen_dir/src/common.sh" || return

jagen_host_dir="$jagen_root_dir/host"
jagen_target_dir="$jagen_root_dir/target"
jagen_cargo_config_dir="$jagen_root_dir/.cargo"

add_PATH "$jagen_host_dir/bin"
add_LD_LIBRARY_PATH "$jagen_host_dir/lib"
export PATH LD_LIBRARY_PATH

jagen__set_path || return

import env || true # it is OK if no env was found

# May be set in layers and not exist during the env sourcing.
if [ "${jagen_private_dir-}" ]; then
add_PATH "$jagen_private_dir/bin"
fi

# export all jagen_* variables
for var in $(set | LC_ALL=C sed -n 's/^\(jagen_[[:alnum:]][[:alnum:]_]*\)=.*/\1/p'); do
export "$var"
Expand Down
33 changes: 14 additions & 19 deletions lib/rules.lua
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
config {
name = 'jagen',
dir = {
bin = '${dir.root}/bin',
build = '${dir.root}/build',
include = '${dir.root}/include',
log = '${dir.root}/log',
src = '${dir.root}/src',
},
dist_dir = 'dist',
src_dir = 'src',
flags = {},
source_exclude = {},
build_profile = "release"
name = 'jagen',
dir = os.getenv('jagen_dir'),
bin_dir = '${dir}/bin',
src_dir = '${dir}/src',
cmd = '${src_dir}/cmd.sh',
}

config {
name = 'root',
dir = os.getenv('jagen_root_dir'),
bin_dir = '${dir}/bin',
src_dir = '${dir}/src',
dist_dir = '${dir}/dist',
build_dir = '${dir}/build',
dir = os.getenv('jagen_root_dir'),
bin_dir = '${dir}/bin',
build_dir = '${dir}/build',
dist_dir = '${dir}/dist',
include_dir = '${dir}/include',
log_dir = '${dir}/log',
lib_dir = '${dir}/lib',
log_dir = '${dir}/log',
src_dir = '${dir}/src',
build_file = '${build_dir}/build.ninja',
build_targets_file = '${build_dir}/.build-targets',
}

config {
Expand Down
21 changes: 12 additions & 9 deletions src/Engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ end
function Engine:load_rules()
Log.debug2('load rules')

local jagen = Module:load('jagen', System.mkpath(Jagen.dir, 'lib', 'rules.lua'))
local root_loaded, root = pcall(Module.load, Module, 'root', System.mkpath(Jagen.root_dir, 'rules.lua'))
local jagen_dir = os.getenv('jagen_dir')
local root_dir = os.getenv('jagen_root_dir')

local jagen = Module:load('jagen', System.mkpath(jagen_dir, 'lib', 'rules.lua'))
local root_loaded, root = pcall(Module.load, Module, 'root', System.mkpath(root_dir, 'rules.lua'))

local toplevel_modules = {}
extend(toplevel_modules, self:unprocessed_uses(jagen))
Expand Down Expand Up @@ -92,13 +95,13 @@ function Engine:load_rules()
self:expand(pkg, pkg)
end

for _, config in pairs(self.config) do
print(pretty(config))
end
-- for _, config in pairs(self.config) do
-- print(pretty(config))
-- end

for pkg in each(self.packages) do
print(pretty(pkg))
end
-- for pkg in each(self.packages) do
-- print(pretty(pkg))
-- end

return self.packages
end
Expand All @@ -114,7 +117,7 @@ function Engine:finalize()
pkg._targets = {}
for name, stage in pairs(pkg.stages or {}) do
local target = Target.from_args(pkg.name, name)
target.log = System.mkpath(self.config.jagen.dir.log, target.ref..'.log')
target.log = System.mkpath(self.config.root.log_dir, target.ref..'.log')
target.inputs = stage.inputs
pkg._targets[name] = target
end
Expand Down
41 changes: 2 additions & 39 deletions src/Jagen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,11 @@ local function die(...)
os.exit(1)
end

Jagen =
{
dir = os.getenv('jagen_dir'),
root_dir = os.getenv('jagen_root_dir'),
Jagen = {}

shell = os.getenv('jagen_shell'),
flags = os.getenv('jagen_flags'),

include_dir = os.getenv('jagen_include_dir'),
log_dir = os.getenv('jagen_log_dir'),

work_dir = os.getenv('jagen_work_dir'),
src_dir = os.getenv('jagen_src_dir'),
source_dir = os.getenv('jagen_src_dir'),
host_dir = assert(os.getenv('jagen_host_dir')),
target_dir = assert(os.getenv('jagen_target_dir')),
build_dir = assert(os.getenv('jagen_build_dir')),

has_console = os.getenv('jagen__has_console')
}

Jagen.cmd = System.mkpath(Jagen.dir, 'src', 'cmd.sh')
Jagen.pager = os.getenv('jagen_pager') or os.getenv('PAGER') or 'less'
Jagen.build_file = System.mkpath(Jagen.build_dir, 'build.ninja')
Jagen.build_targets_file = System.mkpath(Jagen.build_dir, '.build-targets')

function Jagen.flag(f)
error('not implemented')
for w in string.gmatch(Jagen.flags, "[_%w]+") do
if w == f then
return true
Expand All @@ -49,10 +27,6 @@ function Jagen.flag(f)
return false
end

function Jagen:find_for_refresh()
return Command:new(quote(Jagen.cmd), 'find_for_refresh'):aslist()
end

-- src

local function scm_packages(patterns)
Expand Down Expand Up @@ -294,9 +268,6 @@ function Jagen.command.clean(args)
'jagen_build_dir',
'jagen_include_dir',
'jagen_log_dir',
'jagen_host_dir',
'jagen_target_dir',
'jagen_cargo_config_dir',
}
if not System.rmrf(unpack(System.getenv(clean_dirs))) then
return false
Expand Down Expand Up @@ -513,14 +484,6 @@ function Jagen.command.update(args)
return retval
end

function Jagen.command.image(args)
if #args == 0 or help_requested(args) then
return Jagen.command['help'] { 'image' }
end
return Command:new(quote(System.mkpath(Jagen.dir, 'src', 'cmd.sh')),
'image', quote(unpack(args))):exec()
end

function Jagen.command._compare_versions(args)
assert(#args == 3, "function 'compare_versions' requires 3 arguments")
local op_arg = assert(args[1])
Expand Down
15 changes: 9 additions & 6 deletions src/Ninja.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local P = {}
local System = require 'System'
local Target = require 'Target'
local Command = require 'Command'

local format = string.format
local concat = table.concat
Expand Down Expand Up @@ -297,27 +298,29 @@ local function assign_pools(packages)
end
end

function P.generate(out_file, rules)
function P.generate(rules, config, jagen)
check_ninja_features()

local out_file = config.build_file

packages = rules
local file = assert(io.open(out_file, 'w'))

assign_pools(rules)

local lines = {
binding('ninja_required_version', '1.1'),
binding('builddir', assert(Jagen.build_dir)),
binding('builddir', assert(config.build_dir)),
format_pool('gradle_android', 1),
format_pool('rust_toolchain', 1),
format_rule('stage', join {
separated(Jagen.shell), 'jagen-stage $args'
separated(config.shell), 'jagen-stage $args'
}),
format_rule('refresh', join_space(nonempty { Jagen.shell, System.expand('$jagen_root_dir/jagen'), 'refresh' }))
format_rule('refresh', join_space(nonempty { config.shell, System.expand('$jagen_root_dir/jagen'), 'refresh' }))
}

local for_refresh = Jagen:find_for_refresh()
local include_dir = Jagen.include_dir
local for_refresh = Command:new(jagen.cmd, 'find_for_refresh'):aslist()
local include_dir = config.include_dir
for pkg in each(rules) do
append(for_refresh, System.mkpath(include_dir, string.format('%s.sh', pkg.ref)))
end
Expand Down
16 changes: 7 additions & 9 deletions src/Refresh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,17 @@ function Refresh:run(args)

engine:finalize()

-- print(pretty(packages))
local include_dir = engine.config.root.include_dir

-- print(pretty(engine.config.jagen))
local jagen = engine.config.jagen
local root = engine.config.root

local dir = engine.config.jagen.dir

-- System.mkdir(dir.build, dir.include, dir.log)
System.mkdir(dir.build, dir.include)
System.mkdir(root.build_dir, root.include_dir)

local targets = {}

for pkg in each(packages) do
local filename = System.mkpath(dir.include, string.format('%s.sh', pkg.name))
local filename = System.mkpath(include_dir, string.format('%s.sh', pkg.name))
Script:write(pkg, filename)

for name, stage in pairs(pkg.stages or {}) do
Expand All @@ -65,14 +63,14 @@ function Refresh:run(args)
end

for name, config in pairs(engine.config) do
local filename = System.mkpath(dir.include, string.format('%s.config.sh', name))
local filename = System.mkpath(include_dir, string.format('%s.config.sh', name))
if name ~= 'jagen' then
name = 'jagen_'..name
end
Script:write(config, filename, name)
end

Ninja.generate(Jagen.build_file, packages)
Ninja.generate(packages, root, jagen)
end

return Refresh
17 changes: 10 additions & 7 deletions src/command/build.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ local Target = require 'Target'

local P = {}

local function write_targets(targets, args)
local is_interactive = Jagen.has_console and not args['quiet'] and not (args['progress'] or args['follow'] or args['follow-all'])
local function write_targets(targets, args, build_targets_file)
local has_console = os.getenv('jagen__has_console')
local is_interactive = has_console and not args['quiet'] and not (args['progress'] or args['follow'] or args['follow-all'])
local curr_list, saved_list, is_eq = {}, {}, true

if is_interactive then
Expand All @@ -20,7 +21,7 @@ local function write_targets(targets, args)
sort(curr_list)
end

local file = io.open(Jagen.build_targets_file)
local file = io.open(build_targets_file)
if file then
for line in file:lines() do
append(saved_list, line)
Expand All @@ -40,7 +41,7 @@ local function write_targets(targets, args)
end

if not is_eq then
local file = assert(io.open(Jagen.build_targets_file, 'w'))
local file = assert(io.open(build_targets_file, 'w'))
for target in each(curr_list) do
file:write(target, '\n')
end
Expand Down Expand Up @@ -139,16 +140,18 @@ function P:run(args)
return false
end

write_targets(targets, args)
local config = engine.config.root

local args_path = System.mkpath(Jagen.build_dir, '.build-args')
write_targets(targets, args, config.build_targets_file)

local args_path = System.mkpath(config.build_dir, '.build-args')
local args_file = assert(io.open(args_path, 'w'))
if args._args then
args_file:write(table.concat(args._args, '\n'))
end
args_file:close()

local ok = Command:new(quote(Jagen.cmd), 'build', tostring(args), unpack(table.keys(targets))):exec()
local ok = Command:new(quote(config.cmd), 'build', tostring(args), unpack(table.keys(targets))):exec()

-- io.open(args_path, 'w'):close()

Expand Down
2 changes: 0 additions & 2 deletions src/help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ SYNOPSIS
jagen_build_dir
jagen_include_dir
jagen_log_dir
jagen_host_dir
jagen_target_dir
Actual paths depend on configuration. After the deletion regenerates the
build system using the 'jagen refresh' command.
Expand Down

0 comments on commit 1f42a93

Please sign in to comment.