xref: /llvm-project/clang/utils/bundle_resources.py (revision dd3c26a045c081620375a878159f536758baba6e)
1a443b3d1SSam McCall#!/usr/bin/env python3
2a443b3d1SSam McCall
3a443b3d1SSam McCall# ===- bundle_resources.py - Generate string constants with file contents. ===
4a443b3d1SSam McCall#
5a443b3d1SSam McCall# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6a443b3d1SSam McCall# See https://llvm.org/LICENSE.txt for license information.
7a443b3d1SSam McCall# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8a443b3d1SSam McCall#
9a443b3d1SSam McCall# ===----------------------------------------------------------------------===
10a443b3d1SSam McCall
11a443b3d1SSam McCall# Usage: bundle-resources.py foo.inc a.js path/b.css ...
12a443b3d1SSam McCall# Produces foo.inc containing:
13a443b3d1SSam McCall#   const char a_js[] = "...";
14a443b3d1SSam McCall#   const char b_css[] = "...";
15a443b3d1SSam McCallimport os
16a443b3d1SSam McCallimport sys
17a443b3d1SSam McCall
18a443b3d1SSam McCalloutfile = sys.argv[1]
19a443b3d1SSam McCallinfiles = sys.argv[2:]
20a443b3d1SSam McCall
21*dd3c26a0STobias Hietawith open(outfile, "w") as out:
22a443b3d1SSam McCall    for filename in infiles:
23*dd3c26a0STobias Hieta        varname = os.path.basename(filename).replace(".", "_")
24*dd3c26a0STobias Hieta        out.write("const char " + varname + "[] = \n")
25a443b3d1SSam McCall        # MSVC limits each chunk of string to 2k, so split by lines.
26a443b3d1SSam McCall        # The overall limit is 64k, which ought to be enough for anyone.
27*dd3c26a0STobias Hieta        for line in open(filename).read().split("\n"):
28a443b3d1SSam McCall            out.write('  R"x(' + line + ')x" "\\n"\n')
29*dd3c26a0STobias Hieta        out.write("  ;\n")
30