1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 // LINT.IfChange
17 #define DLOPEN_FAILED 11
18 #define SYMBOL_LOOKUP_FAILED 12
19 #define TOO_FEW_ARGUMENTS 13
20 #define UNSUPPORTED_PLATFORM 14
21 // LINT.ThenChange(//tensorflow/lite/experimental/acceleration/mini_benchmark/status_codes.h)
22
23 #ifndef _WIN32
24 #include <dlfcn.h>
25 #include <stdio.h>
26
27 /**
28 * Mini-benchmark helper executable entry point.
29 *
30 * This is a minimal executable that loads the shared library that contains the
31 * actual logic as not to duplicate code between the mini-benchmark and
32 * applicaiton code that uses TFLite.
33 *
34 * The executable takes 2 or more arguments:
35 * executable path_to_shared_library symbol_to_call ...
36 * It loads the path_to_shared_library and calls symbol_to_call with all the
37 * arguments (using original argc and argv).
38 *
39 * Exit codes:
40 * - Happy path exits with the return value from symbol_to_call().
41 * - If library loading fails, exits with 1.
42 * - If symbol lookup fails, exits with 2.
43 * - If less than 2 arguments are passed, exits with 3.
44 */
main(int argc,char ** argv)45 int main(int argc, char** argv) {
46 if (argc < 3) {
47 return TOO_FEW_ARGUMENTS;
48 }
49 void* lib = dlopen(argv[1], RTLD_NOW | RTLD_LOCAL);
50 if (!lib) {
51 printf("dlopen failed for %s: %s\n", argv[1], dlerror());
52 return DLOPEN_FAILED;
53 }
54 void* sym = dlsym(lib, argv[2]);
55 if (!sym) {
56 printf("dlsym failed for %s on library %s with error: %s\n", argv[2],
57 argv[1], dlerror());
58 return SYMBOL_LOOKUP_FAILED;
59 }
60 int (*f)(int argc, char** argv) = (int (*)(int, char**))sym;
61 int exitcode = (*f)(argc, argv);
62 return exitcode;
63 }
64
65 #else // _WIN32
main(int argc,char ** argv)66 int main(int argc, char** argv) { return UNSUPPORTED_PLATFORM; }
67 #endif // !_WIN32
68