summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-02-04 00:49:14 +0800
committerruki <[email protected]>2022-02-04 00:49:14 +0800
commit18d91188e78e351814b2bdf03cb5c8c005cd40ff (patch)
tree21cfee16e2b039f92ef0c4cbb02885e16d7f8a2c
parentbfac9c2bbd90824765c3ef3da2d3ebd0ce414d46 (diff)
add asn1c rule
-rw-r--r--tests/projects/c/asn1c/src/main.c73
-rw-r--r--tests/projects/c/asn1c/src/rectangle.asn17
-rw-r--r--tests/projects/c/asn1c/xmake.lua9
-rw-r--r--xmake/modules/detect/tools/find_asn1c.lua54
-rw-r--r--xmake/rules/asn1c/xmake.lua48
5 files changed, 191 insertions, 0 deletions
diff --git a/tests/projects/c/asn1c/src/main.c b/tests/projects/c/asn1c/src/main.c
new file mode 100644
index 000000000..cb396a543
--- /dev/null
+++ b/tests/projects/c/asn1c/src/main.c
@@ -0,0 +1,73 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <Rectangle.h> /* Rectangle ASN.1 type */
+
+/*
+ * This is a custom function which writes the
+ * encoded output into some FILE stream.
+ */
+static int
+write_out(const void *buffer, size_t size, void *app_key) {
+ FILE *out_fp = app_key;
+ size_t wrote;
+
+ wrote = fwrite(buffer, 1, size, out_fp);
+
+ return (wrote == size) ? 0 : -1;
+}
+
+int main(int ac, char **av) {
+ Rectangle_t *rectangle; /* Type to encode */
+ asn_enc_rval_t ec; /* Encoder return value */
+
+ /* Allocate the Rectangle_t */
+ rectangle = calloc(1, sizeof(Rectangle_t)); /* not malloc! */
+ if(!rectangle) {
+ perror("calloc() failed");
+ exit(71); /* better, EX_OSERR */
+ }
+
+ /*
+ * Initialize the Rectangle members
+ */
+
+ /* height */
+ rectangle->height = 42;
+
+ /* width */
+ rectangle->width = 23;
+
+ /* BER encode the data if filename is given */
+ if(ac < 2) {
+ fprintf(stderr, "Specify filename for BER output\n");
+ } else {
+ const char *filename = av[1];
+ FILE *fp = fopen(filename, "wb"); /* for BER output */
+
+ if(!fp) {
+ perror(filename);
+ exit(71); /* better, EX_OSERR */
+ }
+
+ /* Encode the Rectangle type as BER (DER) */
+ ec = der_encode(&asn_DEF_Rectangle,
+ rectangle, write_out, fp);
+ fclose(fp);
+ if(ec.encoded == -1) {
+ fprintf(stderr,
+ "Could not encode Rectangle (at %s)\n",
+ ec.failed_type ? ec.failed_type->name : "unknown");
+ exit(65); /* better, EX_DATAERR */
+ } else {
+ fprintf(stderr, "Created %s with BER encoded Rectangle\n",
+ filename);
+ }
+ }
+
+ /* Also print the constructed Rectangle XER encoded (XML) */
+ xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);
+
+ return 0; /* Encoding finished successfully */
+}
+
+
diff --git a/tests/projects/c/asn1c/src/rectangle.asn1 b/tests/projects/c/asn1c/src/rectangle.asn1
new file mode 100644
index 000000000..30646dd13
--- /dev/null
+++ b/tests/projects/c/asn1c/src/rectangle.asn1
@@ -0,0 +1,7 @@
+RectangleModule1 DEFINITIONS ::=
+BEGIN
+Rectangle ::= SEQUENCE {
+ height INTEGER,
+ width INTEGER
+}
+END
diff --git a/tests/projects/c/asn1c/xmake.lua b/tests/projects/c/asn1c/xmake.lua
new file mode 100644
index 000000000..d8bca2020
--- /dev/null
+++ b/tests/projects/c/asn1c/xmake.lua
@@ -0,0 +1,9 @@
+add_rules("mode.debug", "mode.release")
+add_requires("asn1c")
+
+target("test")
+ set_kind("binary")
+ add_files("src/*.c")
+ add_files("src/*.asn1")
+ add_rules("asn1c")
+ add_packages("asn1c")
diff --git a/xmake/modules/detect/tools/find_asn1c.lua b/xmake/modules/detect/tools/find_asn1c.lua
new file mode 100644
index 000000000..074485b94
--- /dev/null
+++ b/xmake/modules/detect/tools/find_asn1c.lua
@@ -0,0 +1,54 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file find_asn1c.lua
+--
+
+-- imports
+import("lib.detect.find_program")
+import("lib.detect.find_programver")
+
+-- find asn1c
+--
+-- @param opt the argument options, e.g. {version = true}
+--
+-- @return program, version
+--
+-- @code
+--
+-- local asn1c = find_asn1c()
+-- local asn1c, version = find_asn1c({version = true})
+--
+-- @endcode
+--
+function main(opt)
+
+ -- init options
+ opt = opt or {}
+ opt.check = opt.check or "-v"
+ opt.command = opt.command or "-v"
+
+ -- find program
+ local program = find_program(opt.program or "asn1c", opt)
+
+ -- find program version
+ local version = nil
+ if program and opt and opt.version then
+ version = find_programver(program, opt)
+ end
+ return program, version
+end
diff --git a/xmake/rules/asn1c/xmake.lua b/xmake/rules/asn1c/xmake.lua
new file mode 100644
index 000000000..7efbcd040
--- /dev/null
+++ b/xmake/rules/asn1c/xmake.lua
@@ -0,0 +1,48 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file xmake.lua
+--
+
+rule("asn1c")
+ set_extensions(".asn1")
+ before_buildcmd_file(function (target, batchcmds, sourcefile_asn1, opt)
+
+ -- get asn1c
+ import("lib.detect.find_tool")
+ local asn1c = assert(find_tool("asn1c"), "asn1c not found!")
+
+ -- asn1 to *.c sourcefiles
+ local sourcefile_dir = path.join(target:autogendir(), "rules", "asn1c")
+ batchcmds:show_progress(opt.progress, "${color.build.object}compiling.asn1c %s", sourcefile_asn1)
+ batchcmds:mkdir(sourcefile_dir)
+ batchcmds:vrunv(asn1c.program, {path.absolute(sourcefile_asn1)}, {curdir = sourcefile_dir})
+
+ -- compile *.c
+ for _, sourcefile in ipairs(os.files(path.join(sourcefile_dir, "*.c|converter-*.c"))) do
+ local objectfile = target:objectfile(sourcefile)
+ batchcmds:compile(sourcefile, objectfile, {configs = {includedirs = sourcefile_dir}})
+ table.insert(target:objectfiles(), objectfile)
+ end
+
+ -- add includedirs
+ target:add("includedirs", sourcefile_dir)
+
+ -- add deps
+ batchcmds:add_depfiles(sourcefile_asn1)
+ batchcmds:set_depcache(target:dependfile(sourcefile_asn1))
+ end)