Skip to content

Commit

Permalink
Add a HLL::SysConfig class
Browse files Browse the repository at this point in the history
This class acts as a central point where configuration information is
stored. The class should only ever be accessed via
`nqp::gethllsym('default', 'SysConfig')`. This then allows the class to be
overridden by a subclass.
  • Loading branch information
patrickbkr committed Aug 7, 2020
1 parent 39ba5f1 commit d2a35ac
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 39 deletions.
40 changes: 6 additions & 34 deletions src/HLL/Compiler.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ class HLL::Compiler does HLL::Backend::Default {
has @!cmdoptions;
has $!compiler_progname;
has $!language;
has %!config;
has $!user_progname;
has @!cli-arguments;
has %!cli-options;
has $!backend;
has $!save_ctx;
has $!nqp-home;

method BUILD() {
# Backend is set to the default one, by default.
Expand All @@ -32,7 +30,6 @@ class HLL::Compiler does HLL::Backend::Default {
~ ' confprog=s'
#?endif
);
%!config := nqp::hash();
}

method backend(*@value) {
Expand All @@ -55,7 +52,7 @@ class HLL::Compiler does HLL::Backend::Default {
nqp::getcomp($name);
}

method config() { %!config };
method sysconfig() { nqp::gethllsym('default', 'SysConfig').nqp-build-config }

method autoprint($value) {
self.interactive_result($value)
Expand Down Expand Up @@ -600,7 +597,7 @@ class HLL::Compiler does HLL::Backend::Default {
}

method version() {
my $version := %!config<version>;
my $version := self.sysconfig()<version>;
my $backver := $!backend.version_string();
my $implementation := self.implementation();
my $language_name := self.language_name();
Expand All @@ -624,8 +621,9 @@ class HLL::Compiler does HLL::Backend::Default {
for sorted_keys($!backend.config) -> $key {
nqp::say($bname ~ '::' ~ $key ~ '=' ~ $!backend.config{$key});
}
for sorted_keys(%!config) -> $key {
nqp::say($!language ~ '::' ~ $key ~ '=' ~ %!config{$key});
my %sysconfig := self.sysconfig();
for sorted_keys(%sysconfig) -> $key {
nqp::say($!language ~ '::' ~ $key ~ '=' ~ %sysconfig{$key});
}
nqp::exit(0);
}
Expand Down Expand Up @@ -813,35 +811,9 @@ class HLL::Compiler does HLL::Backend::Default {
nqp::die("The backend { $backend.HOW.name($backend) } doesn't support profiler-snapshot");
}
}

method nqp-home() {
if !$!nqp-home {
# Determine NQP directory
my $config := nqp::backendconfig();
my $sep := $config<osname> eq 'MSWin32' ?? '\\' !! '/';
#?if jvm
# TODO could be replaced by nqp::execname() after the next bootstrap for JVM
my $execname := nqp::atkey(nqp::jvmgetproperties,'nqp.execname') // '';
#?endif
#?if !jvm
my $execname := nqp::execname();
#?endif
my $install-dir := $execname eq ''
?? self.config<prefix>
!! nqp::substr($execname, 0, nqp::rindex($execname, $sep, nqp::rindex($execname, $sep) - 1));

$!nqp-home := nqp::getenvhash()<NQP_HOME>
// self.config<static-nqp-home>
|| $install-dir ~ '/share/nqp';
if nqp::substr($!nqp-home, nqp::chars($!nqp-home) - 1) eq $sep {
$!nqp-home := nqp::substr($!nqp-home, 0, nqp::chars($!nqp-home) - 1);
}
}

$!nqp-home;
}
}

my $compiler := HLL::Compiler.new();
$compiler.language($compiler.backend.name);
nqp::bindcomp('default', $compiler);
nqp::bindhllsym('default', 'SysConfig', HLL::SysConfig.new());
38 changes: 38 additions & 0 deletions src/HLL/SysConfig.nqp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class HLL::SysConfig {
has %!build-config;
has $!nqp-home;
has $!path-sep;

method BUILD() {
%!build-config := nqp::hash();
hll-config(%!build-config);

$!path-sep := nqp::backendconfig<osname> eq 'MSWin32' ?? '\\' !! '/';

# Determine NQP home
#?if jvm
# TODO could be replaced by nqp::execname() after the next bootstrap for JVM
my $execname := nqp::atkey(nqp::jvmgetproperties,'nqp.execname') // '';
#?endif
#?if !jvm
my $execname := nqp::execname;
#?endif
my $install-dir := $execname eq ''
?? %!build-config<prefix>
!! nqp::substr($execname, 0, nqp::rindex($execname, $!path-sep, nqp::rindex($execname, $!path-sep) - 1));

$!nqp-home := nqp::getenvhash<NQP_HOME>
// %!build-config<static-nqp-home>
|| $install-dir ~ '/share/nqp';
if nqp::substr($!nqp-home, nqp::chars($!nqp-home) - 1) eq $!path-sep {
$!nqp-home := nqp::substr($!nqp-home, 0, nqp::chars($!nqp-home) - 1);
}
}

method path-sep() { $!path-sep }

method nqp-build-config() { %!build-config }

method nqp-home() { $!nqp-home }
}

1 change: 0 additions & 1 deletion src/NQP/Compiler.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ my $nqpcomp := NQP::Compiler.new();
$nqpcomp.language('nqp');
$nqpcomp.parsegrammar(NQP::Grammar);
$nqpcomp.parseactions(NQP::Actions);
hll-config($nqpcomp.config);

$nqpcomp.addstage('optimize', :after<ast>);

Expand Down
2 changes: 1 addition & 1 deletion src/vm/moar/HLL/Backend.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ class HLL::Backend::MoarVM {
my str $template;

if !$want_json && !$want_sql {
my $temppath := nqp::getcomp('default').nqp-home() ~ '/lib/profiler/template.html';
my $temppath := nqp::gethllsym('default', 'SysConfig').nqp-home() ~ '/lib/profiler/template.html';
$template := try slurp('src/vm/moar/profiler/template.html');
unless $template {
$template := try slurp($temppath);
Expand Down
10 changes: 7 additions & 3 deletions tools/templates/Makefile-backend-common.in
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
@stage_gencat(@bpm(QASTNODE_COMBINED_@ucstage@)@: @bsm(@ucstage@_DEPS)@ @use_prereqs($(QASTNODE_SOURCES))@)@
@stage_gencat(@bpm(ASTNODES_COMBINED_@ucstage@)@: @bsm(@ucstage@_DEPS)@ @use_prereqs(@bpm(ASTNODES_SOURCES)@)@)@
@stage_gencat(@bpm(QREGEX_COMBINED_@ucstage@)@: @bsm(@ucstage@_DEPS)@ @use_prereqs($(QREGEX_SOURCES))@)@
@stage_gencat(@bpm(HLL_COMBINED_@ucstage@)@: @bsm(@ucstage@_DEPS)@ @use_prereqs(@bpm(HLL_SOURCES)@)@)@
@stage_gencat(@bpm(HLL_COMBINED_@ucstage@)@: @bsm(@ucstage@_DEPS)@ @use_prereqs(@bpm(HLL_SOURCES)@ @nfp(@stage_dir@/nqp-config.nqp)@)@)@
@stage_gencat(@bpm(QAST_COMBINED_@ucstage@)@: @bsm(@ucstage@_DEPS)@ @use_prereqs(@bpm(QAST_SOURCES)@)@)@
@stage_gencat(@bpm(P6QREGEX_COMBINED_@ucstage@)@: @bsm(@ucstage@_DEPS)@ @use_prereqs($(P6QREGEX_SOURCES))@)@

Expand All @@ -117,10 +117,14 @@
@stage_precomp(@bsm(QAST_@ucstage@)@: @use_prereqs(@bpm(QAST_COMBINED_@ucstage@)@)@ @bsm(@ucstage@_DEPS)@ @bsm(HLL_@ucstage@)@ @bsm(ASTNODES_@ucstage@)@ @bsm(ASTOPS_@ucstage@)@ @bsm(QREGEX_@ucstage@)@ @bsm(QASTNODE_@ucstage@)@)@
@stage_precomp(@bsm(P6QREGEX_@ucstage@)@: @use_prereqs(@bpm(P6QREGEX_COMBINED_@ucstage@)@)@ @bsm(@ucstage@_DEPS)@ @bsm(HLL_@ucstage@)@ @bsm(QREGEX_@ucstage@)@ @bsm(QAST_@ucstage@)@ @bsm(QASTNODE_@ucstage@)@)@


@nfp(@stage_dir@/nqp-config.nqp)@:
@echo(+++ Generating stage @stage@ nqp-config.nqp)@
$(NOECHO)$(PERL5) @shquot(@script(gen-version.pl)@)@ @q($(PREFIX))@ @q($(STATIC_NQP_HOME))@ @q($(NQP_LIB_DIR))@ > @nfpq(@stage_dir@/nqp-config.nqp)@

@nfp(@stage_dir@/@bsm(NQP)@)@: @bsm(NQP_@ucprev_stage@)@ @nfp(@stage_dir@/@bsm(QAST)@)@ @nfp(@stage_dir@/@bsm(P6QREGEX)@)@ @bpm(SOURCES)@
@echo(+++ Creating stage @stage@ NQP)@
$(NOECHO)$(PERL5) @shquot(@script(gen-version.pl)@)@ @q($(PREFIX))@ @q($(STATIC_NQP_HOME))@ @q($(NQP_LIB_DIR))@ > @nfpq(@stage_dir@/nqp-config.nqp)@
$(NOECHO@nop())@@bpm(@ucstage@_GEN_CAT)@ @bpm(NQP_SOURCES)@ @nfpq(@stage_dir@/nqp-config.nqp)@ > @nfpq(@stage_dir@/$(NQP_COMBINED))@
$(NOECHO@nop())@@bpm(@ucstage@_GEN_CAT)@ @bpm(NQP_SOURCES)@ > @nfpq(@stage_dir@/$(NQP_COMBINED))@
$(NOECHO@nop())@@bpm(@ucprev_stage@_NQP)@ --module-path=@shquot(@stage_dir@)@ --setting-path=@shquot(@stage_dir@)@ \
--setting=NQPCORE --target=@btarget@ --no-regex-lib @bpm(PRECOMP_@ucstage@_FLAGS)@ @bpm(NQP_@ucstage@_FLAGS)@ \
--output=@nfpq(@stage_dir@/@bsm(NQP)@)@ @nfpq(@stage_dir@/$(NQP_COMBINED))@
Expand Down
1 change: 1 addition & 0 deletions tools/templates/Makefile-common.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ COMMON_HLL_SOURCES = \
@nfp(src/HLL/Grammar.nqp)@ \
@nfp(src/HLL/Actions.nqp)@ \
@nfp(src/HLL/Compiler.nqp)@ \
@nfp(src/HLL/SysConfig.nqp)@ \
@nfp(src/HLL/CommandLine.nqp)@ \
@nfp(src/HLL/World.nqp)@ \
@nfp(src/HLL/sprintf.nqp)@ \
Expand Down

0 comments on commit d2a35ac

Please sign in to comment.