main.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <iostream>
  7. #include "pio_assembler.h"
  8. #define DEFAULT_OUTPUT_FORMAT "c-sdk"
  9. void usage() {
  10. std::cerr << "usage: pioasm <options> <input> (<output>)\n\n";
  11. std::cerr << "Assemble file of PIO program(s) for use in applications.\n";
  12. std::cerr << " <input> the input filename\n";
  13. std::cerr << " <output> the output filename (or filename prefix if the output format produces multiple outputs).\n";
  14. std::cerr << " if not specified, a single output will be written to stdout\n";
  15. std::cerr << "\n";
  16. std::cerr << "options:\n";
  17. std::cerr << " -o <output_format> select output_format (default '" << DEFAULT_OUTPUT_FORMAT << "'); available options are:\n";
  18. for(const auto& f : output_format::all()) {
  19. std::cerr << " " << f->name << std::endl;
  20. std::cerr << " " << f->get_description() << std::endl;
  21. }
  22. std::cerr << " -p <output_param> add a parameter to be passed to the output format generator" << std::endl;
  23. std::cerr << " -?, --help print this help and exit\n";
  24. }
  25. int main(int argc, char *argv[]) {
  26. int res = 0;
  27. pio_assembler pioasm;
  28. std::string format(DEFAULT_OUTPUT_FORMAT);
  29. const char *input = nullptr;
  30. const char *output = nullptr;
  31. std::vector<std::string> options;
  32. int i = 1;
  33. for (; !res && i < argc; i++) {
  34. if (argv[i][0] != '-') break;
  35. if (argv[i] == std::string("-o")) {
  36. if (++i < argc) {
  37. format = argv[i];
  38. } else {
  39. std::cerr << "error: -o requires format value" << std::endl;
  40. res = 1;
  41. }
  42. } else if (argv[i] == std::string("-p")) {
  43. if (++i < argc) {
  44. options.emplace_back(argv[i]);
  45. } else {
  46. std::cerr << "error: -p requires parameter value" << std::endl;
  47. res = 1;
  48. }
  49. } else if (argv[i] == std::string("-?") || argv[i] == std::string("--help")) {
  50. usage();
  51. return 1;
  52. } else {
  53. std::cerr << "error: unknown option " << argv[i] << std::endl;
  54. res = 1;
  55. }
  56. }
  57. if (!res) {
  58. if (i != argc) {
  59. input = argv[i++];
  60. } else {
  61. std::cerr << "error: expected input filename\n";
  62. res = 1;
  63. }
  64. }
  65. if (!res) {
  66. if (i != argc) {
  67. output = argv[i++];
  68. } else {
  69. output = "-";
  70. }
  71. }
  72. if (!res && i != argc) {
  73. std::cerr << "unexpected command line argument " << argv[i] << std::endl;
  74. res = 1;
  75. }
  76. std::shared_ptr<output_format> oformat;
  77. if (!res) {
  78. const auto& e = std::find_if(output_format::all().begin(), output_format::all().end(),
  79. [&](const std::shared_ptr<output_format> &f) {
  80. return f->name == format;
  81. });
  82. if (e == output_format::all().end()) {
  83. std::cerr << "error: unknown output format '" << format << "'" << std::endl;
  84. res = 1;
  85. } else {
  86. oformat = *e;
  87. }
  88. }
  89. if (res) {
  90. std::cerr << std::endl;
  91. usage();
  92. } else {
  93. res = pioasm.generate(oformat, input, output, options);
  94. }
  95. return res;
  96. }