Include text file as constant char array.

94 views
Skip to first unread message

Erik Jensen

unread,
Aug 28, 2019, 9:05:42 PM8/28/19
to Chromium-dev
I have a chunk of text (specifically of GtkBuilder XML for a Chrome Remote Desktop Linux-only helper UI) that I'd like to include as a const char[] in my binary. For ease of editing, though, I'd prefer to have the XML live in it's own file that gets pulled in at build time. My thought was that I'd have a header file declare the constant, and a build step to generate a .cc file with the definition from the text file. However, I'm not sure what the best way is to accomplish this with GN. Is there already functionality in Chromium to do something like this, or would I have to make a completely custom build step to generate a properly-escaped string literal (maybe using xxd)?

Thanks!

Nico Weber

unread,
Aug 28, 2019, 10:02:06 PM8/28/19
to rkj...@chromium.org, Chromium-dev
There's no great way to do this. https://bugs.chromium.org/p/v8/issues/detail?id=8475 lists several not-so-great ways (in the context of wanting to include machine code, but much of it applies to including general binary files).

The cleanest way is probably to include the file as a resource in a grd file if that's an option.

If the file doesn't change often, you could check in the output of bin2inc.

On Wed, Aug 28, 2019 at 9:04 PM Erik Jensen <rkj...@chromium.org> wrote:
I have a chunk of text (specifically of GtkBuilder XML for a Chrome Remote Desktop Linux-only helper UI) that I'd like to include as a const char[] in my binary. For ease of editing, though, I'd prefer to have the XML live in it's own file that gets pulled in at build time. My thought was that I'd have a header file declare the constant, and a build step to generate a .cc file with the definition from the text file. However, I'm not sure what the best way is to accomplish this with GN. Is there already functionality in Chromium to do something like this, or would I have to make a completely custom build step to generate a properly-escaped string literal (maybe using xxd)?

Thanks!

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CAN%3DK5G-qB%3D97rTFncviC2dfrO%3DbW55EDHxWw1qD_BG7ffKC_Aw%40mail.gmail.com.

Ryan Gonzalez

unread,
Aug 28, 2019, 11:37:58 PM8/28/19
to rkj...@chromium.org, Chromium-dev
Disclaimer: I'm not actually affiliated with the Chromium project, so anything I say here may be disallowed...

Since this is GtkBuilder XML, maybe you could use the built in GLib resources system? IIRC there are built-in functions to load Builder XML definitions and/or templates from resources.

Failing that, I've personally found raw strings to drastically help with the escaping issue. You can use some insane delimiter (e.g. R"=+EMBEDDED_STRING+=(...), and if you need to handle the delimiter being in the file, you can use a string replace that replaces any occurrence of the delimiter with `)delimiter""delimiter"R"delimiter(` (so it just closes the raw string, puts the delimiter in a normal string, then re-opens the raw one.


On Wed, Aug 28, 2019, 8:05 PM Erik Jensen <rkj...@chromium.org> wrote:
I have a chunk of text (specifically of GtkBuilder XML for a Chrome Remote Desktop Linux-only helper UI) that I'd like to include as a const char[] in my binary. For ease of editing, though, I'd prefer to have the XML live in it's own file that gets pulled in at build time. My thought was that I'd have a header file declare the constant, and a build step to generate a .cc file with the definition from the text file. However, I'm not sure what the best way is to accomplish this with GN. Is there already functionality in Chromium to do something like this, or would I have to make a completely custom build step to generate a properly-escaped string literal (maybe using xxd)?

Thanks!

Reid Kleckner

unread,
Aug 29, 2019, 2:53:01 PM8/29/19
to Nico Weber, rkj...@chromium.org, Chromium-dev
My suggestion would be to use .incbin, although it is mentioned in the list of not-so-great ways to do this. :)

You could use module level asm to avoid tangling with the build system if you like, like so:

#include <io.h>
extern char foo_start[];
extern char foo_end[];
asm(".section .rdata,\"dr\"\n"
    ".global foo_start\n"
    ".global foo_end\n"
    "foo_start:\n"
    ".incbin \"mydata.txt\"\n"
    "foo_end:\n");
int main() {
  size_t size = &foo_end[0] - &foo_start[0];
  write(1, &foo_start[0], size);
}

The assembler seems to use clang's include paths to resolve .incbin paths, so you can use a src-relative path in chromium.

The section directive will be platform dependent, but the other directives should be target independent.

Reid Kleckner

unread,
Aug 29, 2019, 2:54:38 PM8/29/19
to Nico Weber, rkj...@chromium.org, Chromium-dev
One major concern I have about incbin is whether it shows up in clang's -MD output. I suspect it does not. Maybe that's a bug we should fix. I'm not sure how you'd artificially express the dependency without that.

Dirk Pranke

unread,
Aug 29, 2019, 4:15:42 PM8/29/19
to Reid Kleckner, Nico Weber, rkj...@chromium.org, Chromium-dev
I would worry that the .incbin approach would bypass `gn analyze`. It seems straightforward enough to me to generate a source file from the XML file and include that as a target?

-- Dirk

Reply all
Reply to author
Forward
0 new messages