| 1 | #!/bin/sh |
| 2 | |
| 3 | set -eu |
| 4 | |
| 5 | if [ -n "${DEB_HOST_GNU_TYPE:-}" ]; then |
| 6 | CROSS_COMPILE="$DEB_HOST_GNU_TYPE-gcc" |
| 7 | else |
| 8 | CROSS_COMPILE="cc" |
| 9 | fi |
| 10 | |
| 11 | cat <<EOF > simple_test_compile.c |
| 12 | #include <popt.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | int main(int argc, const char **argv) { |
| 17 | |
| 18 | int test = 0; |
| 19 | int flag = 0; |
| 20 | const char *string = NULL; |
| 21 | const char **files; |
| 22 | int arg; |
| 23 | |
| 24 | const struct poptOption options[] = { |
| 25 | { "test", 't', POPT_ARG_NONE, NULL, 't', "Help message for test", NULL }, |
| 26 | { "flag", 'f', POPT_ARG_NONE, &flag, 0, "Help message for flag", NULL }, |
| 27 | { "string", 's', POPT_ARG_STRING, &string, 0, "Help message for string", "command"}, |
| 28 | POPT_AUTOHELP { NULL, 0, 0, NULL, 0, NULL, NULL } |
| 29 | }; |
| 30 | |
| 31 | poptContext optCon = poptGetContext("simple-test-compile", argc, argv, options, 0); |
| 32 | |
| 33 | poptSetOtherOptionHelp(optCon, "[OPTION...] <file>"); |
| 34 | |
| 35 | while ((arg = poptGetNextOpt(optCon)) >= 0) { |
| 36 | switch (arg) { |
| 37 | case 't': |
| 38 | test = 1; |
| 39 | break; |
| 40 | default: |
| 41 | break; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | if (arg < -1) { |
| 46 | fprintf(stderr, "bad argument: %s\n", poptBadOption(optCon, POPT_BADOPTION_NOALIAS)); |
| 47 | poptFreeContext(optCon); |
| 48 | return EXIT_FAILURE; |
| 49 | } |
| 50 | |
| 51 | files = poptGetArgs(optCon); |
| 52 | if (!files) { |
| 53 | fprintf(stderr, "no arguments\n"); |
| 54 | poptPrintUsage(optCon, stderr, 0); |
| 55 | poptFreeContext(optCon); |
| 56 | return EXIT_FAILURE; |
| 57 | } |
| 58 | |
| 59 | if (!test) { |
| 60 | fprintf(stderr, "test not set\n"); |
| 61 | poptFreeContext(optCon); |
| 62 | return EXIT_FAILURE; |
| 63 | } |
| 64 | |
| 65 | if (!flag) { |
| 66 | fprintf(stderr, "flag not set\n"); |
| 67 | poptFreeContext(optCon); |
| 68 | return EXIT_FAILURE; |
| 69 | } |
| 70 | |
| 71 | if (!string || 0 != strcmp(string, "string")) { |
| 72 | fprintf(stderr, "string not set\n"); |
| 73 | poptFreeContext(optCon); |
| 74 | return EXIT_FAILURE; |
| 75 | } |
| 76 | |
| 77 | if (!files || 0 != strcmp(files[0], "file1") || 0 != strcmp(files[1], "file2") || files[2] != NULL) { |
| 78 | fprintf(stderr, "files not set\n"); |
| 79 | poptFreeContext(optCon); |
| 80 | return EXIT_FAILURE; |
| 81 | } |
| 82 | |
| 83 | poptFreeContext(optCon); |
| 84 | |
| 85 | printf("success\n"); |
| 86 | |
| 87 | return EXIT_SUCCESS; |
| 88 | } |
| 89 | EOF |
| 90 | |
| 91 | ${CROSS_COMPILE} -Wall -Wextra -Werror -O2 simple_test_compile.c -o simple_test_compile_runner -lpopt |
| 92 | ./simple_test_compile_runner -t -f --string string file1 file2 |