From 74ac8bba248bffd62ae2f15915243c4615410321 Mon Sep 17 00:00:00 2001 From: Greg Davill Date: Sat, 22 Oct 2022 19:23:23 +1030 Subject: [PATCH 1/2] msys2: Fix absolute windows paths - Ensure path DATA_DIR isn't made absolute during compilation - Use cygpath to create absolute path at runtime --- CMakeLists.txt | 2 ++ scripts/msys2/PKGBUILD | 2 +- src/altera.cpp | 6 ++++++ src/pathHelper.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/pathHelper.hpp | 13 +++++++++++++ src/xilinx.cpp | 6 ++++++ 6 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/pathHelper.cpp create mode 100644 src/pathHelper.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b3fa611..e591800 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -111,6 +111,7 @@ set(OPENFPGALOADER_SOURCE src/jlink.cpp src/lattice.cpp src/progressBar.cpp + src/pathHelper.cpp src/fsparser.cpp src/mcsParser.cpp src/ftdispi.cpp @@ -138,6 +139,7 @@ set(OPENFPGALOADER_HEADERS src/ice40.hpp src/ihexParser.hpp src/progressBar.hpp + src/pathHelper.hpp src/rawParser.hpp src/usbBlaster.hpp src/bitparser.hpp diff --git a/scripts/msys2/PKGBUILD b/scripts/msys2/PKGBUILD index 1b4638c..51376e9 100644 --- a/scripts/msys2/PKGBUILD +++ b/scripts/msys2/PKGBUILD @@ -27,7 +27,7 @@ build() { -G "MSYS Makefiles" \ -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \ ../../../.. - cmake --build . + MSYS2_ARG_CONV_EXCL="-DDATA_DIR=" cmake --build . } check() { diff --git a/src/altera.cpp b/src/altera.cpp index 70d0d67..b9493ff 100644 --- a/src/altera.cpp +++ b/src/altera.cpp @@ -13,6 +13,7 @@ #include "device.hpp" #include "epcq.hpp" #include "progressBar.hpp" +#include "pathHelper.hpp" #include "rawParser.hpp" #define IDCODE 6 @@ -181,6 +182,11 @@ bool Altera::load_bridge() bitname += _device_package + ".rbf"; #endif +#if defined (_WIN64) || defined (_WIN32) + /* Convert relative path embedded at compile time to an absolute path */ + bitname = PathHelper::absolutePath(bitname); +#endif + std::cout << "use: " << bitname << std::endl; /* first: load spi over jtag */ diff --git a/src/pathHelper.cpp b/src/pathHelper.cpp new file mode 100644 index 0000000..301ddc7 --- /dev/null +++ b/src/pathHelper.cpp @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: Apache-2.0 +/* + * Copyright (C) 2022 Greg Davill + */ + +#if defined (_WIN64) || defined (_WIN32) +#include "pathHelper.hpp" + +#include +#include +#include +#include +#include + +std::string PathHelper::absolutePath(std::string input_path) { + + /* Attempt to execute cygpath */ + std::string cmd = std::string("cygpath -m " + input_path); + + std::array buffer; + std::string result; + std::unique_ptr pipe(popen(cmd.c_str(), "r"), pclose); + if (!pipe) { + /* If cygpath fails to run, return original path */ + return input_path; + } + while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { + result += buffer.data(); + } + + /* Trim trailing newline */ + static const std::regex tws{"[[:space:]]*$", std::regex_constants::extended}; + return std::regex_replace(result, tws, ""); +} + +#endif \ No newline at end of file diff --git a/src/pathHelper.hpp b/src/pathHelper.hpp new file mode 100644 index 0000000..203653f --- /dev/null +++ b/src/pathHelper.hpp @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: Apache-2.0 +/* + * Copyright (C) 2022 Greg Davill + */ + +#if defined (_WIN64) || defined (_WIN32) +#include + +class PathHelper{ + public: + static std::string absolutePath(std::string input_path); +}; +#endif diff --git a/src/xilinx.cpp b/src/xilinx.cpp index 580b3bd..4b5f01b 100644 --- a/src/xilinx.cpp +++ b/src/xilinx.cpp @@ -24,6 +24,7 @@ #include "xilinxMapParser.hpp" #include "part.hpp" #include "progressBar.hpp" +#include "pathHelper.hpp" Xilinx::Xilinx(Jtag *jtag, const std::string &filename, const std::string &file_type, @@ -323,6 +324,11 @@ bool Xilinx::load_bridge() std::string bitname = DATA_DIR "/openFPGALoader/spiOverJtag_"; bitname += _device_package + ".bit.gz"; +#if defined (_WIN64) || defined (_WIN32) + /* Convert relative path embedded at compile time to an absolute path */ + bitname = PathHelper::absolutePath(bitname); +#endif + std::cout << "use: " << bitname << std::endl; /* first: load spi over jtag */ From 53e72a944ab65de7555428515540e68de4e327ea Mon Sep 17 00:00:00 2001 From: Greg Davill Date: Sun, 23 Oct 2022 18:52:47 +1030 Subject: [PATCH 2/2] pathHelper: Only compile on win builds --- CMakeLists.txt | 5 +++-- src/altera.cpp | 4 +++- src/xilinx.cpp | 2 ++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e591800..4749127 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -111,7 +111,6 @@ set(OPENFPGALOADER_SOURCE src/jlink.cpp src/lattice.cpp src/progressBar.cpp - src/pathHelper.cpp src/fsparser.cpp src/mcsParser.cpp src/ftdispi.cpp @@ -139,7 +138,6 @@ set(OPENFPGALOADER_HEADERS src/ice40.hpp src/ihexParser.hpp src/progressBar.hpp - src/pathHelper.hpp src/rawParser.hpp src/usbBlaster.hpp src/bitparser.hpp @@ -198,6 +196,9 @@ target_link_libraries(openFPGALoader if (${CMAKE_SYSTEM_NAME} MATCHES "Windows") # winsock provides ntohs target_link_libraries(openFPGALoader ws2_32) + + target_sources(openFPGALoader PRIVATE src/pathHelper.cpp) + list(APPEND OPENFPGALOADER_HEADERS src/pathHelper.hpp) endif() # libusb_attach_kernel_driver is only available on Linux. diff --git a/src/altera.cpp b/src/altera.cpp index b9493ff..fdef8c3 100644 --- a/src/altera.cpp +++ b/src/altera.cpp @@ -13,8 +13,10 @@ #include "device.hpp" #include "epcq.hpp" #include "progressBar.hpp" -#include "pathHelper.hpp" #include "rawParser.hpp" +#if defined (_WIN64) || defined (_WIN32) +#include "pathHelper.hpp" +#endif #define IDCODE 6 #define USER0 0x0C diff --git a/src/xilinx.cpp b/src/xilinx.cpp index 4b5f01b..e67f53f 100644 --- a/src/xilinx.cpp +++ b/src/xilinx.cpp @@ -24,7 +24,9 @@ #include "xilinxMapParser.hpp" #include "part.hpp" #include "progressBar.hpp" +#if defined (_WIN64) || defined (_WIN32) #include "pathHelper.hpp" +#endif Xilinx::Xilinx(Jtag *jtag, const std::string &filename, const std::string &file_type,