compiler: add "C::nameinfix" annotation
allows changing output filenames from "foo.capnp.c" to "foo.capnpSOMETHING.c" where SOMETHING is the argument to the annotation.
This commit is contained in:
parent
87591da455
commit
5d787b698e
4 changed files with 71 additions and 3 deletions
32
compiler/c.capnp
Normal file
32
compiler/c.capnp
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
# Copyright (c) 2016 NetDEF, Inc. and contributors
|
||||
# Licensed under the MIT License:
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
@0xc0183dd65ffef0f3;
|
||||
|
||||
# add an infix (middle insert) for output file names
|
||||
#
|
||||
# "make" generally has implicit rules for compiling "foo.c" => "foo". This
|
||||
# is very annoying with capnp since the rule will be "foo" => "foo.c", leading
|
||||
# to a loop. $nameinfix (recommended parameter: "-gen") inserts its parameter
|
||||
# before the ".c", so the filename becomes "foo-gen.c"
|
||||
#
|
||||
# ("foo" is really "foo.capnp", so it's foo.capnp-gen.c)
|
||||
annotation nameinfix @0x85a8d86d736ba637 (file): Text;
|
||||
|
|
@ -1243,6 +1243,7 @@ int main() {
|
|||
struct CodeGeneratorRequest_RequestedFile file_req;
|
||||
static struct str b = STR_INIT;
|
||||
char *p;
|
||||
const char *nameinfix = NULL;
|
||||
FILE *srcf, *hdrf;
|
||||
|
||||
g_valc = 0;
|
||||
|
|
@ -1259,6 +1260,29 @@ int main() {
|
|||
exit(2);
|
||||
}
|
||||
|
||||
for (j = capn_len(file_node->n.annotations)-1; j >= 0; j--) {
|
||||
struct Annotation a;
|
||||
struct Value v;
|
||||
get_Annotation(&a, file_node->n.annotations, j);
|
||||
read_Value(&v, a.value);
|
||||
|
||||
switch (a.id) {
|
||||
case 0x85a8d86d736ba637UL: /* $C::nameinfix */
|
||||
if (v.which != Value_text) {
|
||||
fprintf(stderr, "schema breakage on $C::nameinfix annotation\n");
|
||||
exit(2);
|
||||
}
|
||||
if (nameinfix) {
|
||||
fprintf(stderr, "$C::nameinfix annotation appears more than once\n");
|
||||
exit(2);
|
||||
}
|
||||
nameinfix = v.text.str ? v.text.str : "";
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!nameinfix)
|
||||
nameinfix = "";
|
||||
|
||||
str_reset(&HDR);
|
||||
str_reset(&SRC);
|
||||
|
||||
|
|
@ -1311,7 +1335,7 @@ int main() {
|
|||
|
||||
/* write out the header */
|
||||
|
||||
hdrf = fopen(strf(&b, "%s.h", file_node->n.displayName.str), "w");
|
||||
hdrf = fopen(strf(&b, "%s%s.h", file_node->n.displayName.str, nameinfix), "w");
|
||||
if (!hdrf) {
|
||||
fprintf(stderr, "failed to open %s: %s\n", b.str, strerror(errno));
|
||||
exit(2);
|
||||
|
|
@ -1321,13 +1345,13 @@ int main() {
|
|||
|
||||
/* write out the source */
|
||||
|
||||
srcf = fopen(strf(&b, "%s.c", file_node->n.displayName.str), "w");
|
||||
srcf = fopen(strf(&b, "%s%s.c", file_node->n.displayName.str, nameinfix), "w");
|
||||
if (!srcf) {
|
||||
fprintf(stderr, "failed to open %s: %s\n", b.str, strerror(errno));
|
||||
exit(2);
|
||||
}
|
||||
p = strrchr(file_node->n.displayName.str, '/');
|
||||
fprintf(srcf, "#include \"%s.h\"\n", p ? p+1 : file_node->n.displayName.str);
|
||||
fprintf(srcf, "#include \"%s%s.h\"\n", p ? p+1 : file_node->n.displayName.str, nameinfix);
|
||||
fprintf(srcf, "/* AUTO GENERATED - DO NOT EDIT */\n");
|
||||
|
||||
if (g_val0used)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue