mirror of
https://github.com/mkxp-z/mkxp-z.git
synced 2025-08-24 15:53:45 +02:00

This removes the need to have xxd installed and provides a portable way to specify the name of the output array (xxd has an `-n` option for this, but it isn't present in older versions of xxd), which helps reduce the possibility of symbol conflicts in libretro builds and also prevents portability issues since the name of xxd's output array depends on the relative path to the input file, which can break if Meson changes the structure of the build directory or if the user sets the build directory to a different location. Also, this custom executable declares the array as const so that it goes into the read-only data section of the binary instead of the data section.
59 lines
1.5 KiB
C++
59 lines
1.5 KiB
C++
/*
|
|
** embedtool.cpp
|
|
**
|
|
** This file is part of mkxp.
|
|
**
|
|
** Copyright (C) 2013 - 2021 Amaryllis Kulla <ancurio@mapleshrine.eu>
|
|
**
|
|
** mkxp is free software: you can redistribute it and/or modify
|
|
** it under the terms of the GNU General Public License as published by
|
|
** the Free Software Foundation, either version 2 of the License, or
|
|
** (at your option) any later version.
|
|
**
|
|
** mkxp is distributed in the hope that it will be useful,
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
** GNU General Public License for more details.
|
|
**
|
|
** You should have received a copy of the GNU General Public License
|
|
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <cstdint>
|
|
#include <fstream>
|
|
|
|
int main(int argc, char **argv) {
|
|
if (argc < 4) {
|
|
return 1;
|
|
}
|
|
|
|
const char *input = argv[1];
|
|
const char *output = argv[2];
|
|
const char *arrayname = argv[3];
|
|
|
|
std::ifstream inputf(input);
|
|
if (!inputf.is_open()) {
|
|
return 2;
|
|
}
|
|
|
|
std::ofstream outputf(output);
|
|
if (!outputf.is_open()) {
|
|
return 3;
|
|
}
|
|
|
|
outputf << "#include <stddef.h>\n#include <stdint.h>\nconst uint8_t " << arrayname << "[] = {";
|
|
|
|
uint64_t len = 0;
|
|
for (;;) {
|
|
unsigned char c = inputf.get();
|
|
if (inputf.eof()) {
|
|
break;
|
|
}
|
|
outputf << (unsigned int)c << ',';
|
|
++len;
|
|
}
|
|
|
|
outputf << "};\nconst size_t " << arrayname << "_len = " << len << "ULL;";
|
|
|
|
return 0;
|
|
}
|