1 /*
2 * Copyright (c) 2020, Rockchip Electronics Co., Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14 #define _GNU_SOURCE
15
16 #include "common.h"
17 #include <time.h>
18 #include ".j2s_generated.h"
19
main(int argc,char ** argv)20 int main(int argc, char** argv) {
21
22 FILE* fp = NULL;
23 int magic = 0;
24 j2s_struct *struct_obj = NULL;
25
26 DASSERT_MSG(argc > 1, return -1,
27 "usage: %s <j2s_generated.h> \n", argv[0]);
28
29 INFO("optimize j2s_generated.h to file: %s\n", argv[1]);
30
31 j2s_ctx ctx_rel;
32 j2s_ctx *ctx = &ctx_rel;
33 _j2s_init(ctx);
34
35 fp = fopen(argv[1],"w+");
36 DASSERT_MSG(fp, return -1, "failed to open %s\n", argv[1]);
37
38 fprintf(fp, "#include <stdint.h>\n");
39 fprintf(fp, "#include \"j2s.h\"\n\n");
40 fprintf(fp, "#include \"j2s_code2bin.h\"\n\n");
41
42 fprintf(fp, "#define J2S_MAGIC %d\n", ctx->magic);
43 fprintf(fp, "#define J2S_NUM_OBJ %d\n", ctx->num_obj);
44 fprintf(fp, "#define J2S_NUM_STRUCT %d\n", ctx->num_struct);
45 fprintf(fp, "#define J2S_NUM_ENUM %d\n", ctx->num_enum);
46 fprintf(fp, "#define J2S_NUM_ENUM_VALUE %d\n\n", ctx->num_enum_value);
47 fprintf(fp, "static void _j2s_init(j2s_ctx *ctx) {\n");
48 fprintf(fp, "\tsize_t objs_len = 0;\n");
49 fprintf(fp, "\tsize_t structs_len = 0;\n");
50 fprintf(fp, "\tsize_t enums_len = 0;\n\n");
51
52 fprintf(fp, "\tctx->magic = J2S_MAGIC;\n");
53 fprintf(fp, "\tctx->priv = NULL;\n");
54
55 fprintf(fp, "\tctx->objs = (j2s_obj *)(&j2s_code2bin_bin[0]);\n");
56 fprintf(fp, "\tobjs_len = sizeof(j2s_obj) * J2S_NUM_OBJ;\n");
57 fprintf(fp, "\tctx->structs = (j2s_struct *)(&j2s_code2bin_bin[0] + objs_len);\n");
58 fprintf(fp, "\tstructs_len = sizeof(j2s_struct) * J2S_NUM_STRUCT;\n");
59 fprintf(fp, "\tctx->enums = (j2s_enum *)(&j2s_code2bin_bin[0] + objs_len + structs_len);\n");
60 fprintf(fp, "\tenums_len = sizeof(j2s_enum) * J2S_NUM_ENUM;\n");
61 fprintf(fp, "\tctx->enum_values = (j2s_enum_value *)(&j2s_code2bin_bin[0] + objs_len + structs_len + enums_len);\n\n");
62
63 fprintf(fp, "\tctx->num_obj = J2S_NUM_OBJ;\n");
64 fprintf(fp, "\tctx->num_struct = J2S_NUM_STRUCT;\n");
65 fprintf(fp, "\tctx->num_enum = J2S_NUM_ENUM;\n");
66 fprintf(fp, "\tctx->num_enum_value = J2S_NUM_ENUM_VALUE;\n\n");
67
68 fprintf(fp, "\tctx->num_desc = 0;\n");
69
70 fprintf(fp, "\tctx->root_index = %d;\n", ctx->root_index);
71 fprintf(fp, "}\n\n");
72
73 fclose(fp);
74
75 return 0;
76 }
77