From d2a35ac5ac3a3c8a5d5f80bd0ed8e2330d660559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20B=C3=B6ker?= Date: Fri, 31 Jul 2020 18:06:21 +0200 Subject: [PATCH] Add a HLL::SysConfig class 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. --- src/HLL/Compiler.nqp | 40 ++++------------------ src/HLL/SysConfig.nqp | 38 ++++++++++++++++++++ src/NQP/Compiler.nqp | 1 - src/vm/moar/HLL/Backend.nqp | 2 +- tools/templates/Makefile-backend-common.in | 10 ++++-- tools/templates/Makefile-common.in | 1 + 6 files changed, 53 insertions(+), 39 deletions(-) create mode 100644 src/HLL/SysConfig.nqp diff --git a/src/HLL/Compiler.nqp b/src/HLL/Compiler.nqp index 510211eabf..e680684753 100644 --- a/src/HLL/Compiler.nqp +++ b/src/HLL/Compiler.nqp @@ -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. @@ -32,7 +30,6 @@ class HLL::Compiler does HLL::Backend::Default { ~ ' confprog=s' #?endif ); - %!config := nqp::hash(); } method backend(*@value) { @@ -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) @@ -600,7 +597,7 @@ class HLL::Compiler does HLL::Backend::Default { } method version() { - my $version := %!config; + my $version := self.sysconfig(); my $backver := $!backend.version_string(); my $implementation := self.implementation(); my $language_name := self.language_name(); @@ -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); } @@ -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 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 - !! nqp::substr($execname, 0, nqp::rindex($execname, $sep, nqp::rindex($execname, $sep) - 1)); - - $!nqp-home := nqp::getenvhash() - // self.config - || $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()); diff --git a/src/HLL/SysConfig.nqp b/src/HLL/SysConfig.nqp new file mode 100644 index 0000000000..aa186262c7 --- /dev/null +++ b/src/HLL/SysConfig.nqp @@ -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 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 + !! nqp::substr($execname, 0, nqp::rindex($execname, $!path-sep, nqp::rindex($execname, $!path-sep) - 1)); + + $!nqp-home := nqp::getenvhash + // %!build-config + || $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 } +} + diff --git a/src/NQP/Compiler.nqp b/src/NQP/Compiler.nqp index 61beaad689..b1fc96185f 100644 --- a/src/NQP/Compiler.nqp +++ b/src/NQP/Compiler.nqp @@ -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); diff --git a/src/vm/moar/HLL/Backend.nqp b/src/vm/moar/HLL/Backend.nqp index 4de37930c7..5705125f6a 100644 --- a/src/vm/moar/HLL/Backend.nqp +++ b/src/vm/moar/HLL/Backend.nqp @@ -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); diff --git a/tools/templates/Makefile-backend-common.in b/tools/templates/Makefile-backend-common.in index 1c46e2a476..085ad3c644 100644 --- a/tools/templates/Makefile-backend-common.in +++ b/tools/templates/Makefile-backend-common.in @@ -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))@)@ @@ -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))@ diff --git a/tools/templates/Makefile-common.in b/tools/templates/Makefile-common.in index c7523b1486..93fbb5e8e3 100644 --- a/tools/templates/Makefile-common.in +++ b/tools/templates/Makefile-common.in @@ -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)@ \