Skip to content

Commit

Permalink
[translation] Embed _devbuild/help in the C++ binary
Browse files Browse the repository at this point in the history
This makes some more spec tests pass

Also use const char* for the filename
  • Loading branch information
Andy C committed Aug 10, 2023
1 parent e5bce14 commit 14bcde8
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 22 deletions.
2 changes: 1 addition & 1 deletion bin/NINJA_subgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def NinjaGraph(ru):
# I wish Ninja had DIRECTORY-level dependencies? Because this should
# ultimately depend on doc/ref/*.md
# We could probably create a _build/ninja-stamp/HELP file and so forth
files = glob.glob('_devbuild/gen/help/*')
files = glob.glob('_devbuild/help/*')

n.build(['_gen/bin/text_files.cc'], 'embedded-file-gen', files,
implicit=['_bin/shwrap/embedded_file_gen'])
Expand Down
4 changes: 3 additions & 1 deletion core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ def HelpFlag(loader, topic_id, f):
# type: (pyutil._ResourceLoader, str, mylib.Writer) -> None
_PrintVersionLine(loader, f)
f.write('\n')
assert PrintEmbeddedHelp(loader, topic_id, f)
found = PrintEmbeddedHelp(loader, topic_id, f)
# Note: could assert this in C++ too
assert found, 'Missing %s' % topic_id


def VersionFlag(loader, f):
Expand Down
2 changes: 1 addition & 1 deletion cpp/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ Str* ChArrayToString(List<int>* ch_array) {
Str* _ResourceLoader::Get(Str* path) {
TextFile* t = gEmbeddedFiles; // start of generated data
while (t->rel_path != nullptr) {
if (str_equals(t->rel_path, path)) {
if (strcmp(t->rel_path, path->data_) == 0) {
return t->contents;
}
t++;
Expand Down
14 changes: 6 additions & 8 deletions cpp/core_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@ TEST for_test_coverage() {
PASS();
}

GLOBAL_STR(k1, "k1");
GLOBAL_STR(v1, "v1");
GLOBAL_STR(k2, "k2");
GLOBAL_STR(v2, "v2");

TextFile tmp[] = {
{.rel_path = k1, .contents = v1},
{.rel_path = k2, .contents = v2},
TextFile gTmp[] = {
{.rel_path = "k1", .contents = v1},
{.rel_path = "k2", .contents = v2},
{.rel_path = nullptr, .contents = nullptr},
};

TextFile* gEmbeddedFiles = tmp; // array to pointer
TextFile* gEmbeddedFiles = gTmp; // turn array into pointer

TEST loader_test() {
auto loader = pyutil::GetResourceLoader();
Expand All @@ -39,8 +37,8 @@ TEST loader_test() {

pyutil::PrintVersionDetails(loader);

ASSERT_EQ(v1, loader->Get(k1));
ASSERT_EQ(v2, loader->Get(k2));
ASSERT_EQ(v1, loader->Get(StrFromC("k1")));
ASSERT_EQ(v2, loader->Get(StrFromC("k2")));

bool caught = false;
try {
Expand Down
4 changes: 2 additions & 2 deletions cpp/embedded_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include "mycpp/gc_str.h"

struct TextFile {
Str* rel_path;
Str* contents;
const char* rel_path;
Str* contents; // GC string to avoid copying on return
};

// Linear search; last one has nullptr entries
Expand Down
39 changes: 32 additions & 7 deletions cpp/embedded_file_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,50 @@
- _devbuild/help/
- stdlib/
"""

import sys
from mycpp.mylib import log


def main(argv):
#action = argv[1]
paths = argv[1:]

f = sys.stdout
out_f = sys.stdout

# Invoked with _devbuild/help/* stdlib/*.ysh

# Invoke with _devbuild/help/* stdlib/*.ysh I guess?
log('paths %s', paths)

out_f.write('''
#include "cpp/embedded_file.h"
for rel_path in paths:
namespace embedded_file {
''')

# Write global strings
for i, rel_path in enumerate(paths):
with open(rel_path) as f:
contents = f.read()

f.write('''
#include "cpp/embedded_file.h"
# zZXx is a unique string that shouldn't appear in any file
out_f.write('GLOBAL_STR(gStr%d, R"zZXx(%s)zZXx");\n\n' % (i, contents))

out_f.write('''
TextFile array[] = {
''')

# Write global array entries
for i, rel_path in enumerate(paths):
out_f.write(' {.rel_path = "%s", .contents = gStr%d},\n' % (rel_path, i))

out_f.write('''
{.rel_path = nullptr, .contents = nullptr},
};
} // namespace embedded_file
TextFile* gEmbeddedFiles = nullptr;
TextFile* gEmbeddedFiles = embedded_file::array; // turn array into pointer
''')


Expand Down
6 changes: 4 additions & 2 deletions osh/builtin_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,10 +872,12 @@ def Run(self, cmd_val):

topic_id, blame_loc = arg_r.Peek2()
if topic_id is None:
assert self._ShowTopic('help', blame_loc) == 0
found = self._ShowTopic('help', blame_loc) == 0
assert found

# e.g. ysh-chapters
assert self._ShowTopic('%s-chapters' % self.lang, blame_loc) == 0
found = self._ShowTopic('%s-chapters' % self.lang, blame_loc) == 0
assert found

print('All docs: https://www.oilshell.org/release/%s/doc/' %
self.version_str)
Expand Down

0 comments on commit 14bcde8

Please sign in to comment.