replace argp by cxxopts

This commit is contained in:
Gwenhael Goavec-Merou 2020-07-23 18:32:18 +02:00
parent b6681c5430
commit 9c42aecef5
3 changed files with 105 additions and 121 deletions

View File

@ -35,15 +35,6 @@ if(ENABLE_UDEV)
endif() endif()
endif() endif()
# for non glibc, argp-standalone is required and must be
# explicitly linked. This code will fail for others libc
# implementations
include(CheckCXXSourceCompiles)
check_cxx_source_compiles("
#include <argp.h>
int main(int argc, char **argv) {
argp_parse(NULL, argc, argv, 0, 0, NULL);}" HAVE_ARGP)
set(OPENFPGALOADER_SOURCE set(OPENFPGALOADER_SOURCE
src/dirtyJtag.cpp src/dirtyJtag.cpp
src/spiFlash.cpp src/spiFlash.cpp
@ -72,6 +63,7 @@ set(OPENFPGALOADER_SOURCE
set(OPENFPGALOADER_HEADERS set(OPENFPGALOADER_HEADERS
src/altera.hpp src/altera.hpp
src/cxxopts.hpp
src/dirtyJtag.hpp src/dirtyJtag.hpp
src/progressBar.hpp src/progressBar.hpp
src/bitparser.hpp src/bitparser.hpp
@ -114,8 +106,7 @@ include_directories(
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
find_library(LIBFTDI1STATIC libftdi1.a REQUIRED) find_library(LIBFTDI1STATIC libftdi1.a REQUIRED)
find_library(LIBUSB1STATIC libusb-1.0.a REQUIRED) find_library(LIBUSB1STATIC libusb-1.0.a REQUIRED)
find_library(LIBARGPSTATIC libargp.a REQUIRED) target_link_libraries(openFPGALoader ${LIBFTDI1STATIC} ${LIBUSB1STATIC})
target_link_libraries(openFPGALoader ${LIBFTDI1STATIC} ${LIBUSB1STATIC} ${LIBARGPSTATIC})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework CoreFoundation -framework IOKit") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework CoreFoundation -framework IOKit")
link_directories(/usr/local/lib) link_directories(/usr/local/lib)
target_include_directories(openFPGALoader PRIVATE /usr/local/include) target_include_directories(openFPGALoader PRIVATE /usr/local/include)
@ -137,11 +128,6 @@ include_directories(${LIBUDEV_INCLUDE_DIRS})
target_link_libraries(openFPGALoader ${LIBUDEV_LIBRARIES}) target_link_libraries(openFPGALoader ${LIBUDEV_LIBRARIES})
endif() endif()
if(NOT HAVE_ARGP)
find_library(LIBARGPSTATIC libargp.a REQUIRED)
target_link_libraries(openFPGALoader ${LIBARGPSTATIC})
endif()
if (BUILD_STATIC) if (BUILD_STATIC)
set_target_properties(openFPGALoader PROPERTIES LINK_SEARCH_END_STATIC 1) set_target_properties(openFPGALoader PROPERTIES LINK_SEARCH_END_STATIC 1)
endif() endif()

View File

@ -49,9 +49,6 @@ node). If you don't want this option, use:
```-DENABLE_UDEV=OFF``` ```-DENABLE_UDEV=OFF```
For distributions using non-glibc (musl, uClibc) **argp-standalone** must be
installed.
And if not already done, install **pkg-config**, **make** and **g++**. And if not already done, install **pkg-config**, **make** and **g++**.
To build the app: To build the app:
@ -107,8 +104,7 @@ openFPGALoader -- a program to flash cyclone10 LP FPGA
-o, --offset=OFFSET start offset in EEPROM -o, --offset=OFFSET start offset in EEPROM
-r, --reset reset FPGA after operations -r, --reset reset FPGA after operations
-v, --verbose Produce verbose output -v, --verbose Produce verbose output
-?, --help Give this help list -h, --help Give this help list
--usage Give a short usage message
-V, --version Print program version -V, --version Print program version
``` ```

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <argp.h> #include "cxxopts.hpp"
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
@ -53,39 +53,8 @@ struct arguments {
bool is_list_command; bool is_list_command;
}; };
#define LIST_CABLE 1 int parse_opt(int argc, char **argv, struct arguments *args);
#define LIST_BOARD 2
#define LIST_FPGA 3
#define DETECT 4
#define FREQUENCY 5
const char *argp_program_version = "openFPGALoader v0.1";
const char *argp_program_bug_address = "<gwenhael.goavec-merou@trabucayre.com>";
static char doc[] = "openFPGALoader -- a program to flash FPGA";
static char args_doc[] = "BIT_FILE";
static error_t parse_opt(int key, char *arg, struct argp_state *state);
static struct argp_option options[] = {
{"cable", 'c', "CABLE", 0, "jtag interface"},
{"freq", FREQUENCY, "FREQ", 0, "jtag frequency (Hz)"},
{"list-cables", LIST_CABLE, 0, 0, "list all supported cables"},
{"board", 'b', "BOARD", 0, "board name, may be used instead of cable"},
{"list-boards", LIST_BOARD, 0, 0, "list all supported boards"},
#ifdef USE_UDEV
{"device", 'd', "DEVICE", 0, "device to use (/dev/ttyUSBx)"},
#endif
{"list-fpga", LIST_FPGA, 0, 0, "list all supported FPGA"},
{"detect", DETECT, 0, 0, "detect FPGA"},
{"write-flash", 'f', 0, 0,
"write bitstream in flash (default: false, only for Gowin and ECP5 devices)"},
{"write-sram", 'm', 0, 0,
"write bitstream in SRAM (default: true, only for Gowin and ECP5 devices)"},
{"offset", 'o', "OFFSET", 0, "start offset in EEPROM"},
{"verbose", 'v', 0, 0, "Produce verbose output"},
{"reset", 'r', 0, 0, "reset FPGA after operations"},
{0}
};
static struct argp argp = { options, parse_opt, args_doc, doc };
void displaySupported(const struct arguments &args); void displaySupported(const struct arguments &args);
int main(int argc, char **argv) int main(int argc, char **argv)
@ -97,7 +66,13 @@ int main(int argc, char **argv)
struct arguments args = {false, false, false, 0, "", "-", "-", 6000000, "-", struct arguments args = {false, false, false, 0, "", "-", "-", 6000000, "-",
false, false, false, false, true, false}; false, false, false, false, true, false};
/* parse arguments */ /* parse arguments */
argp_parse(&argp, argc, argv, 0, 0, &args); try {
if (parse_opt(argc, argv, &args))
return EXIT_SUCCESS;
} catch (std::exception &e) {
printError("Error in parse arg step");
return EXIT_FAILURE;
}
if (args.is_list_command) { if (args.is_list_command) {
displaySupported(args); displaySupported(args);
@ -240,80 +215,107 @@ static error_t parse_eng(string arg, double *dst) {
} }
/* arguments parser */ /* arguments parser */
static error_t parse_opt(int key, char *arg, struct argp_state *state) int parse_opt(int argc, char **argv, struct arguments *args)
{ {
struct arguments *arguments = (struct arguments *)state->input;
switch (key) { string freqo;
case 'f': try {
arguments->write_flash = true; cxxopts::Options options(argv[0], "openFPGALoader -- a program to flash FPGA",
arguments->write_sram = false; "<gwenhael.goavec-merou@trabucayre.com>");
break; options
case 'm': .positional_help("BIT_FILE")
arguments->write_sram = true; .show_positional_help();
break;
case 'r': options
arguments->reset = true; .add_options()
break; ("bitstream", "bitstream",
cxxopts::value<std::string>(args->bit_file))
("b,board", "board name, may be used instead of cable",
cxxopts::value<string>(args->board))
("c,cable", "jtag interface", cxxopts::value<string>(args->cable))
#ifdef USE_UDEV #ifdef USE_UDEV
case 'd': ("d,device", "device to use (/dev/ttyUSBx)",
arguments->device = arg; cxxopts::value<string>(args->device))
break;
#endif #endif
case 'v': ("detect", "detect FPGA",
arguments->verbose = true; cxxopts::value<bool>(args->detect))
break; ("freq", "jtag frequency (Hz)", cxxopts::value<string>(freqo))
case 'o': ("f,write-flash",
arguments->offset = strtoul(arg, NULL, 16); "write bitstream in flash (default: false, only for Gowin and ECP5 devices)")
break; ("list-boards", "list all supported boards",
case 'c': cxxopts::value<bool>(args->list_boards))
arguments->cable = arg; ("list-cables", "list all supported cables",
break; cxxopts::value<bool>(args->list_cables))
case 'b': ("list-fpga", "list all supported FPGA",
arguments->board = arg; cxxopts::value<bool>(args->list_fpga))
break; ("m,write-sram",
case FREQUENCY: "write bitstream in SRAM (default: true, only for Gowin and ECP5 devices)")
double freq; ("o,offset", "start offset in EEPROM",
if (parse_eng(string(arg), &freq)) { cxxopts::value<unsigned int>(args->offset))
cerr << "Error: invalid format for --freq" << endl; ("r,reset", "reset FPGA after operations",
exit(1); cxxopts::value<bool>(args->reset))
("v,verbose", "Produce verbose output", cxxopts::value<bool>(args->verbose))
("h,help", "Give this help list")
("V,Version", "Print program version");
options.parse_positional({"bitstream"});
auto result = options.parse(argc, argv);
if (result.count("help")) {
cout << options.help() << endl;
return 1;
} }
if (freq < 1) {
cerr << "Error: --freq must be positive" << endl; if (result.count("Version")) {
exit(1); cout << "openFPGALoader v0.1" << endl;
return 1;
} }
arguments->freq = freq;
break; if (result.count("write-flash") && result.count("write-sram")) {
case ARGP_KEY_ARG: printError("Error: both write to flash and write to ram enabled");
arguments->bit_file = arg; throw std::exception();
break;
case ARGP_KEY_END:
if (arguments->bit_file.empty() &&
!arguments->is_list_command &&
!arguments->detect &&
!arguments->reset) {
cout << "Error: bitfile not specified" << endl;
argp_usage(state);
} }
break;
case LIST_CABLE: if (result.count("write-flash")) {
arguments->list_cables = true; args->write_flash = true;
arguments->is_list_command = true; args->write_sram = false;
break; }
case LIST_BOARD:
arguments->list_boards = true; if (result.count("write-sram")) {
arguments->is_list_command = true; args->write_flash = false;
break; args->write_sram = true;
case LIST_FPGA: }
arguments->list_fpga = true;
arguments->is_list_command = true; if (result.count("freq")) {
break; double freq;
case DETECT: if (parse_eng(freqo, &freq)) {
arguments->detect = true; printError("Error: invalid format for --freq");
break; throw std::exception();
default: }
return ARGP_ERR_UNKNOWN; if (freq < 1) {
printError("Error: --freq must be positive");
throw std::exception();
}
args->freq = static_cast<uint32_t>(freq);
}
if (args->list_cables || args->list_boards || args->list_fpga)
args->is_list_command = true;
if (args->bit_file.empty() &&
!args->is_list_command &&
!args->detect &&
!args->reset) {
printError("Error: bitfile not specified");
cout << options.help() << endl;
throw std::exception();
}
} catch (const cxxopts::OptionException& e) {
cerr << "Error parsing options: " << e.what() << endl;
throw std::exception();
} }
return 0; return 0;
} }